fix: hold a payload whose parent has not arrived instead of losing the event

A receiver hit `FOREIGN KEY constraint failed` on an artifact submission
and lost the whole group event. The artifact referenced a dialect the
receiver did not have, MantraArtifact.dialectId is a foreign key, and
SQLite answers a violated constraint by aborting -- which rolled back
the entire transaction the inbound pipeline runs in. Gone with it: the
NostrEvent, the MarmotGroupEvent, the submission's MarmotInnerEvent
holding the payload verbatim, and the transcript line. Nothing retries,
so the artifact stayed lost even once the dialect turned up.

Every nip30303 entity is a child of another and the schema enforces all
of it -- artifact→dialect, version→artifact, chapter→version,
chunk→chapter, translations→both of theirs -- so this was every branch,
not one.

And submissions make arriving before your parent ordinary rather than
exotic. That is the point of them: an admin submits a backlog in
whatever order they hold it, and a member who joined last week can be
sent what the group was told last month. Both produce payloads whose
parents are not here yet, and both were losing data.

So check the parents before inserting. A payload that arrives early is
held on the submission row -- awaitingEventId names what it waits for --
and applied when that arrives. Releasing one can release another, a
version freeing its chapters and those freeing their chunks, so it walks
outward until nothing more comes unstuck. A payload with a second parent
still missing is re-pointed at that one rather than retried on every
arrival.

Nothing is written to the transcript while a payload is held. Nobody has
said anything yet; the line appears when it is applied, in the position
its own timestamp gives it.

Two things fall out of the shape:

parentRefsOf is pure and separate from the lookups, because the mapping
is the part that can silently drift from the schema and there is no
database harness in commonTest to catch it. ParentRefsTest pins one case
per kind. Which table an id lives in is carried as the kind of event
that would have created it, so there is no second enum to keep in step.

applyInnerEvent takes ids rather than a GroupEvent, since replay happens
long after that object is gone. A released payload is recorded as not
ours: we hold the parents of anything we wrote, having written those too.

Also reconstructs a held bare nip30303 event from its own columns rather
than parsing its content as an event -- only submissions carry an event
there, and reading both that way would have stranded every bare one
permanently.

Verified: the v5→v6 migration runs clean on the receiver's real
populated database. The hold path itself still needs a fresh submission
from a sender to exercise end to end.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso Ngako
2026-09-05 21:05:35 +02:00
parent 2d0fe6f5fc
commit d7aac49cf1
7 changed files with 5565 additions and 46 deletions

View File

@@ -0,0 +1,167 @@
package press.mantra.compose.database.model
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import press.mantra.compose.nostr.nip30303.ArtifactEvent
import press.mantra.compose.nostr.nip30303.ArtifactVersionEvent
import press.mantra.compose.nostr.nip30303.ChapterEvent
import press.mantra.compose.nostr.nip30303.ChunkEvent
import press.mantra.compose.nostr.nip30303.DialectEvent
import press.mantra.compose.nostr.nip30303.TranslationArtifactVersionEvent
import press.mantra.compose.nostr.nip30303.TranslationChapterEvent
import press.mantra.compose.nostr.nip30303.TranslationChunkEvent
import press.mantra.compose.nostr.nip30303.tags.ArtifactIdTag
/**
* What the inbound side must wait for before it writes a row.
*
* These have to match the foreign keys on the Mantra* entities exactly. A
* parent claimed here that the schema does not enforce holds a payload back
* for nothing; one the schema enforces but that is missing here is an insert
* that violates a constraint, and SQLite answers that by rolling back the
* whole inbound transaction -- losing the group event, the submission and the
* transcript line, none of which is ever retried. So the mapping is asserted
* rather than trusted to stay in step.
*/
class ParentRefsTest {
private val author = "a".repeat(64)
private val dialect = "d".repeat(64)
private val artifact = "1".repeat(64)
private val version = "2".repeat(64)
private val chapter = "3".repeat(64)
private val chunk = "4".repeat(64)
private val translationVersion = "5".repeat(64)
private val translationChapter = "6".repeat(64)
private fun eventOf(template: EventTemplate<*>) = Event(
id = "f".repeat(64),
pubKey = author,
createdAt = template.createdAt,
kind = template.kind,
tags = template.tags,
content = template.content,
sig = "",
)
private fun refsOf(template: EventTemplate<*>) =
ChatMessage.parentRefsOf(eventOf(template)).map { it.id to it.kind }
@Test
fun `an artifact waits for its dialect`() {
val refs = refsOf(
ArtifactEvent.build(
name = "In Detention",
url = "example.com",
visibility = "private",
license = "cc",
dialectId = dialect,
)
)
assertEquals(listOf(dialect to DialectEvent.KIND), refs)
}
@Test
fun `a version waits for its artifact`() {
val refs = refsOf(
ArtifactVersionEvent.build(content = "1.0") {
addUnique(ArtifactIdTag.assemble(artifact))
}
)
assertEquals(listOf(artifact to ArtifactEvent.KIND), refs)
}
@Test
fun `a chapter waits for its version and a chunk for its chapter`() {
val chapterRefs = refsOf(
ChapterEvent.build(
artifactVersionId = version,
name = "Chapter 1",
originalText = "text",
index = 0,
wordCount = 1,
characterCount = 4,
)
)
val chunkRefs = refsOf(
ChunkEvent.build(
chapterId = chapter,
text = "text",
index = 0,
wordCount = 1,
characterCount = 4,
)
)
assertEquals(listOf(version to ArtifactVersionEvent.KIND), chapterRefs)
assertEquals(listOf(chapter to ChapterEvent.KIND), chunkRefs)
}
@Test
fun `a translation waits for both of its parents`() {
val refs = refsOf(
TranslationArtifactVersionEvent.build(
artifactVersionId = version,
dialectId = dialect,
name = "Sesotho",
visibility = "private",
license = "cc",
)
)
assertEquals(
listOf(
version to ArtifactVersionEvent.KIND,
dialect to DialectEvent.KIND,
),
refs
)
}
@Test
fun `a translated chapter and chunk each wait for both of their parents`() {
val chapterRefs = refsOf(
TranslationChapterEvent.build(
translationArtifactVersionId = translationVersion,
chapterId = chapter,
index = 0,
)
)
val chunkRefs = refsOf(
TranslationChunkEvent.build(
translationChapterId = translationChapter,
chunkId = chunk,
index = 0,
text = "translated",
)
)
assertEquals(
listOf(
translationVersion to TranslationArtifactVersionEvent.KIND,
chapter to ChapterEvent.KIND,
),
chapterRefs
)
assertEquals(
listOf(
translationChapter to TranslationChapterEvent.KIND,
chunk to ChunkEvent.KIND,
),
chunkRefs
)
}
@Test
fun `a dialect waits for nothing, so it can always start a group off`() {
val refs = refsOf(
DialectEvent.build(name = "Sesotho", country = "Lesotho", language = "st")
)
assertTrue(refs.isEmpty())
}
}