Files
ARTEX/chatbridge/outbound_nested_fence_test.go
BaaRai a70c0d74f9 Drop nested code fences whole in chat bridge excerpts
SafeText paired any run of three backticks or tildes with the next run of
either kind, so a longer fence quoting markdown was closed at its first inner
run and the rest of the quoted evidence reached the chat provider as prose.

Fences now follow the CommonMark rules: a fence line has at most three spaces
before its run, and its block ends only on a line holding a run of the same
character at least as long and nothing else; an unclosed fence drops to the
end. Other runs (after text, inside a blockquote or list item, in text
collapsed to one line) are dropped up to the latest end a code span or a
container fence could have, and a fence line met first starts its own block.
Ambiguous markdown errs toward dropping.
2026-12-04 01:51:11 +01:00

150 lines
4.5 KiB
Go

package chatbridge
import (
"strings"
"testing"
)
// A fence closes only on a run of the same character at least as long as the
// one that opened it (CommonMark). Agents quote markdown evidence inside a
// longer fence, so an inner ``` must not end the omitted block early.
func TestSafeTextNestedFences(t *testing.T) {
cases := []struct {
name string
in string
leak []string
keep []string
}{
{
"4-backtick fence around an http block",
"Summary line.\n````\n```http\nPOST /login HTTP/1.1\nX-Custom-Proof: inner-evidence-marker\n```\n````\nAfter.",
[]string{"inner-evidence-marker", "POST /login", "http"},
[]string{"Summary line.", "After.", CodeOmitted},
},
{
"4-backtick fence around a json block",
"Summary line.\n````\n```json\n{\"user\": \"admin\", \"note\": \"inner evidence body\"}\n```\n````\nAfter.",
[]string{"inner evidence body", "json"},
[]string{"Summary line.", "After.", CodeOmitted},
},
{
"markdown fence quoting inline triple backticks",
"See this:\n````markdown\nSome **markdown** quoting ```code inside``` here.\n````\nDone.",
[]string{"code inside", "markdown"},
[]string{"See this:", "Done.", CodeOmitted},
},
{
"5-backtick fence around a 4-backtick fence",
"A\n`````\n````\nstill code\n````\n`````\nB",
[]string{"still code"},
[]string{"A", "B"},
},
{
"tilde fence inside a backtick fence",
"A\n```\n~~~\ninner tilde text\n~~~\n```\nB",
[]string{"inner tilde text"},
[]string{"A", "B"},
},
{
"backtick fence inside a tilde fence",
"A\n~~~~\n```\ninner backtick text\n```\n~~~~\nB",
[]string{"inner backtick text"},
[]string{"A", "B"},
},
{
"a shorter run does not close",
"A\n````\nfirst\n```\nsecond part\n````\nB",
[]string{"first", "second part"},
[]string{"A", "B"},
},
{
"unclosed long fence runs to the end",
"Keep this.\n`````\nevidence body never closed\n```\nmore",
[]string{"evidence body", "more"},
[]string{"Keep this.", CodeOmitted},
},
{
"a closing run followed by text does not close",
"A\n```\nx\n``` not a closer\nhidden evidence\n```\nB",
[]string{"not a closer", "hidden evidence"},
[]string{"A", "B"},
},
{
"a closing run indented four spaces does not close",
"A\n```\nx\n ```\nhidden evidence\n```\nB",
[]string{"hidden evidence"},
[]string{"A", "B"},
},
{
"fences indented up to three spaces",
"A\n ````\n```\nhidden evidence\n```\n ````\nB",
[]string{"hidden evidence"},
[]string{"A", "B"},
},
{
"nested fences inside a blockquote",
"A\n> ````\n> ```http\n> hidden evidence\n> ```\n> ````\nB",
[]string{"hidden evidence"},
[]string{"A", "B"},
},
{
"a longer run closes a container fence",
"A\n> ```\n> hidden evidence\n> ````\nB",
[]string{"hidden evidence"},
[]string{"A", "B"},
},
{
"nested fences inside an unordered list item",
"- ````\n ```http\n SECRET_1\n ```\n LEAKED_EVIDENCE_HERE\n ````\nAfter.",
[]string{"SECRET_1", "LEAKED_EVIDENCE_HERE", "http"},
[]string{"After."},
},
{
"nested fences inside an ordered list item",
"1. ````\n ```http\n SECRET_1\n ```\n LEAKED_EVIDENCE_HERE\n ````\nAfter.",
[]string{"SECRET_1", "LEAKED_EVIDENCE_HERE", "http"},
[]string{"After."},
},
{
"nested fences collapsed to one line",
"Title ```` ```http hidden evidence ``` ```` after",
[]string{"hidden evidence"},
[]string{"Title", "after"},
},
{
"a fence line interrupts a paragraph that has a stray run",
"text ```\nfoo\n```\nhidden evidence",
[]string{"foo", "hidden evidence"},
[]string{"text"},
},
}
for _, c := range cases {
out := SafeText(c.in, nil, 0)
for _, leak := range c.leak {
if strings.Contains(out, leak) {
t.Errorf("%s: %q leaked in %q", c.name, leak, out)
}
}
for _, keep := range c.keep {
if !strings.Contains(out, keep) {
t.Errorf("%s: %q missing in %q", c.name, keep, out)
}
}
}
}
// Inline triple-backtick spans are still dropped, and text around them stays.
func TestSafeTextInlineTripleBackticks(t *testing.T) {
for in, want := range map[string]string{
"Use ```id``` here.": "Use " + CodeOmitted + " here.",
"```a``` then prose": CodeOmitted + " then prose",
"two ``x`` backticks stay": "two ``x`` backticks stay",
"Found it:\n```\nbody\n```\ndone": "Found it:\n" + CodeOmitted + "\ndone",
"Found it:\n~~~ sh\nbody\n~~~\ndone": "Found it:\n" + CodeOmitted + "\ndone",
} {
if out := SafeText(in, nil, 0); out != want {
t.Errorf("SafeText(%q) = %q, want %q", in, out, want)
}
}
}