memoryScan.ts
memdir/memoryScan.ts
95
Lines
3105
Bytes
3
Exports
5
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with memory-layers. It contains 95 lines, 5 detected imports, and 3 detected exports.
Important relationships
Detected exports
MemoryHeaderscanMemoryFilesformatMemoryManifest
Keywords
mtimemsdescriptionmemoryheaderfilenamefilepathreadfileinrangefrontmatterjoinmax_memory_filesmemory
Detected imports
fs/promisespath../utils/frontmatterParser.js../utils/readFileInRange.js./memoryTypes.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
/**
* Memory-directory scanning primitives. Split out of findRelevantMemories.ts
* so extractMemories can import the scan without pulling in sideQuery and
* the API-client chain (which closed a cycle through memdir.ts — #25372).
*/
import { readdir } from 'fs/promises'
import { basename, join } from 'path'
import { parseFrontmatter } from '../utils/frontmatterParser.js'
import { readFileInRange } from '../utils/readFileInRange.js'
import { type MemoryType, parseMemoryType } from './memoryTypes.js'
export type MemoryHeader = {
filename: string
filePath: string
mtimeMs: number
description: string | null
type: MemoryType | undefined
}
const MAX_MEMORY_FILES = 200
const FRONTMATTER_MAX_LINES = 30
/**
* Scan a memory directory for .md files, read their frontmatter, and return
* a header list sorted newest-first (capped at MAX_MEMORY_FILES). Shared by
* findRelevantMemories (query-time recall) and extractMemories (pre-injects
* the listing so the extraction agent doesn't spend a turn on `ls`).
*
* Single-pass: readFileInRange stats internally and returns mtimeMs, so we
* read-then-sort rather than stat-sort-read. For the common case (N ≤ 200)
* this halves syscalls vs a separate stat round; for large N we read a few
* extra small files but still avoid the double-stat on the surviving 200.
*/
export async function scanMemoryFiles(
memoryDir: string,
signal: AbortSignal,
): Promise<MemoryHeader[]> {
try {
const entries = await readdir(memoryDir, { recursive: true })
const mdFiles = entries.filter(
f => f.endsWith('.md') && basename(f) !== 'MEMORY.md',
)
const headerResults = await Promise.allSettled(
mdFiles.map(async (relativePath): Promise<MemoryHeader> => {
const filePath = join(memoryDir, relativePath)
const { content, mtimeMs } = await readFileInRange(
filePath,
0,
FRONTMATTER_MAX_LINES,
undefined,
signal,
)
const { frontmatter } = parseFrontmatter(content, filePath)
return {
filename: relativePath,
filePath,
mtimeMs,
description: frontmatter.description || null,
type: parseMemoryType(frontmatter.type),
}
}),
)
return headerResults
.filter(
(r): r is PromiseFulfilledResult<MemoryHeader> =>
r.status === 'fulfilled',
)
.map(r => r.value)
.sort((a, b) => b.mtimeMs - a.mtimeMs)
.slice(0, MAX_MEMORY_FILES)
} catch {
return []
}
}
/**
* Format memory headers as a text manifest: one line per file with
* [type] filename (timestamp): description. Used by both the recall
* selector prompt and the extraction-agent prompt.
*/
export function formatMemoryManifest(memories: MemoryHeader[]): string {
return memories
.map(m => {
const tag = m.type ? `[${m.type}] ` : ''
const ts = new Date(m.mtimeMs).toISOString()
return m.description
? `- ${tag}${m.filename} (${ts}): ${m.description}`
: `- ${tag}${m.filename} (${ts})`
})
.join('\n')
}