DSHarness Systems Dissection Pinned baseline 47f943859b · 36 verified / 0 in progress / 36 chapters
中文
Core Runtime·Chapter 07

Prompt Assembly and Context Injection

Section registry, ordering, scope, tool schemas, and dynamic context

VerifiedUpstream 47f943859bScope: Analyze the system-prompt registry, section ordering, per-step rebuilds, agent.inject, and context plugins.

Conclusion: the prompt is a per-step, log-reconstructable program

DeepSeek Harness does not build one system string at session creation. For every proposed model step it re-evaluates scoped prompt providers, canonicalizes visible tool schemas, projects volatile runtime facts, admits sourced context messages, and records the effective request state before model I/O.

This architecture serves three goals at once: per-Agent composition, deterministic replay, and provider prefix-cache stability. It deliberately separates stable instructions from volatile facts rather than calling every model-visible input a “system prompt.”

1. Four model-input lanes with different semantics

LaneRepresentationDurable formTypical contentChange behavior
Stable instructionsPromptSection[]request/header.systemHarness identity, persona, plan policy, tool usage guidanceRe-render every step; write a new full header only when the value changes
Tool interfaceToolSchema[]request/header.toolsNames, descriptions, JSON parameters visible in this Agent scopeCanonicalize every step; compare in order
Volatile runtime factsPromptContext[]Sourced user/message snapshotSandbox mode, approval policy, delegation statusAppend only when the complete rendered snapshot changes
Context pluginsagent/pre-step messagesSourced user/messageWorkspace instructions, current time, tmux location, explicit session referencesEach plugin owns refresh, ordering, bounds, and provenance
Architectural invariant

All four lanes become reconstructable before provider dispatch: stable system/tool state is captured in a request-header event, while message-shaped context enters the session surface. The Loop's invariant rebuilds both sides from the log. ROOT-MODEL-LOGGEDLOOP-REQUEST-INVARIANT

2. The registry has five reversible contribution types

Production API

SystemPrompt effect-registers sections, contexts, runtime-context suppressors, tool-schema providers, and variables. Registration and disposal emit system-prompt/change; duplicate named entries are rejected within one layer. The built-in identity at order -100 and deployment persona at order 0 are ordinary sections, not hidden string concatenation. PROMPT-REGISTRY-CONTRACT

RegistrationIdentityEvaluationTeardown
section({name, order, text, complete?})Name unique per layerText provider once per assembly; interpolation laterExact entry removed with owner effect
context({name, order, text})Name unique per layerSkipped entirely when runtime context is suppressedExact entry removed
suppressRuntimeContext()Anonymous independent tokenAny active suppressor in the scope chain winsOnly that suppressor is removed
tools(provider)Anonymous provider membershipReturns visible schemas and optional pre-restriction known namesProvider removed
variable(name, provider)Strict lower-case identifierResolved once per assembly; may yield undefinedExact name removed

The default Agent Loop contributes three variables—provider, model, and cwd—from the Agent and its Session. This is an ownership choice: templates reference values, while the runtime objects remain their source.

3. Scope shadowing occurs before provider evaluation

Scope semantics

The assembly context sets both agent and scope to the same Agent. Global contributions are overlaid by the farthest ancestor and finally by the nearest scope; the nearest same-name section, context, or variable wins. DESIGN-SCOPED-LAYERSPROMPT-STEP-BOUNDARY

Test-defined behavior

A scoped deployment:persona replaces the global persona only for that scope. More importantly, the shadowed global text provider is never evaluated—the effective map is formed before text execution. PROMPT-SCOPE-SHADOW

Mechanism-level deduction

Shadowing is not post-render string replacement. It is provider selection. A child Agent can inherit its parent's composition and override one named slot without executing the displaced provider or copying the entire prompt.

4. One assembly has a precise evaluation order

1

Resolve scope and suppression

Collect the existing scope chain and determine whether any global or inherited suppressor is active.

2

Evaluate variables

Global first, then ancestor-to-nearest scope; a nearer provider overwrites the same name.

3

Materialize named entries

Build effective section and context maps before text providers run.

4

Snapshot tool providers

Freeze membership for this generation, invoke each provider, and clone schema parameters.

5

Order and evaluate

Stable-sort sections/contexts by numeric order, enforce at most one complete section, evaluate text, and canonicalize tools.

6

Run expert waterfall

system-prompt/assemble may cooperatively rewrite the mutable assembly or short-circuit it.

7

Reapply hard constraints

Restore the original evaluated complete section as the sole section and/or clear contexts when suppression is active.

Production control flow

This order is implemented directly in assemble(). Registry output is deterministic before the waterfall; the returned waterfall value is authoritative except for complete-section and suppression enforcement. PROMPT-ASSEMBLY-PIPELINE

Mutation tests

Tool-provider membership is snapshotted, so a provider registered during provider evaluation appears only in the next assembly. Variables use live map iteration, so a variable registered by an earlier variable provider may appear in the same generation. Every assembly receives fresh section/context/tool containers, and tool parameters are structurally cloned. PROMPT-ASSEMBLY-MUTATION-TESTPROMPT-VARIABLE-TEST

5. complete narrows sections, not the entire assembly

Exact semantics

One effective complete: true section does not skip variable, context, tool, or waterfall work. The waterfall still runs; afterward the registry restores the originally evaluated complete section as the sole section. Two effective complete sections make assembly fail. PROMPT-ASSEMBLY-PIPELINEPROMPT-ASSEMBLY-MUTATION-TEST

  • A listener cannot replace the complete section's name or evaluated text.
  • A listener can still alter variables, contexts, and tools.
  • Because interpolation happens later, changing a referenced variable can still change the final rendered complete template.
  • Runtime-context suppression clears contexts even if a waterfall re-adds them.
Design assessment

complete is a section-authority mechanism, not an unmodifiable whole-request mode. That preserves tool and context composition, but callers requiring a byte-fixed complete prompt must also control the variables it references.

6. Rendering is strict and intentionally late

Renderer contract

Section and context providers return uninterpolated text. Rendering accepts only {{name}} where the name matches [a-z][a-z0-9_]*; unknown, undefined, and malformed references fail loudly. Own-property lookup blocks prototype names unless explicitly registered, replacement values are not scanned again, empty entries disappear, and sections join with blank lines. PROMPT-STRICT-RENDERPROMPT-VARIABLE-TEST

Failure pointWhat has already been loggedWhat never happens
Assembly/provider/tool-order failureturn/startNo step/start, request header, or adapter call
Runtime-context interpolation failureturn/startNo step opens
agent/pre-step rejectsturn/start; claimed inbox splice is already durableNo step or model request; Turn ends blocked
Initial accepted message list is emptyTurn boundary and claim effectsNo step; Turn ends completed
System-section interpolation failureturn/start, step/start, entered user messagesNo request dispatch; step/end still closes in finally
Cancellation tests

Cancellation or owner disposal while assembly is blocked produces an aborted Turn with no step, chunks, assistant message, or adapter request. PROMPT-CANCEL-TEST

7. Tool schemas are part of prompt determinism

Ordering algorithm

Without explicit configuration, schemas use locale-independent code-unit lexicographic order. An explicit toolOrder must contain exactly one <unlisted-tools> marker and no duplicates. Unknown configured names fail; names known before scope restriction may be absent; unlisted visible tools sort within the marker position. PROMPT-TOOL-ORDER

Schema visibility and usage guidance are distinct registrations. Hiding a tool schema does not automatically remove an independently contributed section that discusses that tool. A package requiring those to move together must make its guidance provider inspect current scope visibility.

Design assessment

Canonical order is simultaneously a correctness, replay, and cache concern. The registry guarantees order before the expert waterfall; a listener that adds or reorders tools afterward owns the resulting determinism. Equal-name schemas are not silently deduplicated at this layer.

8. Assembly frequency: per proposed step, not per retry

Step boundary

preStep() claims inbox input, assembles the scoped prompt, renders dynamic context, proposes a snapshot, and then enters agent/pre-step. Only an accepted, non-empty decision opens step/start and carries that assembly into step(). PROMPT-STEP-BOUNDARY

Retry boundary

step() renders the system string once before its provider-retry loop. A retry rebuilds request config, folded header, and durable messages in the same numbered Turn and Step, but reuses that Step's system and tool assembly. PROMPT-RETRY-BOUNDARYLOOP-REQUEST-ERROR-TEST

EventReassemble?Why
Tool result requires another model requestYesIt is a new proposed Step; scoped providers and runtime facts may have changed
Next user TurnYesEvery proposed Step evaluates current composition
Provider error with retry actionNoRetry is another request attempt inside the same Step
Prompt registration changes after assemblyNext Step onlyThe current Step already owns a detached assembly snapshot
Rejected or empty proposed StepAlready assembledAdmission is decided after assembly and dynamic-context projection

9. Dynamic runtime context is an append-only full snapshot

Projection state machine

Runtime context renders named sections under a fixed “supersedes earlier snapshots” preamble. RuntimeContextProjection restores the newest owned snapshot still retained in the Session surface, proposes a sourced user message only when the complete value changes, emits an explicit clear marker when context disappears, and forgets a snapshot when a replacement removes it from the surface. LOOP-RUNTIME-CONTEXT

1

No prior value + empty current value

Emit nothing; there is no old semantic state to clear.

2

Same retained text

Emit nothing; history already carries the effective snapshot.

3

Changed non-empty value

Propose one full user-role snapshot with plugin and named-section provenance.

4

Value becomes empty

Propose a clear tombstone so an older snapshot no longer remains semantically active.

5

Compaction replaces retained snapshot

Reset retained state; the next Step reasserts current context even if its text did not change.

The candidate is not self-committing. It is appended only if the final agent/pre-step decision enters it. A listener may remove, rewrite, reorder, or reject it; if removed, projection state does not falsely advance and the next Step can propose it again.

10. Other context plugins use the pre-step lane deliberately

PluginRefresh/admission rulePlacement and provenance
Agent instructionsComposes workspace files and tracks tool-touched paths; preserves pending context when an initial Step will not runAfter the claimed batch and before driver-added runtime context PROMPT-CONTEXT-AGENT-INSTRUCTIONS
Time contextPer Step unless a configured refresh interval still covers the last injection; resolves browser or fallback timezoneAppends a sourced snapshot after downstream pre-step decisions PROMPT-CONTEXT-TIME
Tmux contextStep 1 only; requires shell, refresh due, successful location query, and changed statePrepends its sourced snapshot to the decision PROMPT-CONTEXT-TMUX
Session referenceExplicit host preparation; snapshots bounded referenced Session surfaces in mention orderReturns one aggregated recall message for the caller to enqueue
Design assessment

These inputs are not mislabeled system sections because their refresh, message position, source attribution, and retention rules differ. The pre-step waterfall is powerful but distributed: final message order depends on listener nesting and each plugin's before/after-next() behavior.

11. Request headers make the stable half replayable

Canonical header

The header holds resolved call config, which fields came from adapter defaults, rendered system text, and ordered tool schemas. Empty system/tool collections canonicalize to absent fields; equality compares config, adapter-default markers, system bytes, and schemas in order. Offline replay folds the latest full snapshot. PROMPT-HEADER-CANONICAL

Write policy

The first request writes initial or resume. Later requests write a full change snapshot only when canonical equality fails. Tests show that per-Step reassembly of an unchanged prompt produces no extra header, while a real section change does. LOOP-REQUEST-BUILDPROMPT-HEADER-CHANGE-TEST

The frozen provider request is then formed from the current header plus Session.deriveMessages(). This avoids a second hidden conversation store: both stable instructions and message history have durable anchors.

12. KV-cache reuse is emergent, not a Harness cache

Wire layout

The direct DeepSeek adapter serializes system text as the first wire message and then appends the derived history. It always requests streaming usage; this path does not set an explicit cache-control directive. PROMPT-DEEPSEEK-WIRE

Mechanism-level deduction

Cache reuse follows from a byte-stable prefix: stable system/config/tools, append-only message history, and volatile facts appended as user-role snapshots rather than rewriting the beginning. DeepSeek Harness does not maintain its own KV cache in this path.

Unexecuted real-API test

A key-gated upstream E2E expects every request after the first to report positive provider cache-read tokens across a tool round trip and a later Turn. Dependencies and credentials were not installed for this study, so this is a reviewed test contract—not a locally observed result. PROMPT-CACHE-E2E

13. Strengths, costs, and unresolved edges

ChoiceBenefitCost or edge
Reassemble every proposed StepNo stale change-driven cache; current scope is always evaluatedEvery Step pays provider and assembly work
Full header snapshotsSimple, local reconstruction and equalityLarger log entries than deltas when a large tool catalog changes
Append volatile contextPreserves stable provider-cache prefixes and provenanceSnapshots accumulate until surface replacement/compaction
Strict variables/tool orderTypos fail before a malformed request reaches the modelSome configuration errors appear only at first assembly
Expert waterfallMaximum composability without Loop editsPost-waterfall ordering, uniqueness, and consistency belong to listeners
Stable numeric section orderSimple deterministic ordering across distinct valuesEqual orders fall back to registration order, which is composition-sensitive
Per-Step assembly, per-attempt request buildRetry sees newly durable history/config while preserving Step semanticsPrompt changes during a retry wait for the next Step
Chapter assessment

The strongest idea is not prompt templating but semantic placement. Stable policy belongs in a reconstructable header; volatile policy belongs in sourced snapshots; situational context belongs in independently governed messages. Replay and cache behavior become consequences of the same data model rather than separate optimizations.

Chapter verification checklist

  • Traced all five registry contribution APIs and their scoped effect ownership.
  • Reconstructed the exact assembly order, including membership snapshots and live variable iteration.
  • Verified scope shadowing occurs before displaced provider evaluation.
  • Separated section, tool-schema, runtime-snapshot, and pre-step message lanes.
  • Verified the proposed-Step boundary and the same-Step provider-retry boundary.
  • Traced strict rendering failures to their exact durable Turn/Step outcomes.
  • Verified header canonicalization, change suppression, request freezing, and log reconstruction.
  • Reviewed—but did not claim execution of—the key-gated DeepSeek cache E2E.

My Learning Notes

Autosaved only in this browser. Nothing is uploaded or committed. Export Markdown whenever you want to keep a copy.