-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcodex_session_start.py
More file actions
125 lines (104 loc) · 3.65 KB
/
Copy pathcodex_session_start.py
File metadata and controls
125 lines (104 loc) · 3.65 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
#!/usr/bin/env python3
"""Codex SessionStart hook adapter for lossless-code."""
import json
import os
import sys
from typing import TextIO
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import db
import codex_tail_import
import inject_context
SUPPORTED_SOURCES = {"startup", "resume", "clear"}
AGENT_SOURCE = "codex-cli"
RESERVED_MARKER_PREFIX = "[lcc."
def _warn(stderr: TextIO, message: str) -> None:
print(f"[lossless-code] Codex SessionStart: {message}", file=stderr)
def _read_payload(stdin: TextIO, stderr: TextIO) -> dict | None:
raw = stdin.read()
if not raw.strip():
_warn(stderr, "no JSON payload received")
return None
try:
payload = json.loads(raw)
except json.JSONDecodeError:
_warn(stderr, "invalid JSON payload")
return None
if not isinstance(payload, dict):
_warn(stderr, "payload must be a JSON object")
return None
return payload
def _has_unsafe_context_marker(value: str) -> bool:
return "\n" in value or "\r" in value or RESERVED_MARKER_PREFIX in value
def build_hook_output(payload: dict, stderr: TextIO = sys.stderr) -> dict | None:
"""Return Codex hook JSON for a SessionStart payload, or None for no output."""
if payload.get("hook_event_name") != "SessionStart":
return None
source = payload.get("source")
if source not in SUPPORTED_SOURCES:
_warn(stderr, "unsupported source")
return None
session_id = payload.get("session_id")
if not isinstance(session_id, str) or not session_id.strip():
_warn(stderr, "missing session_id")
return None
session_id = session_id.strip()
if _has_unsafe_context_marker(session_id):
_warn(stderr, "unsafe session_id")
return None
cwd = payload.get("cwd")
working_dir = cwd if isinstance(cwd, str) else ""
if _has_unsafe_context_marker(working_dir):
_warn(stderr, "unsafe cwd omitted")
working_dir = ""
cfg = db.load_config()
if db.matches_any_pattern(session_id, cfg.get("ignoreSessionPatterns", [])):
return None
stateless = db.matches_any_pattern(
session_id,
cfg.get("statelessSessionPatterns", []),
)
db.ensure_session(
session_id,
working_dir,
stateless=stateless,
agent_source=AGENT_SOURCE,
)
if codex_tail_import.is_project_opted_in(working_dir, cfg):
try:
import_result = codex_tail_import.refresh_imported_task_state(
working_dir=working_dir,
current_session_id=session_id,
config=cfg,
)
warning = import_result.get("warning")
if import_result.get("status") == "partial" and warning:
_warn(stderr, f"tail import partial: {warning}")
except Exception:
_warn(stderr, "tail import failed")
context = inject_context.build_context(
session_id=session_id,
working_dir=working_dir,
agent_source=AGENT_SOURCE,
)
if not context:
return None
return {
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"additionalContext": context,
}
}
def main(stdin: TextIO = sys.stdin, stdout: TextIO = sys.stdout, stderr: TextIO = sys.stderr) -> int:
payload = _read_payload(stdin, stderr)
if payload is None:
return 0
try:
output = build_hook_output(payload, stderr=stderr)
except Exception:
_warn(stderr, "failed to build context")
return 0
if output is not None:
print(json.dumps(output), file=stdout)
return 0
if __name__ == "__main__":
raise SystemExit(main())