MCPcopy Create free account
hub / github.com/WJX20/claude-code / getNestedMemoryAttachmentsForFile

Function getNestedMemoryAttachmentsForFile

src/utils/attachments.ts:1793–1863  ·  view source on GitHub ↗

* Loads nested memory files for a given file path and returns them as attachments. * This function performs directory traversal to find CLAUDE.md files and conditional rules * that apply to the target file path. * * Processing order (must be preserved): * 1. Managed/User conditional rules match

(
  filePath: string,
  toolUseContext: ToolUseContext,
  appState: { toolPermissionContext: ToolPermissionContext },
)

Source from the content-addressed store, hash-verified

1791 * @returns Array of nested memory attachments
1792 */
1793async function getNestedMemoryAttachmentsForFile(
1794 filePath: string,
1795 toolUseContext: ToolUseContext,
1796 appState: { toolPermissionContext: ToolPermissionContext },
1797): Promise<Attachment[]> {
1798 const attachments: Attachment[] = []
1799
1800 try {
1801 // Early return if path is not in allowed working path
1802 if (!pathInAllowedWorkingPath(filePath, appState.toolPermissionContext)) {
1803 return attachments
1804 }
1805
1806 const processedPaths = new Set<string>()
1807 const originalCwd = getOriginalCwd()
1808
1809 // Phase 1: Process Managed and User conditional rules
1810 const managedUserRules = await getManagedAndUserConditionalRules(
1811 filePath,
1812 processedPaths,
1813 )
1814 attachments.push(
1815 ...memoryFilesToAttachments(managedUserRules, toolUseContext, filePath),
1816 )
1817
1818 // Phase 2: Get directories to process
1819 const { nestedDirs, cwdLevelDirs } = getDirectoriesToProcess(
1820 filePath,
1821 originalCwd,
1822 )
1823
1824 const skipProjectLevel = getFeatureValue_CACHED_MAY_BE_STALE(
1825 'tengu_paper_halyard',
1826 false,
1827 )
1828
1829 // Phase 3: Process nested directories (CWD → target)
1830 // Each directory gets: CLAUDE.md + unconditional rules + conditional rules
1831 for (const dir of nestedDirs) {
1832 const memoryFiles = (
1833 await getMemoryFilesForNestedDirectory(dir, filePath, processedPaths)
1834 ).filter(
1835 f => !skipProjectLevel || (f.type !== 'Project' && f.type !== 'Local'),
1836 )
1837 attachments.push(
1838 ...memoryFilesToAttachments(memoryFiles, toolUseContext, filePath),
1839 )
1840 }
1841
1842 // Phase 4: Process CWD-level directories (root → CWD)
1843 // Only conditional rules (unconditional rules are already loaded eagerly)
1844 for (const dir of cwdLevelDirs) {
1845 const conditionalRules = (
1846 await getConditionalRulesForCwdLevelDirectory(
1847 dir,
1848 filePath,
1849 processedPaths,
1850 )

Callers 2

getOpenedFileFromIDEFunction · 0.85

Tested by

no test coverage detected