Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import { realpathSync } from 'fs';
import { matchesAnyGlob } from './project-filter.js';
import { logger } from './logger.js';
/**
* A named environment: several directories that share one memory bucket
* (CLAUDE_MEM_PROJECT_ENVIRONMENTS). Patterns use the same glob rules as
* CLAUDE_MEM_EXCLUDED_PROJECTS (`*`, `**`, `?`, leading `~`; a pattern also
* matches a folder's name on its own), and `dir/**` covers `dir` itself.
*/
export interface ProjectEnvironment {
name: string;
patterns: string[];
/** Omitted: retain derived keys. Empty: read only this environment. */
inherit?: string[];
}
/**
* Parse the setting. settings.json may hold it as a JSON string or as a native
* JSON array, so both are accepted. Invalid JSON, names and patterns keep their
* existing warn-and-skip behavior.
* An explicit invalid inherit list throws: ignoring it would widen read scope.
*/
export function parseProjectEnvironments(raw: unknown): ProjectEnvironment[] {
let value = raw;
if (typeof value === 'string') {
if (!value.trim()) return [];
try {
value = JSON.parse(value);
} catch (error: unknown) {
logger.warn('PROJECT_NAME', 'CLAUDE_MEM_PROJECT_ENVIRONMENTS is not valid JSON; ignoring it', {
error: error instanceof Error ? error.message : String(error),
});
return [];
}
}
if (!Array.isArray(value)) {
if (value !== undefined && value !== null) {
logger.warn('PROJECT_NAME', 'CLAUDE_MEM_PROJECT_ENVIRONMENTS must be an array; ignoring it', {});
}
return [];
}
const environments: ProjectEnvironment[] = [];
for (const entry of value as Array<{ name?: unknown; patterns?: unknown; inherit?: unknown }>) {
const name = typeof entry?.name === 'string' ? entry.name.trim() : '';
const patterns = Array.isArray(entry?.patterns)
? entry.patterns.filter((pattern): pattern is string => typeof pattern === 'string' && pattern.trim() !== '')
: [];
if (!name || patterns.length === 0) {
logger.warn('PROJECT_NAME', 'Ignoring a project environment without a name or patterns', { name });
continue;
}
if ('inherit' in entry) {
const inherit = entry.inherit;
if (!Array.isArray(inherit) || inherit.some(key => typeof key !== 'string' || !key.trim() || key.includes(','))) {
throw new Error(`CLAUDE_MEM_PROJECT_ENVIRONMENTS: inherit for "${name}" must be an array of non-empty project names without commas`);
}
environments.push({ name, patterns, inherit: [...new Set(inherit.map(key => key.trim()))] });
} else {
environments.push({ name, patterns });
}
}
return environments;
}
function realpathOrSelf(dir: string): string {
try {
return realpathSync(dir);
} catch {
return dir;
}
}
/**
* The environment `dir` belongs to, or null. Both the path as given and its
* realpath are tried, so a symlinked home or checkout matches patterns written
* either way; the trailing-slash form lets `dir/**` match `dir` itself.
*/
export function matchProjectEnvironment(dir: string, environments: ProjectEnvironment[]): ProjectEnvironment | null {
if (environments.length === 0) return null;
const candidates = [...new Set([dir, realpathOrSelf(dir)])]
.flatMap(candidate => [candidate, `${candidate.replace(/[\\/]+$/, '')}/`]);
for (const environment of environments) {
if (candidates.some(candidate => matchesAnyGlob(candidate, environment.patterns))) {
return environment;
}
}
return null;
}