refactor(context): support copy attributes on root tag into snapshot element

This commit is contained in:
2026-03-29 17:28:41 +08:00
parent 247052e318
commit 1c995923a1
2 changed files with 54 additions and 4 deletions

View File

@@ -299,11 +299,14 @@ private class AggregatedBlockContent(
val tagName = if (index == snapshotIndex) "snapshot" else "history_snapshot" val tagName = if (index == snapshotIndex) "snapshot" else "history_snapshot"
val wrapper = document.createElement(tagName) val wrapper = document.createElement(tagName)
val renderedBlock = groupedBlock.renderedBlock val renderedBlock = groupedBlock.renderedBlock
wrapper.setAttribute("source", renderedBlock.source) val encoded = renderedBlock.encodeToXml()
wrapper.setAttribute("urgency", renderedBlock.urgency.name.lowercase(Locale.ROOT)) val attributes = encoded.attributes
for (attributeIndex in 0 until attributes.length) {
val attribute = attributes.item(attributeIndex)
wrapper.setAttribute(attribute.nodeName, attribute.nodeValue)
}
root.appendChild(wrapper) root.appendChild(wrapper)
val encoded = renderedBlock.encodeToXml()
val childNodes = encoded.childNodes val childNodes = encoded.childNodes
for (childIndex in 0 until childNodes.length) { for (childIndex in 0 until childNodes.length) {
wrapper.appendChild(document.importNode(childNodes.item(childIndex), true)) wrapper.appendChild(document.importNode(childNodes.item(childIndex), true))

View File

@@ -109,6 +109,41 @@ class ContextWorkspaceTest {
assertTrue(aggregatedXml.contains("<content>older</content>")) assertTrue(aggregatedXml.contains("<content>older</content>"))
} }
@Test
fun `aggregated snapshots preserve rendered block root attributes`() {
val manager = ContextWorkspace()
manager.register(
ContextBlock(
blockContent = AttributedTestBlockContent("memory", "main", "older", "historic"),
compactBlock = AttributedTestBlockContent("memory", "main", "older-compact", "historic"),
abstractBlock = AttributedTestBlockContent("memory", "main", "older-abstract", "historic"),
visibleTo = setOf(ContextBlock.VisibleDomain.MEMORY),
replaceFadeFactor = 20.0,
timeFadeFactor = 0.0,
activateFactor = 0.0
)
)
manager.register(
ContextBlock(
blockContent = AttributedTestBlockContent("memory", "main", "newer", "latest"),
compactBlock = AttributedTestBlockContent("memory", "main", "newer-compact", "latest"),
abstractBlock = AttributedTestBlockContent("memory", "main", "newer-abstract", "latest"),
visibleTo = setOf(ContextBlock.VisibleDomain.MEMORY),
replaceFadeFactor = 20.0,
timeFadeFactor = 0.0,
activateFactor = 0.0
)
)
val resolved = manager.resolve(listOf(ContextBlock.VisibleDomain.MEMORY))
val aggregatedXml = resolved.blocks.single().encodeToXmlString()
assertTrue(aggregatedXml.contains("<snapshot category=\"latest\" source=\"main\" urgency=\"normal\">"))
assertTrue(aggregatedXml.contains("<history_snapshot category=\"historic\" source=\"main\" urgency=\"normal\">"))
assertTrue(aggregatedXml.contains("<content>newer</content>"))
assertTrue(aggregatedXml.contains("<content>older</content>"))
}
@Test @Test
fun `register fades matching source blocks and removes zero score ones`() { fun `register fades matching source blocks and removes zero score ones`() {
val manager = ContextWorkspace() val manager = ContextWorkspace()
@@ -420,7 +455,7 @@ class ContextWorkspaceTest {
) )
} }
private class TestBlockContent( private open class TestBlockContent(
blockName: String, blockName: String,
source: String, source: String,
val content: String, val content: String,
@@ -430,4 +465,16 @@ class ContextWorkspaceTest {
appendTextElement(document, root, "content", content) appendTextElement(document, root, "content", content)
} }
} }
private class AttributedTestBlockContent(
blockName: String,
source: String,
content: String,
private val category: String,
urgency: Urgency = Urgency.NORMAL
) : TestBlockContent(blockName, source, content, urgency) {
override fun appendRootAttributes(): Map<String, String> {
return super.appendRootAttributes() + ("category" to category)
}
}
} }