Skip to content

mikewcasale/sage-spec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SAGE Logo

SAGE - Structured Agent Guidance Embeddings

A Documentation Standard for Human ↔ Agent Collaborative Codebases

Version Status License


Overview

SAGE defines a hybrid documentation system optimized for codebases where both humans and AI coding agents (Claude Code, Codex, Cursor, etc.) collaborate. It addresses critical challenges in agent-assisted development:

  • Preventing code duplication - Agents discover canonical homes and existing alternatives before creating new code
  • Making architecture explicit - The "why" and "why not" are machine-discoverable
  • Enforcing constraints - Rules are extractable via deterministic parsing (no NLP required)
  • Minimizing drift - Parameter docs are co-located with signatures
  • Serving dual audiences - Documentation works for both humans and agents

Core Innovation: Contextual Relationship Graph (CRG)

SAGE introduces a machine-parseable Contextual Relationship Graph embedded in docstrings using @ tags. These tags encode:

Concept Purpose
Canonical ownership "Where does this live?"
Similarity/disambiguation "Don't reinvent this"
Extension points Safe extension vs sealed internals
Dependency injection Lifecycle and collaborator expectations
Anti-patterns Warnings tuned to agent failure modes

Quick Example

"""HTTP client implementations for external service communication.

@notice: |
    Canonical async HTTP client for outbound service calls.
    Use create_http_client() or dependency injection; do not instantiate directly.

@dev: |
    Repository-adapter pattern. All HTTP clients MUST be obtained through DI/factory.
    Agents MUST NOT create parallel HTTP implementations.

@graph:
    id: services.http_client
    provides:
        - services.http_client:HTTPClient
        - services.http_client:create_http_client
    consumes:
        - config.http:HTTPClientConfig
        - observability:Logger
    pattern: repository-adapter

@similar:
    - id: services.grpc_client
      when: "Use for gRPC; this module is HTTP-only."

@agent-guidance:
    do:
        - "Use DI/factory to obtain HTTPClient instances."
    do_not:
        - "Create new HTTP client classes."
        - "Use requests/urllib directly in production code."
"""

Design Influences

SAGE synthesizes strengths from multiple documentation approaches:

Approach Influence
Uvicorn-style minimalism Prefer type hints and descriptive names; avoid drift-prone prose
FastAPI-style co-location Document parameters inline with Annotated[..., Doc(...)]
Google/pydantic-ai narrative Keep familiar summary + optional examples for humans
Cursor-style discovery Extract metadata into .sage/ artifacts agents can query on-demand
NatSpec (Solidity) Audience-separated narrative (@notice vs @dev), inheritance (@inheritdoc)

Tag Categories

Navigation & Graph

  • @graph - Component relationships (provides, consumes, pattern)
  • @similar - Disambiguation ("use X instead when...")
  • @canonical-home - Authoritative location for a concept
  • @replaces - Migration from deprecated components

Architecture

  • @pattern - Design pattern with rationale and violation consequences
  • @extends - Allowed extension points vs sealed internals
  • @collaborators - Dependency injection and lifecycle
  • @implements - Protocol/interface implementation

Agent Constraints

  • @agent-guidance - Do/don't rules for agents
  • @anti-patterns - Common mistakes with alternatives
  • @prefer-over - Why to use this instead of alternatives

Semantics

  • @pure - Referential transparency declaration
  • @total - Totality (produces output for all valid inputs)
  • @effects - Side effects (state, I/O, external, environment)
  • @errors - Recoverable vs terminal error categories

Audience

  • @notice - User-facing description (API docs, tooltips)
  • @dev - Developer/agent context (invariants, caveats)

Custom

  • @custom:<name> - Project-specific metadata (e.g., @custom:stability: stable)

Dynamic Context Discovery

SAGE recommends extracting doc metadata into .sage/ artifacts:

.sage/
├── index.json
├── graph.json
├── patterns.json
├── anti-patterns.json
├── agent-guidance.json
├── canonical-homes.json
├── components/
│   ├── services.http_client.json
│   └── services.http_client.HTTPClient.json
└── similar/
    └── http-clients.md

Agents load only what's needed, enabling efficient navigation of large codebases.

Adoption Strategy

SAGE supports incremental adoption:

Level Scope Tags
Level 1 (Minimal) Key modules @graph, @similar, @agent-guidance
Level 2 (Standard) Significant classes Add @pattern, @collaborators, @anti-patterns, @sensitivity
Level 3 (Full) Complete system Extraction + CI validation + MCP querying

Agent Integration Protocol

Before creating new code, agents MUST:

  1. Read relevant @graph blocks
  2. Check @similar for existing alternatives
  3. Respect sealed internals (@sealed, @extends.sealed)
  4. Follow DI/lifecycle (@collaborators)
  5. Avoid documented @anti-patterns
  6. Prefer canonical construction paths (@factory-for, @canonical-home)

Validation Rules

SAGE defines validation rules at ERROR, WARN, and INFO levels:

Rule Level Description
V001 ERROR Non-trivial modules MUST have @graph
V002 ERROR Significant class docstrings MUST include @pattern
V003 ERROR Classes with injected deps MUST include @collaborators
V004 ERROR Attribute docstrings MUST include @sensitivity
C001 ERROR Sealed items MUST NOT be subclassed/called externally

Optional MCP Integration

SAGE can be paired with a knowledge-graph MCP server that:

  • Queries .sage/ extracted metadata
  • Enriches with AST symbol tables and import graphs
  • Proposes doc updates (with human approval for non-mechanical changes)

Specification

The complete specification is available in spec.md, including:

  • Detailed tag syntax and parsing rules
  • Complete templates for modules, classes, methods, and attributes
  • JSON schema for extraction outputs
  • Validation rule definitions

Version History

Version Date Status
1.3.6 2026-01-25 Release Candidate (Implementable)
1.3.5 2026-01-25 Pre-release draft

v1.3.6 Changes

  • Fixed tag grammar to support @custom:<name> without contradicting base grammar
  • Enforced "two value forms" (scalar or block, no hybrid)
  • Standardized multi-line @notice/@dev via YAML block scalars
  • Added @concurrency as clearer replacement for @thread-safety
  • Aligned JSON extraction examples with schema

License

MIT License. (c) 2025-2026 SAGE Contributors

About

Structured Agent Guidance Embeddings (SAGE): A Documentation Standard for Human ↔ Agent Collaborative Codebases

Topics

Resources

License

Stars

Watchers

Forks

Contributors