-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorchestrator.py
More file actions
96 lines (86 loc) · 3.69 KB
/
Copy pathorchestrator.py
File metadata and controls
96 lines (86 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from datetime import datetime
from agents.classifier import ClassifierAgent
from agents.policy import PolicyAgent
from agents.trend import TrendAgent
from agents.lens import LensAgent
from data.mock_posts import MOCK_POSTS
from time_windows import rolling_window_bounds
class Orchestrator:
def __init__(self, blackboard):
self.bb = blackboard
self.classifier = ClassifierAgent(blackboard)
self.policy = PolicyAgent(blackboard)
self.trend = TrendAgent(blackboard)
self.lens = LensAgent(blackboard)
def process_post(self, post):
self.bb.write("orchestrator", f"source_post_{post['post_id']}", post)
classification = self.classifier.run(post)
policy_result = self.policy.run(post["post_id"])
return {"post": post, "classification": classification, "policy": policy_result}
def run_batch(self, posts, source_context=None):
timestamps = [datetime.fromisoformat(post["created_at"]) for post in posts]
window_end = max(timestamps)
window_start, window_end = rolling_window_bounds(window_end)
window_posts = [
post
for post, timestamp in zip(posts, timestamps, strict=True)
if window_start <= timestamp <= window_end
]
results = []
verified_posts = []
for post in window_posts:
result = self.process_post(post)
results.append(result)
if result["policy"].get("passes_filter"):
verified_posts.append(
{
"post_id": post["post_id"],
"subreddit": post["subreddit"],
"title": post["title"],
"body": post["body"],
"engagement_score": result["classification"]["engagement_score"],
"engagement_tier": result["classification"]["engagement_tier"],
"created_at": post["created_at"],
}
)
trend_result = self.trend.run(verified_posts, window_start, window_end)
total = len(window_posts)
passed = len(verified_posts)
signal_quality_score = passed / total if total > 0 else 0
batch_summary = {
"total_posts": total,
"excluded_outside_window": len(posts) - total,
"verified_posts": passed,
"pass_rate": signal_quality_score,
"signal_quality_score": signal_quality_score,
"classification_cache_hits": 0,
"estimated_llm_calls_this_run": 2,
"trend_window": {
"type": "rolling_30_day",
"start": window_start.isoformat(),
"end": window_end.isoformat(),
"recurrence_rule": "Topic must appear in all four weekly buckets.",
},
"source_context": source_context or {"analysis_scope": "unspecified"},
"trend_analysis": trend_result,
"individual_results": results,
}
lens_context = {
key: value for key, value in batch_summary.items() if key != "individual_results"
}
lens_result = self.lens.run(lens_context)
return {"batch_summary": batch_summary, "intelligence": lens_result}
def run_simulation(self):
batches = [MOCK_POSTS]
all_results = []
for i, batch in enumerate(batches):
result = self.run_batch(
batch,
source_context={
"analysis_scope": "mock_simulation",
"source": "data.mock_posts",
},
)
result["batch_number"] = i + 1
all_results.append(result)
return all_results