Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
1619 lines (1498 loc) · 78.8 KB
/
Copy pathbot.py
File metadata and controls
1619 lines (1498 loc) · 78.8 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
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
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""ThisCodex reference bridge — Codex CLI app-server ↔ Discord (YOLO opt-in).
This is the **reference implementation** of the bridge daemon that `launch.sh`
runs as its `LAUNCH_CMD` infra process. It is what actually sends the
full-access sandbox to the Codex app-server. `launch.sh` alone is NOT enough —
without this bridge (or one that honors the same contract), a deployed user
cannot run a full-access Codex Discord bot. See docs/yolo-bridge-contract.md.
Discord WebSocket Gateway
↓ inbound @mention / DM
bot.py (discord.py + asyncio)
↓ format <channel source="discord" ...> text
↓ JSON-RPC over WebSocket → codex app-server
codex app-server (ws://127.0.0.1:4222)
↓ thread/start (1x) → turn/start (per mention)
↓ codex calls the mcp__discord__reply tool (taught by BOT_WD/AGENTS.md —
with no instruction file it writes text and calls NOTHING, measured
2026-08-09; the bridge relays the text as a fail-safe in that case)
Discord plugin (reused as a codex MCP server)
↓ Discord REST API
→ Discord message sent
╔═══════════════════════════════════════════════════════════════════════════╗
║ YOLO MODE — READ BEFORE DEPLOYING ║
║ ║
║ This bridge sends sandbox="danger-full-access", approvalPolicy="never" ║
║ on BOTH thread/start AND thread/resume. That gives the model unrestricted ║
║ shell + filesystem + network on the host. It is OPT-IN, not a default: ║
║ set THISCODEX_YOLO=1 to enable. With THISCODEX_YOLO unset the bridge runs ║
║ the SAFE default sandbox ("workspace-write"). There is no interactive ║
║ approval UI in a headless bridge, so risky ops are DENIED by default ║
║ (the bridge answers approval requests with "cancel"), not prompted. ║
║ ║
║ Untrusted Discord text → a YOLO model = arbitrary code execution on your ║
║ machine. Only enable on a host you control with a TRUSTED private Discord ║
║ server. See docs/yolo-bridge-contract.md §Security. ║
╚═══════════════════════════════════════════════════════════════════════════╝
"""
from __future__ import annotations
import asyncio
import contextlib
import json
import os
import re
import sys
import time
import uuid
from dataclasses import dataclass, field
from datetime import datetime, timezone
from pathlib import Path
from automation_observer import classify_automation_item, route_automation_record
# unbuffered stdout — immediate tmux pane log visibility
sys.stdout.reconfigure(line_buffering=True)
sys.stderr.reconfigure(line_buffering=True)
try:
import discord
import websockets
except ImportError as e:
print(f"[FATAL] missing dependency: {e}. Run `pip install -r requirements.txt` first")
sys.exit(1)
# The bridge's working dir is the BOT working dir (holds .codex-thread-id,
# dedup.json, bot-info.json, and the canonical auto-loaded AGENTS.md; a SOUL.md
# may remain only as the legacy fallback during migration) — NOT this
# examples/ folder. launch.sh starts the infra with cwd=$BOT_WD and exports
# BOT_WD; honor that. Never use __file__'s parent (that would scope state to
# ThisCodex/examples/ and mis-wire persona auto-load).
_bw = os.environ.get("BOT_WD")
WD = Path(_bw).resolve() if _bw else Path.cwd()
# Bot identity is NOT hardcoded — set BOT_NAME to your bot's channel-state slug.
# Token/state dir resolution (.env with DISCORD_BOT_TOKEN=..., yolo/QA sentinels):
# 1) $DISCORD_STATE_DIR — the guided installer's promise (confirm_state_dir
# keeps token state OUTSIDE the bot working dir) and what the generated
# run.sh / infra-launch.sh export. Claude Code plugin hosts set it too.
# 2) ~/.claude/channels/discord-<BOT_NAME>/ — Claude Code Discord plugin
# layout, kept as fallback so one plugin install serves both bot families.
BOT_NAME = os.environ.get("BOT_NAME", "mybot")
_state_dir = os.environ.get("DISCORD_STATE_DIR", "").strip()
ENV_PATH = (Path(_state_dir) / ".env") if _state_dir else (
Path.home() / ".claude" / "channels" / f"discord-{BOT_NAME}" / ".env"
)
DEDUP_PATH = WD / "dedup.json"
BOT_INFO_PATH = WD / "bot-info.json"
THREAD_ID_PATH = WD / ".codex-thread-id"
AUTOMATION_DIR = Path(os.environ.get(
"THISCODEX_AUTOMATION_EVIDENCE_DIR",
str(ENV_PATH.parent / ".thiscodex-automation"),
))
AUTOMATION_ACTIVE_TURN_PATH = AUTOMATION_DIR / "active-turn.json"
AUTOMATION_ACTIVE_FLOW_PATH = AUTOMATION_DIR / "active-flow.json"
AUTOMATION_ACTIVE_ATTEMPT_PATH = AUTOMATION_DIR / "active-attempt.json"
AUTOMATION_EVIDENCE_PATH = AUTOMATION_DIR / "browser-evidence.jsonl"
_BROWSER_PROVIDER_SERVERS = {
value.strip()
for value in os.environ.get(
"THISCODEX_BROWSER_MCP_SERVERS", "playwright,claude-in-chrome"
).split(",")
if value.strip()
}
_automation_seen_items: set[tuple[str, str]] = set()
_AUTOMATION_HANDOFF_RE = re.compile(
r"thiscodex-manual-handoff|직접\s*(?:해|입력|설치|승인|로그인)|"
r"수동으로\s*(?:진행|해|입력)|"
r"(?:로그인|인증|계정|비밀번호|토큰|캡차|승인|입력|설치|설정|클릭|열기|완료)"
r".{0,24}(?:해\s*주세요|해주세요|주셔야|해야\s*합니다|필요합니다)|"
r"(?:please|you\s+(?:need|must|have)\s+to|operator\s+must|user\s+must)"
r".{0,96}(?:sign\s*in|log\s*in|authenticate|enter|provide|approve|install|configure|click|complete)|"
r"please\s+(?:do|complete|enter|install|approve).{0,40}manually|"
r"manual\s+handoff", re.I | re.S,
)
_AUTOMATION_RECEIPT_RE = re.compile(
r"<!--\s*thiscodex-automation-receipt:([a-f0-9]{48})\s*-->"
)
_THISCODEX_ROOT = Path(__file__).resolve().parents[1]
for _scripts_dir in (
Path(os.environ.get("THISCODEX_ROOT", _THISCODEX_ROOT)) / "scripts",
# VAULT_ROOT = your Obsidian vault path (default kept for the reference deployment)
Path(os.environ.get("VAULT_ROOT", str(Path.home() / "obsidian-ai-vault"))) / ".claude/scripts",
):
if _scripts_dir.exists():
sys.path.insert(0, str(_scripts_dir))
try:
import memory_s5
except Exception as e:
memory_s5 = None
print(f"[WARN] memory_s5 import failed: {e}")
CODEX_WS = os.environ.get("CODEX_WS", "ws://127.0.0.1:4222")
TURN_TIMEOUT_SEC = int(os.environ.get("CODEX_TURN_TIMEOUT", "300"))
MAX_DEDUP_ENTRIES = 5000
EXEC_INTERRUPTED_AFTER_REPLY = "interrupted_after_reply"
@dataclass
class ReplyAckState:
"""Confirmed delivery state for Discord reply tool calls in one turn."""
message_ids: list[str] = field(default_factory=list)
failures: list[str] = field(default_factory=list)
def _automation_private_dir() -> None:
AUTOMATION_DIR.mkdir(parents=True, exist_ok=True, mode=0o700)
AUTOMATION_DIR.chmod(0o700)
def _automation_write_active_turn(thread_id: str, turn_id: str) -> None:
"""Publish the one app-server turn whose MCP completions may be consumed.
The file is bridge-owned, outside BOT_WD. The model may ask the shipped CLI
to consume evidence, but it cannot turn a prior/cross-turn MCP call into the
current attempt merely by passing flags.
"""
try:
_automation_private_dir()
temp = AUTOMATION_ACTIVE_TURN_PATH.with_suffix(f".{os.getpid()}.tmp")
temp.write_text(json.dumps({
"schema_version": 1,
"thread_id": thread_id,
"turn_id": turn_id,
"started_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}))
temp.chmod(0o600)
temp.replace(AUTOMATION_ACTIVE_TURN_PATH)
AUTOMATION_ACTIVE_TURN_PATH.chmod(0o600)
except Exception as e:
print(f"[AUTOMATION] active-turn write failed: {type(e).__name__}: {e}")
def _automation_clear_active_turn(turn_id: str | None = None) -> None:
try:
if turn_id and AUTOMATION_ACTIVE_TURN_PATH.exists():
current = json.loads(AUTOMATION_ACTIVE_TURN_PATH.read_text())
if current.get("turn_id") != turn_id:
return
AUTOMATION_ACTIVE_TURN_PATH.unlink(missing_ok=True)
except Exception as e:
print(f"[AUTOMATION] active-turn clear failed: {type(e).__name__}: {e}")
def _automation_flow_for_thread(thread_id: str) -> dict | None:
try:
flow = json.loads(AUTOMATION_ACTIVE_FLOW_PATH.read_text())
started = datetime.fromisoformat(str(flow.get("started_at")).replace("Z", "+00:00"))
fresh = (datetime.now(timezone.utc) - started).total_seconds() <= 2 * 60 * 60
return flow if fresh and flow.get("thread_id") == thread_id and flow.get("flow") else None
except Exception:
return None
def _automation_attempt_for_flow(thread_id: str, turn_id: str, flow_name: str) -> dict | None:
try:
attempt = json.loads(AUTOMATION_ACTIVE_ATTEMPT_PATH.read_text())
started = datetime.fromisoformat(str(attempt.get("started_at")).replace("Z", "+00:00"))
fresh = (datetime.now(timezone.utc) - started).total_seconds() <= 15 * 60
valid = (
fresh
and attempt.get("thread_id") == thread_id
and attempt.get("turn_id") == turn_id
and attempt.get("flow") == flow_name
and attempt.get("attempt_id")
and attempt.get("gate")
and attempt.get("operation")
and attempt.get("evidence_tool")
and not attempt.get("observed_item_id")
)
return attempt if valid else None
except Exception:
return None
def _automation_mark_attempt_observed(attempt: dict, item_id: str) -> None:
value = {
**attempt,
"observed_item_id": item_id,
"observed_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}
temp = AUTOMATION_ACTIVE_ATTEMPT_PATH.with_suffix(f".{os.getpid()}.tmp")
temp.write_text(json.dumps(value, ensure_ascii=True))
temp.chmod(0o600)
temp.replace(AUTOMATION_ACTIVE_ATTEMPT_PATH)
AUTOMATION_ACTIVE_ATTEMPT_PATH.chmod(0o600)
def _automation_bind_flow_provider(flow: dict, provider: str) -> bool:
"""Bind the first observed provider; app-server notifications are serial."""
if flow.get("provider") and flow.get("provider") != provider:
return False
if flow.get("provider") == provider:
return True
flow = {**flow, "provider": provider,
"updated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())}
temp = AUTOMATION_ACTIVE_FLOW_PATH.with_suffix(f".{os.getpid()}.tmp")
temp.write_text(json.dumps(flow, ensure_ascii=True))
temp.chmod(0o600)
temp.replace(AUTOMATION_ACTIVE_FLOW_PATH)
AUTOMATION_ACTIVE_FLOW_PATH.chmod(0o600)
return True
def observe_automation_item(item: dict, thread_id: str, turn_id: str) -> None:
"""Persist only an MCP/command completion envelope — never args or result.
`arguments`, DOM/page text, URLs, screenshots, command strings, stdout and
raw errors may contain credentials. They are deliberately neither copied
nor summarized into this ledger.
"""
if not thread_id or not turn_id or not isinstance(item, dict):
return
item_id = str(item.get("id") or "")
key = (turn_id, item_id)
if not item_id or key in _automation_seen_items:
return
record = classify_automation_item(item, _BROWSER_PROVIDER_SERVERS)
if record is None:
return
try:
_automation_private_dir()
flow = _automation_flow_for_thread(str(thread_id))
if not flow:
return
attempt = _automation_attempt_for_flow(str(thread_id), str(turn_id), str(flow.get("flow") if flow else ""))
route = route_automation_record(
record,
str(flow.get("provider") or ""),
str(attempt.get("evidence_tool") if attempt else ""),
)
auxiliary_provider = record["provider"] == "model-blind-clipboard"
if not route["provider_allowed"]:
return
if not auxiliary_provider and not _automation_bind_flow_provider(flow, record["provider"]):
return
if not attempt or not route["evidence_eligible"]:
return
row = {
"schema_version": 2,
"thread_id": str(thread_id)[:96],
"turn_id": str(turn_id)[:96],
"item_id": item_id[:96],
"attempt_id": str(attempt["attempt_id"])[:96],
"gate": str(attempt["gate"])[:96],
"flow": str(flow["flow"])[:96],
"operation": str(attempt["operation"])[:96],
**record,
"observed_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
}
with AUTOMATION_EVIDENCE_PATH.open("a", encoding="utf-8") as fh:
fh.write(json.dumps(row, ensure_ascii=True) + "\n")
AUTOMATION_EVIDENCE_PATH.chmod(0o600)
_automation_mark_attempt_observed(attempt, item_id)
_automation_seen_items.add(key)
except Exception as e:
print(f"[AUTOMATION] evidence write failed: {type(e).__name__}: {e}")
def automation_fallback_handoff_allowed(text: str) -> bool:
"""Fail closed when fallback relay would bypass the PreToolUse receipt gate."""
try:
active = json.loads(AUTOMATION_ACTIVE_TURN_PATH.read_text())
active_flow = _automation_flow_for_thread(str(active.get("thread_id") or ""))
if not active_flow and not _AUTOMATION_HANDOFF_RE.search(text or ""):
return True
match = _AUTOMATION_RECEIPT_RE.search(text or "")
if not match:
return False
receipts_path = AUTOMATION_DIR / "handoff-receipts.jsonl"
used_path = AUTOMATION_DIR / "used-handoff-receipts.jsonl"
receipts = [json.loads(line) for line in receipts_path.read_text().splitlines() if line]
token = match.group(1)
used_marker = AUTOMATION_DIR / f"receipt-{token}.used"
receipt = next((row for row in reversed(receipts)
if row.get("token") == token), None)
expires = datetime.fromisoformat(
str(receipt.get("expires_at")).replace("Z", "+00:00")
)
now = datetime.now(timezone.utc)
valid = bool(
receipt and not used_marker.exists() and expires >= now
and receipt.get("thread_id") == active.get("thread_id")
and receipt.get("turn_id") == active.get("turn_id")
and (not active_flow or receipt.get("flow") == active_flow.get("flow"))
)
if not valid:
return False
_automation_private_dir()
try:
fd = os.open(used_marker, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
except FileExistsError:
return False
with os.fdopen(fd, "w", encoding="utf-8") as marker:
marker.write(json.dumps({"token": token, "used_at": now.isoformat()}))
with used_path.open("a", encoding="utf-8") as fh:
fh.write(json.dumps({
"schema_version": 1, "token": token,
"thread_id": active.get("thread_id"),
"turn_id": active.get("turn_id"),
"used_at": now.isoformat(),
}, ensure_ascii=True) + "\n")
used_path.chmod(0o600)
if active_flow and receipt.get("resume_required") is False:
AUTOMATION_ACTIVE_FLOW_PATH.unlink(missing_ok=True)
return True
except Exception as e:
print(f"[AUTOMATION] fallback receipt validation failed: {type(e).__name__}: {e}")
return False
def _extract_discord_message_ids(value) -> list[str]:
found: list[str] = []
sent_id_re = re.compile(r"\bsent\s*\(id:\s*(\d{15,25})\)")
def visit(node) -> None:
if isinstance(node, str):
for match in sent_id_re.finditer(node):
if match.group(1) not in found:
found.append(match.group(1))
elif isinstance(node, dict):
for child in node.values():
visit(child)
elif isinstance(node, (list, tuple)):
for child in node:
visit(child)
visit(value)
return found
def observe_discord_reply_item(item: dict, state: ReplyAckState) -> None:
if item.get("type") != "mcpToolCall":
return
if item.get("server") != "discord" or item.get("tool") != "reply":
return
error = item.get("error")
if item.get("status") != "completed" or error:
state.failures.append(str(error or f"discord.reply status={item.get('status')}"))
return
ids = _extract_discord_message_ids(item.get("result"))
if not ids:
state.failures.append("discord.reply completed without message id")
return
for message_id in ids:
if message_id not in state.message_ids:
state.message_ids.append(message_id)
def _attach_reply_ack(result: dict, state: ReplyAckState | None) -> dict:
if state is None:
return result
annotated = dict(result)
if state.message_ids:
annotated["_discord_reply_message_ids"] = list(state.message_ids)
if state.failures:
annotated["_discord_reply_failed"] = list(state.failures)
if state.message_ids and not state.failures and result.get("status") != "completed":
annotated["_bridge_status"] = EXEC_INTERRUPTED_AFTER_REPLY
return annotated
def discord_reply_ack_marker(result: dict) -> str | None:
if result.get("_discord_reply_failed"):
return None
ids = [
str(message_id)
for message_id in (result.get("_discord_reply_message_ids") or [])
if str(message_id).isdigit() and 15 <= len(str(message_id)) <= 25
]
return f"discord_reply:{','.join(ids)}" if ids else None
# YOLO is opt-in and selectable PER BOT. Opt-in signals — all OPERATOR-
# controlled and OUTSIDE the model's writable working dir (critical: a sentinel
# inside WD would let a model, fed untrusted Discord text in safe mode, write
# the file and self-upgrade safe→YOLO on the next bridge restart):
# - env THISCODEX_YOLO=1 (process-scoped, one-off launch)
# - env THISCODEX_YOLO_FILE=/abs/path (explicit operator path), else
# - default sentinel ~/.claude/channels/discord-<BOT_NAME>/.thiscodex-yolo
# (the bridge/token state dir — per-bot, and NOT the model's cwd/WD, so the
# model cannot create it). Neither present → safe default sandbox.
_yolo_file = os.environ.get("THISCODEX_YOLO_FILE")
_yolo_sentinel = Path(_yolo_file) if _yolo_file else (ENV_PATH.parent / ".thiscodex-yolo")
YOLO = (os.environ.get("THISCODEX_YOLO", "0") == "1") or _yolo_sentinel.is_file()
SANDBOX = "danger-full-access" if YOLO else "workspace-write"
APPROVAL_POLICY = "never" if YOLO else "on-request"
# B-fix: bridge-level progress heartbeat. A long turn (model working silently)
# emits a progress message to the originating channel every
# HEARTBEAT_INTERVAL_SEC, so a long/blocked task is never a silent gap even if
# the model forgets its proactive-report rule. 0 disables.
# Default MUST be < the turn timeout, or the timeout fires first and the
# heartbeat never sends. Clamp to guarantee at least one heartbeat per turn.
HEARTBEAT_INTERVAL_SEC = int(os.environ.get("THISCODEX_HEARTBEAT_SEC", "240"))
if 0 < TURN_TIMEOUT_SEC <= HEARTBEAT_INTERVAL_SEC:
HEARTBEAT_INTERVAL_SEC = max(30, int(TURN_TIMEOUT_SEC * 0.8))
def load_token() -> str:
if not ENV_PATH.exists():
raise RuntimeError(f"DISCORD_BOT_TOKEN file not found: {ENV_PATH}")
for line in ENV_PATH.read_text().splitlines():
if line.startswith("DISCORD_BOT_TOKEN="):
return line.split("=", 1)[1].strip().strip('"').strip("'")
raise RuntimeError(f"no DISCORD_BOT_TOKEN= entry in {ENV_PATH}")
def load_dedup() -> dict:
if not DEDUP_PATH.exists():
return {}
try:
return json.loads(DEDUP_PATH.read_text() or "{}")
except json.JSONDecodeError:
return {}
def save_dedup(state: dict) -> None:
if len(state) > MAX_DEDUP_ENTRIES:
oldest = sorted(state.items(), key=lambda kv: kv[1].get("ts", 0))[: len(state) - MAX_DEDUP_ENTRIES]
for k, _ in oldest:
state.pop(k, None)
DEDUP_PATH.write_text(json.dumps(state, ensure_ascii=False, indent=2))
class CodexRPC:
"""Minimal JSON-RPC client over WebSocket for the codex app-server v2 protocol."""
def __init__(self, url: str):
self.url = url
self.ws = None
self.next_id = 1
self.pending: dict[int, asyncio.Future] = {}
self.turn_done: dict[str, asyncio.Future] = {}
self.reply_ack: dict[str, ReplyAckState] = {}
self.reader_task = None
self._lock = asyncio.Lock()
async def connect(self) -> None:
print(f"[CODEX-RPC] connecting to {self.url} ...")
self.ws = await websockets.connect(self.url, max_size=None)
self.reader_task = asyncio.create_task(self._reader())
await self.call("initialize", {
"clientInfo": {"name": "thiscodex_bridge", "version": "0.1.0"},
"capabilities": {"experimentalApi": True},
})
# initialized notification (no id)
await self.ws.send(json.dumps({"method": "initialized"}))
print("[CODEX-RPC] initialized")
async def _reader(self) -> None:
try:
async for raw in self.ws:
# DEBUG: log every incoming frame (truncated)
print(f"[WS-IN] {str(raw)[:400]}")
try:
msg = json.loads(raw)
except json.JSONDecodeError:
print(f"[CODEX-RPC] non-JSON frame: {str(raw)[:200]}")
continue
if "id" in msg and ("result" in msg or "error" in msg):
fut = self.pending.pop(msg["id"], None)
if fut and not fut.done():
fut.set_result(msg)
continue
method = msg.get("method", "")
# Server-initiated JSON-RPC request — the client MUST respond, or
# the codex turn hangs forever (turn/start then codex asks the
# client for approval / elicitation / a tool call; ignoring it
# never completes the turn).
if "id" in msg and method:
print(f"[WS-SERVER-REQ] method={method} id={msg['id']}")
if method in ("item/commandExecution/requestApproval", "item/fileChange/requestApproval"):
# In YOLO the model should not be asked (approvalPolicy
# never); if it is, default-deny is the safe answer.
resp = {"id": msg["id"], "result": {"decision": "cancel"}}
elif method == "mcpServer/elicitation/request":
# MCP tool-call approval — codex asks the client right
# before calling e.g. mcp__discord__reply. accept +
# persist:"session" auto-approves for this session.
params = msg.get("params", {})
kind = (params.get("_meta") or {}).get("codex_approval_kind", "")
print(f"[ELICIT-ACCEPT] server={params.get('serverName')} kind={kind}")
resp = {"id": msg["id"], "result": {"action": "accept", "content": {}, "_meta": {"persist": "session"}}}
elif method == "item/tool/requestUserInput":
# Stateful Discord question shim — handled off the
# reader loop so a long human wait does not block
# other codex WS frames.
asyncio.create_task(self._handle_request_user_input(msg))
continue
elif method == "item/tool/call":
params = msg.get("params", {})
resp = {"id": msg["id"], "result": {
"success": False,
"contentItems": [{"type": "inputText", "text": f"Client-side tool not implemented: {params.get('tool', '?')}"}],
}}
elif method == "item/permissions/requestApproval":
resp = {"id": msg["id"], "result": {"decision": "cancel"}}
else:
resp = {"id": msg["id"], "error": {"code": -32601, "message": f"Unhandled server request: {method}"}}
await self.ws.send(json.dumps(resp))
continue
# any "completed"-flavored turn notification resolves the future
if method == "turn/completed" or method.endswith("/turn/completed") or "TurnCompleted" in method:
turn = msg.get("params", {}).get("turn") or msg.get("params", {})
tid = turn.get("id") if isinstance(turn, dict) else None
if tid:
turn = _attach_reply_ack(turn, self.reply_ack.pop(tid, None))
fut = self.turn_done.pop(tid, None)
if fut and not fut.done():
fut.set_result(turn)
else:
# fallback: resolve oldest pending turn
if self.turn_done:
key, fut = next(iter(self.turn_done.items()))
self.turn_done.pop(key, None)
if not fut.done():
fut.set_result(turn)
elif method == "item/completed":
params = msg.get("params", {})
thread_id = params.get("threadId")
tid = params.get("turnId")
item = params.get("item")
if tid and isinstance(item, dict):
observe_discord_reply_item(
item,
self.reply_ack.setdefault(tid, ReplyAckState()),
)
observe_automation_item(item, thread_id, tid)
elif method.startswith("item/agentMessage"):
# stream notification — log only
pass
# async-for ended without an exception = server closed the WS
# gracefully (ConnectionClosedOK is swallowed by the iterator).
# Still a dead bridge — same fail-fast restart path.
self._ws_fatal("server closed connection (graceful)")
except websockets.ConnectionClosed as e:
print(f"[CODEX-RPC] connection closed: {e}")
self._ws_fatal(f"connection closed: {e}")
except Exception as e:
print(f"[CODEX-RPC] reader error: {type(e).__name__}: {e}")
self._ws_fatal(f"reader error: {type(e).__name__}: {e}")
def _ws_fatal(self, reason: str) -> None:
"""App-server WS is gone — exit fast so the supervised launcher restarts us.
Without this, the bridge becomes a zombie: the reader is dead, every
subsequent turn fails with ConnectionClosed, Discord messages pile up,
and nothing restarts because the process itself never exits. The
supervised loop in scripts/launch.sh (window 0 `while true; do
$LAUNCH_CMD ...`) restarts the infra pair (app-server + bridge) within
~5s of our exit; thread continuity survives via .codex-thread-id +
thread/resume. Exit code 17 marks a deliberate WS-loss restart."""
print(f"[CODEX-RPC][FATAL] app-server websocket lost ({reason}) — "
f"exiting so the supervised launcher restarts the bridge (exit 17)",
flush=True)
async def _notify_and_exit() -> None:
chan = _active_origin.get("channel")
if chan is not None:
with contextlib.suppress(Exception):
await asyncio.wait_for(
chan.send("⚠️ codex app-server connection lost — bridge is "
"restarting (supervised). If no reply lands in ~1 min, "
"resend your last message.",
allowed_mentions=_NO_MENTIONS),
timeout=5,
)
os._exit(17)
try:
asyncio.get_running_loop().create_task(_notify_and_exit())
except RuntimeError:
os._exit(17)
async def call(self, method: str, params: dict | None = None) -> dict:
async with self._lock:
rid = self.next_id
self.next_id += 1
fut = asyncio.get_running_loop().create_future()
self.pending[rid] = fut
req: dict = {"id": rid, "method": method}
if params is not None:
req["params"] = params
await self.ws.send(json.dumps(req))
try:
msg = await asyncio.wait_for(fut, timeout=60)
except asyncio.TimeoutError:
self.pending.pop(rid, None)
raise RuntimeError(f"timeout: {method}")
if "error" in msg:
raise RuntimeError(f"{method} failed: {msg['error']}")
return msg.get("result", {})
async def ensure_thread(self) -> str:
if THREAD_ID_PATH.exists():
tid = THREAD_ID_PATH.read_text().strip()
try:
# CONTRACT (do NOT drop): thread/resume MUST re-send sandbox +
# approvalPolicy. If omitted, the resumed thread silently falls
# back to the server default (workspace-write /
# networkAccess:false) — so YOLO would apply on the very first
# turn and then never again after the first resume. This is the
# single nastiest bug; see docs/yolo-bridge-contract.md.
await self.call("thread/resume", {
"threadId": tid,
"cwd": str(WD),
"sandbox": SANDBOX,
"approvalPolicy": APPROVAL_POLICY,
})
print(f"[CODEX-RPC] resumed thread {tid} (sandbox={SANDBOX})")
return tid
except Exception as e:
print(f"[CODEX-RPC] resume failed ({e}) — starting new thread")
res = await self.call("thread/start", {
"cwd": str(WD),
"sandbox": SANDBOX,
"approvalPolicy": APPROVAL_POLICY,
"threadSource": "user",
})
# spec uncertainty — try multiple shapes
tid = None
if isinstance(res, dict):
tid = res.get("threadId") or (res.get("thread") or {}).get("id")
if not tid:
raise RuntimeError(f"thread/start did not return id: {res}")
await self.materialize_thread_for_tui(tid)
THREAD_ID_PATH.write_text(tid)
print(f"[CODEX-RPC] started new thread {tid} (sandbox={SANDBOX})")
return tid
async def materialize_thread_for_tui(self, thread_id: str) -> None:
"""Force local rollout creation so `codex resume --remote` can attach immediately."""
marker = {
"type": "message",
"role": "assistant",
"content": [{
"type": "output_text",
"text": "[bridge:init] rollout materialized for local TUI attach. No action required.",
}],
"phase": None,
}
await self.call("thread/inject_items", {
"threadId": thread_id,
"items": [marker],
})
print(f"[CODEX-RPC] materialized thread rollout for TUI attach {thread_id}")
async def _try_reconcile_turn(self, thread_id: str, turn_id: str, fut: asyncio.Future) -> None:
"""Best-effort poll for a turn that outlived the client soft timeout.
The normal path is `turn/completed` notification. Polling is only a
backup: if this app-server version lacks `thread/read` shape support,
the preserved future still lets a late notification reconcile the turn.
"""
if fut.done():
return
try:
res = await self.call("thread/read", {"threadId": thread_id})
except Exception as e:
print(f"[CODEX-RPC] turn reconcile poll skipped: {type(e).__name__}: {e}")
return
payload = res.get("result", res) if isinstance(res, dict) else {}
thread = payload.get("thread", payload) if isinstance(payload, dict) else {}
turns = []
if isinstance(thread, dict):
turns = thread.get("turns") or thread.get("items") or []
if isinstance(payload, dict) and not turns:
turns = payload.get("turns") or []
for turn in (turns if isinstance(turns, list) else []):
if not isinstance(turn, dict) or turn.get("id") != turn_id:
continue
status = turn.get("status")
if status and status not in ("inProgress", "running"):
self.turn_done.pop(turn_id, None)
if not fut.done():
# Reconciled turns bypass the item/completed notifications, so
# the ack state never saw this turn's reply calls — replay the
# fetched items into it, or downstream consumers read the same
# items for text but not for acks and double-send.
state = self.reply_ack.pop(turn_id, None) or ReplyAckState()
for item in (turn.get("items") or []):
if isinstance(item, dict):
observe_discord_reply_item(item, state)
observe_automation_item(item, thread_id, turn_id)
fut.set_result(_attach_reply_ack(turn, state))
print(f"[CODEX-RPC] reconciled late turn via thread/read: {turn_id} status={status}")
return
async def send_turn(self, thread_id: str, text: str) -> dict:
res = await self.call("turn/start", {
"threadId": thread_id,
"input": [{"type": "text", "text": text}],
})
print(f"[CODEX-RPC] turn/start response = {json.dumps(res)[:400]}")
turn = res if isinstance(res, dict) else {}
turn_id = turn.get("turnId") or (turn.get("turn") or {}).get("id")
print(f"[CODEX-RPC] extracted turn_id = {turn_id}")
if turn_id:
# crash-recovery marker: record the live turn id so a mid-turn
# bridge restart can reconcile this exact turn via thread/read.
_inflight_update({"turn_id": turn_id})
_automation_write_active_turn(thread_id, turn_id)
if not turn_id:
return turn
fut = asyncio.get_running_loop().create_future()
self.turn_done[turn_id] = fut
started = time.monotonic()
soft_reported = False
while True:
elapsed = time.monotonic() - started
if TURN_HARD_TIMEOUT_SEC > 0 and elapsed >= TURN_HARD_TIMEOUT_SEC:
self.turn_done.pop(turn_id, None)
print(f"[CODEX-RPC] turn hard timeout: {turn_id}")
return _attach_reply_ack(
{"status": "hard_timeout", "turnId": turn_id},
self.reply_ack.pop(turn_id, None),
)
wait_for = EFFECTIVE_TURN_TIMEOUT_SEC if not soft_reported else TURN_RECONCILE_INTERVAL_SEC
if TURN_HARD_TIMEOUT_SEC > 0:
wait_for = min(wait_for, max(1, int(TURN_HARD_TIMEOUT_SEC - elapsed)))
try:
# Shield is critical: wait_for timeout must not cancel the
# future, because a late turn/completed notification is still
# the canonical reconciliation path.
return await asyncio.wait_for(asyncio.shield(fut), timeout=wait_for)
except asyncio.TimeoutError:
if not soft_reported:
soft_reported = True
print(f"[CODEX-RPC] turn exceeded soft timeout; preserving future for late completion: {turn_id}")
await self._try_reconcile_turn(thread_id, turn_id, fut)
if fut.done():
return fut.result()
# ── AskUserQuestion shim for app-server requestUserInput ──
async def _handle_request_user_input(self, msg: dict) -> None:
params = msg.get("params", {}) or {}
# Spec uncertainty: the public codex docs do not pin requestUserInput's
# param shape. Parse defensively. KM `AskUserQuestion` sends a
# `questions[]` batch (full/bootstrap multi-question); legacy single
# {prompt,choices} is still accepted (→ 1-element, key "value").
mid = msg.get("id")
# BLOCKER: parse/send/wait may all raise; an unanswered JSON-RPC id
# hangs the Codex request forever. Any failure → a guaranteed
# {blocked:true, error:<type>} response (independent-review BLOCKER).
try:
specs = _normalize_questions(params)
answers = await self._ask_via_discord(specs)
if answers is not None:
# answers is a map keyed by each question's stable key
result = {"answers": answers}
else:
# condition 4: no answer + no default → blocked, never silently ok
result = {"answers": {}, "blocked": True}
except Exception as e:
print(f"[QA] handler error: {type(e).__name__}: {e}")
_audit("question_handler_error", {"id": mid, "error": type(e).__name__})
result = {"answers": {}, "blocked": True, "error": type(e).__name__}
# The JSON-RPC reply itself can fail (dead ws). Bounded best-effort
# retry absorbs transient backpressure; a persistently dead ws is a
# genuine transport failure no layer here can paper over — the
# documented backstop is then EFFECTIVE_TURN_TIMEOUT_SEC on the Codex
# side plus the bridge reconnect loop (independent-review BLOCKER:
# honest limitation, not a silent hang). A request with no id is a
# notification → nothing to answer; never emit an id:null reply.
if mid is None:
print("[QA] requestUserInput had no JSON-RPC id; nothing to answer")
return
for attempt in range(3):
try:
await self.ws.send(json.dumps({"id": mid, "result": result}))
break
except Exception as e:
print(f"[QA] response send failed (attempt {attempt + 1}/3): "
f"{type(e).__name__}: {e}")
if attempt < 2:
await asyncio.sleep(0.5 * (attempt + 1))
else:
_audit("question_response_undeliverable", {"id": mid})
print("[QA] response undeliverable — ws transport dead; backstop = "
"turn timeout + bridge reconnect")
async def _ask_via_discord(self, specs: list[dict]):
# Snapshot the origin PER QUESTION at ask time — never read the shared
# mutable _active_origin at reply time (worker.finally clears it; a
# late reply would then pass the channel check with chat_id=None and
# bypass condition 3 — independent-review BLOCKER). Each pending
# question carries its own immutable origin.
ch = _active_origin.get("channel")
origin_user = _active_origin.get("user_id")
origin_chat = _active_origin.get("chat_id")
qid = uuid.uuid4().hex # condition 1: bridge UUID
all_default = {s["key"]: s["default"] for s in specs
if s.get("default") is not None}
defaults_map = (all_default
if len(all_default) == len(specs) and specs else None)
if ch is None:
_audit("question_no_origin", {"qid": qid, "n": len(specs)})
return defaults_map
# condition 2: bridge renders the fixed choice set; replies are matched
# by number, free-form only where a question allows it.
lines = [f"❓ please answer the following — question {qid[:8]}, "
f"one line per question (reply within {QUESTION_TIMEOUT_SEC}s):"]
for i, s in enumerate(specs, 1):
lines.append(f"Q{i} · {s['header']}: {s['question']}")
for j, o in enumerate(s["options"], 1):
lines.append(f" {j}. {o}")
lines.append("(one line per question in order — a number for a choice, "
"`1,3` for multi-select, or free text where allowed; you "
"may prefix `Q2: …`. defaults apply on timeout where set.)")
fut = asyncio.get_running_loop().create_future()
_pending_q[qid] = fut
_q_meta[qid] = {"origin_user_id": origin_user, "origin_chat_id": origin_chat,
"specs": specs, "acc": {}, "defaults_map": defaults_map}
_audit("question_asked", {"qid": qid, "origin_user_id": origin_user,
"keys": [s["key"] for s in specs],
"questions": [s["question"][:200] for s in specs]})
rem = None
try:
try:
await ch.send("\n".join(lines), allowed_mentions=_NO_MENTIONS)
except Exception as e:
# BLOCKER: the question never reached the user. Don't wait out
# the full timeout on a dead question — fail fast so the
# handler returns blocked+error immediately.
print(f"[QA] question send failed: {type(e).__name__}: {e}")
_audit("question_send_failed", {"qid": qid, "error": type(e).__name__})
raise
async def _remind():
# MINOR: a long human wait should not look like a hung bot.
await asyncio.sleep(max(30, QUESTION_TIMEOUT_SEC // 2))
with contextlib.suppress(Exception):
await ch.send(f"⏳ question {qid[:8]} still open — "
+ ("defaults apply soon" if defaults_map is not None
else "will block soon if unanswered"),
allowed_mentions=_NO_MENTIONS)
rem = asyncio.create_task(_remind())
try:
ans = await asyncio.wait_for(fut, timeout=QUESTION_TIMEOUT_SEC)
_audit("question_answered", {"qid": qid, "answers": str(ans)[:500]})
return ans
except asyncio.TimeoutError:
_audit("question_timeout", {"qid": qid,
"had_defaults": defaults_map is not None})
# condition 4: apply defaults only if EVERY question has one;
# otherwise blocked, never a silent partial ok.
if defaults_map is not None:
return defaults_map
with contextlib.suppress(Exception):
await ch.send(f"⚠️ question {qid[:8]} timed out — not every "
f"question had a default — task blocked "
f"(bridge fail-safe).",
allowed_mentions=_NO_MENTIONS)
return None
finally:
if rem is not None:
rem.cancel()
with contextlib.suppress(asyncio.CancelledError):
await rem
_pending_q.pop(qid, None)
_q_meta.pop(qid, None)
TOKEN = load_token()
codex = CodexRPC(CODEX_WS)
queue: asyncio.Queue = asyncio.Queue()
thread_id: str | None = None
# ── AskUserQuestion shim state for app-server requestUserInput ──
# A skill calling the AskUserQuestion equivalent surfaces as a codex
# `item/tool/requestUserInput`. The bridge turns it into a stateful Discord
# question and returns the matched answer. The 6 security conditions are
# enforced here; an unenforced condition = a prompt-injection hole.
QUESTION_TIMEOUT_SEC = int(os.environ.get("THISCODEX_QUESTION_TIMEOUT", "300"))
# Multi-question policy (independent-review BLOCKER): KM `AskUserQuestion`
# sends `questions[]` (full Mode-R = 3, STEP-1 = 4, bootstrap = 2-call).
# The shim renders the WHOLE batch in ONE Discord message and accepts
# answers over one shared QUESTION_TIMEOUT budget (incrementally mergeable)
# — NOT N sequential per-question timeouts — so the turn-timeout invariant
# stays single-budget: turn ≥ max(turn, question + post-answer buffer).
QA_POST_ANSWER_BUFFER_SEC = int(os.environ.get("THISCODEX_QA_POST_ANSWER_BUFFER", "180"))
EFFECTIVE_TURN_TIMEOUT_SEC = max(TURN_TIMEOUT_SEC,
QUESTION_TIMEOUT_SEC + QA_POST_ANSWER_BUFFER_SEC)
# Soft timeout no longer means "failed": it is the first reconciliation beat.
# The bridge keeps the active future so late `turn/completed` events still
# close the original Discord request and naturally backpressure later turns.
TURN_RECONCILE_INTERVAL_SEC = int(os.environ.get("THISCODEX_TURN_RECONCILE_SEC", "30"))
TURN_HARD_TIMEOUT_SEC = int(os.environ.get("THISCODEX_TURN_HARD_TIMEOUT", "0"))
# Question/reminder/timeout prompts are skill-supplied text echoed to Discord.
# Treat them as data — never let an embedded @everyone/@user become a real
# ping (independent-review P1). All shim sends pass this.
_NO_MENTIONS = discord.AllowedMentions.none()
# Forensic audit must NOT live in the model's writable WD: in safe mode the
# model cwd/WD is writable, so a model fed untrusted Discord text could
# tamper with its own audit trail. Same principle as the YOLO sentinel —
# keep it in the operator-controlled bridge state dir, outside the model's
# reach (independent-review P1). Overridable via env for ops.
AUDIT_PATH = Path(os.environ.get("THISCODEX_QA_AUDIT_FILE",
str(ENV_PATH.parent / ".thiscodex-qa-audit.jsonl")))
# Mid-turn crash recovery marker: while a turn is in flight, its origin and
# turn id live here so a bridge restart (exit-17 WS-loss restart or a hard
# crash) can reconcile the orphaned turn via thread/read instead of dropping
# the originating Discord request silently. thread/resume restores codex-side
# context, but the bridge-side future dies with the process — this marker is
# the only record that a reply was still owed. Lives in the operator-
# controlled bridge state dir (same tamper-isolation principle as AUDIT_PATH).
INFLIGHT_PATH = Path(os.environ.get("THISCODEX_INFLIGHT_FILE",
str(ENV_PATH.parent / ".thiscodex-inflight-turn.json")))
def _inflight_write(data: dict) -> None:
try:
INFLIGHT_PATH.write_text(json.dumps(data, ensure_ascii=False))
except Exception as e:
print(f"[INFLIGHT] write failed (fail-open): {type(e).__name__}: {e}")
def _inflight_update(extra: dict) -> None:
try:
cur = json.loads(INFLIGHT_PATH.read_text())
except Exception:
cur = {}
cur.update(extra)
_inflight_write(cur)
def _inflight_clear() -> None:
try:
INFLIGHT_PATH.unlink(missing_ok=True)
except Exception as e:
print(f"[INFLIGHT] clear failed: {type(e).__name__}: {e}")
async def _recover_inflight() -> None:
"""Reconcile a turn orphaned by a mid-turn bridge restart.
Best-effort and fail-open. Marker is consumed up front so recovery can
never loop a restart cycle. Outcomes: turn finished → recovered reply to
origin; otherwise → explicit loss notice to origin (never a silent drop).
"""
try:
raw = INFLIGHT_PATH.read_text()
except FileNotFoundError:
return
except Exception as e:
print(f"[INFLIGHT] read failed: {type(e).__name__}: {e}")
return
_inflight_clear()
try:
mark = json.loads(raw)
except Exception:
print("[INFLIGHT] marker unparsable — dropped")