anosys-claude-code 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- anosys_claude_code-0.2.1.dist-info/METADATA +21 -0
- anosys_claude_code-0.2.1.dist-info/RECORD +11 -0
- anosys_claude_code-0.2.1.dist-info/WHEEL +5 -0
- anosys_claude_code-0.2.1.dist-info/entry_points.txt +2 -0
- anosys_claude_code-0.2.1.dist-info/top_level.txt +1 -0
- anosys_sdk_claude_code/__init__.py +9 -0
- anosys_sdk_claude_code/cli.py +151 -0
- anosys_sdk_claude_code/hook_runner.py +421 -0
- anosys_sdk_claude_code/installer.py +162 -0
- anosys_sdk_claude_code/mapper.py +640 -0
- anosys_sdk_claude_code/py.typed +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: anosys-claude-code
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: AnoSys observability hook for Claude Code — automatic audit logging of Claude Code sessions
|
|
5
|
+
Author-email: AnoSys <support@anosys.ai>
|
|
6
|
+
Project-URL: Homepage, https://anosys.ai
|
|
7
|
+
Project-URL: Documentation, https://docs.anosys.ai
|
|
8
|
+
Project-URL: Repository, https://github.com/anosys-ai/anosys-sdk
|
|
9
|
+
Keywords: anosys,claude,claude-code,observability,monitoring,audit,llm
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Classifier: Topic :: System :: Monitoring
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
Requires-Dist: requests>=2.25.1
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
anosys_sdk_claude_code/__init__.py,sha256=LtZcwNC3RuCqt2SthxC2Y0X03fA7E-e89-BUj_EugE0,312
|
|
2
|
+
anosys_sdk_claude_code/cli.py,sha256=nOvrBGe9959R2fcOnik4WmJflU949zV58E19ja9uanU,4939
|
|
3
|
+
anosys_sdk_claude_code/hook_runner.py,sha256=utmDnVKyd_vaZj0EhhfXppoaqsmMAFYcouxcVkTIMz0,15355
|
|
4
|
+
anosys_sdk_claude_code/installer.py,sha256=vOaXHlH81kSIOeK8vDF9Qvg1d_9l-TaW4GRVPe-SVII,4636
|
|
5
|
+
anosys_sdk_claude_code/mapper.py,sha256=p9oEaOeFif_038PjI-deFw8YUbi8PYrIqs10pulh6zg,35673
|
|
6
|
+
anosys_sdk_claude_code/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
anosys_claude_code-0.2.1.dist-info/METADATA,sha256=czUD2blctS7_eI6kxRUFcHMhDF4V3R8cJh4guHfxjOQ,996
|
|
8
|
+
anosys_claude_code-0.2.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
9
|
+
anosys_claude_code-0.2.1.dist-info/entry_points.txt,sha256=PX5Y1PoxfqCssD1CwhVucTVDFBaCMH8JcCbZnnz1D0o,71
|
|
10
|
+
anosys_claude_code-0.2.1.dist-info/top_level.txt,sha256=36nS5TutPn4wYzWSh4zyG3BSv3KQzJDoyb07pD-t3YI,23
|
|
11
|
+
anosys_claude_code-0.2.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
anosys_sdk_claude_code
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AnoSys Claude Code integration — observability hook for Claude Code sessions.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from anosys_sdk_claude_code.installer import update_env, update_stop_hooks
|
|
6
|
+
from anosys_sdk_claude_code.mapper import transform_record
|
|
7
|
+
|
|
8
|
+
__version__ = "0.2.1"
|
|
9
|
+
__all__ = ["transform_record", "update_env", "update_stop_hooks"]
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CLI entry point for anosys-claude-code.
|
|
3
|
+
|
|
4
|
+
Commands:
|
|
5
|
+
install Register the AnoSys Stop hook in ~/.claude/settings.json
|
|
6
|
+
uninstall Remove the AnoSys Stop hook from ~/.claude/settings.json
|
|
7
|
+
status Show current hook registration status
|
|
8
|
+
run Execute the hook (invoked by Claude Code on every Stop event)
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import argparse
|
|
12
|
+
import asyncio
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
from anosys_sdk_claude_code.installer import (
|
|
16
|
+
BACKUP_PATH,
|
|
17
|
+
SETTINGS_PATH,
|
|
18
|
+
backup,
|
|
19
|
+
get_anosys_hook_command,
|
|
20
|
+
has_anosys_hook,
|
|
21
|
+
load_settings,
|
|
22
|
+
remove_env,
|
|
23
|
+
remove_stop_hooks,
|
|
24
|
+
update_env,
|
|
25
|
+
update_stop_hooks,
|
|
26
|
+
write_atomic,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
HOOK_COMMAND = "anosys-claude-code run"
|
|
30
|
+
DEFAULT_ENDPOINT = "https://api.anosys.ai"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _prompt(prompt: str, default: str = "") -> str:
|
|
34
|
+
try:
|
|
35
|
+
val = input(prompt).strip()
|
|
36
|
+
return val if val else default
|
|
37
|
+
except (EOFError, KeyboardInterrupt):
|
|
38
|
+
print()
|
|
39
|
+
sys.exit(0)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def cmd_install(args: argparse.Namespace) -> None:
|
|
43
|
+
print("\nAnoSys Claude Code Hook Installer")
|
|
44
|
+
print("=" * 40)
|
|
45
|
+
|
|
46
|
+
api_key = args.api_key
|
|
47
|
+
if not api_key:
|
|
48
|
+
api_key = _prompt(
|
|
49
|
+
"AnoSys API key (leave blank to skip, can set ANOSYS_HOOK_API_KEY later): "
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
endpoint = args.endpoint or _prompt(
|
|
53
|
+
f"AnoSys endpoint URL [{DEFAULT_ENDPOINT}]: ", DEFAULT_ENDPOINT
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
redaction = args.redaction
|
|
57
|
+
if not redaction and not args.no_redaction:
|
|
58
|
+
choice = _prompt("Enable content redaction? (y/N): ", "n").lower()
|
|
59
|
+
redaction = choice == 'y'
|
|
60
|
+
|
|
61
|
+
new_env: dict = {
|
|
62
|
+
"ANOSYS_HOOK_ENDPOINT_URL": endpoint,
|
|
63
|
+
}
|
|
64
|
+
if api_key:
|
|
65
|
+
new_env["ANOSYS_HOOK_API_KEY"] = api_key
|
|
66
|
+
if redaction:
|
|
67
|
+
new_env["REDACTION"] = "true"
|
|
68
|
+
|
|
69
|
+
print(f"\nUpdating {SETTINGS_PATH} ...")
|
|
70
|
+
settings = load_settings()
|
|
71
|
+
backup()
|
|
72
|
+
settings = update_env(settings, new_env)
|
|
73
|
+
settings = update_stop_hooks(settings, HOOK_COMMAND)
|
|
74
|
+
write_atomic(SETTINGS_PATH, settings)
|
|
75
|
+
|
|
76
|
+
print(f" Backed up original settings -> {BACKUP_PATH}")
|
|
77
|
+
print(f" Hook command registered: {HOOK_COMMAND}")
|
|
78
|
+
print(f" Endpoint: {endpoint}")
|
|
79
|
+
if api_key:
|
|
80
|
+
print(f" API key: {'*' * (len(api_key) - 4)}{api_key[-4:]}")
|
|
81
|
+
print(f" Redaction: {'enabled' if redaction else 'disabled'}")
|
|
82
|
+
print("\nDone. The hook will fire automatically after each Claude Code session.")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def cmd_uninstall(args: argparse.Namespace) -> None:
|
|
86
|
+
print(f"\nRemoving AnoSys hook from {SETTINGS_PATH} ...")
|
|
87
|
+
settings = load_settings()
|
|
88
|
+
if not has_anosys_hook(settings):
|
|
89
|
+
print(" No AnoSys hook found — nothing to remove.")
|
|
90
|
+
return
|
|
91
|
+
backup()
|
|
92
|
+
settings = remove_stop_hooks(settings)
|
|
93
|
+
settings = remove_env(settings)
|
|
94
|
+
write_atomic(SETTINGS_PATH, settings)
|
|
95
|
+
print(f" Backed up original settings -> {BACKUP_PATH}")
|
|
96
|
+
print(" AnoSys hook removed successfully.")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def cmd_status(args: argparse.Namespace) -> None:
|
|
100
|
+
settings = load_settings()
|
|
101
|
+
if has_anosys_hook(settings):
|
|
102
|
+
cmd = get_anosys_hook_command(settings)
|
|
103
|
+
print("AnoSys hook is INSTALLED")
|
|
104
|
+
print(f" Command: {cmd}")
|
|
105
|
+
env = settings.get("env", {})
|
|
106
|
+
endpoint = env.get("ANOSYS_HOOK_ENDPOINT_URL", "(not set)")
|
|
107
|
+
has_key = "ANOSYS_HOOK_API_KEY" in env
|
|
108
|
+
redaction = env.get("REDACTION", "false")
|
|
109
|
+
print(f" Endpoint: {endpoint}")
|
|
110
|
+
print(f" API key: {'set' if has_key else 'not set'}")
|
|
111
|
+
print(f" Redaction: {redaction}")
|
|
112
|
+
else:
|
|
113
|
+
print("AnoSys hook is NOT installed.")
|
|
114
|
+
print(" Run 'anosys-claude-code install' to set it up.")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def cmd_run(args: argparse.Namespace) -> None:
|
|
118
|
+
from anosys_sdk_claude_code.hook_runner import main
|
|
119
|
+
asyncio.run(main())
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def main() -> None:
|
|
123
|
+
parser = argparse.ArgumentParser(
|
|
124
|
+
prog="anosys-claude-code",
|
|
125
|
+
description="AnoSys observability hook for Claude Code"
|
|
126
|
+
)
|
|
127
|
+
sub = parser.add_subparsers(dest="command", metavar="COMMAND")
|
|
128
|
+
sub.required = True
|
|
129
|
+
|
|
130
|
+
p_install = sub.add_parser("install", help="Register the AnoSys Stop hook")
|
|
131
|
+
p_install.add_argument("--api-key", metavar="KEY", help="AnoSys API key")
|
|
132
|
+
p_install.add_argument("--endpoint", metavar="URL", help=f"Endpoint URL (default: {DEFAULT_ENDPOINT})")
|
|
133
|
+
p_install.add_argument("--redaction", action="store_true", help="Enable content redaction")
|
|
134
|
+
p_install.add_argument("--no-redaction", action="store_true", help="Skip redaction prompt")
|
|
135
|
+
p_install.set_defaults(func=cmd_install)
|
|
136
|
+
|
|
137
|
+
p_uninstall = sub.add_parser("uninstall", help="Remove the AnoSys Stop hook")
|
|
138
|
+
p_uninstall.set_defaults(func=cmd_uninstall)
|
|
139
|
+
|
|
140
|
+
p_status = sub.add_parser("status", help="Show hook registration status")
|
|
141
|
+
p_status.set_defaults(func=cmd_status)
|
|
142
|
+
|
|
143
|
+
p_run = sub.add_parser("run", help="Execute the hook (called by Claude Code)")
|
|
144
|
+
p_run.set_defaults(func=cmd_run)
|
|
145
|
+
|
|
146
|
+
args = parser.parse_args()
|
|
147
|
+
args.func(args)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
if __name__ == "__main__":
|
|
151
|
+
main()
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Claude Code "Stop" hook runner.
|
|
3
|
+
|
|
4
|
+
Reads the latest JSONL transcript, incrementally processes new messages,
|
|
5
|
+
maps them through mapper.transform_record(), and POSTs them as a batch
|
|
6
|
+
to the configured endpoint.
|
|
7
|
+
|
|
8
|
+
Configuration (env vars):
|
|
9
|
+
ANOSYS_HOOK_ENDPOINT_URL — Target URL to POST records to
|
|
10
|
+
ANOSYS_HOOK_API_KEY — Optional Bearer token for the endpoint
|
|
11
|
+
ANOSYS_HOOK_DRY_RUN — Set to "true" to log payloads instead of POSTing
|
|
12
|
+
ANOSYS_HOOK_TRANSCRIPT — Optional: explicit .jsonl path (skips auto-detection)
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import logging
|
|
17
|
+
import os
|
|
18
|
+
import sys
|
|
19
|
+
import time
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any, Dict, List, Optional
|
|
23
|
+
|
|
24
|
+
from anosys_sdk_claude_code.mapper import transform_record
|
|
25
|
+
|
|
26
|
+
ENDPOINT_URL = os.environ.get('ANOSYS_HOOK_ENDPOINT_URL', 'https://api.anosys.ai')
|
|
27
|
+
API_KEY = os.environ.get('ANOSYS_HOOK_API_KEY', '')
|
|
28
|
+
DRY_RUN = os.environ.get('ANOSYS_HOOK_DRY_RUN', 'false').lower() == 'true'
|
|
29
|
+
EXPLICIT_TRANSCRIPT = os.environ.get('ANOSYS_HOOK_TRANSCRIPT', '')
|
|
30
|
+
|
|
31
|
+
STATE_DIR = Path.home() / '.claude' / 'state'
|
|
32
|
+
STATE_FILE = STATE_DIR / 'hook_state.json'
|
|
33
|
+
PENDING_RECORDS_FILE = STATE_DIR / 'pending_records.jsonl'
|
|
34
|
+
LOG_FILE = STATE_DIR / 'hook.log'
|
|
35
|
+
|
|
36
|
+
STATE_DIR.mkdir(parents=True, exist_ok=True)
|
|
37
|
+
|
|
38
|
+
logging.basicConfig(
|
|
39
|
+
filename=str(LOG_FILE),
|
|
40
|
+
level=logging.DEBUG,
|
|
41
|
+
format='%(asctime)s [%(levelname)s] %(message)s',
|
|
42
|
+
datefmt='%Y-%m-%d %H:%M:%S'
|
|
43
|
+
)
|
|
44
|
+
log = logging.getLogger('anosys_claude_hook')
|
|
45
|
+
|
|
46
|
+
_stderr = logging.StreamHandler(sys.stderr)
|
|
47
|
+
_stderr.setLevel(logging.INFO)
|
|
48
|
+
_stderr.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
|
|
49
|
+
log.addHandler(_stderr)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class TranscriptInfo:
|
|
54
|
+
path: Path
|
|
55
|
+
state_key: str
|
|
56
|
+
session_id: str
|
|
57
|
+
is_agent: bool
|
|
58
|
+
agent_id: Optional[str] = None
|
|
59
|
+
agent_type: Optional[str] = None
|
|
60
|
+
agent_description: Optional[str] = None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _load_agent_meta(meta_path: Path) -> dict:
|
|
64
|
+
try:
|
|
65
|
+
if meta_path.exists():
|
|
66
|
+
return json.loads(meta_path.read_text(encoding='utf-8'))
|
|
67
|
+
except Exception:
|
|
68
|
+
pass
|
|
69
|
+
return {}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def find_all_transcripts() -> List[TranscriptInfo]:
|
|
73
|
+
projects_dir = Path.home() / '.claude' / 'projects'
|
|
74
|
+
if not projects_dir.exists():
|
|
75
|
+
return []
|
|
76
|
+
|
|
77
|
+
results = []
|
|
78
|
+
for project_dir in projects_dir.iterdir():
|
|
79
|
+
if not project_dir.is_dir():
|
|
80
|
+
continue
|
|
81
|
+
|
|
82
|
+
for f in project_dir.glob('*.jsonl'):
|
|
83
|
+
session_id = f.stem
|
|
84
|
+
results.append(TranscriptInfo(
|
|
85
|
+
path=f,
|
|
86
|
+
state_key=session_id,
|
|
87
|
+
session_id=session_id,
|
|
88
|
+
is_agent=False
|
|
89
|
+
))
|
|
90
|
+
|
|
91
|
+
for session_subdir in project_dir.iterdir():
|
|
92
|
+
if not session_subdir.is_dir():
|
|
93
|
+
continue
|
|
94
|
+
subagents_dir = session_subdir / 'subagents'
|
|
95
|
+
if subagents_dir.exists() and subagents_dir.is_dir():
|
|
96
|
+
session_id = session_subdir.name
|
|
97
|
+
for af in subagents_dir.glob('*.jsonl'):
|
|
98
|
+
agent_id = af.stem
|
|
99
|
+
meta_path = af.with_suffix('.meta.json')
|
|
100
|
+
meta = _load_agent_meta(meta_path)
|
|
101
|
+
state_key = f"{session_id}:subagent:{agent_id}"
|
|
102
|
+
results.append(TranscriptInfo(
|
|
103
|
+
path=af,
|
|
104
|
+
state_key=state_key,
|
|
105
|
+
session_id=session_id,
|
|
106
|
+
is_agent=True,
|
|
107
|
+
agent_id=agent_id,
|
|
108
|
+
agent_type=meta.get('agentType'),
|
|
109
|
+
agent_description=meta.get('description')
|
|
110
|
+
))
|
|
111
|
+
return results
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def find_latest_transcript() -> Optional[TranscriptInfo]:
|
|
115
|
+
transcripts = find_all_transcripts()
|
|
116
|
+
regular = [t for t in transcripts if not t.is_agent]
|
|
117
|
+
if not regular:
|
|
118
|
+
return None
|
|
119
|
+
return max(regular, key=lambda t: t.path.stat().st_mtime)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def get_stdin_context() -> Optional[dict]:
|
|
123
|
+
import select
|
|
124
|
+
if not select.select([sys.stdin], [], [], 0.1)[0]:
|
|
125
|
+
return None
|
|
126
|
+
try:
|
|
127
|
+
data = sys.stdin.read()
|
|
128
|
+
if not data.strip():
|
|
129
|
+
return None
|
|
130
|
+
return json.loads(data)
|
|
131
|
+
except Exception:
|
|
132
|
+
return None
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def load_state() -> dict:
|
|
136
|
+
try:
|
|
137
|
+
if STATE_FILE.exists():
|
|
138
|
+
return json.loads(STATE_FILE.read_text(encoding='utf-8'))
|
|
139
|
+
except Exception:
|
|
140
|
+
pass
|
|
141
|
+
return {}
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def save_state(state: dict):
|
|
145
|
+
STATE_FILE.write_text(json.dumps(state, indent=2), encoding='utf-8')
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def load_pending_records() -> List[dict]:
|
|
149
|
+
if not PENDING_RECORDS_FILE.exists():
|
|
150
|
+
return []
|
|
151
|
+
records = []
|
|
152
|
+
try:
|
|
153
|
+
with open(PENDING_RECORDS_FILE, 'r', encoding='utf-8') as f:
|
|
154
|
+
for line in f:
|
|
155
|
+
if line.strip():
|
|
156
|
+
records.append(json.loads(line))
|
|
157
|
+
except Exception as e:
|
|
158
|
+
log.error("Failed to load pending records: %s", e)
|
|
159
|
+
return records
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def save_pending_records(records: List[dict], overwrite: bool = False):
|
|
163
|
+
try:
|
|
164
|
+
mode = 'w' if overwrite else 'a'
|
|
165
|
+
with open(PENDING_RECORDS_FILE, mode, encoding='utf-8') as f:
|
|
166
|
+
for r in records:
|
|
167
|
+
f.write(json.dumps(r, separators=(',', ':')) + '\n')
|
|
168
|
+
log.info("Saved %d records to pending storage (%s)", len(records), "overwrite" if overwrite else "append")
|
|
169
|
+
except Exception as e:
|
|
170
|
+
log.error("Failed to save pending records: %s", e)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def clear_pending_records():
|
|
174
|
+
try:
|
|
175
|
+
if PENDING_RECORDS_FILE.exists():
|
|
176
|
+
PENDING_RECORDS_FILE.unlink()
|
|
177
|
+
log.info("Cleared pending records.")
|
|
178
|
+
except Exception as e:
|
|
179
|
+
log.error("Failed to clear pending records: %s", e)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
async def post_records_batch(payloads: List[dict]) -> List[dict]:
|
|
183
|
+
import requests
|
|
184
|
+
if not payloads:
|
|
185
|
+
return []
|
|
186
|
+
|
|
187
|
+
headers = {'Content-Type': 'application/json'}
|
|
188
|
+
if API_KEY:
|
|
189
|
+
headers['Authorization'] = f"Bearer {API_KEY}"
|
|
190
|
+
|
|
191
|
+
failed_records = []
|
|
192
|
+
batch_size = 100
|
|
193
|
+
|
|
194
|
+
for i in range(0, len(payloads), batch_size):
|
|
195
|
+
chunk = payloads[i:i + batch_size]
|
|
196
|
+
if DRY_RUN:
|
|
197
|
+
log.info("[DRY RUN] Would POST chunk of %d records", len(chunk))
|
|
198
|
+
continue
|
|
199
|
+
try:
|
|
200
|
+
resp = requests.post(ENDPOINT_URL, json=chunk, headers=headers, timeout=15)
|
|
201
|
+
resp.raise_for_status()
|
|
202
|
+
log.info("Batch POST success — sent %d records", len(chunk))
|
|
203
|
+
except Exception as e:
|
|
204
|
+
log.error("Batch POST failed — trying to send %d records: %s", len(chunk), e)
|
|
205
|
+
failed_records.extend(chunk)
|
|
206
|
+
return failed_records
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
async def main():
|
|
210
|
+
import asyncio
|
|
211
|
+
await asyncio.sleep(0.5)
|
|
212
|
+
|
|
213
|
+
log.info("="*60)
|
|
214
|
+
log.info("anosys-claude-code run invoked at %s", time.strftime('%Y-%m-%d %H:%M:%S'))
|
|
215
|
+
|
|
216
|
+
pending = load_pending_records()
|
|
217
|
+
if pending:
|
|
218
|
+
log.info("Found %d pending records. Attempting resend...", len(pending))
|
|
219
|
+
failed_pending = await post_records_batch(pending)
|
|
220
|
+
if not failed_pending:
|
|
221
|
+
clear_pending_records()
|
|
222
|
+
else:
|
|
223
|
+
log.warning("%d records still failed after resend. Updating storage.", len(failed_pending))
|
|
224
|
+
save_pending_records(failed_pending, overwrite=True)
|
|
225
|
+
|
|
226
|
+
stdin_ctx = get_stdin_context()
|
|
227
|
+
if stdin_ctx:
|
|
228
|
+
log.info("Received context from stdin: %s", json.dumps(stdin_ctx))
|
|
229
|
+
|
|
230
|
+
provided_path = EXPLICIT_TRANSCRIPT
|
|
231
|
+
if not provided_path and stdin_ctx:
|
|
232
|
+
provided_path = stdin_ctx.get('transcriptPath') or stdin_ctx.get('transcript_path') or ''
|
|
233
|
+
if not provided_path:
|
|
234
|
+
sid = stdin_ctx.get('sessionId') or stdin_ctx.get('session_id')
|
|
235
|
+
if sid:
|
|
236
|
+
for t in find_all_transcripts():
|
|
237
|
+
if t.session_id == sid and not t.is_agent:
|
|
238
|
+
provided_path = str(t.path)
|
|
239
|
+
break
|
|
240
|
+
|
|
241
|
+
if provided_path:
|
|
242
|
+
p = Path(provided_path)
|
|
243
|
+
sid = p.stem
|
|
244
|
+
primary_transcript = TranscriptInfo(path=p, state_key=sid, session_id=sid, is_agent=False)
|
|
245
|
+
else:
|
|
246
|
+
primary_transcript = find_latest_transcript()
|
|
247
|
+
|
|
248
|
+
if not primary_transcript:
|
|
249
|
+
log.warning("No transcript found to process.")
|
|
250
|
+
return
|
|
251
|
+
|
|
252
|
+
current_sid = primary_transcript.session_id
|
|
253
|
+
log.info("Targeting transcript: %s (Session: %s)", primary_transcript.path, current_sid)
|
|
254
|
+
|
|
255
|
+
state = {} if DRY_RUN else load_state()
|
|
256
|
+
session_state = state.get(current_sid, {})
|
|
257
|
+
full_scan_done = session_state.get('full_scan_done', False)
|
|
258
|
+
|
|
259
|
+
if not full_scan_done:
|
|
260
|
+
log.info("First run for session %s. Performing global scan...", current_sid)
|
|
261
|
+
transcripts = find_all_transcripts()
|
|
262
|
+
else:
|
|
263
|
+
all_known = find_all_transcripts()
|
|
264
|
+
transcripts = [primary_transcript] + [t for t in all_known if t.is_agent and t.session_id == current_sid]
|
|
265
|
+
|
|
266
|
+
log.info("Processing %d transcript(s) (%d agent(s)).", len(transcripts), sum(1 for t in transcripts if t.is_agent))
|
|
267
|
+
|
|
268
|
+
all_mapped_payloads = []
|
|
269
|
+
session_updates = []
|
|
270
|
+
|
|
271
|
+
for transcript in transcripts:
|
|
272
|
+
state_key = transcript.state_key
|
|
273
|
+
sid = transcript.session_id
|
|
274
|
+
t_state = state.get(state_key, {})
|
|
275
|
+
last_line = t_state.get('last_line', 0)
|
|
276
|
+
last_uuid = t_state.get('last_message_uuid')
|
|
277
|
+
|
|
278
|
+
try:
|
|
279
|
+
content = transcript.path.read_text(encoding='utf-8')
|
|
280
|
+
except Exception as e:
|
|
281
|
+
log.error("Failed to read transcript %s: %s", transcript.path, e)
|
|
282
|
+
continue
|
|
283
|
+
|
|
284
|
+
all_lines = content.strip().split('\n')
|
|
285
|
+
if not all_lines or not all_lines[0]:
|
|
286
|
+
log.debug("Transcript %s is empty.", sid)
|
|
287
|
+
continue
|
|
288
|
+
|
|
289
|
+
total_lines = len(all_lines)
|
|
290
|
+
log.info("Processing %s: %d total lines, previously saw %d", state_key, total_lines, last_line)
|
|
291
|
+
|
|
292
|
+
parsed = []
|
|
293
|
+
for line in all_lines:
|
|
294
|
+
line = line.strip()
|
|
295
|
+
if not line:
|
|
296
|
+
parsed.append(None)
|
|
297
|
+
continue
|
|
298
|
+
try:
|
|
299
|
+
msg = json.loads(line)
|
|
300
|
+
uuid = msg.get('uuid') or msg.get('messageId') or msg.get('id')
|
|
301
|
+
parsed.append((msg, uuid))
|
|
302
|
+
except Exception:
|
|
303
|
+
parsed.append(None)
|
|
304
|
+
|
|
305
|
+
if last_line > 0 and last_uuid:
|
|
306
|
+
expected_prev = last_line - 1
|
|
307
|
+
matches = False
|
|
308
|
+
if expected_prev < total_lines:
|
|
309
|
+
item = parsed[expected_prev]
|
|
310
|
+
if item and item[1] == last_uuid:
|
|
311
|
+
matches = True
|
|
312
|
+
|
|
313
|
+
if not matches:
|
|
314
|
+
found_idx = -1
|
|
315
|
+
for i in range(total_lines - 1, -1, -1):
|
|
316
|
+
item = parsed[i]
|
|
317
|
+
if item and item[1] == last_uuid:
|
|
318
|
+
found_idx = i
|
|
319
|
+
break
|
|
320
|
+
last_line = found_idx + 1 if found_idx != -1 else 0
|
|
321
|
+
|
|
322
|
+
if last_line >= total_lines:
|
|
323
|
+
continue
|
|
324
|
+
|
|
325
|
+
session_mapped = []
|
|
326
|
+
max_tokens_by_id: Dict[str, Dict[str, float]] = {}
|
|
327
|
+
file_mtime = transcript.path.stat().st_mtime * 1000
|
|
328
|
+
current_context: Dict[str, Any] = {
|
|
329
|
+
'sessionId': sid, 'cwd': None, 'gitBranch': None, 'version': None, 'slug': None,
|
|
330
|
+
'permissionMode': None, 'userType': None, 'is_agent': transcript.is_agent,
|
|
331
|
+
'agent_id': transcript.agent_id, 'agent_type': transcript.agent_type,
|
|
332
|
+
'agent_description': transcript.agent_description, 'file_mtime': file_mtime,
|
|
333
|
+
'last_timestamp': None
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
for i in range(last_line, total_lines):
|
|
337
|
+
item = parsed[i]
|
|
338
|
+
if not item:
|
|
339
|
+
continue
|
|
340
|
+
msg, uuid = item
|
|
341
|
+
for f in ['cwd', 'sessionId', 'gitBranch', 'version', 'slug', 'permissionMode', 'userType']:
|
|
342
|
+
if msg.get(f):
|
|
343
|
+
current_context[f] = msg[f]
|
|
344
|
+
if msg.get('timestamp'):
|
|
345
|
+
current_context['last_timestamp'] = msg['timestamp']
|
|
346
|
+
|
|
347
|
+
incremental = None
|
|
348
|
+
if msg.get('type') == 'assistant' and isinstance(msg.get('message'), dict):
|
|
349
|
+
msg_id = msg['message'].get('id')
|
|
350
|
+
if msg_id:
|
|
351
|
+
usage = msg['message'].get('usage', {})
|
|
352
|
+
c_in = float(usage.get('input_tokens', 0))
|
|
353
|
+
c_out = float(usage.get('output_tokens', 0))
|
|
354
|
+
c_read = float(usage.get('cache_read_input_tokens', 0))
|
|
355
|
+
cc_raw = usage.get('cache_creation')
|
|
356
|
+
if isinstance(cc_raw, dict):
|
|
357
|
+
c_create = float(cc_raw.get('ephemeral_1h_input_tokens', 0) + cc_raw.get('ephemeral_5m_input_tokens', 0))
|
|
358
|
+
elif isinstance(cc_raw, (int, float)):
|
|
359
|
+
c_create = float(cc_raw)
|
|
360
|
+
else:
|
|
361
|
+
c_create = float(usage.get('cache_creation_input_tokens', 0))
|
|
362
|
+
|
|
363
|
+
prev = max_tokens_by_id.get(msg_id, {'input': 0.0, 'output': 0.0, 'cache_read': 0.0, 'cache_creation': 0.0})
|
|
364
|
+
delta_in = c_in - prev['input']
|
|
365
|
+
delta_out = c_out - prev['output']
|
|
366
|
+
delta_read = c_read - prev['cache_read']
|
|
367
|
+
delta_create = c_create - prev['cache_creation']
|
|
368
|
+
max_tokens_by_id[msg_id] = {
|
|
369
|
+
'input': max(prev['input'], c_in),
|
|
370
|
+
'output': max(prev['output'], c_out),
|
|
371
|
+
'cache_read': max(prev['cache_read'], c_read),
|
|
372
|
+
'cache_creation': max(prev['cache_creation'], c_create)
|
|
373
|
+
}
|
|
374
|
+
incremental = {
|
|
375
|
+
'input': delta_in, 'output': delta_out,
|
|
376
|
+
'cache_read': delta_read, 'cache_creation': delta_create,
|
|
377
|
+
'total': delta_in + delta_out + delta_read + delta_create
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
try:
|
|
381
|
+
current_context['log_index'] = i
|
|
382
|
+
mapped = transform_record(msg, incremental, current_context)
|
|
383
|
+
session_mapped.append(mapped)
|
|
384
|
+
except Exception as e:
|
|
385
|
+
log.error("Failed to map record in %s at %d: %s", sid, i, e)
|
|
386
|
+
|
|
387
|
+
if session_mapped:
|
|
388
|
+
all_mapped_payloads.extend(session_mapped)
|
|
389
|
+
f_uuid = parsed[total_lines-1][1] if parsed[total_lines-1] else None
|
|
390
|
+
session_updates.append((state_key, {
|
|
391
|
+
'last_line': total_lines,
|
|
392
|
+
'last_message_uuid': f_uuid,
|
|
393
|
+
'last_run': time.strftime('%Y-%m-%dT%H:%M:%S'),
|
|
394
|
+
'records_sent_total': t_state.get('records_sent_total', 0) + len(session_mapped),
|
|
395
|
+
'is_agent': transcript.is_agent
|
|
396
|
+
}))
|
|
397
|
+
|
|
398
|
+
for skey, up in session_updates:
|
|
399
|
+
s = state.get(skey, {})
|
|
400
|
+
s.update(up)
|
|
401
|
+
state[skey] = s
|
|
402
|
+
|
|
403
|
+
pskey = primary_transcript.state_key
|
|
404
|
+
if pskey in state:
|
|
405
|
+
state[pskey]['full_scan_done'] = True
|
|
406
|
+
else:
|
|
407
|
+
state[pskey] = {'full_scan_done': True}
|
|
408
|
+
|
|
409
|
+
if not DRY_RUN:
|
|
410
|
+
save_state(state)
|
|
411
|
+
|
|
412
|
+
if not all_mapped_payloads:
|
|
413
|
+
log.info("No records to send.")
|
|
414
|
+
return
|
|
415
|
+
|
|
416
|
+
failed = await post_records_batch(all_mapped_payloads)
|
|
417
|
+
if failed:
|
|
418
|
+
log.error("%d failed to POST. Saving to pending.", len(failed))
|
|
419
|
+
save_pending_records(failed)
|
|
420
|
+
else:
|
|
421
|
+
log.info("Sent %d records successfully.", len(all_mapped_payloads))
|