Documentation
¶
Overview ¶
PromptContext registration and snapshots (M10), matching context-prompt. Static PromptSections form the system prefix; dynamic Context fragments append to user messages and may be added or removed. Change-only persistence retains the latest snapshot after compaction so context can be reconstructed.
Change-only PromptContext injection and compaction snapshots (N05, D4). ContextRegistry.Compute supplies a stable hash until state changes. ChangeOnlyInjector persists the last injected hash and appends new user messages only on change; it never mutates the system prompt. Plan, goal, and approval changes use this path. CompactPreserve retains the latest snapshot for rebuilding derived messages after compaction.
Strict system-prompt interpolation replaces complete {{name}} groups using variables with names matching [a-z][a-z0-9_]*. Unknown or missing values fail. Replacement values are not rescanned; an unmatched {{ remains literal text.
Tool ordering follows upstream orderTools. Explicit names follow configuration; unlisted names are inserted alphabetically at ToolOrderRest. Configuration must contain exactly one rest marker, no duplicates, and no unknown tool names.
Static system-prompt checks support N03 and D2, matching upstream sections. Sections must render identical bytes for identical inputs, without time, cwd, randomness, or dynamic environment values. StaticCheck detects known patterns that break prefix caching; a clean result has no violations. SystemHash compares assembled prompts across turns to verify stability for the same configuration.
Package sysprompt registers and orders system-prompt sections, matching core/system-prompt. Sections have names, fixed orders, and text. Assembler joins them in ascending order: persona, policy, runtime context, plan policy, tool schemas, then skill catalog. Order constants avoid runtime-dependent ordering that could change model behavior.
Index ¶
- Constants
- func CheckSections(a *Assembler) (violations []string, forbidden bool)
- func Interpolate(text string, variables map[string]string) (string, error)
- func IsPure(sectionText string) bool
- func OrderTools(names, configured []string) ([]string, error)
- func StaticCheck(text string) []string
- func StripBanned(s string) string
- func SystemHash(sys string) string
- func ToolsSectionText(toolList []*tools.Tool) (string, error)
- type Assembler
- type ChangeOnlyInjector
- type CompactPreserve
- type ContextRegistry
- func (r *ContextRegistry) Compute() (text string, hash string)
- func (r *ContextRegistry) LastHash() string
- func (r *ContextRegistry) Register(name string, order int, content string) bool
- func (r *ContextRegistry) SetLastHash(h string)
- func (r *ContextRegistry) Snapshot() map[string]string
- func (r *ContextRegistry) Unregister(name string)
- type GoalRoundContext
- type RuntimeContext
- type Section
Constants ¶
const ( // SectionOrderPersona orders the persona. SectionOrderPersona = 100 // SectionOrderPolicy orders general policy. SectionOrderPolicy = 200 // SectionOrderRuntimeContext orders runtime context snapshots. SectionOrderRuntimeContext = 300 // SectionOrderPlanPolicy orders policy injected only in plan mode. SectionOrderPlanPolicy = 500 // SectionOrderToolsSchema orders tool JSON schemas. SectionOrderToolsSchema = 600 // SectionOrderSkillCatalog orders the stable, change-only skill catalog (D3). SectionOrderSkillCatalog = 700 )
Section ordering values match upstream.
const ClearedContextText = "Current runtime context: none. Earlier runtime-context snapshots no longer apply."
MightInject returns text only when the computed hash changes. When content becomes empty, ClearedContextText explicitly tells the model that prior runtime context no longer applies instead of sending an uninformative empty message.
const ToolOrderRest = "<unlisted-tools>"
ToolOrderRest is the reserved marker for remaining tools.
Variables ¶
This section is empty.
Functions ¶
func CheckSections ¶
CheckSections checks all registered sections; forbidden is true if any cache-breaking violation is detected.
func Interpolate ¶
Interpolate performs strict variable interpolation on text.
func OrderTools ¶
OrderTools orders names according to configured.
func StaticCheck ¶
StaticCheck returns descriptions of matched forbidden patterns, or none.
func StripBanned ¶
StripBanned replaces common dynamic calls with static placeholders for test examples.
func SystemHash ¶
SystemHash returns the assembled prompt's stable SHA-256 digest.
Types ¶
type Assembler ¶
type Assembler struct {
// contains filtered or unexported fields
}
Assembler registers sections and assembles them in order.
func (*Assembler) Assemble ¶
Assemble joins sections by ascending order with one blank line between them.
func (*Assembler) AssembleHash ¶
AssembleHash hashes the assembler's current prompt.
func (*Assembler) Unregister ¶
Unregister removes a section.
type ChangeOnlyInjector ¶
type ChangeOnlyInjector struct {
// contains filtered or unexported fields
}
ChangeOnlyInjector records the last injected context hash and injects again only when Compute returns a different hash.
func NewChangeOnlyInjector ¶
func NewChangeOnlyInjector(reg *ContextRegistry, persistedHash string) *ChangeOnlyInjector
NewChangeOnlyInjector restores an optional persisted lastHash across turns.
func (*ChangeOnlyInjector) InjectCount ¶
func (in *ChangeOnlyInjector) InjectCount() int
InjectCount returns the cumulative injection count.
func (*ChangeOnlyInjector) LastHash ¶
func (in *ChangeOnlyInjector) LastHash() string
LastHash returns the last injected hash for persistence and restoration.
func (*ChangeOnlyInjector) MightInject ¶
func (in *ChangeOnlyInjector) MightInject() (text string, injected bool)
type CompactPreserve ¶
type CompactPreserve struct {
// contains filtered or unexported fields
}
CompactPreserve retains the latest context snapshot so user-message context can be restored after compaction removes older nodes.
func (*CompactPreserve) Latest ¶
func (c *CompactPreserve) Latest() (text, hash string, ok bool)
Latest returns the last snapshot, including after compaction.
func (*CompactPreserve) Record ¶
func (c *CompactPreserve) Record(text, hash string)
Record replaces the retained snapshot with the latest computed value.
type ContextRegistry ¶
type ContextRegistry struct {
// contains filtered or unexported fields
}
ContextRegistry registers PromptContext fragments and hashes snapshots.
func NewContextRegistry ¶
func NewContextRegistry() *ContextRegistry
NewContextRegistry creates a context registry.
func (*ContextRegistry) Compute ¶
func (r *ContextRegistry) Compute() (text string, hash string)
Compute concatenates fragments deterministically in ascending order and hashes them.
func (*ContextRegistry) LastHash ¶
func (r *ContextRegistry) LastHash() string
LastHash returns the last Compute hash for change-only injection checks.
func (*ContextRegistry) Register ¶
func (r *ContextRegistry) Register(name string, order int, content string) bool
Register adds or updates a fragment and reports whether its hash changed.
func (*ContextRegistry) SetLastHash ¶
func (r *ContextRegistry) SetLastHash(h string)
SetLastHash records the injected hash to prevent duplicate injection.
func (*ContextRegistry) Snapshot ¶
func (r *ContextRegistry) Snapshot() map[string]string
Snapshot returns a stable fragment snapshot for change-only persistence.
func (*ContextRegistry) Unregister ¶
func (r *ContextRegistry) Unregister(name string)
Unregister removes a context fragment.
type GoalRoundContext ¶
type GoalRoundContext struct {
// Active reports whether the goal is active.
Active bool
// Round is the current round.
Round int
// GoalDesc describes the goal.
GoalDesc string
}
GoalRoundContext supplies active-goal continuation context and stops after completion.
func (*GoalRoundContext) Render ¶
func (g *GoalRoundContext) Render() string
Render returns continuation guidance only for active goals.
type RuntimeContext ¶
type RuntimeContext struct {
// Name uniquely identifies the fragment.
Name string
// Order determines concatenation order at the end of the user message.
Order int
// Content is the latest dynamic snapshot supplied by the caller.
Content string
}
RuntimeContext is a dynamic fragment whose registration or removal takes effect immediately.
type Section ¶
type Section struct {
// Name uniquely identifies a section for registration, removal, and debugging.
Name string `json:"name"`
// Order is the fixed ordering value supplied from constants.
Order int `json:"order"`
// Text contains the block content.
Text string `json:"text"`
}
Section is a system-prompt block.