Class MemoryConsolidator

java.lang.Object
io.agentscope.harness.agent.memory.MemoryConsolidator

public class MemoryConsolidator extends Object
LLM-based consolidation of daily memory ledgers into the curated MEMORY.md.

This component owns the second layer of the two-layer memory model:

  • Layer 1 — daily ledger: memory/YYYY-MM-DD.md files written by MemoryFlushManager, append-only, one section per compaction flush.
  • Layer 2 — curated MEMORY.md: Owned by this class. Periodically reads the daily ledgers (those modified since the last consolidation watermark) plus the current MEMORY.md, asks the LLM to merge / dedupe / trim, and overwrites MEMORY.md with the result.

A small state file (memory/.consolidation_state) records the timestamp of the last successful consolidation. Daily files whose modifiedAt is at or before that timestamp are skipped — reducing token usage and protecting MEMORY.md from being re-rewritten with stale content.

All file I/O is performed via the AbstractFilesystem obtained from the WorkspaceManager, so this class is backend-agnostic (works with Local, Sandbox, and Remote filesystems without any direct java.nio.file.Files usage).

  • Field Details

    • STATE_FILE

      public static final String STATE_FILE
      Hidden state file inside memory/ tracking the last consolidation Instant.
      See Also:
    • DEFAULT_CONSOLIDATION_PROMPT

      public static final String DEFAULT_CONSOLIDATION_PROMPT
      Default prompt for the consolidation step. Exposed publicly so callers can extend (e.g. append project-specific guidelines) when constructing MemoryConfig. The prompt contains exactly two %d placeholders (max-tokens, max-chars) that are filled in at call time.
  • Constructor Details

    • MemoryConsolidator

      public MemoryConsolidator(WorkspaceManager workspaceManager, io.agentscope.core.model.Model model)
    • MemoryConsolidator

      public MemoryConsolidator(WorkspaceManager workspaceManager, io.agentscope.core.model.Model model, int maxMemoryTokens)
    • MemoryConsolidator

      public MemoryConsolidator(WorkspaceManager workspaceManager, io.agentscope.core.model.Model model, String consolidationPrompt, int maxMemoryTokens)
      Parameters:
      consolidationPrompt - prompt template for the consolidation LLM call. Must contain exactly two %d placeholders (max-tokens, max-chars). null falls back to DEFAULT_CONSOLIDATION_PROMPT.
    • MemoryConsolidator

      public MemoryConsolidator(WorkspaceManager workspaceManager, io.agentscope.core.model.Model model, String consolidationPrompt, int maxMemoryTokens, BaseStore baseStore)
      Parameters:
      baseStore - optional distributed store for the consolidation watermark; when set, watermark reads prefer the store and writes use CAS with a filesystem fallback
  • Method Details

    • consolidate

      public reactor.core.publisher.Mono<Void> consolidate(io.agentscope.core.agent.RuntimeContext rc)
      Runs consolidation: reads daily files modified after the last watermark and the current MEMORY.md, uses the LLM to merge them, overwrites MEMORY.md, and advances the watermark on success.

      If no daily files have been touched since the last run, this is a no-op.