Skip to content

Stabilize prompt prefix: move volatile context to the user turn - #118

Merged
IchenDEV merged 1 commit into
mainfrom
t3code/stabilize-model-prompt-prefix
Sep 30, 2026
Merged

IchenDEV merged 1 commit into
mainfrom
t3code/stabilize-model-prompt-prefix

Conversation

@IchenDEV

Copy link
Copy Markdown
Owner

Summary

Every dictation currently rebuilds one opaque system prompt that mixes byte-stable instructions with per-request context, and LLMEngine creates a fresh ChatSession per call. No prefix is ever reusable, so each request re-prefills the full system prompt.

This change makes the instruction prefix byte-stable:

  • Add PromptAssembly { stablePrefix, volatileContext, userPrompt(containing:) }.
  • PromptBuilder gains buildFormattingAssembly / buildCommandAssembly; the existing buildSystemPrompt / buildCommandSystemPrompt delegate to them and return the identical concatenated string.
  • TextProcessor gains personalContextSections and formattingAssembly / commandAssembly; processing + personal context (screen, memory, input target, timestamp, dictionary, lexicon, edit rules) move to the volatile tail.
  • Generation call sites (main formatting, command, selection edit, edit-command resolver) now pass stablePrefix as the system prompt and volatileContext + userContent as the user turn. Translation already had no volatile system context.
  • Legacy builders used by tests keep their signatures and output.

Local MLX KV reuse and remote cache_control are deferred to follow-up changes; the mlx-swift-lm 3.31.4 prefix-cache spike is recorded in the SDLC artifact.

Testing

  • swift build — pass
  • swift test — 798 passed, 18 skipped, 0 failures (+1 Swift Testing suite)
  • swift test --filter StablePromptPrefixTests — 4 passed
  • bash scripts/sdlc-checks.sh — pass
  • bash scripts/ci-basic-checks.sh — pass
  • New tests assert: the assembly reproduces the legacy prompt; the stable prefix is byte-identical across differing volatile inputs; the runtime timestamp is absent from the prefix; personal context moves to the user turn.

Environment note: the ambient toolchain is CommandLineTools (no XCTest); tests were run with DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer swift test.

SDLC

Bundle: docs/sdlc/changes/2026-10-01-stable-prompt-prefix-cache/ (intent/spec/plan approved; verification pending approval).

Residual risk

Moving volatile context from the system role to the user turn preserves model-visible content and order, but models can weight roles differently; real-model manual QA on the main and command paths is still required before merge. The performance outcome (faster prefill) is not realized until the deferred cache work lands.

Split assembled prompts into a byte-stable, settings-only instruction
prefix and a volatile context tail (screen, memory, input target, time,
personal dictionary/lexicon/edit rules). The volatile tail now travels in
the user turn so the prefix is identical across requests, enabling future
prompt/KV-cache reuse and provider-side prefix caching.

Legacy builders (buildSystemPrompt, buildCommandSystemPrompt,
formattingSystemPrompt, commandSystemPrompt, systemPromptWithPersonalContext,
selectionEditSystemPromptWithPersonalContext) keep their signatures and
concatenated output, so existing behavior and tests are preserved.

Local MLX KV reuse and remote cache_control are deferred to follow-up
changes; the mlx-swift-lm prefix-cache spike is recorded in the SDLC
verification artifact.
@IchenDEV
IchenDEV merged commit 9f70733 into main Sep 30, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant