diff --git a/runtime/cache_stability_test.go b/runtime/cache_stability_test.go index bd8e64e..9374529 100644 --- a/runtime/cache_stability_test.go +++ b/runtime/cache_stability_test.go @@ -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) } diff --git a/runtime/context.go b/runtime/context.go index 8e4d499..acf351b 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -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) } diff --git a/runtime/context_test.go b/runtime/context_test.go index 8adba21..693bab7 100644 --- a/runtime/context_test.go +++ b/runtime/context_test.go @@ -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 diff --git a/runtime/runtime.go b/runtime/runtime.go index db28aca..61de67c 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -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{ diff --git a/runtime/runturn.go b/runtime/runturn.go index c28b6d6..55dc3cf 100644 --- a/runtime/runturn.go +++ b/runtime/runturn.go @@ -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,