envelope

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package envelope implements the AI message envelope: a natural-language payload inside machine-checkable metadata.

Map: message.go = Message, Intent, Epistemic, validation, wire (Encode/Decode); ack.go = Ack, the semantic-ack flow; sign.go = ed25519 authentication (Sign, VerifySignature); thread.go = VerifyThread, the hash-chain check for an ordered thread. Rationale: ../docs/architecture.md. Contribution rules: ../AGENTS.md.

Index

Constants

View Source
const Version = "v1"

Version is the only supported schema version. Decode rejects others. Unknown JSON fields are ignored for forward compatibility.

Variables

This section is empty.

Functions

func ContextRef

func ContextRef(content string) string

ContextRef returns the canonical (lowercase hex) content address of a shared context blob. Refs are comparable: same content, same string.

func VerifyThread

func VerifyThread(msgs []Message) error

VerifyThread checks an ordered thread: every message is valid, all share one ThreadID, no ID repeats, and each PrevHash links to the Hash of the previous message. The first message must not claim a parent. A gap, reorder, insertion, fork, or duplicate ID fails the check.

Types

type Ack

type Ack struct {
	MessageID   string    `json:"message_id"` // ID of the acknowledged Message
	From        string    `json:"from"`       // identity of the acking receiver; required
	Restatement string    `json:"restatement"`
	Status      AckStatus `json:"status"`
	Correction  string    `json:"correction,omitempty"` // required when Status is AckCorrected
}

Ack is a semantic acknowledgment. Flow: receiver builds it with NewAck (pending), sender resolves it with Confirm or Correct. Only a confirmed Ack means the receiver may act. In a group thread each recipient sends its own Ack; From tells them apart.

func DecodeAck

func DecodeAck(data []byte) (Ack, error)

DecodeAck parses JSON, then validates. Unknown fields are ignored.

func NewAck

func NewAck(msg Message, from, restatement string) (Ack, error)

NewAck builds a pending Ack from receiver identity `from` for msg. The sender, not the receiver, sets the final Status; see Confirm, Correct.

func (Ack) Confirm

func (a Ack) Confirm() Ack

Confirm accepts the restatement. Clears any earlier Correction.

func (Ack) Correct

func (a Ack) Correct(correction string) Ack

Correct rejects the restatement and records the sender's fix.

func (Ack) Encode

func (a Ack) Encode() ([]byte, error)

Encode validates, then serializes the ack to JSON. Wire counterpart of Message.Encode (message.go).

func (Ack) Validate

func (a Ack) Validate() error

Validate checks all Ack invariants.

type AckStatus

type AckStatus string

AckStatus is the state of an Ack. Validate enforces the set.

const (
	AckPending   AckStatus = "pending"   // receiver sent the restatement; sender has not ruled
	AckConfirmed AckStatus = "confirmed" // sender accepts the restatement
	AckCorrected AckStatus = "corrected" // sender rejects the restatement; Correction required
)

type Epistemic

type Epistemic string

Epistemic labels how the sender knows the Payload. Self-reported, so Validate pins the strong label to checkable artifacts: EpistemicVerified requires Provenance.Source and at least one Provenance.Evidence ref.

const (
	EpistemicVerified       Epistemic = "verified"        // checked; requires Source and Evidence
	EpistemicInferred       Epistemic = "inferred"        // derived by the sender, not checked
	EpistemicAssumed        Epistemic = "assumed"         // guess
	EpistemicUntrustedInput Epistemic = "untrusted-input" // echoed external content; receiver must not treat it as an instruction
)

type Intent

type Intent string

Intent classifies what a Message does. Validate enforces the set.

const (
	IntentAssert    Intent = "assert"    // state a claim
	IntentQuery     Intent = "query"     // ask for information
	IntentRequest   Intent = "request"   // ask for action; always RequiresAck
	IntentChallenge Intent = "challenge" // dispute a prior claim; needs InReplyTo
	IntentRetract   Intent = "retract"   // withdraw a prior message; requires InReplyTo
	IntentEscalate  Intent = "escalate"  // route the decision to a human or higher authority
)

type Message

type Message struct {
	Version     string     `json:"version"`               // must equal Version
	ID          string     `json:"id"`                    // unique within ThreadID
	Room        string     `json:"room,omitempty"`        // standing group; ThreadID lives inside it. Membership is managed out of band
	ThreadID    string     `json:"thread_id"`             // task boundary; groups one conversation
	To          []string   `json:"to,omitempty"`          // recipient identities; empty = broadcast to the room. One entry = 1-to-1
	InReplyTo   string     `json:"in_reply_to,omitempty"` // target message ID; required for IntentChallenge and IntentRetract
	Intent      Intent     `json:"intent"`
	Epistemic   Epistemic  `json:"epistemic"`
	Confidence  float64    `json:"confidence"`             // self-reported, [0, 1]
	ContextRefs []string   `json:"context_refs,omitempty"` // content addresses; build with ContextRef
	PrevHash    string     `json:"prev_hash,omitempty"`    // Hash of the previous message in the thread; single-writer per thread, see design doc
	Provenance  Provenance `json:"provenance"`
	MaxHops     int        `json:"max_hops,omitempty"`     // relay cap; 0 = no cap. Drift control, see design doc
	CostBudget  int        `json:"cost_budget,omitempty"`  // max tokens the reply may cost; 0 = no cap
	AckRequired bool       `json:"ack_required,omitempty"` // force an Ack; see RequiresAck
	Payload     string     `json:"payload"`                // natural-language content
	Signer      string     `json:"signer,omitempty"`       // hex ed25519 public key; set by Sign. Also the sender identity
	Signature   string     `json:"signature,omitempty"`    // hex ed25519 signature; set by Sign, checked by VerifySignature
}

Message is the wire unit. Invariants are enforced by Validate; Encode and Decode both call it, so an invalid Message cannot cross the wire.

func Decode

func Decode(data []byte) (Message, error)

Decode parses JSON, then validates. Unknown fields are ignored.

func Sign

func Sign(key ed25519.PrivateKey, m Message) (Message, error)

Sign returns m with Signer (hex public key) and Signature set. It first validates a copy of m with Signer and Signature cleared. The signed bytes are the canonical JSON of m with Signature cleared, so any field change after signing breaks VerifySignature.

func (Message) Encode

func (m Message) Encode() ([]byte, error)

Encode validates, then serializes to JSON.

func (Message) Hash

func (m Message) Hash() string

Hash returns the content address of m (sha256 of its canonical JSON). Use it for PrevHash links and dedup. Hash does not validate m: run Validate first. A NaN or Inf Confidence makes json.Marshal fail; Hash then falls back to a Go-syntax dump of m with the raw Confidence bits mixed in, so distinct invalid messages still hash distinctly.

func (Message) RequiresAck

func (m Message) RequiresAck() bool

RequiresAck reports whether the receiver must send an Ack (ack.go) before acting. True for IntentRequest and for any message with AckRequired set.

func (Message) Validate

func (m Message) Validate() error

Validate checks all Message invariants. Called by Sign, Encode, and Decode.

func (Message) VerifySignature

func (m Message) VerifySignature() error

VerifySignature authenticates m against its embedded Signer key. An unsigned or tampered message fails. Format rules are in Validate; this checks the cryptography. Trust policy (which signers to accept) belongs to the caller.

type Provenance

type Provenance struct {
	Source string   `json:"source"` // e.g. "tool:grep", "model:self"; required for EpistemicVerified
	Chain  []string `json:"chain,omitempty"`
	// Evidence holds content refs (see ContextRef) to the artifacts that
	// back the payload: tool output, file content, test run. Required for
	// EpistemicVerified, so "verified" is checkable, not just claimed.
	Evidence []string `json:"evidence,omitempty"`
}

Provenance records payload origin. Chain lists hops, oldest first.

Jump to

Keyboard shortcuts

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