coref

package
v0.7.4-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 3 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 coref builds the Tier-1 co-reference index that co-reference-aware compaction decides from: for each tool output in a request, which identifiers that output INTRODUCED, and whether any later turn carried them forward.

It is deliberately free of the bifrost schema, of the components package and of the tokenizer. The index is a pure function of a flattened message list, which is what lets the Go component and the offline measurement pass (deploy/harbor/coref.py) share ONE definition of "reference" and be checked against the same known-answer fixture. A component whose notion of a reference had drifted from the script's would be calibrated against thresholds measured for a different algorithm — the thresholds are the whole output of the measurement, so that drift would be silent and total.

See docs/proposals/coref-compaction.md for what the index is FOR: §2 (the three tiers and the echo confound), §3 (open vs closed, and why recency is measured from the head of the transcript rather than from the output).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Idents

func Idents(s string) map[string]struct{}

Idents returns the distinctive identifier tokens in s, as a set. Tokens are trimmed of surrounding punctuation so `memory.` and `memory` are ONE token rather than two that can never match each other.

Types

type Class

type Class string

Class is the verdict for one tool output.

const (
	// Opaque — this output introduced NO identifier the index can track, so there is no
	// evidence either way and the only honest verdict is no opinion. Never cut.
	//
	// This is not a corner case, and collapsing it into Unreferenced was a real defect:
	// the two look identical in the arithmetic (refs == 0) and mean opposite things.
	// "Introduced 200 identifiers, nobody touched one" is evidence of deadness;
	// "introduced nothing I can see" is absence of evidence. A tool returning records of
	// human-readable values — `[{"name":"david","id":123,"address":"foobarbaz"}]` — yields
	// no distinctive tokens at all, because short lowercase words and 3-digit numbers are
	// exactly what the precision rules in distinctive exclude. Classified as Unreferenced,
	// that output is cut by the DEFAULT config while the agent still needs the address.
	//
	// So the blind spot is now a class rather than a silent vote for deletion, and the
	// asymmetry is deliberate: an opaque output costs tokens, a wrongly cut one costs an
	// expand round-trip plus a cache-write and can cost the task.
	Opaque Class = "opaque"
	// Unreferenced — the output introduced identifiers and no later turn reused any of
	// them. The safest cut on Tier 1, and blind to Tier 2 (a value that was transformed
	// before being restated leaves no exact match). Never read this as "unused".
	Unreferenced Class = "unreferenced"
	// Closed — referenced a small number of times, and not for a long time. Whatever
	// the model took survives in the turn that took it, so the original is redundant
	// with content still in the request. The case-A large-cut candidate.
	Closed Class = "closed"
	// Open — referenced recently, or repeatedly. Still load-bearing; keep.
	Open Class = "open"
)

The four verdicts. Cutting Unreferenced needs no reference model beyond "nobody ever used this"; cutting Closed is the large, early cut that needs the thresholds.

func Classify

func Classify(r Record, closedDist, openReps, minLater int) Class

Classify applies the open/closed predicate. closedDist is the recency floor (a last reference NEWER than this many messages ago keeps the output open); openReps is the repetition ceiling (referenced at least this many times keeps it open regardless of age, because a span referenced repeatedly is a hot span that happens to be old).

minLater is the opportunity floor: an output with fewer than this many model turns after it is reported Open regardless of everything else, because it has not yet HAD a chance to be referenced. Without it the newest outputs classify as Unreferenced purely for being new, and a batched pass would preferentially cut the most recent context — the worst possible choice, and the reason mask carries keep_recent. 0 disables.

type Message

type Message struct {
	Texts   []string
	Results []Result
}

Message is one flattened transcript entry. Texts is the reference-BEARING surface (prose, plus tool-call names and arguments); Results is the mass under evaluation.

Both surfaces feed the echo-exclusion set, and only Texts can constitute a reference — see Index. Callers build these 1:1 with their own message indices so a Record's Idx points back at the message it came from.

type Record

type Record struct {
	// Idx is the caller's message index; ID the tool-call id within it.
	Idx int
	ID  string
	// SizeTokens is the output's own size — the mass a cut would recover.
	SizeTokens int
	// Novel counts the identifiers this output INTRODUCED (see Index).
	Novel int
	// Refs counts the later turns that reused at least one novel identifier.
	Refs int
	// RefAge is how many messages AGO the last reference was, counted from the head of
	// the transcript; -1 when there was none. This is the A/B axis. The tempting
	// quantities — the output's own depth, or the gap from the output to its reference —
	// are both something else: "recent messages vs early messages" is a statement about
	// now, so it has to be measured from now.
	RefAge int
	// ConsumeLag is how many messages after the output its LAST reference was; -1 when
	// there was none. Reported separately from RefAge because it answers a different
	// question — how long the output stayed live — and conflating the two is what makes
	// a hot old span look like a cold one.
	ConsumeLag int
	// UsedFrac is the share of the novel identifiers the model actually carried
	// forward. A low value on a referenced output is the "took one value, does not need
	// the rest" pattern, measured rather than assumed.
	//
	// It is NOT sufficient on its own to justify a cut, and the reason is worth stating
	// where it will be read. A low UsedFrac is ambiguous: it can mean the model took the
	// value it needed and the remainder is chaff, or it can mean the model took an ANCHOR
	// (a name, an id) precisely in order to point at a payload it never copied. Given
	// `[{"name":"david","id":123,"address":"foobarbaz"}, ...]` and a model that says "I
	// need to remember david 123 address", the reference is real, the payload is not in
	// the model's turn, and cutting the output loses the address.
	UsedFrac float64
	// LaterTurns is how many model turns follow this output — its OPPORTUNITY to be
	// referenced. Near the tail this approaches zero, and an output that has not had a
	// chance to be used must not be scored as unused. See Classify's minLater.
	LaterTurns int
	// NovelIDs are the identifiers this output introduced (the set Novel counts), sorted and capped.
	// Filled only by IndexNovel; Index leaves it nil, so existing callers pay nothing for it.
	NovelIDs []string
}

Record is the per-output measurement the classifier decides from.

func Index

func Index(msgs []Message, minOutputTokens int, tok func(string) int) []Record

Index computes one Record per tool output at least minOutputTokens in size, using tok to measure size (nil falls back to the ~4-chars/token proxy the measurement script uses).

Two exclusions do the real work, and neither is optional:

ECHO. Only identifiers the output INTRODUCED are eligible. If the agent calls Read(src/auth.py), the path arrives in the tool-call ARGUMENT, is echoed by the result, and appears again in a later Edit(src/auth.py) — an exact matcher sees a reference from the output to a later turn, but nothing was ever taken FROM the output. So a token is novel only if it appears nowhere at or before this message (in any surface), nor in a sibling result of the same turn. On the fixture, dropping this guard flips a plainly-unreferenced file read to open and halves the measured cuttable mass: it is the difference between a usable measurement and one that reports everything as load-bearing.

BOILERPLATE. A token echoed by many outputs (more than max(5, outputs/4)) is session furniture — a banner, a prompt, a repeated header — not a carried value.

Outputs below minOutputTokens get no Record but still contribute to both exclusion sets, because they are part of the context the model saw.

func IndexNovel added in v0.6.0

func IndexNovel(msgs []Message, minOutputTokens int, tok func(string) int) []Record

IndexNovel is Index with Record.NovelIDs filled. The sweep keeps a hash of each dropped output's identifiers, so it can tell later that a turn used something the dropped output introduced, after the output itself is no longer in the request.

type Result

type Result struct {
	ID   string
	Text string
}

Result is one tool output under evaluation, identified by the tool-call id that produced it (a single turn can carry several).

Jump to

Keyboard shortcuts

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