From a0e4e3f04ef9f2ca386f4d868cc05263714294c1 Mon Sep 17 00:00:00 2001 From: Hal Eisen Date: Wed, 18 Nov 2026 19:40:15 -0800 Subject: [PATCH 1/2] ADFA-7214 | AI Agent Local: load models picked from shared storage on API 36 With FUSE passthrough, a SAF descriptor for a file in /sdcard/Download is on the lower filesystem, owned by MediaProvider, so /proc/self/fd/N cannot be re-opened by name and every such model was refused as "can't be read from where it is stored". When the procfs path is not re-openable and the document is primary: from com.android.externalstorage.documents, hand llama.cpp the shared-storage path instead (the IDE holds all-files access), provided it opens and has the descriptor's size. The descriptor is then closed: held open, MediaProvider serves the path as direct I/O and mmap fails with ENODEV. Every other case keeps the procfs path. --- .../aiagentlocal/model/NativeModelSource.kt | 41 ++++++++++++- .../model/ContentNativeModelSourceTest.kt | 59 +++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt b/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt index 42db2f2fc..ee967d938 100644 --- a/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt +++ b/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt @@ -3,6 +3,8 @@ package com.itsaky.androidide.plugins.aiagentlocal.model import android.content.Context import android.content.Intent import android.net.Uri +import android.os.Environment +import android.provider.DocumentsContract import java.io.Closeable import java.io.File import java.io.FileInputStream @@ -40,7 +42,8 @@ class OpenModelFile( /** * Whether [nativePath] can be opened *by name*, which is all the native loader ever does with * it. That open re-resolves to the real inode against this app's own credentials rather than - * the SAF grant, so removable storage can refuse it where the descriptor was not. ADFA-5253. + * the SAF grant, so removable storage — and, with FUSE passthrough, shared storage — can refuse + * it where the descriptor was not. ADFA-5253, ADFA-7214. */ fun isReopenable(): Boolean = try { openStream()?.use { true } ?: false @@ -204,15 +207,45 @@ class ContentNativeModelSource( * Takes the document's descriptor and hands the native loader its procfs path. `"r"` is the * only mode asked for, which is all the persisted grant covers. The descriptor need not be a * file — a pipe is reported through [OpenModelFile.isSeekable] for the caller to refuse. + * + * With FUSE passthrough (API 36) a shared-storage descriptor is on the lower filesystem, owned + * by MediaProvider, so the procfs path cannot be re-opened by name. The same document's path + * under shared storage can — the IDE holds all-files access — so that is handed over instead, + * when it opens and is the same size. The descriptor is then closed: while MediaProvider holds + * the file open on the lower filesystem it serves FUSE opens as direct I/O, and llama.cpp's + * `mmap` of the path fails with ENODEV. ADFA-7214. */ private fun openDocument(uriString: String): OpenModelFile? = try { - context.contentResolver.openFileDescriptor(Uri.parse(uriString), "r") - ?.let { OpenModelFile("$FD_DIR${it.fd}", it.statSize, it) } + val uri = Uri.parse(uriString) + context.contentResolver.openFileDescriptor(uri, "r")?.let { descriptor -> + val procfs = OpenModelFile("$FD_DIR${descriptor.fd}", descriptor.statSize, descriptor) + if (procfs.isReopenable()) return@let procfs + sharedStoragePathOf(uri) + ?.takeIf { File(it).length() == descriptor.statSize } + ?.let { OpenModelFile(it, descriptor.statSize, null) } + ?.takeIf { it.isReopenable() } + ?.also { procfs.close() } + ?: procfs + } } catch (e: Exception) { onError("could not open the selected model $uriString", e) null } + /** + * The filesystem path of a document on the primary shared volume, from its + * `primary:` id; null for any other provider or volume. + */ + private fun sharedStoragePathOf(uri: Uri): String? = try { + if (uri.authority != EXTERNAL_STORAGE_AUTHORITY) null + else DocumentsContract.getDocumentId(uri) + .split(':', limit = 2) + .takeIf { it.size == 2 && it[0] == PRIMARY_VOLUME } + ?.let { File(Environment.getExternalStorageDirectory(), it[1]).absolutePath } + } catch (_: Exception) { + null + } + /** * One binder round trip for a document, one stat for a path — nothing is read, so this is cheap * enough to ask before every generation. [SourceReachability.GONE] is only ever an answer the @@ -262,5 +295,7 @@ class ContentNativeModelSource( private companion object { const val CONTENT_SCHEME = "content://" const val FD_DIR = "/proc/self/fd/" + const val EXTERNAL_STORAGE_AUTHORITY = "com.android.externalstorage.documents" + const val PRIMARY_VOLUME = "primary" } } diff --git a/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt b/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt index c108ad2b5..23dc6adab 100644 --- a/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt +++ b/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt @@ -4,7 +4,9 @@ import android.content.ContentResolver import android.content.Context import android.net.Uri import android.os.DeadObjectException +import android.os.Environment import android.os.ParcelFileDescriptor +import android.provider.DocumentsContract import io.mockk.every import io.mockk.mockk import io.mockk.mockkStatic @@ -208,6 +210,63 @@ class ContentNativeModelSourceTest { assertFalse(opened.isReopenable()) } + @Test + fun givenASharedStorageDocumentWhoseProcfsPathCannotReopen_whenOpened_thenTheLoaderGetsItsStoragePath() { + // ADFA-7214: with FUSE passthrough the procfs path of a Download file is refused by name, + // but the same file under shared storage opens. fd 99999 stands in for the refused path. + val model = File(temporaryFolder.newFolder("Download"), "model.gguf").apply { writeBytes(ByteArray(64)) } + val opened = openSharedStorageDocument(documentId = "primary:Download/model.gguf", statSize = 64L) + + assertEquals(model.absolutePath, opened.nativePath) + assertEquals(64L, opened.sizeBytes) + assertTrue(opened.isReopenable()) + // Held open, MediaProvider serves the path as direct I/O and llama.cpp's mmap gets ENODEV. + io.mockk.verify { lastDescriptor.close() } + } + + @Test + fun givenASharedStoragePathOfAnotherSize_whenOpened_thenTheProcfsPathIsKept() { + // A different file at that path is not the picked document; the loader must not get it. + File(temporaryFolder.newFolder("Download"), "model.gguf").writeBytes(ByteArray(32)) + + val opened = openSharedStorageDocument(documentId = "primary:Download/model.gguf", statSize = 64L) + + assertEquals("/proc/self/fd/99999", opened.nativePath) + // The procfs path is still the loader's, so its descriptor must stay open. + io.mockk.verify(exactly = 0) { lastDescriptor.close() } + } + + @Test + fun givenADocumentOnARemovableVolume_whenOpened_thenTheProcfsPathIsKept() { + // Only the primary volume maps to a known path; anything else keeps today's behaviour. + File(temporaryFolder.newFolder("Download"), "model.gguf").writeBytes(ByteArray(64)) + + val opened = openSharedStorageDocument(documentId = "1A2B-3C4D:Download/model.gguf", statSize = 64L) + + assertEquals("/proc/self/fd/99999", opened.nativePath) + } + + private lateinit var lastDescriptor: ParcelFileDescriptor + + private fun openSharedStorageDocument(documentId: String, statSize: Long): OpenModelFile { + val uri = mockk(relaxed = true) + every { uri.authority } returns "com.android.externalstorage.documents" + every { Uri.parse(any()) } returns uri + mockkStatic(DocumentsContract::class, Environment::class) + every { DocumentsContract.getDocumentId(uri) } returns documentId + every { Environment.getExternalStorageDirectory() } returns temporaryFolder.root + val descriptor = mockk(relaxed = true) + every { descriptor.fd } returns 99999 + every { descriptor.statSize } returns statSize + every { resolver.openFileDescriptor(uri, "r") } returns descriptor + lastDescriptor = descriptor + try { + return source.open(CONTENT_URI)!! + } finally { + unmockkStatic(DocumentsContract::class, Environment::class) + } + } + @Test fun givenADeletedPath_whenOpened_thenNoHandleIsReturned() { val model = temporaryFolder.newFile("model.gguf") From 152f5be26900b5a1d437aa33a23dc4c7a9820683 Mon Sep 17 00:00:00 2001 From: Hal Eisen Date: Wed, 18 Nov 2026 22:37:31 -0800 Subject: [PATCH 2/2] ADFA-7214 | AI Agent Local: also load msf: documents from the Downloads root A file in Download that DownloadManager did not fetch appears in the picker's Downloads root with a MediaStore id (content://com.android.providers.downloads.documents/document/msf:NNN). DownloadStorageProvider forwards it to MediaProvider, which returns a lower-filesystem descriptor, so it failed exactly like the external-storage case; the previous fix only recognised the external-storage authority. Resolve the shared-storage path from the descriptor itself instead of parsing per-provider document ids: readlink /proc/self/fd/N. Inside the app's mount namespace it already reads /storage/emulated// (measured on API 36; from outside the sandbox the same descriptor reads /data/media//, which is mapped too). The size and reopen checks and closing the descriptor are unchanged. The mapping is a constructor parameter so the tests need no /proc. --- .../aiagentlocal/model/NativeModelSource.kt | 59 +++++++------- .../model/ContentNativeModelSourceTest.kt | 81 ++++++++++++------- 2 files changed, 82 insertions(+), 58 deletions(-) diff --git a/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt b/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt index ee967d938..5485613c4 100644 --- a/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt +++ b/plugins/AI-Agent-Local/src/main/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/NativeModelSource.kt @@ -3,13 +3,13 @@ package com.itsaky.androidide.plugins.aiagentlocal.model import android.content.Context import android.content.Intent import android.net.Uri -import android.os.Environment -import android.provider.DocumentsContract import java.io.Closeable import java.io.File import java.io.FileInputStream import java.io.FileNotFoundException import java.io.InputStream +import java.nio.file.Files +import java.nio.file.Paths /** * A model file held open for the native loader. @@ -153,6 +153,19 @@ internal fun probeFilePath(path: String, onError: (Throwable) -> Unit): SourceRe SourceReachability.UNKNOWN } +/** + * The shared-storage path for a descriptor's link target on the emulated volume, or null for + * anything else (removable storage, app-private files). Inside the app's mount namespace a + * lower-filesystem descriptor already links to `/storage/emulated//` — measured on + * API 36 — while the same descriptor seen from outside the sandbox links to + * `/data/media//`; both name the one file. ADFA-7214. + */ +internal fun sharedStoragePathFor(linkTarget: String): String? = + EMULATED_VOLUME_PATH.matchEntire(linkTarget)?.destructured + ?.let { (user, path) -> "/storage/emulated/$user/$path" } + +private val EMULATED_VOLUME_PATH = Regex("""(?:/storage/emulated|/data/media)/(\d+)/(.+)""") + /** * Opens the user's selected model for the native loader, in place and without copying it. * An interface so the backend's load path can be exercised without a device. @@ -192,10 +205,17 @@ interface NativeModelSource { * [NativeModelSource] over the document provider and the filesystem. * * @param context supplies the resolver holding the picker's persisted read grant + * @param sharedStoragePathOf maps a descriptor's procfs path to the same file's shared-storage + * path, or null; a parameter so tests can stand in for `/proc` and `/storage` * @param onError reports a failed open, so a bare "model unavailable" can still be explained */ class ContentNativeModelSource( private val context: Context, + private val sharedStoragePathOf: (String) -> String? = { procfsPath -> + runCatching { Files.readSymbolicLink(Paths.get(procfsPath)).toString() } + .getOrNull() + ?.let(::sharedStoragePathFor) + }, private val onError: (String, Throwable) -> Unit = { _, _ -> }, ) : NativeModelSource { @@ -208,19 +228,20 @@ class ContentNativeModelSource( * only mode asked for, which is all the persisted grant covers. The descriptor need not be a * file — a pipe is reported through [OpenModelFile.isSeekable] for the caller to refuse. * - * With FUSE passthrough (API 36) a shared-storage descriptor is on the lower filesystem, owned - * by MediaProvider, so the procfs path cannot be re-opened by name. The same document's path - * under shared storage can — the IDE holds all-files access — so that is handed over instead, - * when it opens and is the same size. The descriptor is then closed: while MediaProvider holds - * the file open on the lower filesystem it serves FUSE opens as direct I/O, and llama.cpp's - * `mmap` of the path fails with ENODEV. ADFA-7214. + * Without FUSE passthrough (Android 11+, any device that does not opt in), MediaProvider + * answers for a shared-storage file with a descriptor on the lower filesystem — whether the + * document came from the external-storage provider or as a Downloads `msf:` id. Its procfs + * path then links to `/data/media//…`, a MediaProvider-owned inode this app cannot open + * by name. The same file's `/storage/emulated//…` path can — the IDE holds all-files + * access — so that is handed over instead, when it opens and is the same size. The descriptor + * is then closed: while MediaProvider holds the file open on the lower filesystem it serves + * FUSE opens as direct I/O, and llama.cpp's `mmap` of the path fails with ENODEV. ADFA-7214. */ private fun openDocument(uriString: String): OpenModelFile? = try { - val uri = Uri.parse(uriString) - context.contentResolver.openFileDescriptor(uri, "r")?.let { descriptor -> + context.contentResolver.openFileDescriptor(Uri.parse(uriString), "r")?.let { descriptor -> val procfs = OpenModelFile("$FD_DIR${descriptor.fd}", descriptor.statSize, descriptor) if (procfs.isReopenable()) return@let procfs - sharedStoragePathOf(uri) + sharedStoragePathOf(procfs.nativePath) ?.takeIf { File(it).length() == descriptor.statSize } ?.let { OpenModelFile(it, descriptor.statSize, null) } ?.takeIf { it.isReopenable() } @@ -232,20 +253,6 @@ class ContentNativeModelSource( null } - /** - * The filesystem path of a document on the primary shared volume, from its - * `primary:` id; null for any other provider or volume. - */ - private fun sharedStoragePathOf(uri: Uri): String? = try { - if (uri.authority != EXTERNAL_STORAGE_AUTHORITY) null - else DocumentsContract.getDocumentId(uri) - .split(':', limit = 2) - .takeIf { it.size == 2 && it[0] == PRIMARY_VOLUME } - ?.let { File(Environment.getExternalStorageDirectory(), it[1]).absolutePath } - } catch (_: Exception) { - null - } - /** * One binder round trip for a document, one stat for a path — nothing is read, so this is cheap * enough to ask before every generation. [SourceReachability.GONE] is only ever an answer the @@ -295,7 +302,5 @@ class ContentNativeModelSource( private companion object { const val CONTENT_SCHEME = "content://" const val FD_DIR = "/proc/self/fd/" - const val EXTERNAL_STORAGE_AUTHORITY = "com.android.externalstorage.documents" - const val PRIMARY_VOLUME = "primary" } } diff --git a/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt b/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt index 23dc6adab..054277006 100644 --- a/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt +++ b/plugins/AI-Agent-Local/src/test/kotlin/com/itsaky/androidide/plugins/aiagentlocal/model/ContentNativeModelSourceTest.kt @@ -4,13 +4,12 @@ import android.content.ContentResolver import android.content.Context import android.net.Uri import android.os.DeadObjectException -import android.os.Environment import android.os.ParcelFileDescriptor -import android.provider.DocumentsContract import io.mockk.every import io.mockk.mockk import io.mockk.mockkStatic import io.mockk.unmockkStatic +import io.mockk.verify import java.io.File import org.junit.After import org.junit.Assert.* @@ -211,60 +210,80 @@ class ContentNativeModelSourceTest { } @Test - fun givenASharedStorageDocumentWhoseProcfsPathCannotReopen_whenOpened_thenTheLoaderGetsItsStoragePath() { - // ADFA-7214: with FUSE passthrough the procfs path of a Download file is refused by name, - // but the same file under shared storage opens. fd 99999 stands in for the refused path. - val model = File(temporaryFolder.newFolder("Download"), "model.gguf").apply { writeBytes(ByteArray(64)) } - val opened = openSharedStorageDocument(documentId = "primary:Download/model.gguf", statSize = 64L) + fun givenADocumentWhoseProcfsPathCannotReopen_whenOpened_thenTheLoaderGetsItsStoragePath() { + // ADFA-7214: without FUSE passthrough a Download file's descriptor is on the lower + // filesystem and its procfs path is refused by name, but the same file under /storage + // opens. fd 99999 stands in for the refused path. + val model = temporaryFolder.newFile("model.gguf").apply { writeBytes(ByteArray(64)) } + val asked = mutableListOf() + + val (opened, descriptor) = openDocument(statSize = 64L) { asked += it; model.absolutePath } + assertEquals(listOf("/proc/self/fd/99999"), asked) assertEquals(model.absolutePath, opened.nativePath) assertEquals(64L, opened.sizeBytes) assertTrue(opened.isReopenable()) // Held open, MediaProvider serves the path as direct I/O and llama.cpp's mmap gets ENODEV. - io.mockk.verify { lastDescriptor.close() } + verify { descriptor.close() } } @Test - fun givenASharedStoragePathOfAnotherSize_whenOpened_thenTheProcfsPathIsKept() { + fun givenAStoragePathOfAnotherSize_whenOpened_thenTheProcfsPathIsKept() { // A different file at that path is not the picked document; the loader must not get it. - File(temporaryFolder.newFolder("Download"), "model.gguf").writeBytes(ByteArray(32)) + val other = temporaryFolder.newFile("model.gguf").apply { writeBytes(ByteArray(32)) } - val opened = openSharedStorageDocument(documentId = "primary:Download/model.gguf", statSize = 64L) + val (opened, descriptor) = openDocument(statSize = 64L) { other.absolutePath } assertEquals("/proc/self/fd/99999", opened.nativePath) // The procfs path is still the loader's, so its descriptor must stay open. - io.mockk.verify(exactly = 0) { lastDescriptor.close() } + verify(exactly = 0) { descriptor.close() } } @Test - fun givenADocumentOnARemovableVolume_whenOpened_thenTheProcfsPathIsKept() { - // Only the primary volume maps to a known path; anything else keeps today's behaviour. - File(temporaryFolder.newFolder("Download"), "model.gguf").writeBytes(ByteArray(64)) + fun givenAStoragePathThatDoesNotExist_whenOpened_thenTheProcfsPathIsKept() { + val (opened, descriptor) = openDocument(statSize = 64L) { "/nonexistent/model.gguf" } - val opened = openSharedStorageDocument(documentId = "1A2B-3C4D:Download/model.gguf", statSize = 64L) + assertEquals("/proc/self/fd/99999", opened.nativePath) + verify(exactly = 0) { descriptor.close() } + } + + @Test + fun givenADescriptorWithNoStoragePath_whenOpened_thenTheProcfsPathIsKept() { + // Removable storage, app-private files, anything not on the emulated volume. + val (opened, descriptor) = openDocument(statSize = 64L) { null } assertEquals("/proc/self/fd/99999", opened.nativePath) + verify(exactly = 0) { descriptor.close() } } - private lateinit var lastDescriptor: ParcelFileDescriptor + @Test + fun givenEmulatedVolumeLinkTargets_whenMapped_thenTheStoragePathIsUsed() { + // Inside the app a lower-FS descriptor (external-storage document or Downloads msf: id) + // links to /storage/emulated//; seen from outside the sandbox, /data/media//. + assertEquals( + "/storage/emulated/0/Download/model.gguf", + sharedStoragePathFor("/storage/emulated/0/Download/model.gguf"), + ) + assertEquals( + "/storage/emulated/0/Download/model.gguf", + sharedStoragePathFor("/data/media/0/Download/model.gguf"), + ) + assertEquals("/storage/emulated/10/Models/m.gguf", sharedStoragePathFor("/data/media/10/Models/m.gguf")) + assertNull(sharedStoragePathFor("/mnt/media_rw/1A2B-3C4D/model.gguf")) + assertNull(sharedStoragePathFor("/data/user/0/com.example/files/model.gguf")) + assertNull(sharedStoragePathFor("/storage/emulated/0/")) + } - private fun openSharedStorageDocument(documentId: String, statSize: Long): OpenModelFile { - val uri = mockk(relaxed = true) - every { uri.authority } returns "com.android.externalstorage.documents" - every { Uri.parse(any()) } returns uri - mockkStatic(DocumentsContract::class, Environment::class) - every { DocumentsContract.getDocumentId(uri) } returns documentId - every { Environment.getExternalStorageDirectory() } returns temporaryFolder.root + private fun openDocument( + statSize: Long, + storagePathOf: (String) -> String?, + ): Pair { val descriptor = mockk(relaxed = true) every { descriptor.fd } returns 99999 every { descriptor.statSize } returns statSize - every { resolver.openFileDescriptor(uri, "r") } returns descriptor - lastDescriptor = descriptor - try { - return source.open(CONTENT_URI)!! - } finally { - unmockkStatic(DocumentsContract::class, Environment::class) - } + every { resolver.openFileDescriptor(any(), "r") } returns descriptor + val opened = ContentNativeModelSource(context, sharedStoragePathOf = storagePathOf).open(CONTENT_URI)!! + return opened to descriptor } @Test