cogblock

package module
v0.0.0-...-dddc2fe Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cogblock implements the content-addressed block format for CogOS.

A CogBlock is the fundamental unit of structured data exchange: a typed, content-addressed envelope that carries arbitrary payloads with integrity guarantees. Blocks are identified by the hash of their canonical encoding, making them suitable for deduplication, caching, and verified transport.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendEvent

func AppendEvent(workspaceRoot, sessionID string, envelope *EventEnvelope) error

AppendEvent appends an event to a session ledger with hash chaining. This is the canonical write path for all events.

The ledger is stored at <workspaceRoot>/.cog/ledger/<sessionID>/events.jsonl.

func CanonicalizeEvent

func CanonicalizeEvent(payload *EventPayload) ([]byte, error)

CanonicalizeEvent produces RFC 8785 canonical JSON for an event payload. This ensures deterministic hashing: same logical content -> same bytes.

RFC 8785 rules:

  • Object keys sorted lexicographically
  • No insignificant whitespace
  • Unicode normalized (NFC)
  • Numbers in canonical form (no leading zeros, etc.)

func GetHashAlgorithm

func GetHashAlgorithm(workspaceRoot string) (string, error)

GetHashAlgorithm retrieves the hash algorithm from the workspace genesis event.

func HashEvent

func HashEvent(canonicalBytes []byte, algorithm string) (string, error)

HashEvent computes the hash of canonical event bytes using the specified algorithm. Supported algorithms: "sha256" (default), "sha512".

func VerifyLedger

func VerifyLedger(workspaceRoot, sessionID string) error

VerifyLedger verifies the hash chain integrity for a session ledger. Returns error if any hash is invalid or chain is broken.

Types

type BlockArtifact

type BlockArtifact struct {
	Kind string `json:"kind"`
	Ref  string `json:"ref"`
}

BlockArtifact references an output produced from processing a CogBlock.

type BlockProvenance

type BlockProvenance struct {
	OriginSession string    `json:"origin_session,omitempty"`
	OriginChannel string    `json:"origin_channel,omitempty"`
	IngestedAt    time.Time `json:"ingested_at"`
	NormalizedBy  string    `json:"normalized_by"`
}

BlockProvenance records the origin and ingestion metadata of a CogBlock.

type CogBlock

type CogBlock struct {
	ID        string    `json:"id"`
	Timestamp time.Time `json:"timestamp"`
	SessionID string    `json:"session_id,omitempty"`
	ThreadID  string    `json:"thread_id,omitempty"`

	// Source identification.
	SourceChannel   string `json:"source_channel"`
	SourceTransport string `json:"source_transport"`
	SourceIdentity  string `json:"source_identity,omitempty"`

	// Target.
	TargetIdentity string `json:"target_identity,omitempty"`
	WorkspaceID    string `json:"workspace_id,omitempty"`

	// Content.
	Kind         CogBlockKind    `json:"kind"`
	RawPayload   json.RawMessage `json:"raw_payload,omitempty"`
	SystemPrompt string          `json:"system_prompt,omitempty"`

	// Messages is kept as raw JSON to avoid coupling to any specific
	// provider message format. The kernel decodes this into its own
	// ProviderMessage type; external consumers can decode as needed.
	Messages json.RawMessage `json:"messages,omitempty"`

	// Provenance.
	Provenance   BlockProvenance `json:"provenance"`
	TrustContext TrustContext    `json:"trust_context"`

	// Ledger linkage.
	LedgerRef string `json:"ledger_ref,omitempty"`

	// Artifacts produced from processing this block.
	Artifacts []BlockArtifact `json:"artifacts,omitempty"`
}

CogBlock is the canonical unit of interaction in the CogOS substrate. Every inbound interaction is normalized into a CogBlock before routing, context assembly, or inference. Every significant kernel action may emit one.

type CogBlockKind

type CogBlockKind string

CogBlockKind identifies the type of interaction a CogBlock represents.

const (
	BlockMessage     CogBlockKind = "message"
	BlockToolCall    CogBlockKind = "tool_call"
	BlockToolResult  CogBlockKind = "tool_result"
	BlockImport      CogBlockKind = "import"
	BlockAttention   CogBlockKind = "attention"
	BlockSystemEvent CogBlockKind = "system_event"

	// ADR-059 block-type vocabulary.
	//
	// Document types (CogBlocks at rest).
	BlockDocInsight   CogBlockKind = "doc.insight"
	BlockDocEpisode   CogBlockKind = "doc.episode"
	BlockDocProcedure CogBlockKind = "doc.procedure"

	// Bus types (CogBlocks in flight).
	BlockBusMessage    CogBlockKind = "bus.message"
	BlockBusAck        CogBlockKind = "bus.ack"
	BlockBusCheckpoint CogBlockKind = "bus.checkpoint"

	// Session types.
	BlockSessionTurn CogBlockKind = "session.turn"
)

type EventEnvelope

type EventEnvelope struct {
	// HashedPayload contains the canonical JSON that gets hashed.
	HashedPayload EventPayload `json:"hashed_payload"`

	// Metadata is NOT included in the hash (for extensibility).
	Metadata EventMetadata `json:"metadata,omitempty"`
}

EventEnvelope represents the canonical event structure with hash chaining. This is the spine of the ledger — every event is hashed and chained.

func GetLastEvent

func GetLastEvent(workspaceRoot, sessionID string) (*EventEnvelope, error)

GetLastEvent retrieves the last event from a session ledger.

func NewEventEnvelope

func NewEventEnvelope(eventType, sessionID string) *EventEnvelope

NewEventEnvelope creates a new event envelope with current timestamp.

func (*EventEnvelope) WithData

func (e *EventEnvelope) WithData(key string, value interface{}) *EventEnvelope

WithData adds data to the event payload.

func (*EventEnvelope) WithSource

func (e *EventEnvelope) WithSource(source string) *EventEnvelope

WithSource adds source metadata.

type EventMetadata

type EventMetadata struct {
	// Hash of the canonical hashed_payload (computed during append).
	Hash string `json:"hash,omitempty"`

	// Seq is the sequence number within the session (computed during append).
	Seq int64 `json:"seq,omitempty"`

	// Source identifies what produced the event (e.g., "kernel", "hook", "cog-chat").
	Source string `json:"source,omitempty"`
}

EventMetadata contains information NOT included in the hash.

type EventPayload

type EventPayload struct {
	// Type of event (e.g., "workspace.genesis", "tool.call", "validation.success").
	Type string `json:"type"`

	// Timestamp when the event occurred (RFC3339 format).
	Timestamp string `json:"timestamp"`

	// SessionID that produced this event.
	SessionID string `json:"session_id"`

	// PriorHash chains to the previous event (empty for genesis).
	PriorHash string `json:"prior_hash,omitempty"`

	// Data contains type-specific payload (optional).
	Data map[string]interface{} `json:"data,omitempty"`
}

EventPayload is the content that gets canonicalized and hashed.

type TrustContext

type TrustContext struct {
	Authenticated bool    `json:"authenticated"`
	TrustScore    float64 `json:"trust_score"`
	Scope         string  `json:"scope"`
}

TrustContext captures authentication and authorization state for a CogBlock.

Jump to

Keyboard shortcuts

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