MCPcopy Create free account
hub / github.com/Noumena-Network/code / isShellCommandTargetingMemory

Function isShellCommandTargetingMemory

src/utils/memoryFileDetection.ts:223–279  ·  view source on GitHub ↗
(command: string)

Source from the content-addressed store, hash-verified

221 * collapse logic.
222 */
223export function isShellCommandTargetingMemory(command: string): boolean {
224 const configDir = getClaudeConfigHomeDir()
225 const memoryBase = getMemoryBaseDir()
226 const autoMemDir = isAutoMemoryEnabled()
227 ? getAutoMemPath().replace(/[/\\]+$/, '')
228 : ''
229
230 // Quick check: does the command mention the config, memory base, or
231 // auto-mem directory? Compare in forward-slash form (PowerShell on Windows
232 // may use either separator while configDir uses the platform-native one).
233 // On Windows also check the MinGW form (/c/...) since BashTool runs under
234 // Git Bash which emits that encoding. On Linux/Mac, configDir is already
235 // posix so only one form to check — and crucially, windowsPathToPosixPath
236 // is NOT called, so Linux paths like /m/foo aren't misinterpreted as MinGW.
237 const commandCmp = toComparable(command)
238 const dirs = [configDir, memoryBase, autoMemDir].filter(Boolean)
239 const matchesAnyDir = dirs.some(d => {
240 if (commandCmp.includes(toComparable(d))) return true
241 if (IS_WINDOWS) {
242 // BashTool on Windows (Git Bash) emits /c/Users/... — check MinGW form too
243 return commandCmp.includes(windowsPathToPosixPath(d).toLowerCase())
244 }
245 return false
246 })
247 if (!matchesAnyDir) {
248 return false
249 }
250
251 // Extract absolute path-like tokens. Matches Unix absolute paths (/foo/bar),
252 // Windows drive-letter paths (C:\foo, C:/foo), and MinGW paths (/c/foo —
253 // they're /-prefixed so the regex already captures them). Bare backslash
254 // tokens (\foo) are intentionally excluded — they appear in regex/grep
255 // patterns and would cause false-positive memory classification after
256 // normalization flips backslashes to forward slashes.
257 const matches = command.match(/(?:[A-Za-z]:[/\\]|\/)[^\s'"]+/g)
258 if (!matches) {
259 return false
260 }
261
262 for (const match of matches) {
263 // Strip trailing shell metacharacters that could be adjacent to a path
264 const cleanPath = match.replace(/[,;|&>]+$/, '')
265 // On Windows, convert MinGW /c/... → native C:\... at this single
266 // point. Downstream predicates (isAutoManagedMemoryFile, isMemoryDirectory,
267 // isAutoMemPath, isAgentMemoryPath) then receive native paths and only
268 // need toComparable() for matching. On other platforms, paths are already
269 // native — no conversion, so /m/foo etc. pass through unmodified.
270 const nativePath = IS_WINDOWS
271 ? posixPathToWindowsPath(cleanPath)
272 : cleanPath
273 if (isAutoManagedMemoryFile(nativePath) || isMemoryDirectory(nativePath)) {
274 return true
275 }
276 }
277
278 return false
279}
280

Callers 1

isMemorySearchFunction · 0.85

Calls 5

getMemoryBaseDirFunction · 0.85
isAutoMemoryEnabledFunction · 0.85
toComparableFunction · 0.85
isAutoManagedMemoryFileFunction · 0.85
isMemoryDirectoryFunction · 0.85

Tested by

no test coverage detected