# How Memory v5 Compiles Execution Traces into a Compact Prompt Format in DeepSeek-Reasonix

> Discover how Memory v5 compiles execution traces into a compact prompt format. Learn about canonicalization, projection, and serialization for efficient prompt engineering in DeepSeek-Reasonix.

- Repository: [YHH/DeepSeek-Reasonix](https://github.com/esengine/DeepSeek-Reasonix)
- Tags: internals
- Published: 2026-06-27

---

**Memory v5 compiles execution traces into a compact prompt format by canonicalizing the Planner IR, projecting it onto a minimal contract containing only actionable fields, and serializing it into a JSON envelope wrapped in fixed XML-like tags.**

The Memory v5 engine in the DeepSeek-Reasonix repository transforms rich agent reasoning into model-readable prompts through a deterministic three-stage pipeline. This compilation process ensures that only relevant, actionable information reaches the language model while preserving execution context for later learning. The implementation centers on the [`internal/memorycompiler/runtime.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/internal/memorycompiler/runtime.go) file, which defines the core transformation functions that convert the full Planner IR into a **memory-compiler execution contract**.

## The Three-Stage Compilation Pipeline

### Step 1: Canonicalizing the Planner IR with canonicalizeIR

The process begins with the `canonicalizeIR` function in [`internal/memorycompiler/runtime.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/internal/memorycompiler/runtime.go) (lines 66-89). This function normalizes the Planner IR by removing whitespace variations, stripping empty fields, and enforcing strict limits on memory references (capped at five). It also deduplicates constraints, strategies, steps, and strings to ensure deterministic output.

By cleaning the IR before projection, the system guarantees that subsequent stages operate on a consistent, noise-free structure. This canonicalization prevents non-semantic differences from affecting the final prompt.

### Step 2: Projecting Onto the Minimal Contract with compactContractIR

Next, `compactContractIR` (lines 62-70 and 78-99 in [`runtime.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/runtime.go)) creates a `contractIR` containing only fields the model can influence. The function explicitly retains:

- **version**, **goal**, **source_event**, and **runtime_mode**
- **constraints** and **risk notes**
- **memory references** (limited to five)
- **execution steps** (limited to three)
- the **selected strategy**

It strips all internal control-loop state—including the full `available_strategies` table, numeric scores, exploration-rate data, and rejected candidates. Fields are copied verbatim to maintain byte-identical values with the original IR, ensuring integrity between the full trace and the compact representation.

### Step 3: Serializing the Contract with compileExecutionContract

Finally, `compileExecutionContract` (lines 645-660 in [`runtime.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/runtime.go)) marshals the contracted IR into a JSON string and wraps it in a fixed envelope: `<memory-compiler-execution>…</memory-compiler-execution>`. This produces the final compact prompt string injected into the user turn.

Simultaneously, the function records the full canonical IR on the execution trace for offline learning. This dual-track approach ensures the slimmed prompt carries only actionable data while the complete context persists for analysis and model improvement.

## Practical Implementation Example

The following Go code demonstrates the complete workflow from Planner IR to compact prompt:

```go
// Example: Turning a PlannerIR into a compact prompt
func example(ir PlannerIR) (string, error) {
    // 1. Normalise the IR (removes noise, caps memory refs)
    ir = canonicalizeIR(ir)

    // 2. Build the compact contract (keeps only actionable fields)
    contract := compactContractIR(ir)

    // 3. Serialize into the final prompt string
    return compileExecutionContract(ir)
}

```

For debugging and inspection, you can examine the intermediate contract structure:

```go
// Example: Inspecting the generated contract
func dumpContract(ir PlannerIR) {
    canonical := canonicalizeIR(ir)
    contract := compactContractIR(canonical)

    b, _ := json.MarshalIndent(contract, "", "  ")
    fmt.Printf("Compact contract JSON:\n%s\n", string(b))
}

```

The key source files involved in this process include:

- [`internal/memorycompiler/runtime.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/internal/memorycompiler/runtime.go) — Core implementation of `canonicalizeIR`, `compactContractIR`, and `compileExecutionContract`
- [`internal/memorycompiler/compression.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/internal/memorycompiler/compression.go) — Handles compression-related telemetry
- [`internal/memorycompiler/runtime_test.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/internal/memorycompiler/runtime_test.go) — Validates contract minimality and learning trace generation
- [`internal/provider/provider.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/internal/provider/provider.go) — Defines `MemoryCitation` structures attached to the contract

## Summary

- **Canonicalization** removes noise and enforces limits (≤5 memory references, ≤3 steps) before projection.
- **Contract projection** strips non-actionable internal state (strategy tables, scores, exploration data) while preserving verbatim copies of actionable fields.
- **Serialization** wraps the minimal contract in a fixed XML-like JSON envelope (`<memory-compiler-execution>`) for injection into the user turn.
- The bounded format prevents prompt ballooning during long sessions while maintaining all information the model needs to execute the turn.

## Frequently Asked Questions

### What is the Planner IR in Memory v5?

The **Planner IR** (Intermediate Representation) is the full structured representation of the agent's reasoning state, including all available strategies, scoring data, exploration rates, and execution history. It serves as the complete source of truth before the memory compiler reduces it to the minimal contract sent to the model.

### Why does Memory v5 limit memory references to five?

According to the source code in [`runtime.go`](https://github.com/esengine/DeepSeek-Reasonix/blob/main/runtime.go), the `canonicalizeIR` function caps memory references at five to maintain deterministic, bounded prompt sizes. This prevents context window overflow during extended sessions while preserving the most relevant historical context for the current turn's execution.

### What fields are removed during the contract projection?

The `compactContractIR` function explicitly removes the full `available_strategies` table, numeric scores, exploration-rate data, and rejected strategy candidates. These internal control-loop fields cannot be influenced by the model's output, so their exclusion reduces token usage without sacrificing decision-making capability.

### How does the compact prompt format prevent token limit exceeded errors?

By enforcing strict limits during canonicalization (≤5 memory references, ≤3 steps) and stripping non-actionable metadata during contract projection, the Memory v5 compiler guarantees that the serialized prompt stays within predictable bounds. The resulting contract contains only the essential JSON fields wrapped in fixed tags, ensuring linear growth characteristics regardless of session length.