conduct-cli 0.4.10__tar.gz → 0.4.12__tar.gz
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.
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/PKG-INFO +1 -1
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/pyproject.toml +1 -1
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli/guard.py +52 -3
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli.egg-info/PKG-INFO +1 -1
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/README.md +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/setup.cfg +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/setup.py +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli/__init__.py +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli/api.py +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli/guardmcp.py +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli/main.py +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli.egg-info/SOURCES.txt +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli.egg-info/dependency_links.txt +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli.egg-info/entry_points.txt +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli.egg-info/requires.txt +0 -0
- {conduct_cli-0.4.10 → conduct_cli-0.4.12}/src/conduct_cli.egg-info/top_level.txt +0 -0
|
@@ -198,13 +198,30 @@ def _post_usage(session_id, tool_name, tokens_input, tokens_output, duration_ms)
|
|
|
198
198
|
)
|
|
199
199
|
|
|
200
200
|
|
|
201
|
+
def _tail_lines(path, n=200):
|
|
202
|
+
"""Read last n lines of a file efficiently without loading the whole file."""
|
|
203
|
+
size = path.stat().st_size
|
|
204
|
+
if size == 0:
|
|
205
|
+
return []
|
|
206
|
+
chunk = min(size, n * 300) # ~300 bytes per line estimate
|
|
207
|
+
with open(path, "rb") as f:
|
|
208
|
+
f.seek(max(0, size - chunk))
|
|
209
|
+
raw = f.read()
|
|
210
|
+
text = raw.decode("utf-8", errors="ignore")
|
|
211
|
+
lines = text.splitlines()
|
|
212
|
+
# If we didn't seek to start, first line may be partial — drop it
|
|
213
|
+
if size > chunk:
|
|
214
|
+
lines = lines[1:]
|
|
215
|
+
return lines
|
|
216
|
+
|
|
217
|
+
|
|
201
218
|
def _read_tokens_from_transcript(transcript_path, tool_use_id):
|
|
202
|
-
"""
|
|
219
|
+
"""Read token counts from Claude Code transcript (matched by tool_use_id)."""
|
|
203
220
|
try:
|
|
204
221
|
path = Path(transcript_path)
|
|
205
222
|
if not path.exists() or not tool_use_id:
|
|
206
223
|
return 0, 0
|
|
207
|
-
lines = path
|
|
224
|
+
lines = _tail_lines(path)
|
|
208
225
|
for line in reversed(lines):
|
|
209
226
|
if not line.strip() or "tool_use" not in line:
|
|
210
227
|
continue
|
|
@@ -232,6 +249,35 @@ def _read_tokens_from_transcript(transcript_path, tool_use_id):
|
|
|
232
249
|
return 0, 0
|
|
233
250
|
|
|
234
251
|
|
|
252
|
+
def _read_codex_tokens():
|
|
253
|
+
"""Read last_token_usage from the most recently modified Codex session file."""
|
|
254
|
+
try:
|
|
255
|
+
sessions_dir = Path.home() / ".codex" / "sessions"
|
|
256
|
+
if not sessions_dir.exists():
|
|
257
|
+
return 0, 0
|
|
258
|
+
files = sorted(sessions_dir.rglob("*.jsonl"), key=lambda p: p.stat().st_mtime, reverse=True)
|
|
259
|
+
if not files:
|
|
260
|
+
return 0, 0
|
|
261
|
+
lines = _tail_lines(files[0])
|
|
262
|
+
for line in reversed(lines):
|
|
263
|
+
if "token_count" not in line:
|
|
264
|
+
continue
|
|
265
|
+
try:
|
|
266
|
+
entry = json.loads(line)
|
|
267
|
+
if entry.get("type") == "event_msg":
|
|
268
|
+
info = entry.get("payload", {}).get("info", {})
|
|
269
|
+
usage = info.get("last_token_usage", {})
|
|
270
|
+
if usage:
|
|
271
|
+
total_in = usage.get("input_tokens", 0)
|
|
272
|
+
total_out = usage.get("output_tokens", 0) + usage.get("reasoning_output_tokens", 0)
|
|
273
|
+
return total_in, total_out
|
|
274
|
+
except Exception:
|
|
275
|
+
continue
|
|
276
|
+
except Exception:
|
|
277
|
+
pass
|
|
278
|
+
return 0, 0
|
|
279
|
+
|
|
280
|
+
|
|
235
281
|
def post_usage_main():
|
|
236
282
|
"""PostToolUse hook entrypoint — captures token usage and duration."""
|
|
237
283
|
try:
|
|
@@ -242,7 +288,10 @@ def post_usage_main():
|
|
|
242
288
|
tool_name = (data.get("tool_name") or "").lower()
|
|
243
289
|
tool_use_id = data.get("tool_use_id")
|
|
244
290
|
transcript_path = data.get("transcript_path")
|
|
245
|
-
|
|
291
|
+
if transcript_path:
|
|
292
|
+
tokens_input, tokens_output = _read_tokens_from_transcript(transcript_path, tool_use_id)
|
|
293
|
+
else:
|
|
294
|
+
tokens_input, tokens_output = _read_codex_tokens()
|
|
246
295
|
_post_usage(session_id, tool_name, tokens_input, tokens_output, None)
|
|
247
296
|
sys.exit(0)
|
|
248
297
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|