Generate SSD truth store event helpers from typed Jerboa

ober

c1a2ee48d4ca80ee12be15005209d3390f8576ff

diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index c4d98d1..306d78f 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -15537,18 +15537,36 @@
       (typed-library (com sfb ssdreview)
         (export truthStoreSha256Hex truthStoreContainedLeaf
                 truthStoreReadLocalText truthStoreTruthPath
-                truthStoreLoadTruth)
+                truthStoreLoadTruth truthStoreSaveSession
+                truthStoreAppendEvent truthStoreApplyTruth)
         (type Exception)
         (type File)
         (type InputStream)
         (type LinkOption)
         (type MessageDigest)
         (type Path)
+        (type SsdSession)
         (type TruthStore)
         (type JSONObject)
         (extern (truthStoreGroundTruthDir
                   (store : TruthStore)) : File
           (kotlin-member-get groundTruthDir))
+        (extern (truthStoreEventsDir
+                  (store : TruthStore)) : File
+          (kotlin-member-get eventsDir))
+        (extern (truthStoreLoadTruthForSession
+                  (store : TruthStore)
+                  (session : SsdSession)) : (Nullable JSONObject)
+          (kotlin-member-call loadTruth))
+        (extern (truthStoreSaveTruthSnapshotRaw
+                  (store : TruthStore)
+                  (truth : JSONObject)) : File
+          (kotlin-member-call saveTruthSnapshot))
+        (extern (truthStoreAppendJsonLineRaw
+                  (store : TruthStore)
+                  (file : File)
+                  (json : JSONObject)) : Unit
+          (kotlin-member-call appendJsonLine))
         (extern (truthStoreFileInputStream
                   (file : File)) : InputStream
           (kotlin-member-call inputStream))
@@ -15651,7 +15669,43 @@
               (nullable-none JSONObject)
               (nullable-some
                 (jsonObjectFromText
-                  (truthStoreReadLocalText file))))))))
+                  (truthStoreReadLocalText file))))))
+        (def (truthStoreSaveSession
+               (store : TruthStore)
+               (session : SsdSession)) : File
+          (truthStoreSaveTruthSnapshotRaw
+            store
+            (ssdSessionToTruthJson session)))
+        (def (truthStoreAppendEvent
+               (store : TruthStore)
+               (type : String)
+               (session : SsdSession)
+               (details : JSONObject)) : Unit
+          (let ((event (sessionEventJson type session details))
+                (eventsDir (truthStoreEventsDir store))
+                (sessionDigest
+                  (truthStoreSha256Hex
+                    (textUtf8Bytes (SsdSession-sessionId session)))))
+            (begin
+              (truthStoreAppendJsonLineRaw
+                store
+                (truthStoreContainedLeaf
+                  eventsDir
+                  (eventsFileName sessionDigest))
+                event)
+              (truthStoreAppendJsonLineRaw
+                store
+                (truthStoreContainedLeaf eventsDir (allEventsFileName))
+                event))))
+        (def (truthStoreApplyTruth
+               (store : TruthStore)
+               (session : SsdSession)) : Unit
+          (let ((truth (truthStoreLoadTruthForSession store session)))
+            (if (truthPresent truth)
+              (applyTruthJsonToSession
+                session
+                (truthJsonObjectOrEmpty truth))
+              (begin))))))
 
     (kotlin-file-lines "com/sfb/ssdreview/TruthStore.kt"
       (
@@ -15701,7 +15755,7 @@
        "    private val root = File(context.filesDir, \"ssd_review\")"
        "    internal val groundTruthDir = File(root, \"ground_truth\")"
        "    private val learnedDir = File(root, \"learned\")"
-       "    private val eventsDir = File(root, \"events\")"
+       "    internal val eventsDir = File(root, \"events\")"
        "    private val reviewsDir = File(root, \"reviews\")"
        "    private val remoteConfig: RemoteConfig? by lazy { loadRemoteConfig() }"
        "    @Volatile internal var seedStarted = false"
@@ -15760,7 +15814,7 @@
        "    }"
        ""
        "    fun saveSession(session: SsdSession): File ="
-       "        saveTruthSnapshot(ssdSessionToTruthJson(session))"
+       "        truthStoreSaveSession(this, session)"
        ""
        "    @Synchronized"
        "    fun saveTruthSnapshot(truth: JSONObject): File {"
@@ -15781,17 +15835,11 @@
        "        return truthFile"
        "    }"
        ""
-       "    fun appendEvent(type: String, session: SsdSession, details: JSONObject = emptyJsonObject()) {"
-       "        val event = sessionEventJson(type, session, details)"
-       "        appendJsonLine(containedLeaf(eventsDir, eventsFileName(sha256Hex(session.sessionId.toByteArray()))), event)"
-       "        appendJsonLine(containedLeaf(eventsDir, allEventsFileName()), event)"
-       "    }"
+       "    fun appendEvent(type: String, session: SsdSession, details: JSONObject = emptyJsonObject()) ="
+       "        truthStoreAppendEvent(this, type, session, details)"
        ""
-	       "    fun applyTruth(session: SsdSession) {"
-	       "        val truth = loadTruth(session)"
-	       "        if (!truthPresent(truth)) return"
-	       "        applyTruthJsonToSession(session, truthJsonObjectOrEmpty(truth))"
-	       "    }"
+       "    fun applyTruth(session: SsdSession) ="
+       "        truthStoreApplyTruth(this, session)"
        ""
        "    private fun sha256Hex(bytes: ByteArray): String ="
        "        truthStoreSha256Hex(bytes)"
@@ -16401,7 +16449,7 @@
        "        else -> null"
        "    }"
        ""
-       "    private fun appendJsonLine(file: File, json: JSONObject) {"
+       "    internal fun appendJsonLine(file: File, json: JSONObject) {"
        "        val previous = if (fileExists(file)) readLocalText(file) else \"\""
        "        val next = previous + json.toString() + \"\\n\""
        "        require(textUtf8BytesWithinLimit(next, MAX_ZIP_ENTRY_BYTES)) { \"Event log quota exceeded\" }"