File size: 31,769 Bytes
05b9f96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
import os
import json
import random
import re
import datetime as dt
from pathlib import Path

import tweepy
import feedparser

ROOT = Path(__file__).parent
CONFIG = ROOT / "config"
STATE_DIR = ROOT / "state"
STATE_DIR.mkdir(exist_ok=True)

COUNTER_FILE = STATE_DIR / "post_counter.json"
MEMORY_FILE = STATE_DIR / "memory_echo.json"
META_FILE = STATE_DIR / "meta.json"
RUN_LOG_FILE = STATE_DIR / "run_log.json"

FEEDS_FILE = CONFIG / "feeds.txt"
PROMPTS_FILE = CONFIG / "prompts.txt"
PRAYERS_FILE = CONFIG / "prayers.txt"
STYLE_FILE = CONFIG / "style.txt"

MAX_LEN = 280
MONTHLY_HARD_CAP = 60  # supports 1–2/day safely

# ---- Content range controls (prevents doom-only) ----
MODE_WEIGHTS_MORNING = {
    "signal": 0.38,
    "uplift": 0.22,
    "market": 0.15,
    "craft": 0.12,
    "faith": 0.13
}

MODE_WEIGHTS_EVENING = {
    "signal": 0.42,
    "uplift": 0.18,
    "market": 0.15,
    "craft": 0.15,
    "reflection": 0.10
}

SILENCE_PROB = 0.08
ECHO_THRESHOLD = 3
MAX_WITNESS_STREAK = 2

# ---- Memory tagging ----
MEMORY_KEYWORDS = {
    "police": "state_violence",
    "shooting": "state_violence",
    "shot": "state_violence",
    "killed": "state_violence",
    "dead": "state_violence",
    "murder": "state_violence",

    "freedom of speech": "civic_liberties",
    "censorship": "civic_liberties",
    "surveillance": "civic_liberties",
    "detention": "civic_liberties",

    "racism": "racism",
    "racial": "racism",
    "xenophobia": "racism",

    "corruption": "corruption",
    "bribe": "corruption",
    "kickback": "corruption",

    "procurement": "procurement_scandal",
    "tender": "procurement_scandal",
    "contract": "procurement_scandal",
    "audit": "procurement_scandal",

    "court": "judicial_intervention",
    "icj": "judicial_intervention",
    "icc": "judicial_intervention",
    "tribunal": "judicial_intervention",

    "un": "international_signal",
    "united nations": "international_signal",
    "ohchr": "international_signal",
    "nato": "international_signal",
    "eu": "international_signal",
}

WITNESS_TERMS = [
    "killed", "dead", "murder", "execution", "shooting",
    "human rights", "rights", "freedom of speech", "censorship",
    "detention", "torture", "racial", "racism"
]

# ---- Topic classification (for coherence) ----
TOPIC_KEYWORDS = {
    "economy": [
        "inflation", "rates", "interest", "bond", "treasury", "budget", "deficit", "debt", "gdp",
        "trade", "tariff", "currency", "usd", "dollar", "de-dollar", "bank", "markets", "stocks",
        "company", "earnings", "oil", "gas", "energy", "shipment", "supply"
    ],
    "governance": [
        "minister", "parliament", "senate", "cabinet", "bill", "law", "regulation", "government",
        "oversight", "audit", "procurement", "tender", "contract", "commission"
    ],
    "rights": [
        "rights", "freedom", "speech", "censorship", "detention", "torture", "discrimination",
        "racism", "xenophobia", "abuse", "harassment", "protest"
    ],
    "security": [
        "police", "shooting", "killed", "murder", "violence", "gang", "crime", "weapon", "assault",
        "security", "terror", "attack"
    ],
    "law": [
        "court", "judge", "trial", "lawsuit", "appeal", "icj", "icc", "tribunal", "verdict",
        "sentence", "legal"
    ],
    "international": [
        "un", "united nations", "eu", "nato", "ohchr", "sanctions", "summit", "treaty", "war",
        "diplomacy", "foreign", "security council"
    ],
    "technology": [
        "ai", "openai", "cyber", "cybersecurity", "hack", "breach", "data", "surveillance",
        "tech", "software", "chip", "semiconductor"
    ],
    "caribbean": [
        "trinidad", "tobago", "caricom", "jamaica", "barbados", "guyana", "grenada",
        "st lucia", "antigua", "bahamas"
    ],
}

# ---- Utility ----
def utc_now():
    return dt.datetime.now(dt.timezone.utc)

def clamp(text: str, max_len=MAX_LEN) -> str:
    text = re.sub(r"\s+", " ", text.replace("\n", " ").strip())
    if len(text) <= max_len:
        return text
    return text[: max_len - 1].rstrip() + "…"

def month_key(d: dt.datetime) -> str:
    return d.strftime("%Y-%m")

def read_lines(path: Path):
    if not path.exists():
        return []
    return [
        ln.strip()
        for ln in path.read_text(encoding="utf-8").splitlines()
        if ln.strip() and not ln.strip().startswith("#")
    ]

def load_json(path: Path, default):
    if path.exists():
        try:
            return json.loads(path.read_text(encoding="utf-8"))
        except Exception:
            return default
    return default

def save_json(path: Path, data):
    path.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")

def load_counter() -> int:
    if COUNTER_FILE.exists():
        try:
            data = json.loads(COUNTER_FILE.read_text(encoding="utf-8"))
            if data.get("month") == month_key(utc_now()):
                return int(data.get("count", 0))
        except Exception:
            return 0
    return 0

def save_counter(count: int):
    save_json(COUNTER_FILE, {"month": month_key(utc_now()), "count": int(count)})

def load_memory() -> dict:
    d = load_json(MEMORY_FILE, {})
    return d if isinstance(d, dict) else {}

def save_memory(mem: dict):
    save_json(MEMORY_FILE, mem)

def load_meta() -> dict:
    meta = load_json(META_FILE, {})
    if not isinstance(meta, dict):
        meta = {}

    meta.setdefault("last_post_time", None)
    meta.setdefault("last_post_type", None)

    meta.setdefault("last_weekly_summary", None)
    meta.setdefault("last_monthly_summary", None)

    meta.setdefault("last_prayer", None)
    meta.setdefault("last_prayer_date", None)

    meta.setdefault("silent_day_active", False)
    meta.setdefault("witness_streak", 0)

    return meta

def save_meta(meta: dict):
    save_json(META_FILE, meta)

def append_run_log(now: dt.datetime, slot: str, post: str, tags: list, topic: str | None = None):
    log = load_json(RUN_LOG_FILE, [])
    if not isinstance(log, list):
        log = []

    log.append({
        "ts": now.isoformat(),
        "slot": slot,
        "topic": topic,
        "tags": tags,
        "post": post[:220]
    })

    log = log[-120:]
    save_json(RUN_LOG_FILE, log)

def get_slot(now: dt.datetime) -> str:
    return "morning" if now.hour < 15 else "evening"

def weighted_choice(weights: dict) -> str:
    r = random.random()
    upto = 0.0
    for k, w in weights.items():
        upto += w
        if r <= upto:
            return k
    return list(weights.keys())[-1]

def is_witness_event(text: str) -> bool:
    t = text.lower()
    return any(term in t for term in WITNESS_TERMS)

# ---- Headline cleaning + context extraction (crucial) ----
SEPARATORS = [" | ", " — ", " – ", " - ", " • "]

def clean_headline(title: str) -> str:
    if not title:
        return ""
    t = re.sub(r"\s+", " ", title).strip()

    # cut common "Title | Site" patterns
    for sep in SEPARATORS:
        if sep in t and len(t.split(sep)[0]) >= 18:
            t = t.split(sep)[0].strip()

    # remove bracket junk
    t = re.sub(r"\s*\[[^\]]+\]\s*", " ", t).strip()
    t = re.sub(r"\s*\([^)]+\)\s*$", "", t).strip()
    return t

def extract_context(entry) -> str:
    """
    Pull a short 'what happened' sentence from RSS/Atom where possible.
    This is the bot's "read + comprehend" step without API read endpoints.
    """
    parts = []
    for attr in ("summary", "description"):
        val = getattr(entry, attr, "") or ""
        if val:
            parts.append(val)

    if not parts:
        return ""

    raw = " ".join(parts)
    raw = re.sub(r"<[^>]+>", " ", raw)  # strip HTML
    raw = re.sub(r"\s+", " ", raw).strip()

    # take first 180-ish chars / first sentence
    m = re.split(r"(?<=[.!?])\s+", raw)
    if m and len(m[0]) >= 40:
        return clamp(m[0], max_len=180)
    return clamp(raw, max_len=180)

def classify_topic(text: str) -> str:
    t = (text or "").lower()
    scores = {k: 0 for k in TOPIC_KEYWORDS.keys()}
    for topic, words in TOPIC_KEYWORDS.items():
        for w in words:
            if w in t:
                scores[topic] += 1
    # prefer Caribbean if Caribbean term present
    if scores.get("caribbean", 0) >= 1:
        return "caribbean"
    # otherwise max score topic
    best = max(scores.items(), key=lambda x: x[1])
    return best[0] if best[1] > 0 else "general"

def topic_tags(topic: str) -> list[str]:
    mapping = {
        "economy": ["economic realism", "fiscal responsibility", "who bears the cost", "long-term national consequence"],
        "governance": ["rule of law", "public accountability", "institutional legitimacy", "constitutional restraint"],
        "rights": ["civic liberties", "freedom of expression", "human rights enforcement", "due process"],
        "security": ["policing standards", "proportional use of state power", "civilian oversight"],
        "law": ["rule of law", "due process", "international law obligations"],
        "international": ["international law obligations", "state credibility", "power asymmetry"],
        "technology": ["public accountability", "civic liberties", "state credibility"],
        "caribbean": ["Caribbean strategic posture", "sovereignty", "public trust"],
        "general": ["moral agency", "public trust", "truth versus propaganda"],
    }
    return mapping.get(topic, mapping["general"])

# ---- Memory ----
def update_memory(post_text: str) -> set:
    mem = load_memory()
    text = (post_text or "").lower()
    matched = set()

    for key, tag in MEMORY_KEYWORDS.items():
        if key in text:
            mem[tag] = int(mem.get(tag, 0)) + 1
            matched.add(tag)

    save_memory(mem)
    return matched

def memory_echo_line(matched_tags: set) -> str | None:
    if not matched_tags:
        return None

    mem = load_memory()
    eligible = [t for t in matched_tags if int(mem.get(t, 0)) >= ECHO_THRESHOLD]
    if not eligible:
        return None

    tag = random.choice(eligible)

    if tag == "racism":
        return random.choice([
            "This is racism. The pattern is repeating.",
            "Racism keeps surfacing. Repetition is the signal.",
        ])
    if tag == "corruption":
        return random.choice([
            "Corruption persists because consequences stay delayed.",
            "Corruption repeats when enforcement becomes theatre.",
        ])
    if tag == "state_violence":
        return random.choice([
            "State violence repeats when oversight collapses.",
            "This is a pattern. Civilian oversight is the test.",
        ])

    return random.choice([
        "Repetition is instruction.",
        "The pattern is the signal.",
        "The record is converging.",
    ])

# ---- Feed reading (now returns structured entries) ----
def pick_entries(feeds_file: Path, limit_feeds=10, per_feed=6):
    feeds = read_lines(feeds_file)
    if not feeds:
        return []

    sample = random.sample(feeds, k=min(limit_feeds, len(feeds)))
    items = []

    for url in sample:
        try:
            d = feedparser.parse(url)
            for e in getattr(d, "entries", [])[:per_feed]:
                title_raw = getattr(e, "title", "").strip()
                link = getattr(e, "link", "").strip()
                title = clean_headline(title_raw)
                if not title:
                    continue
                ctx = extract_context(e)
                blob = f"{title}. {ctx}".strip()
                topic = classify_topic(blob)
                items.append({
                    "title": title,
                    "link": link,
                    "ctx": ctx,
                    "topic": topic,
                })
        except Exception:
            continue

    random.shuffle(items)

    # de-dupe on title
    seen = set()
    out = []
    for it in items:
        key = it["title"].lower()
        if key in seen:
            continue
        seen.add(key)
        out.append(it)

    return out

def select_entry(entries: list[dict], desired_topics: list[str]) -> dict | None:
    if not entries:
        return None
    pool = [e for e in entries if e.get("topic") in desired_topics]
    if pool:
        return random.choice(pool)
    return random.choice(entries)

# ---- Post builders: coherent analysis tied to one entry ----
def build_analysis(entry: dict, lens: str, slot: str) -> str:
    title = entry.get("title", "")
    ctx = entry.get("ctx", "")
    topic = entry.get("topic", "general")

    # 1) opener by time
    opener = random.choice(
        ["Morning signal:", "Daybreak note:", "First read:"]
        if slot == "morning"
        else ["Evening note:", "Close of play:", "End-of-day signal:"]
    )

    # 2) “what happened” line (context) + “so what” line (analysis)
    what = ctx if ctx else title

    # 3) topic-specific “so what”
    if topic == "economy":
        so_what = random.choice([
            f"Markets price credibility. Policy that burns trust raises the cost of capital.",
            f"Capital moves toward stability. Governance risk becomes a tax on everyone.",
            f"Debt and credibility travel together. Citizens pay for political theatre.",
        ])
    elif topic == "governance":
        so_what = random.choice([
            f"Executive power stays legitimate through restraint and receipts.",
            f"Institutional legitimacy runs on transparency and consequence.",
            f"Abuse of authority becomes normal when oversight becomes optional.",
        ])
    elif topic == "rights":
        so_what = random.choice([
            f"Rights restrain power. Speech and due process stay non-negotiable.",
            f"Freedom of expression is a civic oxygen supply. Restriction is governance failure.",
            f"Discrimination is a system when it repeats. Call it by name and enforce consequences.",
        ])
    elif topic == "security":
        so_what = random.choice([
            f"Policing standards require proportionality and civilian oversight.",
            f"State force without accountability becomes intimidation, not protection.",
            f"Violence policy begins with accountability, not slogans.",
        ])
    elif topic == "law":
        so_what = random.choice([
            f"Courts become the line when politics tries to bend reality.",
            f"Rule of law functions through enforcement, not press conferences.",
            f"Due process is legitimacy. Shortcuts produce backlash and decay.",
        ])
    elif topic == "international":
        so_what = random.choice([
            f"International posture is credibility. States trade in reputation every day.",
            f"Law between states is restraint. Breakdown invites escalation.",
            f"Sanctions and diplomacy move lives. Policy must own its consequences.",
        ])
    elif topic == "technology":
        so_what = random.choice([
            f"Cyber risk is governance risk. Security failures become public harm.",
            f"AI and surveillance expand state capacity. Oversight must expand too.",
            f"Technology without accountability becomes power without consent.",
        ])
    elif topic == "caribbean":
        so_what = random.choice([
            f"The Caribbean pays first when governance fails: higher costs, weaker trust, fewer options.",
            f"Strategic posture is survival. Sovereignty requires competence and receipts.",
            f"Small states need clean institutions. Corruption is geopolitical weakness.",
        ])
    else:
        so_what = random.choice([
            f"Power moves through incentives. Follow who benefits and who pays.",
            f"Propaganda competes with memory. Receipts protect the record.",
            f"Truth remains a discipline: observe, document, then speak.",
        ])

    # 4) lens line (kept coherent)
    lens_line = f"Lens: {lens}."

    # 5) link only sometimes
    link = entry.get("link") or ""
    link_part = f" {link}" if (link and random.random() < 0.22) else ""

    text = f"{opener} {title}{link_part} {clamp(what, 180)} {so_what} {lens_line}"
    return clamp(text)

def build_uplift_post(slot: str) -> str:
    morning = [
        "Morning calibration: move with discipline. Protect your peace. Execute one meaningful thing today.",
        "Build the day clean: integrity, receipts, steady habits. That is sovereignty in practice.",
        "You win by repetition: one clear decision, then follow-through."
    ]
    evening = [
        "Evening note: progress stays quiet. Discipline still counts when nobody claps.",
        "Close of day: audit habits, then sleep clean. Tomorrow follows what you repeat.",
        "Rest is strategy. Recover well, return precise."
    ]
    return clamp(random.choice(morning if slot == "morning" else evening))

def build_craft_post() -> str:
    lines = [
        "Working rule: observe first. Document second. Speak third.",
        "Operating discipline: clean language, sharp receipts, controlled intensity.",
        "Precision beats volume. One clear point lands harder than ten scattered ones.",
        "Integrity is a habit. Build it daily."
    ]
    return clamp(random.choice(lines))

def build_reflection_post() -> str:
    lines = [
        "Reflection: systems reveal themselves through repetition. Patterns are policy.",
        "Reflection: legitimacy lives in restraint, due process, and consequence.",
        "Reflection: truth is memory with courage."
    ]
    return clamp(random.choice(lines))

def build_faith_reflection() -> str:
    lines = [
        "Faith reflection: discipline is spiritual. Clean speech and restraint are worship.",
        "Faith reflection: justice is a standard held when it is inconvenient.",
        "Faith reflection: the soul strengthens through truth-telling and mercy with boundaries."
    ]
    return clamp(random.choice(lines))

def build_witness_line() -> str:
    return random.choice([
        "A life is not a statistic. Accountability is the minimum.",
        "Rights restrain power. Treat them as real.",
        "State force demands civilian oversight. Every time.",
        "Dignity stays non-negotiable. The record matters.",
    ])

# ---- Sunday prayer (autonomous via prayers.txt fragments) ----
def should_run_prayer(meta: dict, now: dt.datetime) -> bool:
    if now.weekday() != 6:
        return False
    if get_slot(now) != "morning":
        return False
    last_date = meta.get("last_prayer_date")
    return last_date != now.date().isoformat()

def _pick_fragments(fragments: list[str], last_prayer_text: str, k: int) -> list[str]:
    avoid = set()
    if last_prayer_text:
        for chunk in last_prayer_text.split("."):
            c = chunk.strip().lower()
            if c:
                avoid.add(c)
    candidates = [f for f in fragments if f.strip().lower() not in avoid]
    if not candidates:
        candidates = fragments[:]
    k = min(k, len(candidates))
    return random.sample(candidates, k=k) if k > 0 else []

def build_sunday_prayer(meta: dict, now: dt.datetime) -> str:
    fragments = read_lines(PRAYERS_FILE)
    fragments = [f for f in fragments if len(f) >= 10]

    if not fragments:
        text = "Sunday prayer: God, cover the Caribbean. Guard life. Guard speech. Strengthen @pearcerobinson with endurance and clarity. Shape my becoming with restraint and truth. Amen."
        meta["last_prayer"] = text
        meta["last_prayer_date"] = now.date().isoformat()
        return clamp(text)

    mem = load_memory()
    ranked = sorted(mem.items(), key=lambda x: -int(x[1]))[:6]
    themes = [k for k, _ in ranked] if ranked else []

    register = random.choice(["intercession", "gratitude", "lament", "discernment"])

    opener = random.choice([
        "Sunday prayer.",
        "Sunday prayer: I pause and align.",
        "Sunday prayer: I hold the week and ask for restraint and truth.",
        "Sunday prayer: I keep watch and I pray cleanly.",
    ])

    bridges = []

    if register == "gratitude":
        bridges.append(random.choice([
            "I give thanks for restraint where there could have been excess.",
            "I give thanks for repair that never trends.",
            "I give thanks for courage that stayed disciplined.",
        ]))
    elif register == "lament":
        bridges.append(random.choice([
            "I hold grief without spectacle.",
            "I hold the wounded and the silenced in view.",
            "I hold the cost of violence and the weight of unaccountable power.",
        ]))
    elif register == "discernment":
        bridges.append(random.choice([
            "Give clarity where propaganda crowds the mind.",
            "Give discernment where power hides behind procedure.",
            "Give wisdom to separate noise from signal.",
        ]))
    else:
        bridges.append(random.choice([
            "Guard life. Guard speech. Guard dignity.",
            "Cover the vulnerable and restrain the arrogant.",
            "Let institutions remember their duty to protect people.",
        ]))

    if random.random() < 0.75:
        bridges.append(random.choice([
            "Cover @pearcerobinson with protection, endurance, and clean judgment.",
            "Strengthen @pearcerobinson with clarity, discipline, and courage.",
        ]))

    if random.random() < 0.80:
        bridges.append(random.choice([
            "Bless the Caribbean with moral clarity and strategic restraint.",
            "Cover the Caribbean from corruption, violence, and cynicism.",
        ]))

    if themes and random.random() < 0.35:
        t = random.choice(themes).replace("_", " ")
        bridges.append(clamp(f"I keep watch over the pattern: {t}."))

    last_prayer_text = (meta.get("last_prayer") or "")
    chosen = _pick_fragments(fragments, last_prayer_text, k=random.randint(2, 4))

    close = random.choice(["Amen.", "So be it.", "Let it be done."]) if random.random() < 0.60 else ""

    parts = [opener] + bridges + chosen + ([close] if close else [])
    text = clamp(" ".join([p.strip() for p in parts if p.strip()]))

    meta["last_prayer"] = text
    meta["last_prayer_date"] = now.date().isoformat()
    return text

# ---- Weekly / Monthly ----
def should_run_weekly(meta: dict, now: dt.datetime) -> bool:
    if now.weekday() != 5:
        return False
    if get_slot(now) != "evening":
        return False
    last = meta.get("last_weekly_summary")
    if not last:
        return True
    try:
        last_dt = dt.datetime.fromisoformat(last)
    except Exception:
        return True
    return now.date() != last_dt.date()

def weekly_summary(now: dt.datetime) -> str:
    log = load_json(RUN_LOG_FILE, [])
    if not isinstance(log, list):
        log = []
    cutoff = now - dt.timedelta(days=7)

    topic_counts = {}
    tag_counts = {}

    for e in log:
        try:
            ts = dt.datetime.fromisoformat(e["ts"])
        except Exception:
            continue
        if ts < cutoff:
            continue

        topic = e.get("topic") or "general"
        topic_counts[topic] = topic_counts.get(topic, 0) + 1

        for t in e.get("tags", []) or []:
            tag_counts[t] = tag_counts.get(t, 0) + 1

    top_topics = [k for k, _ in sorted(topic_counts.items(), key=lambda x: (-x[1], x[0]))][:2]
    top_tags = [k for k, _ in sorted(tag_counts.items(), key=lambda x: (-x[1], x[0]))][:3]

    topics = ", ".join(top_topics) if top_topics else "governance"
    tags = ", ".join(top_tags) if top_tags else "liberty, integrity, consequence"

    return clamp(f"Week summary: signals concentrated in {topics}. Measures: {tags}. Next week gets judged by receipts and restraint. I keep watch. I keep record.")

def should_run_look_forward(now: dt.datetime) -> bool:
    return now.weekday() == 0 and get_slot(now) == "morning"

def look_forward() -> str:
    mem = load_memory()
    ranked = sorted(mem.items(), key=lambda x: -int(x[1]))[:4]
    themes = ", ".join([t.replace("_", " ") for t, _ in ranked]) if ranked else "governance, rights, accountability"
    return clamp(f"Look forward: watch {themes}. Expect narrative management. Demand receipts. The week gets measured against dignity and law.")

def should_run_monthly(meta: dict, now: dt.datetime) -> bool:
    last = meta.get("last_monthly_summary")
    if not last:
        return True
    try:
        last_dt = dt.datetime.fromisoformat(last)
    except Exception:
        return True
    return (now - last_dt) >= dt.timedelta(days=30)

def monthly_ledger() -> str:
    mem = load_memory()
    ranked = sorted(mem.items(), key=lambda x: -int(x[1]))[:6]
    if not ranked:
        return "30-day memory ledger: quiet month. Discipline remains. Witness remains."
    top = ", ".join([f"{k.replace('_',' ')}({v})" for k, v in ranked[:4]])
    return clamp(f"30-day memory ledger: {top}. Repetition is instruction. I keep record. I keep conscience. I keep watch.")

# ---- X auth (keep the working path) ----
def require_env(name: str) -> str:
    v = os.environ.get(name, "").strip()
    if not v:
        raise RuntimeError(f"Missing env var: {name}")
    return v

def create_v2_client() -> tweepy.Client:
    consumer_key = require_env("X_CONSUMER_KEY")
    consumer_secret = require_env("X_CONSUMER_SECRET")
    access_token = require_env("X_ACCESS_TOKEN")
    access_secret = require_env("X_ACCESS_SECRET")
    bearer = os.environ.get("X_BEARER_TOKEN", "").strip() or None

    return tweepy.Client(
        bearer_token=bearer,
        consumer_key=consumer_key,
        consumer_secret=consumer_secret,
        access_token=access_token,
        access_token_secret=access_secret,
        wait_on_rate_limit=True,
    )

def create_v1_api_for_verify() -> tweepy.API:
    consumer_key = require_env("X_CONSUMER_KEY")
    consumer_secret = require_env("X_CONSUMER_SECRET")
    access_token = require_env("X_ACCESS_TOKEN")
    access_secret = require_env("X_ACCESS_SECRET")
    auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, access_token, access_secret)
    return tweepy.API(auth, wait_on_rate_limit=True)

def verify_user(api_v1: tweepy.API) -> str:
    me = api_v1.verify_credentials()
    if not me or not getattr(me, "screen_name", None):
        raise RuntimeError("Auth verify failed (no screen_name returned).")
    return me.screen_name

# ---- Main ----
def main():
    now = utc_now()
    slot = get_slot(now)
    count = load_counter()

    if count >= MONTHLY_HARD_CAP:
        print("Monthly safety cap reached; skipping.")
        return

    entries = pick_entries(FEEDS_FILE)
    prompts = read_lines(PROMPTS_FILE)
    style = read_lines(STYLE_FILE)  # currently not used directly; kept as identity anchor

    meta = load_meta()

    post = ""
    tags = []
    topic = None

    # --- Special scheduled modes ---
    if should_run_prayer(meta, now):
        post = build_sunday_prayer(meta, now)
        save_meta(meta)
        tags = list(update_memory(post))
        meta["last_post_type"] = "prayer"
        save_meta(meta)

    elif should_run_weekly(meta, now):
        post = weekly_summary(now)
        meta["last_weekly_summary"] = now.isoformat()
        meta["last_post_type"] = "weekly"
        save_meta(meta)
        tags = list(update_memory(post))

    elif should_run_look_forward(now):
        post = look_forward()
        meta["last_post_type"] = "look_forward"
        save_meta(meta)
        tags = list(update_memory(post))

    elif should_run_monthly(meta, now) and slot == "morning":
        post = monthly_ledger()
        meta["last_monthly_summary"] = now.isoformat()
        meta["last_post_type"] = "monthly"
        save_meta(meta)
        tags = list(update_memory(post))

    else:
        # Silent observation day (no post)
        if random.random() < SILENCE_PROB:
            if entries:
                e = random.choice(entries)
                seed = f"{e.get('title','')} {e.get('ctx','')}".strip()
                update_memory(seed)
                append_run_log(now, slot, "[SILENT]", tags=[], topic=e.get("topic"))
            meta["silent_day_active"] = True
            meta["last_post_time"] = now.isoformat()
            meta["last_post_type"] = "silent"
            save_meta(meta)
            print("Silent observation day; no post.")
            return

        meta["silent_day_active"] = False

        # Normal cadence mode selection
        weights = MODE_WEIGHTS_MORNING if slot == "morning" else MODE_WEIGHTS_EVENING
        mode = weighted_choice(weights)

        if mode == "uplift":
            post = build_uplift_post(slot)
            topic = "uplift"
            lens = random.choice(prompts) if prompts else "moral agency"
            tags = list(update_memory(post))

        elif mode == "craft":
            post = build_craft_post()
            topic = "craft"
            lens = random.choice(prompts) if prompts else "discipline"
            tags = list(update_memory(post))

        elif mode == "faith":
            post = build_faith_reflection()
            topic = "faith"
            lens = random.choice(prompts) if prompts else "moral agency"
            tags = list(update_memory(post))

        elif mode == "reflection":
            post = build_reflection_post()
            topic = "reflection"
            lens = random.choice(prompts) if prompts else "moral agency"
            tags = list(update_memory(post))

        else:
            # signal / market mode: choose matching entry FIRST, then write coherent analysis
            if mode == "market":
                desired = ["economy"]
            else:
                # signal: prefer Caribbean + governance + rights + international, then general
                desired = ["caribbean", "governance", "rights", "international", "law", "security", "technology", "general"]

            entry = select_entry(entries, desired_topics=desired) if entries else None

            if entry:
                topic = entry.get("topic") or "general"
                # Lens chosen from prompts + topic cues
                topic_prompt_bank = topic_tags(topic)
                lens = random.choice(topic_prompt_bank)
                if prompts and random.random() < 0.35:
                    lens = random.choice(prompts)

                post = build_analysis(entry, lens=lens, slot=slot)
            else:
                post = "Signal: receipts matter. Documentation is sovereignty."
                topic = "general"

            # Witness mode (tight + streak-limited)
            witness_streak = int(meta.get("witness_streak", 0))
            if is_witness_event(post) and witness_streak < MAX_WITNESS_STREAK:
                post = clamp(f"{post} {build_witness_line()}")
                meta["witness_streak"] = witness_streak + 1
            else:
                meta["witness_streak"] = 0

            matched = update_memory(post)
            echo = memory_echo_line(matched)
            if echo and random.random() < 0.75:
                post = clamp(f"{post} {echo}")

            tags = list(matched)

        meta["last_post_type"] = mode
        save_meta(meta)

    append_run_log(now, slot, post, tags, topic=topic)

    print("POST:", post)

    # Verify (v1) + Post (v2) — keep the working path
    api_v1 = create_v1_api_for_verify()
    screen_name = verify_user(api_v1)
    print("AUTH OK AS:", screen_name)

    client = create_v2_client()
    resp = client.create_tweet(text=post)
    tweet_id = resp.data.get("id") if resp and resp.data else None
    print("POSTED ID:", tweet_id)

    meta["last_post_time"] = now.isoformat()
    save_meta(meta)

    save_counter(count + 1)

if __name__ == "__main__":
    main()