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