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..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 @@ -8,6 +8,8 @@ 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. @@ -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 @@ -150,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. @@ -189,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 { @@ -204,10 +227,27 @@ 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. + * + * 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 { - context.contentResolver.openFileDescriptor(Uri.parse(uriString), "r") - ?.let { OpenModelFile("$FD_DIR${it.fd}", it.statSize, it) } + 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(procfs.nativePath) + ?.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 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..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 @@ -9,6 +9,7 @@ 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.* @@ -208,6 +209,83 @@ class ContentNativeModelSourceTest { assertFalse(opened.isReopenable()) } + @Test + 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. + verify { descriptor.close() } + } + + @Test + fun givenAStoragePathOfAnotherSize_whenOpened_thenTheProcfsPathIsKept() { + // A different file at that path is not the picked document; the loader must not get it. + val other = temporaryFolder.newFile("model.gguf").apply { writeBytes(ByteArray(32)) } + + 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. + verify(exactly = 0) { descriptor.close() } + } + + @Test + fun givenAStoragePathThatDoesNotExist_whenOpened_thenTheProcfsPathIsKept() { + val (opened, descriptor) = openDocument(statSize = 64L) { "/nonexistent/model.gguf" } + + 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() } + } + + @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 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(any(), "r") } returns descriptor + val opened = ContentNativeModelSource(context, sharedStoragePathOf = storagePathOf).open(CONTENT_URI)!! + return opened to descriptor + } + @Test fun givenADeletedPath_whenOpened_thenNoHandleIsReturned() { val model = temporaryFolder.newFile("model.gguf")