Documentation
¶
Overview ¶
Package gcf implements the GCF (Graph Compact Format) encoder and decoder.
GCF is a compact, text-only, graph-native wire format designed for MCP tool responses. It exploits referential identity (local IDs), graph topology (edges as references), and hierarchical grouping (distance-based sections) to achieve 84% token savings over JSON while remaining human-readable.
Specification: https://github.com/blackwell-systems/gcf
Encode a payload:
out := gcf.Encode(&gcf.Payload{
Tool: "context_for_task",
Symbols: []gcf.Symbol{{QualifiedName: "pkg.Func", Kind: "function", Score: 0.9, Provenance: "lsp_resolved"}},
})
Decode a payload:
p, err := gcf.Decode(input)
Session deduplication (previously-transmitted symbols as bare references):
sess := gcf.NewSession() out1 := gcf.EncodeWithSession(&payload1, sess) // full declarations out2 := gcf.EncodeWithSession(&payload2, sess) // reused symbols as @N refs
Delta encoding (only added/removed symbols):
out := gcf.EncodeDelta(&gcf.DeltaPayload{...})
Index ¶
Constants ¶
This section is empty.
Variables ¶
var KindAbbrev = map[string]string{
"function": "fn",
"type": "type",
"method": "method",
"interface": "iface",
"var": "var",
"const": "const",
"resource": "resource",
"table": "table",
"class": "class",
"selector": "selector",
"field": "field",
"route_handler": "route",
"external": "ext",
"file": "file",
"package": "pkg",
"service": "svc",
}
KindAbbrev maps full kind names to short GCF abbreviations.
var KindExpand = map[string]string{
"fn": "function",
"type": "type",
"method": "method",
"iface": "interface",
"var": "var",
"const": "const",
"resource": "resource",
"table": "table",
"class": "class",
"selector": "selector",
"field": "field",
"route": "route_handler",
"ext": "external",
"file": "file",
"pkg": "package",
"svc": "service",
}
KindExpand is the reverse of KindAbbrev.
Functions ¶
func EncodeDelta ¶
func EncodeDelta(d *DeltaPayload) string
EncodeDelta serializes a DeltaPayload into GCF delta format.
func EncodeGeneric ¶ added in v0.1.1
EncodeGeneric encodes any value into GCF tabular format. Unlike Encode (which handles the graph Payload type), EncodeGeneric works on arbitrary maps, slices, and primitives using GCF's tabular encoding grammar.
func EncodeWithSession ¶
EncodeWithSession encodes a payload using GCF with session deduplication. Symbols that were already transmitted in prior responses are emitted as bare references (`@N # previously transmitted`) instead of full declarations. After encoding, newly-sent symbols are recorded in the session.
Types ¶
type Components ¶
type Components struct {
BlastRadius float64 // number of callers (normalized)
Confidence float64 // edge provenance confidence
Recency float64 // git recency signal
Distance float64 // graph distance penalty
}
Components holds the score breakdown for a symbol.
type DeltaPayload ¶
type DeltaPayload struct {
Tool string
BaseRoot string // pack_root the consumer has
NewRoot string // pack_root of the current result
Removed []Symbol
Added []Symbol
RemovedEdges []Edge
AddedEdges []Edge
DeltaTokens int
FullTokens int
}
DeltaPayload represents the diff between a prior context pack and the current result. Used for incremental context delivery.
type Edge ¶
type Edge struct {
Source string // qualified name of source symbol
Target string // qualified name of target symbol
EdgeType string
Status string // optional: "added", "removed", "unchanged" (for diff responses)
}
Edge represents a directed relationship in a GCF payload.
type Payload ¶
type Payload struct {
Tool string // producing tool name (e.g., "context_for_task")
TokensUsed int // actual tokens consumed by this payload
TokenBudget int // token budget requested by the consumer
PackRoot string // content-addressed identity (hex SHA-256), enables delta encoding
Symbols []Symbol // ordered by score descending within each distance group
Edges []Edge // directed relationships between symbols
}
Payload is the input/output structure for GCF encoding/decoding.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session tracks symbols that have been transmitted to a client, enabling subsequent responses to reference them by ID without full retransmission. This makes multi-call workflows progressively cheaper.
Thread-safe: multiple tool handlers may encode concurrently within a session.
func (*Session) GetID ¶
GetID returns the session-global ID for a previously transmitted symbol. Returns -1 if not found.
func (*Session) Record ¶
Record marks symbols as transmitted and assigns session-global IDs. Call this after a successful encode to register newly-sent symbols.
func (*Session) Transmitted ¶
Transmitted returns true if the symbol has been sent in a previous response.
type Symbol ¶
type Symbol struct {
QualifiedName string // fully qualified identifier (e.g., "pkg/auth.Middleware")
Kind string // node type: "function", "type", "method", etc.
Score float64 // relevance score (0.0 to 1.0)
Provenance string // discovery method: "lsp_resolved", "ast_inferred", etc.
Distance int // hops from query center (0=target, 1=related, 2+=extended)
Signature string // optional: function/method signature
Components Components // optional: score breakdown
}
Symbol represents a node in a GCF payload.