267 lines
12 KiB
Python
267 lines
12 KiB
Python
"""Persistent evidence ledger, versioned situational assessments, sessions, workflow runs, watches, and stage presentations."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy import Column, DateTime, Float, Index, Integer, JSON, String, Text
|
|
from sqlalchemy.sql import func
|
|
|
|
from app.core.time import to_iso8601_utc
|
|
from app.db.session import Base
|
|
|
|
|
|
class EvidenceRecord(Base):
|
|
__tablename__ = "evidence_records"
|
|
|
|
id = Column(String(120), primary_key=True)
|
|
source_type = Column(String(40), nullable=False, default="curated_report", index=True)
|
|
title = Column(String(500), nullable=False)
|
|
publisher = Column(String(255), nullable=False, default="")
|
|
url = Column(String(2000), nullable=False, default="")
|
|
locator = Column(String(500), nullable=False, default="")
|
|
excerpt = Column(Text, nullable=False, default="")
|
|
content_sha256 = Column(String(64), nullable=False, index=True)
|
|
published_at = Column(String(40), nullable=True)
|
|
retrieved_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
|
metadata_json = Column(JSON, nullable=False, default=dict)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"source_type": self.source_type,
|
|
"title": self.title,
|
|
"publisher": self.publisher,
|
|
"url": self.url,
|
|
"locator": self.locator,
|
|
"excerpt": self.excerpt,
|
|
"content_sha256": self.content_sha256,
|
|
"published_at": self.published_at,
|
|
"retrieved_at": to_iso8601_utc(self.retrieved_at),
|
|
"metadata": self.metadata_json or {},
|
|
}
|
|
|
|
|
|
class SituationalAssessment(Base):
|
|
"""Canonical ResultRevision store for compute posture and infrastructure incident assessments."""
|
|
|
|
__tablename__ = "situational_assessments"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
subject_type = Column(String(60), nullable=False, index=True)
|
|
subject_key = Column(String(255), nullable=False, index=True)
|
|
input_fingerprint = Column(String(64), nullable=False, index=True)
|
|
revision = Column(Integer, nullable=False, default=1)
|
|
generation_mode = Column(String(40), nullable=False, default="deterministic_baseline")
|
|
payload = Column(JSON, nullable=False, default=dict)
|
|
evidence_ids = Column(JSON, nullable=False, default=list)
|
|
audit_meta = Column(JSON, nullable=False, default=dict)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True)
|
|
|
|
__table_args__ = (
|
|
Index("idx_situational_assessments_subject", "subject_type", "subject_key", "revision"),
|
|
Index("idx_situational_assessments_fingerprint", "subject_type", "input_fingerprint"),
|
|
)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"result_id": self.subject_key,
|
|
"subject_type": self.subject_type,
|
|
"subject_key": self.subject_key,
|
|
"input_fingerprint": self.input_fingerprint,
|
|
"revision": self.revision,
|
|
"generation_mode": self.generation_mode,
|
|
"payload": self.payload or {},
|
|
"evidence_ids": self.evidence_ids or [],
|
|
"audit_meta": self.audit_meta or {},
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
}
|
|
|
|
|
|
class InvestigationSession(Base):
|
|
"""Continuous multi-turn spatial investigation context & pinned workbench tabs."""
|
|
|
|
__tablename__ = "investigation_sessions"
|
|
|
|
id = Column(String(64), primary_key=True)
|
|
domain = Column(String(40), nullable=False, default="compute", index=True)
|
|
owner_key = Column(String(120), nullable=False, default="anonymous", index=True)
|
|
access_tier = Column(String(40), nullable=False, default="public_zero_cost")
|
|
context_state = Column(JSON, nullable=False, default=dict)
|
|
pinned_items = Column(JSON, nullable=False, default=list)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True)
|
|
updated_at = Column(
|
|
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
|
|
)
|
|
|
|
def to_dict(self) -> dict:
|
|
ctx = self.context_state or {}
|
|
return {
|
|
"session_id": self.id,
|
|
"id": self.id,
|
|
"domain": self.domain,
|
|
"owner_key": self.owner_key,
|
|
"access_tier": self.access_tier,
|
|
"active_run_id": ctx.get("active_run_id"),
|
|
"context_state": ctx,
|
|
"pinned_items": self.pinned_items or [],
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
"updated_at": to_iso8601_utc(self.updated_at),
|
|
}
|
|
|
|
|
|
class WorkflowRun(Base):
|
|
"""Bounded workflow execution trace, budget ledger, and cancellation state."""
|
|
|
|
__tablename__ = "workflow_runs"
|
|
|
|
id = Column(String(64), primary_key=True)
|
|
session_id = Column(String(64), nullable=True, index=True)
|
|
workflow_id = Column(String(80), nullable=False, index=True)
|
|
status = Column(String(24), nullable=False, default="completed", index=True)
|
|
access_tier = Column(String(40), nullable=False, default="public_zero_cost")
|
|
actor_key = Column(String(120), nullable=False, default="anonymous", index=True)
|
|
question = Column(Text, nullable=False, default="")
|
|
input_params = Column(JSON, nullable=False, default=dict)
|
|
tool_trace = Column(JSON, nullable=False, default=list)
|
|
budget_used = Column(JSON, nullable=False, default=dict)
|
|
result_subject_key = Column(String(255), nullable=True, index=True)
|
|
result_revision = Column(Integer, nullable=True)
|
|
error_message = Column(Text, nullable=False, default="")
|
|
started_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True)
|
|
completed_at = Column(DateTime(timezone=True), nullable=True)
|
|
|
|
__table_args__ = (
|
|
Index("idx_workflow_runs_actor_started", "actor_key", "started_at"),
|
|
)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"run_id": self.id,
|
|
"id": self.id,
|
|
"session_id": self.session_id,
|
|
"workflow_id": self.workflow_id,
|
|
"status": self.status,
|
|
"access_tier": self.access_tier,
|
|
"actor_key": self.actor_key,
|
|
"question": self.question,
|
|
"input_params": self.input_params or {},
|
|
"tool_trace": self.tool_trace or [],
|
|
"budget_used": self.budget_used or {},
|
|
"result_subject_key": self.result_subject_key,
|
|
"result_revision": self.result_revision,
|
|
"error_message": self.error_message or "",
|
|
"started_at": to_iso8601_utc(self.started_at),
|
|
"completed_at": to_iso8601_utc(self.completed_at),
|
|
}
|
|
|
|
|
|
class TopicWatch(Base):
|
|
"""Proactive watch subscription triggered by semantic snapshot deltas."""
|
|
|
|
__tablename__ = "topic_watches"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
watch_key = Column(String(160), nullable=False, index=True)
|
|
domain = Column(String(40), nullable=False, default="compute", index=True)
|
|
target_scope = Column(JSON, nullable=False, default=dict)
|
|
trigger_policy = Column(JSON, nullable=False, default=dict)
|
|
owner_key = Column(String(120), nullable=False, default="anonymous", index=True)
|
|
status = Column(String(24), nullable=False, default="active", index=True)
|
|
last_triggered_revision = Column(Integer, nullable=True)
|
|
last_triggered_at = Column(DateTime(timezone=True), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True)
|
|
|
|
__table_args__ = (
|
|
Index("idx_topic_watches_owner_key", "owner_key", "watch_key"),
|
|
)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"watch_key": self.watch_key,
|
|
"domain": self.domain,
|
|
"target_scope": self.target_scope or {},
|
|
"trigger_policy": self.trigger_policy or {},
|
|
"owner_key": self.owner_key,
|
|
"status": self.status,
|
|
"last_triggered_revision": self.last_triggered_revision,
|
|
"last_triggered_at": to_iso8601_utc(self.last_triggered_at),
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
}
|
|
|
|
|
|
class StagePresentation(Base):
|
|
__tablename__ = "stage_presentations"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
presentation_key = Column(String(160), nullable=False, index=True)
|
|
domain = Column(String(40), nullable=False, index=True)
|
|
time_basis = Column(String(40), nullable=False, default="live")
|
|
revision = Column(Integer, nullable=False, default=1)
|
|
title = Column(String(255), nullable=False)
|
|
summary = Column(Text, nullable=False, default="")
|
|
contract = Column(JSON, nullable=False, default=dict)
|
|
evidence_ids = Column(JSON, nullable=False, default=list)
|
|
expires_at = Column(DateTime(timezone=True), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True)
|
|
|
|
__table_args__ = (
|
|
Index("idx_stage_presentations_key_rev", "presentation_key", "revision"),
|
|
Index("idx_stage_presentations_domain_cursor", "domain", "id"),
|
|
)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"cursor": self.id,
|
|
"id": self.id,
|
|
"presentation_key": self.presentation_key,
|
|
"domain": self.domain,
|
|
"time_basis": self.time_basis,
|
|
"revision": self.revision,
|
|
"title": self.title,
|
|
"summary": self.summary,
|
|
"contract": self.contract or {},
|
|
"evidence_ids": self.evidence_ids or [],
|
|
"expires_at": to_iso8601_utc(self.expires_at),
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
}
|
|
|
|
|
|
class ComputeEvidenceCandidate(Base):
|
|
__tablename__ = "compute_evidence_candidates"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
|
|
status = Column(String(24), nullable=False, default="pending", index=True)
|
|
candidate_type = Column(String(40), nullable=False, default="observation")
|
|
proposed_payload = Column(JSON, nullable=False, default=dict)
|
|
source_payload = Column(JSON, nullable=False, default=dict)
|
|
comparison = Column(JSON, nullable=False, default=dict)
|
|
dedupe_fingerprint = Column(String(64), nullable=True, index=True)
|
|
confidence = Column(Float, nullable=False, default=0.8)
|
|
extractor_note = Column(Text, nullable=False, default="")
|
|
review_reason_code = Column(String(40), nullable=True)
|
|
review_reason = Column(Text, nullable=False, default="")
|
|
published_publish_id = Column(String(64), nullable=True, index=True)
|
|
reviewed_by = Column(String(120), nullable=True)
|
|
reviewed_at = Column(DateTime(timezone=True), nullable=True)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False, index=True)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"id": self.id,
|
|
"status": self.status,
|
|
"candidate_type": self.candidate_type,
|
|
"proposed_payload": self.proposed_payload or {},
|
|
"source_payload": self.source_payload or {},
|
|
"comparison": self.comparison or {},
|
|
"dedupe_fingerprint": self.dedupe_fingerprint,
|
|
"confidence": self.confidence,
|
|
"extractor_note": self.extractor_note,
|
|
"review_reason_code": self.review_reason_code,
|
|
"review_reason": self.review_reason or "",
|
|
"published_publish_id": self.published_publish_id,
|
|
"reviewed_by": self.reviewed_by,
|
|
"reviewed_at": to_iso8601_utc(self.reviewed_at),
|
|
"created_at": to_iso8601_utc(self.created_at),
|
|
}
|