Skip to content

tenet

Full name: tenets.models.tenet

tenet

Tenet (guiding principle) data model.

This module defines the data structures for tenets - the guiding principles that can be instilled into context to maintain consistency across AI interactions.

Classes

Priority

Bases: Enum

Tenet priority levels.

Attributes
weightproperty
Python
weight: float

Get numerical weight for priority.

TenetStatus

Bases: Enum

Tenet status in the system.

TenetCategory

Bases: Enum

Common tenet categories.

TenetMetricsdataclass

Python
TenetMetrics(injection_count: int = 0, last_injected: Optional[datetime] = None, contexts_appeared_in: int = 0, compliance_score: float = 0.0, reinforcement_needed: bool = False)

Metrics for tracking tenet effectiveness.

Functions
update_injection
Python
update_injection() -> None

Update metrics after injection.

Source code in tenets/models/tenet.py
Python
def update_injection(self) -> None:
    """Update metrics after injection."""
    self.injection_count += 1
    self.last_injected = datetime.now()
    self.contexts_appeared_in += 1

    # Reduce reinforcement need after injection
    if self.injection_count % 3 == 0:
        self.reinforcement_needed = False

InjectionStrategydataclass

Python
InjectionStrategy(frequency: str = 'adaptive', position: str = 'strategic', max_per_context: int = 3, min_tokens_between: int = 1000)

Strategy for how a tenet should be injected.

Functions
should_inject
Python
should_inject(context_length: int, already_injected: int) -> bool

Determine if tenet should be injected.

Source code in tenets/models/tenet.py
Python
def should_inject(self, context_length: int, already_injected: int) -> bool:
    """Determine if tenet should be injected."""
    if already_injected >= self.max_per_context:
        return False

    if self.frequency == "always":
        return True
    elif self.frequency == "adaptive":
        # Inject based on context length and previous injections
        if context_length < 5000:
            return already_injected == 0
        elif context_length < 20000:
            return already_injected < 2
        else:
            return already_injected < self.max_per_context

    return False

Tenetdataclass

Python
Tenet(id: str = (lambda: str(uuid.uuid4()))(), content: str = '', priority: Priority = Priority.MEDIUM, category: Optional[TenetCategory] = None, status: TenetStatus = TenetStatus.PENDING, created_at: datetime = datetime.now(), instilled_at: Optional[datetime] = None, updated_at: datetime = datetime.now(), session_bindings: list[str] = list(), author: Optional[str] = None, metrics: TenetMetrics = TenetMetrics(), injection_strategy: InjectionStrategy = InjectionStrategy(), metadata: dict[str, Any] = dict())

A guiding principle for code development.

Tenets are persistent instructions that guide AI interactions to maintain consistency across multiple prompts and sessions.

ATTRIBUTEDESCRIPTION
id

Unique identifier

TYPE:str

content

The principle text

TYPE:str

priority

Importance level

TYPE:Priority

category

Classification category

TYPE:Optional[TenetCategory]

status

Current status (pending, instilled, archived)

TYPE:TenetStatus

created_at

When the tenet was created

TYPE:datetime

instilled_at

When first instilled into context

TYPE:Optional[datetime]

updated_at

Last modification time

TYPE:datetime

session_bindings

Sessions this tenet applies to

TYPE:list[str]

author

Who created the tenet

TYPE:Optional[str]

metrics

Usage and effectiveness metrics

TYPE:TenetMetrics

injection_strategy

How this tenet should be injected

TYPE:InjectionStrategy

metadata

Additional custom data

TYPE:dict[str, Any]

Example

tenet = Tenet( ... content="Always use type hints in Python code", ... priority=Priority.HIGH, ... category=TenetCategory.STYLE ... )

Functions
instill
Python
instill() -> None

Mark tenet as instilled.

Source code in tenets/models/tenet.py
Python
def instill(self) -> None:
    """Mark tenet as instilled."""
    self.status = TenetStatus.INSTILLED
    if not self.instilled_at:
        self.instilled_at = datetime.now()
    self.updated_at = datetime.now()
archive
Python
archive() -> None

Archive this tenet.

Source code in tenets/models/tenet.py
Python
def archive(self) -> None:
    """Archive this tenet."""
    self.status = TenetStatus.ARCHIVED
    self.updated_at = datetime.now()
bind_to_session
Python
bind_to_session(session_id: str) -> None

Bind tenet to a specific session.

Source code in tenets/models/tenet.py
Python
def bind_to_session(self, session_id: str) -> None:
    """Bind tenet to a specific session."""
    if session_id not in self.session_bindings:
        self.session_bindings.append(session_id)
        self.updated_at = datetime.now()
unbind_from_session
Python
unbind_from_session(session_id: str) -> None

Remove session binding.

Source code in tenets/models/tenet.py
Python
def unbind_from_session(self, session_id: str) -> None:
    """Remove session binding."""
    if session_id in self.session_bindings:
        self.session_bindings.remove(session_id)
        self.updated_at = datetime.now()
applies_to_session
Python
applies_to_session(session_id: Optional[str]) -> bool

Check if tenet applies to a session.

Source code in tenets/models/tenet.py
Python
def applies_to_session(self, session_id: Optional[str]) -> bool:
    """Check if tenet applies to a session."""
    if not self.session_bindings:
        return True  # Global tenet
    return session_id in self.session_bindings if session_id else False
should_inject
Python
should_inject(context_length: int, already_injected: int) -> bool

Determine if this tenet should be injected.

Source code in tenets/models/tenet.py
Python
def should_inject(self, context_length: int, already_injected: int) -> bool:
    """Determine if this tenet should be injected."""
    # Archived tenets are never injected
    if self.status == TenetStatus.ARCHIVED:
        return False

    # Check injection strategy
    if not self.injection_strategy.should_inject(context_length, already_injected):
        return False

    # High priority tenets are injected more frequently
    if self.priority == Priority.CRITICAL:
        return True
    elif self.priority == Priority.HIGH:
        return self.metrics.reinforcement_needed or already_injected == 0
    else:
        return already_injected == 0 or self.metrics.reinforcement_needed
format_for_injection
Python
format_for_injection() -> str

Format tenet content for injection into context.

Source code in tenets/models/tenet.py
Python
def format_for_injection(self) -> str:
    """Format tenet content for injection into context."""
    prefix = f"[{self.category.value.upper()}]" if self.category else "[PRINCIPLE]"
    return f"{prefix} {self.content}"
to_dict
Python
to_dict() -> dict[str, Any]

Convert to dictionary representation.

Source code in tenets/models/tenet.py
Python
def to_dict(self) -> dict[str, Any]:
    """Convert to dictionary representation."""
    return {
        "id": self.id,
        "content": self.content,
        "priority": self.priority.value,
        "category": self.category.value if self.category else None,
        "status": self.status.value,
        "created_at": self.created_at.isoformat(),
        "instilled_at": self.instilled_at.isoformat() if self.instilled_at else None,
        "updated_at": self.updated_at.isoformat(),
        "session_bindings": self.session_bindings,
        "author": self.author,
        "metrics": {
            "injection_count": self.metrics.injection_count,
            "last_injected": (
                self.metrics.last_injected.isoformat() if self.metrics.last_injected else None
            ),
            "contexts_appeared_in": self.metrics.contexts_appeared_in,
            "compliance_score": self.metrics.compliance_score,
            "reinforcement_needed": self.metrics.reinforcement_needed,
        },
        "injection_strategy": {
            "frequency": self.injection_strategy.frequency,
            "position": self.injection_strategy.position,
            "max_per_context": self.injection_strategy.max_per_context,
        },
        "metadata": self.metadata,
    }
from_dictclassmethod
Python
from_dict(data: dict[str, Any]) -> Tenet

Create Tenet from dictionary.

Source code in tenets/models/tenet.py
Python
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "Tenet":
    """Create Tenet from dictionary."""
    # Parse dates
    created_at = (
        datetime.fromisoformat(data["created_at"]) if "created_at" in data else datetime.now()
    )
    instilled_at = (
        datetime.fromisoformat(data["instilled_at"]) if data.get("instilled_at") else None
    )
    updated_at = (
        datetime.fromisoformat(data["updated_at"]) if "updated_at" in data else datetime.now()
    )

    # Parse metrics
    metrics = TenetMetrics()
    if "metrics" in data:
        m = data["metrics"]
        metrics.injection_count = m.get("injection_count", 0)
        metrics.contexts_appeared_in = m.get("contexts_appeared_in", 0)
        metrics.compliance_score = m.get("compliance_score", 0.0)
        metrics.reinforcement_needed = m.get("reinforcement_needed", False)
        if m.get("last_injected"):
            metrics.last_injected = datetime.fromisoformat(m["last_injected"])

    # Parse injection strategy
    strategy = InjectionStrategy()
    if "injection_strategy" in data:
        s = data["injection_strategy"]
        strategy.frequency = s.get("frequency", "adaptive")
        strategy.position = s.get("position", "strategic")
        strategy.max_per_context = s.get("max_per_context", 3)

    return cls(
        id=data.get("id", str(uuid.uuid4())),
        content=data["content"],
        priority=Priority(data.get("priority", "medium")),
        category=TenetCategory(data["category"]) if data.get("category") else None,
        status=TenetStatus(data.get("status", "pending")),
        created_at=created_at,
        instilled_at=instilled_at,
        updated_at=updated_at,
        session_bindings=data.get("session_bindings", []),
        author=data.get("author"),
        metrics=metrics,
        injection_strategy=strategy,
        metadata=data.get("metadata", {}),
    )

TenetCollectiondataclass

Python
TenetCollection(name: str, description: str = '', tenets: list[Tenet] = list(), created_at: datetime = datetime.now(), tags: list[str] = list())

A collection of related tenets.

Functions
add_tenet
Python
add_tenet(tenet: Tenet) -> None

Add a tenet to the collection.

Source code in tenets/models/tenet.py
Python
def add_tenet(self, tenet: Tenet) -> None:
    """Add a tenet to the collection."""
    if tenet not in self.tenets:
        self.tenets.append(tenet)
remove_tenet
Python
remove_tenet(tenet_id: str) -> bool

Remove a tenet by ID.

Source code in tenets/models/tenet.py
Python
def remove_tenet(self, tenet_id: str) -> bool:
    """Remove a tenet by ID."""
    for i, tenet in enumerate(self.tenets):
        if tenet.id == tenet_id:
            self.tenets.pop(i)
            return True
    return False
get_by_category
Python
get_by_category(category: TenetCategory) -> list[Tenet]

Get all tenets of a specific category.

Source code in tenets/models/tenet.py
Python
def get_by_category(self, category: TenetCategory) -> list[Tenet]:
    """Get all tenets of a specific category."""
    return [t for t in self.tenets if t.category == category]
get_by_priority
Python
get_by_priority(priority: Priority) -> list[Tenet]

Get all tenets of a specific priority.

Source code in tenets/models/tenet.py
Python
def get_by_priority(self, priority: Priority) -> list[Tenet]:
    """Get all tenets of a specific priority."""
    return [t for t in self.tenets if t.priority == priority]
to_dict
Python
to_dict() -> dict[str, Any]

Convert to dictionary representation.

Source code in tenets/models/tenet.py
Python
def to_dict(self) -> dict[str, Any]:
    """Convert to dictionary representation."""
    return {
        "name": self.name,
        "description": self.description,
        "created_at": self.created_at.isoformat(),
        "tags": self.tags,
        "tenets": [t.to_dict() for t in self.tenets],
    }