fence

package
v0.263.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fence renders UNTRUSTED TEXT as a fenced block an agent is handed as a turn: a preface line written by muster, then an opening marker at column 0 carrying a fresh nonce, the content lines, and a closing marker — with EVERY line between the markers behind ContentPrefix.

🔴 THE PREFIX IS WHAT MAKES THE FENCE STRUCTURAL. No content text can reach column 0, where the markers live, so no text can close the block early or open a forged one; the marker word and the block's own nonce are additionally replaced inside the fence (MarkerPlaceholder, NoncePlaceholder), so a line cannot even QUOTE the real closing marker.

🔴 Sanitize IS THE CALLER'S JOB, PER FIELD, BEFORE A LINE IS BUILT. A field holding a line break would start a line with no content prefix; Sanitize folds every line break into " ⏎ " and replaces every control, format, separator and unassigned code point, so no output of it can. The block itself does not sanitise lines a second time: a caller-composed line (a standing sentence, a stanza header) is muster's own text.

This package was extracted from internal/memorelay. A byte golden of the memo relay's rendered turn checked the move changed no byte; it was deleted once the extraction landed, so internal/memorelay's own tests are what pin its shape now. internal/plugins renders inbound plugin messages through it as well.

Index

Constants

View Source
const (
	// ContentPrefix begins every line between the markers.
	ContentPrefix = "| "
	// MarkerPlaceholder replaces the block's marker word wherever a content line
	// carries it (any case).
	MarkerPlaceholder = "[fence-marker]"
	// NoncePlaceholder replaces the block's nonce wherever a content line carries
	// it (any case).
	NoncePlaceholder = "[nonce]"
)
View Source
const MaxURLBytes = 2048

MaxURLBytes bounds a URL WebURL keeps.

Variables

This section is empty.

Functions

func Cut

func Cut(s string, n int) string

Cut is the first n RUNES of s, with "…" when anything was dropped.

func NewNonce

func NewNonce() string

NewNonce is a fresh 16-hex-character nonce, drawn per render, so text that guesses an earlier block's nonce still sits behind the content prefix of this one.

func Sanitize

func Sanitize(s string) string

Sanitize is the replacement every untrusted field passes through before it is written into a turn: each Unsafe code point becomes U+FFFD, except a line break (" ⏎ ", CRLF folded first) and a tab (one space). Invalid UTF-8 decodes to U+FFFD per invalid byte. Sanitising already-sanitised text is a no-op.

🔴 IT IS ALSO WHAT KEEPS A FIELD ON ONE LINE AND OUT OF A RUNTIME'S REFUSAL. A line break inside a field would start a line with no content prefix, and the claude-code runtime refuses a prompt holding any control character other than newline and tab (a 400, nothing pasted). No output of this function holds either.

func Unfold added in v0.250.0

func Unfold(s string) string

Unfold is Sanitize's line-break folding undone, for showing a field as the text it was: each " ⏎ " back to a line break.

func Unsafe

func Unsafe(r rune) bool

Unsafe reports whether Sanitize must replace r: every code point in General Category C* (Cc, Cf, Co, Cs, Cn), plus Zl, Zp, the variation selectors and the other default-ignorable code points (invisible letters and marks). "In none of L, M, N, P, S, Z" is exactly C*, unassigned code points included.

func WebURL added in v0.224.0

func WebURL(u string) string

WebURL is u when it is a URL an outside source may hand muster to SHOW AS TEXT: an absolute http or https URL with a host and no userinfo, at most MaxURLBytes bytes of UTF-8 with no space and no Unsafe code point; "" otherwise. It says nothing about where the URL leads: nothing muster draws from it is a link.

Types

type Block

type Block struct {
	// Preface is the one line OUTSIDE the fence, muster's own words: what the
	// turn is and that it asks for nothing. It is written verbatim.
	Preface string
	Marker  string
	// Count is the number of items the block carries, stated in the opening
	// marker.
	Count int
	// Lines are the content lines, each written behind ContentPrefix. An empty
	// string is a blank content line (the prefix alone).
	Lines []string
}

Block is one fenced turn. Marker is the word the opening and closing markers carry (e.g. "cairn-memo"); it should be a plain lower-case word, since it is matched case-insensitively inside the content.

func Parse added in v0.250.0

func Parse(s string) (b Block, nonce string, ok bool)

Parse reads back a turn Block.Render wrote: ok only when s is EXACTLY one rendered block — a preface, then an opening marker at column 0, every line up to the closing marker behind ContentPrefix, a closing marker carrying the opening one's marker word and nonce, and nothing but white space after it. Lines are the content lines without their prefix. A line a transcript stripped of its trailing space (a blank content line, "|") reads as blank.

It is for DRAWING a turn muster sent (the human's view of it), never for deciding anything: what the agent was handed is s itself.

func (Block) Render

func (b Block) Render(nonce string) string

Render is the whole turn: the preface, a blank line, then the fenced block.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL