Clean up nested duplicate: v2/v2/kintsugi_vendor/kintsugi/bdi/store.py
Browse files
v2/v2/kintsugi_vendor/kintsugi/bdi/store.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
"""In-memory BDI store with revision history."""
|
| 2 |
-
|
| 3 |
-
from dataclasses import asdict
|
| 4 |
-
from datetime import datetime, timezone
|
| 5 |
-
from typing import Any, Dict, List, Optional
|
| 6 |
-
|
| 7 |
-
from .models import (
|
| 8 |
-
BDIBelief,
|
| 9 |
-
BDIDesire,
|
| 10 |
-
BDIIntention,
|
| 11 |
-
BDISnapshot,
|
| 12 |
-
BeliefStatus,
|
| 13 |
-
DesireStatus,
|
| 14 |
-
IntentionStatus,
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
class BDIStore:
|
| 19 |
-
"""Thread-unsafe in-memory store for BDI entities with revision tracking."""
|
| 20 |
-
|
| 21 |
-
def __init__(self, org_id: str) -> None:
|
| 22 |
-
self.org_id = org_id
|
| 23 |
-
self._beliefs: Dict[str, BDIBelief] = {}
|
| 24 |
-
self._desires: Dict[str, BDIDesire] = {}
|
| 25 |
-
self._intentions: Dict[str, BDIIntention] = {}
|
| 26 |
-
self._revisions: List[dict] = []
|
| 27 |
-
|
| 28 |
-
# ------------------------------------------------------------------
|
| 29 |
-
# Beliefs
|
| 30 |
-
# ------------------------------------------------------------------
|
| 31 |
-
|
| 32 |
-
def add_belief(self, belief: BDIBelief) -> None:
|
| 33 |
-
self._record_revision("belief", belief.id, None, asdict(belief))
|
| 34 |
-
self._beliefs[belief.id] = belief
|
| 35 |
-
|
| 36 |
-
def update_belief(self, belief_id: str, **updates: Any) -> None:
|
| 37 |
-
belief = self._beliefs.get(belief_id)
|
| 38 |
-
if belief is None:
|
| 39 |
-
raise KeyError(f"Belief {belief_id!r} not found")
|
| 40 |
-
before = asdict(belief)
|
| 41 |
-
for key, value in updates.items():
|
| 42 |
-
if not hasattr(belief, key):
|
| 43 |
-
raise AttributeError(f"BDIBelief has no attribute {key!r}")
|
| 44 |
-
setattr(belief, key, value)
|
| 45 |
-
belief.version += 1
|
| 46 |
-
belief.last_reviewed = datetime.now(timezone.utc)
|
| 47 |
-
self._record_revision("belief", belief_id, before, asdict(belief))
|
| 48 |
-
|
| 49 |
-
def get_belief(self, belief_id: str) -> Optional[BDIBelief]:
|
| 50 |
-
return self._beliefs.get(belief_id)
|
| 51 |
-
|
| 52 |
-
def list_beliefs(self, status: Optional[BeliefStatus] = None) -> List[BDIBelief]:
|
| 53 |
-
beliefs = list(self._beliefs.values())
|
| 54 |
-
if status is not None:
|
| 55 |
-
beliefs = [b for b in beliefs if b.status == status]
|
| 56 |
-
return beliefs
|
| 57 |
-
|
| 58 |
-
def archive_belief(self, belief_id: str) -> None:
|
| 59 |
-
self.update_belief(belief_id, status=BeliefStatus.ARCHIVED)
|
| 60 |
-
|
| 61 |
-
# ------------------------------------------------------------------
|
| 62 |
-
# Desires
|
| 63 |
-
# ------------------------------------------------------------------
|
| 64 |
-
|
| 65 |
-
def add_desire(self, desire: BDIDesire) -> None:
|
| 66 |
-
self._record_revision("desire", desire.id, None, asdict(desire))
|
| 67 |
-
self._desires[desire.id] = desire
|
| 68 |
-
|
| 69 |
-
def update_desire(self, desire_id: str, **updates: Any) -> None:
|
| 70 |
-
desire = self._desires.get(desire_id)
|
| 71 |
-
if desire is None:
|
| 72 |
-
raise KeyError(f"Desire {desire_id!r} not found")
|
| 73 |
-
before = asdict(desire)
|
| 74 |
-
for key, value in updates.items():
|
| 75 |
-
if not hasattr(desire, key):
|
| 76 |
-
raise AttributeError(f"BDIDesire has no attribute {key!r}")
|
| 77 |
-
setattr(desire, key, value)
|
| 78 |
-
desire.version += 1
|
| 79 |
-
desire.last_reviewed = datetime.now(timezone.utc)
|
| 80 |
-
self._record_revision("desire", desire_id, before, asdict(desire))
|
| 81 |
-
|
| 82 |
-
def get_desire(self, desire_id: str) -> Optional[BDIDesire]:
|
| 83 |
-
return self._desires.get(desire_id)
|
| 84 |
-
|
| 85 |
-
def list_desires(self, status: Optional[DesireStatus] = None) -> List[BDIDesire]:
|
| 86 |
-
desires = list(self._desires.values())
|
| 87 |
-
if status is not None:
|
| 88 |
-
desires = [d for d in desires if d.status == status]
|
| 89 |
-
return desires
|
| 90 |
-
|
| 91 |
-
def suspend_desire(self, desire_id: str) -> None:
|
| 92 |
-
self.update_desire(desire_id, status=DesireStatus.SUSPENDED)
|
| 93 |
-
|
| 94 |
-
# ------------------------------------------------------------------
|
| 95 |
-
# Intentions
|
| 96 |
-
# ------------------------------------------------------------------
|
| 97 |
-
|
| 98 |
-
def add_intention(self, intention: BDIIntention) -> None:
|
| 99 |
-
self._record_revision("intention", intention.id, None, asdict(intention))
|
| 100 |
-
self._intentions[intention.id] = intention
|
| 101 |
-
|
| 102 |
-
def update_intention(self, intention_id: str, **updates: Any) -> None:
|
| 103 |
-
intention = self._intentions.get(intention_id)
|
| 104 |
-
if intention is None:
|
| 105 |
-
raise KeyError(f"Intention {intention_id!r} not found")
|
| 106 |
-
before = asdict(intention)
|
| 107 |
-
for key, value in updates.items():
|
| 108 |
-
if not hasattr(intention, key):
|
| 109 |
-
raise AttributeError(f"BDIIntention has no attribute {key!r}")
|
| 110 |
-
setattr(intention, key, value)
|
| 111 |
-
intention.version += 1
|
| 112 |
-
intention.last_reviewed = datetime.now(timezone.utc)
|
| 113 |
-
self._record_revision("intention", intention_id, before, asdict(intention))
|
| 114 |
-
|
| 115 |
-
def get_intention(self, intention_id: str) -> Optional[BDIIntention]:
|
| 116 |
-
return self._intentions.get(intention_id)
|
| 117 |
-
|
| 118 |
-
def list_intentions(
|
| 119 |
-
self, status: Optional[IntentionStatus] = None
|
| 120 |
-
) -> List[BDIIntention]:
|
| 121 |
-
intentions = list(self._intentions.values())
|
| 122 |
-
if status is not None:
|
| 123 |
-
intentions = [i for i in intentions if i.status == status]
|
| 124 |
-
return intentions
|
| 125 |
-
|
| 126 |
-
def complete_intention(self, intention_id: str) -> None:
|
| 127 |
-
self.update_intention(
|
| 128 |
-
intention_id, status=IntentionStatus.COMPLETED, progress=1.0
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
# ------------------------------------------------------------------
|
| 132 |
-
# Snapshot & history
|
| 133 |
-
# ------------------------------------------------------------------
|
| 134 |
-
|
| 135 |
-
def get_snapshot(self) -> BDISnapshot:
|
| 136 |
-
return BDISnapshot(
|
| 137 |
-
org_id=self.org_id,
|
| 138 |
-
beliefs=list(self._beliefs.values()),
|
| 139 |
-
desires=list(self._desires.values()),
|
| 140 |
-
intentions=list(self._intentions.values()),
|
| 141 |
-
snapshot_at=datetime.now(timezone.utc),
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
def get_revision_history(
|
| 145 |
-
self, entity_type: str, entity_id: str
|
| 146 |
-
) -> List[dict]:
|
| 147 |
-
return [
|
| 148 |
-
r
|
| 149 |
-
for r in self._revisions
|
| 150 |
-
if r["entity_type"] == entity_type and r["entity_id"] == entity_id
|
| 151 |
-
]
|
| 152 |
-
|
| 153 |
-
def _record_revision(
|
| 154 |
-
self,
|
| 155 |
-
entity_type: str,
|
| 156 |
-
entity_id: str,
|
| 157 |
-
before: Optional[dict],
|
| 158 |
-
after: dict,
|
| 159 |
-
) -> None:
|
| 160 |
-
self._revisions.append(
|
| 161 |
-
{
|
| 162 |
-
"entity_type": entity_type,
|
| 163 |
-
"entity_id": entity_id,
|
| 164 |
-
"before": before,
|
| 165 |
-
"after": after,
|
| 166 |
-
"timestamp": datetime.now(timezone.utc).isoformat(),
|
| 167 |
-
}
|
| 168 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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