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

36.7 KB
Raw Blame
import {
  FileText,
  Cpu,
  Brain,
  Activity,
  Settings,
  Calendar,
  Clock,
  Compass,
  GraduationCap,
  Bot,
  Newspaper,
  ChartGantt,
  Play,
  ScrollText,
  MessageCircle
} from 'lucide-react';
import { normalizeReviewerSlug, REVIEWER_VALUES } from '../../lib/reviewerPins';
import { AVATAR_STYLE_LABELS } from '../../lib/avatarStyles';
import { inPlaceClipName } from '../../utils/animationClips';
import { getNavPageForPath, getPageNavTabs } from '../../../../server/lib/navManifest.js';
import { PR_REVIEW_MODES, DEFAULT_PR_REVIEW_MODE, DEFAULT_PR_COMPLETION } from '../../../../server/lib/prDisposition.js';
import { buildPageNavTabs } from '../../lib/pageNavTabs.js';

// Icon per tab id. The manifest (`tabGroup: 'cos'`) owns id/label/order — this
// file owns only how each tab looks. Throws at import time on drift.
const TAB_PRESENTATION = {
  briefing: { icon: Newspaper },
  tasks: { icon: FileText },
  agents: { icon: Cpu },
  jobs: { icon: Bot },
  runs: { icon: Play },
  'run-events': { icon: ScrollText },
  schedule: { icon: Clock },
  workflow: { icon: ChartGantt },
  digest: { icon: Calendar },
  gsd: { icon: Compass },
  learning: { icon: GraduationCap },
  memory: { icon: Brain },
  mind: { icon: MessageCircle },
  health: { icon: Activity },
  config: { icon: Settings },
};

export const TABS = buildPageNavTabs(getPageNavTabs('cos'), TAB_PRESENTATION, 'CoS');

// Intentional category-color enum (#1909/#1924 caution), NOT off-token theme
// inconsistency: 9 files (CoSCharacter, CyberCoSAvatar, EsotericCoSAvatar,
// MiniCharacterCoSAvatar, MuseCoSAvatar, NexusCoSAvatar, SigilCoSAvatar,
// StateLabel, TerminalCoSPanel) key their glow/fill/border color off `color`
// so all 7 agent states stay visually distinguishable at a glance. The app
// only has ~4-5 semantic tokens (accent/accent-2/success/warning/error) —
// collapsing 7 states onto them would make at least 2-3 states render
// identically, destroying the thing this enum exists for. Left as raw hex.
//
// Known issue (flagged on #1909 by codex review of PR #1935): `thinking`'s
// amber (#f59e0b) has poor contrast (~2.1:1) against light day-theme surfaces
// (e.g. Classic Noon) in the 2 consumers whose background is theme-aware
// (StateLabel's border, TerminalCoSPanel's ASCII art over --port-terminal-bg);
// against near-black surfaces (the default theme, and the fixed-dark 3D/SVG
// canvases the other 7 consumers render on) it's ~9.8:1, so the bug is
// day-theme-specific. A full per-theme-mode color swap was evaluated and
// deferred: 6 of the 9 consumers feed this value straight into three.js
// `<meshStandardMaterial color={...}>` props, which cannot resolve CSS custom
// properties (`var(--port-mood-thinking)`) — parity would need a resolved-hex
// lookup (via `getComputedStyle`) threaded through every 3D avatar, not just a
// CSS variable swap. See the follow-up discussion on #1909 for the scoped fix.
export const AGENT_STATES = {
  sleeping: { label: 'Sleeping', color: '#6366f1', icon: '💤' },
  thinking: { label: 'Thinking', color: '#f59e0b', icon: '🧠' },
  coding: { label: 'Coding', color: '#10b981', icon: '⚡' },
  investigating: { label: 'Investigating', color: '#ec4899', icon: '🔍' },
  reviewing: { label: 'Reviewing', color: '#8b5cf6', icon: '📋' },
  planning: { label: 'Planning', color: '#06b6d4', icon: '📐' },
  ideating: { label: 'Ideating', color: '#f97316', icon: '💡' },
};

// Cyber Muse (3D) avatar motion map. The bundled default model
// (data.reference/avatar/model.glb) is three.js's RobotExpressive (CC0), which
// ships 14 clips: Idle, Walking, Running, Dance, Death, Sitting, Standing,
// Jump, Yes, No, Wave, Punch, ThumbsUp, WalkJump.
//
// ONE map drives every state: `state → [{ clip, timeScale, loop }]`, an ordered
// list of steps. `loop` is
//   'infinite'   — loop the clip forever (never fires the mixer's `finished`)
//   'once'       — a single LoopOnce clamped on its final frame (a pose or
//                  transition clip like `Sitting`, held instead of looping)
//   { reps: N }  — a finite LoopRepeat of N cycles; `finished` then advances to
//                  the next step, wrapping around
//
// A length-1 list is a plain base loop. A longer list is a montage — `coding`
// cycles jab → sprint → leap → approve → stride → celebrate so a working agent
// reads as energetic and varied rather than one clip on repeat. THE FIRST STEP
// IS ALSO THE STATE'S FALLBACK: when the loaded GLB resolves fewer than two
// steps, `resolveMuseMotion` collapses the list to its first resolvable step and
// loops that — so "the montage degrades to its base clip" is structural, not a
// convention two maps had to agree on.
//
// Steps name real GLB clips. Walking / Running / WalkJump carry root
// translation (they move the model forward) and would drift a fixed-frame
// avatar out of view, so `resolveMuseMotion` auto-routes them to the
// neutralized in-place variants the avatar synthesizes at load time. A step's
// FIRST entry must still be an in-place clip (it is the fallback loop, played
// as named); the constants test asserts that.
//
// Consumed by MuseCoSAvatar's AnimationMixer (via drei useAnimations). Clip
// names are matched case-sensitively against the loaded GLB: unresolvable steps
// are dropped, a state with none left falls back to MUSE_ANIMATION_FALLBACK,
// and a GLB with NO clips at all falls back to the fully-procedural
// rotation/glow behavior so static models and other variants keep working.
//
// The emote clips (Yes/No/Wave/Punch/ThumbsUp) start and end near a neutral
// pose, so looping them reads as a repeated, deliberate gesture (nodding,
// scanning, jabbing) rather than snapping — that's what lets us give each state
// its own body language instead of collapsing everything onto Idle. The read
// for each: sleeping = seated rest; thinking = calm contemplation (Idle);
// coding = an energetic work montage; investigating = slow side-to-side scan
// (No); reviewing = approving nod (Yes); planning = confident "locked in"
// thumbs-up (ThumbsUp); ideating = creative celebration (Dance).
//
// The `speaking` boolean fires a one-shot gesture overlay
// (MUSE_SPEAKING_GESTURE) that hands control back to the current step when it
// finishes.
export const MUSE_STATE_MOTIONS = {
  sleeping:      [{ clip: 'Sitting',  timeScale: 0.8,  loop: 'once' }],
  thinking:      [{ clip: 'Idle',     timeScale: 0.85, loop: 'infinite' }],
  coding: [
    { clip: 'Punch',    timeScale: 1.2,  loop: { reps: 2 } },
    { clip: 'Running',  timeScale: 1.1,  loop: { reps: 4 } },
    { clip: 'Jump',     timeScale: 1.0,  loop: { reps: 1 } },
    { clip: 'ThumbsUp', timeScale: 0.95, loop: { reps: 1 } },
    { clip: 'Walking',  timeScale: 1.2,  loop: { reps: 4 } },
    { clip: 'Dance',    timeScale: 1.0,  loop: { reps: 1 } },
  ],
  investigating: [{ clip: 'No',       timeScale: 0.7,  loop: 'infinite' }],
  reviewing:     [{ clip: 'Yes',      timeScale: 0.8,  loop: 'infinite' }],
  planning:      [{ clip: 'ThumbsUp', timeScale: 0.85, loop: 'infinite' }],
  ideating:      [{ clip: 'Dance',    timeScale: 1.0,  loop: 'infinite' }],
};

// Naming suffix for the neutralized "run/walk in place" clip variants that
// MuseCoSAvatar synthesizes at load time (see `withInPlaceClips` in
// client/src/utils/animationClips.js). This is an INTERNAL implementation
// detail — the motion map above references real GLB clip names (`Running`) and
// `resolveMuseMotion` routes any root-motion clip to its stripped variant.
// Exported only so the avatar and the clip util agree on the suffix.
export const MUSE_IN_PLACE_SUFFIX = ' (in place)';

// Clip used when none of a state's mapped clips is in the loaded GLB.
export const MUSE_ANIMATION_FALLBACK = 'Idle';

// One-shot gesture played on the rising edge of `speaking`, then the avatar
// returns to the motion step it was on.
export const MUSE_SPEAKING_GESTURE = 'Wave';

// RobotExpressive clips that carry root translation (they walk the model
// forward). Never played as-is by the fixed-frame avatar: `resolveMuseMotion`
// routes a step naming one of these to its neutralized in-place variant, and
// the "GLB has clips but none are mapped" fallback skips them so a custom GLB
// whose first clip happens to be a walk cycle can't drift out of view.
export const MUSE_ROOT_MOTION_CLIPS = ['Walking', 'Running', 'WalkJump'];

// Last-resort clip for a GLB that has clips but none the state maps: prefer the
// canonical fallback, else the first in-place clip so a leading walk cycle can't
// drift the fixed-frame avatar, else whatever the GLB leads with.
const fallbackClip = (names) => (
  names.includes(MUSE_ANIMATION_FALLBACK)
    ? MUSE_ANIMATION_FALLBACK
    : names.find((n) => !MUSE_ROOT_MOTION_CLIPS.includes(n)) || names[0]
);

// Resolve a state's motion steps against `names`, the clip roster of the loaded
// GLB (which already includes the synthesized in-place variants). Root-motion
// steps are routed to their in-place variant, then any step the GLB can't
// supply is dropped. Returns the playable step list:
//   ≥2 steps → the montage, cycled by the avatar's `finished` listener
//    1 step  → a base loop; a finite `{ reps }` degrades to 'infinite' because
//              there is no next step to advance to, while an explicit 'once'
//              pose keeps clamping
//    0 steps → the state's first step re-pointed at a fallback clip, looped
// An empty/absent roster yields `[]` (the caller runs procedural-only).
export function resolveMuseMotion(state, names) {
  if (!Array.isArray(names) || names.length === 0) return [];
  const declared = MUSE_STATE_MOTIONS[state] || [];
  const resolved = declared
    .map((step) => ({ ...step, clip: inPlaceClipName(step.clip, MUSE_ROOT_MOTION_CLIPS, MUSE_IN_PLACE_SUFFIX) }))
    .filter((step) => names.includes(step.clip));
  if (resolved.length >= 2) return resolved;
  const step = resolved[0] || { ...declared[0], clip: fallbackClip(names) };
  return [{ ...step, loop: step.loop === 'once' ? 'once' : 'infinite' }];
}

// Default messages shown when no specific event message is available
export const STATE_MESSAGES = {
  sleeping: "Idle - waiting for tasks...",
  thinking: "Processing...",
  coding: "Working on task...",
  investigating: "Investigating issue...",
  reviewing: "Reviewing results...",
  planning: "Planning next steps...",
  ideating: "Analyzing options...",
};

// A health issue is the ONLY thing that flips an idle-but-running CoS into
// `investigating`, so the status bubble has to say *which* issue. Falling back
// to the generic STATE_MESSAGES line left the avatar parked on "Investigating
// issue..." with zero agents running and the detail reachable only by guessing
// at the Health tab. Returns null when there is nothing to report so callers
// can fall back to their own default. Issue shape mirrors the server's
// `runHealthCheck` (`{ type, category, message }` — server/services/cosHealthMonitor.js).
export const summarizeHealthIssues = (issues) => {
  if (!Array.isArray(issues) || issues.length === 0) return null;
  const messages = issues.map((issue) => issue?.message).filter(Boolean);
  // Counts come from `issues`, never from `messages` — a mixed list like
  // [{message:'A'}, {}] has two problems and one description, and counting the
  // descriptions would under-report it as a single issue.
  const plural = issues.length > 1 ? 's' : '';
  // A message-less issue still has to read as an issue — `null` means "nothing
  // to report", so callers that already know the list is non-empty never need a
  // fallback of their own.
  if (messages.length === 0) return `${issues.length} health issue${plural} detected`;
  if (issues.length === 1) return messages[0];
  return `${issues.length} health issues: ${messages.join(' · ')}`;
};

// Which of two health snapshots to keep. `getCosHealth` reads the *pre-check*
// persisted health while the same fetch batch triggers a fresh server-side check
// whose `cos:health:check` socket event can land first — so the slow read must
// not clobber the fresher socket result. Keep whichever check is newer by
// `lastCheck` (Date.parse normalizes the ISO timestamps so the compare never
// goes lexicographic), keep `prev` when the incoming read has no comparable
// timestamp but `prev` does, and treat a failed read (null) as "keep prev".
export const fresherHealth = (prev, next) => {
  if (!next) return prev ?? null;
  const prevT = Date.parse(prev?.lastCheck ?? '');
  const nextT = Date.parse(next.lastCheck ?? '');
  if (!Number.isNaN(prevT) && (Number.isNaN(nextT) || nextT < prevT)) return prev;
  return next;
};

// StatCard tone for the Issues tile. `error`-type issues mean something is
// broken; a warning-only check (e.g. a memory-hungry process) stays amber
// rather than screaming red — the same severity split HealthTab draws when it
// renders the list. 'default' (not null) so a zero-issue tile keeps its gray icon.
export const healthIssueTone = (issues) => {
  if (!Array.isArray(issues) || issues.length === 0) return 'default';
  return issues.some((issue) => issue?.type === 'error') ? 'critical' : 'warning';
};

// Agent option toggles for task metadata (useWorktree, openPR, simplify, requireApproval).
export const AGENT_OPTIONS = [
  { field: 'requireApproval', label: 'Require approval', shortLabel: 'Apr', description: 'Queue as awaiting-approve and do not auto-run — including Run Now — until you approve. Off (default): Run Now starts immediately; scheduled runs still follow the confidence and safety gates.' },
  { field: 'useWorktree', label: 'Worktree', shortLabel: 'WT', description: 'Work in an isolated git worktree on a feature branch. If unchecked, commits directly to the default branch.' },
  { field: 'openPR', label: 'Open PR', shortLabel: 'PR', description: 'Open a pull request to the default branch (implies worktree). Choose whether PortOS reviews and merges it, merges on green CI, or leaves it open. If unchecked with worktree enabled, auto-merges to the default branch on completion.' },
  { field: 'simplify', label: 'Run /simplify', shortLabel: '/s', description: 'Review code for reuse and quality before committing' }
];

export const PR_COMPLETION_OPTIONS = [
  { value: 'review-then-merge', label: 'Review then merge', description: 'PortOS automatically merges after configured reviewers and required checks pass for the current PR head. CI waits use the pending-merge coordinator without keeping an agent running.' },
  { value: 'merge-on-green', label: 'Merge on green CI', description: 'PortOS automatically merges after required checks pass for the current PR head, without external review. Repository admission checks still apply.' },
  { value: 'leave-open', label: 'Leave PR open', description: 'Open the PR and stop so you can inspect and merge it yourself.' },
  { value: 'draft', label: 'Leave a draft PR', description: 'Open a draft PR and leave it for you to review and merge.' }
];

export const prCompletionOption = (value) => PR_COMPLETION_OPTIONS.find(option => option.value === value);

export { DEFAULT_PR_COMPLETION };

// WHO reviews a PR action's change — orthogonal to `PR_COMPLETION_OPTIONS`
// above, which decides whether a review happens at all. The values come from
// the server leaf that owns the vocabulary (it imports nothing, so reaching for
// it costs the bundle nothing); only the UI copy lives here.
export const PR_REVIEW_MODE_OPTIONS = [
  {
    value: PR_REVIEW_MODES.DELEGATED,
    label: 'Delegated reviewers',
    description: 'The agent orchestrates your Code Review Defaults roster as separate reviewer runs.',
  },
  {
    value: PR_REVIEW_MODES.SELF,
    label: 'Self-review only',
    description: 'The Run with provider reviews the change itself — no child review agents.',
  },
];

export const prReviewModeOption = (value) => PR_REVIEW_MODE_OPTIONS.find(option => option.value === value);

export { PR_REVIEW_MODES, DEFAULT_PR_REVIEW_MODE };

// New scheduled PR tasks inherit the managed app's completion policy, falling
// back to reviewed automatic merge when that app has no explicit preference.
export const IMPLICIT_PR_COMPLETION = DEFAULT_PR_COMPLETION;

// Which option a stored taskMetadata pins, or '' when nothing does (inherit).
// Legacy `reviewLoop` records are the pre-`prCompletion` spelling of
// review-then-merge, so a control seeded from this shows what will actually
// happen rather than claiming to be unpinned. Client mirror of
// `resolvePrCompletion` in server/lib/prDisposition.js, minus its
// IMPLICIT_PR_COMPLETION floor — that floor is what "inherit" resolves to.
export function pinnedPrCompletion(metadata) {
  if (prCompletionOption(metadata?.prCompletion)) return metadata.prCompletion;
  if (metadata?.reviewLoop === true || metadata?.reviewLoop === 'true') return 'review-then-merge';
  return metadata?.reviewLoop === false || metadata?.reviewLoop === 'false' ? 'merge-on-green' : '';
}

// UI copy for each reviewer in the Review Loop picker. `copilot` requests a
// GitHub Copilot review via the native reviewer API; CLI reviewers
// (claude/antigravity/codex/grok/cursor/opencode/kilo/kimi) instruct the follow-up agent
// to invoke the named CLI; local-LLM reviewers (lmstudio/ollama/mtplx) route the
// diff through
// PortOS's `POST /api/code-review/local` endpoint, which runs the model
// configured on the Models → Code Reviewers page.
//
// Copy only — the ROSTER is `REVIEWER_VALUES` in `client/src/lib/reviewerPins.js`,
// which the server suite pins against the server's own enum.
const CODE_REVIEWERS_PAGE = getNavPageForPath('/models/code-reviewers')?.breadcrumb || 'its management page';
const REVIEWER_COPY = {
  pi: { label: 'Pi', description: 'Pi Coding Agent CLI reviews the supplied diff without tools' },
  kilo: { label: 'Kilo Code', description: `Kilo Code CLI (an OpenCode fork) reviews the PR diff (optional provider/model on ${CODE_REVIEWERS_PAGE})` },
  copilot: { label: 'Copilot', description: 'GitHub Copilot (GitHub-only)' },
  claude: { label: 'Claude', description: `Claude CLI reviews the PR diff (optional model on ${CODE_REVIEWERS_PAGE}; supports an Ollama-backed Claude for local-only setups)` },
  antigravity: { label: 'Antigravity', description: 'Antigravity CLI (agy) reviews the PR diff' },
  codex: { label: 'Codex', description: `Codex CLI reviews the PR diff (optional model tier on ${CODE_REVIEWERS_PAGE})` },
  grok: { label: 'Grok', description: 'Grok Build CLI (grok) reviews the PR diff' },
  cursor: { label: 'Cursor Agent', description: 'Cursor Agent CLI (cursor-agent) reviews the PR diff' },
  opencode: { label: 'OpenCode', description: `OpenCode CLI reviews the PR diff (optional provider/model on ${CODE_REVIEWERS_PAGE})` },
  kimi: { label: 'Kimi', description: `Kimi CLI reviews the PR diff (optional model on ${CODE_REVIEWERS_PAGE})` },
  lmstudio: { label: 'LM Studio', description: 'Local LM Studio model reviews the diff (set model on AI Providers)' },
  ollama: { label: 'Ollama', description: 'Local Ollama model reviews the diff (set model on AI Providers)' },
  mtplx: { label: 'MTPLX', description: 'Local MTPLX model reviews the diff (set model on AI Providers)' }
};

// Derived from the roster rather than mirroring it, so a reviewer added
// server-side surfaces as a loud module-load failure here instead of a silently
// missing dropdown row — and so the picker can never offer a slug the server's
// enum rejects.
export const REVIEWER_OPTIONS = REVIEWER_VALUES.map((value) => {
  const copy = REVIEWER_COPY[value];
  if (!copy) throw new Error(`❌ REVIEWER_COPY is missing UI copy for reviewer '${value}'`);
  return { value, ...copy };
});

// The display label for a reviewer token — the one place UI copy turns a
// reviewer slug into prose, so a roster addition renders everywhere without a
// literal edit. Resolves the `gemini` alias; an `@username` (or any token with no
// option row) falls through to itself.
export const reviewerLabel = (value) =>
  REVIEWER_OPTIONS.find(o => o.value === normalizeReviewerSlug(value))?.label || value;

// The whole reviewer vocabulary — the roster, its aliases and defaults, the
// review-username pattern and cap, the max-rounds ceiling, the stop-mode list,
// and the per-reviewer pin vocabularies — lives in `client/src/lib/reviewerPins.js`
// and is re-exported here so existing imports keep working. It is NOT defined in
// this file because the server suite pins the mirror against the server's own
// constants, and this module's `lucide-react` icon import isn't installed in that
// workspace — see the leaf module's header for the full rationale.
export {
  REVIEWER_VALUES,
  REVIEWER_ALIASES,
  DEFAULT_REVIEWER,
  DEFAULT_REVIEWERS,
  MAX_REVIEW_USERNAMES,
  cleanReviewUsername,
  normalizeReviewUsernames,
  MAX_REVIEWER_MAX_ROUNDS,
  REVIEW_STOP_MODES,
  DEFAULT_REVIEW_STOP_MODE,
  REVIEWER_OVERRIDE_KEYS,
  REVIEWER_LIST_OVERRIDE_KEYS,
  hasReviewerOverride,
  normalizeReviewers,
  MODEL_CAPABLE_CLI_REVIEWERS,
  LOCAL_LLM_REVIEWERS,
  MODEL_SELECTABLE_REVIEWERS,
  MAX_REVIEWER_MODEL_LENGTH,
  LOCAL_LLM_EFFORT_LEVELS,
  REVIEWER_EFFORT_LEVELS,
  EFFORT_SELECTABLE_REVIEWERS,
  reviewerEffortLevels,
  sanitizeReviewerModelInput
} from '../../lib/reviewerPins';

// pr-watcher owns trusted remediation. Legacy filter values remain accepted
// server-side for compatibility; every dispatch enforces collaborator trust.
export const PR_AUTHOR_FILTER_OPTIONS = [
  { value: 'trusted', label: 'Owner and write collaborators', description: 'Verified repository collaborators and the signed-in operator; external PRs use PR Reviewer' }
];

// claim-issue author gate (taskMetadata.issueAuthorFilter). Mirrors
// ISSUE_AUTHOR_FILTERS in server/lib/validation.js. 'self' = only claim issues
// YOU filed (the slashdo /do:next --self security boundary; the default);
// 'collaborators' = you plus everyone with repo/project access; 'owner' = only
// claim issues the repo owner filed; 'any' = claim any open issue. Listed
// narrowest-first so the dropdown reads as a widening scale.
export const ISSUE_AUTHOR_FILTER_OPTIONS = [
  { value: 'self', label: 'Filed by me only', description: 'Only claim open issues you filed (the /do:next --self security boundary — avoids acting on work embedded in a third party\'s issue)' },
  { value: 'collaborators', label: 'Me + collaborators', description: 'Claim open issues filed by you or by any account with access to the repo (GitHub collaborators / GitLab project members, including group-inherited access)' },
  { value: 'owner', label: 'Owner-filed only', description: 'Only claim open issues filed by the repository owner/creator' },
  { value: 'any', label: 'Any author', description: 'Claim the next eligible open issue regardless of who filed it' }
];

// Task types that claim from a forge issue tracker and therefore expose the
// issueAuthorFilter control. `claim-work` resolves to a concrete claim flow
// (github/gitlab) at dispatch but configures the filter here too. Add any new
// issue-claiming task type here rather than OR-ing literals across components.
export const ISSUE_AUTHOR_FILTER_TASK_TYPES = new Set(['claim-issue', 'claim-work']);

// Scheduled claim swarm fan-out (taskMetadata.swarmCount). 0 = off;
// 2..12 = parallel workers, matching SWARM_COUNT_MIN/MAX in cosValidation.js.
// Exposed on the same forge-issue task types as the author filter.
export const SWARM_TASK_TYPES = ISSUE_AUTHOR_FILTER_TASK_TYPES;
export const SWARM_COUNT_OPTIONS = [
  { value: 0, label: 'Off (one issue per run)', description: 'Claim and ship a single issue per scheduled run (default)' },
  ...Array.from({ length: 11 }, (_, index) => {
    const value = index + 2;
    return { value, label: `${value} in parallel`, description: `Claim and ship up to ${value} independent issues per run, merges serialized` };
  })
];

// branch-reconcile coordinator batch size (taskMetadata.branchesPerAgent).
// Keep this separate from claim-issue's swarm controls: one coordinator gets a
// prioritized branch batch, while issue claiming fans out one agent per issue.
export const BRANCHES_PER_AGENT_DEFAULT = 3;
export const BRANCHES_PER_AGENT_TASK_TYPES = new Set(['branch-reconcile']);
export const BRANCHES_PER_AGENT_OPTIONS = [1, 2, 3, 4, 5, 6].map((value) => ({
  value,
  label: `${value} branch${value === 1 ? '' : 'es'} per agent`,
  description: `Give each branch-reconcile coordinator up to ${value} prioritized branch${value === 1 ? '' : 'es'} per run`
}));

// error-log-triage run caps (taskMetadata integers). Values and defaults mirror
// ERROR_LOG_TRIAGE_LIMITS in server/lib/cosValidation.js; the server drops a
// value outside them.
export const LOG_TRIAGE_TASK_TYPES = new Set(['error-log-triage']);
export const LOG_TRIAGE_CAPS = [
  {
    field: 'triageMinOccurrences',
    label: 'Occurrences before investigating',
    defaultValue: 3,
    options: [1, 2, 3, 5, 10, 25].map((value) => ({ value, label: `${value} occurrence${value === 1 ? '' : 's'}` })),
    description: 'A new fault with no matching issue queues an investigation agent once it has been logged this many times',
  },
  {
    field: 'triageMaxInvestigations',
    label: 'Investigations per run',
    defaultValue: 2,
    options: [0, 1, 2, 3, 5, 10].map((value) => ({ value, label: value === 0 ? 'None (dedupe only)' : `Up to ${value}` })),
    description: 'Most investigation agents one pass may queue; the busiest new faults go first',
  },
];

// do-release options (taskMetadata booleans, each ON unless explicitly false).
// Fields mirror RELEASE_OPTION_KEYS in server/lib/cosValidation.js — the
// server drops any key outside that list.
export const RELEASE_OPTIONS_TASK_TYPES = new Set(['do-release']);
export const RELEASE_OPTIONS = [
  { field: 'finishInFlight', label: 'Finish in-flight work', description: 'Finish only in-flight fixes required by the frozen release candidate; leave unrelated PRs for the next release' },
  { field: 'mergeDependencyUpdates', label: 'Merge dependency updates', description: 'Include only dependency updates required to fix the frozen release candidate' },
  { field: 'resolveBlockers', label: 'Resolve blockers', description: 'Fix failing tests, red CI, and blocking bugs (delegating to a sub-agent) instead of stopping to report them' },
  { field: 'autoDecide', label: 'Make decisions', description: 'Make routine design and preference decisions and list them for your review, instead of stopping to ask' },
];

// The idle stall nudge (taskMetadata.stallNudge), ON unless explicitly false.
// Shown for every scheduled task; do-release ships with it off because its
// long quiet stretch is waiting on its own release PR's CI.
export const STALL_NUDGE_OPTION = {
  label: 'Stall nudge',
  description: 'After 10 quiet minutes, paste a reminder into the agent session to keep working. Turn off for tasks that wait a long time on work they started, such as CI on a release PR.',
};

// better-dependency-freedom's removal bar — the slashdo `/do:depfree` modes.
// Values mirror DEPFREE_MODES in server/lib/cosValidation.js; the server drops
// anything else, and an absent key means the default bar.
export const DEPFREE_MODE_DEFAULT = 'default';
export const DEPFREE_MODE_TASK_TYPES = new Set(['better-dependency-freedom']);
export const DEPFREE_MODE_OPTIONS = [
  { value: 'default', label: 'Default — small and wrapper packages', description: 'Keep large, widely audited libraries; replace micro-packages, native-API wrappers, and one-function imports' },
  { value: 'heavy', label: 'Heavy — everything but foundations', description: 'Only frameworks, runtimes, and core platform tooling are exempt; popularity does not protect a library' },
  { value: 'extreme', label: 'Extreme — toward zero dependencies', description: 'Only the runtime, security-sensitive protocols, native bindings, and CI-required tooling are exempt; large libraries are replaced when their used surface can be rebuilt behind parity tests. Pair with the strongest model at max effort' },
];
export const depfreeModeOption = (value) => DEPFREE_MODE_OPTIONS.find((o) => o.value === (value || DEPFREE_MODE_DEFAULT));

// Returns the Tailwind className string for an agent option toggle button.
// effective: whether the option is on (global + override resolved)
// hasOverride: whether there's an explicit per-app override set
export function agentOptionButtonClass(effective, hasOverride) {
  if (effective) {
    return hasOverride
      ? 'bg-port-accent text-white border-port-accent font-semibold'
      : 'bg-port-accent/40 text-port-accent border-port-accent/50 font-semibold';
  }
  return hasOverride
    ? 'bg-gray-700 text-gray-400 border-gray-500'
    : 'bg-transparent text-gray-600 border-gray-700/50';
}

// Normalize a per-app provider/model pin (`taskTypeOverrides[<taskType>].providerId`
// / `.model`) to the ONE shape every write site sends. '' and null both mean
// "inherit" — the route deletes the key for either — but three surfaces used to
// clear with three different values ('', null, and '' → null), so the same user
// action read as three different intents in the diff. Emitting null from all of
// them is what makes the stored result identical (#4783).
export function providerPinPatch(providerId, model) {
  return { providerId: providerId || null, model: model || null };
}

// Whether an app has pinned anything of its own for a task type (as opposed to
// inheriting the task's Schedule pin).
export function hasProviderPin(override) {
  return !!(override?.providerId || override?.model);
}

// Whether an app's per-task provider pin NAMES A DIFFERENT PROVIDER than the
// task's own Schedule pin. The app pin always wins at spawn (#4783), so this is
// the "silently overrides what the Schedule page shows" case worth flagging —
// not merely "an override exists" (hasProviderPin above): an app pin that
// happens to match the schedule, or a schedule with no pin of its own, isn't a
// surprise. Both sides must actually name a provider; an unset schedule pin
// (any override "diverges" from nothing) or an unset app pin (nothing to
// diverge) are not divergence.
export function providerPinDivergesFromSchedule(override, globalConfig) {
  return !!(override?.providerId && globalConfig?.providerId && override.providerId !== globalConfig.providerId);
}

// Compute new taskMetadata after toggling a field in a per-app override.
// Returns null when all overrides are cleared (inherit everything).
// Enforces invariant: openPR implies useWorktree (turning on openPR forces
// useWorktree on; turning off useWorktree forces openPR off).
export function toggleAppMetadataOverride(overrideMetadata, globalMetadata, field) {
  const current = overrideMetadata || {};
  const newMeta = { ...current };
  if (newMeta[field] !== undefined) {
    delete newMeta[field];
  } else {
    const effective = overrideMetadata?.[field] ?? globalMetadata?.[field] ?? false;
    newMeta[field] = !effective;
  }

  const resolve = (f) => newMeta[f] ?? globalMetadata?.[f] ?? false;

  // Enforce invariant: openPR implies useWorktree
  if (!resolve('useWorktree') && resolve('openPR')) {
    // useWorktree is effectively off but openPR is on — force openPR off
    newMeta.openPR = false;
  }
  if (resolve('openPR') && !resolve('useWorktree')) {
    // openPR on requires useWorktree — force useWorktree on
    newMeta.useWorktree = true;
  }

  // Clean entries that match the global value (revert to inherit)
  for (const key of Object.keys(newMeta)) {
    if (newMeta[key] === (globalMetadata?.[key] ?? false)) {
      delete newMeta[key];
    }
  }
  return Object.keys(newMeta).length ? newMeta : null;
}

export const MEMORY_TYPES = ['fact', 'learning', 'observation', 'decision', 'preference', 'context'];

// Intentional category-color enum (#1909/#1924 caution): a fixed 6-hue palette
// so each memory type reads as a distinct badge at a glance (fact vs learning
// vs observation etc.). Left as raw Tailwind hues rather than port-* tokens —
// the app only has ~4-5 semantic tokens (accent/accent-2/success/warning/error),
// which isn't enough to keep 6 categories visually distinct without collisions.
export const MEMORY_TYPE_COLORS = {
  fact: 'bg-blue-500/20 text-blue-400 border-blue-500/30',
  learning: 'bg-green-500/20 text-green-400 border-green-500/30',
  observation: 'bg-purple-500/20 text-purple-400 border-purple-500/30',
  decision: 'bg-orange-500/20 text-orange-400 border-orange-500/30',
  preference: 'bg-pink-500/20 text-pink-400 border-pink-500/30',
  context: 'bg-gray-500/20 text-gray-400 border-gray-500/30'
};

// Per-domain autonomy guardrails (#711). Mirrors server/lib/domainAutonomy.js —
// each domain is independently set to off | dry-run | execute. Default is
// `execute` (historical behavior). Keep this list in sync with the server.
export const AUTONOMY_DOMAINS = [
  { id: 'brain', label: 'Brain auto-classify', description: 'Auto-classify captured thoughts and file them.' },
  { id: 'memory', label: 'Memory auto-extract', description: 'Auto-store high-confidence memories from agent runs.' },
  { id: 'cos', label: 'CoS auto-run', description: 'Auto-spawn autonomous (non-user) tasks without approval.' },
  { id: 'messages', label: 'Messages auto-send', description: 'Auto-forward notifications to outbound channels (Telegram).' }
];

export const DOMAIN_AUTONOMY_MODES = [
  { id: 'off', label: 'Off', description: 'Never act automatically — leave it for manual action.' },
  { id: 'dry-run', label: 'Dry-run', description: 'Plan the action and surface it, but don\'t commit the side effect.' },
  { id: 'execute', label: 'Execute', description: 'Act automatically (default).' }
];

export const DEFAULT_DOMAIN_MODE = 'execute';

// Resolve a domain's mode from config, tolerating absent/partial config.
export const getDomainMode = (config, domainId) => {
  const candidate = config?.domainAutonomy?.[domainId];
  return DOMAIN_AUTONOMY_MODES.some(m => m.id === candidate) ? candidate : DEFAULT_DOMAIN_MODE;
};

// Per-domain daily autonomy budgets (#711). Mirrors server/lib/domainBudgets.js.
// Each domain caps autonomous work on two measurable dimensions; an empty/0 cap
// means unlimited. (No token/$ caps — CLI subscription providers expose no
// per-run metering, so a money/token cap couldn't be enforced honestly.)
export const DOMAIN_BUDGET_FIELDS = [
  { id: 'maxActionsPerDay', label: 'Actions/day', usageKey: 'actions' },
  { id: 'maxMinutesPerDay', label: 'Minutes/day', usageKey: 'minutes' }
];

// Coerce a cap to a positive integer or null (unlimited) — mirrors the server's
// normalizeBudgetLimit so the UI's "is a cap set?" view matches enforcement.
export const normalizeBudgetLimit = (value) => {
  const n = typeof value === 'number' ? value : Number(value);
  return Number.isFinite(n) && n > 0 ? Math.floor(n) : null;
};

// Resolve a domain's budget from config, tolerating absent/partial config.
export const getDomainBudget = (config, domainId) => {
  const b = config?.domainBudgets?.[domainId] || {};
  return {
    maxActionsPerDay: normalizeBudgetLimit(b.maxActionsPerDay),
    maxMinutesPerDay: normalizeBudgetLimit(b.maxMinutesPerDay)
  };
};

// Avatar style labels for display
export { AVATAR_STYLE_LABELS };

// Dynamic avatar rules - maps task context to avatar styles
// Priority order: provider > analysisType > taskType > priority > fallback
const DYNAMIC_AVATAR_RULES = {
  // Provider-based: different providers get distinct visual identities
  provider: {
    codex: 'esoteric',        // OpenAI Codex → mystical/ancient aesthetic
    'lm-studio': 'sigil',    // Local LM Studio → arcane/occult aesthetic
    'antigravity-cli': 'sigil', // Antigravity → arcane aesthetic
    'gemini-cli': 'sigil',      // Legacy Gemini configs → arcane aesthetic
  },
  // Improvement task analysis types → cyberpunk (system working on itself)
  analysisType: {
    security: 'cyber',
    'code-quality': 'cyber',
    'test-coverage': 'cyber',
    performance: 'cyber',
    'console-errors': 'cyber',
  },
  // Task analysis types
  taskType: {
    internal: 'sigil',        // Internal CoS tasks → arcane
  },
  // Priority-based: critical tasks get a distinctive look
  priority: {
    CRITICAL: 'esoteric',
  }
};

/**
 * Resolve which avatar style to display based on active agent metadata.
 * Returns null if no rule matches (caller should use configured default).
 */
export const resolveDynamicAvatar = (agentMetadata) => {
  if (!agentMetadata) return null;

  // Check provider rules first
  const providerId = agentMetadata.providerId || agentMetadata.provider;
  if (providerId && DYNAMIC_AVATAR_RULES.provider[providerId]) {
    return DYNAMIC_AVATAR_RULES.provider[providerId];
  }

  // Check analysis type (improvement tasks)
  const analysisType = agentMetadata.analysisType || agentMetadata.selfImprovementType;
  if (analysisType && DYNAMIC_AVATAR_RULES.analysisType[analysisType]) {
    return DYNAMIC_AVATAR_RULES.analysisType[analysisType];
  }

  // Check task type
  if (agentMetadata.taskType && DYNAMIC_AVATAR_RULES.taskType[agentMetadata.taskType]) {
    return DYNAMIC_AVATAR_RULES.taskType[agentMetadata.taskType];
  }

  // Check priority
  if (agentMetadata.priority && DYNAMIC_AVATAR_RULES.priority[agentMetadata.priority]) {
    return DYNAMIC_AVATAR_RULES.priority[agentMetadata.priority];
  }

  return null;
};