Generate SSD remote truth pulls from typed Jerboa

ober

50da850f8ae034aa93ab337d8d2e788ab2a95a1d

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index ec804bb..248f93a 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -15594,6 +15594,7 @@
                 truthStoreTruthIndexEntriesLocal
                 truthStoreTruthIndexEntryLocal
                 truthStoreLoadTruthForSessionLocal
+                truthStorePullRemoteTruthLocal
                 truthStoreExportZipLocal
                 truthStoreImportZipLocal
                 truthStoreExportToTreeLocal
@@ -16579,6 +16580,138 @@
                     (truthStoreTruthIndexEntryFile
                       (nullable-get best)))))
               (nullable-none JSONObject))))
+        (def (truthStoreRemoteImportCount
+               (first : Bool)
+               (second : Bool)) : Int32
+          (+ (if first (int32 1) (int32 0))
+             (if second (int32 1) (int32 0))))
+        (def (truthStoreWriteSameShipCachedRemoteTruthLocal
+               (store : TruthStore)
+               (session : SsdSession)
+               (remoteTruth : JSONObject)
+               (modified : Int)) : Bool
+          (if (sameShipTruth session remoteTruth)
+            (truthStoreWriteRemoteTruthLocal
+              store
+              (truthForSession session remoteTruth)
+              (nullableByteCountOrDefault
+                (truthStoreTruthTimeLocal remoteTruth)
+                modified)
+              #t)
+            #f))
+        (def (truthStorePullRemoteSingleTruthLocal
+               (store : TruthStore)
+               (session : SsdSession)
+               (payload : JSONObject)
+               (compactTruth : (Nullable JSONObject))) : Int32
+          (let ((remoteTruth
+                  (remoteTruthPayloadTruthOrEmpty compactTruth))
+                (modified
+                  (remoteTruthPayloadMatchModified payload)))
+            (let ((imported
+                    (truthStoreRemoteImportCount
+                      (truthStoreWriteRemoteTruthLocal
+                        store
+                        remoteTruth
+                        modified
+                        #f)
+                      (truthStoreWriteSameShipCachedRemoteTruthLocal
+                        store
+                        session
+                        remoteTruth
+                        modified))))
+              (begin
+                (if (positiveCount imported)
+                  (truthStoreRebuildLearnedExamplesLocal store)
+                  (begin))
+                (remoteTruthPayloadCount payload)))))
+        (def (truthStorePullRemoteEntriesLocal
+               (store : TruthStore)
+               (session : SsdSession)
+               (entries : JSONArray)) : Int32
+          (begin
+            (if (zipEntryCountExceeded
+                  (remoteTruthEntriesCount entries)
+                  (truthStoreMaxZipEntries))
+              (throw
+                (truthStoreIllegalStateException
+                  "Too many remote truth entries")
+                Unit)
+              (begin))
+            (let ((imported
+                    (countImportedRemoteTruthEntries
+                      entries
+                      (lambda ((truth : JSONObject) (modified : Int))
+                        (truthStoreWriteRemoteTruthLocal
+                          store
+                          truth
+                          modified
+                          #f))))
+                  (bestForSession
+                    (bestRemoteTruthForSession session entries)))
+              (let ((totalImported
+                      (if (remoteTruthPayloadHasTruth bestForSession)
+                        (let ((truth
+                                (remoteTruthPayloadTruthOrEmpty
+                                  bestForSession)))
+                          (if (truthStoreWriteRemoteTruthLocal
+                                store
+                                (truthForSession session truth)
+                                (remoteTruthTimestamp truth)
+                                #t)
+                            (+ imported (int32 1))
+                            imported))
+                        imported)))
+                (begin
+                  (if (positiveCount totalImported)
+                    (truthStoreRebuildLearnedExamplesLocal store)
+                    (begin))
+                  (remoteTruthEntriesCount entries))))))
+        (def (truthStorePullRemoteResponseLocal
+               (store : TruthStore)
+               (session : SsdSession)
+               (connection : HttpsURLConnection)) : Int32
+          (let ((code (truthStoreConnectionResponseCode connection)))
+            (if (httpStatusFailed code)
+              (begin
+                (closeInputStreamIfPresent
+                  (truthStoreConnectionErrorStream connection))
+                (int32 0))
+              (let ((payload
+                      (jsonObjectFromText
+                        (truthStoreReadResponseTextLocal connection))))
+                (let ((compactTruth
+                        (remoteTruthPayloadTruth payload)))
+                  (if (remoteTruthPayloadHasTruth compactTruth)
+                    (truthStorePullRemoteSingleTruthLocal
+                      store
+                      session
+                      payload
+                      compactTruth)
+                    (truthStorePullRemoteEntriesLocal
+                      store
+                      session
+                      (remoteTruthPayloadTruths payload))))))))
+        (def (truthStorePullRemoteTruthLocal
+               (store : TruthStore)
+               (session : SsdSession)) : Int32
+          (try
+            (let ((connection
+                    (truthStoreOpenPinnedConnectionRaw
+                      store
+                      (remoteTruthQuery session)
+                      "GET"
+                      (nullable-none Int)
+                      (nullable-none String)
+                      #t)))
+              (try-finally
+                (truthStorePullRemoteResponseLocal
+                  store
+                  session
+                  connection)
+                (truthStoreConnectionDisconnect connection)))
+            (catch (error : Exception)
+              (int32 0))))
         (def (truthStoreCopyInputToZipOutput
                (input : InputStream)
                (zip : ZipOutputStream)
@@ -18077,55 +18210,8 @@
        "        }.socketFactory"
        "    }"
        ""
-       "    fun pullRemoteTruth(session: SsdSession): Int {"
-       "        return try {"
-       "            val relative = remoteTruthQuery(session)"
-       "            val connection = openPinnedConnection(relative, \"GET\", acceptGzip = true)"
-       "            try {"
-       "            val code = connection.responseCode"
-       "            if (httpStatusFailed(code)) {"
-       "                closeInputStreamIfPresent(connection.errorStream)"
-       "                return 0"
-       "            }"
-       "            val response = readResponseText(connection)"
-       "            val payload = jsonObjectFromText(response)"
-       "            val compactTruth = remoteTruthPayloadTruth(payload)"
-       "            if (remoteTruthPayloadHasTruth(compactTruth)) {"
-       "                val remoteTruth = remoteTruthPayloadTruthOrEmpty(compactTruth)"
-       "                val modified = remoteTruthPayloadMatchModified(payload)"
-       "                var imported = 0"
-       "                if (writeRemoteTruth(remoteTruth, modified)) imported += 1"
-       "                if (sameShipTruth(session, remoteTruth)) {"
-       "                    val cached = truthForSession(session, remoteTruth)"
-       "                    if (writeRemoteTruth(cached, nullableByteCountOrDefault(truthTime(remoteTruth), modified), replaceIfMoreComplete = true)) {"
-       "                        imported += 1"
-       "                    }"
-       "                }"
-       "                if (positiveCount(imported)) rebuildLearnedExamples()"
-       "                return remoteTruthPayloadCount(payload)"
-       "            }"
-       "            val entries = remoteTruthPayloadTruths(payload)"
-       "            if (zipEntryCountExceeded(remoteTruthEntriesCount(entries), MAX_ZIP_ENTRIES)) throw IllegalStateException(\"Too many remote truth entries\")"
-       "            var imported = countImportedRemoteTruthEntries(entries) { truth, modified ->"
-       "                writeRemoteTruth(truth, modified)"
-       "            }"
-       "            val bestForSession = bestRemoteTruthForSession(session, entries)"
-       "            if (remoteTruthPayloadHasTruth(bestForSession)) {"
-       "                val truth = remoteTruthPayloadTruthOrEmpty(bestForSession)"
-       "                val cached = truthForSession(session, truth)"
-       "                if (writeRemoteTruth(cached, remoteTruthTimestamp(truth), replaceIfMoreComplete = true)) {"
-       "                    imported += 1"
-       "                }"
-       "            }"
-       "            if (positiveCount(imported)) rebuildLearnedExamples()"
-       "                remoteTruthEntriesCount(entries)"
-       "            } finally {"
-       "                connection.disconnect()"
-       "            }"
-       "        } catch (_: Exception) {"
-       "            0"
-       "        }"
-       "    }"
+       "    fun pullRemoteTruth(session: SsdSession): Int ="
+       "        truthStorePullRemoteTruthLocal(this, session)"
        ""
        "    private fun readResponseText(connection: HttpsURLConnection): String ="
        "        truthStoreReadResponseTextLocal(connection)"