Skip to main content

Persistent Memory

Persistent Memory defines declarative retrieval pipeline definitions that the Context Assembly Engine executes at runtime. Each "memory type" is a reusable, configurable retrieval recipe. Pipelines fetch data from storage layers, transform it, and format it for inclusion in LLM context.

Persistent Memory


Storage

Persistent Memory definitions are file-based, stored at:

{projectPath}/.codebolt/PersistentMemory/{id}.json

Each file is a complete, self-contained PersistentMemory definition.


Memory Type Definition Structure

A PersistentMemory definition follows this schema:

id: string # unique identifier
label: string # human-readable name
description: string # what this retrieves
version: string # semver
status: 'active' | 'deprecated'
inputs_scope: InputScope[] # which scopes provide variables
additional_variables: AdditionalVariable[]
retrieval:
pipeline: PipelineStep[] # the retrieval pipeline
contribution:
section: ContextSection # where in context this appears
priority: 'low' | 'medium' | 'high' | 'critical'
format: 'bullet_list' | 'numbered' | 'prose' | 'json'
max_items: number

Input Scopes

There are 8 input scopes, each providing standard variables for template substitution within pipeline parameters.

ScopeVariables
swarmswarm_id, swarm_name
agentagent_id, agent_name, agent_type
projectproject_id, project_path
orchestratororchestrator_id
threadthread_id
tasktask_id, task_type
useruser_id
orgorg_id

Pipeline Execution

The executePipeline(memoryId, intent, threadId) function drives retrieval:

  1. Load the PersistentMemory definition by ID.
  2. Create execution context containing: memoryId, memory, intent, a results Map, query, projectPath, threadId, and resolved variables.
  3. Execute steps sequentially via executeSteps().
  4. Store each step result in context.results keyed by the step's alias.
  5. Return a PipelineExecutionResult with per-step results, final output, formatted output, and timing information.

Template Variable Resolution

Pipeline parameters support template substitution using ${variable_name} syntax. Variables are resolved from three sources:

  • Intent fields (keywords, query)
  • Scope variables (from the input scopes defined on the memory type)
  • Step outputs (results from previously executed pipeline steps)

Resolution is handled by two functions:

  • resolveTemplateVariables() — recursively replaces ${...} placeholders in strings.
  • resolveStepParams() — recursively resolves template variables in nested objects and arrays.

Pipeline Step Types

There are 21 pipeline step types organized into 8 categories.

Input (2 steps)

StepDescription
derive_queryForms a query from intent keywords, action, context, or query. Joins keywords with spaces. Source: intent fields.
rewrite_queryNormalizes a query (lowercases). Reads from a source step result or the context query.

Retrieval (4 steps)

StepDescription
vector_searchSearches a vector DB collection. Params: collection (required), topK (default 5), minScore (default 0.5), query (from context). Calls vectorDbService.queryDocuments().
graph_view_readExecutes a Knowledge Graph view. Params: viewId (required). Calls kgInstanceService.executeView().
kv_getFetches from a KV store. Params: instance (required), namespace (required), keys[] (required). Calls kvStoreDataService.get() for each key.
log_searchSearches the event log. Params: instance (required), eventType, limit (default 20), since, streamId. Compiles EventLogQueryDSL and executes.

Expansion (2 steps)

StepDescription
graph_view_expandExpand graph nodes. (Placeholder in current implementation.)
graph_neighbor_expandExpand node neighbors. (Placeholder in current implementation.)

Filter (2 steps)

StepDescription
filterFilters items by field/operator/value. Operators: eq, neq, gt, lt, gte, lte, contains, startsWith, endsWith, in, exists. Gets source data from a previous step.
deduplicateRemoves duplicates by field value. Uses a Map to track seen values.

Aggregation (3 steps)

StepDescription
group_byGroups items by a field value. Returns Record<string, any[]>.
reduce_latestKeeps only the latest item per entity (by entityField). Sorts by sortField descending.
mergeMerges multiple source arrays. Strategies: concat (default), interleave, unique (by uniqueField).

Ranking (2 steps)

StepDescription
rankSorts by strategy. Strategies: score_desc (by score field descending), recency (by timestamp descending), relevance (by relevance field descending). Applies topK limit.
scoreApplies weighted scoring. Weights map field names to multipliers. Computes weighted sum, sorts descending, applies topK.

Transform (3 steps)

StepDescription
summarizeLimits output to max_items from source data.
formatFormats output as a string. Formats: bullet_list, numbered, prose, json, markdown. Uses formatItem() to extract text from items.
annotateAdds provenance metadata to items (source memory, retrieval timestamp, step info, optional label).

Control (2 steps)

StepDescription
conditionalEvaluates a condition expression. If true, runs thenSteps; if false, runs elseSteps. Returns boolean.
early_exitEvaluates a condition. If true, stops pipeline execution immediately. Returns boolean.

Expression Evaluation

The evaluateExpression(expression, ctx) function handles condition evaluation in control steps. Supported expression patterns:

PatternExampleDescription
Result countresults.count >= 5Count of items in results
Array lengthresults.alias.length > 10Length of a specific step's output array
String searchquery.contains('text')Check if the query contains a substring
ComparisonAny of >=, <=, >, <, ==, !=Standard comparison operators

Custom Steps

The executeCustom() function supports three execution modes:

  • inlineCode — Evaluated in a sandbox environment.
  • actionBlockPath — Executed via SideExecutionManager.
  • handler — A named, registered handler function.

Context Sections

The contribution.section field determines where the memory output appears in the assembled LLM context:

  • state
  • warnings
  • knowledge
  • history
  • constraints
  • suggestions
  • working_memory

Priority Weights

Priority values map to numeric weights used during context assembly:

PriorityWeight
critical4
high3
medium2
low1

Validation

The validatePersistentMemory() function performs structural validation on a definition and returns { valid, errors[], warnings[] }.

Checks include:

  • Required fields: id, label must be present.
  • Pipeline steps: Each step must have a valid type and the required params for that step type.
  • Contribution config: The section, priority, and format values must be from the allowed enumerations.

Operations

OperationDescription
CreateCreate a new PersistentMemory definition
GetRetrieve a definition by ID
ListList all definitions
UpdateUpdate an existing definition
DeleteRemove a definition
ListActiveList definitions with status='active' only
ListByInputScopeFilter definitions by a specific input scope
DuplicateDeep copy with a new ID
ExportSerialize a definition to a JSON string
ImportDeserialize from a JSON string, with optional overwrite
ExecuteRetrievalRun the pipeline with an intent
ValidateValidate a definition structure
GetStepSpecsRetrieve available step type specifications

WebSocket Events

EventTrigger
persistentMemory:createdA new definition is created
persistentMemory:updatedAn existing definition is updated
persistentMemory:deletedA definition is deleted
persistentMemory:pipeline-executedA pipeline execution completes

REST API

Memory Type CRUD

GET /persistent-memory/types # List all memory types
POST /persistent-memory/types # Create a new memory type
GET /persistent-memory/types/:id # Get a specific memory type
PUT /persistent-memory/types/:id # Update a memory type
DELETE /persistent-memory/types/:id # Delete a memory type

Execution and Validation

POST /persistent-memory/types/:id/execute # Execute a memory type's pipeline
POST /persistent-memory/validate # Validate a definition

Step Specifications

GET /persistent-memory/step-specs # Get all step specs
GET /persistent-memory/step-specs/user-configurable # Get user-configurable step specs

Import / Export / Duplicate

GET /persistent-memory/types/:id/export # Export a definition as JSON
POST /persistent-memory/import # Import a definition from JSON
POST /persistent-memory/types/:id/duplicate # Duplicate a definition