Add Runtime.DynamicIdentityHint, a live, per-turn identity fact
Runtime.StaticSystemPrompt is cached and reused verbatim every turn — right for anything that doesn't change mid-conversation, wrong for a fact a caller needs to update live (e.g. after a runtime model switch) and have the model actually notice. A model tends to just repeat what it already told the user earlier in the same conversation over a background instruction it read once near the top of a large cached block, so updating only the static prompt after a switch left the model insisting it was still whatever it started as. DynamicIdentityHint rides the same per-turn (uncached) system prompt slot Run/RunTurn already use for the date line and KG hint, so setting it takes effect on the very next turn with the same recency the model grants its own last statement.
This commit is contained in:
1 parent
f624e4c7bd
commit
0ccfdb2a24
5 files changed
+41
-14
No files matched your search
@@ -443,8 +443,8 @@ func TestRuntimeDynamicSuffixIncludesDate(t *testing.T) {
|
||||
// this proves the date is stable across turns of a single Run.
|
||||
func TestRuntimeDateLineComputedOncePerRun(t *testing.T) {
|
||||
dl := "Today's date is 2026-05-01."
|
||||
a := buildDynamicSystemPromptSuffix(dl, "")
|
||||
b := buildDynamicSystemPromptSuffix(dl, "")
|
||||
a := buildDynamicSystemPromptSuffix("", dl, "")
|
||||
b := buildDynamicSystemPromptSuffix("", dl, "")
|
||||
require.Equal(t, a, b)
|
||||
}
|
||||
|
||||
|
||||
+10
-5
@@ -142,18 +142,23 @@ func BuildStaticSystemPrompt(
|
||||
}
|
||||
|
||||
// buildDynamicSystemPromptSuffix concatenates the per-turn dynamic context
|
||||
// — the date line and the cortex hint — into a single string the runtime
|
||||
// sends as the second (un-cached) SystemPromptPart. The date line, when
|
||||
// non-empty, appears at the top so the model anchors on "today" before
|
||||
// the per-turn content. Returns "" when all inputs are empty/nil.
|
||||
// — the identity hint, date line, and cortex hint — into a single string
|
||||
// the runtime sends as the second (un-cached) SystemPromptPart. identityHint
|
||||
// leads (see Runtime.DynamicIdentityHint: it needs to outweigh the model's
|
||||
// own prior in-conversation statements, so it gets the most prominent
|
||||
// position), then the date line so the model anchors on "today" before the
|
||||
// per-turn content. Returns "" when all inputs are empty/nil.
|
||||
//
|
||||
// As of sub-project 5, skill bodies and memory entries are no longer
|
||||
// auto-injected here. Their indices live in the cached static prompt
|
||||
// and the agent loads bodies on demand via the load_skill / load_memory
|
||||
// tools. This trims 5–15 KB of speculative skill bodies and 5–10 KB of
|
||||
// speculative memory bodies from every prefill.
|
||||
func buildDynamicSystemPromptSuffix(dateLine, cortexContext string) string {
|
||||
func buildDynamicSystemPromptSuffix(identityHint, dateLine, cortexContext string) string {
|
||||
var sb strings.Builder
|
||||
if identityHint != "" {
|
||||
sb.WriteString(identityHint)
|
||||
}
|
||||
if dateLine != "" {
|
||||
sb.WriteString(dateLine)
|
||||
}
|
||||
|
||||
+12
-4
@@ -85,17 +85,17 @@ func TestBuildStaticSystemPromptIncludesMemoryFiles(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuildDynamicSystemPromptSuffixEmpty(t *testing.T) {
|
||||
got := buildDynamicSystemPromptSuffix("", "")
|
||||
got := buildDynamicSystemPromptSuffix("", "", "")
|
||||
require.Equal(t, "", got)
|
||||
}
|
||||
|
||||
func TestBuildDynamicSystemPromptSuffixHintOnly(t *testing.T) {
|
||||
got := buildDynamicSystemPromptSuffix("", "\n\nHINT")
|
||||
got := buildDynamicSystemPromptSuffix("", "", "\n\nHINT")
|
||||
require.Equal(t, "\n\nHINT", got)
|
||||
}
|
||||
|
||||
func TestBuildDynamicSystemPromptSuffixDateAndHint(t *testing.T) {
|
||||
got := buildDynamicSystemPromptSuffix("Today's date is 2026-05-01.", "\n\nKG_HINT")
|
||||
got := buildDynamicSystemPromptSuffix("", "Today's date is 2026-05-01.", "\n\nKG_HINT")
|
||||
dateIdx := strings.Index(got, "Today's date is")
|
||||
hintIdx := strings.Index(got, "KG_HINT")
|
||||
require.True(t, dateIdx >= 0 && hintIdx > dateIdx,
|
||||
@@ -103,11 +103,19 @@ func TestBuildDynamicSystemPromptSuffixDateAndHint(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuildDynamicSystemPromptSuffixIncludesDate(t *testing.T) {
|
||||
got := buildDynamicSystemPromptSuffix("Today's date is 2026-05-01.", "")
|
||||
got := buildDynamicSystemPromptSuffix("", "Today's date is 2026-05-01.", "")
|
||||
require.True(t, strings.HasPrefix(got, "Today's date is 2026-05-01."),
|
||||
"date line must appear at the top of the dynamic suffix")
|
||||
}
|
||||
|
||||
func TestBuildDynamicSystemPromptSuffixIdentityHintLeadsDate(t *testing.T) {
|
||||
got := buildDynamicSystemPromptSuffix("IDENTITY_HINT", "Today's date is 2026-05-01.", "\n\nKG_HINT")
|
||||
identityIdx := strings.Index(got, "IDENTITY_HINT")
|
||||
dateIdx := strings.Index(got, "Today's date is")
|
||||
require.True(t, identityIdx == 0 && dateIdx > identityIdx,
|
||||
"order must be identity hint < date; got %d %d", identityIdx, dateIdx)
|
||||
}
|
||||
|
||||
func TestFormatDateLine(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
|
||||
+14
-1
@@ -85,6 +85,19 @@ type Runtime struct {
|
||||
// prompt cache hits.
|
||||
StaticSystemPrompt string
|
||||
|
||||
// DynamicIdentityHint is caller-set text re-sent, uncached, in every
|
||||
// turn's system prompt (see buildDynamicSystemPromptSuffix) — unlike
|
||||
// StaticSystemPrompt, safe to mutate mid-conversation (e.g. after a
|
||||
// live model switch) and have the very next turn pick it up. Exists
|
||||
// because a fact baked only into the cached static prompt competes
|
||||
// poorly against the model's own prior statements already visible in
|
||||
// message history: asked "which model are you" a second time, a model
|
||||
// tends to just repeat what it told the user earlier in the same
|
||||
// conversation over a background instruction it read once at the top
|
||||
// of a large cached block. Resending the fact fresh, every turn, gives
|
||||
// it the same recency the model grants its own last answer.
|
||||
DynamicIdentityHint string
|
||||
|
||||
// Permission gates tool execution at dispatch time. nil → allow-all.
|
||||
Permission tool.PermissionChecker
|
||||
|
||||
@@ -437,7 +450,7 @@ func (r *Runtime) Run(ctx context.Context, userMsg string, images []llm.ImageCon
|
||||
kgCh = nil
|
||||
}
|
||||
|
||||
dynamicSuffix := buildDynamicSystemPromptSuffix(dateLine, kgContext)
|
||||
dynamicSuffix := buildDynamicSystemPromptSuffix(r.DynamicIdentityHint, dateLine, kgContext)
|
||||
|
||||
staticText := r.StaticSystemPrompt
|
||||
parts := []llm.SystemPromptPart{
|
||||
|
||||
+3
-2
@@ -84,9 +84,10 @@ func (r *Runtime) RunTurn(ctx context.Context, userMsg string, images []llm.Imag
|
||||
cancel()
|
||||
}
|
||||
|
||||
dynamicSuffix := buildDynamicSystemPromptSuffix(r.DynamicIdentityHint, "", kgHint)
|
||||
parts := []llm.SystemPromptPart{{Text: r.StaticSystemPrompt, Cache: true}}
|
||||
if kgHint != "" {
|
||||
parts = append(parts, llm.SystemPromptPart{Text: kgHint, Cache: false})
|
||||
if dynamicSuffix != "" {
|
||||
parts = append(parts, llm.SystemPromptPart{Text: dynamicSuffix, Cache: false})
|
||||
}
|
||||
req := llm.ChatRequest{
|
||||
Model: r.Model,
|
||||
|
||||
Reference in new issue
Block a user