-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsession_log.py
More file actions
646 lines (579 loc) · 24.6 KB
/
Copy pathsession_log.py
File metadata and controls
646 lines (579 loc) · 24.6 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
"""File-based session diagnostics log writer.
Writes structured JSON logs to a global, XDG-aware directory. Each headless
session gets its own directory keyed by session ID, containing process trace
data, a session summary, and flagged anomalies. An append-only index file
provides quick scanning across all sessions.
"""
from __future__ import annotations
import json
import shutil
from collections.abc import Callable
from datetime import UTC, datetime
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal
if TYPE_CHECKING:
from autoskillit.core import ProviderOutcome, RecipeIdentity, SessionLocator, SessionTelemetry
from autoskillit.core import (
ModelIdentity,
atomic_write,
default_log_dir,
get_logger,
iter_merged_assistant_turns,
write_versioned_json,
)
from autoskillit.core import fast_dumps as _fast_dumps
from autoskillit.execution.anomaly_detection import (
detect_anomalies,
detect_identity_drift,
detect_model_drift,
detect_outcome_anomalies,
)
logger = get_logger(__name__)
_MAX_SESSIONS = 2000
def _primary_model_identifier(token_usage: dict[str, Any] | None) -> str:
"""Return the model name with the most output tokens from model_breakdown.
Returns "" when token_usage is absent or model_breakdown is empty.
"""
if not token_usage:
return ""
mb = token_usage.get("model_breakdown", {})
if not isinstance(mb, dict) or not mb:
return ""
return max(mb, key=lambda m: mb[m].get("output_tokens", 0) if isinstance(mb[m], dict) else 0)
_CLEAR_MARKER_FILENAME = ".telemetry_cleared_at"
def resolve_log_dir(log_dir: str) -> Path:
"""Resolve session log directory. Empty string = platform default."""
if log_dir:
return Path(log_dir).expanduser()
return default_log_dir()
def write_telemetry_clear_marker(log_root: Path) -> None:
"""Write the current UTC timestamp as a telemetry-clear fence.
Called when any pipeline log is cleared via clear=True. On the next server
startup, _state._initialize reads this marker and excludes sessions that
predate it from load_from_log_dir replay, preventing double-counting.
Silently no-ops on any error — never raises.
"""
try:
log_root = Path(log_root)
log_root.mkdir(parents=True, exist_ok=True)
atomic_write(log_root / _CLEAR_MARKER_FILENAME, datetime.now(UTC).isoformat())
except Exception:
logger.debug("write_telemetry_clear_marker failed", exc_info=True)
def read_telemetry_clear_marker(log_root: Path) -> datetime | None:
"""Read the persisted telemetry-clear timestamp, or None if absent/corrupt."""
try:
text = (Path(log_root) / _CLEAR_MARKER_FILENAME).read_text(encoding="utf-8").strip()
return datetime.fromisoformat(text)
except (OSError, ValueError):
return None
def _resolve_session_label(step_name: str, dispatch_id: str) -> str:
"""Derive a non-empty session label for telemetry file identification.
Recipe steps use step_name. Fleet dispatches use dispatch_id.
Ad-hoc sessions get a fallback label.
"""
if step_name:
return step_name
if dispatch_id:
return f"dispatch:{dispatch_id}"
return "(ad-hoc)"
def flush_session_log(
*,
log_dir: str,
cwd: str,
kitchen_id: str = "",
caller_session_id: str = "",
order_id: str = "",
campaign_id: str = "",
dispatch_id: str = "",
project_dir: str = "",
build_protected_campaign_ids: Callable[[Path], frozenset[str]] | None = None,
session_id: str,
pid: int,
skill_command: str,
success: bool,
subtype: str,
exit_code: int,
start_ts: str,
proc_snapshots: list[dict[str, object]] | None,
end_ts: str = "",
elapsed_seconds: float | None = None,
termination_reason: str = "",
kill_reason: str = "",
provider_outcome: ProviderOutcome,
recipe_identity: RecipeIdentity,
snapshot_interval_seconds: float = 0.0,
step_name: str = "",
cli_subtype: str = "",
write_path_warnings: list[str] | None = None,
write_call_count: int = 0,
fs_writes_detected: bool = False,
git_writes_detected: bool = False,
file_changes_count: int = 0,
clone_contamination_reverted: bool = False,
tracked_comm: str | None = None,
comm_aliases: frozenset[str] = frozenset(),
exception_text: str = "",
orphaned_tool_result: bool = False,
raw_stdout: str = "",
last_stop_reason: str = "",
has_thinking_only_turn: bool = False,
api_retry_count: int = 0,
api_retry_last_error: str = "",
api_retry_last_status: int | None = None,
api_retry_exhausted: bool = False,
versions: dict[str, Any] | None = None,
model_identity: ModelIdentity = ModelIdentity.unknown(),
max_sessions: int | None = None,
is_resume: bool = False,
codex_log_path: Path | None = None,
session_locator: SessionLocator | None = None,
backend: Literal["claude-code", "codex"] = "claude-code",
telemetry: SessionTelemetry,
) -> None:
"""Flush session diagnostics to disk.
Writes proc_trace.jsonl, summary.json, anomalies.jsonl (if any),
and appends to the global sessions.jsonl index. Applies retention
to keep at most ``_MAX_SESSIONS`` session directories (default 2000,
configurable via ``linux_tracing.max_sessions``).
When step_name is provided, also writes token_usage.json, step_timing.json,
and (if telemetry.audit_record is set) audit_log.json to the session directory
for recovery at next server startup.
"""
token_usage = telemetry.token_usage
timing_seconds = telemetry.timing_seconds
audit_record = telemetry.audit_record
loc_insertions = telemetry.loc_insertions
loc_deletions = telemetry.loc_deletions
effective_write_path_warnings: list[str] = (
write_path_warnings if write_path_warnings is not None else []
)
log_root = resolve_log_dir(log_dir)
if session_id and is_resume:
dir_name = f"{session_id}_{start_ts.replace(':', '-')}"
elif session_id:
dir_name = session_id
else:
dir_name = f"no_session_{start_ts.replace(':', '-')}"
if codex_log_path is not None:
cc_log = None
cc_log_str = None
else:
from autoskillit.execution.backends.claude import (
ClaudeSessionLocator,
) # deferred: avoids circular import via backends/claude.py
_locator = session_locator or ClaudeSessionLocator()
cc_log = _locator.session_log_path(cwd, session_id)
cc_log_str = str(cc_log) if cc_log else None
if cc_log and not cc_log.exists():
logger.warning("claude_code_log_not_found", path=cc_log_str, session_id=session_id)
silent_gap_seconds: float | None = None
if cc_log and cc_log.exists() and end_ts:
try:
cc_log_mtime = cc_log.stat().st_mtime
end_dt = datetime.fromisoformat(end_ts)
silent_gap_seconds = max(0.0, end_dt.timestamp() - cc_log_mtime)
except (OSError, ValueError):
pass
_cb_request_ids: list[str] = []
_cb_turn_timestamps: list[str] = []
_cb_turn_tool_calls: list[tuple[str, ...]] = []
if cc_log and cc_log.exists():
try:
_text = cc_log.read_text(encoding="utf-8", errors="replace")
for _turn in iter_merged_assistant_turns(_text):
_cb_request_ids.append(_turn.request_id)
_cb_turn_timestamps.append(_turn.timestamp)
_cb_turn_tool_calls.append(_turn.tool_names)
except OSError:
logger.debug("channel_b_log_read_error", path=cc_log_str, exc_info=True)
session_dir = log_root / "sessions" / dir_name
session_dir.mkdir(parents=True, exist_ok=True)
snapshot_count = 0
peak_rss_kb = 0
peak_oom_score = 0
peak_fd_ratio = 0.0
anomalies: list[dict[str, object]] = []
_effective_tracked_comm: str | None = tracked_comm
_tracked_comm_drift: bool = False
# Write proc_trace.jsonl
if proc_snapshots:
snapshot_count = len(proc_snapshots)
trace_path = session_dir / "proc_trace.jsonl"
with trace_path.open("w") as f:
for seq, snap in enumerate(proc_snapshots):
record = {
"ts": snap.get("captured_at") or start_ts,
"seq": seq,
"pid": pid,
**snap,
"event": snap.get("event", "snapshot"),
}
f.write(_fast_dumps(record, sort_keys=True) + "\n")
# Track peaks
rss = snap.get("vm_rss_kb", 0)
if isinstance(rss, int) and rss > peak_rss_kb:
peak_rss_kb = rss
oom = snap.get("oom_score", 0)
if isinstance(oom, int) and oom > peak_oom_score:
peak_oom_score = oom
fd_count = snap.get("fd_count", 0)
fd_limit = snap.get("fd_soft_limit", 0)
if isinstance(fd_count, int) and isinstance(fd_limit, int) and fd_limit > 0:
ratio = fd_count / fd_limit
if ratio > peak_fd_ratio:
peak_fd_ratio = ratio
# Compute effective tracked_comm from snapshots if not provided by caller
if _effective_tracked_comm is None:
# Use modal comm value across all snapshots
comm_counts: dict[str, int] = {}
for snap in proc_snapshots:
c = snap.get("comm", "")
if c and isinstance(c, str):
comm_counts[c] = comm_counts.get(c, 0) + 1
if comm_counts:
_effective_tracked_comm = max(comm_counts, key=lambda k: comm_counts[k])
# Detect identity drift: if snapshots have mixed comm values, flag it
if _effective_tracked_comm:
comms_seen = {snap.get("comm", "") for snap in proc_snapshots if snap.get("comm", "")}
if len(comms_seen) > 1:
_tracked_comm_drift = True
# Anomaly detection (standard)
anomalies = detect_anomalies(proc_snapshots, pid)
# Identity drift anomaly (post-fix immunity check)
if _effective_tracked_comm:
drift_anomalies = detect_identity_drift(
proc_snapshots, _effective_tracked_comm, comm_aliases=comm_aliases
)
anomalies.extend(drift_anomalies)
# Outcome anomaly detection (correlates session result with token usage)
if token_usage:
outcome_anomalies = detect_outcome_anomalies(
token_usage, subtype, has_thinking_only_turn=has_thinking_only_turn
)
anomalies.extend(outcome_anomalies)
# API retry exhaustion anomaly — fires regardless of token_usage presence
if api_retry_exhausted:
from autoskillit.execution.anomaly_detection import (
OUTCOME_ANOMALY_PID_SENTINEL,
OUTCOME_ANOMALY_SEQ_SENTINEL,
AnomalyKind,
AnomalySeverity,
)
anomalies.append(
{
"ts": datetime.now(UTC).isoformat(),
"seq": OUTCOME_ANOMALY_SEQ_SENTINEL,
"event": "anomaly",
"kind": str(AnomalyKind.API_RETRY_EXHAUSTION),
"severity": str(AnomalySeverity.WARNING),
"pid": OUTCOME_ANOMALY_PID_SENTINEL,
"detail": {"subtype": subtype, "api_retry_count": api_retry_count},
"snapshot": {},
}
)
effective_model_id = model_identity.effective_model or _primary_model_identifier(token_usage)
_observed = _primary_model_identifier(token_usage) if token_usage else ""
anomalies.extend(
detect_model_drift(
model_identity.configured_model, _observed, profile_name=model_identity.profile_name
)
)
if model_identity.profile_name and model_identity.profile_name != "anthropic":
if effective_model_id.startswith("claude-") or effective_model_id in (
"sonnet",
"opus",
"haiku",
):
logger.warning(
"model_identity_mismatch",
effective_model_id=effective_model_id,
profile_name=model_identity.profile_name,
configured_model=model_identity.configured_model,
)
# Write anomalies.jsonl (only if anomalies exist)
if anomalies:
anomalies_path = session_dir / "anomalies.jsonl"
with anomalies_path.open("w") as f:
for a in anomalies:
f.write(_fast_dumps(a, sort_keys=True) + "\n")
anomaly_count = len(anomalies)
duration_seconds: float | None = None
if elapsed_seconds is not None:
duration_seconds = elapsed_seconds
elif end_ts:
try:
duration_seconds = max(
0.0,
(
datetime.fromisoformat(end_ts) - datetime.fromisoformat(start_ts)
).total_seconds(),
)
except ValueError:
pass
# Write github_api_usage.json from pre-computed telemetry bundle
github_api_requests = telemetry.github_api_requests
if telemetry.github_api_usage is not None:
atomic_write(
session_dir / "github_api_usage.json",
_fast_dumps(telemetry.github_api_usage, sort_keys=True, indent=True) + "\n",
)
# Write summary.json
summary = {
"session_id": session_id,
"dir_name": dir_name,
"pid": pid,
"cwd": cwd,
"claude_code_log": cc_log_str,
"skill_command": skill_command,
"success": success,
"subtype": subtype,
"cli_subtype": cli_subtype,
"exit_code": exit_code,
"start_ts": start_ts,
"end_ts": end_ts,
"duration_seconds": duration_seconds,
"silent_gap_seconds": silent_gap_seconds,
"snapshot_interval_seconds": snapshot_interval_seconds,
"snapshot_count": snapshot_count,
"anomaly_count": anomaly_count,
"peak_rss_kb": peak_rss_kb,
"peak_oom_score": peak_oom_score,
"peak_fd_ratio": round(peak_fd_ratio, 3),
"termination_reason": termination_reason,
"kill_reason": kill_reason,
"provider_used": provider_outcome.provider_used,
"provider_fallback": provider_outcome.fallback_activated,
"write_path_warnings": effective_write_path_warnings,
"write_call_count": write_call_count,
"fs_writes_detected": fs_writes_detected,
"git_writes_detected": git_writes_detected,
"file_changes_count": file_changes_count,
"clone_contamination_reverted": clone_contamination_reverted,
# Tracer target resolution fields (issue #806)
"tracked_comm": _effective_tracked_comm,
"tracked_comm_drift": _tracked_comm_drift,
"tracer_target_resolution_version": 2,
"backend": backend,
"orphaned_tool_result": orphaned_tool_result,
"last_stop_reason": last_stop_reason,
"request_ids": _cb_request_ids,
"turn_timestamps": _cb_turn_timestamps,
"turn_tool_calls": _cb_turn_tool_calls,
"campaign_id": campaign_id,
"dispatch_id": dispatch_id,
"github_api_requests": github_api_requests,
"caller_session_id": caller_session_id,
"api_retry_count": api_retry_count,
"api_retry_last_error": api_retry_last_error,
"api_retry_last_status": api_retry_last_status,
"api_retry_exhausted": api_retry_exhausted,
}
if versions is not None:
summary["versions"] = {
**versions,
"model_identifier": effective_model_id,
}
if recipe_identity.name or recipe_identity.content_hash:
summary["recipe_provenance"] = {
"schema_version": 1,
"name": recipe_identity.name,
"version": recipe_identity.version,
"content_hash": recipe_identity.content_hash,
"composite_hash": recipe_identity.composite_hash,
}
summary_path = session_dir / "summary.json"
atomic_write(summary_path, _fast_dumps(summary, sort_keys=True, indent=True) + "\n")
if campaign_id:
meta_path = session_dir / "meta.json"
atomic_write(
meta_path,
_fast_dumps({"campaign_id": campaign_id, "dispatch_id": dispatch_id}, sort_keys=True),
)
if not success and raw_stdout:
atomic_write(session_dir / "raw_stdout.jsonl", raw_stdout)
if exception_text:
atomic_write(session_dir / "crash_exception.txt", exception_text)
# Write per-session telemetry files; gate on data presence, not session identity
label = _resolve_session_label(step_name, dispatch_id)
if token_usage is not None:
_cw_raw = token_usage.get("cache_write_tokens")
_cache_write = (
_cw_raw
if _cw_raw is not None
else (token_usage.get("cache_creation_input_tokens") or 0)
)
_cr_raw = token_usage.get("cache_read_tokens")
_cache_read = (
_cr_raw if _cr_raw is not None else (token_usage.get("cache_read_input_tokens") or 0)
)
tu_data = {
"session_label": label,
"input_tokens": token_usage.get("input_tokens") or 0,
"output_tokens": token_usage.get("output_tokens") or 0,
"cache_write_tokens": _cache_write,
"cache_read_tokens": _cache_read,
"timing_seconds": timing_seconds if timing_seconds is not None else 0.0,
"order_id": order_id,
"loc_insertions": loc_insertions,
"loc_deletions": loc_deletions,
"peak_context": token_usage.get("peak_context", 0),
"turn_count": token_usage.get("turn_count", 0),
"provider_used": provider_outcome.provider_used,
"model_identifier": effective_model_id,
"configured_model": model_identity.configured_model,
"profile_name": model_identity.profile_name,
"dispatch_id": dispatch_id,
"campaign_id": campaign_id,
}
write_versioned_json(session_dir / "token_usage.json", tu_data, schema_version=2)
if timing_seconds is not None:
atomic_write(
session_dir / "step_timing.json",
_fast_dumps(
{
"step_name": label,
"total_seconds": max(0.0, timing_seconds),
"order_id": order_id,
}
),
)
if step_name and audit_record is not None:
atomic_write(session_dir / "audit_log.json", _fast_dumps([audit_record]))
# Append to sessions.jsonl index
index_entry = {
"session_id": session_id,
"dir_name": dir_name,
"timestamp": start_ts,
"cwd": cwd,
"kitchen_id": kitchen_id,
"order_id": order_id,
"campaign_id": campaign_id,
"dispatch_id": dispatch_id,
"claude_code_log": cc_log_str,
"codex_log": str(codex_log_path) if codex_log_path else None,
"skill_command": skill_command,
"success": success,
"subtype": subtype,
"cli_subtype": cli_subtype,
"exit_code": exit_code,
"snapshot_count": snapshot_count,
"anomaly_count": anomaly_count,
"peak_rss_kb": peak_rss_kb,
"peak_oom_score": peak_oom_score,
"step_name": step_name,
"input_tokens": (token_usage.get("input_tokens") or 0) if token_usage else 0,
"output_tokens": (token_usage.get("output_tokens") or 0) if token_usage else 0,
"cache_write_tokens": (token_usage.get("cache_write_tokens") or 0) if token_usage else 0,
"cache_read_tokens": (token_usage.get("cache_read_tokens") or 0) if token_usage else 0,
"write_call_count": write_call_count,
"fs_writes_detected": fs_writes_detected,
"git_writes_detected": git_writes_detected,
"file_changes_count": file_changes_count,
"tracked_comm": _effective_tracked_comm,
"tracked_comm_drift": _tracked_comm_drift,
"backend": backend,
"autoskillit_version": versions.get("autoskillit_version", "") if versions else "",
"claude_code_version": versions.get("claude_code_version", "") if versions else "",
"codex_version": versions.get("codex_version", "") if versions else "",
"recipe_name": recipe_identity.name,
"recipe_content_hash": recipe_identity.content_hash,
"recipe_composite_hash": recipe_identity.composite_hash,
"recipe_version": recipe_identity.version,
"duration_seconds": duration_seconds,
"github_api_requests": github_api_requests,
"provider_used": provider_outcome.provider_used,
"provider_fallback": provider_outcome.fallback_activated,
"caller_session_id": caller_session_id,
"api_retry_count": api_retry_count,
"api_retry_exhausted": api_retry_exhausted,
"api_retry_last_error": api_retry_last_error,
"api_retry_last_status": api_retry_last_status,
"model_identifier": effective_model_id,
"configured_model": model_identity.configured_model,
"profile_name": model_identity.profile_name,
"schema_version": 3,
}
index_path = log_root / "sessions.jsonl"
with index_path.open("a") as f:
f.write(_fast_dumps(index_entry, sort_keys=True) + "\n")
# Co-write session provenance record
if cwd:
from autoskillit.core import ProvenanceRecord, write_provenance_record
write_provenance_record(
ProvenanceRecord(
session_id=session_id,
caller_session_id=caller_session_id,
kitchen_id=kitchen_id,
dispatch_id=dispatch_id,
recipe_name=recipe_identity.name,
step_name=step_name,
timestamp=start_ts or end_ts or "",
),
project_dir=Path(project_dir) if project_dir else None,
)
# Retention: keep at most _MAX_SESSIONS session directories
_enforce_retention(
log_root,
project_dir=project_dir,
build_protected_campaign_ids=build_protected_campaign_ids,
max_sessions=max_sessions if max_sessions is not None else _MAX_SESSIONS,
)
def _enforce_retention(
log_root: Path,
project_dir: str | None = None,
build_protected_campaign_ids: Callable[[Path], frozenset[str]] | None = None,
*,
max_sessions: int = _MAX_SESSIONS,
) -> None:
"""Delete oldest session directories if count exceeds *max_sessions*.
When ``project_dir`` is provided, reads fleet state files and ``meta.json``
sidecars to skip deletion of sessions belonging to active campaigns.
"""
sessions_dir = log_root / "sessions"
if not sessions_dir.is_dir():
return
dirs = sorted(sessions_dir.iterdir(), key=lambda p: p.stat().st_mtime)
if len(dirs) <= max_sessions:
return
expired = dirs[: len(dirs) - max_sessions]
surviving_names = {d.name for d in dirs[len(dirs) - max_sessions :]}
protected_ids = (
build_protected_campaign_ids(Path(project_dir))
if project_dir and build_protected_campaign_ids is not None
else frozenset()
)
for d in expired:
if protected_ids:
meta_path = d / "meta.json"
if meta_path.is_file():
try:
meta = json.loads(meta_path.read_text(encoding="utf-8"))
if meta.get("campaign_id") in protected_ids:
surviving_names.add(d.name)
continue
except (json.JSONDecodeError, OSError):
pass
shutil.rmtree(d, ignore_errors=True)
# Rewrite sessions.jsonl to remove expired entries
index_path = log_root / "sessions.jsonl"
if index_path.is_file():
# Accepted read-modify-write race: between read_text() below and atomic_write()
# at the end of this block, a concurrent flush_session_log() call may append a
# new entry to sessions.jsonl via open("a"). That entry will not be present in
# `lines`, so it will be silently lost when atomic_write() overwrites the file.
# Worst case: one diagnostic index entry dropped per concurrent session flush that
# races this retention sweep. Correctness of the running session is unaffected.
# File locking (fcntl.flock) is not warranted: the overhead exceeds the value of
# protecting a best-effort diagnostic artifact.
lines = index_path.read_text().splitlines()
kept: list[str] = []
for line in lines:
if not line.strip():
continue
try:
entry = json.loads(line)
if entry.get("dir_name") in surviving_names:
kept.append(line)
except json.JSONDecodeError:
continue
atomic_write(index_path, "\n".join(kept) + "\n" if kept else "")