Modern Apps git

← Modern-Apps.git

translate: model packs pin their file sizes so truncated downloads are caught

Commit
3ce8b5410947ab72425f9e3266608cc5ccac0aea
Author
Vayun Mathur <[email protected]>
When
2026-12-01 14:57 UTC (4 days ago)
Parents
4d4a39f
Both packs documented verified byte sizes in comments but
item() never passed expectedBytes (null), so the download
gate's size check never ran: a truncated file (1 byte after
an interrupted install) with stale verified markers passed
the gate, the handle failed to load, and Failed's Retry only
reloaded the engine — the download screen was never offered
again. The size pin exists precisely to catch this without
rehashing gigabytes every start, and the Gemma pack already
pinned it.

item() now takes and passes expectedBytes in both packs
(NLLB 617,059,520/3,849,114; MADLAD 1,015,713,728/
3,638,800). Regression test pins each file's size by name
in both packs. Language routing and history were audited
and cleared. Verified: :translate:testDevUnitTest 156 tests
pass; lint errors are pre-existing on untouched files.

Diff

translate/src/main/java/com/vayunmathur/translate/platform/MadladModel.kt
diff --git a/translate/src/main/java/com/vayunmathur/translate/platform/MadladModel.kt b/translate/src/main/java/com/vayunmathur/translate/platform/MadladModel.ktindex 3bd515955f..7950b942e6 100644--- a/translate/src/main/java/com/vayunmathur/translate/platform/MadladModel.kt+++ b/translate/src/main/java/com/vayunmathur/translate/platform/MadladModel.kt@@ -26,23 +26,25 @@ object MadladModel {     private const val BASE = "https://data.maproj.org/models/madlad400/"     const val DIR = "madlad400" -    /** The 2 runtime files, SHA-256 pinned. Names and order come from [MadladHandle.FILES]. */+    /** The 2 runtime files, SHA-256 and size pinned. Names and order come from [MadladHandle.FILES]. */     val FILES: List<ModelDownloadItem> = listOf(         item(             MadladHandle.GRAPH,-            // 1,015,713,728 bytes, the Q2_K port (`madlad400-q2k` graph, same graph id 25).+            // The Q2_K port (`madlad400-q2k` graph, same graph id 25).             // Verified against build/madlad400-q2k/madlad400.maml, uploaded to the mirror.             "76991c58c3b8fd5bce8e5963dcba7f0d371afc65385ecd9bdd7701a789bd407b",+            1_015_713_728L,         ),         item(             MadladHandle.TOKENIZER,-            // 3,638,800 bytes, verified against build/madlad400-q2k/tokenizer.bin.+            // Verified against build/madlad400-q2k/tokenizer.bin.             "477abfdd0e236a20c4c960b6768ed965ce44fa250e22f2b504f2beafac6a2c5d",+            3_638_800L,         ),     ) -    private fun item(name: String, sha256: String?) =-        ModelDownloadItem("$BASE$name", "$DIR/$name", "MADLAD400 $name", sha256)+    private fun item(name: String, sha256: String?, expectedBytes: Long) =+        ModelDownloadItem("$BASE$name", "$DIR/$name", "MADLAD400 $name", sha256, expectedBytes)      /** Directory [MadladHandle.inDirectory] loads from. */     fun modelDir(context: Context): File = File(context.getExternalFilesDir(null), DIR)
translate/src/main/java/com/vayunmathur/translate/platform/NllbModel.kt
diff --git a/translate/src/main/java/com/vayunmathur/translate/platform/NllbModel.kt b/translate/src/main/java/com/vayunmathur/translate/platform/NllbModel.ktindex e59613085b..56c8ebc489 100644--- a/translate/src/main/java/com/vayunmathur/translate/platform/NllbModel.kt+++ b/translate/src/main/java/com/vayunmathur/translate/platform/NllbModel.kt@@ -26,17 +26,19 @@ object NllbModel {     private const val BASE = "https://data.maproj.org/models/nllb600/"     const val DIR = "nllb600" -    /** The 2 runtime files, SHA-256 pinned. Names and order come from [NllbHandle.FILES]. */+    /** The 2 runtime files, SHA-256 and size pinned. Names and order come from [NllbHandle.FILES]. */     val FILES: List<ModelDownloadItem> = listOf(         item(             NllbHandle.GRAPH,-            // 617,059,520 bytes, verified against build/nllb600/nllb600.maml.+            // Verified against build/nllb600/nllb600.maml.             "1f08cebe3cb6e629fc40fbc93c71c82c4e23cd379fe0da798ebc817cc769553c",+            617_059_520L,         ),         item(             NllbHandle.TOKENIZER,-            // 3,849,114 bytes, verified against build/nllb600/tokenizer.bin.+            // Verified against build/nllb600/tokenizer.bin.             "36a6bed003d4a66cb9a513d1056355fe4bc1c73518cff82477671811fd482b57",+            3_849_114L,         ),     ) @@ -62,8 +64,8 @@ object NllbModel {         "pos_weights.f32.bin",     ) -    private fun item(name: String, sha256: String?) =-        ModelDownloadItem("$BASE$name", "$DIR/$name", "NLLB-200 $name", sha256)+    private fun item(name: String, sha256: String?, expectedBytes: Long) =+        ModelDownloadItem("$BASE$name", "$DIR/$name", "NLLB-200 $name", sha256, expectedBytes)      /** Directory [NllbHandle.inDirectory] loads from. */     fun modelDir(context: Context): File = File(context.getExternalFilesDir(null), DIR)
translate/src/test/java/com/vayunmathur/translate/platform/ModelPackSpecTest.kt
diff --git a/translate/src/test/java/com/vayunmathur/translate/platform/ModelPackSpecTest.kt b/translate/src/test/java/com/vayunmathur/translate/platform/ModelPackSpecTest.ktnew file mode 100644index 0000000000..285b00117d--- /dev/null+++ b/translate/src/test/java/com/vayunmathur/translate/platform/ModelPackSpecTest.kt@@ -0,0 +1,42 @@+package com.vayunmathur.translate.platform++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull++/**+ * The model-pack download specs must pin the exact on-disk size of every file,+ * not only its SHA-256: the launch gate checks the size beside the verified+ * marker so a truncated file left by an interrupted install is re-downloaded+ * instead of passing the gate and then failing to load forever. Regression:+ * both packs documented their verified sizes in comments but never pinned+ * them, so a short file with a stale verified marker read as downloaded.+ */+class ModelPackSpecTest {++    @Test+    fun `nllb pack pins the verified size of each stated file`() {+        val byFile = NllbModel.FILES.associateBy { it.fileName }+        assertEquals(2, byFile.size)+        // build/nllb600/nllb600.maml and build/nllb600/tokenizer.bin, as+        // verified for the SHA-256 pins in NllbModel.+        assertEquals(617_059_520L, byFile.getValue("nllb600/nllb600.maml").expectedBytes)+        assertEquals(3_849_114L, byFile.getValue("nllb600/tokenizer.bin").expectedBytes)+        for (item in NllbModel.FILES) {+            assertNotNull(item.expectedBytes, item.fileName)+        }+    }++    @Test+    fun `madlad pack pins the verified size of each stated file`() {+        val byFile = MadladModel.FILES.associateBy { it.fileName }+        assertEquals(2, byFile.size)+        // build/madlad400-q2k/madlad400.maml and tokenizer.bin, as verified+        // for the SHA-256 pins in MadladModel.+        assertEquals(1_015_713_728L, byFile.getValue("madlad400/madlad400.maml").expectedBytes)+        assertEquals(3_638_800L, byFile.getValue("madlad400/tokenizer.bin").expectedBytes)+        for (item in MadladModel.FILES) {+            assertNotNull(item.expectedBytes, item.fileName)+        }+    }+}