Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

[BUG] Observability file watcher stops detecting events after midnight - stale file path comparison

Open bnkath2o opened this issue 11 months ago • 2 comments

[BUG] Observability file watcher stops detecting events after midnight - stale file path comparison

Description

The observability skill's file ingestion system fails to detect events from Claude sessions that span across midnight (calendar day boundary). Long-running sessions stop appearing in the observability dashboard after midnight because the file watcher continues monitoring yesterday's events file instead of switching to the new day's file.

This is particularly problematic for users who:

  • Keep Claude sessions open for extended periods
  • Work across midnight
  • Don't want to lose accumulated context by restarting sessions

Steps to Reproduce

  1. Start the observability server: ~/.claude/skills/observability/manage.sh start
  2. Open a Claude Code session with hooks configured (including capture-all-events.ts)
  3. Verify events appear in the dashboard at http://localhost:5172
  4. Keep the Claude session running past midnight (or manually test by changing system date)
  5. After midnight, perform tool operations in Claude
  6. Observe: New events do NOT appear in the dashboard

Expected Behavior

The observability dashboard should continue showing events from all active Claude sessions regardless of how long they've been running or whether they span midnight. The file watcher should automatically switch to monitoring the new day's events file (YYYY-MM-DD_all-events.jsonl) at or shortly after midnight.

Actual Behavior

After midnight:

  • The capture-all-events.ts hook correctly writes events to the NEW day's file (e.g., 2025-12-11_all-events.jsonl)
  • The observability server continues watching the OLD day's file (e.g., 2025-12-10_all-events.jsonl)
  • Dashboard shows stale/no events for sessions that crossed midnight
  • Requires manual restart of observability server OR Claude session to restore functionality

Root Cause Analysis

File: ~/.claude/skills/observability/apps/server/src/file-ingest.ts Lines: 298-309

export function startFileIngestion(callback?: (events: HookEvent[]) => void): void {
  // ...

  // Watch today's file
  const todayFile = getTodayEventsFile();  // BUG: const captured at startup
  watchFile(todayFile);

  // Check for new day's file every hour
  setInterval(() => {
    const newTodayFile = getTodayEventsFile();
    if (newTodayFile !== todayFile) {  // BUG: Always compares against STALE value
      console.log('New day detected, watching new file');
      watchFile(newTodayFile);
    }
  }, 60 * 60 * 1000); // Check every hour
  // ...
}

Bug Details

  1. Stale Variable Comparison: todayFile is declared as const and captured in the closure at startup. The setInterval callback compares newTodayFile !== todayFile, but todayFile never gets updated after a new day is detected.

  2. Timing Gap: The check only runs every hour (60 * 60 * 1000ms). This creates a potential 0-59 minute window after midnight where the new day's file may not be detected.

  3. File Existence: The watchFile() function checks existsSync() before watching, but if the file doesn't exist at check time, no watcher is set up and there's no retry mechanism.

Recommended Fix

Option A: Minimal Fix (Update Variable + Reduce Interval)

export function startFileIngestion(callback?: (events: HookEvent[]) => void): void {
  // ...

  // Watch today's file
  let currentWatchedFile = getTodayEventsFile();  // CHANGE: let instead of const
  watchFile(currentWatchedFile);

  // Check for new day's file every 5 minutes
  setInterval(() => {
    const newTodayFile = getTodayEventsFile();
    if (newTodayFile !== currentWatchedFile) {  // CHANGE: compare against mutable variable
      console.log('New day detected, watching new file');
      watchFile(newTodayFile);
      currentWatchedFile = newTodayFile;  // ADD: update the tracked file
    }
  }, 5 * 60 * 1000); // CHANGE: Check every 5 minutes instead of 1 hour
  // ...
}

Option B: More Robust (Watch Directory Instead)

Watch the ~/.claude/history/raw-outputs/YYYY-MM/ directory for new files matching *_all-events.jsonl pattern:

import { watch } from 'fs';
import { join } from 'path';

function watchEventsDirectory(): void {
  const monthDir = getMonthDirectory();

  watch(monthDir, (eventType, filename) => {
    if (filename && filename.endsWith('_all-events.jsonl')) {
      const filePath = join(monthDir, filename);
      if (!watchedFiles.has(filePath)) {
        watchFile(filePath);
      }
    }
  });
}

Option C: Hybrid (Recommended)

Combine both approaches:

  1. Use let variable with 5-minute interval as backup
  2. Watch directory for immediate detection of new files
  3. Add retry logic in watchFile() if file doesn't exist

Impact

  • Users Affected: Anyone using the observability skill with long-running Claude sessions
  • Workaround: Restart observability server after midnight: ~/.claude/skills/observability/manage.sh restart
  • Data Loss: No permanent data loss - events are still captured to files, just not displayed in real-time dashboard

Environment

  • PAI Version: Latest (as of 2025-12-11)
  • File: ~/.claude/skills/observability/apps/server/src/file-ingest.ts
  • Runtime: Bun
  • Platform: macOS (likely affects all platforms)

Related Files

  • ~/.claude/skills/observability/apps/server/src/file-ingest.ts - Main file with bug
  • ~/.claude/skills/observability/apps/server/src/index.ts - Server that uses file-ingest
  • ~/.claude/hooks/capture-all-events.ts - Hook that writes events (works correctly)
  • ~/.claude/history/raw-outputs/YYYY-MM/YYYY-MM-DD_all-events.jsonl - Event storage files

bnkath2o avatar Dec 11 '25 17:12 bnkath2o

Furthermore be aware of timezone. If TIME_ZONE env is not set, it defaults to America/Los Angeles. e.g. https://github.com/danielmiessler/Personal_AI_Infrastructure/blob/main/.claude/hooks/capture-all-events.ts#L26

Even when changing the TIME_ZONE env, the logged date output still references to PST time in the log output (even with another timezone set ) -> (timestamp_pst)

sti0 avatar Dec 15 '25 07:12 sti0

Fixed in commit e0c2685.

The bug was a stale variable reference in the setInterval closure:

// Before (buggy) - todayFile never updates
const todayFile = getTodayEventsFile();
setInterval(() => {
  const newTodayFile = getTodayEventsFile();
  if (newTodayFile !== todayFile) { // Always compares to stale value
    watchFile(newTodayFile);
  }
}, 60 * 60 * 1000);

// After (fixed)
let currentWatchedFile = getTodayEventsFile();
setInterval(() => {
  const newTodayFile = getTodayEventsFile();
  if (newTodayFile !== currentWatchedFile) {
    watchFile(newTodayFile);
    currentWatchedFile = newTodayFile; // Update tracked file
  }
}, 5 * 60 * 1000); // Also reduced to 5 minutes

Thanks for reporting!

danielmiessler avatar Dec 15 '25 08:12 danielmiessler