generated from coulomb/repo-seed
T44: ActivityDefinition markdown file parser (definition_parser.py)
- Scans activity-definitions/*.md and ACTIVITY_DEFINITION_DIRS paths
- Parses YAML frontmatter + fenced rule/instruction blocks
- Raises ParseError on any malformed file — never silently skips
T45: ActivityDefinition sync command
- Migration 0006: adds rules_json/instructions_json JSONB columns
- sync_activity_definitions.py + make sync-activity-definitions
- Called at worker startup before schedule sync
T46: Rule/instruction pipeline wired into RunActivityWorkflow
- New evaluate_rules and emit_tasks Temporal activities
- Workflow passes event_envelope_json to enable rule evaluation
- EventRouter now passes full envelope JSON as 4th workflow arg
- IssueSink.emit() writes task_spawn_log rows per task
T47: ScheduledTriggerConfig model (one-off future datetime trigger)
T48: One-off Temporal Schedule support
- Fixed timezone_name → time_zone_name (was causing all schedule tests to fail)
- Added ScheduleCalendarSpec-based one-off schedule with remaining_actions=1
- cancel_scheduled() for admin cancellation
- Fixed backfill() call to use *args unpacking (not list wrapper)
- Fixed ScheduleAlreadyRunningError catch in upsert_schedule
- sync_schedules now handles ScheduledTriggerConfig definitions
T49: Webhook receiver
- POST /webhooks/gitea — HMAC-SHA256 via X-Gitea-Signature-256
- POST /webhooks/github — HMAC-SHA256 via X-Hub-Signature-256
- Normalisers: repo.created, push, issue.closed → EventEnvelope
- Publishes to NATS activity.{type} subject after registry validation
- Mounted in api.py at /webhooks prefix
T50: Gitea event type definitions
- gitea.repo.created.md, gitea.push.md, gitea.issue.closed.md
- Each includes normaliser field mapping in Consumer Notes
Tests: 18 passed, 1 skipped (integration). Fixed embedded Temporal
server visibility latency in test_upsert_schedule_creates_schedule.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
157 lines
6.5 KiB
Python
157 lines
6.5 KiB
Python
"""
|
|
Core domain models for activity-core.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Annotated, Any, Literal, Union
|
|
from datetime import datetime
|
|
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
# ── EventEnvelope (T40) ───────────────────────────────────────────────────────
|
|
|
|
class EventEnvelope(BaseModel):
|
|
"""Standard internal event envelope. All inbound events (NATS, webhook, cron)
|
|
are normalised into this shape before processing."""
|
|
|
|
id: str = Field(description="UUID v4 — stable unique ID for deduplication.")
|
|
type: str = Field(description="Dot-namespaced event type, e.g. 'org.repo.registered'.")
|
|
version: str = Field(default="1.0", description="Schema version string.")
|
|
timestamp: datetime = Field(description="When the event occurred (UTC).")
|
|
publisher: str = Field(description="Originating service, e.g. 'the-custodian/state-hub'.")
|
|
attributes: dict[str, Any] = Field(
|
|
default_factory=dict,
|
|
description="Event-specific attributes; structure varies by event type.",
|
|
)
|
|
|
|
@classmethod
|
|
def from_nats_message(cls, msg: Any) -> "EventEnvelope":
|
|
"""Decode a NATS JetStream message into an EventEnvelope."""
|
|
raw = json.loads(msg.data.decode())
|
|
return cls.model_validate(raw)
|
|
|
|
@classmethod
|
|
def from_webhook_payload(cls, source: str, payload: dict) -> "EventEnvelope":
|
|
"""Build an EventEnvelope from a raw webhook payload (pre-normalised)."""
|
|
return cls.model_validate(payload)
|
|
|
|
|
|
# ── Trigger configs ───────────────────────────────────────────────────────────
|
|
|
|
class CronTriggerConfig(BaseModel):
|
|
trigger_type: Literal["cron"] = "cron"
|
|
cron_expression: str = Field(
|
|
description="Standard 5-field cron expression, e.g. '0 9 * * 1-5'."
|
|
)
|
|
timezone: str = Field(default="UTC", description="IANA timezone name.")
|
|
jitter_seconds: int = Field(default=0, ge=0)
|
|
misfire_policy: Literal["skip", "catchup", "compress"] = Field(default="skip")
|
|
|
|
|
|
class EventTriggerConfig(BaseModel):
|
|
trigger_type: Literal["event"] = "event"
|
|
event_type: str = Field(
|
|
description="Matches EventEnvelope.type. Router fires this activity on match."
|
|
)
|
|
filters: dict[str, Any] = Field(
|
|
default_factory=dict,
|
|
description="All filters must match EventEnvelope.attributes for routing.",
|
|
)
|
|
|
|
|
|
class ScheduledTriggerConfig(BaseModel):
|
|
"""One-off future trigger that fires once at a specified UTC datetime."""
|
|
|
|
trigger_type: Literal["scheduled"] = "scheduled"
|
|
at: datetime = Field(description="UTC datetime when the workflow should be triggered.")
|
|
timezone: str = Field(default="UTC", description="IANA timezone name (informational).")
|
|
|
|
|
|
TriggerConfig = Annotated[
|
|
Union[CronTriggerConfig, EventTriggerConfig, ScheduledTriggerConfig],
|
|
Field(discriminator="trigger_type"),
|
|
]
|
|
|
|
|
|
# ── Rules and instructions (T34) ──────────────────────────────────────────────
|
|
|
|
class ActionDef(BaseModel):
|
|
task_template: str = Field(description="Path to task template .md, relative to repo root.")
|
|
target_repo: str | None = Field(
|
|
default=None,
|
|
description="Attribute-access expression or literal repo slug.",
|
|
)
|
|
priority: str = Field(default="medium")
|
|
labels: list[str] = Field(default_factory=list)
|
|
due_in_days: int | None = Field(default=None)
|
|
|
|
|
|
class RuleDef(BaseModel):
|
|
id: str
|
|
condition: str = Field(
|
|
default="",
|
|
description="Rule DSL expression; empty string means always true.",
|
|
)
|
|
action: ActionDef
|
|
|
|
|
|
class InstructionDef(BaseModel):
|
|
id: str
|
|
condition: str = Field(
|
|
default="",
|
|
description="Optional pre-filter using Rule DSL; empty means always execute.",
|
|
)
|
|
trusted_fields: list[str] = Field(
|
|
description="Allowlist of event/context fields that may appear in the prompt template.",
|
|
)
|
|
model: str = Field(description="LLM model identifier, e.g. 'claude-sonnet-4-6'.")
|
|
prompt: str = Field(description="Prompt template with {field.path} placeholders.")
|
|
output_schema: str = Field(description="Path to JSON Schema file for output validation.")
|
|
review_required: bool = Field(default=False)
|
|
|
|
|
|
# ── Context sources ───────────────────────────────────────────────────────────
|
|
|
|
class ContextSource(BaseModel):
|
|
"""One external data source that the workflow queries to build the context snapshot."""
|
|
|
|
name: str = Field(description="Logical name; referenced as 'context.<name>' in templates.")
|
|
type: str = Field(description="Source adapter type: 'repo-scoping' | 'state-hub' | etc.")
|
|
query: str = Field(default="", description="Named query to execute against the source.")
|
|
params: dict[str, Any] = Field(default_factory=dict)
|
|
bind_to: str = Field(default="", description="Context key to bind the result to.")
|
|
|
|
|
|
# ── Task templates (legacy) ───────────────────────────────────────────────────
|
|
|
|
class TaskTemplate(BaseModel):
|
|
"""Legacy task template — ignored when ActivityDefinition.rules is non-empty."""
|
|
|
|
task_type: str
|
|
condition: str | None = None
|
|
params_template: dict[str, Any] = Field(default_factory=dict)
|
|
|
|
|
|
# ── ActivityDefinition ────────────────────────────────────────────────────────
|
|
|
|
class ActivityDefinition(BaseModel):
|
|
"""Versioned definition: trigger + context sources + rules/instructions."""
|
|
|
|
id: UUID
|
|
name: str
|
|
enabled: bool = True
|
|
trigger_config: TriggerConfig
|
|
context_sources: list[ContextSource] = Field(default_factory=list)
|
|
# New rule/instruction pipeline (T34)
|
|
rules: list[RuleDef] = Field(default_factory=list)
|
|
instructions: list[InstructionDef] = Field(default_factory=list)
|
|
# Legacy — ignored when rules is non-empty
|
|
task_templates: list[TaskTemplate] = Field(default_factory=list)
|
|
dedupe_key_strategy: Literal["skip", "catchup", "compress"] = Field(default="skip")
|
|
version: int = Field(default=1, ge=1)
|
|
status: str = Field(default="active")
|