Skip to content

Prompt History

Eshan Roy edited this page Jun 21, 2026 · 3 revisions

Prompt History

M31 Autonomous (M31A) maintains a frecent (frequent + recent) prompt history that powers autocomplete suggestions in the REPL. History is persisted as JSON and survives across sessions.

Source: pkg/history/history.go

FrecentHistory

type FrecentHistory struct {
    mu        sync.RWMutex
    entries   []FrecentEntry
    textIndex map[string]int // text → index in entries for O(1) lookup
    filePath  string
}

type FrecentEntry struct {
    Text     string    `json:"text"`
    Score    float64   `json:"score"`
    LastUsed time.Time `json:"last_used"`
    UseCount int       `json:"use_count"`
}

Scoring Algorithm

Each entry's score combines recency and frequency:

func (fh *FrecentHistory) computeScore(e FrecentEntry) float64 {
    ageHours := time.Since(e.LastUsed).Hours()
    recency := 1.0 / (1.0 + ageHours/24.0)   // exponential decay over 24h
    return recency * float64(e.UseCount) * 10 + recency
}
  • Recency: Exponential decay with 24-hour half-life
  • Frequency: Use count multiplied by recency factor
  • Entries are sorted by score (highest first)

Operations

Method Description
Upsert(text) Add or update entry, increment use count, save to disk
Search(query, limit) Substring match, sorted by most recently used
Save() Persist to disk as JSON

Upsert Behavior

  1. Trim whitespace
  2. If text exists (O(1) lookup via textIndex map): increment UseCount, update LastUsed, recompute score
  3. If new: create entry with UseCount=1, add to textIndex
  4. Cap at 500 entries (trim lowest-scored, rebuild index)
  5. Persist to disk immediately

Search Behavior

  1. Case-insensitive substring match
  2. Sort results by LastUsed (most recent first)
  3. Limit results if requested

Persistence

History is stored as JSON in the session directory:

  • File permissions: 0600 (owner read/write only)
  • Directory permissions: 0700
  • On corrupt file: log warning and start with empty history
  • Save is triggered on every upsert and on application shutdown

Configuration

[ui]
frecent_history_size = 100    # Max entries in frecent history

Integration

The REPL model uses FrecentHistory for:

  • Autocomplete suggestions as the user types
  • Quick access to recent prompts via Up arrow
  • Prompt history browser (/prompt-history command)

Clone this wiki locally