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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
import { useCallback } from 'react';
import { useSearchParams } from 'react-router';
import { AlertCircle, RefreshCw } from 'lucide-react';
import toast from '../../ui/Toast';
import * as api from '../../../services/api';
import { formatDateTime, formatTimeOfDaySeconds, timeAgo } from '../../../utils/formatters';
import Banner from '../../ui/Banner';
import { CodeReviewDefaultsProvider } from '../../../hooks/useCodeReviewDefaults';
import { useAppOverrideActions } from '../../../hooks/useAppOverrideActions';
import { useSocketResource } from '../../../hooks/useSocketResource';
import AppTaskTypeSection from './schedule/AppTaskTypeSection';
import TaskConfigDrawer from './schedule/TaskConfigDrawer';
import { TASK_FILTERS, DEFAULT_FILTER_ID, TASK_SORTS, DEFAULT_SORT_ID, suggestedOrderSteps } from './schedule/scheduleConstants';
const SCHEDULE_EVENTS = ['cos:schedule:changed'];
export function mergeUpdatedTaskInterval(schedule, taskType, interval, status) {
if (!schedule) return schedule;
return {
...schedule,
tasks: {
...(schedule.tasks || {}),
[taskType]: {
...(schedule.tasks?.[taskType] || {}),
...interval,
...(status !== undefined ? { status } : {}),
},
},
};
}
function mergeOnDemandRequest(schedule, request) {
if (!schedule || !request?.id) return schedule;
return {
...schedule,
onDemandRequests: [
...(schedule.onDemandRequests || []).filter(current => current.id !== request.id),
request,
],
};
}
// `providers` is owned by ChiefOfStaff (30s poll, see useAutoRefetch there) and
// passed down — same convention as TasksTab/AgentsTab — so this tab's provider/
// model pickers stay live without standing up a second independent poll of the
// same data.
export default function ScheduleTab({ apps, providers, providersLoaded, activeProviderId, daemonRunning }) {
const [searchParams, setSearchParams] = useSearchParams();
const { data: schedule, updateData: setSchedule, loading, error, refetch: fetchSchedule } = useSocketResource(
() => api.getCosSchedule({ silent: true }),
{ namespace: 'cos', events: SCHEDULE_EVENTS }
);
const label = searchParams.get('label') || '';
const setLabel = next => {
const params = new URLSearchParams(searchParams);
if (next) params.set('label', next);
else params.delete('label');
setSearchParams(params, { replace: true });
};
const filterParam = searchParams.get('filter');
const filter = TASK_FILTERS.some(f => f.id === filterParam) ? filterParam : DEFAULT_FILTER_ID;
const setFilter = useCallback((next) => {
const params = new URLSearchParams(searchParams);
if (next === DEFAULT_FILTER_ID) params.delete('filter');
else params.set('filter', next);
setSearchParams(params, { replace: true });
}, [searchParams, setSearchParams]);
// Card ordering rides the URL like its neighbours in the same control row, so
// a link to "the schedule in run order" survives a reload and can be shared.
const sortParam = searchParams.get('sort');
const sort = TASK_SORTS.some(option => option.id === sortParam) ? sortParam : DEFAULT_SORT_ID;
const setSort = useCallback((next) => {
const params = new URLSearchParams(searchParams);
if (next === DEFAULT_SORT_ID) params.delete('sort');
else params.set('sort', next);
setSearchParams(params, { replace: true });
}, [searchParams, setSearchParams]);
// The open config drawer is deep-linkable via ?task= per the URL-param convention.
const selectedTask = searchParams.get('task');
const setSelectedTask = useCallback((next) => {
const params = new URLSearchParams(searchParams);
if (next) params.set('task', next);
else params.delete('task');
setSearchParams(params, { replace: true });
}, [searchParams, setSearchParams]);
// Resolves a boolean so optimistic callers (the card's quick model pins) can
// roll their local selection back — this handler owns the error toast, so it
// never rejects. Stable identity: every card holds it through useTaskModelPins,
// so a fresh arrow per render would invalidate their memoized handlers.
const handleUpdateTask = useCallback(async (taskType, settings) => {
const result = await api.updateCosTaskInterval(taskType, settings, { silent: true }).catch(err => {
toast.error(err.message);
return null;
});
if (!result?.success) return false;
// Not "interval" — this handler carries every task setting (provider, model,
// effort, prompt, dependencies), and the card's pins now use it too.
toast.success(`Updated ${taskType}`);
// Apply the authoritative response before resolving so a rapid second edit
// reads the value that was just persisted instead of overwriting it from a
// stale render while a background refetch is still in flight.
setSchedule(current => mergeUpdatedTaskInterval(current, taskType, result.interval, result.status));
return true;
}, [setSchedule]);
const handleTriggerTask = useCallback(async (taskType, appId = null) => {
const result = await api.triggerCosOnDemandTask(taskType, appId, { silent: true }).catch(err => {
toast.error(err.message);
return null;
});
if (!result?.success) return null;
const appName = appId ? apps?.find(app => app.id === appId)?.name || 'selected app' : null;
const queuedMsg = `Queued ${taskType} request${appName ? ` for ${appName}` : ''}`;
if (daemonRunning === false) {
toast.error(`${queuedMsg} — but the CoS daemon is stopped, so it will not run until you start it`);
} else {
toast.success(`${queuedMsg} — it will appear in Tasks when evaluation begins`);
}
// The POST returns the persisted request. Paint it immediately instead of
// waiting for a second round trip; the evaluator may drain it into Tasks
// before that GET returns, while RunTaskButton retains the sent receipt at
// the click locus either way.
if (result.request) {
setSchedule(current => mergeOnDemandRequest(current, result.request));
}
fetchSchedule();
return result.request || true;
}, [apps, fetchSchedule, daemonRunning, setSchedule]);
const handleTriggerAppImprovement = handleTriggerTask;
const { handleUpdateOverride, handleBulkToggleOverride } = useAppOverrideActions(apps, fetchSchedule);
if (loading) {
return (
<div className="flex items-center justify-center h-64">
<div className="text-gray-500">Loading schedule...</div>
</div>
);
}
if (!schedule) {
return (
<div className="text-center py-8 text-gray-500">
<AlertCircle className="w-12 h-12 mx-auto mb-3 opacity-50" />
<p>Failed to load task schedule</p>
<button type="button" onClick={fetchSchedule} className="mt-3 underline">Retry</button>
</div>
);
}
const improvementDisabled = schedule.improvementEnabled === false;
const tasks = schedule.tasks || schedule.appImprovement || schedule.selfImprovement || {};
const allTaskTypes = Object.keys(tasks);
const selectedConfig = selectedTask ? tasks[selectedTask] : null;
// The drawer's header shows the same advisory step the card does, so it is
// ranked from the same graph rather than from the one open task.
const selectedOrderStep = selectedTask ? suggestedOrderSteps(tasks)[selectedTask] : undefined;
return (
<CodeReviewDefaultsProvider>
<div className="space-y-4">
<div className="flex items-center justify-between gap-3 flex-wrap">
<div className="flex items-baseline gap-2 flex-wrap min-w-0">
<h2 className="text-xl font-semibold text-white">Task Schedule</h2>
<p className="text-sm text-gray-400">Configure how often each task type runs.</p>
</div>
<button
onClick={fetchSchedule}
className="flex items-center gap-2 px-3 py-1.5 text-sm bg-port-border hover:bg-port-border/80 text-white rounded transition-colors"
title="Refresh schedule"
>
<RefreshCw size={14} />
Refresh
</button>
</div>
{error && (
<Banner tone="error" icon={AlertCircle} title="Schedule unavailable — showing the last loaded schedule">
{error.message || String(error)}
</Banner>
)}
{improvementDisabled && (
<Banner size="md" icon={AlertCircle} title="Improvement is disabled">
<div className="text-xs text-port-warning/80 mt-1">
No scheduled or on-demand improvement tasks will run. Enable the <span className="font-mono">Improve</span> toggle in
{' '}<a href="/cos/config" className="underline hover:text-port-warning">CoS → Config</a> to use this page.
</div>
</Banner>
)}
{schedule.onDemandRequests?.length > 0 && (
<Banner
tone={daemonRunning === false ? 'warning' : 'info'}
size="lg"
title="Pending On-Demand Tasks"
>
{daemonRunning === false && (
<div className="text-sm mb-2">
The CoS daemon is stopped, so these requests will not run until it's started — use the Start button above.
</div>
)}
<div className="space-y-1 mt-2">
{schedule.onDemandRequests.map(req => (
<div key={req.id} className="text-sm text-gray-300">
{req.taskType}{req.appId ? ` (${apps?.find(app => app.id === req.appId)?.name || req.appId})` : ''} - requested {formatTimeOfDaySeconds(req.requestedAt)} ({timeAgo(req.requestedAt)})
</div>
))}
</div>
</Banner>
)}
<AppTaskTypeSection
tasks={tasks}
apps={apps}
providers={providers}
providersLoaded={providersLoaded}
activeProviderId={activeProviderId}
onTrigger={handleTriggerAppImprovement}
onUpdate={handleUpdateTask}
onSelectTask={setSelectedTask}
improvementDisabled={improvementDisabled}
filter={filter}
label={label}
onLabelChange={setLabel}
onFilterChange={setFilter}
sort={sort}
onSortChange={setSort}
/>
{schedule.lastUpdated && (
<div className="text-xs text-gray-500 text-right">
Schedule last updated: {formatDateTime(schedule.lastUpdated)}
</div>
)}
<TaskConfigDrawer
open={!!selectedConfig}
taskType={selectedTask}
config={selectedConfig}
onClose={() => setSelectedTask(null)}
onUpdate={handleUpdateTask}
onTrigger={handleTriggerAppImprovement}
providers={providers}
providersLoaded={providersLoaded}
activeProviderId={activeProviderId}
apps={apps}
onUpdateOverride={handleUpdateOverride}
onBulkToggleOverride={handleBulkToggleOverride}
allTaskTypes={allTaskTypes}
orderStep={selectedOrderStep}
improvementDisabled={improvementDisabled}
dataInputCatalog={schedule.dataInputCatalog || []}
/>
</div>
</CodeReviewDefaultsProvider>
);
}