Implementation and Reproduction Status

August 30, 2026 · View on GitHub

Chinese | English

Implementation and Reproduction Status

This document describes the plugin architecture and its reproduction scope for DeepSeek-OCR (OCR1) and OCR-Memory. Platform-specific experiments live in DEPLOYMENT.md, and the research log is in EXPLORATION.md.

1. Architecture

LayerCodeResponsibility
Optical memory enginelib/core.jssegmentation, tier decay, retrieval, locating, caching, and atomic persistence
DSH entrylib/index.jsconfiguration, backend clients, lifecycle, tool and context registration
Context snapshotlib/context.jsbounded synchronous prompt context from the manifest
Governed adapterlib/memory-system.js, lib/governance.jsL1/L2/L3 memory, namespaces, evidence, and provenance
Rendererscripts/render_memory.pysquare SoM images, numbered segments, CJK and long-text layout
Training helpersscripts/prepare_hotpotqa_locator.py, train_locator_unsloth.py, eval_locator.pylocator data, LoRA training, evaluation, and alignment checks

2. Data flow

2.1 Write

  1. Input text is split by blank lines and length into ordered segments, starting at segment id 1.
  2. The renderer draws the segments into a numbered square SoM image; the original segments are written to memories.json as the sole source for deterministic Fetch.
  3. The image cache key includes content, resolution, and renderer version. A content or tier change invalidates OCR, locator, and embedding evidence.
  4. OCR readback is optional: with requireOcr: false, the text path remains available without an OCR client; with requireOcr: true, missing or failed OCR readback is reported.
  5. Persistent visual embeddings are generated only when embeddingRetrieval: true; ocr1_mem_embed_test can still probe a configured embedding client independently.

2.2 Tiers and hit frequency

The default tiers are:

  • vivid: high-resolution representation of a fresh memory;
  • normal: representation after the first age transition;
  • fuzzy: low-resolution representation of a long-term memory.

The base policy uses only createdAt. With dynamicDecayEnabled, up to 32 access timestamps are retained and recent hits contribute a bounded exponential multiplier:

effectiveAge = age(createdAt) / boundedHeatMultiplier(recent access frequency)

The multiplier is capped and decays smoothly as accesses become old. It does not modify createdAt or keep a memory permanently high-resolution. The option is disabled by default so upgrading an existing store preserves its tier behavior. A hit on a low-resolution memory still triggers active recall and a short vivid grace period.

2.3 Retrieval

Without a locator, the plugin uses the legacy path of text overlap, OCR readback evidence, and optional embeddings. With opticalLocatorEnabled, the flow is:

  1. request K-bit 0/1 relevance labels for each current SoM image;
  2. strictly parse labels and logprobs with parseBinaryRelevance;
  3. apply the default 0.4 threshold with Top-K fallback through selectRelevanceIndices;
  4. Fetch the selected original segments by index from memories.json and return them verbatim.

Locator requests use an OpenAI-compatible interface. The image appears at the front of the message with the training-consistent newline prefix, temperature 0, logprobs, and llama.cpp GBNF constraints. With opticalLocatorStrict enabled, malformed output fails instead of falling back to text scoring.

2.4 Per-turn context snapshots

With autoInjectContext enabled, contextMode: index registers ocr1-memory:index through DSH systemPrompt.context(). It synchronously reads the governed L1 index plus optical metadata and never injects memory bodies. contextMode: snapshot instead registers ocr1-memory:context; that provider ranks persisted segments by hit count/recent access, truncates body text, and enforces contextMaxEntries and contextMaxChars.

Both providers read disk only; they perform no OCR, embedding, retrieval, or network request. A malformed manifest produces an empty string without blocking Prompt assembly. list, status, and metrics likewise project the current manifest without rendering or network work. Explicit maintenance migrates stale tiers in batches of at most maintenanceBatchSize entries. Maintenance is single-flight per namespace; duplicate calls report already-running, cancellation reaches renderer/OCR/embedding, and disposal cancels and drains owned work. Reports expose remaining and complete for later batches.

3. Key options

OptionDefaultPurpose
memoryDir~/.dsh/memorygoverned L1/L2/L3, pending, archive, and history root
maxIndexLines30maximum L1 index lines
defaultNamespaceemptynamespace used when automatic resolution is disabled
autoNamespacetrueresolve namespace from workspace/git context
autoPendingtruecreate pending candidates from failure-then-success sequences
maintainEveryTurns20persisted turn interval for automatic maintenance; 0 disables it
reflectPendingThreshold5pending threshold for reflection reminders
reflectSopsThreshold40active-SOP threshold for reflection reminders
maintenanceBatchSize8maximum stale or missing-image entries rerendered by one maintenance run
ocrBaseUrlemptyOpenAI-compatible /v1/chat/completions endpoint; an explicit port controls auto-start
ocrApiKeyemptyAPI key for the OCR endpoint
ocrModeldeepseek-ai/DeepSeek-OCRmodel name sent to the OCR endpoint
ocrRepeatPenalty1.2OCR repetition penalty
ocrNoRepeatNgramSize30OCR no-repeat n-gram size
ocrTextOnlyPromptTokens5text-only baseline subtracted from OCR usage
requireOcrfalsefail instead of silently degrading when OCR is unavailable
autoStartOcrServerfalsehave the plugin ensure llama-server is online, deduplicated by endpoint
ocrServerPath / ocrModelDiremptyexecutable and model directory for auto-start; environment overrides are supported
ocrServerPort18080fallback launch port when the URL has no explicit port
ocrEmbeddingBaseUrlemptyembedding endpoint; falls back to ocrBaseUrl
ocrEmbeddingAutoStartfalseauto-start a separate embedding endpoint at plugin load
ocrEmbeddingOnDemandtruestart a separate embedding endpoint on first use
ocrEmbeddingPort18084fallback port for a separate embedding endpoint
ocrEmbeddingUbatchSize2048physical batch limit for embedding server startup
ocrEmbeddingContextSize2048context size for combined/separate embedding startup
ocrEmbeddingIdleTimeoutMs300000idle shutdown delay for on-demand separate embedding
ocrMaxEntriesPerRetrieve5maximum entries sent through OCR per retrieval
opticalLocatorEnabledfalseenable the trained optical locating path
opticalLocatorBaseUrlemptylocator endpoint; falls back to ocrBaseUrl
opticalLocatorModeldeepseek-ocr-memorylocator model name
opticalLocatorTimeoutMs120000locator request timeout
opticalLocatorMaxSegments20maximum segments exposed to one locator request
opticalLocatorAlwaysUnionTopKfalseunion Top-K with threshold-selected segments
opticalLocatorThreshold0.4p(1) selection threshold
opticalLocatorTopK5fallback when no segment crosses the threshold
opticalLocatorStricttruereject malformed labels
opticalLocatorAutoStartfalseauto-start a trained locator on a separate endpoint
opticalLocatorServerPath / opticalLocatorModelDiremptyllama-server and model directory for locator startup; OPTICAL_LOCATOR_MODEL_DIR is supported
opticalLocatorServerPort18081fallback launch port when the locator URL has no explicit port
opticalLocatorModelFile / opticalLocatorMmprojFileQ8_0 locator namesmain model and vision projector loaded for locator startup
dynamicDecayEnabledfalseenable recent-hit-aware tier aging
decayFrequencyWindowMs7 dayssmoothing window for hit frequency
decayRecencyHalfLifeMs14 dayshalf-life for recent-access weight
decayHitWeight1hit-frequency weight in the multiplier
decayMaxMultiplier4maximum effective-age multiplier
autoInjectContexttrueinject bounded context each turn (index for L1/optical metadata, snapshot for full-body snapshots)
contextModeindexselect metadata index or body snapshot context
contextMaxEntries5maximum entries in the context
contextMaxChars4000maximum context characters
sharedStorefalsereload the manifest before each operation
embeddingRetrievalfalseenable visual-embedding retrieval signals
decayFrequencyWindowMs7 dayssmoothing window for hit frequency
decayRecencyHalfLifeMs14 dayshalf-life for recent-access weight
decayHitWeight1hit-frequency weight in the multiplier
decayMaxMultiplier4maximum effective-age multiplier

The remaining renderer options (pythonPath, renderScript, repetition controls), locator limits/timeouts, embedding API settings, and text-token baseline are defined in the Config object in lib/index.js.

When OCR and embeddings share an endpoint, the plugin uses one combined startup specification. A separate embedding endpoint can be started on demand and stopped after ocrEmbeddingIdleTimeoutMs. Only plugin-started processes with recorded PIDs are stopped during disposal; an already-running external service is left alone.

4. Locator training and deployment chain

The training scripts convert question/distractor samples into SoM images and K-bit binary labels, then apply a LoRA adapter to the DeepSeek-OCR decoder. The visual encoder remains frozen. Training supervises only the target label interval and uses the same digit space ... output grammar as inference.

For deployment, merge the adapter into the base model and convert it to a format supported by the target multimodal backend. Runtime requires an OpenAI-compatible /v1/chat/completions endpoint with the corresponding image input. Model conversion, quantization choices, service parameters, and validated end-to-end examples are documented in DEPLOYMENT.md.

The repository includes data preparation, training, evaluation, and alignment-check scripts, but small-scale local training is not paper-table reproduction; full scale requires the paper's datasets and evaluation suites.

5. Reproduction matrix

5.1 DeepSeek-OCR (OCR1)

Paper conceptPlugin implementationDegree
Long text to optical 2D mappingparagraph → square SoM imageengineering approximation
Visual tokens carry informationresolution-aligned tiers and endpoint-level token statisticsinterface-level approximation
DeepEncoder internal compressionno internal tensors or layer-wise token export from the llama.cpp public interfacenot reproduced
Official visual embeddingspersisted through a compatible embedding endpoint; disabled as the primary retrieval signal by defaultinterface-level

5.2 OCR-Memory

Method conceptPlugin implementationDegree
SoM numbered segmentsnumbered boxes and persistent segment indexesimplemented
LocateLoRA locator emits K-bit binary labels with strict grammar decodingimplemented
Transcribepersistent verbatim text returned by segment indeximplemented
Age-aware multi-resolutionvivid → normal → fuzzy, with age-based rerenderingimplemented
Hit-frequency decaybounded, optional, backward-compatible recent-hit policyimplemented, disabled by default
Active recalllow-resolution hit restores vividimplemented
Threshold and Top-Kdefault threshold with no-hit fallback; union rules can be selectedimplemented
Per-turn memory contextL1/optical metadata, full-body snapshots optional via contextMode: snapshotimplemented, enabled by default (index mode)

6. Explicit boundaries

This repository does not claim full reproduction of:

  • DeepEncoder internal compression, layer-wise visual-token counts, or internal tensor visualization;
  • paper-scale training data and Mind2Web/AppWorld/RULER main-table evaluation;
  • multimodal embeddings fully equivalent to the official internal DeepEncoder representation;
  • universal compatibility across any specific hardware, driver, quantization format, or service supervisor.

These boundaries arise from public runtime interfaces, model formats, and available resources. They do not block the engineering implementation of SoM, locating, deterministic Fetch, age tiers, active recall, optional dynamic decay, or DSH context integration.

  • README: quick start;
  • DEPLOYMENT: backend deployment and platform validation;
  • STATUS: current status;
  • BENCHMARK: isolated benchmark;
  • EXPLORATION: research and experiment log;
  • TEST_SPEC / TEST_REPORT: test specification and results; the current full regression is 88/88 with a healthy live backend.
  • Paper mapping: paper-native mechanisms, engineering extensions, and acceptance boundaries.