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
fix: explain CoS delivery waits and show live recovery progress (#13475)
[._.]/1fe87336d77b
diff --git a/client/src/components/cos/DeliveryStatus.jsx b/client/src/components/cos/DeliveryStatus.jsxnew file mode 100644index 0000000000..76e5754e47--- /dev/null+++ b/client/src/components/cos/DeliveryStatus.jsx@@ -0,0 +1,53 @@+import { Link } from 'react-router';+import { Clock, Loader2 } from 'lucide-react';+import { formatCount, formatDateTime, formatDurationMs } from '../../utils/formatters';++const HOLD_REASONS = {+ 'cleanup-already-warned': 'A previous cleanup attempt left warnings; waiting for a fresh delivery and preservation check.',+ 'already-absent-preservation-unverified': 'The workspace is gone; its preservation evidence still needs verification.',+ 'cognitive-review-pending': 'Waiting for the cleanup recovery task to review preservation and finish cleanup.',+ 'active-agent-owner': 'Waiting for the current workspace owner to finish.',+};++export function DeliveryRecoverySchedule({ recovery }) {+ return <div className="px-3 py-2 text-sm text-port-text-muted space-y-1">+ <p>Agent execution has ended. These tasks are waiting for delivery verification or workspace cleanup.</p>+ <p>{!recovery ? 'Automatic recovery schedule unavailable.'+ : recovery.blockedReason ? `Automatic recovery is waiting: ${recovery.blockedReason}.`+ : recovery.nextRunAt ? `Next recovery sweep: ${formatDateTime(recovery.nextRunAt)}.${recovery.nextRunAt <= Date.now() ? ' Scheduled time reached; awaiting scheduler confirmation.' : ''}`+ : 'The recovery sweep has no confirmed next run time.'}</p>+ {recovery?.intervalMs && <p>When enabled, checks run every {formatDurationMs(recovery.intervalMs)}, up to {formatCount(recovery.batchSize)} jobs per sweep. A sweep may leave a job waiting if its dependency is unresolved; completion time is unknown.</p>}+ </div>;+}++export default function DeliveryStatus({ agent, ownerId }) {+ const metadata = agent?.metadata;+ const cleanup = metadata?.cleanupOutcome;+ const verification = metadata?.repoStateVerification;+ const activity = agent?.deliveryActivity;+ const reason = cleanup?.reason || verification?.reasons?.join(', ') || metadata?.completionCleanup?.reason;+ const checkedAt = verification?.checkedAt || cleanup?.observedAt;+ const stages = Object.values(metadata?.completionCleanup?.stages || {});+ const recoveries = [...new Set([cleanup?.taskId,+ ...Object.values(metadata?.repoCleanupRecoveries || {}).map(entry => entry.taskId)].filter(Boolean))];+ const id = agent?.id || ownerId;++ return <div className="mb-2 space-y-1 text-sm text-port-text-muted [overflow-wrap:anywhere]" aria-label="Delivery activity">+ <p className="flex items-center gap-2 text-port-warning">+ {activity ? <Loader2 size={14} aria-hidden="true" className="animate-spin motion-reduce:animate-none shrink-0" />+ : <Clock size={14} aria-hidden="true" className="shrink-0" />}+ {activity ? activity.label : 'Waiting for delivery recovery — agent is not running'}+ </p>+ <p>{reason ? `Last check: ${HOLD_REASONS[reason] || `${reason.replaceAll('-', ' ')}.`}` : 'No delivery check result is available yet.'}</p>+ {checkedAt && <p>Last checked: {formatDateTime(checkedAt)}</p>}+ {agent?.result?.taskOutcome?.reason && <details>+ <summary className="cursor-pointer">Agent handoff: what remains</summary>+ <p className="whitespace-pre-wrap">{agent.result.taskOutcome.reason}</p>+ </details>}+ {stages.length > 0 && <p>Completion steps confirmed: {formatCount(stages.filter(stage => stage.status === 'done').length)} of {formatCount(stages.length)}</p>}+ {cleanup?.owners?.length > 0 && <p>Waiting for current owners to finish: {cleanup.owners.map((owner, index) =>+ <span key={owner}>{index > 0 && ', '}<Link className="underline" to={`/cos/agents/${encodeURIComponent(owner)}`}>{owner}</Link></span>)}</p>}+ {recoveries.map(taskId => <p key={taskId}><Link className="text-port-accent underline" to={`/cos/tasks?task=${encodeURIComponent(taskId)}`}>View recovery task {taskId}</Link> — its current status determines when recovery can continue.</p>)}+ {id && <Link className="inline-block text-port-accent underline" to={`/cos/agents/${encodeURIComponent(id)}`}>View agent report and execution log</Link>}+ </div>;+}diff --git a/client/src/components/cos/tabs/AgentCard.jsx b/client/src/components/cos/tabs/AgentCard.jsxindex 002395e4f5..6a2249ff5c 100644--- a/client/src/components/cos/tabs/AgentCard.jsx+++ b/client/src/components/cos/tabs/AgentCard.jsx@@ -33,6 +33,7 @@ import { copyToClipboard } from '../../../lib/clipboard';import { estimateCosDuration } from '../../../lib/cosDurationEstimate';import { isAgentFeedbackUpdateTarget, isSystemAgent as isSystemAgentRecord } from '../../../lib/cosAgentFeedback';import AgentResultLine, { SecurityScanEvidence } from '../AgentResultLine';+import DeliveryStatus from '../DeliveryStatus';import AuditSourceEvidence from '../AuditSourceEvidence';import { DEFAULT_REVIEWER, normalizeReviewers } from '../constants';import { formatBytes, formatCount, formatDurationMs, formatDateTime, formatTimeOfDay, shortClaudeModelName } from '../../../utils/formatters';@@ -735,7 +736,7 @@ export default function AgentCard({ agent, onPause, onKill, onDelete, onResume,return (<div className={`min-w-0 bg-port-card border rounded-lg overflow-hidden ${completed && expanded ? 'col-span-full' : ''} ${- inactive+ agent.deliveryPending ? 'border-port-warning/40' : inactive? isSystemAgent ? 'border-port-border opacity-50' : 'border-port-border opacity-75': 'border-port-accent/50'}`}>@@ -1133,6 +1134,7 @@ export default function AgentCard({ agent, onPause, onKill, onDelete, onResume,/><AuditSourceEvidence evidence={agent.metadata?.auditSourceEvidence} />+ {agent.deliveryPending && <DeliveryStatus agent={agent} />}{!agent.result && <SecurityScanEvidence scanner={agent.metadata?.securityScan} control={agent.metadata?.securityScanControl} />}diff --git a/client/src/components/cos/tabs/AgentsTab.jsx b/client/src/components/cos/tabs/AgentsTab.jsxindex 4a509fe79f..cd3f755804 100644--- a/client/src/components/cos/tabs/AgentsTab.jsx+++ b/client/src/components/cos/tabs/AgentsTab.jsx@@ -15,6 +15,7 @@ import InlineConfirmRow from '../../ui/InlineConfirmRow';import { agentResumeMessage } from '../../../lib/agentResumeOutcome';import { isAgentFeedbackEligible } from '../../../lib/cosAgentFeedback';import { formatCount } from '../../../utils/formatters';+import { DeliveryRecoverySchedule } from '../DeliveryStatus';// What each `resumeAgent` outcome actually did (server modes, agentManagement.js).// `already-active` and `superseded` deliberately queue NOTHING — the task is already@@ -39,7 +40,7 @@ const RESUME_MESSAGES = {const needsAgentFeedback = isAgentFeedbackEligible;const FEEDBACK_EVENTS = ['cos:agent:feedback', 'cos:agent:completed'];-export default function AgentsTab({ agentsLoaded = true, agentsError = null, onRetryAgents, completedRevision = 0, agents, onRefresh, liveOutputs, providers, providersLoaded, apps }) {+export default function AgentsTab({ completionRecovery, agentsLoaded = true, agentsError = null, onRetryAgents, completedRevision = 0, agents, onRefresh, liveOutputs, providers, providersLoaded, apps }) {const { agentId } = useParams();const mounted = useMounted();const [deletedIds, setDeletedIds] = useState(() => new Set());@@ -310,7 +311,7 @@ export default function AgentsTab({ agentsLoaded = true, agentsError = null, onR{finalizingAgents.length > 0 && (<section aria-label="Finalizing delivery" className="space-y-2"><h3 className="text-lg font-semibold text-white">Finalizing delivery</h3>- <p className="text-sm text-gray-400">Awaiting delivery verification and workspace cleanup.</p>+ <DeliveryRecoverySchedule recovery={completionRecovery} />{finalizingAgents.map(agent => (<div key={agent.id} className="space-y-1"><Link to={`/cos/tasks?task=${encodeURIComponent(agent.taskId)}`} className="inline-block text-sm text-port-accent hover:underline [overflow-wrap:anywhere]">diff --git a/client/src/components/cos/tabs/TaskItem.jsx b/client/src/components/cos/tabs/TaskItem.jsxindex 261b9347d3..317f0e2941 100644--- a/client/src/components/cos/tabs/TaskItem.jsx+++ b/client/src/components/cos/tabs/TaskItem.jsx@@ -36,6 +36,7 @@ import EffortSelect from '../EffortSelect';import RelaunchAgentModal from './RelaunchAgentModal';import AgentCard from './AgentCard';import PreflightProgress from './PreflightProgress';+import DeliveryStatus from '../DeliveryStatus';import { isDeliveryCleanupHeld } from '../../../../../server/lib/taskRetryHold.js';const statusIcons = {@@ -446,7 +447,7 @@ export default function TaskItem({ task, agent = null, liveOutput, isSystem, spa{statusIcons[displayStatus]}</button><div className="flex-1 min-w-0">- {finalizing && <p className="mb-1 text-sm text-gray-400">Agent execution ended. Delivery verification and cleanup are pending.</p>}+ {finalizing && <DeliveryStatus agent={agent} ownerId={task.metadata.deliveryRecoveryAgentId} />}<div className="flex items-center gap-2 mb-1 flex-wrap"><span className="text-sm font-mono text-gray-500 min-w-0 break-words">{task.id}</span>{task.metadata?.app && apps?.find(a => a.id === task.metadata.app)?.name && (diff --git a/client/src/components/cos/tabs/TasksTab.jsx b/client/src/components/cos/tabs/TasksTab.jsxindex 945231afac..5ea2db9952 100644--- a/client/src/components/cos/tabs/TasksTab.jsx+++ b/client/src/components/cos/tabs/TasksTab.jsx@@ -28,6 +28,7 @@ import { formatCount } from '../../../utils/formatters';import { runningAgentsByTaskId, isSpawningTask } from '../../../lib/cosSpawnWindow';import BrailleSpinner from '../../BrailleSpinner';import { isDeliveryCleanupHeld } from '../../../../../server/lib/taskRetryHold.js';+import { DeliveryRecoverySchedule } from '../DeliveryStatus';// Maps a task-section status → micrographic glyph spec. Animation only on// states where motion communicates real work happening (running tasks).@@ -44,7 +45,7 @@ function SectionGlyph({ status }) {return <MicroGlyph variant={spec.variant} state={spec.state} animated={spec.animated} size={13} />;}-export default function TasksTab({ completedRevision = 0, tasks, agents = [], liveOutputs = {}, onRefresh, onTaskAdded, onTaskUnblocked, onTaskDeleted, providers, providersLoaded, apps }) {+export default function TasksTab({ completionRecovery, completedRevision = 0, tasks, agents = [], liveOutputs = {}, onRefresh, onTaskAdded, onTaskUnblocked, onTaskDeleted, providers, providersLoaded, apps }) {const [searchParams] = useSearchParams();const [userTasksLocal, setUserTasksLocal] = useState([]);const [durations, setDurations] = useState(null);@@ -111,6 +112,7 @@ export default function TasksTab({ completedRevision = 0, tasks, agents = [], li// socket agent feed, which lands ~400ms ahead of the next task refetch; the// map is what lets an Active row offer relaunch without its own agent fetch.const runningAgentByTaskId = useMemo(() => runningAgentsByTaskId(agents), [agents]);+ const agentById = useMemo(() => new Map(agents.map(agent => [agent.id, agent])), [agents]);const isSpawning = useCallback((task) => isSpawningTask(task, runningAgentByTaskId),@@ -251,9 +253,11 @@ export default function TasksTab({ completedRevision = 0, tasks, agents = [], li<h3 className="px-3 py-2 text-sm font-medium text-gray-400 border-b border-port-border">Finalizing delivery ({formatCount(rows.length)})</h3>+ <DeliveryRecoverySchedule recovery={completionRecovery} /><div className="p-2 space-y-1.5">{rows.map(task => (<TaskItem key={task.id} task={task} isSystem={source === 'internal'} selected={isTaskSelected(task, source)}+ agent={agentById.get(task.metadata.deliveryRecoveryAgentId)}onRefresh={onRefresh} onTaskUnblocked={onTaskUnblocked} onTaskDeleted={handleTaskDeleted}providers={providers} providersLoaded={providersLoaded} durations={durations} apps={apps} instances={assignableInstances} />))}diff --git a/client/src/components/cos/tabs/TasksTab.test.jsx b/client/src/components/cos/tabs/TasksTab.test.jsxindex a084a54487..5af9879a72 100644--- a/client/src/components/cos/tabs/TasksTab.test.jsx+++ b/client/src/components/cos/tabs/TasksTab.test.jsx@@ -78,6 +78,33 @@ describe('TasksTab spawning window', () => {expect(screen.queryByRole('region', { name: /finalizing delivery/ })).toBeNull();});+ it('shows the exact cleanup owner, last check and retry gate, and animates only live recovery', async () => {+ const task = { id: 'task-held', description: 'Deliver example change', status: 'in_progress',+ metadata: { deliveryRecovery: 'cleanup', deliveryRecoveryAgentId: 'agent-owner' } };+ const owner = { id: 'agent-owner', taskId: task.id, status: 'completed', metadata: {+ repoStateVerification: { checkedAt: '2026-01-01T12:00:00Z', reasons: ['cleanup-already-warned'] },+ cleanupOutcome: { status: 'blocked', reason: 'preservation-unverified', taskId: 'task-recovery' },+ }, result: { taskOutcome: { reason: 'The archive still needs verification.' } } };+ const view = activity => <MemoryRouter><TasksTab tasks={{ user: { tasks: [task] }, cos: { tasks: [] } }}+ agents={[{ ...owner, deliveryActivity: activity }, { id: 'agent-other', taskId: task.id, status: 'completed' }]}+ completionRecovery={{ blockedReason: 'CoS is paused', nextRunAt: null, intervalMs: 1800000, batchSize: 10 }}+ onRefresh={vi.fn()} providers={[]} apps={[]} /></MemoryRouter>;+ const { rerender } = render(view(null));+ expect(await screen.findByText(/Automatic recovery is waiting: CoS is paused/)).toBeInTheDocument();+ expect(screen.getByText(/Waiting for delivery recovery — agent is not running/)).toBeInTheDocument();+ expect(screen.getByText('Last check: preservation unverified.')).toBeInTheDocument();+ expect(screen.getByText(/Last checked:/)).toBeInTheDocument();+ expect(screen.getByRole('link', { name: /View recovery task/ })).toHaveAttribute('href', '/cos/tasks?task=task-recovery');+ expect(screen.getByRole('link', { name: /View agent report/ })).toHaveAttribute('href', '/cos/agents/agent-owner');+ expect(screen.getByLabelText('Delivery activity').querySelector('.animate-spin')).toBeNull();+ rerender(view({ label: 'Checking repository preservation' }));+ expect(screen.getByText('Checking repository preservation')).toBeInTheDocument();+ expect(screen.getByLabelText('Delivery activity').querySelector('.animate-spin')).not.toBeNull();+ rerender(view(null));+ expect(screen.getByText(/agent is not running/)).toBeInTheDocument();+ expect(screen.getByLabelText('Delivery activity').querySelector('.animate-spin')).toBeNull();+ });+it('moves a pending task with a live agent out of Pending and into Active', async () => {renderTab({tasks: { user: { tasks: [pendingTask('task-spawning'), pendingTask('task-waiting')] }, cos: { tasks: [] } },diff --git a/client/src/pages/ChiefOfStaff.jsx b/client/src/pages/ChiefOfStaff.jsxindex 41a7540bee..9426be2a5a 100644--- a/client/src/pages/ChiefOfStaff.jsx+++ b/client/src/pages/ChiefOfStaff.jsx@@ -597,7 +597,8 @@ export default function ChiefOfStaff() {const handleAgentUpdated = (updatedAgent) => {if (!needsAgents) return;- agentReadRef.current?.updates.set(updatedAgent.id, updatedAgent);+ const updates = agentReadRef.current?.updates;+ updates?.set(updatedAgent.id, { ...updates.get(updatedAgent.id), ...updatedAgent });// Update the specific agent in the agents list without fetching all datasetAgents(prev => prev.map(agent =>agent.id === updatedAgent.id ? { ...agent, ...updatedAgent } : agent@@ -672,6 +673,10 @@ export default function ChiefOfStaff() {}};socket.on('cos:log', handleCosLog);+ const handleRecoveryScheduleChanged = event => {+ if (needsAgents && event?.id === 'cos-pending-merge-sweep') refreshQueue();+ };+ socket.on('cos:scheduler:changed', handleRecoveryScheduleChanged);// Stable reference data has its own invalidation path, independent of the queue.const handleAppsChanged = coalesce(() => {@@ -704,6 +709,7 @@ export default function ChiefOfStaff() {socket.off('cos:agent:completed', handleAgentCompleted);socket.off('cos:health:check', handleHealthCheck);socket.off('cos:log', handleCosLog);+ socket.off('cos:scheduler:changed', handleRecoveryScheduleChanged);socket.off('apps:changed', handleAppsChanged);refreshQueue.cancel();};@@ -1449,14 +1455,14 @@ export default function ChiefOfStaff() {<div role="tabpanel" id="tabpanel-tasks" aria-labelledby="tab-tasks"><ActionableInsightsBanner insights={insights} onTaskUnblocked={handleTaskUnblocked} onRefresh={fetchData} /><Suspense fallback={<TabLoadFallback label="tasks" />}>- <TasksTab completedRevision={taskHistoryRevision} tasks={tasks} agents={agents} liveOutputs={liveOutputs} onRefresh={fetchData} onTaskAdded={handleUserTaskAdded} onTaskUnblocked={handleTaskUnblocked} onTaskDeleted={handleTaskDeleted} providers={providers} providersLoaded={providersLoaded} apps={apps} />+ <TasksTab completionRecovery={status?.completionRecovery} completedRevision={taskHistoryRevision} tasks={tasks} agents={agents} liveOutputs={liveOutputs} onRefresh={fetchData} onTaskAdded={handleUserTaskAdded} onTaskUnblocked={handleTaskUnblocked} onTaskDeleted={handleTaskDeleted} providers={providers} providersLoaded={providersLoaded} apps={apps} /></Suspense></div>)}{activeTab === 'agents' && (<div role="tabpanel" id="tabpanel-agents" aria-labelledby="tab-agents"><Suspense fallback={<TabLoadFallback label="agents" />}>- <AgentsTab agentsLoaded={agentsLoaded} agentsError={agentsError} onRetryAgents={fetchQueue} completedRevision={completedRevision} agents={agents} onRefresh={fetchData} liveOutputs={liveOutputs} providers={providers} providersLoaded={providersLoaded} apps={apps} />+ <AgentsTab completionRecovery={status?.completionRecovery} agentsLoaded={agentsLoaded} agentsError={agentsError} onRetryAgents={fetchQueue} completedRevision={completedRevision} agents={agents} onRefresh={fetchData} liveOutputs={liveOutputs} providers={providers} providersLoaded={providersLoaded} apps={apps} /></Suspense></div>)}diff --git a/client/src/pages/ChiefOfStaff.test.jsx b/client/src/pages/ChiefOfStaff.test.jsxindex e4b1fff4d2..d750f04c21 100644--- a/client/src/pages/ChiefOfStaff.test.jsx+++ b/client/src/pages/ChiefOfStaff.test.jsx@@ -1379,6 +1379,27 @@ describe('ChiefOfStaff active-agent snapshot readiness', () => {await act(async () => socketHandler('cos:agent:updated')(archived));expect(screen.getByRole('region', { name: 'Finalizing delivery' })).toBeInTheDocument();+ await act(async () => socketHandler('cos:agent:updated')({ id: finalizing.id,+ metadata: { ...finalizing.metadata, cleanupOutcome: { status: 'blocked', reason: 'preservation-unverified' } } }));+ expect(within(section).getByText('Last check: preservation unverified.')).toBeInTheDocument();+ await act(async () => socketHandler('cos:agent:updated')({ id: finalizing.id,+ deliveryActivity: { label: 'Checking delivery preservation' } }));+ expect(within(section).getByText('Checking delivery preservation')).toBeInTheDocument();+ await act(async () => socketHandler('cos:agent:updated')({ id: finalizing.id, deliveryActivity: null }));+ expect(within(section).getByText(/agent is not running/)).toBeInTheDocument();++ const statusReads = api.getCosStatus.mock.calls.length;+ await act(async () => {+ socketHandler('cos:scheduler:changed')({ id: 'unrelated-job' });+ await vi.advanceTimersByTimeAsync(450);+ });+ expect(api.getCosStatus).toHaveBeenCalledTimes(statusReads);+ await act(async () => {+ socketHandler('cos:scheduler:changed')({ id: 'cos-pending-merge-sweep' });+ await vi.advanceTimersByTimeAsync(450);+ });+ expect(api.getCosStatus).toHaveBeenCalledTimes(statusReads + 1);+api.getCosAgents.mockResolvedValue([]);await act(async () => {socketHandler('cos:tasks:changed')({ task: { id: finalizing.taskId, status: 'completed' } });diff --git a/server/routes/cos.test.js b/server/routes/cos.test.jsindex 28e69b9ad3..957991aa14 100644--- a/server/routes/cos.test.js+++ b/server/routes/cos.test.js@@ -5,6 +5,7 @@ import express from 'express';import { rmSync } from 'node:fs';import { request } from '../lib/testHelper.js';import { ServerError } from '../lib/errorHandler.js';+import { withDeliveryActivity } from '../services/agentDeliveryActivity.js';// The CoS task routes write operator-action rows (#5594) through the ledger's// file backend, which resolves under PATHS.data — re-root it so this suite can@@ -1924,6 +1925,26 @@ describe('CoS scoped collections', () => {expect(getAgentRecord.mock.calls.map(([id]) => id).sort()).toEqual(['agent-mismatch', 'agent-missing']);});+ it('reports only live in-process delivery activity and clears it when recovery fails', async () => {+ const owner = { id: 'agent-cleanup-owner', taskId: 'task-held', status: 'completed' };+ getAgents.mockResolvedValue([owner]);+ getTaskView.mockResolvedValue({ user: { tasks: [{ id: owner.taskId, status: 'in_progress',+ metadata: { deliveryRecovery: 'cleanup', deliveryRecoveryAgentId: owner.id } }] }, cos: { tasks: [] } });+ let reject;+ const recovery = withDeliveryActivity(owner.id, 'Checking preservation', () => new Promise((_, fail) => { reject = fail; }));+ const outcome = recovery.catch(error => error.message);+ try {+ const active = await request(app).get('/api/cos/agents?active=1');+ expect(active.body[0]).toMatchObject({ status: 'completed', deliveryPending: true,+ deliveryActivity: { label: 'Checking preservation', startedAt: expect.any(String) } });+ } finally {+ reject(new Error('verification-unavailable'));+ expect(await outcome).toBe('verification-unavailable');+ }+ const waiting = await request(app).get('/api/cos/agents?active=1');+ expect(waiting.body).toEqual([{ ...owner, deliveryPending: true }]);+ });+it('scopes live agents and validates completed page bounds without changing legacy lists', async () => {getAgents.mockResolvedValue([{ id: 'running', status: 'running' }, { id: 'done', status: 'completed' }]);expect((await request(app).get('/api/cos/agents?active=1')).body.map(agent => agent.id)).toEqual(['running']);diff --git a/server/routes/cosAgentRoutes.js b/server/routes/cosAgentRoutes.jsindex 6071c4db62..541a4faa1b 100644--- a/server/routes/cosAgentRoutes.js+++ b/server/routes/cosAgentRoutes.js@@ -15,6 +15,7 @@ import * as agentOrchestrator from '../services/agentOrchestrator.js';import { asyncHandler, ServerError } from '../lib/errorHandler.js';import { toAgentListItems } from '../lib/cosAgentListProjection.js';import { isDeliveryCleanupHeld } from '../lib/taskRetryHold.js';+import { getDeliveryActivity } from '../services/agentDeliveryActivity.js';import { securityScanRequestSchema } from '../lib/securityScanEvidence.js';import { validateRequest, resumeCosAgentSchema, relaunchCosAgentSchema, cosAgentFeedbackBodySchema, cosAgentBtwBodySchema } from '../lib/validation.js';@@ -149,7 +150,8 @@ router.get('/agents', asyncHandler(async (req, res) => {if (visible.has(id)) return;const agent = resident.get(id) || await getAgentRecord(id);if (agent?.status === 'completed' && agent.taskId === task.id) {- visible.set(id, { ...agent, deliveryPending: true });+ const deliveryActivity = getDeliveryActivity(id);+ visible.set(id, { ...agent, deliveryPending: true, ...(deliveryActivity && { deliveryActivity }) });}}));res.json(toAgentListItems([...visible.values()]));diff --git a/server/services/agentCompletionCleanup.js b/server/services/agentCompletionCleanup.jsindex 60415d3db4..cafe7a6b14 100644--- a/server/services/agentCompletionCleanup.js+++ b/server/services/agentCompletionCleanup.js@@ -1,4 +1,5 @@import { createHash } from 'node:crypto';+import { withDeliveryActivity } from './agentDeliveryActivity.js';import { storeCompletionData, readCompletionData, completionReplayInput } from './agentCompletionData.js';/*** Agent Completion Cleanup@@ -347,7 +348,8 @@ export async function runAgentCompletionCleanup({ agentId, task, agent, effectivasync function runCompletionCleanupSteps(context, { onStepError }) {if (!Object.values(STEP_ERROR_POLICY).includes(onStepError)) throw new Error(`Unknown completion-cleanup step error policy: ${onStepError}`);if (cleanupRuns.has(context.agentId)) return cleanupRuns.get(context.agentId);- const run = performCompletionCleanupSteps(context, onStepError);+ const run = withDeliveryActivity(context.agentId, 'Processing completion handoffs and workspace cleanup',+ () => performCompletionCleanupSteps(context, onStepError));cleanupRuns.set(context.agentId, run);try { return await run; } finally { cleanupRuns.delete(context.agentId); }}diff --git a/server/services/agentCompletionCleanup.replay.test.js b/server/services/agentCompletionCleanup.replay.test.jsindex ecc46f0410..be87b1f446 100644--- a/server/services/agentCompletionCleanup.replay.test.js+++ b/server/services/agentCompletionCleanup.replay.test.js@@ -11,7 +11,7 @@ vi.mock('./cosAgentIndex.js', () => ({getAgentDir: id => join(fixture.runRoot, ...(fixture.indexReady ? [] : ['unindexed']), id),}));vi.mock('./cos.js', () => ({}));-vi.mock('./cosEvents.js', () => ({ emitLog: vi.fn() }));+vi.mock('./cosEvents.js', async original => ({ ...await original(), emitLog: vi.fn() }));vi.mock('./cosTaskStore.js', () => ({ updateTask: vi.fn(async () => ({})), addTask: vi.fn(), reviveBlockedTask: vi.fn() }));vi.mock('./cosTaskGenerator.js', () => ({ checkStagePrecondition: () => ({ passed: true }) }));vi.mock('./cosAgentLifecycle.js', () => ({diff --git a/server/services/agentCompletionCleanup.test.js b/server/services/agentCompletionCleanup.test.jsindex 23d1eb26ca..b603184543 100644--- a/server/services/agentCompletionCleanup.test.js+++ b/server/services/agentCompletionCleanup.test.js@@ -14,7 +14,7 @@ import { tmpdir } from 'node:os';import { join } from 'node:path';vi.mock('./cos.js', () => ({}));-vi.mock('./cosEvents.js', () => ({ emitLog: vi.fn() }));+vi.mock('./cosEvents.js', async original => ({ ...await original(), emitLog: vi.fn() }));vi.mock('./cosTaskStore.js', () => ({updateTask: vi.fn().mockResolvedValue({}),addTask: vi.fn().mockResolvedValue({}),diff --git a/server/services/agentDeliveryActivity.js b/server/services/agentDeliveryActivity.jsnew file mode 100644index 0000000000..1d7a0d5b47--- /dev/null+++ b/server/services/agentDeliveryActivity.js@@ -0,0 +1,25 @@+import { cosEvents } from './cosEvents.js';++// Process-local evidence: persisted "running" stages cannot prove liveness after a crash.+const activities = new Map();+export const COMPLETION_RECOVERY_BATCH_SIZE = 10;++export function getDeliveryActivity(agentId) {+ return activities.get(agentId)?.at(-1)?.activity || null;+}++export async function withDeliveryActivity(agentId, label, work) {+ const entry = { activity: { label, startedAt: new Date().toISOString() } };+ const entries = activities.get(agentId) || [];+ activities.set(agentId, entries);+ entries.push(entry);+ const publish = () => cosEvents.emit('agent:updated', { id: agentId, deliveryActivity: getDeliveryActivity(agentId) });+ try {+ publish();+ return await work();+ } finally {+ entries.splice(entries.indexOf(entry), 1);+ if (!entries.length) activities.delete(agentId);+ publish();+ }+}diff --git a/server/services/agentRepoStateVerification.js b/server/services/agentRepoStateVerification.jsindex 67999383a2..a3de1bfe03 100644--- a/server/services/agentRepoStateVerification.js+++ b/server/services/agentRepoStateVerification.js@@ -1,4 +1,5 @@import { recoverCompletionTaskHandoff } from './agentCompletionTaskHandoff.js';+import { withDeliveryActivity, COMPLETION_RECOVERY_BATCH_SIZE } from './agentDeliveryActivity.js';import { MANAGED_WORKTREE_CLEANUP_CONTRACT } from '../lib/mergeGateContract.js';import { processAuditFingerprint } from '../lib/persistentMindProcessAudit.js';import { completionSentinelPresent } from './agentCompletionSentinel.js';@@ -277,7 +278,8 @@ export async function verifyAgentRepoState(input) {return verifyRunBranches({ ...input, success: input.success || cleanupPending,...(cleanupPending ? { cleanupWarnings: [] } : {}) });};- const run = verify().then(async result => {+ const run = withDeliveryActivity(input.agentId, 'Checking delivery and repository preservation', async () => {+ const result = await verify();if (result.skipReason === 'cleanup-owner-changed' || result.skipReason === 'cleanup-task-unavailable') return result;const outcomes = result.branches || [result];const unsupported = prStaysOpen(input.task) ? { skipReason: 'leave-open-cleanup-unsupported' } : outcomes.find(item => [REPO_STATE_SKIPS.DISABLED, REPO_STATE_SKIPS.NOT_WORKTREE,@@ -371,7 +373,7 @@ async function resumeCompletionAudits() {|| agent.metadata?.completionCleanup?.status === 'pending')).sort((a, b) => lastAttempt(a).localeCompare(lastAttempt(b)));let checked = 0;- for (const original of pending.slice(0, 10)) {+ for (const original of pending.slice(0, COMPLETION_RECOVERY_BATCH_SIZE)) {const { recordCompletionCleanup } = await import('./cosAgentLifecycle.js');await recordCompletionCleanup(original.id || original.agentId, original.startedAt, { lastAttemptAt: new Date().toISOString() });const { replayAgentCompletionCleanup } = await import('./agentCompletionCleanup.js');diff --git a/server/services/agentRepoStateVerification.test.js b/server/services/agentRepoStateVerification.test.jsindex 319b554783..b31735e7b3 100644--- a/server/services/agentRepoStateVerification.test.js+++ b/server/services/agentRepoStateVerification.test.js@@ -38,7 +38,7 @@ vi.mock('./heldCheckoutRecovery.js', () => ({retireInventoriedCheckout: vi.fn(async () => ({ ok: true, archiveDir: '/private/archive', branchDeleted: true })),prepareInventoriedCheckoutRetirement: vi.fn().mockResolvedValue({ ok: true, review: { digest: 'a'.repeat(64) } }),}));-vi.mock('./cosEvents.js', () => ({ emitLog: vi.fn() }));+vi.mock('./cosEvents.js', async original => ({ ...await original(), emitLog: vi.fn() }));vi.mock('./cosTaskStore.js', () => ({addTask: vi.fn().mockResolvedValue({ id: 'task-recovery-1' }),getAllTasks: vi.fn().mockResolvedValue({ user: { tasks: [] }, cos: { tasks: [] } }),diff --git a/server/services/cos.js b/server/services/cos.jsindex 6f572b5a4d..67d9092dfa 100644--- a/server/services/cos.js+++ b/server/services/cos.js@@ -30,7 +30,8 @@ import { clearStaleActiveAgents } from './appActivity.js';import { drainOnDemandRequests } from './onDemandDrain.js';import { logCosScheduleUpdate } from './userActionScheduleLog.js';import { getPerformanceSummary, checkAndRehabilitateSkippedTasks, getLearningInsights } from './taskLearning.js';-import { schedule as scheduleEvent, cancel as cancelEvent } from './eventScheduler.js';+import { schedule as scheduleEvent, cancel as cancelEvent, getEvent } from './eventScheduler.js';+import { COMPLETION_RECOVERY_BATCH_SIZE } from './agentDeliveryActivity.js';import { recordJobExecution } from './autonomousJobs.js';import { safeJSONParse, sleep, isTopLevelEntryName } from '../lib/fileUtils.js';import { ON_DEMAND_ORIGINS } from './taskScheduleConstants.js';@@ -158,6 +159,10 @@ export async function getStatus() {const stateCompletedIds = Object.keys(state.agents).filter(id => state.agents[id].status === 'completed');const stateOnlyCompleted = stateCompletedIds.filter(id => !idx.has(id)).length;const tasksCompleted = Math.max(state.stats.tasksCompleted, idx.size + stateOnlyCompleted);+ const recoveryEvent = getEvent('cos-pending-merge-sweep');+ const recoveryBlocked = !isDaemonRunning() ? 'CoS is stopped' : state.paused ? 'CoS is paused'+ : getDomainMode(state.config, 'cos') !== 'execute' ? 'CoS auto-run must be in Execute mode'+ : !recoveryEvent?.active ? 'Completion recovery is not scheduled' : null;return {running: isDaemonRunning(),@@ -169,6 +174,12 @@ export async function getStatus() {activeAgents,pendingTasks: unclaimedTaskIds(await readPendingTaskIds(), runningAgentsByTaskId(Object.values(state.agents))).length,pausedAgents,+ completionRecovery: {+ blockedReason: recoveryBlocked,+ nextRunAt: recoveryBlocked ? null : recoveryEvent.nextRunAt,+ intervalMs: recoveryEvent?.intervalMs || null,+ batchSize: COMPLETION_RECOVERY_BATCH_SIZE,+ },provider: provider ? { id: provider.id, name: provider.name } : null};}diff --git a/server/services/cos.startStop.test.js b/server/services/cos.startStop.test.jsindex 6bd12d3dc0..6c3b4ab7ee 100644--- a/server/services/cos.startStop.test.js+++ b/server/services/cos.startStop.test.js@@ -55,7 +55,10 @@ vi.mock('./cosTaskStore.js', async (importOriginal) => ({...(await importOriginal()),getAllTasks: vi.fn(async () => ({ user: { tasks: [], grouped: {} }, cos: { tasks: [], grouped: {} } })),getCosTasks: vi.fn(async () => ({ tasks: [] })),+ readPendingTaskIds: vi.fn(async () => []),}));+vi.mock('./providers.js', () => ({ getActiveProvider: vi.fn(async () => null) }));+vi.mock('./cosAgentIndex.js', async importOriginal => ({ ...(await importOriginal()), loadAgentIndex: vi.fn(async () => new Map()) }));vi.mock('./instanceIdentity.js', async (importOriginal) => ({...(await importOriginal()),ensureInstanceId: vi.fn(async () => 'instance-test'),@@ -99,7 +102,7 @@ vi.mock('./prWatcher.js', () => ({cancelHandoffFollowUps: () => { handoffFollowUp.gate = null; },}));-const { start, stop } = await import('./cos.js');+const { start, stop, getStatus } = await import('./cos.js');const { cosEvents } = await import('./cosEvents.js');const { isDaemonRunning } = await import('./cosState.js');@@ -132,6 +135,23 @@ afterEach(async () => {});describe('CoS daemon start/stop lifecycle (#11860)', () => {+ it('exposes recovery gates and the actual scheduled sweep time without promising a job completion time', async () => {+ persisted = { ...persisted, paused: false, stats: { tasksCompleted: 0 }, config: { ...persisted.config, domainAutonomy: { cos: 'execute' } } };+ expect((await getStatus()).completionRecovery).toMatchObject({ blockedReason: 'CoS is stopped', nextRunAt: null, batchSize: 10 });+ const starting = start();+ await waitFor(() => typeof releaseRecovery === 'function');+ releaseRecovery(); await starting;+ // This suite's scheduler registry deliberately has no real timer.+ expect((await getStatus()).completionRecovery.blockedReason).toBe('Completion recovery is not scheduled');+ const scheduler = await import('./eventScheduler.js');+ vi.spyOn(scheduler, 'getEvent').mockReturnValue({ active: true, nextRunAt: 1800000, intervalMs: 1800000 });+ expect((await getStatus()).completionRecovery).toEqual({ blockedReason: null, nextRunAt: 1800000, intervalMs: 1800000, batchSize: 10 });+ persisted.paused = true;+ expect((await getStatus()).completionRecovery).toMatchObject({ blockedReason: 'CoS is paused', nextRunAt: null });+ persisted.paused = false;+ persisted.config.domainAutonomy.cos = 'dry-run';+ expect((await getStatus()).completionRecovery).toMatchObject({ blockedReason: 'CoS auto-run must be in Execute mode', nextRunAt: null });+ });it('a Stop issued while start() is mid-boot leaves no CoS timer armed', async () => {const starting = start();await waitFor(() => typeof releaseRecovery === 'function');diff --git a/server/services/cosAgentLifecycle.js b/server/services/cosAgentLifecycle.jsindex 174e73c712..551a2a0584 100644--- a/server/services/cosAgentLifecycle.js+++ b/server/services/cosAgentLifecycle.js@@ -293,6 +293,7 @@ async function updateCompletionReceipt(agentId, update) {await atomicWrite(join(getAgentDir(agentId, date), 'metadata.json'), record);}if (state.agents[agentId]) { state.agents[agentId] = updated; await saveState(state, { changedAgentIds: [agentId] }); }+ cosEvents.emit('agent:updated', updated);return true;}));}diff --git a/server/services/cosAgentLifecycle.test.js b/server/services/cosAgentLifecycle.test.jsindex 385476a3f7..fe23d4b8dd 100644--- a/server/services/cosAgentLifecycle.test.js+++ b/server/services/cosAgentLifecycle.test.js@@ -418,6 +418,30 @@ describe('cosAgentLifecycle', () => {const persisted = JSON.parse(await readFile(join(archiveDir, 'metadata.json'), 'utf8'));expect(persisted.metadata.malwareScan.verdict).toBe('DANGEROUS');});++ it('publishes saved cleanup diagnostics for an archived owner without reviving execution', async () => {+ const id = 'agent-archived-cleanup';+ const date = '2026-01-01';+ const record = { id, status: 'completed', completedAt: `${date}T12:00:00Z`, metadata: {} };+ const directory = join(mockCosState.agentsDir, date, id);+ await mkdir(directory, { recursive: true });+ await writeFile(join(directory, 'metadata.json'), JSON.stringify(record));+ (await loadAgentIndex()).set(id, date);+ const observed = [];+ const listener = agent => {+ if (agent.id === id) observed.push(agent);+ };+ cosEvents.on('agent:updated', listener);+ try {+ await recordCleanupOutcome(id, { status: 'blocked', reason: 'preservation-unverified' });+ const persisted = JSON.parse(await readFile(join(directory, 'metadata.json'), 'utf8'));+ expect(observed).toEqual([persisted]);+ expect(observed[0]).toMatchObject({ status: 'completed', metadata: { cleanupOutcome: { reason: 'preservation-unverified' } } });+ expect(mockCosState.state.agents[id]).toBeUndefined();+ } finally {+ cosEvents.off('agent:updated', listener);+ }+ });});describe('completeAgent budget-ledger ordering (#1683)', () => {diff --git a/server/services/socket.js b/server/services/socket.jsindex 36e1331d8e..cf16206a8f 100644--- a/server/services/socket.js+++ b/server/services/socket.js@@ -498,7 +498,7 @@ function setupCosEventForwarding() {['learning:changed', 'cos:learning:changed'],]) {cosEvents.on(source, data => {- broadcastToCos(target, {});+ broadcastToCos(target, target === 'cos:scheduler:changed' && typeof data?.id === 'string' ? { id: data.id } : {});if (target === 'cos:scheduler:changed' && data?.id === 'backup-daily') {ioInstance?.emit('backup:changed', {});}diff --git a/server/services/socket.test.js b/server/services/socket.test.jsindex 8fcdc6a6df..a7277530b4 100644--- a/server/services/socket.test.js+++ b/server/services/socket.test.js@@ -314,6 +314,8 @@ describe('socket.js — initSocket', () => {dashboardEvents.emit('cos:day:changed');expect(subscriber.emitted).toContainEqual(['cos:day:changed', {}]);expect(other.emitted.some(([event]) => event === 'cos:day:changed')).toBe(false);+ queueListeners.cos.find(([event]) => event === 'scheduler:ran')[1]({ id: 'cos-pending-merge-sweep', privateContent: 'Example private record' });+ expect(subscriber.emitted).toContainEqual(['cos:scheduler:changed', { id: 'cos-pending-merge-sweep' }]);dashboardEvents.emit('cos:decisions:changed');expect(subscriber.emitted).toContainEqual(['cos:decisions:changed', {}]);expect(other.emitted.some(([event]) => event === 'cos:decisions:changed')).toBe(false);