* 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 },
)
| 1842 | * @returns Array of nested memory attachments |
| 1843 | */ |
| 1844 | async function getNestedMemoryAttachmentsForFile( |
| 1845 | filePath: string, |
| 1846 | toolUseContext: ToolUseContext, |
| 1847 | appState: { toolPermissionContext: ToolPermissionContext }, |
| 1848 | ): Promise<Attachment[]> { |
| 1849 | const attachments: Attachment[] = [] |
| 1850 | |
| 1851 | try { |
| 1852 | // Early return if path is not in allowed working path |
| 1853 | if (!pathInAllowedWorkingPath(filePath, appState.toolPermissionContext)) { |
| 1854 | return attachments |
| 1855 | } |
| 1856 | |
| 1857 | const processedPaths = new Set<string>() |
| 1858 | const originalCwd = getOriginalCwd() |
| 1859 | |
| 1860 | // Phase 1: Process Managed and User conditional rules |
| 1861 | const managedUserRules = await getManagedAndUserConditionalRules( |
| 1862 | filePath, |
| 1863 | processedPaths, |
| 1864 | ) |
| 1865 | attachments.push( |
| 1866 | ...memoryFilesToAttachments(managedUserRules, toolUseContext, filePath), |
| 1867 | ) |
| 1868 | |
| 1869 | // Phase 2: Get directories to process |
| 1870 | const { nestedDirs, cwdLevelDirs } = getDirectoriesToProcess( |
| 1871 | filePath, |
| 1872 | originalCwd, |
| 1873 | ) |
| 1874 | |
| 1875 | const skipProjectLevel = getFeatureValue_CACHED_MAY_BE_STALE( |
| 1876 | 'tengu_paper_halyard', |
| 1877 | false, |
| 1878 | ) |
| 1879 | |
| 1880 | // Phase 3: Process nested directories (CWD → target) |
| 1881 | // Each directory gets: CLAUDE.md + unconditional rules + conditional rules |
| 1882 | for (const dir of nestedDirs) { |
| 1883 | const memoryFiles = ( |
| 1884 | await getMemoryFilesForNestedDirectory(dir, filePath, processedPaths) |
| 1885 | ).filter( |
| 1886 | f => !skipProjectLevel || (f.type !== 'Project' && f.type !== 'Local'), |
| 1887 | ) |
| 1888 | attachments.push( |
| 1889 | ...memoryFilesToAttachments(memoryFiles, toolUseContext, filePath), |
| 1890 | ) |
| 1891 | } |
| 1892 | |
| 1893 | // Phase 4: Process CWD-level directories (root → CWD) |
| 1894 | // Only conditional rules (unconditional rules are already loaded eagerly) |
| 1895 | for (const dir of cwdLevelDirs) { |
| 1896 | const conditionalRules = ( |
| 1897 | await getConditionalRulesForCwdLevelDirectory( |
| 1898 | dir, |
| 1899 | filePath, |
| 1900 | processedPaths, |
| 1901 | ) |
no test coverage detected