Documentation
¶
Overview ¶
Package receipt implements the Exposure Receipt: a per-run, customer-held manifest of every outbound request an agent made — destination, category, byte size, and content SHA-256 (NOT content). It is the mechanism behind the "Receipts, not promises" trust model.
This is the SHARED, versioned schema contract imported by every QualityMax agent that emits receipts (the `qmax` CLI and the `qmax-code` terminal agent). The wire format is pinned by Version; a verifier in any language can reproduce the signed bytes (see sign.go). Each agent supplies its own traffic taxonomy — this package treats Entry.Category as a free-form string and never enumerates categories.
Quality commitments enforced here:
- Fail loud, never silent: a request with no receipt-bearing context records into a process-global "current" run, then an "untracked" fallback (with a one-time warning) — entries are never dropped.
- Concurrency-safe: the seq counter is per-receipt and the entry slice is mutex-guarded, so concurrent goroutines never interleave or race.
- Deterministic signing: receipts are signed over encoding/json output, whose field order (struct order) and map-key order (sorted) are stable.
This package intentionally does NOT import net/http: it operates on plain strings, keeping all net/http usage inside each agent's httpx chokepoint.
Index ¶
Constants ¶
const Version = "1"
Version of the receipt schema. Bump only on a wire-format change; both agents and any external verifier key off this.
Variables ¶
var AgentVersion = "dev"
AgentVersion is stamped into each receipt; set by the importing agent at init.
var BaseDir = defaultBaseDir()
BaseDir is the directory under which receipts (BaseDir/receipts/) and the signing key (BaseDir/receipt_ed25519.seed) are stored. Defaults to ~/.qamax, preserving the qmax CLI's original layout; qmax-code sets this to ~/.qmax-code at init so the two agents keep separate identities and stores.
var BuildSHA = "dev"
BuildSHA identifies the agent build that produced a receipt. Override via -ldflags "-X github.com/Quality-Max/qmax-receipt.BuildSHA=<sha>".
Functions ¶
func ReceiptsDir ¶
ReceiptsDir is the directory where finalized receipts are written.
func Templatize ¶
Templatize collapses id-like path segments (numeric, uuid, long hex) to {id} so receipts are comparable across runs and don't leak ids into the path. It is generic and shared by every agent's classifier before recording an Entry.Path.
func Verify ¶
Verify checks a receipt's embedded signature against its embedded public key. It proves PROVENANCE (this key produced this receipt), not honesty — trust roots in open source + the static egress guard + the customer diffing the receipt against their own firewall logs, never the signature alone.
Types ¶
type Entry ¶
type Entry struct {
Seq int `json:"seq"`
Timestamp time.Time `json:"timestamp"`
Method string `json:"method"`
Host string `json:"host"`
Path string `json:"path"` // templatized: /api/agent/{id}/crawl/{id}/snapshot
Category string `json:"category"`
Model *string `json:"model"`
ReqBytes int64 `json:"req_bytes"`
ReqSHA256 string `json:"req_sha256"`
RespStatus int `json:"resp_status"`
RespBytes int64 `json:"resp_bytes"`
Allowed bool `json:"allowed"`
Rule string `json:"rule"`
Note string `json:"note,omitempty"` // e.g. "blocked-by-egress-policy", "transport-error: ..."
}
Entry is a single recorded outbound request. It stores a hash and size, never content — so the receipt itself is non-sensitive and safe to share.
type Receipt ¶
type Receipt struct {
ReceiptVersion string `json:"receipt_version"`
RunID string `json:"run_id"`
RunKind string `json:"run_kind"`
AgentVersion string `json:"agent_version"`
AgentBuildSHA string `json:"agent_build_sha"`
StartedAt time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
Destinations []string `json:"destinations"`
Entries []Entry `json:"entries"`
Summary Summary `json:"summary"`
Signature *Signature `json:"signature,omitempty"`
// contains filtered or unexported fields
}
Receipt is one run's manifest. The Signature field is omitted while signing (omitempty), so the signed payload is everything else, deterministically.
func Begin ¶
Begin creates a receipt and returns a context carrying it. Use this for concurrent work (each daemon assignment/crawl session, or a qmax-code session) so entries route to the right run regardless of the process-global current.
func FromContext ¶
FromContext resolves the active receipt: context first, then process-global current, then a fail-loud untracked fallback. It never returns nil and never drops an entry on the floor.
func NewCurrent ¶
NewCurrent creates a receipt and installs it as the process-global current run. Use this for single-run-per-process work (CLI commands, the daemon control plane). Returns the receipt so the caller can Finalize it.
func (*Receipt) EntryCount ¶
EntryCount returns the number of recorded entries (concurrency-safe).
type Signature ¶
type Signature struct {
Alg string `json:"alg"`
PublicKey string `json:"public_key"` // base64 raw ed25519 public key — receipt is self-verifying
Value string `json:"value"` // base64 signature over the canonical payload
}
Signature is the detached ed25519 signature over the receipt payload.