'use strict';
// Status authority inversion.
//
// CTM derives session status from two kinds of evidence:
//   1. Structured signals — OTLP telemetry (codex) and provider hooks
//      (claude/gemini), surfaced via lib/telemetry-receiver. These say
//      exactly when a turn starts and ends.
//   2. PTY byte inference — provider state detectors classifying raw
//      terminal chunks (workers/state-detectors/*).
//
// Byte inference is inherently low-precision for full-screen TUIs: an idle
// codex still repaints (background MCP/rate-limit fetches, focus events,
// animation frames), and one misclassified chunk holds the sidebar on
// "Running" for the provider's whole idle-debounce window. So when a
// structured source owns a session, generic bytes must not CREATE a running
// state out of idle — only the provider's own busy-status line ("Working …
// esc to interrupt", matched strictly) or the structured source itself may.
//
// Two deliberate escape hatches keep this safe:
//   - No structured source (fresh install, otel/notify not configured):
//     full byte inference remains, it is the only signal available.
//   - Provider without a precise busy channel (no textHasCodexBusyStatus
//     on its detector): never strand it — its hooks can lag and generic
//     output is the only unstick signal it has.

function ptyActivityMayDriveRunning({
  hasAuthoritativeSource = false,
  busyEvidenceCapable = false,
  lastBusyStatusAt = 0,
  now = Date.now(),
  busyHoldMs = 15000,
} = {}) {
  if (!hasAuthoritativeSource) return true;
  if (!busyEvidenceCapable) return true;
  const busyAt = Number(lastBusyStatusAt) || 0;
  return !!(busyAt && (now - busyAt) < busyHoldMs);
}

module.exports = { ptyActivityMayDriveRunning };
