Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

15.5 KB
Raw Blame
import { useState, memo } from 'react';
import { Link } from 'react-router';
import {
  Eye,
  ChevronRight,
  ChevronDown,
  AlertCircle,
  CheckCircle,
  ArrowRight,
  Clock,
  RefreshCw,
  Zap,
  Users,
  Pause,
  Moon
} from 'lucide-react';
import * as api from '../services/api';
import { useSocketResource } from '../hooks/useSocketResource';
import { useTimeTick } from '../hooks/useTimeTick';
import { timeAgo } from '../utils/formatters';
import { equalByKeys, equalListByKeys } from '../lib/compareHelpers';
import ProvenanceChip from './ui/ProvenanceChip';

const RESOURCE_EVENTS = ['cos:decisions:changed'];

// Every row here is a record of an autonomous choice the Chief of Staff made —
// what it skipped, switched, or deferred and why. That's read straight off the
// decision ledger it wrote as it ran, so the provenance is data-backed: a log
// of actions taken, not a model's guess about them.
const DECISION_PROVENANCE = {
  level: 'data-backed',
  explainer:
    'Read directly from the decision ledger your Chief of Staff writes as it runs — each row is an autonomous choice it actually made (skip, switch, defer) with the reason it recorded at the time.',
  whatWouldChange:
    'New entries appear as the Chief of Staff makes more scheduling decisions; the log only grows, it is never re-estimated.',
};

/**
 * DecisionLogWidget - Shows transparency into CoS decision-making
 * Displays why tasks were skipped, intervals adjusted, or alternatives chosen.
 * Surfaces rich context for each decision type so the user can understand
 * exactly why the CoS took (or didn't take) action.
 */
const DecisionLogWidget = memo(function DecisionLogWidget() {
  // Let errors throw — `useSocketResource` preserves the last-good summary on
  // transient failures instead of dropping the widget back to its loading
  // state on every blip.
  const { data: summary, loading } = useSocketResource(
    () => api.getCosDecisionSummary({ silent: true }),
    {
      namespace: 'cos', events: RESOURCE_EVENTS,
      // Decision stream is append-only; same 24h totals + same per-decision
      // tuple of every rendered field means nothing visible advanced this
      // minute. Comparator walks: id (key + dedup), type (icon + label),
      // reason (body + tooltip), count (×N badge), and the context fields
      // that renderContextDetails surfaces (running/max/project/limit, appId/
      // cooldownMs, fromTask/toTask, attempts, runningAgents/awaitingApproval,
      // taskType, successRate). The `timeAgo(decision.lastTimestamp ||
      // decision.timestamp)` label is re-rendered by the useTimeTick(60000)
      // below — including the timestamp fields in this comparator would
      // pointlessly break dedup on every backend mtime nudge. Keep this tuple
      // in sync with the JSX above.
      compare: (prev, next) => prev != null &&
        equalByKeys(prev.last24Hours, next.last24Hours, [
          'total', 'skipped', 'switched', 'capacityFull',
          'cooldownActive', 'selected', 'adjusted',
        ])
        && prev.transparencyScore === next.transparencyScore
        && equalListByKeys(prev.impactfulDecisions, next.impactfulDecisions, [
          'id', 'type', 'reason', (d) => d?.count ?? 1,
          'context.running', 'context.max', 'context.project', 'context.limit',
          'context.appId', 'context.cooldownMs', 'context.fromTask', 'context.toTask',
          'context.attempts', 'context.runningAgents', 'context.awaitingApproval',
          'context.taskType', 'context.successRate',
        ]),
    },
  );
  const [expanded, setExpanded] = useState(false);
  // Tick every minute so the `timeAgo(...)` relative-time labels on each
  // decision row roll over even when the resource snapshot is unchanged by the
  // comparator.
  useTimeTick(60000);

  // Don't render while loading or if no data
  if (loading || !summary) {
    return null;
  }

  // Only show if there are impactful decisions to display
  if (!summary.hasImpactfulDecisions && summary.last24Hours.total === 0) {
    return null;
  }

  const { last24Hours, impactfulDecisions, transparencyScore } = summary;

  const getDecisionStyle = (type) => {
    switch (type) {
      case 'task_skipped':
        return { icon: AlertCircle, color: 'text-port-warning', bg: 'bg-port-warning/10' };
      case 'task_switched':
        return { icon: ArrowRight, color: 'text-purple-400', bg: 'bg-purple-400/10' };
      case 'interval_adjusted':
        return { icon: Clock, color: 'text-blue-400', bg: 'bg-blue-400/10' };
      case 'rehabilitation':
        return { icon: RefreshCw, color: 'text-port-success', bg: 'bg-port-success/10' };
      case 'task_selected':
        return { icon: CheckCircle, color: 'text-port-success', bg: 'bg-port-success/10' };
      case 'capacity_full':
        return { icon: Users, color: 'text-orange-400', bg: 'bg-orange-400/10' };
      case 'cooldown_active':
        return { icon: Pause, color: 'text-cyan-400', bg: 'bg-cyan-400/10' };
      case 'idle':
        return { icon: Moon, color: 'text-gray-400', bg: 'bg-gray-400/10' };
      default:
        return { icon: Eye, color: 'text-gray-400', bg: 'bg-gray-400/10' };
    }
  };

  // Format decision type for display
  const formatDecisionType = (type) => {
    const labels = {
      task_skipped: 'Skipped',
      task_switched: 'Switched',
      interval_adjusted: 'Adjusted',
      cooldown_active: 'Cooldown',
      not_due: 'Not Due',
      queue_full: 'Queue Full',
      capacity_full: 'At Capacity',
      task_selected: 'Selected',
      rehabilitation: 'Retried',
      idle: 'Idle'
    };
    return labels[type] || type;
  };

  // Render context-specific detail pills for a decision
  const renderContextDetails = (decision) => {
    const ctx = decision.context;
    if (!ctx) return null;

    const pills = [];

    switch (decision.type) {
      case 'capacity_full':
        if (ctx.running !== undefined && ctx.max !== undefined) {
          pills.push(
            <span key="slots" className="text-xs px-1.5 py-0.5 rounded bg-orange-400/15 text-orange-300">
              {ctx.running}/{ctx.max} agents
            </span>
          );
        }
        if (ctx.project && ctx.project !== '_self') {
          pills.push(
            <span key="project" className="text-xs px-1.5 py-0.5 rounded bg-gray-500/15 text-gray-300">
              {ctx.project}
            </span>
          );
        }
        if (ctx.limit) {
          pills.push(
            <span key="limit" className="text-xs px-1.5 py-0.5 rounded bg-orange-400/15 text-orange-300">
              limit: {ctx.limit}/project
            </span>
          );
        }
        break;

      case 'cooldown_active':
        if (ctx.appId) {
          pills.push(
            <span key="app" className="text-xs px-1.5 py-0.5 rounded bg-cyan-400/15 text-cyan-300">
              {ctx.appId}
            </span>
          );
        }
        if (ctx.cooldownMs) {
          pills.push(
            <span key="cooldown" className="text-xs px-1.5 py-0.5 rounded bg-cyan-400/15 text-cyan-300">
              {Math.round(ctx.cooldownMs / 60000)}min window
            </span>
          );
        }
        break;

      case 'task_switched':
        if (ctx.fromTask && ctx.toTask) {
          pills.push(
            <span key="switch" className="text-xs px-1.5 py-0.5 rounded bg-purple-400/15 text-purple-300 inline-flex items-center gap-1">
              {ctx.fromTask} <ArrowRight size={10} /> {ctx.toTask}
            </span>
          );
        }
        break;

      case 'task_skipped':
        if (ctx.attempts) {
          pills.push(
            <span key="attempts" className="text-xs px-1.5 py-0.5 rounded bg-port-warning/15 text-yellow-300">
              {ctx.attempts} attempts
            </span>
          );
        }
        break;

      case 'idle':
        if (ctx.runningAgents !== undefined) {
          pills.push(
            <span key="running" className="text-xs px-1.5 py-0.5 rounded bg-gray-500/15 text-gray-300">
              {ctx.runningAgents} running
            </span>
          );
        }
        if (ctx.awaitingApproval > 0) {
          pills.push(
            <span key="approval" className="text-xs px-1.5 py-0.5 rounded bg-port-warning/15 text-yellow-300">
              {ctx.awaitingApproval} awaiting approval
            </span>
          );
        }
        break;

      default:
        break;
    }

    // Always show taskType if present and not already shown by type-specific pills
    if (ctx.taskType && decision.type !== 'task_switched') {
      pills.unshift(
        <span key="taskType" className="text-xs text-gray-400">
          {ctx.taskType}
        </span>
      );
    }

    if (pills.length === 0) return null;

    return <div className="flex flex-wrap items-center gap-1.5 mt-1">{pills}</div>;
  };

  return (
    <div className="@container min-w-0 bg-port-card border border-port-border rounded-xl p-4">
      {/* Header */}
      <div className="flex items-center justify-between gap-2 mb-4">
        <div className="flex items-center gap-3 min-w-0">
          <div className="shrink-0 text-2xl" aria-hidden="true">
            <Eye className="w-6 h-6 text-purple-400" />
          </div>
          <div className="min-w-0">
            <h3 className="text-lg font-semibold text-white truncate">Decision Log</h3>
            <p className="text-sm text-gray-500 truncate">
              {last24Hours.total > 0
                ? `${last24Hours.total} decisions in last 24h`
                : 'No recent decisions'}
            </p>
          </div>
        </div>
        <div className="flex items-center gap-2 shrink-0">
          <ProvenanceChip {...DECISION_PROVENANCE} align="end" />
          <Link
            to="/cos/learning"
            className="flex items-center gap-1 text-sm text-port-accent hover:text-port-accent/80 transition-colors min-h-[40px] px-2"
          >
            <span className="@max-xs:sr-only">Details</span>
            <ChevronRight size={16} />
          </Link>
        </div>
      </div>

      {/* Quick Stats Row */}
      <div className="flex flex-wrap gap-3 mb-4">
        {last24Hours.skipped > 0 && (
          <div className="flex items-center gap-1.5 px-2 py-1 rounded bg-port-warning/10 text-port-warning text-xs">
            <AlertCircle size={12} />
            <span>{last24Hours.skipped} skipped</span>
          </div>
        )}
        {last24Hours.switched > 0 && (
          <div className="flex items-center gap-1.5 px-2 py-1 rounded bg-purple-400/10 text-purple-400 text-xs">
            <ArrowRight size={12} />
            <span>{last24Hours.switched} switched</span>
          </div>
        )}
        {last24Hours.capacityFull > 0 && (
          <div className="flex items-center gap-1.5 px-2 py-1 rounded bg-orange-400/10 text-orange-400 text-xs">
            <Users size={12} />
            <span>{last24Hours.capacityFull} deferred</span>
          </div>
        )}
        {last24Hours.cooldownActive > 0 && (
          <div className="flex items-center gap-1.5 px-2 py-1 rounded bg-cyan-400/10 text-cyan-400 text-xs">
            <Pause size={12} />
            <span>{last24Hours.cooldownActive} cooldowns</span>
          </div>
        )}
        {last24Hours.selected > 0 && (
          <div className="flex items-center gap-1.5 px-2 py-1 rounded bg-port-success/10 text-port-success text-xs">
            <CheckCircle size={12} />
            <span>{last24Hours.selected} selected</span>
          </div>
        )}
        {last24Hours.adjusted > 0 && (
          <div className="flex items-center gap-1.5 px-2 py-1 rounded bg-blue-400/10 text-blue-400 text-xs">
            <Clock size={12} />
            <span>{last24Hours.adjusted} adjusted</span>
          </div>
        )}
      </div>

      {/* Impactful Decisions Section */}
      {impactfulDecisions.length > 0 && (
        <div className="border-t border-port-border pt-4">
          <button
            onClick={() => setExpanded(!expanded)}
            className="flex items-center justify-between w-full text-left mb-2 group"
          >
            <div className="flex items-center gap-2">
              <Zap size={14} className="text-purple-400" />
              <span className="text-sm font-medium text-gray-300">Recent Decisions</span>
              <span className="text-xs text-gray-500">
                (why tasks were skipped or changed)
              </span>
            </div>
            <ChevronDown
              size={16}
              className={`text-gray-400 transition-transform ${expanded ? 'rotate-180' : ''}`}
            />
          </button>

          {expanded && (
            <div className="space-y-2">
              {impactfulDecisions.map((decision) => {
                const style = getDecisionStyle(decision.type);
                const Icon = style.icon;

                return (
                  <div
                    key={decision.id}
                    className={`flex items-start gap-2 p-2 rounded-lg ${style.bg}`}
                  >
                    <Icon size={14} className={`${style.color} mt-0.5 shrink-0`} />
                    <div className="flex-1 min-w-0">
                      <div className="flex items-center gap-2">
                        <span className={`text-xs font-medium ${style.color}`}>
                          {formatDecisionType(decision.type)}
                        </span>
                      </div>
                      <p className="text-sm text-gray-300 truncate" title={decision.reason}>
                        {decision.reason}
                      </p>
                      {renderContextDetails(decision)}
                      <div className="text-xs text-gray-500 mt-0.5">
                        {timeAgo(decision.lastTimestamp || decision.timestamp)}
                        {(decision.count || 1) > 1 && (
                          <span className="ml-2 text-gray-400">
                            ({decision.count}x)
                          </span>
                        )}
                        {decision.context?.successRate !== undefined && (
                          <span className={`ml-2 ${
                            decision.context.successRate < 30 ? 'text-port-error' :
                            decision.context.successRate < 60 ? 'text-port-warning' : 'text-port-success'
                          }`}>
                            {decision.context.successRate}% success
                          </span>
                        )}
                      </div>
                    </div>
                  </div>
                );
              })}
            </div>
          )}

          {!expanded && (
            <div className="flex gap-1">
              {impactfulDecisions.slice(0, 5).map((decision) => {
                const style = getDecisionStyle(decision.type);
                return (
                  <div
                    key={decision.id}
                    className={`w-2 h-2 rounded-full ${style.color.replace('text-', 'bg-')}`}
                    title={`${formatDecisionType(decision.type)}: ${decision.reason}`}
                  />
                );
              })}
            </div>
          )}
        </div>
      )}

      {/* Transparency Score */}
      {transparencyScore !== undefined && (
        <div className="mt-3 pt-3 border-t border-port-border flex items-center justify-between text-xs">
          <span className="text-gray-500">Transparency score</span>
          <span className={`font-medium ${
            transparencyScore >= 90 ? 'text-port-success' :
            transparencyScore >= 70 ? 'text-port-warning' : 'text-port-error'
          }`}>
            {transparencyScore}%
          </span>
        </div>
      )}
    </div>
  );
});

export default DecisionLogWidget;