feat(context): compression pipeline stage 1 — level policy + deterministic stages

Additive CompressionPolicy (levels 1-9), ProtectedSpanTagger (regex
load-bearing spans), FormatCompressor (json→dotted lines), ContextClassifier
(static/structured/freeform). Stage 1 (FORMAT_COMPRESS) wired into
DefaultContextPackBuilder behind the policy; structured entries only, lossless.
PromptRenderer repetition-anchoring of steering directives.
This commit is contained in:
2026-07-01 13:53:19 +04:00
parent 4be8f292ae
commit 61be90070f
8 changed files with 302 additions and 2 deletions
@@ -0,0 +1,78 @@
import com.correx.core.context.compression.CompressionPolicy
import com.correx.core.context.compression.CompressionStage
import com.correx.core.context.compression.ContextClass
import com.correx.core.context.compression.ContextClassifier
import com.correx.core.context.compression.FormatCompressor
import com.correx.core.context.compression.ProtectedSpanTagger
import com.correx.core.context.model.ContextEntry
import com.correx.core.context.model.ContextLayer
import com.correx.core.context.model.EntryRole
import com.correx.core.events.types.ContextEntryId
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
class CompressionPipelineStagesTest {
private fun entry(sourceType: String, layer: ContextLayer, role: EntryRole, content: String = "x") =
ContextEntry(
id = ContextEntryId("e"),
layer = layer,
content = content,
sourceType = sourceType,
sourceId = "s",
tokenEstimate = 1,
role = role,
)
@Test
fun `levels are additive`() {
val l1 = CompressionPolicy(1)
assertTrue(l1.enabled(CompressionStage.FORMAT_COMPRESS))
assertFalse(l1.enabled(CompressionStage.CACHE_STATIC))
val l7 = CompressionPolicy(7)
// includes everything from 1..7
assertTrue(l7.enabled(CompressionStage.FORMAT_COMPRESS))
assertTrue(l7.enabled(CompressionStage.FACT_SHEET))
assertTrue(l7.enabled(CompressionStage.PROTECTED_SPAN_TAG))
assertFalse(l7.enabled(CompressionStage.TOME_MERGE)) // level 8
}
@Test
fun `protected spans capture ids numbers paths and code`() {
val tagger = ProtectedSpanTagger(extraEntities = listOf("GatedDeltaNet"))
val spans = tagger.protectedSpans(
"connect to 10.0.0.1 at path /etc/nginx.conf value 0xFF port = 8080 " +
"`inline` on GatedDeltaNet"
)
assertTrue(spans.any { it == "10.0.0.1" }, spans.toString())
assertTrue(spans.any { it.contains("/etc/nginx.conf") }, spans.toString())
assertTrue(spans.any { it == "0xFF" }, spans.toString())
assertTrue(spans.any { it == "`inline`" }, spans.toString())
assertTrue(spans.contains("GatedDeltaNet"), spans.toString())
assertTrue(spans.any { it == "8080" }, spans.toString())
}
@Test
fun `format compressor flattens json and preserves exact values`() {
val out = FormatCompressor().compress("""{"a":{"b":1},"c":"10.0.0.1"}""")
assertEquals("a.b = 1\nc = 10.0.0.1", out)
}
@Test
fun `format compressor leaves non-json content untouched`() {
val out = FormatCompressor().compress("log line\nlog line\nother")
assertEquals("log line\nlog line\nother", out)
}
@Test
fun `classifier separates static structured freeform`() {
val c = ContextClassifier()
assertEquals(ContextClass.STATIC, c.classify(entry("systemPrompt", ContextLayer.L0, EntryRole.SYSTEM)))
assertEquals(ContextClass.STRUCTURED, c.classify(entry("toolLog", ContextLayer.L2, EntryRole.TOOL)))
assertEquals(ContextClass.STRUCTURED, c.classify(entry("steeringNote", ContextLayer.L2, EntryRole.SYSTEM)))
assertEquals(ContextClass.FREEFORM, c.classify(entry("chat", ContextLayer.L1, EntryRole.USER)))
}
}