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

3.7 KB
Raw Blame
/**
 * Generic "Queue agent to investigate" action (#5981) for any UI-detected
 * failure that should hand a reproducible task to the CoS queue.
 *
 * The caller builds the task (`{ description, prompt }`, e.g. via
 * `buildInstallFailureTask` or `buildSelectorTestFailureTask`) — this
 * component owns everything downstream: posting it with the durable
 * `isInvestigation` marker, handling the store's 409 `DUPLICATE_TASK` as an
 * "already queued" state rather than an error, and the three button states.
 * `QueueInstallInvestigationButton` is a thin wrapper of this for its
 * existing installer-failure call sites.
 */

import { useState } from 'react';
import { Bot, Check } from 'lucide-react';
import { useAsyncAction } from '../../hooks/useAsyncAction';
import { addCosTask } from '../../services/api';
import toast from './Toast';

// The task's identity as an investigation (#6043) is the only posture flag sent:
// the server recognizes `isInvestigation`, derives the dedup fingerprint itself,
// and applies `CLIENT_INVESTIGATION_DELIVERY` (`server/lib/investigationTasks.js`)
// — worktree-isolated, PR-gated, reviewed rather than merged on green because a
// human asked for this one.
const INVESTIGATION_MARKER = { isInvestigation: true };

// `task.label` is presentation only (e.g. "Resolve with agent"); it is never posted.
export default function QueueInvestigationButton({ task: { label, ...task }, className = '' }) {
  // null = not queued yet · 'queued' = this click created the task ·
  // 'duplicate' = the store already held one. They read differently to the user:
  // the store's 409 also fires for a task it has BLOCKED, which will not run
  // without intervention, so this must not claim an agent is on it.
  const [queueResult, setQueueResult] = useState(null);
  // `useAsyncAction` owns the failure toast, so the request itself is silent —
  // otherwise the user gets two toasts for one failed queue.
  const [queueTask, queueing] = useAsyncAction(async () => {
    // The description is deterministic per failure, so retrying the same
    // failure and clicking again hits the store's duplicate guard (409
    // DUPLICATE_TASK). A task already exists — not a failure to report.
    const duplicateMessage = await addCosTask({ ...task, ...INVESTIGATION_MARKER }, { silent: true })
      .then(() => null)
      .catch((err) => {
        // The server names the existing task's status ("already pending" /
        // "already blocked"); pass it through rather than guessing.
        if (err?.code === 'DUPLICATE_TASK') return err.message || 'A task for this failure already exists';
        throw err;
      });
    setQueueResult(duplicateMessage ? 'duplicate' : 'queued');
    if (duplicateMessage) toast(duplicateMessage, { icon: '🤖' });
    else toast.success('Queued an agent to investigate this issue');
  }, { errorMessage: 'Failed to queue the investigation task' });

  return (
    <button
      type="button"
      onClick={queueTask}
      disabled={queueing || queueResult !== null}
      title={queueResult === 'queued'
        ? 'Queued — track it in Agent Ops'
        : queueResult === 'duplicate'
          ? 'A CoS task for this failure already exists — see Agent Ops'
          : 'Queue a PortOS agent task to investigate this failure'}
      className={`flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium transition-colors bg-port-accent text-white hover:bg-port-accent/80 disabled:opacity-50 disabled:hover:bg-port-accent ${className}`}
    >
      {queueResult ? <Check size={14} /> : <Bot size={14} />}
      {queueResult === 'queued'
        ? 'Agent queued'
        : queueResult === 'duplicate'
          ? 'Task already exists'
          : queueing ? 'Queueing…' : (label || 'Queue agent to investigate')}
    </button>
  );
}