feat(server): add conversation mtime tracking for real-time updates
Add conversation_mtime_ns field to session state that tracks the actual
modification time of conversation files. This enables more responsive
dashboard updates by detecting changes that occur between hook events
(e.g., during streaming tool execution).
Changes:
- state.py: Add _get_conversation_mtime() to stat conversation files
and include mtime_ns in session payloads when available
- conversation.py: Add stable message IDs (claude-{session}-{n} format)
for React key stability and message deduplication
- control.py: Fix FIFO eviction for dismissed_codex_ids - set.pop()
removes arbitrary element, now uses dict with insertion-order iteration
- context.py: Update dismissed_codex_ids type from set to dict
The mtime approach complements existing last_event_at tracking:
- last_event_at: Changes on hook events (session boundaries)
- conversation_mtime_ns: Changes on every file write (real-time)
Dashboard can now detect mid-session conversation updates without
waiting for the next hook event.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import json
|
||||
import subprocess
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
from amc_server.context import (
|
||||
EVENTS_DIR,
|
||||
@@ -119,6 +120,11 @@ class StateMixin:
|
||||
if context_usage:
|
||||
data["context_usage"] = context_usage
|
||||
|
||||
# Track conversation file mtime for real-time update detection
|
||||
conv_mtime = self._get_conversation_mtime(data)
|
||||
if conv_mtime:
|
||||
data["conversation_mtime_ns"] = conv_mtime
|
||||
|
||||
sessions.append(data)
|
||||
except (json.JSONDecodeError, OSError):
|
||||
continue
|
||||
@@ -166,6 +172,38 @@ class StateMixin:
|
||||
|
||||
return None # Return None on error (don't clean up if we can't verify)
|
||||
|
||||
def _get_conversation_mtime(self, session_data):
|
||||
"""Get the conversation file's mtime for real-time change detection."""
|
||||
agent = session_data.get("agent")
|
||||
|
||||
if agent == "claude":
|
||||
conv_file = self._get_claude_conversation_file(
|
||||
session_data.get("session_id", ""),
|
||||
session_data.get("project_dir", ""),
|
||||
)
|
||||
if conv_file:
|
||||
try:
|
||||
return conv_file.stat().st_mtime_ns
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
elif agent == "codex":
|
||||
transcript_path = session_data.get("transcript_path", "")
|
||||
if transcript_path:
|
||||
try:
|
||||
return Path(transcript_path).stat().st_mtime_ns
|
||||
except OSError:
|
||||
pass
|
||||
# Fallback to discovery
|
||||
transcript_file = self._find_codex_transcript_file(session_data.get("session_id", ""))
|
||||
if transcript_file:
|
||||
try:
|
||||
return transcript_file.stat().st_mtime_ns
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
def _cleanup_stale(self, sessions):
|
||||
"""Remove orphan event logs >24h and stale 'starting' sessions >1h."""
|
||||
active_ids = {s.get("session_id") for s in sessions if s.get("session_id")}
|
||||
|
||||
Reference in New Issue
Block a user