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 { basename } from 'path';
import { expandHome } from '../shared/expand-home.js';
import { logger } from './logger.js';
function globToRegex(pattern: string): RegExp {
// Separators first, then home expansion. A pattern written on Windows as `~\projects\secret`
// is home-relative, but `expandHome` only recognises `~` and `~/` on POSIX — resolving
// another user's home is deliberately out of its scope. Expanding before normalising left
// that pattern as a literal `~/projects/secret`, which matches no absolute path, so a
// configured exclusion silently stopped excluding.
let expanded = expandHome(pattern.replace(/\\/g, '/'));
// Again on the way out: on Windows `homedir()` itself returns backslashes, so the
// expanded path needs normalising even when the pattern arrived POSIX-form.
expanded = expanded.replace(/\\/g, '/');
let regex = expanded.replace(/[.+^${}()|[\]\\]/g, '\\$&');
regex = regex
// A complete **/ directory segment also matches no directory. Keep its
// slash until after tokenization so consecutive globstars are recognized.
.replace(/(^|\/)\*\*(?=\/)/g, '$1<<<GLOBSTAR_DIR>>>')
.replace(/\*\*/g, '<<<GLOBSTAR>>>')
.replace(/\*/g, '[^/]*')
.replace(/\?/g, '[^/]')
.replace(/<<<GLOBSTAR>>>/g, '.*')
.replace(/<<<GLOBSTAR_DIR>>>\//g, '(?:.*/)?');
// Directory names may contain line terminators, which globstars must also match.
return new RegExp(`^${regex}$`, 's');
}
/**
* `dir/**` means "everything under dir" and users also expect it to cover `dir`
* itself, but the regex needs a character after `dir/`. Match the pattern with
* its trailing `/**` removed against the path as well.
*/
function matchesGlobPattern(rawPattern: string, normalizedPath: string, pathBasename: string): boolean {
// A folder pattern written with a trailing slash (`~/work/client/`) names the
// same folder; kept, it required a slash no checkout path ends with.
const pattern = rawPattern.replace(/[\\/]+$/, '') || rawPattern;
const regex = globToRegex(pattern);
if (regex.test(normalizedPath) || regex.test(pathBasename)) return true;
if (pattern.replace(/\\/g, '/').endsWith('/**')) {
return globToRegex(pattern.slice(0, -3)).test(normalizedPath.replace(/\/+$/, ''));
}
return false;
}
/**
* Returns true when `folderPath` matches any of the supplied glob patterns.
* Patterns support `*`, `**`, `?`, and a leading `~`. Matches against both the
* full normalized path and the basename. Reuses the same glob semantics as
* project exclusion. Used by the skeleton-CLAUDE.md deny-list (#2400).
*/
export function matchesAnyGlob(folderPath: string, patterns: string[]): boolean {
if (!patterns.length) return false;
const normalizedPath = folderPath.replace(/\\/g, '/');
const pathBasename = basename(normalizedPath);
for (const rawPattern of patterns) {
const pattern = rawPattern.trim();
if (!pattern) continue;
try {
if (matchesGlobPattern(pattern, normalizedPath, pathBasename)) {
return true;
}
} catch (error: unknown) {
logger.warn('PROJECT_NAME', 'Invalid glob pattern', { pattern, error: error instanceof Error ? error.message : String(error) });
continue;
}
}
return false;
}
export function isProjectExcluded(projectPath: string, exclusionPatterns: string): boolean {
if (!exclusionPatterns || !exclusionPatterns.trim()) {
return false;
}
const normalizedProjectPath = projectPath.replace(/\\/g, '/');
const projectBasename = basename(normalizedProjectPath);
const patternList = exclusionPatterns
.split(',')
.map(p => p.trim())
.filter(Boolean);
for (const pattern of patternList) {
try {
if (matchesGlobPattern(pattern, normalizedProjectPath, projectBasename)) {
return true;
}
} catch (error: unknown) {
logger.warn('PROJECT_NAME', 'Invalid exclusion pattern', { pattern, error: error instanceof Error ? error.message : String(error) });
continue;
}
}
return false;
}