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

feat: store the CoS task queues in PostgreSQL instead of Markdown files

Adam Eivyfd14b41b08b8
diff --git a/client/src/components/CosDashboardWidget.jsx b/client/src/components/CosDashboardWidget.jsx
index f5dd38cc79..bed61687fc 100644
--- a/client/src/components/CosDashboardWidget.jsx
+++ b/client/src/components/CosDashboardWidget.jsx
@@ -40,7 +40,7 @@ const comparableSnapshot = (snapshot) => {
};
};
-const RESOURCE_EVENTS = ['cos:day:changed', 'cos:tasks:changed', 'cos:tasks:user:changed', 'cos:tasks:cos:changed', 'cos:agent:spawned', 'cos:agent:updated', 'cos:agent:completed', 'cos:agents:changed', 'cos:status', 'cos:status:paused', 'cos:status:resumed', 'cos:learning:changed'];
+const RESOURCE_EVENTS = ['cos:day:changed', 'cos:tasks:changed', 'cos:agent:spawned', 'cos:agent:updated', 'cos:agent:completed', 'cos:agents:changed', 'cos:status', 'cos:status:paused', 'cos:status:resumed', 'cos:learning:changed'];
/**
* CosDashboardWidget - Compact CoS status widget for the main Dashboard
diff --git a/client/src/components/apps/tabs/IssuesTab.test.jsx b/client/src/components/apps/tabs/IssuesTab.test.jsx
index f828aa48f1..38352e97ab 100644
--- a/client/src/components/apps/tabs/IssuesTab.test.jsx
+++ b/client/src/components/apps/tabs/IssuesTab.test.jsx
@@ -254,7 +254,7 @@ describe('IssuesTab', () => {
expect(screen.queryByRole('link', { name: /Completed/ })).not.toBeInTheDocument();
});
- it('applies every task from a user task-list update', async () => {
+ it('applies every task update that arrives in the same tick', async () => {
const SECOND_ISSUE = {
...ISSUE,
number: 43,
@@ -272,12 +272,10 @@ describe('IssuesTab', () => {
fireEvent.click(claimButtons[1]);
await waitFor(() => expect(api.createSlashdoTask).toHaveBeenCalledTimes(2));
- act(() => socketHandlers.get('cos:tasks:user:changed')({
- tasks: [
- { id: 'task-1', status: 'completed' },
- { id: 'task-2', status: 'completed' },
- ],
- }));
+ act(() => {
+ socketHandlers.get('cos:tasks:changed')({ task: { id: 'task-1', status: 'completed' } });
+ socketHandlers.get('cos:tasks:changed')({ task: { id: 'task-2', status: 'completed' } });
+ });
await waitFor(() => expect(screen.getAllByRole('link', { name: /Completed/ })).toHaveLength(2));
});
@@ -286,7 +284,7 @@ describe('IssuesTab', () => {
const listenerCountBeforeMount = socketHandlers.size;
const { unmount } = await renderTab();
- expect(socketHandlers.size).toBe(listenerCountBeforeMount + 6);
+ expect(socketHandlers.size).toBe(listenerCountBeforeMount + 4);
unmount();
expect(socketHandlers.size).toBe(listenerCountBeforeMount);
});
diff --git a/client/src/components/apps/tabs/PullRequestsTab.test.jsx b/client/src/components/apps/tabs/PullRequestsTab.test.jsx
index 14604efc38..9d42864414 100644
--- a/client/src/components/apps/tabs/PullRequestsTab.test.jsx
+++ b/client/src/components/apps/tabs/PullRequestsTab.test.jsx
@@ -278,7 +278,7 @@ describe('PullRequestsTab', () => {
expect(screen.queryByRole('link', { name: /Completed/ })).not.toBeInTheDocument();
});
- it('applies every task from a user task-list update', async () => {
+ it('applies every task update that arrives in the same tick', async () => {
const SECOND_PULL_REQUEST = {
...PULL_REQUEST,
number: 18,
@@ -296,12 +296,10 @@ describe('PullRequestsTab', () => {
fireEvent.click(resolveButtons[1]);
await waitFor(() => expect(api.resolveAppPullRequest).toHaveBeenCalledTimes(2));
- act(() => socketHandlers.get('cos:tasks:user:changed')({
- tasks: [
- { id: 'task-1', status: 'completed' },
- { id: 'task-2', status: 'completed' },
- ],
- }));
+ act(() => {
+ socketHandlers.get('cos:tasks:changed')({ task: { id: 'task-1', status: 'completed' } });
+ socketHandlers.get('cos:tasks:changed')({ task: { id: 'task-2', status: 'completed' } });
+ });
await waitFor(() => expect(screen.getAllByRole('link', { name: /Completed/ })).toHaveLength(2));
});
@@ -310,7 +308,7 @@ describe('PullRequestsTab', () => {
const listenerCountBeforeMount = socketHandlers.size;
const { unmount } = await renderTab();
- expect(socketHandlers.size).toBe(listenerCountBeforeMount + 6);
+ expect(socketHandlers.size).toBe(listenerCountBeforeMount + 4);
unmount();
expect(socketHandlers.size).toBe(listenerCountBeforeMount);
});
diff --git a/client/src/components/cos/tabs/ConfigTab.jsx b/client/src/components/cos/tabs/ConfigTab.jsx
index bd9f71cb19..dc02601c55 100644
--- a/client/src/components/cos/tabs/ConfigTab.jsx
+++ b/client/src/components/cos/tabs/ConfigTab.jsx
@@ -5,7 +5,6 @@ import {
Brain,
Database,
ExternalLink,
- FileText,
Gauge,
Palette,
Server,
@@ -557,22 +556,6 @@ export default function ConfigTab({ config, onUpdate, onEvaluate, avatarStyle, r
)) : <span className="text-xs text-port-text-muted">No MCP servers configured</span>}
</div>
</div>
- <div className="rounded-lg border border-port-border bg-port-card p-4">
- <h5 className="text-sm font-medium text-port-text">Task files</h5>
- <p className="mt-1 text-xs text-port-text-muted">Editable Markdown queues. Changes made in these files are picked up automatically.</p>
- <div className="mt-3 space-y-2 text-xs">
- <div className="flex items-center gap-2 rounded-md bg-port-bg px-3 py-2">
- <FileText size={14} className="text-port-text-muted" />
- <span className="text-port-text-muted">User</span>
- <span className="ml-auto font-mono text-port-text">{config?.userTasksFile || 'TASKS.md'}</span>
- </div>
- <div className="flex items-center gap-2 rounded-md bg-port-bg px-3 py-2">
- <FileText size={14} className="text-port-text-muted" />
- <span className="text-port-text-muted">System</span>
- <span className="ml-auto font-mono text-port-text">{config?.cosTasksFile || 'COS-TASKS.md'}</span>
- </div>
- </div>
- </div>
</div>
</section>
</div>
diff --git a/client/src/components/cos/tabs/TaskItem.jsx b/client/src/components/cos/tabs/TaskItem.jsx
index 657fda1678..03910c0e46 100644
--- a/client/src/components/cos/tabs/TaskItem.jsx
+++ b/client/src/components/cos/tabs/TaskItem.jsx
@@ -144,18 +144,17 @@ function getSuccessRateStyle(rate) {
}
export default function TaskItem({ task, agent = null, liveOutput, isSystem, spawning = false, selected = false, onRefresh, onTaskUnblocked, onTaskDeleted, providers, providersLoaded, durations, dragHandleProps, apps, instances = null, onEditingChange }) {
- // System tasks are persisted in COS-TASKS.md. Every task
- // mutation must name that source; otherwise the API's user-queue default
- // searches TASKS.md and reports the system task as missing.
+ // System tasks live in the internal queue. Every task mutation must name that
+ // source; otherwise the API's user-queue default searches the user queue and
+ // reports the system task as missing.
const taskSource = isSystem ? 'internal' : 'user';
// A spawning task is still persisted as 'pending' but is already running —
// show the running glyph, not the queued clock. See spawnAgentForTask's
// registration ordering for why the two disagree.
const displayStatus = spawning ? 'in_progress' : task.status;
const idScope = isSystem ? 'sys' : 'user';
- // Either queue. A USER row can hold too: a row the parser had to RECOVER is
- // withheld from the unattended spawn and persists that hold in TASKS.md
- // (#7300, #7367). Gating the button on `isSystem` would leave it pending with
+ // Either queue. A USER row can hold too: a row the importer had to RECOVER is
+ // withheld from the unattended spawn and keeps that hold (#7300, #7367). Gating the button on `isSystem` would leave it pending with
// no way for the human it is waiting on to release it.
const requiresApproval = Boolean(task.approvalRequired);
// Name the hold's reason on the APPROVE button so "why is this one waiting on
diff --git a/client/src/components/cos/tabs/TasksTab.jsx b/client/src/components/cos/tabs/TasksTab.jsx
index f579ce61e2..62e923f631 100644
--- a/client/src/components/cos/tabs/TasksTab.jsx
+++ b/client/src/components/cos/tabs/TasksTab.jsx
@@ -272,7 +272,7 @@ export default function TasksTab({ completedRevision = 0, tasks, agents = [], li
) : pendingUserTasksLocal.length === 0 && activeUserTasksLocal.length === 0 && blockedUserTasksLocal.length === 0 && (tasks.user?.completedCount ?? completedUserTasksLocal.length) === 0 ? (
<div className="relative bg-port-card border border-port-border rounded-lg p-6 text-center text-gray-500">
<SchematicLabel module="USER" status="EMPTY" glyph="bracket-pair" state="idle" variant="tab" />
- No user tasks. Add one above or edit TASKS.md directly.
+ No user tasks. Add one above.
</div>
) : (
<div className="space-y-3">
@@ -370,7 +370,7 @@ export default function TasksTab({ completedRevision = 0, tasks, agents = [], li
{/* System Tasks */}
<div>
- <h3 className="text-lg font-semibold text-white mb-3">System Tasks (COS-TASKS.md)</h3>
+ <h3 className="text-lg font-semibold text-white mb-3">System Tasks</h3>
{/* System Tasks Sections */}
{isCosLoading ? (
diff --git a/client/src/components/dashboard/builtins/AutoFixMetricsWidget.jsx b/client/src/components/dashboard/builtins/AutoFixMetricsWidget.jsx
index 1436d3e50b..4bfdac8477 100644
--- a/client/src/components/dashboard/builtins/AutoFixMetricsWidget.jsx
+++ b/client/src/components/dashboard/builtins/AutoFixMetricsWidget.jsx
@@ -6,7 +6,7 @@ import * as api from '../../../services/api';
import { useSocketResource } from '../../../hooks/useSocketResource';
import { formatDurationMs } from '../../../utils/formatters';
-const RESOURCE_EVENTS = ['cos:tasks:changed', 'cos:tasks:user:changed', 'cos:tasks:cos:changed'];
+const RESOURCE_EVENTS = ['cos:tasks:changed'];
// Auto-fix telemetry (issue #2328). Self-fetches the aggregated
// GET /api/autofix/metrics (derived server-side from persisted
diff --git a/client/src/components/system-resources/QueuesPanel.jsx b/client/src/components/system-resources/QueuesPanel.jsx
index ec15b2cdeb..45a97b1854 100644
--- a/client/src/components/system-resources/QueuesPanel.jsx
+++ b/client/src/components/system-resources/QueuesPanel.jsx
@@ -9,8 +9,7 @@ import { useSocketResource } from '../../hooks/useSocketResource.js';
import * as api from '../../services/api.js';
const QUEUE_EVENTS = [
- 'cos:tasks:changed', 'cos:tasks:user:changed', 'cos:tasks:user:added',
- 'cos:tasks:user:completed', 'cos:tasks:cos:changed',
+ 'cos:tasks:changed',
'cos:agent:spawned', 'cos:agent:updated', 'cos:agent:completed',
];
diff --git a/client/src/components/system-resources/QueuesPanel.test.jsx b/client/src/components/system-resources/QueuesPanel.test.jsx
index faeb0b71dc..f331e16296 100644
--- a/client/src/components/system-resources/QueuesPanel.test.jsx
+++ b/client/src/components/system-resources/QueuesPanel.test.jsx
@@ -117,11 +117,10 @@ it('updates queues from task/agent events, coalesces reads and preserves stale d
let resolveRead;
api.getCosTasks.mockImplementationOnce(() => new Promise(resolve => { resolveRead = resolve; }));
- await act(async () => socket.fire('cos:tasks:user:added'));
+ await act(async () => socket.fire('cos:tasks:changed'));
await act(async () => {
socket.fire('cos:agent:spawned');
socket.fire('cos:tasks:changed');
- socket.fire('cos:tasks:cos:changed');
});
expect(api.getCosTasks).toHaveBeenCalledTimes(2);
api.getCosTasks.mockResolvedValue({ user: { tasks: [{ ...pending.user.tasks[0], spawning: true }] }, cos: { tasks: [] } });
@@ -131,7 +130,7 @@ it('updates queues from task/agent events, coalesces reads and preserves stale d
expect(countCard('Agent pending')).toBe('0');
api.getCosTasks.mockRejectedValueOnce(new Error('offline'));
- await act(async () => socket.fire('cos:tasks:user:completed'));
+ await act(async () => socket.fire('cos:agent:completed'));
expect(countCard('Agent running')).toBe('1');
await act(async () => socket.fire('connect'));
expect(api.getCosTasks).toHaveBeenCalledTimes(5);
diff --git a/client/src/hooks/useActionQueue.js b/client/src/hooks/useActionQueue.js
index 8a953365e9..efb2d4c476 100644
--- a/client/src/hooks/useActionQueue.js
+++ b/client/src/hooks/useActionQueue.js
@@ -8,7 +8,7 @@ import { ACTION_QUEUE_CHANGED, INSTANCE_FEATURES_CHANGED } from '../constants/ev
// invalidations only; source records are always re-read from this install.
const EVENTS = [
'connect', 'brain:classified', 'brain:threads:changed',
- 'cos:tasks:user:changed', 'cos:tasks:cos:changed', 'cos:agent:completed',
+ 'cos:tasks:changed', 'cos:agent:completed',
'cos:memory:approval-needed', 'cos:memory:approved', 'cos:memory:rejected', 'cos:agent:feedback',
'messages:changed', 'messages:draft:created', 'messages:draft:sent',
'backup:started', 'backup:completed', 'backup:failed',
diff --git a/client/src/hooks/useCosTaskUpdates.js b/client/src/hooks/useCosTaskUpdates.js
index 4352a505f5..3200968170 100644
--- a/client/src/hooks/useCosTaskUpdates.js
+++ b/client/src/hooks/useCosTaskUpdates.js
@@ -17,12 +17,6 @@ export function useCosTaskUpdates(onTaskUpdate) {
if (task) onTaskUpdateRef.current?.(task);
};
const handleTaskChanged = event => emitTaskUpdate(event?.task);
- const handleTaskListChanged = event => {
- for (const task of event?.tasks || []) emitTaskUpdate(task);
- };
- const handleTaskCompleted = event => {
- for (const task of event?.tasks || []) emitTaskUpdate(task);
- };
const handleAgentSpawned = agent => {
if (agent?.taskId) emitTaskUpdate({ id: agent.taskId, status: 'in_progress' });
};
@@ -36,15 +30,11 @@ export function useCosTaskUpdates(onTaskUpdate) {
};
socket.on('cos:tasks:changed', handleTaskChanged);
- socket.on('cos:tasks:user:changed', handleTaskListChanged);
- socket.on('cos:tasks:user:completed', handleTaskCompleted);
socket.on('cos:agent:spawned', handleAgentSpawned);
socket.on('cos:agent:completed', handleAgentCompleted);
return () => {
socket.off('cos:tasks:changed', handleTaskChanged);
- socket.off('cos:tasks:user:changed', handleTaskListChanged);
- socket.off('cos:tasks:user:completed', handleTaskCompleted);
socket.off('cos:agent:spawned', handleAgentSpawned);
socket.off('cos:agent:completed', handleAgentCompleted);
};
diff --git a/client/src/pages/ChiefOfStaff.jsx b/client/src/pages/ChiefOfStaff.jsx
index 27dd094056..ef0c27c92b 100644
--- a/client/src/pages/ChiefOfStaff.jsx
+++ b/client/src/pages/ChiefOfStaff.jsx
@@ -555,27 +555,9 @@ export default function ChiefOfStaff() {
};
socket.on('cos:status', handleCosStatus);
- const handleTasksUserChanged = (data) => {
- if (!needsTasks) return;
- if (data?.tasks) setTasks(prev => ({ ...prev, user: { ...data, tasks: data.tasks.filter(task => task.status !== 'completed' || task.id === selectedTaskRef.current), completedCount: data.tasks.filter(task => task.status === 'completed').length } }));
- else refreshQueue();
- };
- socket.on('cos:tasks:user:changed', handleTasksUserChanged);
-
- // System (COS-TASKS.md) tasks change on their own file-watcher event, and
- // scheduled/on-demand CoS work IS an internal task — so without this handler
- // a freshly queued scheduled task only appeared once explicit refresh or reconnect came
- // around. Same full-list payload as the user event, so swap it in directly.
- const handleTasksCosChanged = (data) => {
- if (!needsTasks) return;
- if (data?.tasks) setTasks(prev => ({ ...prev, cos: { ...data, tasks: data.tasks.filter(task => task.status !== 'completed' || task.id === selectedTaskRef.current), completedCount: data.tasks.filter(task => task.status === 'completed').length } }));
- else refreshQueue();
- };
- socket.on('cos:tasks:cos:changed', handleTasksCosChanged);
-
// The store's own per-mutation event, which fires the instant a task is added
- // or its status flips — ahead of the debounced file watcher above. It carries
- // one task rather than the list, and also fires for writes with nothing to
+ // or its status flips. It carries one task rather than the list, and also
+ // fires for writes with nothing to
// show here (every running task's federation lease heartbeat), so it drives
// one coalesced queue refresh rather than a fetch per event. This is what
// collapses the pending→in_progress lag: 'cos:agent:spawned' fires BEFORE
@@ -713,8 +695,6 @@ export default function ChiefOfStaff() {
handleAppsChanged.cancel();
handleProvidersChanged.cancel();
socket.off('cos:status', handleCosStatus);
- socket.off('cos:tasks:user:changed', handleTasksUserChanged);
- socket.off('cos:tasks:cos:changed', handleTasksCosChanged);
socket.off('cos:tasks:changed', handleTaskMutation);
socket.off('cos:agent:spawned', handleAgentSpawned);
socket.off('cos:agent:updated', handleAgentUpdated);
diff --git a/client/src/pages/ChiefOfStaff.test.jsx b/client/src/pages/ChiefOfStaff.test.jsx
index b0de159ba8..57e89aaf96 100644
--- a/client/src/pages/ChiefOfStaff.test.jsx
+++ b/client/src/pages/ChiefOfStaff.test.jsx
@@ -769,25 +769,6 @@ describe('ChiefOfStaff task-change subscriptions', () => {
const renderSettledTasksTab = () => renderSettledAt('tasks');
- it('renders a newly queued system task straight off the watcher event', async () => {
- await renderSettledTasksTab();
- await waitFor(() => expect(api.getCosTasks).toHaveBeenCalled());
- const before = api.getCosTasks.mock.calls.length;
-
- const handleCosTasksChanged = getSocketHandler('cos:tasks:cos:changed');
- expect(handleCosTasksChanged).toBeTypeOf('function');
- await act(async () => {
- handleCosTasksChanged({
- exists: true,
- tasks: [{ id: 'cos-task-1', description: 'Example scheduled task', status: 'pending', metadata: {} }],
- });
- });
-
- expect(await screen.findByText('Example scheduled task')).toBeInTheDocument();
- // The event carries the whole list, so it must not cost a round trip.
- expect(api.getCosTasks.mock.calls.length).toBe(before);
- });
-
it('renders a submitted user task before the follow-up refresh resolves', async () => {
await renderSettledTasksTab();
await screen.findByRole('button', { name: 'Add test task' });
diff --git a/data.reference/COS-TASKS.md b/data.reference/COS-TASKS.md
deleted file mode 100644
index d4d12de509..0000000000
--- a/data.reference/COS-TASKS.md
+++ /dev/null
@@ -1,7 +0,0 @@
-# Tasks
-
-## Pending
-
-## In Progress
-
-## Completed
diff --git a/data.reference/TASKS.md b/data.reference/TASKS.md
deleted file mode 100644
index d4d12de509..0000000000
--- a/data.reference/TASKS.md
+++ /dev/null
@@ -1,7 +0,0 @@
-# Tasks
-
-## Pending
-
-## In Progress
-
-## Completed
diff --git a/docs/API.md b/docs/API.md
index 3bc4462b7d..21a06f8faa 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -269,7 +269,7 @@ Context tools remain read-only. Semantic reads and writes are independent, defau
| POST | `/cos/tools/call` | Execute one schema-validated semantic tool with server-derived authority and idempotency |
| GET | `/cos/tools/calls/:requestId` | Read a retained normalized tool result |
| PUT | `/cos/config` | Update configuration |
-| GET | `/cos/tasks` | Bounded task list: all non-completed tasks plus the first 25 completed (`completedCount`, `completedNextCursor`); `view=queue`, `view=completed` (paged), `view=full` (everything the task files hold: the open queue plus the recent completed window; completed tasks older than that move to the archive and page through `view=completed`) |
+| GET | `/cos/tasks` | Bounded task list: all non-completed tasks plus the first 25 completed (`completedCount`, `completedNextCursor`); `view=queue`, `view=completed` (paged), `view=full` (the working set: the open queue plus completed tasks from the last 3 days and the newest 100; older completed history pages through `view=completed`); `limit`/`offset` window the working set |
| GET | `/cos/tasks/:id` | One task as stored plus the queue it lives in: `{ task, source: 'user' \| 'internal' }`; 404 when unknown |
| POST | `/cos/evaluate` | Force task evaluation |
| GET | `/cos/health` | Get health status |
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index e39d157fdb..a42e454523 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -84,7 +84,7 @@ PortOS/
│ ├── lib/ # Shared utilities
│ │ ├── errorHandler.js # Error normalization
│ │ ├── validation.js # Zod schemas
-│ │ └── taskParser.js # TASKS.md parser
+│ │ └── taskParser.js # Task stored-form parser (and migration 018's input reader)
│ └── cos-runner/ # Isolated agent runner
│ └── index.js # Standalone Express server
│
@@ -101,8 +101,6 @@ PortOS/
│ ├── providers.json # AI provider configs
│ ├── history.jsonl # Action history (JSON Lines)
│ ├── browser-config.json # Browser CDP/health configuration
-│ ├── TASKS.md # User task file
-│ ├── COS-TASKS.md # System task file
│ ├── cos/ # CoS state and agents
│ │ ├── state.json # Daemon state
│ │ └── agents/ # Agent outputs
@@ -175,8 +173,7 @@ Server Event → Socket.IO → socket.js → React Component State Update
### Chief of Staff Flow
```
-1. Task Watcher monitors TASKS.md for changes
-2. CoS Service evaluates tasks on interval
+1. Task writes emit `tasks:changed`; the CoS Service evaluates tasks on that event and on an interval
3. For each pending task:
a. Select appropriate AI model based on task complexity
b. Build prompt with context and memory injection
diff --git a/docs/BACKUP.md b/docs/BACKUP.md
index 16594885e1..06c8760dde 100644
--- a/docs/BACKUP.md
+++ b/docs/BACKUP.md
@@ -351,7 +351,7 @@ a snapshot as a complete recovery point.
Now that PostgreSQL is a **required** dependency (it owns the creative catalog, memory, and a growing set of app-native records — see [Storage Classification Contract](./STORAGE.md)), **the database dump is part of required system state, not an optional extra.** A snapshot that captured `data/` but failed to capture the DB is incomplete, and PortOS surfaces that explicitly.
-The dump includes the machine-local `cos_pending_agent_feedback` reference index, the `cos_task_archive` completed-task history (the only copy once a task leaves `TASKS.md` / `COS-TASKS.md`), and `review_queue_triage` presentation markers. The queue's source records remain in their owning stores; restoring the dump therefore preserves snooze/dismissal decisions without exporting them through federation or duplicating source payloads.
+The dump includes the machine-local `cos_pending_agent_feedback` reference index, the `cos_tasks` CoS task queues and their completed-task history (the Markdown task files were retired into it and kept as `*.imported`), and `review_queue_triage` presentation markers. The queue's source records remain in their owning stores; restoring the dump therefore preserves snooze/dismissal decisions without exporting them through federation or duplicating source payloads.
Implementation: `server/services/backup.js` (snapshot/dump/restore), `server/services/backupScheduler.js` (cron), `server/routes/backup.js` (API), and `server/routes/database.js` (DB export/sync).
diff --git a/docs/STORAGE.md b/docs/STORAGE.md
index 8cca6dff1f..cc9afc848e 100644
--- a/docs/STORAGE.md
+++ b/docs/STORAGE.md
@@ -153,9 +153,11 @@ does not recover nonempty output or general failed saves; those still require th
**When to use.** The payload is long externally-editable prose; the domain syncs through iCloud/file-sync outside PortOS; or forcing the record through the app DB would break an existing sync boundary.
-**CoS task queues** (`data/TASKS.md`, `data/COS-TASKS.md`, or configured paths) are being moved to PostgreSQL in two steps. Direct Markdown editing and the file watcher are no longer supported inputs, so nothing about the files has to stay human-readable or watcher-compatible; that contract is retired (decision 2026-10-20, [plan](./plans/2026-10-20-cos-task-queue-to-postgres.md)). Step one is the completed-task archive below. Step two moves the open queue into per-row storage, replaces the file watcher (`taskWatcher.js`) with events emitted at the write, and keeps the Markdown files as a one-time import with a recovery copy. Until step two lands the open queue and a recent window of completed tasks remain in the files, and `cosTaskStore.js` still caches parsed snapshots by file stamp and lazily indexes task IDs; single-task reads clone only the matching record, without grouping or copying either backlog. Merely storing the complete Markdown blob in PostgreSQL would retain whole-queue parsing and rewriting, so step two is per-row.
+**CoS task queues** (`cos_tasks`, `db-primary`): the user and internal queues are one row per task, keyed `(source, task_id)`. The row's `task` JSONB is the task in its **stored form** (`server/lib/cosTaskRows.js`): what the retired Markdown round-trip produced, so scalar metadata still reads back as strings and every consumer is unchanged. `status`, `priority_value`, `position`, `completed_at` and `description` are projections for the queries; `position` is fractional, so a task can be inserted anywhere by taking the midpoint of its neighbours. `cos_task_lists.version` bumps with every write in the same transaction, so a cached read is validated with a single-row read. Callers work with a **working set** (every open task plus completed tasks from the last 3 days and the newest 100): that is what `getAllTasks()` / `getUserTasks()` / `getCosTasks()` return and what the task cache holds. Older completed history is reached by id and by paged `view=completed` queries, never loaded wholesale; those prompt bodies were ~99% of the old files (tens of MB) and every full read cloned them. A save diffs the whole list against the stored working set and writes only the changed rows in one transaction. Ids are the row key, minted from the clock, so `addTask` suffixes `-dup2`, `-dup3`, ... on a collision with any stored task.
-`cos_task_archive` (`db-primary`, **machine-local, never federated**) holds completed tasks older than the recent window, with their full records, prompt bodies included. Those prompts were ~99% of the two files (tens of MB on a busy install), and every full-queue read cloned all of it. `archiveCompletedTasks()` runs in the 15-minute CoS health check: a completed task leaves the file once it is at least 3 days past its `completedAt`/`updatedAt` (undated tasks count as oldest) AND beyond the newest 100 completed per queue, at most 300 per queue per tick. The archive write lands first and is idempotent, so a crash before the file rewrite leaves a harmless duplicate the next sweep removes. Reads fall through: the completed-history pages, the queue/bare views' `completedCount`, `getTaskById`/`findTaskWithSource` and `deleteTask` consult the archive when the files miss, and `character.syncTaskXP` includes archived summaries so a task archived before an XP sync still earns it. Peers advertise and merge only what their own files hold, and `mergePeerTasks` skips an incoming completed task this machine already archived so a peer cannot resurrect it. No migration or seed: the table is additive (`ensureSchema` and `init-db.sql`), an absent archive is the correct empty state, and the first sweeps drain an existing backlog. Backed up by the Postgres dump. Adapter: `server/services/cosTaskArchive.js` (file backend only under the dev/test escape hatch).
+The queues federate exactly as before: a full-sync peer advertises its working set at `GET /api/peer-sync/cos-tasks` and the receiver runs the claim-aware per-task merge (`cosTaskStore.mergePeerTasks`); no sync cursor or tombstone is added. A completed task the receiver already holds is not merged again, so a peer cannot pull old history back into the working set. Completed history older than the working set is machine-local.
+
+Direct editing of the Markdown files and the file watcher are retired (decision 2026-10-20, [plan](./plans/2026-10-20-cos-task-queue-to-postgres.md)); there is no runtime fallback to either. **Migration:** db-migration 018 imports `data/TASKS.md` and `data/COS-TASKS.md` (at the location the install's CoS config named, else the defaults) and, on installs that ran the interim completed-task archive, folds `cos_task_archive` in and drops it; it verifies that every parsed task landed as a row before committing. db-migration 019, which runs only after 018 has committed, renames each file to `<name>.imported` (the recovery copy; nothing reads it) and drops the `userTasksFile` / `cosTasksFile` config keys. Input-derived, so no `data.reference` seed ships for either file. Backed up by the Postgres dump. Repository: `server/services/cosTaskRepository.js` (in-memory backend only under the dev/test escape hatch); store: `server/services/cosTaskStore.js`.
**Private security assessments** reuse the existing Review Hub item store for report prose and CoS agent archives for local source/transcripts. They add no store format or database migration. The source inventory stays in run memory; interruption before report validation requires a new assessment. Assessment tasks and all their archive files are excluded from peer federation, and shared socket notifications carry no report prose. Reports follow existing local backup and Review Hub retention; temporary sandbox homes are removed after completion. See [the assessment design and research](research/2026-09-06-private-security-models.md).
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index ece107bcef..8a8c8c2426 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -90,7 +90,7 @@ npm run pm2:restart
`setup:data` (`scripts/setup-data.js`) creates `data/` from `data.reference/` when it is absent. Otherwise it copies only the missing files, adds new starter entries to `providers.json` and the prompt configs without touching yours, and expands `__PORTOS_ROOT__` in `apps.json`. It also skips any path a migration derives from your own records (`scripts/lib/migrationOwnedPaths.js`). Pending migrations run when the server boots.
-**Never copy `data.reference/` over `data/` by hand** (`cp -r data.reference/* data/`). The seed tree includes `settings.json`, `apps.json`, `providers.json`, `cos/state.json`, `TASKS.md`, Brain settings (`brain/meta.json`), the digital-twin documents and every prompt template. Copying it overwrites your copies of all of them with shipped defaults, and nothing reports what was lost.
+**Never copy `data.reference/` over `data/` by hand** (`cp -r data.reference/* data/`). The seed tree includes `settings.json`, `apps.json`, `providers.json`, `cos/state.json`, Brain settings (`brain/meta.json`), the digital-twin documents and every prompt template. Copying it overwrites your copies of all of them with shipped defaults, and nothing reports what was lost.
### Dynamic import callback missing after profiling
@@ -379,15 +379,11 @@ Still never run `npm install` in a worktree.
### Tasks Not Being Picked Up
-**Symptom**: Added tasks to TASKS.md but CoS ignores them.
+**Symptom**: Added a task but CoS ignores it.
**Solution**:
-1. Verify task format matches expected syntax:
- ```markdown
- ## Pending
- - [ ] #task-001 | HIGH | Task description
- ```
-2. Check file path in CoS config matches your TASKS.md location
+1. Add tasks through the UI or `POST /api/cos/tasks`; the queues are database rows (`cos_tasks`), and editing `data/TASKS.md` no longer does anything. After an update the old files are kept as `data/*.md.imported`.
+2. Check the task's status and approval flag in the Tasks tab (a task awaiting approval does not run).
3. Trigger manual evaluation via UI
### Agent Writes Files Into the PortOS Folder Instead of My App
diff --git a/docs/features/chief-of-staff.md b/docs/features/chief-of-staff.md
index 604464b335..4cb02ee601 100644
--- a/docs/features/chief-of-staff.md
+++ b/docs/features/chief-of-staff.md
@@ -1,19 +1,18 @@
# Chief of Staff
-Autonomous agent manager that watches task files, spawns sub-agents, and maintains system health.
+Autonomous agent manager that works the task queues, spawns sub-agents, and maintains system health.
## Architecture
-- **Task Parser** (`server/lib/taskParser.js`): Parses TASKS.md and COS-TASKS.md formats
+- **Task Store** (`server/services/cosTaskStore.js`): The user and internal queues as `cos_tasks` rows (working-set cache, row-diff saves); `server/services/cosTaskRepository.js` is the persistence layer
- **CoS Service** (`server/services/cos.js`): State management, health monitoring, task evaluation
-- **Task Watcher** (`server/services/taskWatcher.js`): File watching with chokidar
- **Sub-Agent Spawner** (`server/services/subAgentSpawner.js`): Claude CLI execution with MCP
- **CoS Routes** (`server/routes/cos.js`): REST API endpoints
- **CoS UI** (`client/src/pages/ChiefOfStaff.jsx`): Tasks, Agents, Health, Config tabs
## Features
-1. **Dual Task Lists**: User tasks (TASKS.md) and system tasks (COS-TASKS.md)
+1. **Dual Task Lists**: User tasks and system tasks, both stored in `cos_tasks`
2. **Autonomous Execution**: Auto-approved tasks run without user intervention
3. **Approval Workflow**: Tasks marked APPROVAL require user confirmation
4. **System Health Monitoring**: PM2 process checks, memory usage, error detection
diff --git a/docs/features/development-watchdog.md b/docs/features/development-watchdog.md
index 571715347d..8e3fd80e61 100644
--- a/docs/features/development-watchdog.md
+++ b/docs/features/development-watchdog.md
@@ -39,7 +39,7 @@ failed evidence need no resolution agent.
The latest receipt and 24 historical receipts use the existing machine-local
CoS runtime state, not a new record store. A bounded current-PR dispatch ledger
survives restarts and prevents repeated resolution of unchanged evidence. Queue
-admission shares structured work identity across user/internal task files and
+admission shares structured work identity across the user/internal task queues and
existing scheduler/manual follow-up producers. Untargeted claims reserve their
app's issue lane. Full-sync peers participate in ownership reads; unrelated
peers do not block the scan. Failed or capped participating-peer and forge reads
diff --git a/docs/features/identity-system.md b/docs/features/identity-system.md
index d633884ff4..35e0462a29 100644
--- a/docs/features/identity-system.md
+++ b/docs/features/identity-system.md
@@ -15,7 +15,7 @@ Four separate workstreams converge on the same vision: a personal digital twin t
| **Genome** | Fully implemented: 23andMe upload, 117 curated SNP markers across 32 categories, ClinVar integration, epigenetic tracking | `server/services/genome.js`, `GenomeTab.jsx`, `data/meatspace/genome.json` |
| **Chronotype** | Implemented: `deriveChronotype()` combines the 5 sleep/circadian markers (CLOCK rs1801260, DEC2 rs57875989, PER2 rs35333999, CRY1 rs2287161, MTNR1B rs10830963) with `daily_routines` enrichment and writes `chronotype.json` | `server/services/identity/chronotype.js`, `identity.test.js`, `data/digital-twin/chronotype.json` |
| **Aesthetic Taste** | P2 complete: Taste questionnaire with 5 sections (movies, music, visual_art, architecture, food), conversational Q&A, AI summary generation. Enrichment categories also feed taste data from book/movie/music lists | `TasteTab.jsx`, `taste-questionnaire.js`, `data/digital-twin/taste-profile.json` |
-| **Goal Tracking** | Partially exists: `GOALS.md` for repository mission and `docs/GOALS_OPERATIONAL.md` for operational CoS goals, `TASKS.md` for user tasks, `EXISTENTIAL.md` soul doc | `GOALS.md`, `docs/GOALS_OPERATIONAL.md`, `data/TASKS.md`, `data/digital-twin/EXISTENTIAL.md` |
+| **Goal Tracking** | Partially exists: `GOALS.md` for repository mission and `docs/GOALS_OPERATIONAL.md` for operational CoS goals, the user task queue for user tasks, `EXISTENTIAL.md` soul doc | `GOALS.md`, `docs/GOALS_OPERATIONAL.md`, `data/TASKS.md`, `data/digital-twin/EXISTENTIAL.md` |
These should be unified under a single **Identity** architecture so the twin can reason across all dimensions (e.g., "your CLOCK gene says evening chronotype — schedule deep work after 8pm" or "given your longevity markers and age, here's how to prioritize your 10-year goals").
diff --git a/docs/features/self-improvement-audits.md b/docs/features/self-improvement-audits.md
index e66421c2bd..bfc75f194f 100644
--- a/docs/features/self-improvement-audits.md
+++ b/docs/features/self-improvement-audits.md
@@ -188,7 +188,7 @@ and an obsolete scope label cannot remain after upstream changes. The former
The prefix is spelled with a hyphen rather than `better:` because a task type is
interpolated into the CoS task id, which becomes a git branch name
(`cos/<taskId>/<agentId>`) and a worktree directory. Git refs reject a colon,
-Windows paths reject a colon, and the `TASKS.md` id pattern accepts only word
+Windows paths reject a colon, and the task id pattern accepts only word
characters and hyphens.
## Filing and mode
diff --git a/docs/plans/2026-10-20-cos-task-queue-to-postgres.md b/docs/plans/2026-10-20-cos-task-queue-to-postgres.md
index f2a8f5ef3a..6d8e47beeb 100644
--- a/docs/plans/2026-10-20-cos-task-queue-to-postgres.md
+++ b/docs/plans/2026-10-20-cos-task-queue-to-postgres.md
@@ -21,50 +21,68 @@ contract: nobody needs to edit these files by hand. With it gone, nothing about
the data fits a file better than a table, and the repo already requires every
install and federated peer to run PostgreSQL.
-## Step one: completed-task archive (this change)
+## What shipped
-- Table `cos_task_archive (source, task_id, description, completed_at, archived_at,
- task jsonb)`, key `(source, task_id COLLATE "C")`. `COLLATE "C"` matches the
- JavaScript code-unit order every consumer already uses for newest-id-first paging.
-- `archiveCompletedTasks()` in the 15-minute CoS health check moves a completed
- task out of the file when it is at least 3 days past its stamp (undated = oldest)
- AND beyond the newest 100 completed per queue, at most 300 per queue per tick.
- Archive write first (idempotent), then the file rewrite, all under the same
- state lock as every task write: a crash leaves a duplicate, never a loss.
-- Reads fall through when the file misses: completed-history pages (merged across
- file window and archive, file copy wins), `completedCount`, a selected archived
- task in the queue view, `getTaskById`/`findTaskWithSource`, `deleteTask`.
-- Consumers audited for old-completed reads: `screenshotsGc` ignores terminal tasks;
- the PR-reviewer-failure lookup only needs a recent marker; `character.syncTaskXP`
- would lose XP for a task archived before a sync, so it also reads archived
- summaries (id + description, no prompt bodies).
-- Federation: the archive is machine-local. Peers advertise and merge only their
- own files; `mergePeerTasks` drops an incoming completed task this machine already
- archived so a peer cannot resurrect it.
-- No migration or seed. The table is additive; the first sweeps drain the existing
- backlog (300 per queue per tick, ~90 minutes for a ~1,600-task queue). Backup is
- the Postgres dump.
+Step one was an interim completed-task archive (`cos_task_archive`). Once hand
+editing was ruled out, keeping a second table beside the Markdown files made no
+sense, so the end state replaced it: ONE table, no files.
-## Step two: the open queue (next)
+- `cos_tasks` (one row per task, key `(source, task_id COLLATE "C")`) and
+ `cos_task_lists` (a per-queue write version).
+- A task is stored in its **stored form**: the old Markdown round-trip's
+ normalization (`lib/cosTaskRows.js#storedForm`). Many consumers handle scalar
+ metadata read back as strings; storing anything else would have changed their
+ behavior as a side effect. Typed metadata is a separate cleanup.
+- `position` is a fractional tie-break inside a status+priority, so insertion
+ anywhere is a midpoint and a reorder rewrites only the moved rows
+ (`assignPositions`, longest-increasing-run).
+- Callers read the **working set**: open tasks, completed in the last 3 days, and
+ the newest 100 completed. Older history is reached by id and by paged
+ `view=completed`; nothing loads it wholesale. No sweep is needed, because the
+ working set is a query, not a file that has to be trimmed.
+- The store's cache is validated by the queue version (one single-row read) and
+ updated in place by its own writes. A save is `planListSave`: diff the list
+ against the stored working set, write only changed rows in one transaction.
+- Concurrent writers are still serialized by the CoS state lock; the win is that
+ a write is a few rows instead of the whole file, and no reader parses anything.
+- The file watcher and its routes, socket forwards, client listeners and config
+ keys are gone. It was not running on installs that start the CoS at boot, and
+ the store's own `tasks:changed` event was always the real signal.
-- Per-row storage for open tasks, replacing whole-file read-modify-write: concurrent
- agents update their own row instead of queueing on one file.
-- Writers emit `tasks:*` events at the write; `taskWatcher.js` (chokidar over both
- files, snapshot diffing) and the file-stamp parse cache go away.
-- `mergePeerTasks` / `buildCosTasksPayload` keep their claim-aware per-task LWW
- semantics over rows.
-- Migration imports both configured Markdown sources without losing task metadata
- or order, retains a recovery copy of each file, and gates on the presence of the
- input (the files), never on the absence of the output.
-- The Markdown files become a one-time import input only.
+## Upgrading existing installs
+
+Data standards are updated by migration, not carried as runtime fallbacks:
+
+- **018** imports the Markdown files (at the location the install's CoS config
+ named, else the defaults), folds in `cos_task_archive` and drops it, verifies
+ every parsed task landed as a row, and bumps the queue versions. It is
+ input-gated and ships no seed.
+- **019**, which runs only after 018 has committed, renames each file to
+ `<name>.imported` (the recovery copy) and drops the retired config keys.
+- `data.reference/` no longer ships the two files.
+
+Rolling back to a release that reads the Markdown files finds no files; restore
+them from `*.imported` first.
+
+## Follow-ups (not in this change)
+
+- Agent status and activity still contend on `data/cos/state.json` (one 4 MB
+ file, one lock). Moving it is a separate design.
+- True per-row concurrency (dropping the state lock around task writes) now
+ becomes possible and is a separate change.
+- Typed metadata (stop round-tripping scalars through strings) would let the
+ `isTruthyMeta`-style helpers retire.
## Verification
-- Store tests cover the sweep (age floor, newest-N floor, oldest-first under the
- limit, idempotence, crash between archive and rewrite), the fall-through reads,
- delete, and the no-resurrection merge; each was checked to fail with its behavior
- removed. The file backend is covered in `cosTaskArchive.test.js`, the Postgres
- backend in `cosTaskArchive.db.test.js` (`npm run test:db`).
-- After deployment: `data/TASKS.md` / `COS-TASKS.md` shrink to the open queue plus
- the recent window; steady-state server RSS and the `readTaskFile` clone volume
- drop; the Tasks page still pages through full history.
+- `lib/cosTaskRows.test.js`: stored form, working-set rule, position assignment,
+ save planning. `services/_cosTaskRepositoryContract.js` runs the same
+ assertions against the in-memory backend and, in `npm run test:db`, PostgreSQL.
+- `services/cosTaskStore.test.js`: cache behavior (hit, in-place update, version
+ reload, cold-read coalescing, overlapping read and write), views, unique ids,
+ history older than the working set, peer merge, and the interleaved-writer races.
+- Migrations 018/019: unit tests over real fixture files, plus a real-PostgreSQL
+ test (CI) that imports a 230-task queue with awkward text, folds the archive
+ table, and checks every task byte-for-byte.
+- After deployment: `data/*.md` are gone (kept as `*.imported`), the queues read
+ the same, and `readTaskFile` clone volume and server RSS stay low.
diff --git a/docs/plans/README.md b/docs/plans/README.md
index 4cf778c7cc..e4e95ffe09 100644
--- a/docs/plans/README.md
+++ b/docs/plans/README.md
@@ -16,8 +16,9 @@ Index of plans other docs point at:
resource suites in pregate, and the pregate attestation CI honors.
- [2026-10-20 — CoS task queue to PostgreSQL](./2026-10-20-cos-task-queue-to-postgres.md):
- why `TASKS.md` / `COS-TASKS.md` outgrew their files, the completed-task archive
- (step one) and the per-row open queue that replaces the file watcher (step two).
+ why `TASKS.md` / `COS-TASKS.md` outgrew their files, the single `cos_tasks`
+ table that replaced them (and the interim archive), and the migrations that
+ bring existing installs to it.
These are point-in-time design records, not living docs — they are not updated
to track implementation drift. For current behavior, see the code, `CLAUDE.md`,
diff --git a/scripts/seed-readme-demo-data.mjs b/scripts/seed-readme-demo-data.mjs
index 3ae532c102..b4354b3c0f 100644
--- a/scripts/seed-readme-demo-data.mjs
+++ b/scripts/seed-readme-demo-data.mjs
@@ -45,9 +45,11 @@ const writeJson = (relativePath, value) => {
writeFileSync(filePath, `${JSON.stringify(value, null, 2)}\n`);
};
-// The task store resolves configured files relative to PATHS.root. Keep these
-// paths relative to the checkout so PORTOS_DATA_ROOT still points at the
-// disposable root when the server runs from this repository.
+// The task queues live in PostgreSQL. These Markdown files are the input of
+// db-migration 018, which imports them into whichever database the demo server
+// uses on its first boot; the migration resolves configured files relative to
+// PATHS.root, so keep these paths relative to the checkout and PORTOS_DATA_ROOT
+// still points at the disposable root when the server runs from this repository.
const userTasksFile = relative(repoRoot, join(dataRoot, 'TASKS.md'));
const cosTasksFile = relative(repoRoot, join(dataRoot, 'COS-TASKS.md'));
diff --git a/server/lib/README.md b/server/lib/README.md
index 5335cfe05f..b25a1411ab 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -542,9 +542,10 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `htmlToText.js` | Shared HTML → plain-text converter. `htmlToText(html, { extraEntities?, paragraphBreak?, collapseSpaces? })` — strips script/style/head/noscript blocks, converts `<br>`/block closes to newlines, strips remaining tags, decodes entities via `decodeXmlEntities`, collapses 3+ newlines, trims. Options preserve caller-specific output (Gmail: `paragraphBreak: '\n\n'` + `collapseSpaces`; SongBook import: defaults — space runs preserved for tab alignment). |
| `jsonExtract.js` | Pull JSON blocks out of LLM responses. `extractNonPlaceholderJson(raw, { field })` skips echoed schema placeholders and distinguishes placeholder-only from parse failures. `findBalancedBlocks` walks top-level blocks and stops at the first unbalanced one; `findAllBalancedBlocks` is the stack-based variant that scans past stray braces and returns nested blocks in closing order (reverse it for outermost-newest-first). |
| `streamJsonParser.js` | Stream-JSON parser for Claude CLI `--output-format stream-json`. `createStreamJsonParser()` returns a stateful parser with `processChunk`/`flush`/`getFinalResult` that extracts human-readable lines from streaming NDJSON events (text deltas, tool-use markers, tool input summaries, intermediate assistant results). Also exports `summarizeToolInput(toolName, input)` for concise tool-call descriptions and `safeParse(str)` as a plain `JSON.parse` try/catch. Shared by `cos-runner/index.js` and `services/agentCliSpawning.js`. |
-| `taskParser.js` | Parse and generate the `TASKS.md` format, and own the vocabularies it can represent: `TASK_STATUS_VALUES` / `TASK_PRIORITY_VALUES` (derived from `STATUS_MAP` / `PRIORITY_VALUES`, read by `cosValidation.js` and `peerSyncValidation.js` so the HTTP and wire enums cannot drift from the store), plus `UNKNOWN_STATUS_BLOCKED_CATEGORY` — the marker `generateTasksMarkdown` stamps when it REPAIRS an unrepresentable task instead of dropping its row (#7239). |
+| `taskParser.js` | Parse and generate the Markdown task format (a task's stored form, and the input of db-migration 018), and own the vocabularies it can represent: `TASK_STATUS_VALUES` / `TASK_PRIORITY_VALUES` (derived from `STATUS_MAP` / `PRIORITY_VALUES`, read by `cosValidation.js` and `peerSyncValidation.js` so the HTTP and wire enums cannot drift from the store), plus `UNKNOWN_STATUS_BLOCKED_CATEGORY` — the marker `generateTasksMarkdown` stamps when it REPAIRS an unrepresentable task instead of dropping its row (#7239). |
| `taskGenerationOverrides.js` | `applyTaskGenerationOverrides(provider, metadata)` — the per-RUN `temperature` / `thinking` / `effort` a task form or schedule entry pins, layered onto a COPY of the provider record (the saved record is shared by every other run; the child-env composer turns the copy into OpenCode’s dynamic `agent.build` config downstream). Every value arrives untrusted — a COS-TASKS.md round-trip hands back strings — so each override applies only when it validates, and an empty temperature field is absent rather than `0`, which would otherwise pin greedy decoding on a task whose author just cleared the box. |
| `cosTaskPrompt.js` | The CoS task prompt/note split (#4153) — `metadata.prompt` is the full agent-facing payload, `metadata.context` the one-line human note. `isPromptPayload(value)` is the single discriminator (a newline means it's a prompt, not a note), shared by the store's write path and the on-disk migration so they can't classify the same task differently. `getTaskPrompt(task)` prefers `metadata.prompt` and falls back to a legacy `metadata.context`; `getTaskContextNote(task)` returns the note only on an already-split task; `taskContextBlock(task)` renders both as one block so prompt templates (including customized ones) never see the split; `splitTaskPromptFields(metadata)` routes a multi-line `context` to `prompt` at CREATE time only. Pure, dependency-free. |
+| `cosTaskRows.js` | Row model for the `cos_tasks` queues: the stored form of a task (the old Markdown round-trip's normalization), the recent-completed working-set rule, fractional queue `position` assignment, and `planListSave` (turn "the whole list changed" into "only these rows changed"). Shared by the Postgres and in-memory repository backends so they cannot drift |
| `taskPauseHold.js` | The user-PAUSE hold — pure metadata helpers shared by the writer (`pauseAgent`), the reader (`resumeAgent`), and the clearer (`updateTask`). `AGENT_PAUSED_CATEGORY` is the `blockedCategory` a pause stamps; `PAUSE_METADATA_KEYS` names every key it writes; `pauseMetadata({agentId, pausedAt, workspacePath, runId})` arms it and `clearedPauseMetadata()` releases it (undefined-valued, so `updateTask` deletes rather than serializing `"null"`); `isAgentPausedTask(task)` / `isResumablePausedTask(task, agentId)` answer whether a pause is still live and whether it is THIS agent's. `updateTask` clears the whole set whenever the task leaves `blocked`, so a revive by any path (dedupe, cooldown expiry, manual unblock) can't leave a task running under a stale pause. Also carries the cycle-safe RELEASE adapter (#3730) `agentManagement.js` registers at load — `resolvePausedTaskResume(task)` hands `updateTask` the paused run's resume pointer before the write (so any un-block resumes on the preserved worktree, not just the Resume dialog) and `retirePausedAgent(agentId, taskId, branchName)` retires the paused record after it. |
| `taskBlockCategories.js` | The `blockedCategory` vocabulary — which blocks are the system's to clear and which are a person's, read by the pause logic, the failure reaper, and the investigation auto-retry so they can't drift into three literal sets. `PAUSED_BLOCKED_CATEGORIES` (paused until something outside the task changes — keeps the resume pointer), `USER_DECISION_BLOCKED_CATEGORIES` (user intent / open decision — the reaper's exemption), `NON_AUTO_RETRY_BLOCK_CATEGORIES` (their union — a completed investigation must never revive these), `TIMED_COOLDOWN_BLOCKED_CATEGORIES` (a timer clears these — the cooldown sweeper revives them, so block reporters stay quiet), `PROVIDER_CONFIG_BLOCKED_CATEGORY` (the shared literal the agent-spawn stamp site uses, so the stamp cannot drift from the two sets it belongs to). Pure. |
| `taskRequeue.js` | The REQUEUE stamp (#3376) — pure metadata helpers for the one BACKWARD lifecycle step (`in_progress → pending`, performed by the orphan sweep and the retry-hold release). `REQUEUED_AT_KEY` / `LAST_SPAWNED_AT_KEY` name the two stamps; `isPostSpawnRequeue(pendingTask, inProgressTask)` answers whether the requeue happened strictly AFTER that spawn, which is how the federated merge tells a real requeue apart from an ordinary edit landing on a peer's stale `pending` copy. Returns false when either stamp is missing, so callers fall back to the lifecycle rank. |
diff --git a/server/lib/cosTaskRows.js b/server/lib/cosTaskRows.js
new file mode 100644
index 0000000000..b86ce25b70
--- /dev/null
+++ b/server/lib/cosTaskRows.js
@@ -0,0 +1,180 @@
+/**
+ * Row model for the CoS task queues (`cos_tasks`): the pure rules the repository
+ * and the task store share, so a Postgres backend and an in-memory backend cannot
+ * drift apart.
+ *
+ * A task is stored in its normalized form: exactly what the old Markdown
+ * round-trip produced (scalar metadata read back as strings, nullish metadata
+ * dropped, one-line descriptions). Consumers were written against that shape and
+ * still carry helpers (`isTruthyMeta`, ...) for it; storing anything else would
+ * change their behavior as a side effect of changing the storage.
+ *
+ * Queue order is (status section, priority high to low, position). `position`
+ * only breaks ties inside a status+priority, and is fractional so a task can be
+ * inserted anywhere by taking the midpoint of its neighbours.
+ */
+
+import { isDeepStrictEqual } from 'node:util';
+import { generateTasksMarkdown, parseTasksMarkdown } from './taskParser.js';
+
+/** Completed tasks stay in the working set (what `getAllTasks()` readers see) this long... */
+export const WORKING_SET_COMPLETED_WINDOW_MS = 3 * 24 * 60 * 60 * 1000;
+/** ...and the newest this many per queue stay regardless of age. */
+export const WORKING_SET_KEEP_COMPLETED = 100;
+export const POSITION_STEP = 1024;
+// Below this a midpoint is no longer reliably distinct; renumber the list instead.
+const MIN_POSITION_GAP = 1e-6;
+const STATUS_ORDER = ['pending', 'in_progress', 'challenged', 'blocked', 'completed'];
+
+/** Section rank of a status; unknown statuses sort last, like the Markdown writer's repair bucket. */
+export const statusRank = (status) => {
+ const rank = STATUS_ORDER.indexOf(status);
+ return rank === -1 ? STATUS_ORDER.length : rank;
+};
+
+/** When a task finished, in ms. Undated tasks (older than the stamps) count as oldest: 0. */
+export function completionStamp(task) {
+ const stamp = Date.parse(task?.metadata?.completedAt || task?.metadata?.updatedAt || '');
+ return Number.isFinite(stamp) ? stamp : 0;
+}
+
+/**
+ * The task as it is stored and read back. Round-trips through the single-row
+ * Markdown form: a lossy-by-design normalization every existing consumer expects.
+ * That includes the id: one without a registered prefix reads back as `task-<id>`,
+ * so the stored id, not the incoming one, is the row key.
+ */
+export function storedForm(task, source) {
+ const [stored] = parseTasksMarkdown(generateTasksMarkdown([task], source === 'internal'));
+ if (!stored) throw new Error(`Task ${task?.id} cannot be stored`);
+ return stored;
+}
+
+/** The projection columns of a stored task. */
+export function rowColumns(task) {
+ const stamp = task.status === 'completed' ? completionStamp(task) : 0;
+ return {
+ status: task.status,
+ priorityValue: Number.isFinite(task.priorityValue) ? task.priorityValue : 2,
+ completedAt: stamp > 0 ? new Date(stamp).toISOString() : null,
+ description: typeof task.description === 'string' ? task.description : '',
+ };
+}
+
+const byId = (a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
+
+/** Queue order for `{ task, position }` rows. */
+export function compareRows(a, b) {
+ return statusRank(a.task.status) - statusRank(b.task.status)
+ || (b.task.priorityValue ?? 2) - (a.task.priorityValue ?? 2)
+ || a.position - b.position
+ || byId(a.task, b.task);
+}
+
+/**
+ * The working set: every open task, completed tasks inside the recent window, and
+ * the newest few completed whatever their age. Returned in queue order. The
+ * Postgres backend runs the same rule in SQL; this is the in-memory one.
+ */
+export function selectWorkingSet(rows, { now = Date.now() } = {}) {
+ const completed = rows.filter((row) => row.task.status === 'completed')
+ .sort((a, b) => completionStamp(b.task) - completionStamp(a.task) || byId(b.task, a.task));
+ const keptCompleted = new Set(completed.filter((row, index) => index < WORKING_SET_KEEP_COMPLETED
+ || completionStamp(row.task) >= now - WORKING_SET_COMPLETED_WINDOW_MS).map((row) => row.task.id));
+ return rows.filter((row) => row.task.status !== 'completed' || keptCompleted.has(row.task.id)).sort(compareRows);
+}
+
+/**
+ * Which indexes of `ids` keep their stored position: the longest run whose stored
+ * positions already increase in list order, so a single move rewrites a single row.
+ */
+function keptByLongestIncreasingRun(previous, ids) {
+ const candidates = [];
+ ids.forEach((id, index) => { if (previous.has(id)) candidates.push(index); });
+ const tails = [];
+ const link = new Array(ids.length).fill(-1);
+ for (const index of candidates) {
+ const position = previous.get(ids[index]);
+ let low = 0;
+ let high = tails.length;
+ while (low < high) {
+ const mid = (low + high) >> 1;
+ if (previous.get(ids[tails[mid]]) < position) low = mid + 1; else high = mid;
+ }
+ if (low > 0) link[index] = tails[low - 1];
+ tails[low] = index;
+ }
+ const kept = ids.map(() => false);
+ for (let index = tails.at(-1) ?? -1; index !== -1; index = link[index]) kept[index] = true;
+ return kept;
+}
+
+/**
+ * Positions for the ids whose stored position is missing or out of order.
+ * `previous` maps id -> stored position; `ids` is the new queue order. Ids that
+ * still sit in increasing position order keep theirs; the rest take evenly spaced
+ * values between their kept neighbours. Returns only the ids that need a write
+ * (all of them when the gaps have collapsed and the list is renumbered).
+ */
+export function assignPositions(previous, ids) {
+ const kept = keptByLongestIncreasingRun(previous, ids);
+
+ const assigned = new Map();
+ let renumber = false;
+ for (let start = 0; start < ids.length;) {
+ if (kept[start]) { start++; continue; }
+ let end = start;
+ while (end < ids.length && !kept[end]) end++;
+ const count = end - start;
+ const left = start > 0 ? previous.get(ids[start - 1]) : undefined;
+ const right = end < ids.length ? previous.get(ids[end]) : undefined;
+ let first;
+ let step = POSITION_STEP;
+ if (left === undefined && right === undefined) first = 0;
+ else if (left === undefined) first = right - POSITION_STEP * count;
+ else if (right === undefined) first = left + POSITION_STEP;
+ else {
+ step = (right - left) / (count + 1);
+ first = left + step;
+ if (step < MIN_POSITION_GAP) renumber = true;
+ }
+ for (let offset = 0; offset < count; offset++) assigned.set(ids[start + offset], first + step * offset);
+ start = end;
+ }
+ if (!renumber) return assigned;
+ return new Map(ids.map((id, index) => [id, index * POSITION_STEP]));
+}
+
+/**
+ * What to write so the stored working set becomes `next`. `previous` is the
+ * `{ task, position }` rows as loaded. Unchanged tasks are never re-normalized or
+ * rewritten; a deleted id is one that left the working set's list, so rows outside
+ * the working set (older completed tasks) are never touched.
+ *
+ * @returns {{ upserts: Array<{task: object, position: number}>, deletes: string[], saved: object[] }}
+ * `saved` is the list as it will read back, in `next` order.
+ */
+export function planListSave({ previous, next, source }) {
+ const prior = new Map(previous.map((row) => [row.task.id, row]));
+
+ // Keyed by the stored id (see storedForm); a repeated id keeps its first slot and its last value.
+ const stored = new Map();
+ const changed = new Set();
+ for (const task of next) {
+ const before = prior.get(task.id)?.task;
+ if (before && isDeepStrictEqual(task, before)) { stored.set(task.id, before); continue; }
+ const normalized = storedForm(task, source);
+ const existing = prior.get(normalized.id)?.task;
+ const kept = existing && isDeepStrictEqual(normalized, existing) ? existing : normalized;
+ stored.set(normalized.id, kept);
+ if (kept !== existing) changed.add(normalized.id);
+ }
+ const nextIds = [...stored.keys()];
+
+ const positions = assignPositions(new Map(previous.map((row) => [row.task.id, row.position])), nextIds);
+ const upserts = nextIds
+ .filter((id) => changed.has(id) || positions.has(id))
+ .map((id) => ({ task: stored.get(id), position: positions.get(id) ?? prior.get(id).position }));
+ const deletes = previous.map((row) => row.task.id).filter((id) => !stored.has(id));
+ return { upserts, deletes, saved: nextIds.map((id) => stored.get(id)) };
+}
diff --git a/server/lib/cosTaskRows.test.js b/server/lib/cosTaskRows.test.js
new file mode 100644
index 0000000000..0e6890476c
--- /dev/null
+++ b/server/lib/cosTaskRows.test.js
@@ -0,0 +1,175 @@
+import { describe, expect, it } from 'vitest';
+import {
+ POSITION_STEP, WORKING_SET_COMPLETED_WINDOW_MS, WORKING_SET_KEEP_COMPLETED,
+ assignPositions, compareRows, completionStamp, planListSave, rowColumns, selectWorkingSet, storedForm,
+} from './cosTaskRows.js';
+
+const NOW = Date.parse('2026-10-20T12:00:00Z');
+const DAY = 86_400_000;
+const task = (id, extra = {}) => ({ id, status: 'pending', priority: 'MEDIUM', priorityValue: 2, description: `Example ${id}`, approvalRequired: false, autoApproved: true, metadata: {}, ...extra });
+const done = (id, ageDays, extra = {}) => task(id, { status: 'completed', metadata: { updatedAt: new Date(NOW - ageDays * DAY).toISOString() }, ...extra });
+
+describe('storedForm', () => {
+ it('normalizes exactly as the old Markdown round-trip did', () => {
+ const stored = storedForm(task('task-a', { description: 'Line one\nLine two', metadata: { openPR: true, count: 3, gone: null, note: undefined } }), 'user');
+ // Scalars read back as strings, nullish metadata is dropped, the description is one line.
+ expect(stored.metadata).toEqual({ openPR: 'true', count: '3' });
+ expect(stored.description).not.toContain('\n');
+ expect(stored.id).toBe('task-a');
+ });
+
+ it('reads an unregistered id prefix back as task-<id>, as the Markdown reader always did', () => {
+ expect(storedForm(task('a'), 'user').id).toBe('task-a');
+ });
+
+ it('keeps the approval flags only for the internal queue', () => {
+ const internal = storedForm(task('sys-a', { approvalRequired: true, autoApproved: false }), 'internal');
+ expect(internal).toMatchObject({ approvalRequired: true, autoApproved: false });
+ });
+});
+
+describe('rowColumns', () => {
+ it('projects status, priority, description and a completed_at only for completed tasks', () => {
+ expect(rowColumns(done('task-a', 2))).toEqual({ status: 'completed', priorityValue: 2, completedAt: new Date(NOW - 2 * DAY).toISOString(), description: 'Example task-a' });
+ expect(rowColumns(task('task-b', { metadata: { updatedAt: '2026-10-01T00:00:00Z' } })).completedAt).toBeNull();
+ expect(rowColumns(done('task-c', 0, { metadata: {} })).completedAt).toBeNull();
+ });
+
+ it('prefers completedAt over updatedAt', () => {
+ expect(completionStamp({ metadata: { completedAt: '2026-10-02T00:00:00Z', updatedAt: '2026-10-09T00:00:00Z' } })).toBe(Date.parse('2026-10-02T00:00:00Z'));
+ expect(completionStamp({ metadata: {} })).toBe(0);
+ });
+});
+
+describe('queue order', () => {
+ it('sorts by status section, then priority high to low, then position, then id', () => {
+ const rows = [
+ { task: task('c', { status: 'blocked' }), position: 0 },
+ { task: task('b', { priorityValue: 1 }), position: 0 },
+ { task: task('a', { priorityValue: 3 }), position: 5 },
+ { task: task('d', { priorityValue: 3 }), position: 1 },
+ { task: task('e', { status: 'in_progress' }), position: 0 },
+ ];
+ expect(rows.sort(compareRows).map((row) => row.task.id)).toEqual(['d', 'a', 'b', 'e', 'c']);
+ });
+});
+
+describe('selectWorkingSet', () => {
+ it('keeps every open task, completed tasks inside the window, and the newest completed beyond it', () => {
+ const rows = [
+ { task: task('open'), position: 0 },
+ { task: done('recent', 1), position: 0 },
+ ...Array.from({ length: WORKING_SET_KEEP_COMPLETED + 2 }, (_, n) => ({ task: done(`old-${String(n).padStart(3, '0')}`, 10 + n / 100), position: 0 })),
+ ];
+ const ids = selectWorkingSet(rows, { now: NOW }).map((row) => row.task.id);
+ expect(ids).toContain('open');
+ expect(ids).toContain('recent');
+ // The newest 100 completed stay (recent is one of them); the 2 oldest drop out.
+ expect(ids.filter((id) => id.startsWith('old-'))).toHaveLength(WORKING_SET_KEEP_COMPLETED - 1);
+ expect(ids).not.toContain('old-101');
+ });
+
+ it('treats undated completed tasks as oldest', () => {
+ const rows = [{ task: done('dated', 20), position: 0 }, ...Array.from({ length: WORKING_SET_KEEP_COMPLETED }, (_, n) => ({ task: done(`new-${n}`, 5), position: 0 })), { task: done('undated', 0, { metadata: {} }), position: 0 }];
+ const ids = selectWorkingSet(rows, { now: NOW }).map((row) => row.task.id);
+ expect(ids).not.toContain('undated');
+ expect(ids).not.toContain('dated');
+ });
+
+ it('is inclusive at the window edge', () => {
+ const edge = { task: done('edge', WORKING_SET_COMPLETED_WINDOW_MS / DAY), position: 0 };
+ const filler = Array.from({ length: WORKING_SET_KEEP_COMPLETED }, (_, n) => ({ task: done(`new-${n}`, 0.5), position: 0 }));
+ expect(selectWorkingSet([edge, ...filler], { now: NOW }).map((row) => row.task.id)).toContain('edge');
+ });
+});
+
+describe('assignPositions', () => {
+ const positions = (entries) => new Map(entries);
+
+ it('numbers an empty list from zero', () => {
+ expect([...assignPositions(new Map(), ['a', 'b', 'c'])]).toEqual([['a', 0], ['b', POSITION_STEP], ['c', 2 * POSITION_STEP]]);
+ });
+
+ it('writes nothing when order is unchanged, and only the new id when one is appended, prepended or inserted', () => {
+ const previous = positions([['a', 0], ['b', 1024], ['c', 2048]]);
+ expect(assignPositions(previous, ['a', 'b', 'c']).size).toBe(0);
+ expect([...assignPositions(previous, ['a', 'b', 'c', 'n'])]).toEqual([['n', 3072]]);
+ expect([...assignPositions(previous, ['n', 'a', 'b', 'c'])]).toEqual([['n', -1024]]);
+ expect([...assignPositions(previous, ['a', 'n', 'b', 'c'])]).toEqual([['n', 512]]);
+ });
+
+ it('spaces several inserts evenly between their neighbours', () => {
+ const out = assignPositions(positions([['a', 0], ['b', 900]]), ['a', 'x', 'y', 'z', 'b']);
+ expect([...out.keys()]).toEqual(['x', 'y', 'z']);
+ expect([...out.values()]).toEqual([225, 450, 675]);
+ });
+
+ it('moves only the displaced id on a reorder, wherever it goes', () => {
+ const previous = positions([['a', 0], ['b', 1024], ['c', 2048], ['d', 3072]]);
+ const toEnd = assignPositions(previous, ['b', 'c', 'd', 'a']);
+ expect([...toEnd.keys()]).toEqual(['a']);
+ expect(toEnd.get('a')).toBeGreaterThan(3072);
+ const toFront = assignPositions(previous, ['d', 'a', 'b', 'c']);
+ expect([...toFront.keys()]).toEqual(['d']);
+ expect(toFront.get('d')).toBeLessThan(0);
+ const swapped = assignPositions(previous, ['a', 'c', 'b', 'd']);
+ expect(swapped.size).toBe(1);
+ });
+
+ it('renumbers the whole list when the gap between neighbours has collapsed', () => {
+ const out = assignPositions(positions([['a', 0], ['b', 1e-9]]), ['a', 'n', 'b']);
+ expect([...out]).toEqual([['a', 0], ['n', 1024], ['b', 2048]]);
+ });
+});
+
+describe('planListSave', () => {
+ const load = (tasks) => tasks.map((t, i) => ({ task: storedForm(t, 'user'), position: i * POSITION_STEP }));
+
+ it('plans nothing for an unchanged list, even when the caller holds clones', () => {
+ const previous = load([task('task-a'), task('task-b', { metadata: { openPR: true } })]);
+ const plan = planListSave({ previous, next: structuredClone(previous.map((row) => row.task)), source: 'user' });
+ expect(plan.upserts).toEqual([]);
+ expect(plan.deletes).toEqual([]);
+ expect(plan.saved).toEqual(previous.map((row) => row.task));
+ });
+
+ it('ignores a difference the stored form erases (undefined or null metadata)', () => {
+ const previous = load([task('task-a')]);
+ const next = [{ ...previous[0].task, metadata: { ...previous[0].task.metadata, gone: undefined, nothing: null } }];
+ expect(planListSave({ previous, next, source: 'user' }).upserts).toEqual([]);
+ });
+
+ it('upserts only changed and new tasks in stored form, and deletes ids that left the list', () => {
+ const previous = load([task('task-a'), task('task-b'), task('task-c')]);
+ const next = [previous[0].task, { ...previous[1].task, status: 'completed', metadata: { openPR: true } }, task('task-d')];
+ const plan = planListSave({ previous, next, source: 'user' });
+ expect(plan.upserts.map((row) => row.task.id)).toEqual(['task-b', 'task-d']);
+ expect(plan.upserts[0].task.metadata).toEqual({ openPR: 'true' });
+ expect(plan.upserts[0].position).toBe(1024);
+ expect(plan.deletes).toEqual(['task-c']);
+ expect(plan.saved.map((t) => t.id)).toEqual(['task-a', 'task-b', 'task-d']);
+ });
+
+ it('writes only the moved task when a list is reordered', () => {
+ const previous = load([task('task-a'), task('task-b'), task('task-c')]);
+ const plan = planListSave({ previous, next: [previous[2].task, previous[0].task, previous[1].task], source: 'user' });
+ expect(plan.upserts.map((row) => row.task.id)).toEqual(['task-c']);
+ expect(plan.deletes).toEqual([]);
+ });
+
+ it('keys the row by the id the stored form reads back, and stays stable across saves', () => {
+ const first = planListSave({ previous: [], next: [task('a')], source: 'user' });
+ expect(first.upserts.map((row) => row.task.id)).toEqual(['task-a']);
+ const previous = first.upserts.map((row) => ({ task: row.task, position: row.position }));
+ // The cached list now holds the stored id, so the next save changes nothing and deletes nothing.
+ const again = planListSave({ previous, next: previous.map((row) => row.task), source: 'user' });
+ expect(again.upserts).toEqual([]);
+ expect(again.deletes).toEqual([]);
+ });
+
+ it('collapses a duplicate id to its last occurrence', () => {
+ const plan = planListSave({ previous: [], next: [task('task-a'), task('task-b'), task('task-a', { description: 'Later copy' })], source: 'user' });
+ expect(plan.saved.map((t) => t.id)).toEqual(['task-a', 'task-b']);
+ expect(plan.saved[0].description).toBe('Later copy');
+ });
+});
diff --git a/server/lib/db/schema/README.md b/server/lib/db/schema/README.md
index bbe7016205..acd332d2c3 100644
--- a/server/lib/db/schema/README.md
+++ b/server/lib/db/schema/README.md
@@ -30,7 +30,7 @@ init-db.sql, each with a one-line reason.
| `commissions.js` | `commissionsDdl` | Creative Commissions + feedback (machine-local) |
| `userActions.js` | `userActionsDdl` | Operator-action ledger — what the human did in the UI (machine-local) |
| `reviewQueueTriage.js` | `reviewQueueTriageDdl` | Machine-local Review Hub presentation markers — snooze, optional dismissal, and delivery generation keyed by canonical action identity |
-| `cosTaskArchive.js` | `cosTaskArchiveDdl` | Machine-local archive of completed CoS tasks (full records, prompts included) moved out of `TASKS.md` / `COS-TASKS.md` |
+| `cosTasks.js` | `cosTasksDdl` | The CoS task queues, one row per task (stored form, queue order, completed-at window) plus a per-queue write version |
| `cosAgentFeedback.js` | `cosAgentFeedbackDdl` | Durable machine-local references to completed manual CoS runs awaiting a rating (agent id + archive locator only) |
| `aiGraph.js` | `aiGraphDdl` | AI provider connection graph — connections, harness bindings, executable route bindings (machine-local) |
| `peerExecution.js` | `peerExecutionDdl` | Receiver-local permanent execution consumption and generation floors; no grants or federation |
@@ -52,7 +52,7 @@ init-db.sql, each with a one-line reason.
### Composer (`index.js`)
-- `buildUpgradeDdl()` → phase-1 list (`core` → `tribe` → `humanActivity` → `post` → `commissions` → `userActions` → `reviewQueueTriage` → `cosAgentFeedback` → `cosTaskArchive` → `aiGraph` → `peerExecution`).
+- `buildUpgradeDdl()` → phase-1 list (`core` → `tribe` → `humanActivity` → `post` → `commissions` → `userActions` → `reviewQueueTriage` → `cosAgentFeedback` → `cosTasks` → `aiGraph` → `peerExecution`).
- `buildCatalogDdl()` → phase-2 list (`catalog` → `media` → `catalogUserTypes` →
`universes` → `library` → `pipeline` → `writersRoom` → `lora` → `privacy` → `stackerNews` → `x` →
`beeper` → `audit` DDL → audit triggers → `syncFeed` DDL → sync-feed triggers).
diff --git a/server/lib/db/schema/cosTaskArchive.js b/server/lib/db/schema/cosTaskArchive.js
deleted file mode 100644
index daf1d3d27f..0000000000
--- a/server/lib/db/schema/cosTaskArchive.js
+++ /dev/null
@@ -1,16 +0,0 @@
-// Machine-local archive of completed CoS tasks. The task files keep the open
-// queue plus a recent window of completed tasks; everything older lives here,
-// prompt bodies included (they are what made TASKS.md / COS-TASKS.md tens of MB).
-// `description` is a light projection so scans that only need titles never
-// detoast the JSONB. Never enters federation payloads.
-export const cosTaskArchiveDdl = [
- `CREATE TABLE IF NOT EXISTS cos_task_archive (
- source TEXT NOT NULL CHECK (source IN ('user', 'internal')),
- task_id TEXT COLLATE "C" NOT NULL,
- description TEXT NOT NULL DEFAULT '',
- completed_at TIMESTAMPTZ,
- archived_at TIMESTAMPTZ NOT NULL DEFAULT now(),
- task JSONB NOT NULL,
- PRIMARY KEY (source, task_id)
- )`,
-];
diff --git a/server/lib/db/schema/cosTasks.js b/server/lib/db/schema/cosTasks.js
new file mode 100644
index 0000000000..00341a632f
--- /dev/null
+++ b/server/lib/db/schema/cosTasks.js
@@ -0,0 +1,25 @@
+// The CoS task queues (user + internal), one row per task. `task` is the task in
+// its stored form (see lib/cosTaskRows.js); the other columns are the projections
+// the queries need: status for the open queue and history paging, priority and
+// position for queue order, completed_at for the recent-completed window.
+// `cos_task_lists.version` bumps on every write so a cached read can tell
+// whether anything changed without loading rows. Machine-local, never federated.
+export const cosTasksDdl = [
+ `CREATE TABLE IF NOT EXISTS cos_tasks (
+ source TEXT NOT NULL CHECK (source IN ('user', 'internal')),
+ task_id TEXT COLLATE "C" NOT NULL,
+ status TEXT NOT NULL,
+ priority_value INTEGER NOT NULL DEFAULT 2,
+ position DOUBLE PRECISION NOT NULL,
+ completed_at TIMESTAMPTZ,
+ description TEXT NOT NULL DEFAULT '',
+ task JSONB NOT NULL,
+ PRIMARY KEY (source, task_id)
+ )`,
+ `CREATE INDEX IF NOT EXISTS idx_cos_tasks_open ON cos_tasks (source, status) WHERE status <> 'completed'`,
+ `CREATE INDEX IF NOT EXISTS idx_cos_tasks_completed ON cos_tasks (source, completed_at DESC NULLS LAST, task_id DESC) WHERE status = 'completed'`,
+ `CREATE TABLE IF NOT EXISTS cos_task_lists (
+ source TEXT PRIMARY KEY CHECK (source IN ('user', 'internal')),
+ version BIGINT NOT NULL DEFAULT 0
+ )`,
+];
diff --git a/server/lib/db/schema/index.js b/server/lib/db/schema/index.js
index 893d194e71..c5d654898f 100644
--- a/server/lib/db/schema/index.js
+++ b/server/lib/db/schema/index.js
@@ -21,7 +21,7 @@ import { commissionsDdl } from './commissions.js';
import { userActionsDdl } from './userActions.js';
import { reviewQueueTriageDdl } from './reviewQueueTriage.js';
import { cosAgentFeedbackDdl } from './cosAgentFeedback.js';
-import { cosTaskArchiveDdl } from './cosTaskArchive.js';
+import { cosTasksDdl } from './cosTasks.js';
import { aiGraphDdl } from './aiGraph.js';
import { peerExecutionDdl } from './peerExecution.js';
import { catalogDdl, catalogUserTypesDdl, catalogPendingAppliesDdl } from './catalog.js';
@@ -51,7 +51,7 @@ export {
userActionsDdl,
reviewQueueTriageDdl,
cosAgentFeedbackDdl,
- cosTaskArchiveDdl,
+ cosTasksDdl,
aiGraphDdl,
peerExecutionDdl,
catalogDdl,
@@ -92,7 +92,7 @@ export function buildUpgradeDdl() {
...userActionsDdl,
...reviewQueueTriageDdl,
...cosAgentFeedbackDdl,
- ...cosTaskArchiveDdl,
+ ...cosTasksDdl,
...aiGraphDdl,
...peerExecutionDdl,
];
diff --git a/server/lib/db/schema/schema.test.js b/server/lib/db/schema/schema.test.js
index f3af0bea3d..f22cd5849c 100644
--- a/server/lib/db/schema/schema.test.js
+++ b/server/lib/db/schema/schema.test.js
@@ -25,7 +25,7 @@ import {
} from './index.js';
const DOMAIN_ARRAYS = [
- 'coreDdl', 'tribeDdl', 'humanActivityDdl', 'postDdl', 'commissionsDdl', 'userActionsDdl', 'reviewQueueTriageDdl', 'cosAgentFeedbackDdl', 'cosTaskArchiveDdl', 'aiGraphDdl', 'peerExecutionDdl',
+ 'coreDdl', 'tribeDdl', 'humanActivityDdl', 'postDdl', 'commissionsDdl', 'userActionsDdl', 'reviewQueueTriageDdl', 'cosAgentFeedbackDdl', 'cosTasksDdl', 'aiGraphDdl', 'peerExecutionDdl',
'catalogDdl', 'catalogIdentityDdl', 'catalogUserTypesDdl', 'mediaDdl', 'universesDdl',
'libraryDdl', 'pipelineDdl', 'writersRoomDdl', 'loraDdl', 'privacyDdl', 'stackerNewsDdl', 'xDdl',
'beeperDdl', 'mindToolRecipesDdl',
@@ -50,7 +50,7 @@ describe('db/schema barrel + composer (#2832)', () => {
...schema.userActionsDdl,
...schema.reviewQueueTriageDdl,
...schema.cosAgentFeedbackDdl,
- ...schema.cosTaskArchiveDdl,
+ ...schema.cosTasksDdl,
...schema.aiGraphDdl,
...schema.peerExecutionDdl,
];
diff --git a/server/lib/index.js b/server/lib/index.js
index 037a6a44df..6d529bcfbd 100644
--- a/server/lib/index.js
+++ b/server/lib/index.js
@@ -493,6 +493,7 @@ export * from './streamJsonParser.js';
export * from './taskParser.js';
export * from './taskGenerationOverrides.js';
export * from './cosTaskPrompt.js';
+export * from './cosTaskRows.js';
export * from './taskPauseHold.js';
export * from './taskBlockCategories.js';
export * from './taskRequeue.js';
diff --git a/server/lib/peerSyncValidation.js b/server/lib/peerSyncValidation.js
index 54f9a3725b..671cc156bd 100644
--- a/server/lib/peerSyncValidation.js
+++ b/server/lib/peerSyncValidation.js
@@ -366,10 +366,10 @@ export const peerCosHistoryManifestSchema = z.object({
}).strict();
// --- Live CoS task-list + claim-metadata federation (#1712) --------------
-// A full-sync peer advertises its live task backlog (both the user TASKS.md and
-// the internal COS-TASKS.md) at GET /api/peer-sync/cos-tasks; a receiver (only
+// A full-sync peer advertises its live task backlog (both the user and the
+// internal queue) at GET /api/peer-sync/cos-tasks; a receiver (only
// for peers it flags fullSync) runs a claim-aware per-task LWW merge into its own
-// task files (see cosTaskStore.mergePeerTasks). Unlike the cos-history archives
+// task queues (see cosTaskStore.mergePeerTasks). Unlike the cos-history archives
// this is NOT byte replication — both peers mutate the files live and each task
// carries claim/lease metadata (#1563), so the merge unions by id and respects
// `leaseExpiresAt` so a peer's fresh claim is never clobbered.
diff --git a/server/lib/schemaVersions.js b/server/lib/schemaVersions.js
index c3eab2952d..d8b8946f08 100644
--- a/server/lib/schemaVersions.js
+++ b/server/lib/schemaVersions.js
@@ -796,7 +796,7 @@ export const PORTOS_SCHEMA_VERSIONS = Object.freeze({
// #1650, part of epic #1561). NOT a record kind — it's the wire contract for
// the task payload a full-sync peer advertises at GET /api/peer-sync/cos-tasks.
// Unlike the immutable, append-only `cosHistory` archives (pure byte
- // replication), the task files (data/COS-TASKS.md / data/TASKS.md) are mutated
+ // replication), the task queues (`cos_tasks`, both sources) are mutated
// live by BOTH peers and carry `claimedBy`/`claimedAt`/`leaseExpiresAt` (#1563),
// so they ride a claim-aware per-task LWW merge (syncCosTasksFromPeer →
// cosTaskStore.mergePeerTasks), not the asset path. The receiver reads the
diff --git a/server/lib/taskParser.js b/server/lib/taskParser.js
index b4ca13a75b..494c77e050 100644
--- a/server/lib/taskParser.js
+++ b/server/lib/taskParser.js
@@ -1,7 +1,13 @@
/**
- * Task Parser for TASKS.md format
+ * Task Parser for the Markdown task format
*
- * Parses markdown task files with the following format:
+ * The CoS task queues are rows in `cos_tasks` now. This format survives as two
+ * things: a task's STORED FORM (`lib/cosTaskRows.js` normalizes every task by
+ * round-tripping it through one row, which is why scalar metadata reads back as
+ * strings; comments elsewhere that mention a "TASKS.md round-trip" mean that),
+ * and the input of db-migration 018, the only code that reads whole task files.
+ *
+ * Parses markdown task text with the following format:
*
* # Tasks
*
diff --git a/server/routes/cos.test.js b/server/routes/cos.test.js
index 54a5f47d4d..cb5780c4c2 100644
--- a/server/routes/cos.test.js
+++ b/server/routes/cos.test.js
@@ -84,14 +84,6 @@ vi.mock('../services/agentOrchestrator.js', () => ({
getAgentProcessStats: vi.fn()
}));
-// Mock the taskWatcher service
-vi.mock('../services/taskWatcher.js', () => ({
- startWatching: vi.fn(),
- stopWatching: vi.fn(),
- refreshTasks: vi.fn(),
- getWatcherStatus: vi.fn()
-}));
-
// Mock the appActivity service
vi.mock('../services/appActivity.js', () => ({
loadAppActivity: vi.fn(),
@@ -148,7 +140,6 @@ vi.mock('../services/codeReview.js', () => ({
// Import mocked modules
import * as cos from '../services/cos.js';
import * as agentOrchestrator from '../services/agentOrchestrator.js';
-import * as taskWatcher from '../services/taskWatcher.js';
import * as appActivity from '../services/appActivity.js';
import * as claudeChangelog from '../services/claudeChangelog.js';
import { enhanceTaskPrompt } from '../services/taskEnhancer.js';
@@ -194,28 +185,24 @@ describe('CoS Routes', () => {
describe('POST /api/cos/start', () => {
it('should start CoS daemon', async () => {
cos.start.mockResolvedValue({ success: true });
- taskWatcher.startWatching.mockResolvedValue();
const response = await request(app).post('/api/cos/start');
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(cos.start).toHaveBeenCalled();
- expect(taskWatcher.startWatching).toHaveBeenCalled();
});
});
describe('POST /api/cos/stop', () => {
it('should stop CoS daemon', async () => {
cos.stop.mockResolvedValue({ success: true });
- taskWatcher.stopWatching.mockResolvedValue();
const response = await request(app).post('/api/cos/stop');
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
expect(cos.stop).toHaveBeenCalled();
- expect(taskWatcher.stopWatching).toHaveBeenCalled();
});
});
@@ -892,143 +879,6 @@ describe('CoS Routes', () => {
});
});
- describe('GET /api/cos/watcher', () => {
- it('should return watcher status', async () => {
- taskWatcher.getWatcherStatus.mockReturnValue({
- watching: true,
- files: ['TASKS.md']
- });
-
- const response = await request(app).get('/api/cos/watcher');
-
- expect(response.status).toBe(200);
- expect(response.body.watching).toBe(true);
- });
- });
-
- describe('GET /api/cos/app-activity', () => {
- it('should return app activity data', async () => {
- appActivity.loadAppActivity.mockResolvedValue({
- 'app-001': { lastReview: '2024-01-15T10:00:00Z' }
- });
-
- const response = await request(app).get('/api/cos/app-activity');
-
- expect(response.status).toBe(200);
- });
- });
-
- describe('GET /api/cos/app-activity/:appId', () => {
- it('should return activity for specific app', async () => {
- appActivity.getAppActivityById.mockResolvedValue({
- lastReview: '2024-01-15T10:00:00Z'
- });
-
- const response = await request(app).get('/api/cos/app-activity/app-001');
-
- expect(response.status).toBe(200);
- expect(response.body.appId).toBe('app-001');
- });
-
- it('should return message if no activity', async () => {
- appActivity.getAppActivityById.mockResolvedValue(null);
-
- const response = await request(app).get('/api/cos/app-activity/app-999');
-
- expect(response.status).toBe(200);
- expect(response.body.activity).toBeNull();
- expect(response.body.message).toBeDefined();
- });
- });
-
- describe('POST /api/cos/app-activity/:appId/clear-cooldown', () => {
- it('should clear cooldown for app', async () => {
- appActivity.clearAppCooldown.mockResolvedValue({ cooldownCleared: true });
-
- const response = await request(app).post('/api/cos/app-activity/app-001/clear-cooldown');
-
- expect(response.status).toBe(200);
- expect(response.body.success).toBe(true);
- expect(appActivity.clearAppCooldown).toHaveBeenCalledWith('app-001');
- });
- });
-
- describe('POST /api/cos/tasks/:id/spawn', () => {
- it('should force-spawn a pending task', async () => {
- cos.forceSpawnTask.mockResolvedValue({ success: true, taskId: 'task-001' });
-
- const response = await request(app).post('/api/cos/tasks/task-001/spawn');
-
- expect(response.status).toBe(200);
- expect(response.body.success).toBe(true);
- expect(cos.forceSpawnTask).toHaveBeenCalledWith('task-001');
- });
-
- it('should return 404 when task not found', async () => {
- cos.forceSpawnTask.mockResolvedValue({ error: 'Task not found' });
-
- const response = await request(app).post('/api/cos/tasks/bad-id/spawn');
-
- expect(response.status).toBe(404);
- expect(response.body.code).toBe('NOT_FOUND');
- });
-
- it('should return 409 when task is not pending', async () => {
- cos.forceSpawnTask.mockResolvedValue({ error: 'Task is completed, not pending' });
-
- const response = await request(app).post('/api/cos/tasks/task-002/spawn');
-
- expect(response.status).toBe(409);
- expect(response.body.code).toBe('TASK_NOT_PENDING');
- });
-
- it('should return 429 when no agent slots available', async () => {
- cos.forceSpawnTask.mockResolvedValue({ error: 'No available agent slots (3/3)' });
-
- const response = await request(app).post('/api/cos/tasks/task-003/spawn');
-
- expect(response.status).toBe(429);
- expect(response.body.code).toBe('NO_CAPACITY');
- });
- });
-
- // ============================================================
- // Task Routes — additional coverage
- // ============================================================
-
- describe('GET /api/cos/tasks/user', () => {
- it('should return user tasks', async () => {
- cos.getUserTasks.mockResolvedValue({ tasks: [{ id: 't1' }], grouped: {} });
-
- const response = await request(app).get('/api/cos/tasks/user');
-
- expect(response.status).toBe(200);
- expect(response.body.tasks).toHaveLength(1);
- });
- });
-
- describe('GET /api/cos/tasks/internal', () => {
- it('should return internal tasks', async () => {
- cos.getCosTasks.mockResolvedValue({ tasks: [], grouped: {} });
-
- const response = await request(app).get('/api/cos/tasks/internal');
-
- expect(response.status).toBe(200);
- expect(response.body).toHaveProperty('tasks');
- });
- });
-
- describe('POST /api/cos/tasks/refresh', () => {
- it('should force refresh tasks', async () => {
- taskWatcher.refreshTasks.mockResolvedValue({ user: [], cos: [] });
-
- const response = await request(app).post('/api/cos/tasks/refresh');
-
- expect(response.status).toBe(200);
- expect(taskWatcher.refreshTasks).toHaveBeenCalled();
- });
- });
-
describe('POST /api/cos/tasks/enhance', () => {
it('should enhance a task prompt', async () => {
enhanceTaskPrompt.mockResolvedValue({ enhanced: 'Better description' });
diff --git a/server/routes/cosReportRoutes.js b/server/routes/cosReportRoutes.js
index d3e8e7910d..8b760f42b8 100644
--- a/server/routes/cosReportRoutes.js
+++ b/server/routes/cosReportRoutes.js
@@ -5,7 +5,6 @@
import { Router } from 'express';
import { z } from 'zod';
import * as cos from '../services/cos.js';
-import * as taskWatcher from '../services/taskWatcher.js';
import * as appActivity from '../services/appActivity.js';
import * as claudeChangelog from '../services/claudeChangelog.js';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
@@ -115,11 +114,6 @@ router.get('/scripts/:name', asyncHandler(async (req, res) => {
res.json(script);
}));
-// GET /api/cos/watcher - Get watcher status
-router.get('/watcher', (req, res) => {
- res.json(taskWatcher.getWatcherStatus());
-});
-
// GET /api/cos/app-activity - Get per-app activity data
router.get('/app-activity', asyncHandler(async (req, res) => {
const activity = await appActivity.loadAppActivity();
diff --git a/server/routes/cosStatusRoutes.js b/server/routes/cosStatusRoutes.js
index e37fa56a40..6281caffd3 100644
--- a/server/routes/cosStatusRoutes.js
+++ b/server/routes/cosStatusRoutes.js
@@ -6,7 +6,6 @@ import { persistentMindMaintainerSchema } from '../lib/persistentMindMaintainer.
import { Router } from 'express';
import * as cos from '../services/cos.js';
import { getAllDomainUsageToday } from '../services/domainUsage.js';
-import * as taskWatcher from '../services/taskWatcher.js';
import { reinitialize as reinitializeEmbeddings } from '../services/memoryEmbeddings.js';
import { asyncHandler } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
@@ -30,8 +29,6 @@ const pauseSchema = z.object({
});
export const cosConfigSchema = z.object({
- userTasksFile: z.string().optional(),
- cosTasksFile: z.string().optional(),
goalsFile: z.string().optional(),
healthCheckIntervalMs: z.number().int().min(1000).optional(),
maxConcurrentAgents: z.number().int().min(1).optional(),
@@ -131,14 +128,12 @@ router.get('/', asyncHandler(async (req, res) => {
// POST /api/cos/start - Start CoS daemon
router.post('/start', asyncHandler(async (req, res) => {
const result = await cos.start();
- await taskWatcher.startWatching();
res.json(result);
}));
// POST /api/cos/stop - Stop CoS daemon
router.post('/stop', asyncHandler(async (req, res) => {
const result = await cos.stop();
- await taskWatcher.stopWatching();
res.json(result);
}));
diff --git a/server/routes/cosStatusRoutesAvatar.test.js b/server/routes/cosStatusRoutesAvatar.test.js
index 8f6a34f440..09139d7402 100644
--- a/server/routes/cosStatusRoutesAvatar.test.js
+++ b/server/routes/cosStatusRoutesAvatar.test.js
@@ -6,7 +6,6 @@ import { describe, it, expect, vi } from 'vitest';
// under test here.
vi.mock('../services/cos.js', () => ({}));
vi.mock('../services/domainUsage.js', () => ({ getAllDomainUsageToday: vi.fn() }));
-vi.mock('../services/taskWatcher.js', () => ({}));
vi.mock('../services/memoryEmbeddings.js', () => ({ reinitialize: vi.fn() }));
import { cosConfigSchema } from './cosStatusRoutes.js';
diff --git a/server/routes/cosTaskRoutes.js b/server/routes/cosTaskRoutes.js
index 146f141a78..27130146bb 100644
--- a/server/routes/cosTaskRoutes.js
+++ b/server/routes/cosTaskRoutes.js
@@ -7,7 +7,6 @@ import { z } from 'zod';
import { existsSync } from 'fs';
import { isRepo, notGitRepoMessage } from '../services/git.js';
import * as cos from '../services/cos.js';
-import * as taskWatcher from '../services/taskWatcher.js';
import { enhanceTaskPrompt } from '../services/taskEnhancer.js';
import { buildClaimWorkTask, buildIssueReplanTask, buildJiraTicketTask } from '../services/cosTaskClaimWork.js';
import { getAppById, getAppWorkTracker, PORTOS_APP_ID } from '../services/apps.js';
@@ -256,12 +255,6 @@ router.get('/tasks/:id', asyncHandler(async (req, res) => {
res.json(found);
}));
-// POST /api/cos/tasks/refresh - Force refresh tasks
-router.post('/tasks/refresh', asyncHandler(async (req, res) => {
- const tasks = await taskWatcher.refreshTasks();
- res.json(tasks);
-}));
-
// POST /api/cos/tasks/reorder - Reorder tasks
router.post('/tasks/reorder', asyncHandler(async (req, res) => {
const { taskIds } = validateRequest(cosTaskReorderBodySchema, req.body);
diff --git a/server/routes/cosTaskRoutes.test.js b/server/routes/cosTaskRoutes.test.js
index 6ec4243799..11847000ea 100644
--- a/server/routes/cosTaskRoutes.test.js
+++ b/server/routes/cosTaskRoutes.test.js
@@ -60,7 +60,6 @@ vi.mock('../services/cos.js', () => ({
reviveBlockedTask: vi.fn(),
forceSpawnTask: vi.fn(),
}));
-vi.mock('../services/taskWatcher.js', () => ({ refreshTasks: vi.fn() }));
vi.mock('../services/taskEnhancer.js', () => ({ enhanceTaskPrompt: vi.fn() }));
vi.mock('../services/cosTaskClaimWork.js', () => ({
buildClaimWorkTask: vi.fn(),
diff --git a/server/routes/peerSync.js b/server/routes/peerSync.js
index 36eca98aeb..b71d695e26 100644
--- a/server/routes/peerSync.js
+++ b/server/routes/peerSync.js
@@ -196,8 +196,8 @@ router.get('/cos-history-manifest', asyncHandler(async (req, res) => {
// a full-sync peer can run a claim-aware per-task merge into its own task files.
//
// Returns `{ schemaVersion, listHash, tasks: [{ id, taskType, status, priority,
-// description, metadata, ... }] }` over the user (TASKS.md) + internal
-// (COS-TASKS.md) backlogs. Unlike the cos-history archives this is a MERGE
+// description, metadata, ... }] }` over the user + internal task
+// queues' working sets. Unlike the cos-history archives this is a MERGE
// source, not byte replication — the task metadata carries claim/lease fields
// (#1563) the receiver respects so a peer's fresh claim is never clobbered.
// Unauthenticated like every peer-sync route (Tailnet-only per the threat model);
diff --git a/server/scripts/db-migrations/018-cos-tasks-from-files.db.test.js b/server/scripts/db-migrations/018-cos-tasks-from-files.db.test.js
new file mode 100644
index 0000000000..bae451c4bf
--- /dev/null
+++ b/server/scripts/db-migrations/018-cos-tasks-from-files.db.test.js
@@ -0,0 +1,96 @@
+/**
+ * Migrations 018 + 019 against a real PostgreSQL: the Markdown task files and the
+ * interim `cos_task_archive` table become `cos_tasks` rows, nothing is lost or
+ * altered by the import, and the files are only set aside afterwards.
+ *
+ * Runs via `npm run test:db` (→ `portos_test`) ONLY; skips cleanly when no
+ * database is reachable.
+ */
+import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
+import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const paths = vi.hoisted(() => ({ root: '' }));
+vi.mock('../../lib/paths.js', async (importOriginal) => {
+ const actual = await importOriginal();
+ const override = { root: () => paths.root, data: () => `${paths.root}/data`, cos: () => `${paths.root}/data/cos` };
+ return { ...actual, PATHS: new Proxy(actual.PATHS, { get: (target, key) => (key in override ? override[key]() : target[key]) }) };
+});
+
+const { checkHealth, close, ensureSchema, query, withTransaction } = await import('../../lib/db.js');
+const { requireDbOrSkip } = await import('../../lib/dbTestGate.js');
+const { up: importUp } = await import('./018-cos-tasks-from-files.js');
+const { up: retireUp } = await import('./019-retire-cos-task-files.js');
+const { parseTasksMarkdown, generateTasksMarkdown } = await import('../../lib/taskParser.js');
+
+const health = await checkHealth().catch(() => ({ connected: false }));
+if (health.connected) await ensureSchema();
+const runDb = requireDbOrSkip('db-migrations/018-cos-tasks-from-files.db.test', health.connected, 'disposable PostgreSQL unavailable');
+
+const clean = async () => {
+ await query('DELETE FROM cos_tasks');
+ await query('DELETE FROM cos_task_lists');
+ await query('DROP TABLE IF EXISTS cos_task_archive');
+};
+
+beforeAll(async () => {
+ if (!runDb) return;
+ paths.root = mkdtempSync(join(tmpdir(), 'cos-task-migration-db-'));
+ await clean();
+});
+afterAll(async () => {
+ if (runDb) await clean();
+ if (paths.root) rmSync(paths.root, { recursive: true, force: true });
+ if (health.connected) await close();
+});
+
+describe.skipIf(!runDb)('migrations 018 + 019 — CoS task queues into PostgreSQL', () => {
+ it('imports both files and the interim archive, keeps every task intact, then retires the files', async () => {
+ const make = (n) => ({
+ id: `task-example-${String(n).padStart(4, '0')}`,
+ description: `Example task ${n} "quoted" and unicode: café`,
+ status: n % 5 === 0 ? 'completed' : 'pending',
+ priority: n % 3 === 0 ? 'HIGH' : 'LOW',
+ metadata: { prompt: `Line one\nLine two\twith "quotes", \\backslash and emoji 🚀 #${n}`.repeat(10), updatedAt: '2026-10-01T00:00:00.000Z', openPR: true },
+ });
+ const userTasks = Array.from({ length: 230 }, (_, n) => make(n));
+ mkdirSync(join(paths.root, 'data'), { recursive: true });
+ writeFileSync(join(paths.root, 'data/TASKS.md'), generateTasksMarkdown(userTasks));
+ writeFileSync(join(paths.root, 'data/COS-TASKS.md'), generateTasksMarkdown(
+ [{ id: 'sys-example-1', description: 'System work', status: 'pending', priority: 'HIGH', approvalRequired: false, autoApproved: true, metadata: {} }], true));
+ const expected = parseTasksMarkdown(generateTasksMarkdown(userTasks));
+
+ // The interim archive: one row also in the file (the sweep's crash window), two only here.
+ await query(`CREATE TABLE cos_task_archive (source TEXT NOT NULL, task_id TEXT COLLATE "C" NOT NULL, description TEXT NOT NULL DEFAULT '',
+ completed_at TIMESTAMPTZ, archived_at TIMESTAMPTZ NOT NULL DEFAULT now(), task JSONB NOT NULL, PRIMARY KEY (source, task_id))`);
+ for (const [id, description] of [[expected[0].id, 'Stale archive copy'], ['task-archived-1', 'Archived one'], ['task-archived-2', 'Archived two']]) {
+ const task = { id, description, status: 'completed', priority: 'MEDIUM', priorityValue: 2, metadata: { updatedAt: '2026-09-01T00:00:00.000Z' } };
+ await query(`INSERT INTO cos_task_archive (source, task_id, description, completed_at, task) VALUES ('user', $1, $2, '2026-09-01T00:00:00Z', $3::jsonb)`,
+ [id, description, JSON.stringify(task)]);
+ }
+
+ await withTransaction(importUp);
+
+ const counts = (await query('SELECT source, count(*)::int AS n FROM cos_tasks GROUP BY source ORDER BY source')).rows;
+ expect(counts).toEqual([{ source: 'internal', n: 1 }, { source: 'user', n: 232 }]);
+ const stored = new Map((await query(`SELECT task_id, task FROM cos_tasks WHERE source = 'user'`)).rows.map((row) => [row.task_id, row.task]));
+ // The file copy won over the stale archive copy, and every file task is byte-for-byte what the reader returned.
+ for (const task of expected) expect(stored.get(task.id)).toEqual(JSON.parse(JSON.stringify(task)));
+ expect(stored.get('task-archived-2').description).toBe('Archived two');
+ expect((await query(`SELECT to_regclass('cos_task_archive') AS archive`)).rows[0].archive).toBeNull();
+ const ordered = (await query(`SELECT task_id FROM cos_tasks WHERE source = 'user' AND task_id LIKE 'task-example-%' ORDER BY position LIMIT 3`)).rows;
+ expect(ordered.map((row) => row.task_id)).toEqual(expected.slice(0, 3).map((task) => task.id));
+ expect((await query(`SELECT count(*)::int AS n FROM cos_tasks WHERE status = 'completed' AND completed_at IS NOT NULL`)).rows[0].n).toBeGreaterThan(40);
+
+ // Files are only set aside after the import, and kept.
+ expect(existsSync(join(paths.root, 'data/TASKS.md'))).toBe(true);
+ await retireUp();
+ expect(existsSync(join(paths.root, 'data/TASKS.md'))).toBe(false);
+ expect(existsSync(join(paths.root, 'data/TASKS.md.imported'))).toBe(true);
+
+ // With the files gone and the archive dropped, a re-run changes nothing.
+ await withTransaction(importUp);
+ expect((await query('SELECT count(*)::int AS n FROM cos_tasks')).rows[0].n).toBe(233);
+ });
+});
diff --git a/server/scripts/db-migrations/018-cos-tasks-from-files.js b/server/scripts/db-migrations/018-cos-tasks-from-files.js
new file mode 100644
index 0000000000..f894d5f034
--- /dev/null
+++ b/server/scripts/db-migrations/018-cos-tasks-from-files.js
@@ -0,0 +1,92 @@
+/**
+ * Move the CoS task queues into PostgreSQL.
+ *
+ * Until now the queues were two Markdown files (`data/TASKS.md`,
+ * `data/COS-TASKS.md`), plus, on installs that ran the interim completed-task
+ * archive, a `cos_task_archive` table. Both now live in `cos_tasks`, one row per
+ * task. This migration is the only code that still reads the Markdown format.
+ *
+ * Input-derived, so it ships no `data.reference` seed and gates on its INPUT (the
+ * files, and the archive table when present), never on the absence of its output.
+ * A task already in `cos_tasks` is left alone (`DO NOTHING`), which keeps a re-run
+ * and a file-plus-archive duplicate (the archive sweep's crash window) harmless;
+ * the file copy is inserted first, so it wins. Counts are verified before commit:
+ * every task parsed from a file must exist as a row, or the migration throws and
+ * the whole transaction rolls back to be retried on the next boot.
+ *
+ * The files are retired separately by 019, only after this one has committed.
+ */
+import { readFile } from 'node:fs/promises';
+import { parseTasksMarkdown } from '../../lib/taskParser.js';
+import { resolveCosTaskFiles } from './_cosTaskFiles.js';
+
+const POSITION_STEP = 1024;
+const BATCH = 100;
+
+// Frozen copy of the projection columns the repository derives from a stored task.
+function rowColumns(task) {
+ const stamp = task.status === 'completed' ? Date.parse(task.metadata?.completedAt || task.metadata?.updatedAt || '') : NaN;
+ return {
+ status: task.status,
+ priorityValue: Number.isFinite(task.priorityValue) ? task.priorityValue : 2,
+ completedAt: Number.isFinite(stamp) && stamp > 0 ? new Date(stamp).toISOString() : null,
+ description: typeof task.description === 'string' ? task.description : '',
+ };
+}
+
+async function insertTasks(client, source, tasks) {
+ for (let start = 0; start < tasks.length; start += BATCH) {
+ const rows = tasks.slice(start, start + BATCH).map((task, offset) => ({ task, position: (start + offset) * POSITION_STEP, ...rowColumns(task) }));
+ await client.query(
+ `INSERT INTO cos_tasks (source, task_id, status, priority_value, position, completed_at, description, task)
+ SELECT $1, u.task_id, u.status, u.priority_value, u.position, u.completed_at, u.description, u.task::jsonb
+ FROM unnest($2::text[], $3::text[], $4::int[], $5::float8[], $6::timestamptz[], $7::text[], $8::text[])
+ AS u(task_id, status, priority_value, position, completed_at, description, task)
+ ON CONFLICT (source, task_id) DO NOTHING`,
+ [source, rows.map((r) => r.task.id), rows.map((r) => r.status), rows.map((r) => r.priorityValue),
+ rows.map((r) => r.position), rows.map((r) => r.completedAt), rows.map((r) => r.description),
+ rows.map((r) => JSON.stringify(r.task))],
+ );
+ }
+}
+
+async function importFile(client, { source, path }) {
+ const text = await readFile(path, 'utf-8').catch((err) => {
+ if (err.code === 'ENOENT') return null;
+ throw err;
+ });
+ if (text === null) return 0;
+ const tasks = parseTasksMarkdown(text);
+ await insertTasks(client, source, tasks);
+ const ids = tasks.map((task) => task.id);
+ const { rows } = await client.query('SELECT count(*)::int AS n FROM cos_tasks WHERE source = $1 AND task_id = ANY($2::text[])', [source, ids]);
+ if (rows[0].n !== ids.length) {
+ throw new Error(`CoS task import verification failed for ${source}: ${rows[0].n} of ${ids.length} tasks are stored`);
+ }
+ return ids.length;
+}
+
+async function foldInArchive(client) {
+ const { rows } = await client.query(`SELECT to_regclass('cos_task_archive') AS archive`);
+ if (!rows[0]?.archive) return 0;
+ const { rowCount } = await client.query(
+ `INSERT INTO cos_tasks (source, task_id, status, priority_value, position, completed_at, description, task)
+ SELECT source, task_id, COALESCE(task->>'status', 'completed'), COALESCE((task->>'priorityValue')::int, 2), 0,
+ completed_at, description, task
+ FROM cos_task_archive
+ ON CONFLICT (source, task_id) DO NOTHING`,
+ );
+ await client.query('DROP TABLE cos_task_archive');
+ return rowCount;
+}
+
+export async function up(client) {
+ let fromFiles = 0;
+ for (const file of await resolveCosTaskFiles()) fromFiles += await importFile(client, file);
+ const fromArchive = await foldInArchive(client);
+ await client.query(
+ `INSERT INTO cos_task_lists (source, version) VALUES ('user', 1), ('internal', 1)
+ ON CONFLICT (source) DO UPDATE SET version = cos_task_lists.version + 1`,
+ );
+ console.log(`🗄️ CoS tasks: imported ${fromFiles} from the task files and ${fromArchive} from the archive table`);
+}
diff --git a/server/scripts/db-migrations/018-cos-tasks-from-files.test.js b/server/scripts/db-migrations/018-cos-tasks-from-files.test.js
new file mode 100644
index 0000000000..5cf381a9cb
--- /dev/null
+++ b/server/scripts/db-migrations/018-cos-tasks-from-files.test.js
@@ -0,0 +1,143 @@
+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const paths = vi.hoisted(() => ({ root: '' }));
+vi.mock('../../lib/paths.js', () => ({
+ PATHS: { get root() { return paths.root; }, get data() { return `${paths.root}/data`; }, get cos() { return `${paths.root}/data/cos`; } },
+}));
+
+const { up } = await import('./018-cos-tasks-from-files.js');
+
+const USER_MARKDOWN = `# Tasks
+
+## Pending
+- [ ] #task-example-b | LOW | Second pending
+- [ ] #task-example-a | HIGH | First pending
+ - app: example-app
+ - openPR: true
+
+## Completed
+- [x] #task-example-done | MEDIUM | Finished work
+ - updatedAt: 2026-10-01T00:00:00.000Z
+ - prompt: Example prompt body
+`;
+const INTERNAL_MARKDOWN = `# Tasks
+
+## Pending
+- [ ] #sys-example | HIGH | AUTO | System work
+`;
+
+let queries;
+const client = (overrides = {}) => ({
+ query: vi.fn(async (sql, params) => {
+ queries.push({ sql, params });
+ if (/SELECT count\(\*\)/.test(sql)) return { rows: [{ n: params[1].length }] };
+ if (/to_regclass/.test(sql)) return { rows: [{ archive: null }] };
+ return { rows: [], rowCount: 0 };
+ }),
+ ...overrides,
+});
+const inserts = () => queries.filter((q) => /INSERT INTO cos_tasks \(source/.test(q.sql) && q.params?.[0]);
+const write = (rel, text) => { mkdirSync(join(paths.root, rel, '..'), { recursive: true }); writeFileSync(join(paths.root, rel), text); };
+
+beforeEach(() => {
+ rmSync(paths.root, { recursive: true, force: true });
+ paths.root = mkdtempSync(join(tmpdir(), 'cos-task-import-'));
+ queries = [];
+});
+afterAll(() => rmSync(paths.root, { recursive: true, force: true }));
+
+describe('db-migration 018 — CoS tasks from the Markdown files', () => {
+ it('imports both queues in file order with stored-form rows and projection columns', async () => {
+ write('data/TASKS.md', USER_MARKDOWN);
+ write('data/COS-TASKS.md', INTERNAL_MARKDOWN);
+ await up(client());
+
+ const [user, internal] = inserts();
+ expect(user.params[0]).toBe('user');
+ expect(internal.params[0]).toBe('internal');
+ const [, ids, statuses, priorities, positions, completedAt, descriptions, stored] = user.params;
+ // File order is kept in `position`; the queue's priority order is applied when it is read.
+ expect(ids).toEqual(['task-example-b', 'task-example-a', 'task-example-done']);
+ expect(statuses).toEqual(['pending', 'pending', 'completed']);
+ expect(priorities).toEqual([1, 3, 2]);
+ expect(positions).toEqual([0, 1024, 2048]);
+ expect(completedAt).toEqual([null, null, '2026-10-01T00:00:00.000Z']);
+ expect(descriptions[1]).toBe('First pending');
+ // Scalar metadata is stored as the Markdown reader returned it (strings).
+ expect(JSON.parse(stored[1]).metadata).toEqual({ app: 'example-app', openPR: 'true' });
+ expect(JSON.parse(stored[2]).metadata.prompt).toBe('Example prompt body');
+ expect(internal.params[1]).toEqual(['sys-example']);
+ });
+
+ it('is input-gated: a missing file contributes nothing, and no file at all inserts nothing', async () => {
+ write('data/COS-TASKS.md', INTERNAL_MARKDOWN);
+ await up(client());
+ expect(inserts().map((q) => q.params[0])).toEqual(['internal']);
+
+ queries = [];
+ rmSync(join(paths.root, 'data/COS-TASKS.md'));
+ await up(client());
+ expect(inserts()).toEqual([]);
+ });
+
+ it('honors a configured file location the way the old reader did', async () => {
+ write('data/cos/config.json', JSON.stringify({ userTasksFile: 'custom/mine.md', cosTasksFile: 'custom/system.md' }));
+ write('custom/mine.md', USER_MARKDOWN);
+ write('data/TASKS.md', '# Tasks\n\n## Pending\n- [ ] #task-ignored | LOW | Default location is ignored\n');
+ await up(client());
+ expect(inserts()).toHaveLength(1);
+ expect(inserts()[0].params[1]).toContain('task-example-a');
+ expect(inserts()[0].params[1]).not.toContain('task-ignored');
+ });
+
+ it('falls back to the legacy state.json config when there is no config.json', async () => {
+ write('data/cos/state.json', JSON.stringify({ config: { userTasksFile: 'legacy/user.md' }, agents: {} }));
+ write('legacy/user.md', USER_MARKDOWN);
+ await up(client());
+ expect(inserts()[0].params[1]).toContain('task-example-a');
+ });
+
+ it('batches a large queue and keeps file order across batches', async () => {
+ const rows = Array.from({ length: 250 }, (_, n) => `- [ ] #task-bulk-${String(n).padStart(3, '0')} | MEDIUM | Bulk ${n}`).join('\n');
+ write('data/TASKS.md', `# Tasks\n\n## Pending\n${rows}\n`);
+ await up(client());
+ const batches = inserts();
+ expect(batches.map((q) => q.params[1].length)).toEqual([100, 100, 50]);
+ expect(batches[2].params[4][0]).toBe(200 * 1024);
+ });
+
+ it('throws, rolling the migration back, when a parsed task did not land as a row', async () => {
+ write('data/TASKS.md', USER_MARKDOWN);
+ const short = client({ query: vi.fn(async (sql) => (/SELECT count\(\*\)/.test(sql) ? { rows: [{ n: 2 }] } : { rows: [], rowCount: 0 })) });
+ await expect(up(short)).rejects.toThrow(/verification failed for user: 2 of 3/);
+ });
+
+ it('folds the interim archive table into cos_tasks and drops it, only when it exists', async () => {
+ write('data/TASKS.md', USER_MARKDOWN);
+ const withArchive = client({ query: vi.fn(async (sql, params) => {
+ queries.push({ sql, params });
+ if (/SELECT count\(\*\)/.test(sql)) return { rows: [{ n: params[1].length }] };
+ if (/to_regclass/.test(sql)) return { rows: [{ archive: 'cos_task_archive' }] };
+ return { rows: [], rowCount: 7 };
+ }) });
+ await up(withArchive);
+ const fold = queries.find((q) => /FROM cos_task_archive/.test(q.sql));
+ expect(fold.sql).toMatch(/ON CONFLICT \(source, task_id\) DO NOTHING/);
+ expect(queries.some((q) => /DROP TABLE cos_task_archive/.test(q.sql))).toBe(true);
+ // The file copy is inserted before the archive row can claim the key.
+ expect(queries.findIndex((q) => /INSERT INTO cos_tasks \(source/.test(q.sql) && q.params)).toBeLessThan(queries.indexOf(fold));
+
+ queries = [];
+ await up(client());
+ expect(queries.some((q) => /cos_task_archive/.test(q.sql) && !/to_regclass/.test(q.sql))).toBe(false);
+ });
+
+ it('bumps both queue versions so no earlier cache can match', async () => {
+ await up(client());
+ const bump = queries.find((q) => /INSERT INTO cos_task_lists/.test(q.sql));
+ expect(bump.sql).toMatch(/\('user', 1\), \('internal', 1\)/);
+ });
+});
diff --git a/server/scripts/db-migrations/019-retire-cos-task-files.js b/server/scripts/db-migrations/019-retire-cos-task-files.js
new file mode 100644
index 0000000000..6eb8d615b2
--- /dev/null
+++ b/server/scripts/db-migrations/019-retire-cos-task-files.js
@@ -0,0 +1,47 @@
+/**
+ * Retire the Markdown task files and the config keys that pointed at them.
+ *
+ * Runs after 018 has committed (migrations apply in order, each in its own
+ * transaction), so a task file is only ever set aside once its tasks are rows.
+ * Each file is renamed to `<name>.imported`, never deleted: that is the recovery
+ * copy, and nothing reads it. The two config keys are dropped from the CoS config
+ * because nothing reads them either. Both steps do nothing when there is nothing
+ * to do, so a re-run (or a fresh install) is a no-op.
+ */
+import { existsSync } from 'node:fs';
+import { rename } from 'node:fs/promises';
+import { join } from 'node:path';
+import { PATHS } from '../../lib/paths.js';
+import { readJsonOrNull, writeJsonAtomic } from '../../../scripts/migrations/_lib.js';
+import { resolveCosTaskFiles } from './_cosTaskFiles.js';
+
+const RETIRED_KEYS = ['userTasksFile', 'cosTasksFile'];
+
+async function setAsideFile(path) {
+ if (!existsSync(path)) return false;
+ const target = existsSync(`${path}.imported`) ? `${path}.imported-${Date.now()}` : `${path}.imported`;
+ await rename(path, target);
+ return true;
+}
+
+// config.json is the config; a legacy state.json nests it under `config`.
+async function dropRetiredKeys() {
+ for (const [name, pick] of [['config.json', (doc) => doc], ['state.json', (doc) => doc?.config]]) {
+ const path = join(PATHS.cos, name);
+ const doc = await readJsonOrNull(path);
+ const config = doc && pick(doc);
+ if (!config || typeof config !== 'object' || !RETIRED_KEYS.some((key) => key in config)) continue;
+ for (const key of RETIRED_KEYS) delete config[key];
+ await writeJsonAtomic(path, doc);
+ }
+}
+
+export async function up() {
+ let setAside = 0;
+ // Resolve the paths BEFORE dropping the keys that name them.
+ for (const { path } of await resolveCosTaskFiles()) {
+ if (await setAsideFile(path)) setAside++;
+ }
+ await dropRetiredKeys();
+ if (setAside > 0) console.log(`🗄️ CoS tasks: set aside ${setAside} Markdown task file(s) as *.imported`);
+}
diff --git a/server/scripts/db-migrations/019-retire-cos-task-files.test.js b/server/scripts/db-migrations/019-retire-cos-task-files.test.js
new file mode 100644
index 0000000000..0c611a811c
--- /dev/null
+++ b/server/scripts/db-migrations/019-retire-cos-task-files.test.js
@@ -0,0 +1,66 @@
+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const paths = vi.hoisted(() => ({ root: '' }));
+vi.mock('../../lib/paths.js', () => ({
+ PATHS: { get root() { return paths.root; }, get data() { return `${paths.root}/data`; }, get cos() { return `${paths.root}/data/cos`; } },
+}));
+
+const { up } = await import('./019-retire-cos-task-files.js');
+
+const write = (rel, text) => { mkdirSync(join(paths.root, rel, '..'), { recursive: true }); writeFileSync(join(paths.root, rel), text); };
+const read = (rel) => readFileSync(join(paths.root, rel), 'utf-8');
+const json = (rel) => JSON.parse(read(rel));
+
+beforeEach(() => {
+ rmSync(paths.root, { recursive: true, force: true });
+ paths.root = mkdtempSync(join(tmpdir(), 'cos-task-retire-'));
+});
+afterAll(() => rmSync(paths.root, { recursive: true, force: true }));
+
+describe('db-migration 019 — retire the CoS Markdown task files', () => {
+ it('sets each file aside as *.imported (kept, not deleted) and drops the retired config keys', async () => {
+ write('data/TASKS.md', '# Tasks\n');
+ write('data/COS-TASKS.md', '# Tasks\n');
+ write('data/cos/config.json', JSON.stringify({ userTasksFile: 'data/TASKS.md', cosTasksFile: 'data/COS-TASKS.md', maxConcurrentAgents: 3 }));
+ await up();
+ expect(existsSync(join(paths.root, 'data/TASKS.md'))).toBe(false);
+ expect(read('data/TASKS.md.imported')).toBe('# Tasks\n');
+ expect(read('data/COS-TASKS.md.imported')).toBe('# Tasks\n');
+ expect(json('data/cos/config.json')).toEqual({ maxConcurrentAgents: 3 });
+ });
+
+ it('finds a configured location before dropping the key that names it', async () => {
+ write('data/cos/config.json', JSON.stringify({ userTasksFile: 'custom/mine.md' }));
+ write('custom/mine.md', '# Tasks\n');
+ await up();
+ expect(existsSync(join(paths.root, 'custom/mine.md.imported'))).toBe(true);
+ expect(json('data/cos/config.json')).toEqual({});
+ });
+
+ it('also cleans a legacy state.json config without touching the rest of the state', async () => {
+ write('data/cos/state.json', JSON.stringify({ running: true, config: { userTasksFile: 'data/TASKS.md', goalsFile: 'GOALS.md' }, agents: { a: { id: 'a' } } }));
+ await up();
+ expect(json('data/cos/state.json')).toEqual({ running: true, config: { goalsFile: 'GOALS.md' }, agents: { a: { id: 'a' } } });
+ });
+
+ it('is a no-op on a fresh install and on a second run', async () => {
+ await up();
+ write('data/TASKS.md', '# Tasks\n');
+ await up();
+ await up();
+ expect(readdirSync(join(paths.root, 'data')).sort()).toEqual(['TASKS.md.imported']);
+ });
+
+ it('never overwrites an earlier recovery copy', async () => {
+ write('data/TASKS.md.imported', 'earlier copy');
+ write('data/TASKS.md', 'newer file');
+ await up();
+ expect(read('data/TASKS.md.imported')).toBe('earlier copy');
+ const set = readdirSync(join(paths.root, 'data')).filter((name) => name.startsWith('TASKS.md.imported-'));
+ expect(set).toHaveLength(1);
+ expect(read(`data/${set[0]}`)).toBe('newer file');
+ });
+});
diff --git a/server/scripts/db-migrations/_cosTaskFiles.js b/server/scripts/db-migrations/_cosTaskFiles.js
new file mode 100644
index 0000000000..9eef3e39ec
--- /dev/null
+++ b/server/scripts/db-migrations/_cosTaskFiles.js
@@ -0,0 +1,25 @@
+/**
+ * Where an install kept its CoS task queues before they moved into `cos_tasks`,
+ * resolved exactly as the retired Markdown reader did: the configured
+ * `userTasksFile` / `cosTasksFile` (relative to the checkout root) when the
+ * install's CoS config sets one, else `data/TASKS.md` / `data/COS-TASKS.md`.
+ * Shared by migrations 018 (import) and 019 (retire), which must agree.
+ */
+import { dirname, isAbsolute, join } from 'node:path';
+import { PATHS } from '../../lib/paths.js';
+import { readCosConfig } from '../../../scripts/migrations/_lib.js';
+
+const QUEUES = [
+ { source: 'user', configKey: 'userTasksFile', fallback: 'data/TASKS.md' },
+ { source: 'internal', configKey: 'cosTasksFile', fallback: 'data/COS-TASKS.md' },
+];
+
+/** `[{ source, path }]` for both queues, whether or not the files exist. */
+export async function resolveCosTaskFiles() {
+ // config.json lives under the install's data root, which is not always the checkout root.
+ const config = await readCosConfig({ rootDir: dirname(PATHS.data), label: 'CoS task import' });
+ return QUEUES.map(({ source, configKey, fallback }) => {
+ const configured = typeof config[configKey] === 'string' && config[configKey].trim() ? config[configKey].trim() : fallback;
+ return { source, path: isAbsolute(configured) ? configured : join(PATHS.root, configured) };
+ });
+}
diff --git a/server/scripts/init-db.sql b/server/scripts/init-db.sql
index 0708e0b4ba..9bea6536c0 100644
--- a/server/scripts/init-db.sql
+++ b/server/scripts/init-db.sql
@@ -342,18 +342,27 @@ CREATE TABLE IF NOT EXISTS cos_pending_agent_feedback (
archive_date TEXT
);
--- Machine-local archive of completed CoS tasks moved out of TASKS.md /
--- COS-TASKS.md. Full task records (prompt bodies included) live in `task`;
--- `description` is a light projection. Never enters federation payloads.
-CREATE TABLE IF NOT EXISTS cos_task_archive (
+-- The CoS task queues, one row per task. `task` is the task in its stored form;
+-- the other columns are the projections the queries need (open queue, history
+-- paging, queue order, recent-completed window). cos_task_lists.version bumps on
+-- every write so a cached read can detect change. Machine-local, never federated.
+CREATE TABLE IF NOT EXISTS cos_tasks (
source TEXT NOT NULL CHECK (source IN ('user', 'internal')),
task_id TEXT COLLATE "C" NOT NULL,
- description TEXT NOT NULL DEFAULT '',
+ status TEXT NOT NULL,
+ priority_value INTEGER NOT NULL DEFAULT 2,
+ position DOUBLE PRECISION NOT NULL,
completed_at TIMESTAMPTZ,
- archived_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ description TEXT NOT NULL DEFAULT '',
task JSONB NOT NULL,
PRIMARY KEY (source, task_id)
);
+CREATE INDEX IF NOT EXISTS idx_cos_tasks_open ON cos_tasks (source, status) WHERE status <> 'completed';
+CREATE INDEX IF NOT EXISTS idx_cos_tasks_completed ON cos_tasks (source, completed_at DESC NULLS LAST, task_id DESC) WHERE status = 'completed';
+CREATE TABLE IF NOT EXISTS cos_task_lists (
+ source TEXT PRIMARY KEY CHECK (source IN ('user', 'internal')),
+ version BIGINT NOT NULL DEFAULT 0
+);
-- Auto-update updated_at and sync_sequence on content/metadata changes.
-- Skips bump for access-stat-only updates (access_count, last_accessed)
diff --git a/server/services/_cosTaskRepositoryContract.js b/server/services/_cosTaskRepositoryContract.js
new file mode 100644
index 0000000000..c83c930520
--- /dev/null
+++ b/server/services/_cosTaskRepositoryContract.js
@@ -0,0 +1,129 @@
+/**
+ * The behavior every cos_tasks backend must have, run by the in-memory suite
+ * (cosTaskRepository.test.js) and the PostgreSQL suite (cosTaskRepository.db.test.js)
+ * so the two cannot drift. Not a test file itself: it only registers cases.
+ */
+import { beforeEach, describe, expect, it } from 'vitest';
+import { WORKING_SET_KEEP_COMPLETED, planListSave } from '../lib/cosTaskRows.js';
+
+const DAY = 86_400_000;
+const NOW = Date.now();
+const id = (n) => `task-example-${String(n).padStart(4, '0')}`;
+const task = (n, extra = {}) => ({
+ id: id(n), status: 'pending', priority: 'MEDIUM', priorityValue: 2, description: `Example task ${n}`,
+ approvalRequired: false, autoApproved: true, metadata: {}, ...extra,
+});
+const done = (n, ageDays, extra = {}) => task(n, { status: 'completed', metadata: { updatedAt: new Date(NOW - ageDays * DAY).toISOString(), prompt: `Prompt ${n}` }, ...extra });
+
+/** @param {{ name: string, makeBackend: () => Promise<object>|object }} options */
+export function __runCosTaskRepositoryContract({ name, makeBackend }) {
+ describe(`cos_tasks contract (${name})`, () => {
+ let backend;
+ // Saves a whole list the way the store does: plan against what is loaded, then apply.
+ const save = async (source, next) => {
+ const { rows } = await backend.loadWorkingSet(source);
+ const plan = planListSave({ previous: rows, next, source });
+ await backend.apply(source, { upserts: plan.upserts, deletes: plan.deletes });
+ return plan;
+ };
+ const ids = async (source) => (await backend.loadWorkingSet(source)).rows.map((row) => row.task.id);
+
+ beforeEach(async () => { backend = await makeBackend(); });
+
+ it('starts empty at version 0', async () => {
+ expect(await backend.loadWorkingSet('user')).toEqual({ version: 0, rows: [] });
+ expect(await backend.readVersion('internal')).toBe(0);
+ });
+
+ it('stores a list in queue order and bumps the version per write, independently per queue', async () => {
+ await save('user', [task(1, { priorityValue: 1, priority: 'LOW' }), task(2, { priorityValue: 3, priority: 'HIGH' }), task(3, { status: 'blocked' }), task(4)]);
+ expect(await ids('user')).toEqual([id(2), id(4), id(1), id(3)]);
+ expect(await backend.readVersion('user')).toBe(1);
+ expect(await backend.readVersion('internal')).toBe(0);
+ await save('user', [task(1)]);
+ expect(await backend.readVersion('user')).toBe(2);
+ });
+
+ it('breaks priority ties by saved position and keeps a reorder', async () => {
+ await save('user', [task(1), task(2), task(3)]);
+ expect(await ids('user')).toEqual([id(1), id(2), id(3)]);
+ const { rows } = await backend.loadWorkingSet('user');
+ await save('user', [rows[2].task, rows[0].task, rows[1].task]);
+ expect(await ids('user')).toEqual([id(3), id(1), id(2)]);
+ });
+
+ it('upserts a changed task in place and deletes ids that left the list', async () => {
+ await save('user', [task(1), task(2)]);
+ const plan = await save('user', [task(1, { description: 'Edited' })]);
+ expect(plan.upserts).toHaveLength(1);
+ expect(plan.deletes).toEqual([id(2)]);
+ const { rows } = await backend.loadWorkingSet('user');
+ expect(rows.map((row) => row.task.description)).toEqual(['Edited']);
+ });
+
+ it('returns copies, never references into the backend', async () => {
+ await save('user', [task(1, { metadata: { note: 'kept' } })]);
+ const first = await backend.loadWorkingSet('user');
+ first.rows[0].task.metadata.note = 'mutated';
+ expect((await backend.loadWorkingSet('user')).rows[0].task.metadata.note).toBe('kept');
+ (await backend.getTask(id(1), 'user')).task.metadata.note = 'mutated';
+ expect((await backend.getTask(id(1), 'user')).task.metadata.note).toBe('kept');
+ });
+
+ it('keeps old completed tasks out of the working set but reachable by id, page and count', async () => {
+ const recent = done(1000, 1);
+ const old = Array.from({ length: WORKING_SET_KEEP_COMPLETED + 2 }, (_, n) => done(n + 1, 20 + n / 100));
+ await save('user', [task(2000), recent, ...old]);
+ // Every row is stored; the working set returns only the recent and the newest completed ones.
+ const listed = await ids('user');
+ expect(listed).toContain(id(1000));
+ const oldest = id(WORKING_SET_KEEP_COMPLETED + 2);
+ expect(listed).not.toContain(oldest);
+ expect((await backend.getTask(oldest, 'user')).task.status).toBe('completed');
+ expect(await backend.countCompleted('user')).toBe(old.length + 1);
+ });
+
+ it('pages completed history newest id first with a strict cursor and an offset', async () => {
+ await save('user', [done(1, 1), done(2, 1), done(3, 1), done(4, 1), task(5)]);
+ const page = async (opts) => (await backend.pageCompleted('user', { before: null, offset: 0, limit: 10, ...opts })).map((t) => t.id);
+ expect(await page({})).toEqual([id(4), id(3), id(2), id(1)]);
+ expect(await page({ before: id(3), limit: 1 })).toEqual([id(2)]);
+ expect(await page({ offset: 1, limit: 2 })).toEqual([id(3), id(2)]);
+ });
+
+ it('reports which ids are stored as completed, only those', async () => {
+ await save('user', [done(1, 1), task(2)]);
+ expect(await backend.completedIds('user', [id(1), id(2), id(3)])).toEqual(new Set([id(1)]));
+ expect(await backend.completedIds('user', [])).toEqual(new Set());
+ expect(await backend.completedIds('internal', [id(1)])).toEqual(new Set());
+ });
+
+ it('looks an id up user-first, or in the named queue', async () => {
+ await save('internal', [task(1, { description: 'Internal copy' })]);
+ await save('user', [task(1, { description: 'User copy' })]);
+ expect((await backend.getTask(id(1), null)).task.description).toBe('User copy');
+ expect((await backend.getTask(id(1), 'internal')).source).toBe('internal');
+ expect(await backend.getTask(id(9), null)).toBeNull();
+ });
+
+ it('lists completed summaries without prompt bodies', async () => {
+ await save('user', [done(1, 1), task(2)]);
+ await save('internal', [done(3, 1)]);
+ const summaries = await backend.completedSummaries();
+ expect(summaries).toHaveLength(2);
+ expect(summaries).toContainEqual({ source: 'user', id: id(1), description: 'Example task 1' });
+ expect(JSON.stringify(summaries)).not.toContain('Prompt');
+ });
+
+ it('deletes by id from the named or either queue and bumps only the affected version', async () => {
+ await save('user', [task(1)]);
+ await save('internal', [task(1)]);
+ const before = await backend.readVersion('internal');
+ expect(await backend.deleteById(id(1), 'user')).toBe(1);
+ expect(await backend.readVersion('internal')).toBe(before);
+ expect(await backend.deleteById(id(1), 'user')).toBe(0);
+ expect(await backend.deleteById(id(1), null)).toBe(1);
+ expect(await ids('internal')).toEqual([]);
+ });
+ });
+}
diff --git a/server/services/agentImportCycles.test.js b/server/services/agentImportCycles.test.js
index 5b92305ac5..0c9a81346a 100644
--- a/server/services/agentImportCycles.test.js
+++ b/server/services/agentImportCycles.test.js
@@ -443,7 +443,7 @@ describe('agent lifecycle cluster — no static import cycles (#2837)', () => {
it('keeps cos.js out of every static import cycle (#5684)', () => {
// cos.js re-exports cosState/cosTaskStore for backward compat with
// `import * as cos`. Two leaf services used to reach THROUGH that barrel for
- // one symbol each (memoryEmbeddings -> getConfig, character -> getAllTasks),
+ // one symbol each (memoryEmbeddings -> getConfig, character -> the task data),
// and because cos.js transitively reaches the CoS tool registry -> voice
// tools -> askService -> both of those leaves, each one-symbol forward closed
// a 7-module ring. Both now import the DECLARING module, which is the same
@@ -455,8 +455,8 @@ describe('agent lifecycle cluster — no static import cycles (#2837)', () => {
// the barrel fails with the reason rather than as an opaque ring.
expect(graph.get('memoryEmbeddings.js'), 'memoryEmbeddings must import getConfig from cosState.js').not.toContain('cos.js');
expect(graph.get('memoryEmbeddings.js')).toContain('cosState.js');
- expect(graph.get('character.js'), 'character must import getAllTasks from cosTaskStore.js').not.toContain('cos.js');
- expect(graph.get('character.js')).toContain('cosTaskStore.js');
+ expect(graph.get('character.js'), 'character must import the completed-task summaries from cosTaskRepository.js').not.toContain('cos.js');
+ expect(graph.get('character.js')).toContain('cosTaskRepository.js');
});
it('no longer needs the dynamic-import workaround for handleOrphanedTask', () => {
diff --git a/server/services/autonomousJobs.js b/server/services/autonomousJobs.js
index f0b5041080..a6188f8040 100644
--- a/server/services/autonomousJobs.js
+++ b/server/services/autonomousJobs.js
@@ -5,7 +5,7 @@
* on behalf of the user, using their digital twin identity to make decisions.
*
* Jobs are different from tasks:
- * - Tasks are one-shot work items (TASKS.md)
+ * - Tasks are one-shot work items (the CoS task queues)
* - Jobs are recurring schedules that generate tasks when due
*
* Job types:
diff --git a/server/services/character.js b/server/services/character.js
index 5aa2a5b585..a9d5cd4eb3 100644
--- a/server/services/character.js
+++ b/server/services/character.js
@@ -17,8 +17,7 @@ import crypto from 'crypto';
import path from 'path';
import { atomicWrite, ensureDir, readJSONFile, PATHS, sleep } from '../lib/fileUtils.js';
import * as jiraService from './jira.js';
-import { getAllTasks } from './cosTaskStore.js';
-import { listArchivedSummaries } from './cosTaskArchive.js';
+import { listCompletedTaskSummaries } from './cosTaskRepository.js';
import { getBirthDateStrict } from './meatspace.js';
import { getCharacterSkills } from './characterSkills.js';
import { getCharacterMetrics } from './characterMetrics.js';
@@ -587,14 +586,9 @@ export async function syncJiraXP() {
}
export async function syncTaskXP() {
- const { user: userTasks, cos: cosTasks } = await getAllTasks();
- // Completed tasks past the file window live in the archive; one that finished
- // before this sync ran must still earn its XP, so include them by summary.
- const archived = await listArchivedSummaries();
- const completed = [
- ...[...(userTasks.tasks || []), ...(cosTasks.tasks || [])].filter(t => t.status === 'completed'),
- ...archived,
- ];
+ // Every completed task, not only the recent working set: one that finished
+ // before this sync ran must still earn its XP. Summaries carry no prompt bodies.
+ const completed = await listCompletedTaskSummaries();
const { saved, result } = await mutateCharacter((character) => {
character.syncedTaskIds ??= [];
diff --git a/server/services/character.test.js b/server/services/character.test.js
index ef69e61c82..001ea85ba7 100644
--- a/server/services/character.test.js
+++ b/server/services/character.test.js
@@ -9,7 +9,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
const store = vi.hoisted(() => ({
value: null, birthDate: null, readable: true, beforeWrite: null,
jira: { instances: {}, projects: [], tickets: [], beforeTickets: null },
- tasks: { user: { tasks: [] }, cos: { tasks: [] }, before: null, archived: [] },
+ tasks: { completed: [], before: null },
}));
vi.mock('../lib/fileUtils.js', () => ({
@@ -48,17 +48,14 @@ vi.mock('./jira.js', () => ({
return store.jira.tickets;
}),
}));
-// character.js reaches cosTaskStore for the one symbol it uses (getAllTasks); syncTaskXP
-// is not exercised here, so the stub only has to satisfy the static import.
-vi.mock('./cosTaskStore.js', () => ({
- getAllTasks: async () => {
+// character.js reads completed-task summaries from the task repository; syncTaskXP
+// the stub feeds the fixture each test sets on `store.tasks.completed`.
+vi.mock('./cosTaskRepository.js', () => ({
+ listCompletedTaskSummaries: async () => {
await store.tasks.before?.();
- return { user: store.tasks.user, cos: store.tasks.cos };
+ return store.tasks.completed;
},
}));
-vi.mock('./cosTaskArchive.js', () => ({
- listArchivedSummaries: async () => store.tasks.archived,
-}));
vi.mock('./characterSkills.js', () => ({
getCharacterSkills: vi.fn(async () => FAKE_SKILLS),
}));
@@ -573,7 +570,7 @@ describe('Character mutation queue (#9817)', () => {
store.birthDate = null;
store.beforeWrite = null;
store.jira = { instances: {}, projects: [], tickets: [], beforeTickets: null };
- store.tasks = { user: { tasks: [] }, cos: { tasks: [] }, before: null, archived: [] };
+ store.tasks = { completed: [], before: null };
});
afterEach(() => { store.beforeWrite = null; });
@@ -700,7 +697,7 @@ describe('Character mutation queue (#9817)', () => {
describe('imports re-check deduplication after slow discovery', () => {
it('syncTaskXP does not double-grant a task another writer synced during discovery', async () => {
- store.tasks.user = { tasks: [{ id: 't1', status: 'completed', title: 'One' }, { id: 't2', status: 'completed', title: 'Two' }] };
+ store.tasks.completed = [{ source: 'user', id: 't1', description: 'One' }, { source: 'user', id: 't2', description: 'Two' }];
store.tasks.before = async () => {
// A concurrent writer (e.g. a federation merge) records t1 while discovery is in flight.
await characterService.applyRemoteSnapshot({
@@ -715,21 +712,20 @@ describe('Character mutation queue (#9817)', () => {
expect(store.value.xp).toBe(125);
});
- it('syncTaskXP also grants XP for completed tasks that have already left the task files', async () => {
- store.tasks.user = { tasks: [{ id: 't1', status: 'completed', title: 'In the file window' }] };
- store.tasks.archived = [
- { source: 'user', id: 't0', description: 'Archived before any sync' },
- { source: 'internal', id: 't1', description: 'Same id as a file task' },
+ it('syncTaskXP grants XP once per completed task, including old ones, and skips ones already synced', async () => {
+ store.tasks.completed = [
+ { source: 'user', id: 't0', description: 'Old and already synced' },
+ { source: 'user', id: 't1', description: 'Recent' },
+ { source: 'internal', id: 't1', description: 'Same id in the other queue' },
+ { source: 'user', id: 't9', description: 'Old and unsynced' },
];
store.value.syncedTaskIds = ['t0'];
const result = await characterService.syncTaskXP();
- // t0 was already synced; t1 appears in both places but is granted once.
- expect(result.taskCount).toBe(1);
- expect(store.value.syncedTaskIds.sort()).toEqual(['t0', 't1']);
- store.tasks.archived = [{ source: 'user', id: 't9', description: 'Archived and unsynced' }];
- expect((await characterService.syncTaskXP()).taskCount).toBe(1);
- expect(store.value.syncedTaskIds).toContain('t9');
+ // t0 was already synced; t1 appears in both queues but is granted once.
+ expect(result.taskCount).toBe(2);
+ expect(store.value.syncedTaskIds.sort()).toEqual(['t0', 't1', 't9']);
+ expect((await characterService.syncTaskXP()).taskCount).toBe(0);
});
it('syncJiraXP does not double-grant a ticket synced during discovery, and keeps a racing edit', async () => {
diff --git a/server/services/cos.dequeueGuard.test.js b/server/services/cos.dequeueGuard.test.js
index d2d352071a..64c718a34f 100644
--- a/server/services/cos.dequeueGuard.test.js
+++ b/server/services/cos.dequeueGuard.test.js
@@ -51,8 +51,7 @@ describe('scheduleDequeue — rejections stay inside the cycle (#5644)', () => {
// are already attached by then, which is what this test drives.
await init().catch(() => {});
- // Two independent event sources, so a guard added to only one of them fails.
- cosEvents.emit('tasks:cos:added');
+ // Independent event sources, so a guard added to only one of them fails.
cosEvents.emit('tasks:changed', { action: 'approved' });
// A completed task returned to pending must wake this real listener (#12058).
cosEvents.emit('tasks:changed', {
@@ -68,7 +67,7 @@ describe('scheduleDequeue — rejections stay inside the cycle (#5644)', () => {
const logged = errorSpy.mock.calls
.map(args => args.join(' '))
.filter(line => line.includes('CoS dequeue cycle failed'));
- expect(logged).toHaveLength(3);
+ expect(logged).toHaveLength(2);
expect(logged[0]).toContain(STATE_ERROR);
});
});
diff --git a/server/services/cos.js b/server/services/cos.js
index a3957a38c7..80acd904c5 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -4,7 +4,7 @@ import { mergePersistentMindMaintainer } from '../lib/persistentMindMaintainer.j
/**
* Chief of Staff (CoS) Service
*
- * Manages the autonomous agent manager that watches TASKS.md,
+ * Manages the autonomous agent manager that works the task queues,
* spawns sub-agents, and orchestrates task completion.
*
* Decomposed modules:
@@ -88,12 +88,12 @@ export { generateReport, getReport, getTodayReport, listReports, listBriefings,
import { runHealthCheck, getHealthStatus } from './cosHealthMonitor.js';
export { runHealthCheck, getHealthStatus };
-// Task store: CRUD + queue persistence (TASKS.md / COS-TASKS.md). Imported for
+// Task store: CRUD + queue persistence (the `cos_tasks` queues). Imported for
// internal use by evaluateTasks/dequeueNextTask/generators and re-exported for
// backward compat with `import * as cos` and the cos route handlers. The store
// emits `tasks:changed`; init() below turns that into tryImmediateSpawn /
// dequeueNextTask so the spawn-side logic stays here, not in the store.
-import { firstLine, getUserTasks, getCosTasks, getAllTasks, getTaskView, getTasks, getTaskById, findTaskWithSource, addTask, updateTask, reviveBlockedTask, deleteTask, reorderTasks, approveTask, challengeTask, resolveTaskChallenge, resolveTaskChallengeWithRecheck, sweepResolvedFailureTasks, archiveCompletedTasks } from './cosTaskStore.js';
+import { firstLine, getUserTasks, getCosTasks, getAllTasks, getTaskView, getTasks, getTaskById, findTaskWithSource, addTask, updateTask, reviveBlockedTask, deleteTask, reorderTasks, approveTask, challengeTask, resolveTaskChallenge, resolveTaskChallengeWithRecheck, sweepResolvedFailureTasks } from './cosTaskStore.js';
export { getPendingTaskIds } from './cosTaskStore.js';
export { firstLine, getUserTasks, getCosTasks, getAllTasks, getTaskView, getTasks, getTaskById, findTaskWithSource, addTask, updateTask, reviveBlockedTask, deleteTask, reorderTasks, approveTask, challengeTask, resolveTaskChallenge, resolveTaskChallengeWithRecheck, sweepResolvedFailureTasks };
import { ensureInstanceId } from './instanceIdentity.js';
@@ -480,13 +480,8 @@ async function bootDaemon(state) {
if (swept.reaped > 0) {
emitLog('info', `🧹 Auto-expired ${swept.reaped} resolved failure task(s) (${swept.staleBlocks} block(s), ${swept.investigations} investigation(s))`);
}
- // Completed tasks past the recent window leave the task files for the
- // archive table; the files stay small enough for every poller to read.
- await archiveCompletedTasks().catch((err) => {
- console.warn(`⚠️ archiveCompletedTasks failed: ${err?.message || err}`);
- });
},
- metadata: { description: 'CoS health check + orphan cleanup + agent archival + failure-artifact reaper + completed-task archive' }
+ metadata: { description: 'CoS health check + orphan cleanup + agent archival + failure-artifact reaper' }
});
// Performance summary (10 min)
@@ -979,13 +974,8 @@ export async function resetOrphanedTasks({ bootRecovery = false } = {}) {
}
};
- if (userTaskData.exists) {
- await processOrphanedTasks(userTaskData.grouped.in_progress || []);
- }
-
- if (cosTaskData.exists) {
- await processOrphanedTasks(cosTaskData.grouped.in_progress || []);
- }
+ await processOrphanedTasks(userTaskData.grouped.in_progress || []);
+ await processOrphanedTasks(cosTaskData.grouped.in_progress || []);
}
/**
@@ -1240,7 +1230,7 @@ const scheduleDequeue = (options = {}) => setImmediate(() => {
/**
* Event-driven task dequeue — the primary way tasks get spawned.
*
- * Triggered by: agent:completed, tasks:user:added, tasks:cos:added, status:resumed
+ * Triggered by: agent:completed, tasks:changed, status:resumed
* Thin orchestrator: computes per-cycle capacity, then threads a shared `ctx`
* through the priority tiers in order (same tiers/numbering as evaluateTasks;
* evaluateTasks' queue-regeneration step is
@@ -1694,10 +1684,6 @@ export async function init() {
.catch(err => console.error(`❌ Critical-health recovery check failed: ${err.message}`));
});
- cosEvents.on('tasks:user:added', () => {
- if (isDaemonRunning()) scheduleDequeue();
- });
-
// Changing the investigation override can make an already-queued held task
// eligible. Re-run the normal dequeue so autonomy, budgets, leases, and
// capacity remain the same as for every other system task.
@@ -1711,10 +1697,6 @@ export async function init() {
}
});
- cosEvents.on('tasks:cos:added', () => {
- if (isDaemonRunning()) scheduleDequeue();
- });
-
cosEvents.on('task:on-demand-requested', () => {
if (isDaemonRunning()) scheduleDequeue();
});
diff --git a/server/services/cos.startStop.test.js b/server/services/cos.startStop.test.js
index b143c72736..818c86ac62 100644
--- a/server/services/cos.startStop.test.js
+++ b/server/services/cos.startStop.test.js
@@ -53,7 +53,7 @@ vi.mock('./creativeDirector/recovery.js', () => ({
vi.mock('./cosTaskStore.js', async (importOriginal) => ({
...(await importOriginal()),
- getAllTasks: vi.fn(async () => ({ user: { tasks: [] }, cos: { tasks: [] } })),
+ getAllTasks: vi.fn(async () => ({ user: { tasks: [], grouped: {} }, cos: { tasks: [], grouped: {} } })),
getCosTasks: vi.fn(async () => ({ tasks: [] })),
}));
vi.mock('./instanceIdentity.js', async (importOriginal) => ({
diff --git a/server/services/cos.test.js b/server/services/cos.test.js
index 5b80fead89..40da17534f 100644
--- a/server/services/cos.test.js
+++ b/server/services/cos.test.js
@@ -2024,8 +2024,8 @@ describe('resetOrphanedTasks — latest run outcome (#12057)', () => {
vi.spyOn(state, 'loadState').mockResolvedValue({ agents: { latest, older } });
vi.spyOn(identity, 'ensureInstanceId').mockResolvedValue('synthetic-instance');
vi.spyOn(taskStore, 'getAllTasks').mockResolvedValue({
- user: { exists: true, grouped: { in_progress: [{ id: taskId, status: 'in_progress', metadata: {} }] } },
- cos: { exists: false },
+ user: { grouped: { in_progress: [{ id: taskId, status: 'in_progress', metadata: {} }] } },
+ cos: { grouped: {} },
});
vi.spyOn(taskStore, 'updateTask').mockResolvedValue({});
vi.spyOn(agentManagement, 'handleOrphanedTask').mockResolvedValue({});
diff --git a/server/services/cosChallengeOwnership.test.js b/server/services/cosChallengeOwnership.test.js
index 0c9238223c..9e26e8d571 100644
--- a/server/services/cosChallengeOwnership.test.js
+++ b/server/services/cosChallengeOwnership.test.js
@@ -59,12 +59,14 @@ vi.mock('./cosLocalEndpointSlots.js', () => ({
}));
import { addTask, updateTask, challengeTask, resolveTaskChallenge, getTaskById, __resetTaskCache } from './cosTaskStore.js';
+import { __resetCosTaskRepository } from './cosTaskRepository.js';
import { activeAgents, runnerAgents } from './agentState.js';
import { withStateLock } from './cosState.js';
import { prepareAgentSpawn } from './agentSpawnPreparation.js';
const { dequeueNextTask } = await vi.importActual('./cos.js');
beforeEach(() => {
+ __resetCosTaskRepository();
__resetTaskCache();
activeAgents.clear();
runnerAgents.clear();
diff --git a/server/services/cosState.js b/server/services/cosState.js
index 203bfbeacd..176894bb8e 100644
--- a/server/services/cosState.js
+++ b/server/services/cosState.js
@@ -90,8 +90,6 @@ export function withStateLock(fn) {
export const withConfigLock = createFileWriteQueue();
export const DEFAULT_CONFIG = {
- userTasksFile: 'data/TASKS.md',
- cosTasksFile: 'data/COS-TASKS.md',
goalsFile: 'GOALS.md',
healthCheckIntervalMs: 900000,
maxConcurrentAgents: 3,
diff --git a/server/services/cosTaskArchive.db.test.js b/server/services/cosTaskArchive.db.test.js
deleted file mode 100644
index c6ed417b76..0000000000
--- a/server/services/cosTaskArchive.db.test.js
+++ /dev/null
@@ -1,62 +0,0 @@
-/**
- * Postgres round-trip for the completed-task archive. The file backend's
- * contract is pinned without a database in cosTaskArchive.test.js; what needs a
- * real Postgres is the part only Postgres enforces: the (source, task_id) key,
- * the source CHECK, the JSONB round-trip, and `COLLATE "C"` ordering matching
- * JavaScript's code-unit comparison.
- *
- * Runs via `npm run test:db` (→ `portos_test`) ONLY. Skips cleanly when no
- * database is reachable.
- */
-import { afterAll, beforeEach, describe, expect, it } from 'vitest';
-import { checkHealth, close, ensureSchema, query } from '../lib/db.js';
-import { requireDbOrSkip } from '../lib/dbTestGate.js';
-import { __makePgCosTaskArchiveBackend } from './cosTaskArchive.js';
-
-const health = await checkHealth().catch(() => ({ connected: false }));
-if (health.connected) await ensureSchema();
-const runDb = requireDbOrSkip('services/cosTaskArchive.db.test', health.connected, 'disposable PostgreSQL unavailable');
-const backend = __makePgCosTaskArchiveBackend({ query });
-const task = (id, extra = {}) => ({ id, description: `Example ${id}`, status: 'completed', metadata: { updatedAt: '2026-10-01T00:00:00.000Z', prompt: `Prompt ${id}` }, ...extra });
-
-beforeEach(async () => { if (runDb) await query('DELETE FROM cos_task_archive'); });
-afterAll(async () => { if (runDb) await query('DELETE FROM cos_task_archive'); if (health.connected) await close(); });
-
-describe.skipIf(!runDb)('cos_task_archive (postgres backend)', () => {
- it('round-trips a full record, upserts by (source, id) and keeps sources apart', async () => {
- await backend.archive('user', [task('task-a')]);
- await backend.archive('internal', [task('task-a', { description: 'Internal twin' })]);
- await backend.archive('user', [task('task-a', { description: 'Edited' })]);
- expect(await backend.get('task-a', 'user')).toEqual({ source: 'user', task: expect.objectContaining({ description: 'Edited', metadata: expect.objectContaining({ prompt: 'Prompt task-a' }) }) });
- expect((await backend.get('task-a', 'internal')).task.description).toBe('Internal twin');
- expect(await backend.count('user')).toBe(1);
- const { rows } = await query("SELECT completed_at FROM cos_task_archive WHERE source = 'user' AND task_id = 'task-a'");
- expect(rows[0].completed_at.toISOString()).toBe('2026-10-01T00:00:00.000Z');
- });
-
- it('pages newest id first in code-unit order with a strict before cursor', async () => {
- await backend.archive('user', ['task-b', 'task-a', 'task-C', 'task-d'].map((id) => task(id)));
- expect((await backend.list('user', { offset: 0, limit: 10 })).map((t) => t.id)).toEqual(['task-d', 'task-b', 'task-a', 'task-C']);
- expect((await backend.list('user', { before: 'task-b', offset: 0, limit: 2 })).map((t) => t.id)).toEqual(['task-a', 'task-C']);
- expect((await backend.list('user', { offset: 1, limit: 2 })).map((t) => t.id)).toEqual(['task-b', 'task-a']);
- });
-
- it('reports archived ids, projects summaries without bodies, and deletes by source', async () => {
- await backend.archive('user', [task('task-a'), task('task-b')]);
- expect(await backend.existingIds('user', ['task-a', 'task-z'])).toEqual(new Set(['task-a']));
- expect(await backend.existingIds('user', [])).toEqual(new Set());
- expect(await backend.summaries()).toContainEqual({ source: 'user', id: 'task-a', description: 'Example task-a' });
- expect(await backend.remove('task-a', 'internal')).toBe(false);
- expect(await backend.remove('task-a', 'user')).toBe(true);
- });
-
- it('looks an id up user-first when both queues archived the same id', async () => {
- await backend.archive('internal', [task('task-shared', { description: 'Internal copy' })]);
- await backend.archive('user', [task('task-shared', { description: 'User copy' })]);
- expect(await backend.get('task-shared', null)).toMatchObject({ source: 'user', task: { description: 'User copy' } });
- });
-
- it('refuses an unknown source', async () => {
- await expect(backend.archive('elsewhere', [task('task-a')])).rejects.toThrow();
- });
-});
diff --git a/server/services/cosTaskArchive.js b/server/services/cosTaskArchive.js
deleted file mode 100644
index a923e980d6..0000000000
--- a/server/services/cosTaskArchive.js
+++ /dev/null
@@ -1,180 +0,0 @@
-/**
- * Machine-local archive of completed CoS tasks.
- *
- * `TASKS.md` / `COS-TASKS.md` keep the open queue plus a recent window of
- * completed tasks; older completed tasks move here with their full records
- * (prompt bodies were ~99% of those files). PostgreSQL is authoritative in a
- * normal install; the JSON backend exists only for the documented
- * development/test escape hatch. Never federated: peers advertise and merge
- * only what their own task files hold.
- *
- * Task ids sort with JavaScript's code-unit order everywhere (`byIdDesc` in
- * cosTaskStore), so the table's key column is `COLLATE "C"` to match.
- */
-
-import { join } from 'node:path';
-import { atomicWrite, PATHS, readJSONFile } from '../lib/fileUtils.js';
-import { createFileWriteQueue } from '../lib/fileWriteQueue.js';
-import { createPgFileFacade, resolvePgBackend } from '../lib/pgFileFacade.js';
-
-const SOURCES = ['user', 'internal'];
-const archiveFile = () => join(PATHS.data, 'cos-task-archive.json');
-const queueFileWrite = createFileWriteQueue();
-
-const completedAtOf = (task) => {
- const stamp = Date.parse(task?.metadata?.completedAt || task?.metadata?.updatedAt || '');
- return Number.isFinite(stamp) ? new Date(stamp).toISOString() : null;
-};
-const descriptionOf = (task) => (typeof task?.description === 'string' ? task.description : '');
-const byIdDesc = (a, b) => (a.id < b.id ? 1 : a.id > b.id ? -1 : 0);
-
-async function readFileArchive() {
- const raw = await readJSONFile(archiveFile(), {}, { logError: false, strict: true });
- return Object.fromEntries(SOURCES.map((source) => [source, raw?.[source] && typeof raw[source] === 'object' ? raw[source] : {}]));
-}
-
-function makeFileBackend() {
- return {
- name: 'file',
- archive: (source, tasks) => queueFileWrite(async () => {
- const archive = await readFileArchive();
- for (const task of tasks) archive[source][task.id] = task;
- await atomicWrite(archiveFile(), archive);
- }),
- async get(taskId, source) {
- const archive = await readFileArchive();
- for (const candidate of source ? [source] : SOURCES) {
- if (archive[candidate][taskId]) return { source: candidate, task: archive[candidate][taskId] };
- }
- return null;
- },
- async existingIds(source, ids) {
- const archive = await readFileArchive();
- return new Set(ids.filter((id) => archive[source][id]));
- },
- async list(source, { before, offset, limit }) {
- const archive = await readFileArchive();
- return Object.values(archive[source])
- .filter((task) => before == null || task.id < before)
- .sort(byIdDesc)
- .slice(offset, offset + limit);
- },
- async count(source) {
- return Object.keys((await readFileArchive())[source]).length;
- },
- async summaries() {
- const archive = await readFileArchive();
- return SOURCES.flatMap((source) => Object.values(archive[source]).map((task) => ({ source, id: task.id, description: descriptionOf(task) })));
- },
- remove: (taskId, source) => queueFileWrite(async () => {
- const archive = await readFileArchive();
- let removed = false;
- for (const candidate of source ? [source] : SOURCES) {
- if (archive[candidate][taskId]) { delete archive[candidate][taskId]; removed = true; }
- }
- if (removed) await atomicWrite(archiveFile(), archive);
- return removed;
- }),
- };
-}
-
-/** The PostgreSQL backend over a `{ query }` handle. Exported so the DB suite can drive it directly. */
-export function __makePgCosTaskArchiveBackend(db) {
- return {
- name: 'postgres',
- async archive(source, tasks) {
- for (const task of tasks) {
- await db.query(
- `INSERT INTO cos_task_archive (source, task_id, description, completed_at, task)
- VALUES ($1, $2, $3, $4, $5::jsonb)
- ON CONFLICT (source, task_id) DO UPDATE
- SET description = EXCLUDED.description, completed_at = EXCLUDED.completed_at, task = EXCLUDED.task`,
- [source, task.id, descriptionOf(task), completedAtOf(task), JSON.stringify(task)],
- );
- }
- },
- async get(taskId, source) {
- const { rows } = await db.query(
- // User queue first, matching findTaskWithSource's file precedence on an id collision.
- `SELECT source, task FROM cos_task_archive WHERE task_id = $1 AND ($2::text IS NULL OR source = $2)
- ORDER BY (source = 'user') DESC LIMIT 1`,
- [taskId, source],
- );
- return rows[0] ? { source: rows[0].source, task: rows[0].task } : null;
- },
- async existingIds(source, ids) {
- if (ids.length === 0) return new Set();
- const { rows } = await db.query('SELECT task_id FROM cos_task_archive WHERE source = $1 AND task_id = ANY($2::text[])', [source, ids]);
- return new Set(rows.map((row) => row.task_id));
- },
- async list(source, { before, offset, limit }) {
- const { rows } = await db.query(
- `SELECT task FROM cos_task_archive WHERE source = $1 AND ($2::text IS NULL OR task_id < $2)
- ORDER BY task_id DESC LIMIT $3 OFFSET $4`,
- [source, before ?? null, limit, offset],
- );
- return rows.map((row) => row.task);
- },
- async count(source) {
- const { rows } = await db.query('SELECT count(*)::int AS n FROM cos_task_archive WHERE source = $1', [source]);
- return rows[0].n;
- },
- async summaries() {
- const { rows } = await db.query('SELECT source, task_id, description FROM cos_task_archive');
- return rows.map((row) => ({ source: row.source, id: row.task_id, description: row.description }));
- },
- async remove(taskId, source) {
- const { rowCount } = await db.query('DELETE FROM cos_task_archive WHERE task_id = $1 AND ($2::text IS NULL OR source = $2)', [taskId, source]);
- return rowCount > 0;
- },
- };
-}
-
-const backendFacade = createPgFileFacade({
- makeFile: makeFileBackend,
- makePg: () => resolvePgBackend({
- requirement: 'The CoS task archive requires PostgreSQL — run `npm run setup:db` (dev/test only: set MEMORY_BACKEND=file for the file escape hatch)',
- loadDb: () => import('../lib/db.js'),
- makePg: __makePgCosTaskArchiveBackend,
- }),
-});
-const backend = async () => backendFacade.getBackend();
-
-/** Upsert full task records for one source. Idempotent: a retried sweep rewrites the same rows. */
-export async function archiveTasks(source, tasks) {
- if (!SOURCES.includes(source) || tasks.length === 0) return;
- await (await backend()).archive(source, tasks);
-}
-
-/** One archived task as `{ source, task }`, or null. Omit `source` to search both. */
-export async function getArchivedTask(taskId, source = null) {
- return (await backend()).get(taskId, source);
-}
-
-/** The subset of `ids` already archived for `source`. */
-export async function getArchivedIds(source, ids) {
- return (await backend()).existingIds(source, ids);
-}
-
-/** Up to `limit` archived tasks newest id first, strictly before `before`, skipping `offset`. */
-export async function listArchivedTasks(source, { before = null, offset = 0, limit = 25 } = {}) {
- return (await backend()).list(source, { before, offset, limit });
-}
-
-export async function countArchivedTasks(source) {
- return (await backend()).count(source);
-}
-
-/** `{ source, id, description }` for every archived task, without the prompt bodies. */
-export async function listArchivedSummaries() {
- return (await backend()).summaries();
-}
-
-export async function deleteArchivedTask(taskId, source = null) {
- return (await backend()).remove(taskId, source);
-}
-
-/** Test seam for suites that swap the file data root between cases. */
-export function __resetCosTaskArchive() {
- backendFacade.reset();
-}
diff --git a/server/services/cosTaskArchive.test.js b/server/services/cosTaskArchive.test.js
deleted file mode 100644
index ab4ebba0ef..0000000000
--- a/server/services/cosTaskArchive.test.js
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
- * File backend of the completed-task archive (the dev/test escape hatch). The
- * PostgreSQL backend is covered by cosTaskArchive.db.test.js against the same
- * contract: newest-id-first paging, a strict `before` cursor, upsert by
- * (source, id), and light summaries without prompt bodies.
- */
-import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
-import { rmSync } from 'node:fs';
-import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';
-
-const { makeProxy, cleanup, tempRoot } = mockPathsDataRoot({ prefix: 'cos-task-archive-test-' });
-vi.mock('../lib/fileUtils.js', async () => makeProxy(await vi.importActual('../lib/fileUtils.js')));
-
-const {
- archiveTasks, countArchivedTasks, deleteArchivedTask, getArchivedIds, getArchivedTask,
- listArchivedSummaries, listArchivedTasks, __resetCosTaskArchive,
-} = await import('./cosTaskArchive.js');
-
-const task = (id, extra = {}) => ({ id, description: `Example ${id}`, status: 'completed', priority: 'MEDIUM', metadata: { prompt: `Prompt for ${id}` }, ...extra });
-
-beforeEach(() => {
- rmSync(`${tempRoot}/cos-task-archive.json`, { force: true });
- __resetCosTaskArchive();
-});
-afterAll(cleanup);
-
-describe('cosTaskArchive (file backend)', () => {
- it('round-trips a full record per source and upserts by id', async () => {
- await archiveTasks('user', [task('task-a')]);
- await archiveTasks('internal', [task('sys-a')]);
- await archiveTasks('user', [task('task-a', { description: 'Edited' })]);
- expect(await getArchivedTask('task-a')).toEqual({ source: 'user', task: expect.objectContaining({ description: 'Edited', metadata: { prompt: 'Prompt for task-a' } }) });
- expect(await getArchivedTask('sys-a', 'internal')).toMatchObject({ source: 'internal' });
- expect(await getArchivedTask('sys-a', 'user')).toBeNull();
- expect(await countArchivedTasks('user')).toBe(1);
- });
-
- it('pages newest id first with a strict before cursor, in code-unit order', async () => {
- await archiveTasks('user', ['task-b', 'task-a', 'task-C', 'task-d'].map((id) => task(id)));
- expect((await listArchivedTasks('user', { limit: 10 })).map((t) => t.id)).toEqual(['task-d', 'task-b', 'task-a', 'task-C']);
- expect((await listArchivedTasks('user', { before: 'task-b', limit: 2 })).map((t) => t.id)).toEqual(['task-a', 'task-C']);
- expect(await listArchivedTasks('user', { before: 'task-C', limit: 5 })).toEqual([]);
- expect((await listArchivedTasks('user', { offset: 1, limit: 2 })).map((t) => t.id)).toEqual(['task-b', 'task-a']);
- });
-
- it('reports which ids are archived, summaries without prompt bodies, and deletes by source', async () => {
- await archiveTasks('user', [task('task-a'), task('task-b')]);
- await archiveTasks('internal', [task('sys-a')]);
- expect(await getArchivedIds('user', ['task-a', 'task-z', 'sys-a'])).toEqual(new Set(['task-a']));
- expect(await getArchivedIds('user', [])).toEqual(new Set());
- const summaries = await listArchivedSummaries();
- expect(summaries).toHaveLength(3);
- expect(summaries).toContainEqual({ source: 'internal', id: 'sys-a', description: 'Example sys-a' });
- expect(JSON.stringify(summaries)).not.toContain('Prompt for');
- expect(await deleteArchivedTask('sys-a', 'user')).toBe(false);
- expect(await deleteArchivedTask('sys-a')).toBe(true);
- expect(await countArchivedTasks('internal')).toBe(0);
- });
-
- it('looks an id up user-first when both queues archived the same id', async () => {
- await archiveTasks('internal', [task('task-shared', { description: 'Internal copy' })]);
- await archiveTasks('user', [task('task-shared', { description: 'User copy' })]);
- expect(await getArchivedTask('task-shared')).toMatchObject({ source: 'user', task: { description: 'User copy' } });
- expect(await getArchivedTask('task-shared', 'internal')).toMatchObject({ source: 'internal' });
- });
-
- it('ignores an unknown source and an empty batch', async () => {
- await archiveTasks('elsewhere', [task('task-a')]);
- await archiveTasks('user', []);
- expect(await countArchivedTasks('user')).toBe(0);
- });
-});
diff --git a/server/services/cosTaskGenerator.js b/server/services/cosTaskGenerator.js
index 7cfd33fe0f..ffaa315102 100644
--- a/server/services/cosTaskGenerator.js
+++ b/server/services/cosTaskGenerator.js
@@ -581,8 +581,6 @@ export async function admitAutoApprovedSystemTasks(ctx, adapter) {
} = ctx;
const { canSpawn, trackSpawn, emitSpawn, isOwned = () => false, onDefer = async () => {} } = adapter;
- if (!cosTaskData.exists) return [];
-
// A task already spawning or admitted this cycle is not a candidate: letting
// it reach the gates would record a capacity/cooldown deferral for work that
// is already running.
@@ -1053,7 +1051,7 @@ export async function evaluateTasks(options) {
}
// Emit awaiting approval count if any
- if (cosTaskData.exists && cosTaskData.awaitingApproval?.length > 0) {
+ if (cosTaskData.awaitingApproval?.length > 0) {
emitLog('info', `${cosTaskData.awaitingApproval.length} tasks awaiting approval`);
cosEvents.emit('evaluation', {
message: 'Tasks awaiting approval',
diff --git a/server/services/cosTaskMerge.js b/server/services/cosTaskMerge.js
index 6a0e8ab512..f01318ef15 100644
--- a/server/services/cosTaskMerge.js
+++ b/server/services/cosTaskMerge.js
@@ -2,8 +2,8 @@
* CoS Task Merge — claim-aware per-task LWW for cross-peer federation (#1712)
*
* The second half of #1650: where the completed-agent HISTORY federates as pure
- * append-only byte replication, the live task files (data/COS-TASKS.md /
- * data/TASKS.md) are the opposite — BOTH full-sync peers mutate them, and each
+ * append-only byte replication, the live task queues (`cos_tasks`, both
+ * sources) are the opposite — BOTH full-sync peers mutate them, and each
* task carries claim/lease metadata (`claimedBy`/`claimedAt`/`leaseExpiresAt`
* from #1563). A naive whole-file last-writer-wins would clobber a peer's fresh
* claim and re-introduce the exact double-spawn hazard the lease exists to
diff --git a/server/services/cosTaskRepository.db.test.js b/server/services/cosTaskRepository.db.test.js
new file mode 100644
index 0000000000..f9ceb5a47e
--- /dev/null
+++ b/server/services/cosTaskRepository.db.test.js
@@ -0,0 +1,30 @@
+/**
+ * PostgreSQL backend of the CoS task queues, against the shared contract in
+ * _cosTaskRepositoryContract.js. Runs via `npm run test:db` (→ `portos_test`)
+ * ONLY; skips cleanly when no database is reachable.
+ */
+import { afterAll, describe } from 'vitest';
+import { checkHealth, close, ensureSchema, query, withTransaction } from '../lib/db.js';
+import { requireDbOrSkip } from '../lib/dbTestGate.js';
+import { __makePgCosTaskBackend } from './cosTaskRepository.js';
+import { __runCosTaskRepositoryContract } from './_cosTaskRepositoryContract.js';
+
+const health = await checkHealth().catch(() => ({ connected: false }));
+if (health.connected) await ensureSchema();
+const runDb = requireDbOrSkip('services/cosTaskRepository.db.test', health.connected, 'disposable PostgreSQL unavailable');
+
+afterAll(async () => {
+ if (runDb) { await query('DELETE FROM cos_tasks'); await query('DELETE FROM cos_task_lists'); }
+ if (health.connected) await close();
+});
+
+describe.skipIf(!runDb)('cos_tasks repository on PostgreSQL', () => {
+ __runCosTaskRepositoryContract({
+ name: 'postgres',
+ makeBackend: async () => {
+ await query('DELETE FROM cos_tasks');
+ await query('DELETE FROM cos_task_lists');
+ return __makePgCosTaskBackend({ query, withTransaction });
+ },
+ });
+});
diff --git a/server/services/cosTaskRepository.js b/server/services/cosTaskRepository.js
new file mode 100644
index 0000000000..e0d53d5953
--- /dev/null
+++ b/server/services/cosTaskRepository.js
@@ -0,0 +1,235 @@
+/**
+ * Persistence for the CoS task queues (`cos_tasks`): one row per task, per
+ * queue ('user' = the human's task list, 'internal' = the system's).
+ *
+ * PostgreSQL is authoritative. The in-memory backend exists only for the
+ * documented development/test escape hatch (it persists to `data/cos-tasks.json`
+ * outside the test runner so a database-less dev run survives a restart).
+ *
+ * Callers work with the WORKING SET: every open task plus recently completed
+ * ones (see lib/cosTaskRows.js), which is what the old task files held. Older
+ * completed tasks are reached by id or history paging, never loaded wholesale.
+ * `version` bumps on every write so a cached working set can be checked with a
+ * single-row read.
+ */
+
+import { join } from 'node:path';
+import { atomicWrite, PATHS, readJSONFileStrict } from '../lib/fileUtils.js';
+import { createPgFileFacade, resolvePgBackend } from '../lib/pgFileFacade.js';
+import { isTestRunner } from '../lib/runtimeEnv.js';
+import {
+ WORKING_SET_COMPLETED_WINDOW_MS, WORKING_SET_KEEP_COMPLETED, completionStamp, rowColumns, selectWorkingSet,
+} from '../lib/cosTaskRows.js';
+
+const SOURCES = ['user', 'internal'];
+// Bounds a single statement's parameter payload (prompts can be MBs).
+const UPSERT_BATCH = 100;
+const tasksFile = () => join(PATHS.data, 'cos-tasks.json');
+const byIdDesc = (a, b) => (a.id < b.id ? 1 : a.id > b.id ? -1 : 0);
+
+function makePgBackend(db) {
+ const { query, withTransaction } = db;
+ return {
+ name: 'postgres',
+ async readVersion(source) {
+ const { rows } = await query('SELECT version FROM cos_task_lists WHERE source = $1', [source]);
+ return rows[0] ? Number(rows[0].version) : 0;
+ },
+ async loadWorkingSet(source, { now = Date.now() } = {}) {
+ const version = await this.readVersion(source);
+ const { rows } = await query(
+ `WITH newest AS (
+ SELECT task_id FROM cos_tasks WHERE source = $1 AND status = 'completed'
+ ORDER BY completed_at DESC NULLS LAST, task_id DESC LIMIT $3
+ )
+ SELECT task, position FROM cos_tasks
+ WHERE source = $1 AND (status <> 'completed' OR completed_at >= $2 OR task_id IN (SELECT task_id FROM newest))
+ ORDER BY CASE status WHEN 'pending' THEN 0 WHEN 'in_progress' THEN 1 WHEN 'challenged' THEN 2
+ WHEN 'blocked' THEN 3 ELSE 4 END,
+ priority_value DESC, position, task_id`,
+ [source, new Date(now - WORKING_SET_COMPLETED_WINDOW_MS).toISOString(), WORKING_SET_KEEP_COMPLETED],
+ );
+ return { version, rows: rows.map((row) => ({ task: row.task, position: row.position })) };
+ },
+ async apply(source, { upserts, deletes }) {
+ return withTransaction(async (client) => {
+ if (deletes.length > 0) await client.query('DELETE FROM cos_tasks WHERE source = $1 AND task_id = ANY($2::text[])', [source, deletes]);
+ for (let start = 0; start < upserts.length; start += UPSERT_BATCH) {
+ const columns = upserts.slice(start, start + UPSERT_BATCH).map(({ task, position }) => ({ task, position, ...rowColumns(task) }));
+ await client.query(
+ `INSERT INTO cos_tasks (source, task_id, status, priority_value, position, completed_at, description, task)
+ SELECT $1, u.task_id, u.status, u.priority_value, u.position, u.completed_at, u.description, u.task::jsonb
+ FROM unnest($2::text[], $3::text[], $4::int[], $5::float8[], $6::timestamptz[], $7::text[], $8::text[])
+ AS u(task_id, status, priority_value, position, completed_at, description, task)
+ ON CONFLICT (source, task_id) DO UPDATE SET status = EXCLUDED.status, priority_value = EXCLUDED.priority_value,
+ position = EXCLUDED.position, completed_at = EXCLUDED.completed_at, description = EXCLUDED.description, task = EXCLUDED.task`,
+ [source, columns.map((c) => c.task.id), columns.map((c) => c.status), columns.map((c) => c.priorityValue),
+ columns.map((c) => c.position), columns.map((c) => c.completedAt), columns.map((c) => c.description),
+ columns.map((c) => JSON.stringify(c.task))],
+ );
+ }
+ return bumpVersions(client, [source]);
+ });
+ },
+ async getTask(taskId, source) {
+ const { rows } = await query(
+ `SELECT source, task, position FROM cos_tasks WHERE task_id = $1 AND ($2::text IS NULL OR source = $2)
+ ORDER BY (source = 'user') DESC LIMIT 1`,
+ [taskId, source],
+ );
+ return rows[0] ? { source: rows[0].source, task: rows[0].task, position: rows[0].position } : null;
+ },
+ async completedIds(source, ids) {
+ if (ids.length === 0) return new Set();
+ const { rows } = await query(
+ `SELECT task_id FROM cos_tasks WHERE source = $1 AND status = 'completed' AND task_id = ANY($2::text[])`,
+ [source, ids],
+ );
+ return new Set(rows.map((row) => row.task_id));
+ },
+ async pageCompleted(source, { before, offset, limit }) {
+ const { rows } = await query(
+ `SELECT task FROM cos_tasks WHERE source = $1 AND status = 'completed' AND ($2::text IS NULL OR task_id < $2)
+ ORDER BY task_id DESC LIMIT $3 OFFSET $4`,
+ [source, before ?? null, limit, offset],
+ );
+ return rows.map((row) => row.task);
+ },
+ async countCompleted(source) {
+ const { rows } = await query(`SELECT count(*)::int AS n FROM cos_tasks WHERE source = $1 AND status = 'completed'`, [source]);
+ return rows[0].n;
+ },
+ async completedSummaries() {
+ const { rows } = await query(`SELECT source, task_id, description FROM cos_tasks WHERE status = 'completed'`);
+ return rows.map((row) => ({ source: row.source, id: row.task_id, description: row.description }));
+ },
+ async deleteById(taskId, source) {
+ return withTransaction(async (client) => {
+ const { rows } = await client.query(
+ 'DELETE FROM cos_tasks WHERE task_id = $1 AND ($2::text IS NULL OR source = $2) RETURNING source',
+ [taskId, source],
+ );
+ if (rows.length > 0) await bumpVersions(client, [...new Set(rows.map((row) => row.source))]);
+ return rows.length;
+ });
+ },
+ };
+}
+
+// Bumps each source's version and returns the last one (callers pass one source for a save).
+async function bumpVersions(client, sources) {
+ let version = 0;
+ for (const source of sources) {
+ const { rows } = await client.query(
+ `INSERT INTO cos_task_lists (source, version) VALUES ($1, 1)
+ ON CONFLICT (source) DO UPDATE SET version = cos_task_lists.version + 1 RETURNING version`,
+ [source],
+ );
+ version = Number(rows[0].version);
+ }
+ return version;
+}
+
+async function makeMemoryBackend({ persist }) {
+ const lists = Object.fromEntries(SOURCES.map((source) => [source, new Map()]));
+ const versions = Object.fromEntries(SOURCES.map((source) => [source, 0]));
+ if (persist) {
+ const saved = await readJSONFileStrict(tasksFile(), {}, { logError: false });
+ for (const source of SOURCES) {
+ for (const row of saved?.[source]?.rows ?? []) lists[source].set(row.task.id, row);
+ versions[source] = saved?.[source]?.version ?? 0;
+ }
+ }
+ const flush = () => (persist
+ ? atomicWrite(tasksFile(), Object.fromEntries(SOURCES.map((source) => [source, { version: versions[source], rows: [...lists[source].values()] }])))
+ : Promise.resolve());
+ // Plain JSON, like a database round trip; also keeps structuredClone spies in tests measuring only the store.
+ const clone = (value) => JSON.parse(JSON.stringify(value));
+ const completed = (source) => [...lists[source].values()].map((row) => row.task).filter((task) => task.status === 'completed');
+
+ return {
+ name: 'memory',
+ async readVersion(source) { return versions[source]; },
+ async loadWorkingSet(source, { now = Date.now() } = {}) {
+ return { version: versions[source], rows: clone(selectWorkingSet([...lists[source].values()], { now })) };
+ },
+ async apply(source, { upserts, deletes }) {
+ for (const id of deletes) lists[source].delete(id);
+ for (const { task, position } of upserts) lists[source].set(task.id, clone({ task, position }));
+ versions[source] += 1;
+ await flush();
+ return versions[source];
+ },
+ async getTask(taskId, source) {
+ for (const candidate of source ? [source] : SOURCES) {
+ const row = lists[candidate].get(taskId);
+ if (row) return { source: candidate, ...clone(row) };
+ }
+ return null;
+ },
+ async completedIds(source, ids) {
+ return new Set(completed(source).map((task) => task.id).filter((id) => ids.includes(id)));
+ },
+ async pageCompleted(source, { before, offset, limit }) {
+ return clone(completed(source).filter((task) => before == null || task.id < before).sort(byIdDesc).slice(offset, offset + limit));
+ },
+ async countCompleted(source) { return completed(source).length; },
+ async completedSummaries() {
+ return SOURCES.flatMap((source) => completed(source).map((task) => ({ source, id: task.id, description: rowColumns(task).description })));
+ },
+ async deleteById(taskId, source) {
+ let removed = 0;
+ for (const candidate of source ? [source] : SOURCES) {
+ if (lists[candidate].delete(taskId)) { removed += 1; versions[candidate] += 1; }
+ }
+ if (removed > 0) await flush();
+ return removed;
+ },
+ };
+}
+
+const backendFacade = createPgFileFacade({
+ makeFile: () => makeMemoryBackend({ persist: !isTestRunner() }),
+ makePg: () => resolvePgBackend({
+ requirement: 'The CoS task queues require PostgreSQL — run `npm run setup:db` (dev/test only: set MEMORY_BACKEND=file for the file escape hatch)',
+ loadDb: () => import('../lib/db.js'),
+ makePg: makePgBackend,
+ }),
+});
+const backend = async () => backendFacade.getBackend();
+
+/** Queue version: changes whenever a write lands, so a cached working set can be revalidated cheaply. */
+export async function readTaskListVersion(source) { return (await backend()).readVersion(source); }
+
+/** `{ version, rows: [{ task, position }] }` in queue order for one queue's working set. */
+export async function loadTaskWorkingSet(source) { return (await backend()).loadWorkingSet(source); }
+
+/** Persist a save plan (`planListSave`'s upserts/deletes) in one transaction; returns the new version. */
+export async function applyTaskListSave(source, plan) { return (await backend()).apply(source, plan); }
+
+/** One task as `{ source, task, position }` whatever its status or age, user queue first. Omit `source` to search both. */
+export async function getStoredTask(taskId, source = null) { return (await backend()).getTask(taskId, source); }
+
+/** The subset of `ids` already stored as completed in `source`, including ones outside the working set. */
+export async function getCompletedTaskIds(source, ids) { return (await backend()).completedIds(source, ids); }
+
+/** Completed history, newest id first: strictly before `before`, skipping `offset`, at most `limit`. */
+export async function pageCompletedTasks(source, { before = null, offset = 0, limit = 25 } = {}) {
+ return (await backend()).pageCompleted(source, { before, offset, limit });
+}
+
+export async function countCompletedTasks(source) { return (await backend()).countCompleted(source); }
+
+/** `{ source, id, description }` for every completed task, without prompt bodies. */
+export async function listCompletedTaskSummaries() { return (await backend()).completedSummaries(); }
+
+/** Remove a task wherever it is stored; returns how many rows went. */
+export async function deleteStoredTask(taskId, source = null) { return (await backend()).deleteById(taskId, source); }
+
+/** Test seam: drop the selected backend (and, for the in-memory one, everything in it). */
+export function __resetCosTaskRepository() { backendFacade.reset(); }
+
+/** Test seam: the PostgreSQL backend over a `{ query, withTransaction }` handle, for the DB suite. */
+export const __makePgCosTaskBackend = makePgBackend;
+/** Test seam: a fresh in-memory backend that never touches disk. */
+export const __makeMemoryCosTaskBackend = () => makeMemoryBackend({ persist: false });
diff --git a/server/services/cosTaskRepository.test.js b/server/services/cosTaskRepository.test.js
new file mode 100644
index 0000000000..8a6e85549b
--- /dev/null
+++ b/server/services/cosTaskRepository.test.js
@@ -0,0 +1,17 @@
+import { describe, expect, it } from 'vitest';
+import { __makeMemoryCosTaskBackend, __resetCosTaskRepository, applyTaskListSave, loadTaskWorkingSet, readTaskListVersion } from './cosTaskRepository.js';
+import { __runCosTaskRepositoryContract } from './_cosTaskRepositoryContract.js';
+
+__runCosTaskRepositoryContract({ name: 'in-memory', makeBackend: () => __makeMemoryCosTaskBackend() });
+
+describe('cosTaskRepository facade (test runner)', () => {
+ it('selects the in-memory backend and does not touch disk', async () => {
+ __resetCosTaskRepository();
+ const task = { id: 'task-example-0001', status: 'pending', priority: 'MEDIUM', priorityValue: 2, description: 'Example', approvalRequired: false, autoApproved: true, metadata: {} };
+ await applyTaskListSave('user', { upserts: [{ task, position: 0 }], deletes: [] });
+ expect((await loadTaskWorkingSet('user')).rows).toHaveLength(1);
+ expect(await readTaskListVersion('user')).toBe(1);
+ __resetCosTaskRepository();
+ expect((await loadTaskWorkingSet('user')).rows).toHaveLength(0);
+ });
+});
diff --git a/server/services/cosTaskStore.js b/server/services/cosTaskStore.js
index 9aee8657e4..3daccc698b 100644
--- a/server/services/cosTaskStore.js
+++ b/server/services/cosTaskStore.js
@@ -5,9 +5,9 @@ import { getSkipReason } from './cosTaskClaim.js';
* CoS Task Store Module
*
* Task CRUD + queue persistence extracted from cos.js. Owns the read/write
- * round-trip to the user (TASKS.md) and internal (COS-TASKS.md) task files:
- * parsing, grouping, dedup, and the `tasks:changed` event emissions that drive
- * the scheduler. The request → record mapping (ID generation, metadata
+ * round-trip to the user and internal task queues (`cos_tasks`, through
+ * `cosTaskRepository.js`): the cached working set, row-diff saves, grouping,
+ * dedup, and the `tasks:changed` event emissions that drive the scheduler. The request → record mapping (ID generation, metadata
* normalization) is `cosTaskIntake.js`, kept in parity with the request schema.
*
* Self-contained — it emits `tasks:changed` rather than calling the scheduler
@@ -18,12 +18,8 @@ import { getSkipReason } from './cosTaskClaim.js';
import { DEVELOPMENT_ACTIVE_STATUSES, developmentWorkIdentity, sameDevelopmentWork } from '../lib/developmentWorkIdentity.js';
import { reviewerModelsFromDefaults } from '../lib/reviewerConfig.js';
-import { readFile, stat } from 'fs/promises';
-import { atomicWrite } from '../lib/fileUtils.js';
import { createSingleFlight } from '../lib/singleFlight.js';
-import { existsSync } from 'fs';
-import { join } from 'path';
-import { parseTasksMarkdown, groupTasksByStatus, getAutoApprovedTasks, getAwaitingApprovalTasks, generateTasksMarkdown, hasKnownPrefix, toRepresentableTask, PRIORITY_VALUES } from '../lib/taskParser.js';
+import { groupTasksByStatus, getAutoApprovedTasks, getAwaitingApprovalTasks, hasKnownPrefix, toRepresentableTask, PRIORITY_VALUES } from '../lib/taskParser.js';
import { MAX_TOTAL_SPAWNS, cosTaskTypeSchema, validateRequest } from '../lib/validation.js';
import { RETRY_HOLD_KEY, RETRY_HOLD_SINCE_KEY, isRetryHeld } from '../lib/taskRetryHold.js';
import { resolveTaskTargetBranch, shouldStripTaskTargetBranch } from '../lib/taskTargetBranch.js';
@@ -34,12 +30,13 @@ import { isInvestigationTask } from '../lib/investigationTasks.js';
import { PAUSED_BLOCKED_CATEGORIES, USER_DECISION_BLOCKED_CATEGORIES } from '../lib/taskBlockCategories.js';
import { splitTaskPromptFields, TASK_PROMPT_KEY, TASK_CONTEXT_KEY } from '../lib/cosTaskPrompt.js';
import { normalizeOrchestrationMode, normalizeOrchestrationProfile } from '../lib/orchestrationProfile.js';
-import { loadState, withStateLock, ROOT_DIR } from './cosState.js';
+import { loadState, withStateLock } from './cosState.js';
import { hasActiveTaskOwner } from './agentState.js';
import { cosEvents } from './cosEvents.js';
import { CLAIM_METADATA_KEYS } from './cosTaskClaim.js';
import { mergeTaskLists } from './cosTaskMerge.js';
-import { archiveTasks, countArchivedTasks, deleteArchivedTask, getArchivedIds, getArchivedTask, listArchivedTasks } from './cosTaskArchive.js';
+import { applyTaskListSave, countCompletedTasks, deleteStoredTask, getCompletedTaskIds, getStoredTask, loadTaskWorkingSet, pageCompletedTasks, readTaskListVersion } from './cosTaskRepository.js';
+import { compareRows, planListSave } from '../lib/cosTaskRows.js';
import { buildQueuedTask } from './cosTaskIntake.js';
import { canChallenge, getChallengeCount, buildChallengePatch, buildChallengeResolutionPatch, classifyRecheckOutcome, MAX_CHALLENGES_PER_TASK } from './cosChallenge.js';
import { runLocalCodeReview, getCodeReviewDefaults } from './codeReview.js';
@@ -161,71 +158,51 @@ function isContentEdit(updates, existingMetadata = {}) {
return false;
}
-// ── Parsed-task cache (issue #3497) ─────────────────────────────────────────
+// ── Working-set cache ───────────────────────────────────────────────────────
//
// `getUserTasks`/`getCosTasks`/`getTaskById` run on every daemon evaluation
-// tick, scheduler sweep, and agent state update, and each call used to do a
-// full `readFile` + regex-heavy `parseTasksMarkdown` of a `COS-TASKS.md` that
-// grows to hundreds of tasks carrying prompt bodies and JSON metadata strings.
-// Cache the parsed array per file so an unchanged file costs one `stat` plus a
-// structured clone instead of a read + full parse.
+// tick, scheduler sweep, and agent state update. A queue's WORKING SET (every
+// open task plus recently completed ones, see lib/cosTaskRows.js) is loaded once
+// per queue version and shared: an unchanged queue costs one single-row version
+// read plus a structured clone instead of a query over the rows.
//
-// Concurrent API/daemon readers share the read/parse operation for each stamp.
-// Otherwise a cold cache multiplies whole-file strings and parsed task arrays
-// by the number of readers (#12225). Correctness rests on two signals:
+// Concurrent readers share the load for a version (#12225), so a cold cache does
+// not multiply the working set by the number of readers. Correctness rests on:
//
-// 1. The stamp (`mtimeMs` + `size`) catches writes this module did NOT make —
-// a user editing `TASKS.md` in an editor, a restore, a peer's direct write.
-// 2. Every write through `writeTaskFile` DROPS the entry before AND after the
-// write, including reads started while the rename was pending. mtime can
-// be as coarse as one second on some filesystems, so a write-then-read in
-// the same tick must not depend on the stamp having moved.
-const parsedTaskCache = new Map(); // filePath -> generation { stamp, snapshot? }
-const taskSnapshotReads = createSingleFlight();
-
-// `null` = could not stat (missing/unreadable) → do not cache, distinct from a
-// legitimately empty file, which stamps normally and caches its empty parse.
-const taskFileStamp = async (filePath) => {
- const stats = await stat(filePath).catch(() => null);
- return stats ? `${stats.mtimeMs}:${stats.size}` : null;
-};
+// 1. The queue version, bumped by every write in the same transaction, so a
+// write this process did NOT make is noticed on the next read.
+// 2. Our own writes replace the cached entry only when the version moved by
+// exactly one (nobody else wrote in between); otherwise they drop it.
+const snapshotCache = new Map(); // source -> { version, snapshot }
+const snapshotLoads = createSingleFlight();
// Private snapshots never leave this module: list readers clone the array,
// while ID lookups clone only the matching task. Both share invalidation.
-async function readTaskSnapshot(filePath) {
- // Every caller checks external edits before joining a flight: a new writer
- // must not consume a read that captured the file before an editor's change.
- const stamp = await taskFileStamp(filePath);
- let generation = parsedTaskCache.get(filePath);
- if (!generation || generation.stamp !== stamp) {
- generation = { stamp };
- parsedTaskCache.set(filePath, generation);
- }
- // The entry identity is the flight key: invalidation detaches older reads
- // without letting them repopulate the cache or capture post-write callers.
- return taskSnapshotReads.run(generation, async () => {
- if (stamp && generation.snapshot) return generation.snapshot;
-
- const snapshot = { stamp, tasks: parseTasksMarkdown(await readFile(filePath, 'utf-8')) };
- if (parsedTaskCache.get(filePath) === generation) {
- if (stamp) generation.snapshot = snapshot;
- else parsedTaskCache.delete(filePath);
+// `rows` keeps each task's stored position for the next save's diff.
+async function readTaskSnapshot(source) {
+ const version = await readTaskListVersion(source);
+ const cached = snapshotCache.get(source);
+ if (cached?.version === version) return cached.snapshot;
+ return snapshotLoads.run(`${source}:${version}`, async () => {
+ const loaded = await loadTaskWorkingSet(source);
+ const snapshot = { rows: loaded.rows, tasks: loaded.rows.map((row) => row.task) };
+ if (!snapshotCache.get(source) || snapshotCache.get(source).version <= loaded.version) {
+ snapshotCache.set(source, { version: loaded.version, snapshot });
}
return snapshot;
});
}
-async function readTaskFile(filePath) {
- return structuredClone((await readTaskSnapshot(filePath)).tasks);
+async function readTaskFile(source) {
+ return structuredClone((await readTaskSnapshot(source)).tasks);
}
-async function findTaskInFile(filePath, taskId) {
- if (!existsSync(filePath)) return null;
- const snapshot = await readTaskSnapshot(filePath);
+async function findTaskInFile(source, taskId) {
+ const snapshot = await readTaskSnapshot(source);
if (!snapshot.byId) {
snapshot.byId = new Map();
for (const task of snapshot.tasks) {
- // Preserve Array.find's first-match behavior for hand-edited duplicate IDs.
+ // Preserve Array.find's first-match behavior for duplicate IDs.
if (!snapshot.byId.has(task.id)) snapshot.byId.set(task.id, task);
}
}
@@ -234,57 +211,51 @@ async function findTaskInFile(filePath, taskId) {
}
/**
- * Write task markdown, dropping the now-stale parse for that file. Every write
- * path in this module goes through here — see invalidation signal 2 above.
+ * Persist the queue as `tasks`: diff against the stored working set and write
+ * only the rows that changed, in one transaction. Every write path in this
+ * module goes through here. Resolves to whether anything was written.
*/
-async function writeTaskFile(filePath, markdown) {
- parsedTaskCache.delete(filePath);
- // Temp-file + rename, never an in-place truncate: TASKS.md is the user's whole
- // task list, and a plain writeFile killed mid-write (PM2 restart, crash, full
- // disk) leaves it empty or half-written — which parses as "no tasks" and is
- // then persisted by the next write.
- await atomicWrite(filePath, markdown).finally(() => parsedTaskCache.delete(filePath));
+async function writeTaskList(source, tasks) {
+ const snapshot = await readTaskSnapshot(source);
+ const cached = snapshotCache.get(source);
+ const plan = planListSave({ previous: snapshot.rows, next: tasks, source });
+ if (plan.upserts.length === 0 && plan.deletes.length === 0) return false;
+ const version = await applyTaskListSave(source, plan);
+
+ const positions = new Map(snapshot.rows.map((row) => [row.task.id, row.position]));
+ for (const { task, position } of plan.upserts) positions.set(task.id, position);
+ if (cached?.snapshot === snapshot && version === cached.version + 1) {
+ const rows = plan.saved.map((task) => ({ task, position: positions.get(task.id) })).sort(compareRows);
+ snapshotCache.set(source, { version, snapshot: { rows, tasks: rows.map((row) => row.task) } });
+ } else {
+ snapshotCache.delete(source);
+ }
+ return true;
}
-/** Test hook: forget every cached parse. */
+/** Test hook: forget every cached working set. */
export function __resetTaskCache() {
- parsedTaskCache.clear();
+ snapshotCache.clear();
}
/**
- * Get user tasks from TASKS.md
+ * Get user tasks
*/
-export async function getUserTasks(tasksFilePath = null) {
- const state = await loadState();
- const filePath = tasksFilePath || join(ROOT_DIR, state.config.userTasksFile);
-
- if (!existsSync(filePath)) {
- return { tasks: [], grouped: groupTasksByStatus([]), file: filePath, exists: false, type: 'user' };
- }
-
- const tasks = await readTaskFile(filePath);
- const grouped = groupTasksByStatus(tasks);
-
- return { tasks, grouped, file: filePath, exists: true, type: 'user' };
+export async function getUserTasks() {
+ const tasks = await readTaskFile('user');
+ return { tasks, grouped: groupTasksByStatus(tasks), type: 'user' };
}
/**
- * Get CoS internal tasks from COS-TASKS.md
+ * Get CoS internal tasks
*/
-export async function getCosTasks(tasksFilePath = null) {
- const state = await loadState();
- const filePath = tasksFilePath || join(ROOT_DIR, state.config.cosTasksFile);
-
- if (!existsSync(filePath)) {
- return { tasks: [], grouped: groupTasksByStatus([]), file: filePath, exists: false, type: 'internal' };
- }
-
- const tasks = await readTaskFile(filePath);
+export async function getCosTasks() {
+ const tasks = await readTaskFile('internal');
const grouped = groupTasksByStatus(tasks);
const autoApproved = getAutoApprovedTasks(tasks);
const awaitingApproval = getAwaitingApprovalTasks(tasks);
- return { tasks, grouped, file: filePath, exists: true, type: 'internal', autoApproved, awaitingApproval };
+ return { tasks, grouped, type: 'internal', autoApproved, awaitingApproval };
}
/**
@@ -298,74 +269,73 @@ export async function getAllTasks() {
// ── Bounded task views (#9677) ──────────────────────────────────────────────
//
// `GET /api/cos/tasks` returns the open queue plus at most one page of completed
-// history, yet `getAllTasks()` structured-clones every archived prompt body from
-// BOTH sources first. These readers select from the private snapshot and clone
-// only what the response carries. Nothing returned here aliases the cache.
+// history, yet `getAllTasks()` structured-clones the whole working set (recent
+// completed prompt bodies included) from BOTH queues first. These readers select
+// from the private snapshot, or page completed history straight from the table,
+// and clone only what the response carries. Nothing returned here aliases the cache.
const COMPLETED_FIRST_PAGE = 25;
-const byIdDesc = (a, b) => (a.id < b.id ? 1 : a.id > b.id ? -1 : 0);
+const SOURCES = ['user', 'internal'];
const isCompleted = (task) => task.status === 'completed';
-// Per-snapshot derivations, dropped with the snapshot on any stamp/write change.
+// Per-snapshot derivations, dropped with the snapshot on any version change.
function snapshotPartitions(snapshot) {
if (!snapshot.partitions) {
const open = [];
const completed = [];
for (const task of snapshot.tasks) (isCompleted(task) ? completed : open).push(task);
- completed.sort(byIdDesc);
snapshot.partitions = { open, completed };
}
return snapshot.partitions;
}
-async function resolveSourceFile(type) {
- const { config } = await loadState();
- const filePath = join(ROOT_DIR, type === 'internal' ? config.cosTasksFile : config.userTasksFile);
- return existsSync(filePath) ? filePath : null;
-}
-
// Shape of getUserTasks()/getCosTasks() over an already-selected task subset.
-// Callers pass CLONED tasks; grouped/derived lists share those clones. A missing
-// file keeps getCosTasks()'s shape: no derived lists.
-function sourceShape(type, filePath, tasks, { exists = true } = {}) {
- const shape = { tasks, grouped: groupTasksByStatus(tasks), file: filePath, exists, type };
- if (type === 'internal' && exists) {
+// Callers pass CLONED tasks; grouped/derived lists share those clones.
+function sourceShape(type, tasks) {
+ const shape = { tasks, grouped: groupTasksByStatus(tasks), type };
+ if (type === 'internal') {
shape.autoApproved = getAutoApprovedTasks(tasks);
shape.awaitingApproval = getAwaitingApprovalTasks(tasks);
}
return shape;
}
+/**
+ * One page of completed history from ONE queue, newest id first, straight from
+ * the table. `nextCursor` is the last id of a full page when more remain.
+ */
+async function pageCompleted(type, { cursor = null, limit }) {
+ const rows = await pageCompletedTasks(type, { before: cursor, limit: limit + 1 });
+ const items = rows.slice(0, limit);
+ return { items, total: await countCompletedTasks(type), nextCursor: rows.length > limit ? items[limit - 1].id : null };
+}
+
/**
* Open queue (every non-completed task, plus the `selected` historical task)
- * for both sources. `completedCount` carries the archived total.
+ * for both queues. `completedCount` carries the completed total.
*/
async function readQueueView(selected) {
const read = async (type) => {
- const filePath = await resolveSourceFile(type);
- const archivedCount = await countArchivedTasks(type);
- if (!filePath) return { ...sourceShape(type, filePath, [], { exists: false }), completedCount: archivedCount };
- const snapshot = await readTaskSnapshot(filePath);
- const { open, completed } = snapshotPartitions(snapshot);
+ const snapshot = await readTaskSnapshot(type);
+ const { open } = snapshotPartitions(snapshot);
const refs = selected === undefined ? open : snapshot.tasks.filter((task) => !isCompleted(task) || task.id === selected);
const tasks = structuredClone(refs);
- // A selected historical task that has aged out of the file lives in the archive.
+ // A selected historical task older than the working set is fetched by id.
if (selected !== undefined && !refs.some((task) => task.id === selected)) {
- const archived = await getArchivedTask(selected, type);
- if (archived) tasks.push(archived.task);
+ const stored = await getStoredTask(selected, type);
+ if (stored) tasks.push(stored.task);
}
- return { ...sourceShape(type, filePath, tasks), completedCount: completed.length + archivedCount };
+ return { ...sourceShape(type, tasks), completedCount: await countCompletedTasks(type) };
};
const [user, cos] = await Promise.all([read('user'), read('internal')]);
return { user, cos };
}
-/** Open queue plus the first completed page; completed history stays archived. */
+/** Open queue plus the first completed page; the rest of the history stays in the table. */
async function readBareView() {
const read = async (type) => {
- const filePath = await resolveSourceFile(type);
- const { open, completed } = filePath ? snapshotPartitions(await readTaskSnapshot(filePath)) : { open: [], completed: [] };
- const page = await pageCompleted(type, completed, { limit: COMPLETED_FIRST_PAGE });
- const shape = sourceShape(type, filePath, [...structuredClone(open), ...page.items], { exists: Boolean(filePath) });
+ const { open } = snapshotPartitions(await readTaskSnapshot(type));
+ const page = await pageCompleted(type, { limit: COMPLETED_FIRST_PAGE });
+ const shape = sourceShape(type, [...structuredClone(open), ...page.items]);
// Completed rows leave `grouped`; only the page inside `tasks` carries them.
return { ...shape, grouped: { ...shape.grouped, completed: [] }, completedCount: page.total, completedNextCursor: page.nextCursor };
};
@@ -373,51 +343,20 @@ async function readBareView() {
return { user, cos };
}
-/**
- * One page of completed history from ONE source, newest id first, merged from
- * the file's recent window and the archive. A task caught between archive and
- * file removal exists in both; the file copy wins. `completedRefs` are snapshot
- * references (newest first); only the returned page is cloned.
- */
-async function pageCompleted(type, completedRefs, { cursor = null, limit }) {
- const fileRemaining = cursor ? completedRefs.filter((task) => task.id < cursor) : completedRefs;
- const filePage = fileRemaining.slice(0, limit + 1);
- const fileIds = new Set(filePage.map((task) => task.id));
- const archivedPage = (await listArchivedTasks(type, { before: cursor, limit: limit + 1 })).filter((task) => !fileIds.has(task.id));
- const merged = [...filePage, ...archivedPage].sort(byIdDesc).slice(0, limit + 1);
- const items = merged.slice(0, limit);
- const total = completedRefs.length + await countArchivedTasks(type);
- return {
- items: items.map((task) => (fileIds.has(task.id) ? structuredClone(task) : task)),
- total,
- nextCursor: merged.length > limit ? items[limit - 1].id : null,
- };
-}
-
/** One page of completed history from ONE source, newest id first. */
async function readCompletedView({ source, cursor, limit }) {
- const type = source === 'internal' ? 'internal' : 'user';
- const filePath = await resolveSourceFile(type);
- const completed = filePath ? snapshotPartitions(await readTaskSnapshot(filePath)).completed : [];
- return pageCompleted(type, completed, { cursor, limit });
+ return pageCompleted(source === 'internal' ? 'internal' : 'user', { cursor, limit });
}
/**
- * Legacy limit/offset window over each queue's tasks: the file's tasks in file
- * order, then the archived completed tasks newest id first. The slice and scalar
- * metadata only.
+ * Legacy limit/offset window over each queue's working set, in queue order. The
+ * slice and scalar metadata only; completed history older than the working set
+ * pages through the `completed` view.
*/
async function readWindowView({ offset, limit }) {
const read = async (type) => {
- const filePath = await resolveSourceFile(type);
- const fileTasks = filePath ? (await readTaskSnapshot(filePath)).tasks : [];
- const fileSlice = structuredClone(fileTasks.slice(offset, offset + limit));
- const archivedWanted = limit - fileSlice.length;
- const archived = archivedWanted > 0
- ? await listArchivedTasks(type, { offset: Math.max(0, offset - fileTasks.length), limit: archivedWanted })
- : [];
- const total = fileTasks.length + await countArchivedTasks(type);
- return { meta: { file: filePath, exists: Boolean(filePath), type }, tasks: [...fileSlice, ...archived], total };
+ const { tasks } = await readTaskSnapshot(type);
+ return { meta: { type }, tasks: structuredClone(tasks.slice(offset, offset + limit)), total: tasks.length };
};
const [user, cos] = await Promise.all([read('user'), read('internal')]);
return {
@@ -432,9 +371,9 @@ async function readWindowView({ offset, limit }) {
* - 'queue' { selected? } open tasks (+ the selected historical one) + completedCount
* - 'bare' open tasks + first completed page (+ completedCount/completedNextCursor)
* - 'completed' { source, cursor?, limit } one source's page → { items, total, nextCursor }
- * - 'window' { offset, limit } legacy slice → { user, cos, totals }
+ * - 'window' { offset, limit } legacy slice of the working set → { user, cos, totals }
* Sources come back unsettled for the spawn window; `getAllTasks()` stays the raw
- * truth for scheduler readers and `view=full`.
+ * truth (the working set) for scheduler readers and `view=full`.
*/
export async function getTaskView({ kind, ...options }) {
if (kind === 'queue') return readQueueView(options.selected);
@@ -445,16 +384,13 @@ export async function getTaskView({ kind, ...options }) {
}
/**
- * Lightweight live-activity input. Derive once per task-file snapshot, so a
+ * Lightweight live-activity input. Derive once per queue version, so a
* three-second poll never clones prompts or groups completed history.
* Keep duplicates and source order, matching getAllTasks() counting semantics.
*/
export async function getPendingTaskIds() {
- const { config } = await loadState();
- const sources = await Promise.all([config.userTasksFile, config.cosTasksFile].map(async (file) => {
- const filePath = join(ROOT_DIR, file);
- if (!existsSync(filePath)) return [];
- const snapshot = await readTaskSnapshot(filePath);
+ const sources = await Promise.all(SOURCES.map(async (source) => {
+ const snapshot = await readTaskSnapshot(source);
snapshot.pendingIds ??= snapshot.tasks.filter(task => task.status === 'pending').map(task => task.id);
return snapshot.pendingIds;
}));
@@ -464,15 +400,12 @@ export async function getPendingTaskIds() {
/**
* Telemetry reads only diagnostics-bearing records, never full prompts or
- * grouped history. Derive per snapshot and reuse the task cache's internal-write
- * and external-edit invalidation; clone the projection to keep it private.
+ * grouped history. Derive per snapshot and reuse the cache's write invalidation;
+ * clone the projection to keep it private.
*/
export async function getTaskDiagnostics() {
- const { config } = await loadState();
- const sources = await Promise.all([config.userTasksFile, config.cosTasksFile].map(async (file) => {
- const filePath = join(ROOT_DIR, file);
- if (!existsSync(filePath)) return [];
- const snapshot = await readTaskSnapshot(filePath);
+ const sources = await Promise.all(SOURCES.map(async (source) => {
+ const snapshot = await readTaskSnapshot(source);
snapshot.diagnosticTasks ??= snapshot.tasks.filter(task => {
const diagnostics = task.metadata?.diagnostics;
return diagnostics && typeof diagnostics === 'object' && !Array.isArray(diagnostics);
@@ -488,13 +421,11 @@ export async function getTaskDiagnostics() {
/**
* Approval-state view of the internal queue for pollers that need the awaiting
* list or the auto-approved count, not the whole queue. `getCosTasks()` clones
- * every archived prompt body to answer the same question. Derived once per
+ * every working-set prompt body to answer the same question. Derived once per
* snapshot; only the first `limit` awaiting tasks are cloned.
*/
export async function getCosApprovalView({ limit = Infinity } = {}) {
- const filePath = await resolveSourceFile('internal');
- if (!filePath) return { awaitingApproval: [], autoApprovedCount: 0 };
- const snapshot = await readTaskSnapshot(filePath);
+ const snapshot = await readTaskSnapshot('internal');
snapshot.approval ??= {
awaiting: getAwaitingApprovalTasks(snapshot.tasks),
autoApprovedCount: getAutoApprovedTasks(snapshot.tasks).length,
@@ -517,18 +448,17 @@ export const getTasks = getUserTasks;
* is what a client merging it into a list it already holds needs.
*/
export async function findTaskWithSource(taskId) {
- const { config } = await loadState();
// Keep user-first precedence, including legacy/custom IDs. Do not infer the
// source from a prefix or prepare grouped copies of both queues for one ID.
- const userTask = await findTaskInFile(join(ROOT_DIR, config.userTasksFile), taskId);
+ const userTask = await findTaskInFile('user', taskId);
if (userTask) return { task: userTask, source: 'user' };
- const cosTask = await findTaskInFile(join(ROOT_DIR, config.cosTasksFile), taskId);
+ const cosTask = await findTaskInFile('internal', taskId);
if (cosTask) return { task: cosTask, source: 'internal' };
- // Completed history older than the file window lives in the archive.
- const archived = await getArchivedTask(taskId);
- return archived ? { task: archived.task, source: archived.source } : null;
+ // Completed history older than the working set is fetched by id.
+ const stored = await getStoredTask(taskId);
+ return stored ? { task: stored.task, source: stored.source } : null;
}
// Legacy shape: the source overwrites the record's `taskType`. Callers that
@@ -538,6 +468,15 @@ export async function getTaskById(taskId) {
return found ? { ...found.task, taskType: found.source } : null;
}
+async function uniqueTaskId(source, id, tasks) {
+ const taken = async (candidate) => tasks.some((task) => task.id === candidate) || Boolean(await getStoredTask(candidate, source));
+ if (!await taken(id)) return id;
+ for (let suffix = 2; ; suffix++) {
+ const candidate = `${id}-dup${suffix}`;
+ if (!await taken(candidate)) return candidate;
+ }
+}
+
/**
* Add a new task to the user or internal queue.
*
@@ -552,23 +491,13 @@ export async function addTask(taskData, taskType = 'user', { raw = false, ignore
validateRequest(cosTaskTypeSchema, taskType);
if (!raw && taskData.metadata) taskData = { ...taskData, metadata: newAuditMetadata(taskData.metadata) };
return withStateLock(async () => {
- const state = await loadState();
- const filePath = taskType === 'user'
- ? join(ROOT_DIR, state.config.userTasksFile)
- : join(ROOT_DIR, state.config.cosTasksFile);
-
- // Read existing tasks or start fresh
- let tasks = [];
- if (existsSync(filePath)) {
- tasks = await readTaskFile(filePath);
- }
+ const tasks = await readTaskFile(taskType);
- // One state lock covers BOTH task files: manual and autonomous producers
+ // One state lock covers BOTH queues: manual and autonomous producers
// must acquire the same durable work identity even with different titles.
const newWorkIdentity = developmentWorkIdentity(taskData);
if (newWorkIdentity) {
- const otherPath = join(ROOT_DIR, taskType === 'user' ? state.config.cosTasksFile : state.config.userTasksFile);
- const other = existsSync(otherPath) ? await readTaskFile(otherPath) : [];
+ const other = await readTaskFile(taskType === 'user' ? 'internal' : 'user');
const owner = [...tasks, ...other].find(task => task.id !== ignoreTaskId
&& DEVELOPMENT_ACTIVE_STATUSES.has(task.status) && sameDevelopmentWork(task, taskData)
// Targeted claims and the backlog drain coexist in either admission order.
@@ -637,6 +566,14 @@ export async function addTask(taskData, taskType = 'user', { raw = false, ignore
console.error(`❌ Task id ${newTask.id} uses an unregistered prefix — it will be read back as task-${newTask.id}. Register the prefix in lib/taskParser.js`);
}
+ // Ids are the row key and are minted from the clock, so two tasks created in the
+ // same millisecond collide. Suffix `-dup2`, `-dup3`, ... as the Markdown reader
+ // always did, but checked against every stored task, not just the working set.
+ if (newTask?.id) {
+ const id = await uniqueTaskId(taskType, newTask.id, tasks);
+ if (id !== newTask.id) newTask = { ...newTask, id };
+ }
+
// Route a multi-line context payload to `metadata.prompt` (#4153). Applied to
// BOTH branches — the raw path is how the generator, the reference-watch
// analysis, and the repo-study filer queue their pre-built tasks, and they
@@ -679,10 +616,7 @@ export async function addTask(taskData, taskType = 'user', { raw = false, ignore
tasks.push(newTask);
}
- // Write back to file
- const includeApprovalFlags = taskType === 'internal';
- const markdown = generateTasksMarkdown(tasks, includeApprovalFlags);
- await writeTaskFile(filePath, markdown);
+ await writeTaskList(taskType, tasks);
// cos.js init listens for this event. For user tasks it fires
// tryImmediateSpawn so the task starts instantly if slots are available,
@@ -833,33 +767,20 @@ async function writeTaskUpdate(taskId, updates, taskType, { now, suppressDequeue
// Caller holds withStateLock, including its status and ownership decision.
async function writeTaskUpdateLocked(taskId, updates, taskType, { now, suppressDequeue = false, expectedStatus = null, expectedMetadata = null }) {
- const state = await loadState();
- const filePath = taskType === 'user'
- ? join(ROOT_DIR, state.config.userTasksFile)
- : join(ROOT_DIR, state.config.cosTasksFile);
-
- if (!existsSync(filePath)) {
- console.log(`⚠️ updateTask: file not found for ${taskId} (taskType=${taskType}, path=${filePath})`);
- return { error: 'Task file not found' };
- }
-
- let tasks = await readTaskFile(filePath);
+ const tasks = await readTaskFile(taskType);
let taskIndex = tasks.findIndex(t => t.id === taskId);
- // A history item the archive sweep moved out of the file can still be edited or
- // reopened: pull it back, apply the update, and drop the archive row only once
- // the file write lands. An early return below leaves the archive untouched.
- let restoredFromArchive = false;
+ // A history item older than the working set can still be edited or reopened:
+ // fetch it by id into the list being saved; the save upserts its row in place.
if (taskIndex === -1) {
- const archived = await getArchivedTask(taskId, taskType);
- if (archived) {
- tasks.push(archived.task);
+ const stored = await getStoredTask(taskId, taskType);
+ if (stored) {
+ tasks.push(stored.task);
taskIndex = tasks.length - 1;
- restoredFromArchive = true;
}
}
if (taskIndex === -1) {
- console.log(`⚠️ updateTask: task ${taskId} not found in ${filePath} (taskType=${taskType}, parsed ${tasks.length} tasks, status update: ${updates.status || 'none'})`);
+ console.log(`⚠️ updateTask: task ${taskId} not found in the ${taskType} queue (working set of ${tasks.length} tasks, status update: ${updates.status || 'none'})`);
return { error: 'Task not found' };
}
@@ -989,11 +910,7 @@ async function writeTaskUpdateLocked(taskId, updates, taskType, { now, suppressD
tasks[taskIndex] = updatedTask;
- // Write back to file
- const includeApprovalFlags = taskType === 'internal';
- const markdown = generateTasksMarkdown(tasks, includeApprovalFlags);
- await writeTaskFile(filePath, markdown);
- if (restoredFromArchive) await deleteArchivedTask(taskId, taskType);
+ await writeTaskList(taskType, tasks);
// `transition.action` distinguishes the two flips that must WAKE the scheduler
// from an ordinary edit. A blocked → pending flip is a revive: the task is newly
@@ -1074,37 +991,26 @@ export async function reviveBlockedTask(taskId, { priority, metadata } = {}, tas
export async function mergePeerTasks(taskType, remoteTasks, { now = Date.now() } = {}) {
validateRequest(cosTaskTypeSchema, taskType);
const { changed, count, merged } = await withStateLock(async () => {
- const state = await loadState();
- const filePath = taskType === 'user'
- ? join(ROOT_DIR, state.config.userTasksFile)
- : join(ROOT_DIR, state.config.cosTasksFile);
-
- const localTasks = existsSync(filePath)
- ? await readTaskFile(filePath)
- : [];
+ const localTasks = await readTaskFile(taskType);
- // A peer's file still holds completed tasks this machine has archived; merging
- // them back would resurrect history the sweep already moved out of the file.
+ // A peer still advertises completed tasks this machine already holds in
+ // history; merging them in would pull them back into the working set.
const remoteCompletedIds = remoteTasks.filter((task) => task?.status === 'completed').map((task) => task.id);
- const archivedIds = await getArchivedIds(taskType, remoteCompletedIds);
- const incoming = archivedIds.size === 0
+ const knownCompleted = await getCompletedTaskIds(taskType, remoteCompletedIds);
+ const incoming = knownCompleted.size === 0
? remoteTasks
- : remoteTasks.filter((task) => !(task?.status === 'completed' && archivedIds.has(task.id)));
+ : remoteTasks.filter((task) => !(task?.status === 'completed' && knownCompleted.has(task.id)));
const merged = mergeTaskLists(localTasks, incoming, { now });
- const includeApprovalFlags = taskType === 'internal';
- const localMarkdown = generateTasksMarkdown(localTasks, includeApprovalFlags);
- const mergedMarkdown = generateTasksMarkdown(merged, includeApprovalFlags);
// Nothing the peer sent changed our state — skip the write (and the event that would
// wake the scheduler). `merged` is still returned so the post-lock LI-verdict consume
// (#2779) can run even on a no-op sweep — durability requires it (an old peer that
// stored a terminal verdict before upgrading, or a crash before a prior consume, leaves
// the task terminal with the merge producing no change, so a changed-only consume would
// never catch up). Re-offering is cheap: the consumer is a durable no-op after the first.
- if (mergedMarkdown === localMarkdown) return { changed: false, merged };
+ if (!await writeTaskList(taskType, merged)) return { changed: false, merged };
- await writeTaskFile(filePath, mergedMarkdown);
cosEvents.emit('tasks:changed', { type: taskType, action: 'peer-merged' });
return { changed: true, count: merged.length, merged };
});
@@ -1136,30 +1042,11 @@ export async function mergePeerTasks(taskType, remoteTasks, { now = Date.now() }
export async function deleteTask(taskId, taskType = 'user') {
validateRequest(cosTaskTypeSchema, taskType);
return withStateLock(async () => {
- const state = await loadState();
- const filePath = taskType === 'user'
- ? join(ROOT_DIR, state.config.userTasksFile)
- : join(ROOT_DIR, state.config.cosTasksFile);
-
- let deletedFromFile = false;
- if (existsSync(filePath)) {
- const tasks = await readTaskFile(filePath);
- if (tasks.some(t => t.id === taskId)) {
- const includeApprovalFlags = taskType === 'internal';
- await writeTaskFile(filePath, generateTasksMarkdown(tasks.filter(t => t.id !== taskId), includeApprovalFlags));
- deletedFromFile = true;
- }
- }
-
- // The archive copy goes too: during the sweep's archive-then-rewrite window a
- // task lives in both stores, and a file-only delete would leave it in history.
- const deletedFromArchive = await deleteArchivedTask(taskId, taskType);
- if (!deletedFromFile && !deletedFromArchive) {
- return { error: existsSync(filePath) ? 'Task not found' : 'Task file not found' };
- }
-
- cosEvents.emit('tasks:changed', { type: taskType, action: 'deleted', taskId });
- return { success: true, taskId };
+ // One row delete, whether the task is in the working set or old history. It
+ // bumps the queue version, which is what makes the next read reload.
+ if (await deleteStoredTask(taskId, taskType) === 0) return { error: 'Task not found' };
+ cosEvents.emit('tasks:changed', { type: taskType, action: 'deleted', taskId });
+ return { success: true, taskId };
});
}
@@ -1172,9 +1059,7 @@ export async function deleteTask(taskId, taskType = 'user') {
export async function reconcileLegacyForgeMaintenanceTasks({ instanceId, now = Date.now() } = {}) {
return withStateLock(async () => {
const state = await loadState();
- const filePath = join(ROOT_DIR, state.config.cosTasksFile);
- if (!existsSync(filePath)) return { recovered: 0, retired: 0 };
- const tasks = await readTaskFile(filePath);
+ const tasks = await readTaskFile('internal');
let recovered = 0;
let retired = 0;
const resetApps = new Set();
@@ -1364,83 +1249,15 @@ export async function sweepResolvedFailureTasks({
return { reaped, staleBlocks, investigations };
}
-// ── Completed-task archive ──────────────────────────────────────────────────
-//
-// TASKS.md / COS-TASKS.md grew to tens of MB because every finished task keeps
-// its full prompt, and every full-queue read clones all of it. Completed tasks
-// past the recent window move to `cos_task_archive`; the files keep the open
-// queue plus that window, which is what dedup, retry bookkeeping and the PR
-// reviewer-failure lookup actually consult.
-const ARCHIVE_AFTER_MS = 3 * 24 * 60 * 60 * 1000;
-const ARCHIVE_KEEP_NEWEST = 100;
-// Bounds how long one sweep holds the state lock; a first run over a large
-// backlog drains across several ticks.
-const ARCHIVE_SWEEP_LIMIT = 300;
-
-// Undated tasks sort as oldest: they predate the stamps current writers add.
-const completionStamp = (task) => {
- const stamp = Date.parse(task.metadata?.completedAt || task.metadata?.updatedAt || '');
- return Number.isFinite(stamp) ? stamp : 0;
-};
-
-/** Completed tasks old enough, and beyond the newest few, to leave the file. Oldest first. */
-function selectArchivable(tasks, { now, afterMs, keepNewest, limit }) {
- const completed = tasks.filter(isCompleted).sort((a, b) => completionStamp(b) - completionStamp(a) || byIdDesc(a, b));
- return completed
- .slice(keepNewest)
- .filter((task) => completionStamp(task) <= now - afterMs)
- .reverse()
- .slice(0, limit);
-}
-
-/**
- * Move aged completed tasks out of the task files into the archive. The archive
- * write lands first and is idempotent, so a crash before the file rewrite leaves
- * a harmless duplicate that the next sweep removes. Runs under the same lock as
- * every task write.
- */
-export async function archiveCompletedTasks({
- now = Date.now(),
- afterMs = ARCHIVE_AFTER_MS,
- keepNewest = ARCHIVE_KEEP_NEWEST,
- limit = ARCHIVE_SWEEP_LIMIT,
-} = {}) {
- let archived = 0;
- for (const type of ['user', 'internal']) {
- archived += await withStateLock(async () => {
- const filePath = await resolveSourceFile(type);
- if (!filePath) return 0;
- const snapshot = await readTaskSnapshot(filePath);
- const selected = selectArchivable(snapshot.tasks, { now, afterMs, keepNewest, limit });
- if (selected.length === 0) return 0;
- await archiveTasks(type, structuredClone(selected));
- const leaving = new Set(selected.map((task) => task.id));
- await writeTaskFile(filePath, generateTasksMarkdown(snapshot.tasks.filter((task) => !leaving.has(task.id)), type === 'internal'));
- cosEvents.emit('tasks:changed', { type, action: 'archived' });
- return selected.length;
- });
- }
- if (archived > 0) console.log(`🗄️ Archived ${archived} completed task(s) out of the task files`);
- return { archived };
-}
-
/**
* Reorder user tasks based on an array of task IDs
*/
export async function reorderTasks(taskIds) {
return withStateLock(async () => {
- const state = await loadState();
- const filePath = join(ROOT_DIR, state.config.userTasksFile);
+ const tasks = await readTaskFile('user');
- if (!existsSync(filePath)) {
- return { error: 'Task file not found' };
- }
-
- const tasks = await readTaskFile(filePath);
-
- // Create a map of tasks by ID for quick lookup. parseTasksMarkdown guarantees
- // unique ids (it suffixes any duplicate it encounters), so this Map can't
- // silently collapse colliding tasks and drop them on write-back.
+ // Create a map of tasks by ID for quick lookup. Ids are the row key, so this
+ // Map can't silently collapse colliding tasks and drop them on write-back.
const taskMap = new Map(tasks.map(t => [t.id, t]));
// Reorder based on the provided order
@@ -1458,9 +1275,7 @@ export async function reorderTasks(taskIds) {
reorderedTasks.push(task);
}
- // Write back to file
- const markdown = generateTasksMarkdown(reorderedTasks, false);
- await writeTaskFile(filePath, markdown);
+ await writeTaskList('user', reorderedTasks);
cosEvents.emit('tasks:changed', { type: 'user', action: 'reordered' });
return { success: true, order: reorderedTasks.map(t => t.id) };
@@ -1476,28 +1291,17 @@ export async function reorderTasks(taskIds) {
*/
export async function approveTask(taskId, { now = Date.now() } = {}) {
return withStateLock(async () => {
- const state = await loadState();
-
// Both queues, internal first. A USER row can hold too: a row the parser had
// to RECOVER is withheld from the unattended spawn and now persists that hold
// as `| APPROVAL |` (#7300, #7367). Without a release path here that row is a
// zombie — pending forever, with nothing in the product able to clear it —
// which is a worse outcome than the evaporating hold it replaced.
- const queues = [
- { taskType: 'internal', filePath: join(ROOT_DIR, state.config.cosTasksFile) },
- { taskType: 'user', filePath: join(ROOT_DIR, state.config.userTasksFile) },
- ].filter(({ filePath }) => existsSync(filePath));
-
- if (queues.length === 0) {
- return { error: 'CoS task file not found' };
- }
-
let found = null;
- for (const queue of queues) {
- const tasks = await readTaskFile(queue.filePath);
+ for (const taskType of ['internal', 'user']) {
+ const tasks = await readTaskFile(taskType);
const taskIndex = tasks.findIndex(t => t.id === taskId);
if (taskIndex !== -1) {
- found = { ...queue, tasks, taskIndex };
+ found = { taskType, tasks, taskIndex };
break;
}
}
@@ -1506,7 +1310,7 @@ export async function approveTask(taskId, { now = Date.now() } = {}) {
return { error: 'Task not found' };
}
- const { taskType, filePath, tasks, taskIndex } = found;
+ const { taskType, tasks, taskIndex } = found;
if (!tasks[taskIndex].approvalRequired) {
return { error: 'Task does not require approval' };
@@ -1523,9 +1327,7 @@ export async function approveTask(taskId, { now = Date.now() } = {}) {
metadata: { ...tasks[taskIndex].metadata, updatedAt: new Date(now).toISOString() }
};
- // Write back to file
- const markdown = generateTasksMarkdown(tasks, taskType === 'internal');
- await writeTaskFile(filePath, markdown);
+ await writeTaskList(taskType, tasks);
cosEvents.emit('tasks:changed', { type: taskType, action: 'approved', task: tasks[taskIndex] });
diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.js
index 08ebf042a2..f01ceb2de0 100644
--- a/server/services/cosTaskStore.test.js
+++ b/server/services/cosTaskStore.test.js
@@ -14,8 +14,9 @@ import { readFileSync as realReadFileSync } from 'node:fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { removeByMetadata } from './notifications.js';
-import { readFile } from 'fs/promises';
-import { atomicWrite } from '../lib/fileUtils.js';
+import * as repository from './cosTaskRepository.js';
+const actualRepository = await vi.importActual('./cosTaskRepository.js');
+import { planListSave } from '../lib/cosTaskRows.js';
vi.mock('./prWatcher.js', () => ({ persistPrWatcherState: vi.fn(async () => ({})) }));
import { persistPrWatcherState } from './prWatcher.js';
@@ -24,15 +25,12 @@ import { LEGACY_FORGE_MAINTENANCE_REASON } from '../lib/forgeMaintenanceTasks.js
vi.mock('./notifications.js', () => ({ removeByMetadata: vi.fn().mockResolvedValue(1) }));
const mock = vi.hoisted(() => ({
- files: new Map(),
- // Mocked mtimes for the parsed-task cache (#3497). Each mocked write bumps the
- // path's stamp; a test that wants to simulate an EXTERNAL edit sets it itself.
- mtimes: new Map(),
- // Counts real parses so the cache tests can prove a hit did no work.
- parseCalls: 0,
- // When set, atomicWrite rejects with it (a write that dies before the rename).
- atomicFailure: null,
+ // When set, the next repository write rejects with it (a save that dies before commit).
+ writeFailure: null,
+ // Called with the source after each committed write; a truthy return clears it.
afterWrite: null,
+ // Counts real working-set loads so the cache tests can prove a hit did no work.
+ loads: 0,
state: null,
events: [],
// Controls the mocked codeReview.js for resolveTaskChallengeWithRecheck (#2471).
@@ -40,64 +38,28 @@ const mock = vi.hoisted(() => ({
reviewDefaults: { lmstudioModel: 'default-lmstudio', ollamaModel: 'default-ollama' },
reviewCalls: [],
// Captures cross-peer LI execution verdicts consumed by mergePeerTasks (#2779).
- liVerdicts: [],
- // In-memory stand-in for the cos_task_archive table, per source.
- archive: { user: new Map(), internal: new Map() }
+ liVerdicts: []
}));
-// existsSync is driven by the in-memory file map; readFileSync stays real so
-// the source-level regression guards below can read cosTaskStore.js off disk.
-vi.mock('fs', async (importOriginal) => {
+// The real in-memory repository, wrapped so tests can count loads, fail a save,
+// or run a hook right after a committed write (the old file-write seams).
+vi.mock('./cosTaskRepository.js', async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
- existsSync: (p) => mock.files.has(p)
- };
-});
-
-// Task files are written through atomicWrite (temp file + rename). The mock
-// applies the same in-memory write the fs/promises writeFile mock used to, and
-// `mock.atomicFailure` lets a test simulate the write dying before the rename.
-vi.mock('../lib/fileUtils.js', async (importOriginal) => {
- const actual = await importOriginal();
- return {
- ...actual,
- atomicWrite: vi.fn(async (p, content) => {
- if (mock.atomicFailure) throw mock.atomicFailure;
- mock.files.set(p, content);
- mock.mtimes.set(p, (mock.mtimes.get(p) || 0) + 1000);
- // Lets a test mutate the file between two reaper flips (snapshot race).
- if (mock.afterWrite && mock.afterWrite(p)) mock.afterWrite = null;
- })
- };
-});
-
-vi.mock('fs/promises', () => ({
- readFile: vi.fn(async (p) => {
- if (!mock.files.has(p)) throw new Error(`ENOENT: ${p}`);
- return mock.files.get(p);
- }),
- // The parsed-task cache stamps on mtimeMs + size, so the mock must model both.
- stat: vi.fn(async (p) => {
- if (!mock.files.has(p)) throw new Error(`ENOENT: ${p}`);
- return { mtimeMs: mock.mtimes.get(p) || 0, size: mock.files.get(p).length };
- })
-}));
-
-// Counts parseTasksMarkdown calls (cache-hit assertions) while keeping every
-// other taskParser export real — the store's whole read path depends on them.
-vi.mock('../lib/taskParser.js', async (importOriginal) => {
- const actual = await importOriginal();
- return {
- ...actual,
- parseTasksMarkdown: (content) => { mock.parseCalls += 1; return actual.parseTasksMarkdown(content); }
+ loadTaskWorkingSet: vi.fn(async (...args) => { mock.loads += 1; return actual.loadTaskWorkingSet(...args); }),
+ applyTaskListSave: vi.fn(async (source, plan) => {
+ if (mock.writeFailure) throw mock.writeFailure;
+ const version = await actual.applyTaskListSave(source, plan);
+ if (mock.afterWrite && await mock.afterWrite(source)) mock.afterWrite = null;
+ return version;
+ }),
};
});
vi.mock('./cosState.js', async () => ({
loadState: vi.fn(async () => mock.state),
withStateLock: (await import('../lib/fileWriteQueue.js')).createFileWriteQueue(),
- ROOT_DIR: '/root'
}));
vi.mock('./cos.js', () => ({
@@ -107,34 +69,6 @@ vi.mock('./cos.js', () => ({
updateTask: (...args) => updateTask(...args),
}));
-// The archive adapter is a PostgreSQL/JSON store; this suite mocks the filesystem
-// wholesale, so it gets an in-memory table with the same ordering and `before`
-// cursor semantics as the real adapter.
-vi.mock('./cosTaskArchive.js', () => {
- const sources = ['user', 'internal'];
- const byIdDesc = (a, b) => (a.id < b.id ? 1 : a.id > b.id ? -1 : 0);
- return {
- archiveTasks: vi.fn(async (source, tasks) => { for (const task of tasks) mock.archive[source].set(task.id, task); }),
- getArchivedTask: vi.fn(async (taskId, source = null) => {
- for (const candidate of source ? [source] : sources) {
- if (mock.archive[candidate].has(taskId)) return { source: candidate, task: structuredClone(mock.archive[candidate].get(taskId)) };
- }
- return null;
- }),
- getArchivedIds: vi.fn(async (source, ids) => new Set(ids.filter((id) => mock.archive[source].has(id)))),
- listArchivedTasks: vi.fn(async (source, { before = null, offset = 0, limit = 25 } = {}) => [...mock.archive[source].values()]
- .filter((task) => before == null || task.id < before).sort(byIdDesc).slice(offset, offset + limit).map((task) => structuredClone(task))),
- countArchivedTasks: vi.fn(async (source) => mock.archive[source].size),
- listArchivedSummaries: vi.fn(async () => sources.flatMap((source) => [...mock.archive[source].values()]
- .map((task) => ({ source, id: task.id, description: task.description || '' })))),
- deleteArchivedTask: vi.fn(async (taskId, source = null) => {
- let removed = false;
- for (const candidate of source ? [source] : sources) removed = mock.archive[candidate].delete(taskId) || removed;
- return removed;
- }),
- };
-});
-
vi.mock('./cosEvents.js', () => ({
emitLog: vi.fn(),
cosEvents: { emit: (name, payload) => mock.events.push({ name, payload }) }
@@ -179,7 +113,6 @@ import {
isReapableBlockedFailure,
isReapableInvestigation,
sweepResolvedFailureTasks,
- archiveCompletedTasks,
findTaskWithSource,
reconcileLegacyForgeMaintenanceTasks,
DEFAULT_FAILURE_TASK_MAX_AGE_MS,
@@ -191,22 +124,25 @@ import { getTaskPrompt, getTaskContextNote } from '../lib/cosTaskPrompt.js';
import { AGENT_PAUSED_CATEGORY, PAUSE_METADATA_KEYS, registerPauseReleaseAdapter, __resetPauseReleaseAdapter } from '../lib/taskPauseHold.js';
import { MAX_TOTAL_SPAWNS } from '../lib/cosValidation.js';
-const USER_FILE = join('/root', 'TASKS.md');
-const COS_FILE = join('/root', 'COS-TASKS.md');
+const baseState = () => ({ agents: {} });
-const baseState = () => ({
- config: { userTasksFile: 'TASKS.md', cosTasksFile: 'COS-TASKS.md' }
-});
+// Test-side access to what is stored, bypassing the store's cache and the
+// failure/after-write hooks above (they wrap only the store's own saves).
+const seed = async (source, tasks) => {
+ const { rows } = await actualRepository.loadTaskWorkingSet(source);
+ const plan = planListSave({ previous: rows, next: tasks, source });
+ await actualRepository.applyTaskListSave(source, plan);
+ __resetTaskCache();
+};
+const stored = async (source) => (await actualRepository.loadTaskWorkingSet(source)).rows.map((row) => row.task);
beforeEach(() => {
- mock.atomicFailure = null;
+ mock.writeFailure = null;
mock.afterWrite = null;
- mock.files = new Map();
- mock.mtimes = new Map();
- mock.archive = { user: new Map(), internal: new Map() };
- mock.parseCalls = 0;
- // The parse cache is module-level state; a leftover entry from the previous
- // test would answer for a path this test just re-created from scratch.
+ mock.loads = 0;
+ // Both the repository's backend and the store's cache are module-level state;
+ // a leftover entry from the previous test would answer for tasks it never created.
+ actualRepository.__resetCosTaskRepository();
__resetTaskCache();
mock.state = baseState();
mock.events = [];
@@ -329,18 +265,15 @@ describe('cosTaskStore.updateTask expectedStatus', () => {
});
describe('cosTaskStore.getUserTasks / getCosTasks', () => {
- it('returns an empty, non-existent result when the file is missing', async () => {
+ it('returns an empty result for an empty queue', async () => {
const result = await getUserTasks();
- expect(result.exists).toBe(false);
expect(result.tasks).toEqual([]);
expect(result.type).toBe('user');
- expect(result.file).toBe(USER_FILE);
});
- it('parses an existing user task file', async () => {
+ it('returns an added user task in stored form', async () => {
await addTask({ description: 'do a thing', priority: 'HIGH' }, 'user');
const result = await getUserTasks();
- expect(result.exists).toBe(true);
expect(result.tasks).toHaveLength(1);
expect(result.tasks[0].description).toBe('do a thing');
expect(result.tasks[0].priority).toBe('HIGH');
@@ -356,24 +289,26 @@ describe('cosTaskStore.getUserTasks / getCosTasks', () => {
});
});
-// Parsed-task cache (#3497). The daemon's evaluation tick re-reads both task
-// files many times a minute; these pin that an unchanged file is parsed once,
-// that every invalidation signal fires, and that a cached read can never leak a
+// Working-set cache (#3497). The daemon's evaluation tick re-reads both queues
+// many times a minute; these pin that an unchanged queue is loaded once, that
+// every invalidation signal fires, and that a cached read can never leak a
// caller's in-place mutations into the next reader.
-describe('cosTaskStore parsed-task cache (#3497)', () => {
- it('reads telemetry without cloning prompts and keeps metrics fresh after task writes and external edits', async () => {
+describe('cosTaskStore working-set cache (#3497)', () => {
+ const gate = () => Promise.withResolvers();
+ const task = (n, extra = {}) => ({ id: `task-example-${String(n).padStart(4, '0')}`, description: `Example task ${n}`, status: 'pending', priority: 'MEDIUM', priorityValue: 2, approvalRequired: false, autoApproved: true, metadata: {}, ...extra });
+
+ it('reads telemetry without cloning prompts and keeps metrics fresh after task writes and out-of-band changes', async () => {
const now = Date.parse('2026-07-09T12:00:00Z');
expect(await getTaskDiagnostics()).toEqual([]);
- const tasks = Array.from({ length: 300 }, (_, index) => ({
- id: `task-${index}`, description: `Example task ${index}`, status: 'pending', priority: 'MEDIUM',
+ const tasks = Array.from({ length: 300 }, (_, index) => task(index, {
metadata: {
prompt: 'Example prompt. '.repeat(512),
updatedAt: '2026-07-09T11:00:00Z',
...(index % 30 === 0 ? { diagnostics: { tier: 1, category: 'config', observedAt: '2026-07-09T10:00:00Z' } } : {})
}
}));
- mock.files.set(USER_FILE, generateTasksMarkdown(tasks.slice(0, 150)));
- mock.files.set(COS_FILE, generateTasksMarkdown(tasks.slice(150)));
+ await seed('user', tasks.slice(0, 150));
+ await seed('internal', tasks.slice(150));
const { user, cos } = await getAllTasks();
const expected = aggregateAutoFixDiagnostics([...user.tasks, ...cos.tasks], { now });
const clone = vi.spyOn(globalThis, 'structuredClone');
@@ -381,25 +316,24 @@ describe('cosTaskStore parsed-task cache (#3497)', () => {
const projectedBytes = JSON.stringify(clone.mock.calls[0][0]).length;
expect(projectedBytes).toBeLessThan(JSON.stringify([...user.tasks, ...cos.tasks]).length / 100);
expect(clone.mock.calls[0][0]).toHaveLength(10);
- const parses = mock.parseCalls;
+ const loads = mock.loads;
const projection = await getTaskDiagnostics();
projection[0].metadata.diagnostics.tier = 99;
expect(await getAutoFixMetrics({ now })).toEqual(expected);
- expect(mock.parseCalls).toBe(parses);
+ expect(mock.loads).toBe(loads);
clone.mockRestore();
- await updateTask('task-0', { status: 'completed' }, 'user');
+ await updateTask('task-example-0000', { status: 'completed' }, 'user');
expect((await getAutoFixMetrics({ now })).overall.resolved).toBe(1);
- mock.files.set(COS_FILE, mock.files.get(COS_FILE).replace('- [ ]', '- [x]'));
- mock.mtimes.set(COS_FILE, 5000);
+ await seed('internal', (await stored('internal')).map((t, i) => (i === 0 ? { ...t, status: 'completed' } : t)));
expect((await getAutoFixMetrics({ now })).overall.resolved).toBe(2);
- mock.files.delete(USER_FILE);
+ await seed('user', []);
expect((await getAutoFixMetrics({ now })).total).toBe(5);
- mock.files.set(COS_FILE, '');
+ await seed('internal', []);
expect((await getAutoFixMetrics({ now })).total).toBe(0);
});
- it('projects pending IDs without cloning task payloads and refreshes after writes and external edits', async () => {
+ it('projects pending IDs without cloning task payloads and refreshes after writes and out-of-band changes', async () => {
expect(await getPendingTaskIds()).toEqual([]);
const user = await addTask({ description: 'Example user task' }, 'user');
const internal = await addTask({ description: 'Example internal task' }, 'internal');
@@ -407,18 +341,17 @@ describe('cosTaskStore parsed-task cache (#3497)', () => {
const ids = await getPendingTaskIds();
expect(ids).toEqual([user.id, internal.id]);
ids.push('caller-only');
- const parses = mock.parseCalls;
+ const loads = mock.loads;
expect(await getPendingTaskIds()).toEqual([user.id, internal.id]);
- expect(mock.parseCalls).toBe(parses);
+ expect(mock.loads).toBe(loads);
expect(clone).not.toHaveBeenCalled();
clone.mockRestore();
await updateTask(user.id, { status: 'completed' }, 'user');
expect(await getPendingTaskIds()).toEqual([internal.id]);
- mock.files.set(COS_FILE, mock.files.get(COS_FILE).replace('- [ ]', '- [x]'));
- mock.mtimes.set(COS_FILE, mock.mtimes.get(COS_FILE) + 5000);
+ await seed('internal', (await stored('internal')).map((t) => ({ ...t, status: 'completed' })));
expect(await getPendingTaskIds()).toEqual([]);
- mock.files.delete(USER_FILE);
+ await seed('user', []);
expect(await getPendingTaskIds()).toEqual([]);
});
@@ -445,68 +378,76 @@ describe('cosTaskStore parsed-task cache (#3497)', () => {
expect((await getCosApprovalView({ limit: 10 })).awaitingApproval).toHaveLength(3);
});
- it('serves a cached parse while the file is unchanged', async () => {
+ it('serves a cached working set while the queue version is unchanged', async () => {
await addTask({ description: 'cached thing' }, 'user');
- mock.parseCalls = 0;
+ __resetTaskCache();
+ mock.loads = 0;
const first = await getUserTasks();
- expect(mock.parseCalls).toBe(1);
+ expect(mock.loads).toBe(1);
const second = await getUserTasks();
- expect(mock.parseCalls).toBe(1); // no re-parse — the file did not move
+ expect(mock.loads).toBe(1); // no reload — the queue did not change
expect(second.tasks).toEqual(first.tasks);
});
- it('coalesces concurrent task views after cold and externally changed files', async () => {
+ it('keeps its cache current across the store\'s own writes without reloading', async () => {
+ await addTask({ description: 'original' }, 'user');
+ const created = (await getUserTasks()).tasks[0];
+ mock.loads = 0;
+
+ await updateTask(created.id, { description: 'rewritten' }, 'user');
+ await addTask({ description: 'second' }, 'user');
+ expect((await getUserTasks()).tasks.map((t) => t.description).sort()).toEqual(['rewritten', 'second']);
+ expect(mock.loads).toBe(0);
+ });
+
+ it('coalesces concurrent task views after cold and out-of-band changed queues', async () => {
await addTask({ description: 'user task' }, 'user');
await addTask({ description: 'internal task' }, 'internal');
__resetTaskCache();
- mock.parseCalls = 0;
- vi.mocked(readFile).mockClear();
+ mock.loads = 0;
const views = await Promise.all(Array.from({ length: 24 }, () => getTaskView({ kind: 'bare' })));
- expect(mock.parseCalls).toBe(2);
- expect(readFile).toHaveBeenCalledTimes(2);
+ expect(mock.loads).toBe(2);
views[0].user.tasks[0].description = 'caller mutation';
expect(views[1].user.tasks[0].description).toBe('user task');
- mock.files.set(USER_FILE, mock.files.get(USER_FILE).replace('user task', 'edited task'));
- mock.mtimes.set(USER_FILE, mock.mtimes.get(USER_FILE) + 1000);
+ await seed('user', (await stored('user')).map((t) => ({ ...t, description: 'edited task' })));
+ mock.loads = 0;
const changed = await Promise.all(Array.from({ length: 24 }, () => getTaskView({ kind: 'bare' })));
- expect(mock.parseCalls).toBe(3);
- expect(readFile).toHaveBeenCalledTimes(3);
+ expect(mock.loads).toBe(2);
expect(changed.every(view => view.user.tasks[0].description === 'edited task')).toBe(true);
});
- it('retries shared read failures on the next task view request', async () => {
+ it('retries shared load failures on the next task view request', async () => {
await addTask({ description: 'readable again' }, 'user');
__resetTaskCache();
- vi.mocked(readFile).mockClear().mockRejectedValueOnce(new Error('temporary read failure'));
+ vi.mocked(repository.loadTaskWorkingSet).mockClear().mockRejectedValueOnce(new Error('temporary read failure'));
const attempts = await Promise.allSettled(Array.from({ length: 8 }, () => getUserTasks()));
expect(attempts.every(result => result.status === 'rejected' && result.reason.message === 'temporary read failure')).toBe(true);
- expect(readFile).toHaveBeenCalledTimes(1);
+ expect(repository.loadTaskWorkingSet).toHaveBeenCalledTimes(1);
expect((await getUserTasks()).tasks[0].description).toBe('readable again');
- expect(readFile).toHaveBeenCalledTimes(2);
+ expect(repository.loadTaskWorkingSet).toHaveBeenCalledTimes(2);
});
- it('preserves an external edit when an update starts after an older read captured the file', async () => {
+ it('preserves an out-of-band change when an update starts after an older read captured the queue', async () => {
const created = await addTask({ description: 'original', metadata: { context: 'before edit' } }, 'user');
__resetTaskCache();
- const oldContent = mock.files.get(USER_FILE);
- const reading = Promise.withResolvers();
- const finishRead = Promise.withResolvers();
- vi.mocked(readFile).mockImplementationOnce(async () => {
+ const older = await actualRepository.loadTaskWorkingSet('user');
+ const reading = gate();
+ const finishRead = gate();
+ vi.mocked(repository.loadTaskWorkingSet).mockImplementationOnce(async () => {
reading.resolve();
await finishRead.promise;
- return oldContent;
+ return older;
});
const olderRead = getUserTasks();
await reading.promise;
- mock.files.set(USER_FILE, oldContent.replace('before edit', 'external edit'));
- mock.mtimes.set(USER_FILE, mock.mtimes.get(USER_FILE) + 1000);
+ await seed('user', older.rows.map((row) => ({ ...row.task, metadata: { ...row.task.metadata, context: 'external edit' } })));
const update = updateTask(created.id, { description: 'API edit' }, 'user');
- // Releasing the old read must not make a newer update overwrite the editor.
+ // Releasing the old read must not make a newer update overwrite the other writer.
finishRead.resolve();
expect((await olderRead).tasks[0].metadata.context).toBe('before edit');
expect((await update).metadata.context).toBe('external edit');
@@ -518,83 +459,50 @@ describe('cosTaskStore parsed-task cache (#3497)', () => {
it('keeps a read overlapping a write from capturing or poisoning post-write readers', async () => {
const created = await addTask({ description: 'original' }, 'user');
await getUserTasks();
- const originalContent = mock.files.get(USER_FILE);
- const originalMtime = mock.mtimes.get(USER_FILE);
- const writing = Promise.withResolvers();
- const finishWrite = Promise.withResolvers();
- const originalWrite = vi.mocked(atomicWrite).getMockImplementation();
- vi.mocked(atomicWrite).mockImplementationOnce(async (...args) => {
+ const before = await actualRepository.loadTaskWorkingSet('user');
+ const writing = gate();
+ const finishWrite = gate();
+ vi.mocked(repository.applyTaskListSave).mockImplementationOnce(async (source, plan) => {
writing.resolve();
await finishWrite.promise;
- await originalWrite(...args);
- // A length-neutral edit on a coarse-mtime filesystem has the same stamp.
- mock.files.set(USER_FILE, originalContent.replace('original', 'REWRITTN'));
- mock.mtimes.set(USER_FILE, originalMtime);
+ return actualRepository.applyTaskListSave(source, plan);
});
const write = updateTask(created.id, { description: 'REWRITTN' }, 'user');
await writing.promise;
- const reading = Promise.withResolvers();
- const finishRead = Promise.withResolvers();
- vi.mocked(readFile).mockImplementationOnce(async () => {
+ // A reader that misses the cache while the write is still uncommitted loads the old rows.
+ __resetTaskCache();
+ const reading = gate();
+ const finishRead = gate();
+ vi.mocked(repository.loadTaskWorkingSet).mockImplementationOnce(async () => {
reading.resolve();
await finishRead.promise;
- return originalContent;
+ return before;
});
const overlappingRead = getUserTasks();
await reading.promise;
finishWrite.resolve();
await write;
- // This must neither join the old flight nor consume its eventual snapshot.
+ // Post-write readers must neither join the old load nor consume its eventual snapshot.
expect((await getUserTasks()).tasks[0].description).toBe('REWRITTN');
finishRead.resolve();
expect((await overlappingRead).tasks[0].description).toBe('original');
expect((await getUserTasks()).tasks[0].description).toBe('REWRITTN');
});
- it('re-parses after an external edit bumps mtime', async () => {
+ it('reloads after an out-of-band write bumps the queue version', async () => {
await addTask({ description: 'first' }, 'user');
await getUserTasks();
- mock.parseCalls = 0;
+ mock.loads = 0;
- // Simulate an out-of-band write (user editing TASKS.md, a restore, a peer).
- mock.files.set(USER_FILE, mock.files.get(USER_FILE).replace('first', 'edited outside the store'));
- mock.mtimes.set(USER_FILE, mock.mtimes.get(USER_FILE) + 5000);
+ await seed('user', (await stored('user')).map((t) => ({ ...t, description: 'edited outside the store' })));
+ mock.loads = 0;
const after = await getUserTasks();
- expect(mock.parseCalls).toBe(1);
+ expect(mock.loads).toBe(1);
expect(after.tasks[0].description).toBe('edited outside the store');
});
- it('re-parses after a write through the store even when the stamp has not moved', async () => {
- await addTask({ description: 'original' }, 'user');
- const created = (await getUserTasks()).tasks[0]; // primes the cache
-
- // Snapshot the EXACT stamp the cache is now holding, so it can be restored
- // byte-for-byte after the write below.
- const cachedContent = mock.files.get(USER_FILE);
- const cachedMtime = mock.mtimes.get(USER_FILE);
-
- await updateTask(created.id, { description: 'original' }, 'user');
-
- // Put back content of the identical LENGTH ('original' and 'REWRITTN' are
- // both 8 chars) at the identical mtime, so `mtimeMs:size` is bit-for-bit
- // what the cached entry carries. The stamp check therefore cannot save this
- // read — only writeTaskFile having dropped the entry itself can. Restoring
- // a different-length string instead would move `size`, the stamp would
- // differ anyway, and this test would pass with the write-side invalidation
- // deleted. That matters because the length-neutral rewrite is the COMMON
- // production case (a status flip, an `updatedAt` restamp), and on a
- // coarse-mtime filesystem two writes inside one tick stamp identically.
- mock.files.set(USER_FILE, cachedContent.replace('original', 'REWRITTN'));
- mock.mtimes.set(USER_FILE, cachedMtime);
- mock.parseCalls = 0;
-
- const after = await getUserTasks();
- expect(mock.parseCalls).toBe(1);
- expect(after.tasks[0].description).toBe('REWRITTN');
- });
-
it('invalidates on deleteTask so the removed task never comes back cached', async () => {
const created = await addTask({ description: 'doomed' }, 'user');
expect((await getUserTasks()).tasks).toHaveLength(1);
@@ -645,15 +553,15 @@ describe('cosTaskStore.getTaskView (#9677)', () => {
id: `task-${type}-${String(n).padStart(4, '0')}`, description: `Example ${type} task ${n}`, status, priority: 'MEDIUM',
metadata: { prompt: status === 'completed' ? PROMPT : 'Open prompt.' }, ...extra
});
- const seed = (completedPerSource = 60) => {
- const archived = (type) => Array.from({ length: completedPerSource }, (_, n) => row(type, n, 'completed'));
- mock.files.set(USER_FILE, generateTasksMarkdown([
- row('user', 1000, 'pending'), ...archived('user'), row('user', 1001, 'in_progress'), row('user', 1002, 'blocked'),
- ]));
- mock.files.set(COS_FILE, generateTasksMarkdown([
- row('cos', 1000, 'pending', { approvalRequired: false, autoApproved: true }), ...archived('cos'),
+ const seedViews = async (completedPerSource = 60) => {
+ const completed = (type) => Array.from({ length: completedPerSource }, (_, n) => row(type, n, 'completed'));
+ await seed('user', [
+ row('user', 1000, 'pending'), ...completed('user'), row('user', 1001, 'in_progress'), row('user', 1002, 'blocked'),
+ ]);
+ await seed('internal', [
+ row('cos', 1000, 'pending', { approvalRequired: false, autoApproved: true }), ...completed('cos'),
row('cos', 1001, 'pending', { approvalRequired: true, autoApproved: false }),
- ]));
+ ]);
};
const desc = (a, b) => (a.id < b.id ? 1 : a.id > b.id ? -1 : 0);
const withSpy = async (fn) => {
@@ -665,8 +573,8 @@ describe('cosTaskStore.getTaskView (#9677)', () => {
} finally { clone.mockRestore(); }
};
- it('bare view equals the raw store minus archived rows beyond the first page, and clones only that page', async () => {
- seed();
+ it('bare view equals the raw store minus completed rows beyond the first page, and clones only the open queue', async () => {
+ await seedViews();
const raw = await getAllTasks();
const { result, cloned } = await withSpy(() => getTaskView({ kind: 'bare' }));
for (const key of ['user', 'cos']) {
@@ -679,14 +587,12 @@ describe('cosTaskStore.getTaskView (#9677)', () => {
...(key === 'cos' ? { autoApproved: all.autoApproved, awaitingApproval: all.awaitingApproval } : {}),
});
}
- const rawBytes = JSON.stringify(raw).length;
- // 2 sources x (4 open + 25 page) of 120 rows: well under half the raw payload.
- expect(cloned).toBeLessThan(rawBytes * 0.3);
- console.log(`bounded bare clone: ${cloned} of ${rawBytes} bytes`);
+ // The completed page is read straight from the table; only the few open rows are cloned.
+ expect(cloned).toBeLessThan(JSON.stringify(raw).length * 0.05);
});
- it('queue view clones no archived prompt unless one is selected, then exactly that row', async () => {
- seed();
+ it('queue view clones no completed prompt unless one is selected, then exactly that row', async () => {
+ await seedViews();
const raw = await getAllTasks();
const queue = await withSpy(() => getTaskView({ kind: 'queue' }));
expect(queue.result.user.tasks.map(t => t.id)).toEqual(['task-user-1000', 'task-user-1001', 'task-user-1002']);
@@ -697,21 +603,21 @@ describe('cosTaskStore.getTaskView (#9677)', () => {
expect(JSON.stringify(queue.result)).not.toContain('Archived prompt body');
const picked = await getTaskView({ kind: 'queue', selected: 'task-user-0007' });
- // File order is kept: the selected archived row stays where the file put it.
+ // Queue order is kept: the selected completed row stays where the queue puts it.
expect(picked.user.tasks.map(t => t.id)).toEqual(raw.user.tasks.filter(t => t.status !== 'completed' || t.id === 'task-user-0007').map(t => t.id));
expect(picked.user.grouped.completed.map(t => t.id)).toEqual(['task-user-0007']);
expect(picked.user.tasks.find(t => t.id === 'task-user-0007')).toEqual(raw.user.tasks.find(t => t.id === 'task-user-0007'));
expect(picked.cos.tasks.every(t => t.status !== 'completed')).toBe(true);
});
- it('completed view reads one source, pages by cursor, and clones just the page', async () => {
- seed();
+ it('completed view reads one source, pages by cursor, and clones nothing from the cache', async () => {
+ await seedViews();
const raw = await getAllTasks();
const expectedIds = raw.cos.tasks.filter(t => t.status === 'completed').sort(desc).map(t => t.id);
const first = await withSpy(() => getTaskView({ kind: 'completed', source: 'internal', limit: 10 }));
expect(first.result.items.map(t => t.id)).toEqual(expectedIds.slice(0, 10));
expect(first.result).toMatchObject({ total: 60, nextCursor: expectedIds[9] });
- expect(first.cloned).toBeLessThan(JSON.stringify(raw.cos.tasks).length / 5);
+ expect(first.cloned).toBe(0);
const next = await getTaskView({ kind: 'completed', source: 'internal', limit: 10, cursor: first.result.nextCursor });
expect(next.items.map(t => t.id)).toEqual(expectedIds.slice(10, 20));
const tail = await getTaskView({ kind: 'completed', source: 'user', limit: 100, cursor: 'task-user-0003' });
@@ -719,29 +625,47 @@ describe('cosTaskStore.getTaskView (#9677)', () => {
expect(tail.items.map(t => t.id)).toEqual(['task-user-0002', 'task-user-0001', 'task-user-0000']);
});
- it('window view slices in file order with true totals and scalar metadata only', async () => {
- seed();
+ it('serves completed history older than the working set by page, count and selection', async () => {
+ await seedViews(130);
+ // Undated completed tasks count as oldest: the working set keeps the newest 100 by id.
+ const { user } = await getAllTasks();
+ expect(user.tasks.filter(t => t.status === 'completed')).toHaveLength(100);
+ expect(user.tasks.some(t => t.id === 'task-user-0000')).toBe(false);
+
+ const oldest = await getTaskView({ kind: 'completed', source: 'user', limit: 100, cursor: 'task-user-0030' });
+ expect(oldest.items.map(t => t.id)).toEqual(Array.from({ length: 30 }, (_, n) => `task-user-${String(29 - n).padStart(4, '0')}`));
+ expect(oldest.total).toBe(130);
+ expect(oldest.items[0].metadata.prompt).toBe(PROMPT);
+
+ const picked = await getTaskView({ kind: 'queue', selected: 'task-user-0002' });
+ expect(picked.user.completedCount).toBe(130);
+ expect(picked.user.tasks.find(t => t.id === 'task-user-0002')).toMatchObject({ status: 'completed', metadata: { prompt: PROMPT } });
+ expect((await getTaskView({ kind: 'bare' })).user.completedNextCursor).toBe('task-user-0105');
+ });
+
+ it('window view slices the working set in queue order with true totals and scalar metadata only', async () => {
+ await seedViews();
const raw = await getAllTasks();
const { result, cloned } = await withSpy(() => getTaskView({ kind: 'window', offset: 2, limit: 3 }));
- expect(result.user).toEqual({ file: USER_FILE, exists: true, type: 'user', tasks: raw.user.tasks.slice(2, 5) });
+ expect(result.user).toEqual({ type: 'user', tasks: raw.user.tasks.slice(2, 5) });
expect(result.cos.tasks).toEqual(raw.cos.tasks.slice(2, 5));
expect(result.totals).toEqual({ user: raw.user.tasks.length, cos: raw.cos.tasks.length });
expect(cloned).toBeLessThan(JSON.stringify(raw).length / 10);
});
- it('missing files keep the getUserTasks/getCosTasks shape', async () => {
+ it('empty queues keep the getUserTasks/getCosTasks shape', async () => {
const raw = await getAllTasks();
const bare = await getTaskView({ kind: 'bare' });
- expect(bare.user).toMatchObject({ tasks: [], exists: false, type: 'user', completedCount: 0, completedNextCursor: null });
- expect(bare.cos.autoApproved).toBeUndefined();
- expect(raw.cos.autoApproved).toBeUndefined();
+ expect(bare.user).toMatchObject({ tasks: [], type: 'user', completedCount: 0, completedNextCursor: null });
+ expect(bare.cos.autoApproved).toEqual([]);
+ expect(raw.cos.autoApproved).toEqual([]);
expect((await getTaskView({ kind: 'queue' })).cos.completedCount).toBe(0);
expect(await getTaskView({ kind: 'completed', source: 'user', limit: 5 })).toEqual({ items: [], total: 0, nextCursor: null });
expect((await getTaskView({ kind: 'window', offset: 0, limit: 5 })).totals).toEqual({ user: 0, cos: 0 });
});
- it('never leaks the cache: caller mutation, internal writes and same-size external edits', async () => {
- seed(30);
+ it('never leaks the cache: caller mutation, internal writes and out-of-band changes', async () => {
+ await seedViews(30);
const first = await getTaskView({ kind: 'bare' });
first.user.tasks[0].description = 'mutated in place';
first.user.tasks.push({ id: 'ghost' });
@@ -761,15 +685,11 @@ describe('cosTaskStore.getTaskView (#9677)', () => {
expect(written.user.tasks.map(t => t.id)).toEqual(['task-user-1001', 'task-user-1002']);
expect(written.user.completedCount).toBe(31);
- // External same-size edit with a moved stamp (a peer/editor flips a status).
- const text = mock.files.get(USER_FILE);
- mock.files.set(USER_FILE, text.replace('- [~] #task-user-1001', '- [x] #task-user-1001'));
- mock.mtimes.set(USER_FILE, mock.mtimes.get(USER_FILE) + 5000);
+ // Out-of-band change (another writer flips a status): the version bump drops the snapshot.
+ await seed('user', (await stored('user')).map(t => (t.id === 'task-user-1001' ? { ...t, status: 'completed' } : t)));
const external = await getTaskView({ kind: 'queue' });
expect(external.user.tasks.map(t => t.id)).toEqual(['task-user-1002']);
expect(external.user.completedCount).toBe(32);
- mock.files.delete(USER_FILE);
- expect((await getTaskView({ kind: 'bare' })).user.exists).toBe(false);
});
});
@@ -800,39 +720,36 @@ describe('cosTaskStore.getAllTasks / getTasks / getTaskById', () => {
});
it('looks up one task without cloning either backlog, and isolates returned metadata', async () => {
- const { generateTasksMarkdown } = await import('../lib/taskParser.js');
const tasks = Array.from({ length: 1000 }, (_, index) => ({
id: `task-${index}`, description: `Example task ${index}`, status: 'pending',
priority: 'MEDIUM', metadata: { prompt: 'Example prompt', reviewers: ['codex'] }
}));
- mock.files.set(USER_FILE, generateTasksMarkdown(tasks));
+ await seed('user', tasks);
+ mock.loads = 0;
const clone = vi.spyOn(globalThis, 'structuredClone');
const found = await getTaskById('task-999');
expect(clone.mock.calls.map(([value]) => value.id)).toEqual(['task-999']);
clone.mockRestore();
found.metadata.reviewers.push('changed');
expect((await getTaskById('task-999')).metadata.reviewers).toEqual(['codex']);
- expect(mock.parseCalls).toBe(1);
+ expect(mock.loads).toBe(1);
});
- it('preserves user-first and first-duplicate precedence with custom paths and IDs', async () => {
- mock.state.config = { userTasksFile: 'custom/user.md', cosTasksFile: 'custom/system.md' };
- const { generateTasksMarkdown } = await import('../lib/taskParser.js');
+ it('prefers the user queue when both queues hold the id, and falls back to the internal one', async () => {
const task = { id: 'sys-legacy', description: 'first', status: 'pending', priority: 'HIGH', metadata: {} };
- mock.files.set(join('/root', 'custom/user.md'), generateTasksMarkdown([task, { ...task, description: 'duplicate' }]));
- mock.files.set(join('/root', 'custom/system.md'), generateTasksMarkdown([{ ...task, description: 'internal' }]));
+ await seed('user', [task]);
+ await seed('internal', [{ ...task, description: 'internal' }]);
expect(await getTaskById(task.id)).toMatchObject({ description: 'first', taskType: 'user' });
- mock.files.delete(join('/root', 'custom/user.md'));
+ await deleteTask(task.id, 'user');
expect(await getTaskById(task.id)).toMatchObject({ description: 'internal', taskType: 'internal' });
});
- it('invalidates the ID index after store mutations, external edits, and deletion', async () => {
+ it('invalidates the ID index after store mutations, out-of-band changes, and deletion', async () => {
await addTask({ id: 'task-index', description: 'before' }, 'user');
expect((await getTaskById('task-index')).description).toBe('before');
await updateTask('task-index', { description: 'after' }, 'user');
expect((await getTaskById('task-index')).description).toBe('after');
- mock.files.set(USER_FILE, mock.files.get(USER_FILE).replace('after', 'external'));
- mock.mtimes.set(USER_FILE, mock.mtimes.get(USER_FILE) + 5000);
+ await seed('user', (await stored('user')).map((t) => ({ ...t, description: 'external' })));
expect((await getTaskById('task-index')).description).toBe('external');
await deleteTask('task-index', 'user');
expect(await getTaskById('task-index')).toBeNull();
@@ -853,11 +770,11 @@ describe('cosTaskStore.addTask', () => {
expect(mock.events.some(e => e.name === 'tasks:changed' && e.payload.action === 'added' && e.payload.type === 'user')).toBe(true);
});
- it('keeps the existing task list intact and surfaces the error when the file write fails', async () => {
+ it('keeps the existing task list intact and surfaces the error when the save fails', async () => {
await addTask({ description: 'survivor', id: 'task-survivor' }, 'user');
- mock.atomicFailure = Object.assign(new Error('ENOSPC: no space left on device'), { code: 'ENOSPC' });
+ mock.writeFailure = Object.assign(new Error('ENOSPC: no space left on device'), { code: 'ENOSPC' });
await expect(addTask({ description: 'lost write', id: 'task-lost' }, 'user')).rejects.toThrow('ENOSPC');
- mock.atomicFailure = null;
+ mock.writeFailure = null;
expect((await getUserTasks()).tasks.map(t => t.id)).toEqual(['task-survivor']);
});
@@ -2131,11 +2048,6 @@ describe('cosTaskStore.updateTask', () => {
expect(updated.metadata.context).toBeUndefined();
});
- it('returns an error object when the file is missing', async () => {
- const result = await updateTask('task-x', { status: 'completed' }, 'user');
- expect(result.error).toBe('Task file not found');
- });
-
it('returns an error object when the task id is absent', async () => {
await addTask({ description: 'present' }, 'user');
const result = await updateTask('task-missing', { status: 'completed' }, 'user');
@@ -2149,15 +2061,15 @@ describe('cosTaskStore collection selection', () => {
['update', () => updateTask('sys-example', { description: 'Unexpected edit' }, 'cos')],
['delete', () => deleteTask('sys-example', ['user', 'internal'])],
['merge', () => mergePeerTasks('cos', [{ id: 'sys-example', description: 'Unexpected peer edit' }])],
- ])('rejects an invalid %s selector without changing either task file', async (_name, mutate) => {
+ ])('rejects an invalid %s selector without changing either task queue', async (_name, mutate) => {
await addTask({ description: 'User task', id: 'task-example' });
await addTask({ description: 'Internal task', id: 'sys-example', approvalRequired: true }, 'internal');
- const before = new Map(mock.files);
+ const before = [await stored('user'), await stored('internal')];
mock.events = [];
await expect(mutate()).rejects.toMatchObject({ status: 400, code: 'VALIDATION_ERROR' });
- expect(mock.files).toEqual(before);
+ expect([await stored('user'), await stored('internal')]).toEqual(before);
expect(mock.events).toEqual([]);
});
});
@@ -2213,30 +2125,27 @@ describe('cosTaskStore.approveTask', () => {
expect((await approveTask('sys-auto')).error).toBe('Task does not require approval');
});
- it('returns an error when the cos task file is missing', async () => {
- expect((await approveTask('sys-x')).error).toBe('CoS task file not found');
+ it('returns an error when the task is in neither queue', async () => {
+ expect((await approveTask('sys-x')).error).toBe('Task not found');
});
- it('releases a USER row the parser withheld, so the hold is not a zombie', async () => {
- // A row the parser had to RECOVER is withheld from the unattended spawn and
- // now persists that hold in TASKS.md (#7300, #7367). Nothing else in the
- // product can clear it: `updateTask` carries the flags through untouched, so
- // without this the row stays pending forever.
- const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
- mock.files.set(USER_FILE, '# Tasks\n\n## Pending\n- [ ] #task-held | URGENT | legacy user row\n - prompt: payload\n');
- mock.mtimes.set(USER_FILE, 1);
+ it('releases a USER row that holds for approval, so the hold is not a zombie', async () => {
+ // A user row the importer had to RECOVER is withheld from the unattended spawn
+ // and keeps that hold (#7300, #7367). Nothing else in the product can clear it:
+ // `updateTask` carries the flags through untouched, so without this the row
+ // stays pending forever.
+ await seed('user', [{ id: 'task-held', description: 'legacy user row', status: 'pending', priority: 'URGENT', approvalRequired: true, autoApproved: false, metadata: { prompt: 'payload' } }]);
const held = (await getUserTasks()).tasks[0];
expect(held.approvalRequired).toBe(true);
const approved = await approveTask('task-held');
- warn.mockRestore();
expect(approved.approvalRequired).toBe(false);
expect(approved.autoApproved).toBe(true);
expect(approved.metadata.prompt).toBe('payload');
expect(mock.events.some(e => e.name === 'tasks:changed' && e.payload.action === 'approved' && e.payload.type === 'user')).toBe(true);
- // And the release is persisted: the rewritten row carries no hold.
+ // And the release is persisted: the stored row carries no hold.
__resetTaskCache();
expect((await getUserTasks()).tasks[0].approvalRequired).toBe(false);
});
@@ -2247,11 +2156,11 @@ describe('cosTaskStore.approveTask', () => {
const __dirname = dirname(fileURLToPath(import.meta.url));
const STORE_SRC = realReadFileSync(join(__dirname, 'cosTaskStore.js'), 'utf-8');
-describe('parsed-task cache — no bypass (source guards, #3497)', () => {
- // Everything outside the cache accessors. Any fs call the rest of the
- // module makes to the task files is a bypass by definition.
+describe('working-set cache — no bypass (source guards, #3497)', () => {
+ // Everything outside the cache accessors. Any direct repository load or save
+ // the rest of the module makes is a bypass by definition.
const ACCESSOR_START = 'async function readTaskSnapshot';
- const ACCESSOR_END = '/** Test hook: forget every cached parse. */';
+ const ACCESSOR_END = '/** Test hook: forget every cached working set. */';
const outsideAccessors = (() => {
const start = STORE_SRC.indexOf(ACCESSOR_START);
const end = STORE_SRC.indexOf(ACCESSOR_END);
@@ -2262,31 +2171,30 @@ describe('parsed-task cache — no bypass (source guards, #3497)', () => {
})();
it('locates the cache accessors it is guarding', () => {
- // If this fails the two guards below are vacuous — the anchors drifted.
+ // If this fails the guards below are vacuous — the anchors drifted.
expect(outsideAccessors, `anchors "${ACCESSOR_START}" / "${ACCESSOR_END}" not found in order`).not.toBeNull();
});
- it('every task-file read goes through the snapshot cache', () => {
- // A read path calling readFile + parseTasksMarkdown directly still works,
- // but pays the full parse the cache exists to avoid and skips the
- // copy-on-read that keeps a caller's in-place mutations out of the cache.
- const bypasses = outsideAccessors.filter(line => /\breadFile\(/.test(line));
- expect(bypasses, `direct readFile call(s): ${bypasses.join(' | ')}`).toEqual([]);
+ it('every working-set read goes through the snapshot cache', () => {
+ // A read path calling the repository directly still works, but skips the
+ // version check, the shared load, and the copy-on-read that keeps a caller's
+ // in-place mutations out of the cache.
+ const bypasses = outsideAccessors.filter(line => /\b(loadTaskWorkingSet|readTaskListVersion)\(/.test(line));
+ expect(bypasses, `direct repository read(s): ${bypasses.join(' | ')}`).toEqual([]);
});
- it('writeTaskFile persists through atomicWrite, never an in-place fs writeFile', () => {
- // An in-place truncate killed mid-write leaves TASKS.md empty/half-written,
- // which parses as "no tasks" and is persisted by the next write.
- expect(STORE_SRC).toMatch(/import \{ atomicWrite \} from '\.\.\/lib\/fileUtils\.js'/);
- expect(STORE_SRC).not.toMatch(/import \{[^}]*\bwriteFile\b[^}]*\} from 'fs\/promises'/);
+ it('every list save goes through writeTaskList', () => {
+ // A save calling the repository directly leaves the cached working set
+ // stale until its version is next checked, and skips the diff that keeps a
+ // one-field change a one-row write.
+ const bypasses = outsideAccessors.filter(line => /\bapplyTaskListSave\(/.test(line));
+ expect(bypasses, `direct applyTaskListSave call(s): ${bypasses.join(' | ')}`).toEqual([]);
});
- it('every task-file write goes through writeTaskFile', () => {
- // A write calling writeFile directly leaves the previous parse cached, and
- // mtime granularity is too coarse to be relied on to catch it — the next
- // read would serve pre-write tasks.
- const bypasses = outsideAccessors.filter(line => /\b(writeFile|atomicWrite)\(/.test(line));
- expect(bypasses, `direct writeFile/atomicWrite call(s): ${bypasses.join(' | ')}`).toEqual([]);
+ it('writeTaskList persists a row diff, never the whole queue', () => {
+ const code = STORE_SRC.split('\n').filter(line => !line.trim().startsWith('//') && !line.trim().startsWith('*')).join('\n');
+ expect(code).toMatch(/planListSave\(\{ previous: snapshot\.rows, next: tasks, source \}\)/);
+ expect(code).not.toMatch(/generateTasksMarkdown|parseTasksMarkdown|atomicWrite/);
});
});
@@ -2338,12 +2246,11 @@ describe('cosTaskStore.mergePeerTasks', () => {
it('is a no-op (no write, no event) when the peer payload changes nothing', async () => {
await addTask({ description: 'same', priority: 'MEDIUM', id: 'task-same' }, 'user');
mock.events = [];
- const writeSpy = (await import('../lib/fileUtils.js')).atomicWrite;
- writeSpy.mockClear();
+ vi.mocked(repository.applyTaskListSave).mockClear();
const remote = [{ id: 'task-same', taskType: 'user', status: 'pending', priority: 'MEDIUM', description: 'same', metadata: {} }];
const res = await mergePeerTasks('user', remote, { now: NOW });
expect(res).toEqual({ changed: false });
- expect(writeSpy).not.toHaveBeenCalled();
+ expect(repository.applyTaskListSave).not.toHaveBeenCalled();
expect(mock.events.some(e => e.name === 'tasks:changed')).toBe(false);
});
@@ -2623,11 +2530,12 @@ describe('cosTaskStore — stale failure-artifact reaper (#2619)', () => {
}
// After the first flip lands, the user revives the second target (Resume
// → in_progress). Its flip was planned from the pre-revive snapshot.
- mock.afterWrite = (p) => {
- if (p !== USER_FILE) return false;
+ mock.afterWrite = async (source) => {
+ if (source !== 'user') return false;
// Revive whichever target the sweep has not flipped yet.
- mock.files.set(p, mock.files.get(p).replace(/^(- )\[!\](.*#task-race)/m, '$1[~]$2'));
- mock.mtimes.set(p, (mock.mtimes.get(p) || 0) + 1000);
+ const tasks = await stored('user');
+ const target = tasks.find(task => task.status === 'blocked' && task.id.startsWith('task-race'));
+ await seed('user', tasks.map(task => (task === target ? { ...task, status: 'in_progress' } : task)));
return true;
};
const res = await sweepResolvedFailureTasks({ now: NOW });
@@ -2654,12 +2562,10 @@ describe('cosTaskStore — stale failure-artifact reaper (#2619)', () => {
// The first user-queue flip lands before the investigation's planned
// internal-queue flip. Resume the origin in that gap; the investigation
// must be checked against this fresh origin state, not the sweep snapshot.
- mock.afterWrite = (path) => {
- if (path !== USER_FILE) return false;
- const tasks = parseTasksMarkdown(mock.files.get(path));
- tasks.find(task => task.id === 'task-race-origin').status = 'pending';
- mock.files.set(path, generateTasksMarkdown(tasks));
- mock.mtimes.set(path, (mock.mtimes.get(path) || 0) + 1000);
+ mock.afterWrite = async (source) => {
+ if (source !== 'user') return false;
+ const tasks = await stored('user');
+ await seed('user', tasks.map(task => (task.id === 'task-race-origin' ? { ...task, status: 'pending' } : task)));
return true;
};
@@ -2678,14 +2584,13 @@ describe('cosTaskStore — stale failure-artifact reaper (#2619)', () => {
status: 'blocked', metadata: { blockedCategory: 'max-retries', blockedAt: daysAgo(20) },
}, 'user', { now: NOW });
}
- mock.afterWrite = (path) => {
- if (path !== USER_FILE) return false;
+ mock.afterWrite = async (source) => {
+ if (source !== 'user') return false;
// Model a fresh failure or user-decision hold after the snapshot, with
// the same status as before. The completed first row stays untouched.
- const tasks = parseTasksMarkdown(mock.files.get(path));
- tasks.find(task => task.status === 'blocked').metadata[key] = value;
- mock.files.set(path, generateTasksMarkdown(tasks));
- mock.mtimes.set(path, (mock.mtimes.get(path) || 0) + 1000);
+ const tasks = await stored('user');
+ const held = tasks.find(task => task.status === 'blocked');
+ await seed('user', tasks.map(task => (task === held ? { ...task, metadata: { ...task.metadata, [key]: value } } : task)));
return true;
};
expect(await sweepResolvedFailureTasks({ now: NOW })).toMatchObject({ reaped: 1, staleBlocks: 1 });
@@ -2971,10 +2876,11 @@ it('round-trips the server-owned audit disposition contract through persisted ta
expect(reloaded.metadata).toMatchObject({ analysisType: 'security', auditDispositionVersion: '1', fileIssues: 'false' });
});
-// Completed-task archive. TASKS.md / COS-TASKS.md keep the open queue plus a recent
-// window of completed tasks; older completed tasks move to the archive with their
-// full records. The fake archive above mirrors the real adapter's contract.
-describe('cosTaskStore completed-task archive', () => {
+// Completed history older than the working set. The queues are rows in one table:
+// open tasks and recent/newest completed ones are the working set every poller
+// reads; the rest stays reachable by id and by paged history, and must keep working
+// for edit, reopen, delete and peer merge without being pulled back wholesale.
+describe('cosTaskStore completed history older than the working set', () => {
const DAY = 86_400_000;
const CLOCK = Date.parse('2026-10-20T12:00:00Z');
const id = (n) => `task-example-${String(n).padStart(4, '0')}`;
@@ -2983,161 +2889,79 @@ describe('cosTaskStore completed-task archive', () => {
metadata: { updatedAt: new Date(CLOCK - ageDays * DAY).toISOString(), prompt: `Prompt body ${n}.` }, ...extra,
});
const pending = (n) => ({ id: id(n), description: `Example task ${n}`, status: 'pending', priority: 'MEDIUM', metadata: {} });
- const writeUser = (tasks) => mock.files.set(USER_FILE, generateTasksMarkdown(tasks));
- const fileIds = (file = USER_FILE) => parseTasksMarkdown(mock.files.get(file)).map((task) => task.id);
- const archiveIds = (source = 'user') => [...mock.archive[source].keys()].sort();
-
- describe('archiveCompletedTasks', () => {
- it('moves only completed tasks past both the age and newest-N floor, with their full records', async () => {
- writeUser([pending(1), done(2, 10), done(3, 9), done(4, 8), done(5, 1), done(6, 0)]);
- // keepNewest 1 leaves the 1-day-old task rank-eligible, so only the age floor keeps it.
- const res = await archiveCompletedTasks({ now: CLOCK, keepNewest: 1 });
- expect(res).toEqual({ archived: 3 });
- expect(archiveIds()).toEqual([id(2), id(3), id(4)]);
- expect(mock.archive.user.get(id(2)).metadata.prompt).toBe('Prompt body 2.');
- expect(fileIds().sort()).toEqual([id(1), id(5), id(6)]);
- expect(mock.events.some((e) => e.name === 'tasks:changed' && e.payload.action === 'archived')).toBe(true);
- });
-
- it('keeps the newest completed tasks however old they are, and treats undated tasks as oldest', async () => {
- writeUser([done(1, 30), done(2, 20), done(3, 10), done(4, 5), { ...done(5, 0), metadata: {} }]);
- expect(await archiveCompletedTasks({ now: CLOCK, keepNewest: 4 })).toEqual({ archived: 1 });
- expect(archiveIds()).toEqual([id(5)]);
- });
-
- it('drains the oldest first under the per-sweep limit and is idempotent once drained', async () => {
- writeUser([done(1, 40), done(2, 30), done(3, 20), done(4, 10), done(5, 5)]);
- expect(await archiveCompletedTasks({ now: CLOCK, keepNewest: 0, limit: 2 })).toEqual({ archived: 2 });
- expect(archiveIds()).toEqual([id(1), id(2)]);
- expect(await archiveCompletedTasks({ now: CLOCK, keepNewest: 0, limit: 10 })).toEqual({ archived: 3 });
- expect(fileIds()).toEqual([]);
- expect(await archiveCompletedTasks({ now: CLOCK, keepNewest: 0 })).toEqual({ archived: 0 });
- });
-
- it('leaves a duplicate, never a loss, when the file rewrite dies after the archive write', async () => {
- writeUser([done(1, 10), done(2, 9)]);
- mock.atomicFailure = new Error('disk full');
- await expect(archiveCompletedTasks({ now: CLOCK, keepNewest: 0 })).rejects.toThrow('disk full');
- expect(archiveIds()).toEqual([id(1), id(2)]);
- expect(fileIds().sort()).toEqual([id(1), id(2)]);
- mock.atomicFailure = null;
- expect(await archiveCompletedTasks({ now: CLOCK, keepNewest: 0 })).toEqual({ archived: 2 });
- expect(fileIds()).toEqual([]);
- expect(archiveIds()).toEqual([id(1), id(2)]);
- });
-
- it('does nothing when a task file does not exist', async () => {
- expect(await archiveCompletedTasks({ now: CLOCK })).toEqual({ archived: 0 });
- });
- });
-
- describe('reads fall through to the archive', () => {
- const seedSplit = () => {
- // Archive holds the oldest ids, the file holds the recent window.
- for (const n of [1, 2, 3, 4]) mock.archive.user.set(id(n), done(n, 20));
- writeUser([pending(9), done(5, 1), done(6, 1), done(7, 0)]);
- };
+ // 105 completed tasks, ids 1..105, ages 200 days (id 1) to 96 days (id 105): the
+ // newest 100 by completion stay in the working set; ids 1..5 are older history.
+ const seedHistory = async () => {
+ const history = Array.from({ length: 105 }, (_, index) => done(index + 1, 200 - index));
+ await seed('user', [pending(900), ...history]);
+ };
+ const inWorkingSet = async (taskId) => (await getUserTasks()).tasks.some((task) => task.id === taskId);
+
+ it('keeps the oldest completed tasks out of the working set but reachable by id', async () => {
+ await seedHistory();
+ expect(await inWorkingSet(id(1))).toBe(false);
+ expect(await inWorkingSet(id(5))).toBe(false);
+ expect(await inWorkingSet(id(6))).toBe(true);
+ expect(await getTaskById(id(1))).toMatchObject({ id: id(1), status: 'completed', taskType: 'user', metadata: { prompt: 'Prompt body 1.' } });
+ expect(await findTaskWithSource(id(1))).toMatchObject({ source: 'user' });
+ expect(await getTaskById(id(999))).toBeNull();
+ });
+
+ it('reopens an old completed task in place: the edit applies, it joins the working set, no second row appears', async () => {
+ await seedHistory();
+ const result = await updateTask(id(1), { status: 'pending', description: 'Reopened task' }, 'user', { now: CLOCK });
+ expect(result).toMatchObject({ id: id(1), status: 'pending', description: 'Reopened task' });
+ expect(await inWorkingSet(id(1))).toBe(true);
+ expect((await getTaskView({ kind: 'completed', source: 'user', limit: 200 })).total).toBe(104);
+ expect(mock.events.some((e) => e.name === 'tasks:changed' && e.payload.type === 'user')).toBe(true);
+ });
+
+ it('leaves an old completed task untouched when its update is refused', async () => {
+ await seedHistory();
+ expect(await updateTask(id(1), { status: 'pending' }, 'user', { now: CLOCK, expectedStatus: 'blocked' })).toMatchObject({ statusChanged: true });
+ expect((await getTaskById(id(1))).status).toBe('completed');
+ expect(await updateTask(id(999), { status: 'pending' }, 'user', { now: CLOCK })).toEqual({ error: 'Task not found' });
+ });
+
+ it('deletes an old completed task and reports an unknown one as not found', async () => {
+ await seedHistory();
+ expect(await deleteTask(id(1), 'user')).toEqual({ success: true, taskId: id(1) });
+ expect(await getTaskById(id(1))).toBeNull();
+ expect((await getTaskView({ kind: 'completed', source: 'user', limit: 200 })).total).toBe(104);
+ expect(await deleteTask(id(1), 'user')).toEqual({ error: 'Task not found' });
+ expect(mock.events.filter((e) => e.payload?.action === 'deleted')).toHaveLength(1);
+ });
- it('pages completed history across the file window and the archive, newest id first', async () => {
- seedSplit();
- const first = await getTaskView({ kind: 'completed', source: 'user', limit: 5 });
- expect(first.items.map((task) => task.id)).toEqual([id(7), id(6), id(5), id(4), id(3)]);
- expect(first.total).toBe(7);
- expect(first.nextCursor).toBe(id(3));
- const second = await getTaskView({ kind: 'completed', source: 'user', cursor: first.nextCursor, limit: 5 });
- expect(second.items.map((task) => task.id)).toEqual([id(2), id(1)]);
- expect(second.nextCursor).toBeNull();
- });
-
- it('counts archived history in the queue and bare views, and returns a selected archived task', async () => {
- seedSplit();
- const queue = await getTaskView({ kind: 'queue', selected: id(2) });
- expect(queue.user.completedCount).toBe(7);
- expect(queue.user.tasks.map((task) => task.id).sort()).toEqual([id(2), id(9)]);
- expect(queue.user.tasks.find((task) => task.id === id(2)).metadata.prompt).toBe('Prompt body 2.');
- const bare = await getTaskView({ kind: 'bare' });
- expect(bare.user.completedCount).toBe(7);
- expect(bare.user.completedNextCursor).toBeNull();
- expect(bare.user.tasks.map((task) => task.id)).toHaveLength(8);
- });
-
- it('finds an archived task by id, preferring the file copy and the user queue', async () => {
- seedSplit();
- const found = await findTaskWithSource(id(2));
- expect(found).toMatchObject({ source: 'user', task: { id: id(2), status: 'completed' } });
- expect(await getTaskById(id(2))).toMatchObject({ id: id(2), taskType: 'user' });
- mock.archive.user.set(id(5), { ...done(5, 20), description: 'stale archived copy' });
- expect((await getTaskById(id(5))).description).toBe('Example task 5');
- expect(await getTaskById(id(99))).toBeNull();
- });
-
- it('deletes an archived task and reports an unknown one as not found', async () => {
- seedSplit();
- expect(await deleteTask(id(2), 'user')).toEqual({ success: true, taskId: id(2) });
- expect(archiveIds()).toEqual([id(1), id(3), id(4)]);
- expect(await deleteTask(id(2), 'user')).toEqual({ error: 'Task not found' });
- });
- });
-
- describe('archived tasks stay usable', () => {
- it('reopens an archived task: pulls it back into the file, applies the edit, and drops the archive row', async () => {
- mock.archive.user.set(id(1), done(1, 20));
- writeUser([pending(2)]);
- const result = await updateTask(id(1), { status: 'pending', description: 'Reopened task' }, 'user', { now: CLOCK });
- expect(result).toMatchObject({ id: id(1), status: 'pending', description: 'Reopened task' });
- expect(fileIds().sort()).toEqual([id(1), id(2)]);
- expect(archiveIds()).toEqual([]);
- expect(mock.events.some((e) => e.name === 'tasks:changed' && e.payload.type === 'user')).toBe(true);
- });
-
- it('keeps the archive row when the update is refused before the file write', async () => {
- mock.archive.user.set(id(1), done(1, 20));
- writeUser([pending(2)]);
- const result = await updateTask(id(1), { status: 'pending' }, 'user', { now: CLOCK, expectedStatus: 'blocked' });
- expect(result).toMatchObject({ statusChanged: true });
- expect(archiveIds()).toEqual([id(1)]);
- expect(fileIds()).toEqual([id(2)]);
- });
-
- it('still reports an id that is in neither store as not found', async () => {
- writeUser([pending(2)]);
- expect(await updateTask(id(9), { status: 'pending' }, 'user', { now: CLOCK })).toEqual({ error: 'Task not found' });
- });
-
- it('deletes both copies of a task caught between the archive write and the file rewrite', async () => {
- mock.archive.user.set(id(1), done(1, 20));
- writeUser([pending(2), done(1, 20)]);
- expect(await deleteTask(id(1), 'user')).toEqual({ success: true, taskId: id(1) });
- expect(fileIds()).toEqual([id(2)]);
- expect(archiveIds()).toEqual([]);
- });
-
- it('deletes an archived task even when its task file is gone', async () => {
- mock.archive.user.set(id(1), done(1, 20));
- expect(await deleteTask(id(1), 'user')).toEqual({ success: true, taskId: id(1) });
- expect(await deleteTask(id(1), 'user')).toEqual({ error: 'Task file not found' });
- });
-
- it('windows by offset across the file tasks, then the archive newest first, with a combined total', async () => {
- for (const n of [1, 2, 3, 4]) mock.archive.user.set(id(n), done(n, 20));
- writeUser([pending(9), done(5, 1)]);
- const first = await getTaskView({ kind: 'window', offset: 0, limit: 3 });
- expect(first.user.tasks.map((task) => task.id)).toEqual([id(9), id(5), id(4)]);
- expect(first.totals.user).toBe(6);
- const second = await getTaskView({ kind: 'window', offset: 3, limit: 5 });
- expect(second.user.tasks.map((task) => task.id)).toEqual([id(3), id(2), id(1)]);
- const fileOnly = await getTaskView({ kind: 'window', offset: 0, limit: 1 });
- expect(fileOnly.user.tasks.map((task) => task.id)).toEqual([id(9)]);
- });
- });
-
- it('does not let a peer resurrect a completed task this machine archived', async () => {
- mock.archive.user.set(id(1), done(1, 20));
- writeUser([pending(2)]);
+ it('does not let a peer overwrite or re-introduce completed history this machine already holds', async () => {
+ await seedHistory();
const wire = (task) => ({ id: task.id, taskType: 'user', status: task.status, priority: task.priority, description: task.description, metadata: task.metadata });
- const res = await mergePeerTasks('user', [wire(done(1, 20)), wire(done(3, 1)), wire(pending(4))], { now: CLOCK });
+ const res = await mergePeerTasks('user', [wire({ ...done(1, 200), description: 'Peer copy' }), wire(done(2000, 1)), wire(pending(2001))], { now: CLOCK });
expect(res.changed).toBe(true);
- // The archived completion stays out; an unarchived completion and a new open task still adopt.
- expect(fileIds().sort()).toEqual([id(2), id(3), id(4)]);
+ expect((await getTaskById(id(1))).description).toBe('Example task 1');
+ expect(await inWorkingSet(id(1))).toBe(false);
+ // A completion this machine has never seen, and a new open task, still arrive.
+ expect(await inWorkingSet(id(2000))).toBe(true);
+ expect(await inWorkingSet(id(2001))).toBe(true);
+ });
+
+ it('mints a unique id when a new task collides with a stored one, in the working set or in history', async () => {
+ await seedHistory();
+ const inHistory = await addTask({ description: 'Collides with history', id: id(1) }, 'user', { raw: true });
+ expect(inHistory.id).toBe(`${id(1)}-dup2`);
+ const again = await addTask({ description: 'Collides again', id: id(1) }, 'user', { raw: true });
+ expect(again.id).toBe(`${id(1)}-dup3`);
+ const live = await addTask({ description: 'Collides with an open task', id: id(900) }, 'user', { raw: true });
+ expect(live.id).toBe(`${id(900)}-dup2`);
+ expect((await getTaskById(id(1))).description).toBe('Example task 1');
+ const ids = (await getUserTasks()).tasks.map((task) => task.id);
+ expect(new Set(ids).size).toBe(ids.length);
+ });
+
+ it('stores two tasks created in the same millisecond as two tasks', async () => {
+ const now = Date.parse('2026-10-20T12:00:00Z');
+ const first = await addTask({ description: 'First of a burst' }, 'user', { now });
+ const second = await addTask({ description: 'Second of a burst' }, 'user', { now });
+ expect(second.id).not.toBe(first.id);
+ expect((await getUserTasks()).tasks.map((task) => task.description).sort()).toEqual(['First of a burst', 'Second of a burst']);
});
});
diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.js
index e9cc32c979..7d8c7973d5 100644
--- a/server/services/sharing/peerSync.test.js
+++ b/server/services/sharing/peerSync.test.js
@@ -4579,17 +4579,14 @@ describe('peerSync', () => {
// must not ride the federation layer at all (ADR
// 2026-08-08-privacy-records-machine-local), and a peer has no use for a
// record of a button someone pressed on another install.
- // - cosTaskArchive is the completed-task history moved out of TASKS.md /
- // COS-TASKS.md: prompt bodies and run notes for ONE machine. Peers merge only
- // the tasks their own files hold, never this table.
// This guard pins that decision: federating any of them later is a conscious
// act — wire the kind AND update this assertion + the ADR together.
- const localOnlyKinds = ['tribe', 'tribePerson', 'tribeTouchpoint', 'tribeMemoryLink', 'tribeIdentity', 'universeRun', 'humanActivityEvent', 'userActionEvent', 'cosTaskArchive'];
+ const localOnlyKinds = ['tribe', 'tribePerson', 'tribeTouchpoint', 'tribeMemoryLink', 'tribeIdentity', 'universeRun', 'humanActivityEvent', 'userActionEvent'];
for (const kind of localOnlyKinds) {
expect(PEER_SUBSCRIBABLE_KINDS).not.toContain(kind);
expect(RECORD_KIND_SCHEMA_CATEGORIES[kind]).toBeUndefined();
}
- const localOnlyCategories = ['tribe', 'tribePeople', 'tribeTouchpoints', 'tribeMemoryLinks', 'tribeIdentities', 'universeRuns', 'humanActivityEvents', 'userActionEvents', 'cosTaskArchive'];
+ const localOnlyCategories = ['tribe', 'tribePeople', 'tribeTouchpoints', 'tribeMemoryLinks', 'tribeIdentities', 'universeRuns', 'humanActivityEvents', 'userActionEvents'];
for (const category of localOnlyCategories) {
expect(PORTOS_SCHEMA_VERSIONS[category]).toBeUndefined();
expect(NON_RECORD_SCHEMA_CATEGORIES.has(category)).toBe(false);
diff --git a/server/services/sharing/watcher.js b/server/services/sharing/watcher.js
index 537aac75c8..0082d6805f 100644
--- a/server/services/sharing/watcher.js
+++ b/server/services/sharing/watcher.js
@@ -5,9 +5,8 @@
* When a peer's cloud-sync app drops a new manifest into the shared folder,
* chokidar fires `add` and we ingest it via importer.processManifest. When
* asset files arrive after the manifest, we rescan the backlog so pending
- * manifests can finish. Mirrors the pattern in server/services/taskWatcher.js
- * — persistent + ignoreInitial + awaitWriteFinish so we don't pick up the file
- * mid-write.
+ * manifests can finish. The chokidar options are persistent + ignoreInitial +
+ * awaitWriteFinish so we don't pick up the file mid-write.
*
* Per-bucket watchers are kept in a map keyed by bucket id so a bucket
* registration / removal can attach / detach individually without restarting
diff --git a/server/services/socket.js b/server/services/socket.js
index c8a32f1bb2..c54ddd0056 100644
--- a/server/services/socket.js
+++ b/server/services/socket.js
@@ -442,14 +442,14 @@ export const __testing = { registerAuthHandlers, PEER_RELAY_ALLOWED_EVENTS, regi
// Broadcast to CoS subscribers only
function broadcastToCos(event, data) {
- if (!['cos:tasks:user:changed', 'cos:tasks:cos:changed', 'cos:tasks:changed'].includes(event)) {
+ if (event !== 'cos:tasks:changed') {
broadcastToSet(cosSubscribers, event, data);
return;
}
for (const socket of cosSubscribers) {
if (!socket.connected) { cosSubscribers.delete(socket); continue; }
- const completedChanged = event === 'cos:tasks:changed' && (data?.task?.status === 'completed'
- || data?.previousStatus === 'completed' || ['deleted', 'peer-merged'].includes(data?.action));
+ const completedChanged = data?.task?.status === 'completed'
+ || data?.previousStatus === 'completed' || ['deleted', 'peer-merged'].includes(data?.action);
socket.emit(event, socket.cosTaskLists === 'invalidate'
? { invalidated: true, ...(completedChanged ? { completedChanged: true } : {}) } : data);
}
@@ -516,10 +516,6 @@ function setupCosEventForwarding() {
// transition. Forward the task itself so focused views can update one row
// without waiting for the file watcher to rebuild the entire task list.
cosEvents.on('tasks:changed', (data) => broadcastToCos('cos:tasks:changed', data));
- cosEvents.on('tasks:user:changed', (data) => broadcastToCos('cos:tasks:user:changed', data));
- cosEvents.on('tasks:user:added', (data) => broadcastToCos('cos:tasks:user:added', data));
- cosEvents.on('tasks:user:completed', (data) => broadcastToCos('cos:tasks:user:completed', data));
- cosEvents.on('tasks:cos:changed', (data) => broadcastToCos('cos:tasks:cos:changed', data));
cosEvents.on('maintenance:updated', (data) => broadcastToCos('cos:maintenance:updated', data));
@@ -565,7 +561,6 @@ function setupCosEventForwarding() {
cosEvents.on('feature-agent:run-complete', (data) => broadcastToCos('cos:feature-agent:run-complete', data));
// Watcher events
- cosEvents.on('watcher:started', (data) => broadcastToCos('cos:watcher:started', data));
cosEvents.on('watcher:stopped', (data) => broadcastToCos('cos:watcher:stopped', data));
// A user-initiated on-demand "Run" that produced no task — the client toasts
@@ -735,7 +730,7 @@ function setupPeerAgentEventForwarding() {
function setupReviewEventForwarding() {
// Global invalidations reach the bell/dashboard even without a CoS room
// subscription. They carry no record payload and are never peer-forwarded.
- for (const event of ['tasks:changed', 'tasks:user:changed', 'tasks:cos:changed', 'agent:completed', 'agent:feedback', 'memory:approved', 'memory:rejected']) {
+ for (const event of ['tasks:changed', 'agent:completed', 'agent:feedback', 'memory:approved', 'memory:rejected']) {
cosEvents.on(event, () => {
if (ioInstance) ioInstance.emit('review:queue:changed');
});
diff --git a/server/services/socket.test.js b/server/services/socket.test.js
index dbc764f844..f9981a9ee9 100644
--- a/server/services/socket.test.js
+++ b/server/services/socket.test.js
@@ -456,12 +456,11 @@ describe('socket.js — initSocket', () => {
compact.handlers['cos:subscribe']({ taskLists: 'invalidate' });
compact.handlers['cos:subscribe'](); // another shared consumer must not widen it
legacy.handlers['cos:subscribe']();
- const payload = { tasks: [{ id: 'example', metadata: { prompt: 'large private prompt' } }] };
- const forward = queueListeners.cos.find(([event]) => event === 'tasks:user:changed')[1];
- forward(payload);
- expect(compact.emitted).toContainEqual(['cos:tasks:user:changed', { invalidated: true }]);
- expect(legacy.emitted).toContainEqual(['cos:tasks:user:changed', payload]);
+ const payload = { task: { id: 'example', status: 'in_progress', metadata: { prompt: 'large private prompt' } } };
const forwardChange = queueListeners.cos.find(([event]) => event === 'tasks:changed')[1];
+ forwardChange(payload);
+ expect(compact.emitted).toContainEqual(['cos:tasks:changed', { invalidated: true }]);
+ expect(legacy.emitted).toContainEqual(['cos:tasks:changed', payload]);
forwardChange({ task: { id: 'done', status: 'completed' } });
expect(compact.emitted).toContainEqual(['cos:tasks:changed', { invalidated: true, completedChanged: true }]);
forwardChange({ task: { id: 'live', status: 'in_progress' } });
diff --git a/server/services/taskWatcher.js b/server/services/taskWatcher.js
deleted file mode 100644
index 6f9352f7cb..0000000000
--- a/server/services/taskWatcher.js
+++ /dev/null
@@ -1,300 +0,0 @@
-/**
- * Task Watcher Service
- *
- * Watches TASKS.md and COS-TASKS.md for changes and emits events
- * when tasks are added, modified, or completed.
- */
-
-import { watch } from 'chokidar';
-import { join } from 'path';
-import { isDeepStrictEqual } from 'util';
-import { cosEvents, getUserTasks, getCosTasks, getConfig } from './cos.js';
-import { PATHS } from '../lib/fileUtils.js';
-
-const ROOT_DIR = PATHS.root;
-
-let watcher = null;
-let isWatching = false;
-// In-flight start; set synchronously so overlapping starts coalesce and a stop can await it.
-let startPromise = null;
-let lastUserTasks = null;
-let lastCosTasks = null;
-const pendingFileEvents = new Map();
-
-// Chokidar does not await async listeners. Keep one promise tail per watched
-// file so a quick add/change/unlink burst cannot race the cached snapshot, and
-// keep each lane alive after a failed read.
-function queueFileEvent(path, event, operation) {
- const previous = pendingFileEvents.get(path) || Promise.resolve();
- const tail = previous
- .then(operation)
- .catch((error) => {
- console.error(`❌ Task watcher ${event} failed for ${path}: ${error?.message || error}`);
- cosEvents.emit('watcher:error', { error: error?.message || String(error), event, file: path });
- })
- .finally(() => {
- if (pendingFileEvents.get(path) === tail) pendingFileEvents.delete(path);
- });
- pendingFileEvents.set(path, tail);
- return tail;
-}
-
-/**
- * Start watching task files
- */
-export function startWatching() {
- if (startPromise) return startPromise;
- if (isWatching) {
- return Promise.resolve({ success: false, error: 'Already watching' });
- }
- startPromise = doStartWatching().finally(() => { startPromise = null; });
- return startPromise;
-}
-
-async function doStartWatching() {
-
- const config = await getConfig();
- const userTasksPath = join(ROOT_DIR, config.userTasksFile);
- const cosTasksPath = join(ROOT_DIR, config.cosTasksFile);
-
- // Load initial state
- lastUserTasks = await getUserTasks();
- lastCosTasks = await getCosTasks();
-
- watcher = watch([userTasksPath, cosTasksPath], {
- persistent: true,
- ignoreInitial: true,
- awaitWriteFinish: {
- stabilityThreshold: 300,
- pollInterval: 100
- }
- });
-
- watcher.on('change', (path) => {
- queueFileEvent(path, 'change', async () => {
- if (path.endsWith(config.userTasksFile)) {
- await handleUserTasksChange();
- } else if (path.endsWith(config.cosTasksFile)) {
- await handleCosTasksChange();
- }
- });
- });
-
- watcher.on('add', (path) => {
- queueFileEvent(path, 'add', async () => {
- if (path.endsWith(config.userTasksFile)) {
- lastUserTasks = await getUserTasks();
- cosEvents.emit('tasks:user:created', lastUserTasks);
- } else if (path.endsWith(config.cosTasksFile)) {
- lastCosTasks = await getCosTasks();
- cosEvents.emit('tasks:cos:created', lastCosTasks);
- }
- });
- });
-
- watcher.on('unlink', (path) => {
- queueFileEvent(path, 'unlink', async () => {
- if (path.endsWith(config.userTasksFile)) {
- cosEvents.emit('tasks:user:deleted', { file: path });
- lastUserTasks = null;
- } else if (path.endsWith(config.cosTasksFile)) {
- cosEvents.emit('tasks:cos:deleted', { file: path });
- lastCosTasks = null;
- }
- });
- });
-
- watcher.on('error', (error) => {
- cosEvents.emit('watcher:error', { error: error.message });
- });
-
- isWatching = true;
- cosEvents.emit('watcher:started', {
- files: [userTasksPath, cosTasksPath]
- });
-
- return { success: true };
-}
-
-/**
- * Stop watching task files
- */
-export async function stopWatching() {
- // A stop issued mid-start must tear down the watcher that start creates.
- if (startPromise) await startPromise.catch(() => {});
- if (!isWatching || !watcher) {
- return { success: false, error: 'Not watching' };
- }
-
- await watcher.close();
- await Promise.all([...pendingFileEvents.values()]);
- watcher = null;
- isWatching = false;
-
- cosEvents.emit('watcher:stopped');
- return { success: true };
-}
-
-/**
- * Handle changes to user tasks file
- */
-async function handleUserTasksChange() {
- const current = await getUserTasks();
-
- if (!lastUserTasks) {
- lastUserTasks = current;
- cosEvents.emit('tasks:user:changed', current);
- return;
- }
-
- const changes = diffTasks(lastUserTasks.tasks, current.tasks);
-
- if (changes.added.length > 0) {
- cosEvents.emit('tasks:user:added', { tasks: changes.added });
- }
-
- if (changes.completed.length > 0) {
- cosEvents.emit('tasks:user:completed', { tasks: changes.completed });
- }
-
- if (changes.modified.length > 0) {
- cosEvents.emit('tasks:user:modified', { tasks: changes.modified });
- // Emit added event for tasks changed to pending so cos.js picks them up
- const pendingChanges = changes.modified.filter(c => c.new.status === 'pending' && c.old.status !== 'pending');
- if (pendingChanges.length > 0) {
- cosEvents.emit('tasks:user:added', { tasks: pendingChanges.map(c => c.new) });
- }
- }
-
- if (changes.removed.length > 0) {
- cosEvents.emit('tasks:user:removed', { tasks: changes.removed });
- }
-
- lastUserTasks = current;
- cosEvents.emit('tasks:user:changed', current);
-}
-
-/**
- * Handle changes to CoS internal tasks file
- */
-async function handleCosTasksChange() {
- const current = await getCosTasks();
-
- if (!lastCosTasks) {
- lastCosTasks = current;
- cosEvents.emit('tasks:cos:changed', current);
- return;
- }
-
- const changes = diffTasks(lastCosTasks.tasks, current.tasks);
-
- if (changes.added.length > 0) {
- cosEvents.emit('tasks:cos:added', { tasks: changes.added });
- }
-
- if (changes.completed.length > 0) {
- cosEvents.emit('tasks:cos:completed', { tasks: changes.completed });
- }
-
- if (changes.modified.length > 0) {
- cosEvents.emit('tasks:cos:modified', { tasks: changes.modified });
- // Emit added event for newly approved tasks so cos.js picks them up
- const approvedTasks = changes.modified.filter(c =>
- c.new.autoApproved && !c.old.autoApproved
- );
- if (approvedTasks.length > 0) {
- cosEvents.emit('tasks:cos:added', { tasks: approvedTasks.map(c => c.new) });
- }
- }
-
- if (changes.removed.length > 0) {
- cosEvents.emit('tasks:cos:removed', { tasks: changes.removed });
- }
-
- lastCosTasks = current;
- cosEvents.emit('tasks:cos:changed', current);
-}
-
-// Legacy parsed files can carry undefined-valued entries (a bare `undefined`
-// metadata value); the old JSON comparison dropped them, so treat them as absent.
-// Shallow copies share the prompt strings by reference - nothing is serialized.
-function withoutUndefined(obj) {
- const out = {};
- for (const [key, value] of Object.entries(obj)) {
- if (value !== undefined) out[key] = value;
- }
- return out;
-}
-
-function normalizeTask(task) {
- const out = withoutUndefined(task);
- if (out.metadata && typeof out.metadata === 'object' && !Array.isArray(out.metadata)) {
- out.metadata = withoutUndefined(out.metadata);
- }
- return out;
-}
-
-function tasksEqual(a, b) {
- return isDeepStrictEqual(normalizeTask(a), normalizeTask(b));
-}
-
-/**
- * Diff two task arrays to find changes
- */
-function diffTasks(oldTasks, newTasks) {
- const oldMap = new Map(oldTasks.map(t => [t.id, t]));
- const newMap = new Map(newTasks.map(t => [t.id, t]));
-
- const added = [];
- const completed = [];
- const modified = [];
- const removed = [];
-
- // Find added and modified tasks
- for (const [id, task] of newMap) {
- const oldTask = oldMap.get(id);
-
- if (!oldTask) {
- added.push(task);
- } else if (oldTask.status !== 'completed' && task.status === 'completed') {
- completed.push(task);
- } else if (!tasksEqual(oldTask, task)) {
- modified.push({ old: oldTask, new: task });
- }
- }
-
- // Find removed tasks
- for (const [id, task] of oldMap) {
- if (!newMap.has(id)) {
- removed.push(task);
- }
- }
-
- return { added, completed, modified, removed };
-}
-
-/**
- * Get watcher status
- */
-export function getWatcherStatus() {
- return {
- watching: isWatching,
- lastUserTasks: lastUserTasks?.tasks?.length ?? 0,
- lastCosTasks: lastCosTasks?.tasks?.length ?? 0
- };
-}
-
-/**
- * Force refresh of cached tasks
- */
-export async function refreshTasks() {
- lastUserTasks = await getUserTasks();
- lastCosTasks = await getCosTasks();
-
- cosEvents.emit('tasks:refreshed', {
- user: lastUserTasks,
- cos: lastCosTasks
- });
-
- return { user: lastUserTasks, cos: lastCosTasks };
-}
diff --git a/server/services/taskWatcher.test.js b/server/services/taskWatcher.test.js
deleted file mode 100644
index 8df0e3e49d..0000000000
--- a/server/services/taskWatcher.test.js
+++ /dev/null
@@ -1,186 +0,0 @@
-import { EventEmitter } from 'events';
-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-
-const mocks = vi.hoisted(() => ({
- watch: vi.fn(),
- emit: vi.fn(),
- getUserTasks: vi.fn(),
- getCosTasks: vi.fn(),
- getConfig: vi.fn(),
-}));
-
-vi.mock('chokidar', () => ({ watch: (...args) => mocks.watch(...args) }));
-vi.mock('./cos.js', () => ({
- cosEvents: { emit: (...args) => mocks.emit(...args) },
- getUserTasks: (...args) => mocks.getUserTasks(...args),
- getCosTasks: (...args) => mocks.getCosTasks(...args),
- getConfig: (...args) => mocks.getConfig(...args),
-}));
-
-const { getWatcherStatus, startWatching, stopWatching } = await import('./taskWatcher.js');
-
-class FakeWatcher extends EventEmitter {
- close = vi.fn(async () => {});
-}
-
-const deferred = () => {
- let resolve;
- const promise = new Promise((r) => { resolve = r; });
- return { promise, resolve };
-};
-
-beforeEach(() => {
- vi.clearAllMocks();
- mocks.getConfig.mockResolvedValue({ userTasksFile: 'TASKS.md', cosTasksFile: 'COS-TASKS.md' });
- mocks.getUserTasks.mockResolvedValue({ tasks: [] });
- mocks.getCosTasks.mockResolvedValue({ tasks: [] });
-});
-
-afterEach(async () => {
- if (getWatcherStatus().watching) await stopWatching();
- vi.restoreAllMocks();
-});
-
-describe('taskWatcher event queue', () => {
- it('serializes callbacks for one file and waits for the tail during stop', async () => {
- const watcher = new FakeWatcher();
- const first = deferred();
- const order = [];
- mocks.watch.mockReturnValue(watcher);
- mocks.getUserTasks
- .mockResolvedValueOnce({ tasks: [] })
- .mockImplementationOnce(async () => {
- order.push('first:start');
- await first.promise;
- order.push('first:end');
- return { tasks: [{ id: 'a', status: 'pending' }] };
- })
- .mockImplementationOnce(async () => {
- order.push('second:start');
- return { tasks: [{ id: 'a', status: 'completed' }] };
- });
-
- await startWatching();
- watcher.emit('change', '/repo/TASKS.md');
- watcher.emit('change', '/repo/TASKS.md');
- await Promise.resolve();
- await Promise.resolve();
-
- expect(order).toEqual(['first:start']);
- let stopped = false;
- const stopping = stopWatching().then(() => { stopped = true; });
- await Promise.resolve();
- expect(stopped).toBe(false);
-
- first.resolve();
- await stopping;
-
- expect(order).toEqual(['first:start', 'first:end', 'second:start']);
- expect(watcher.close).toHaveBeenCalledOnce();
- });
-
- it('catches a failed callback and continues the same file lane', async () => {
- const watcher = new FakeWatcher();
- const log = vi.spyOn(console, 'error').mockImplementation(() => {});
- mocks.watch.mockReturnValue(watcher);
- mocks.getUserTasks
- .mockResolvedValueOnce({ tasks: [] })
- .mockRejectedValueOnce(new Error('read failed'))
- .mockResolvedValueOnce({ tasks: [{ id: 'a', status: 'pending' }] });
-
- await startWatching();
- watcher.emit('change', '/repo/TASKS.md');
- watcher.emit('change', '/repo/TASKS.md');
- await stopWatching();
-
- expect(mocks.getUserTasks).toHaveBeenCalledTimes(3);
- expect(log).toHaveBeenCalledWith(expect.stringContaining('read failed'));
- expect(mocks.emit).toHaveBeenCalledWith('watcher:error', expect.objectContaining({
- error: 'read failed',
- event: 'change',
- file: '/repo/TASKS.md',
- }));
- });
-});
-
-describe('taskWatcher change detection', () => {
- const bigPrompt = 'p'.repeat(17408);
- const makeTasks = (n) => Array.from({ length: n }, (_, i) => ({
- id: `t${i}`, status: 'completed', description: `task ${i}`, priority: 'MEDIUM',
- metadata: { prompt: bigPrompt, tags: ['a', { b: null }] },
- }));
-
- const runChange = async (before, after, file = '/repo/TASKS.md') => {
- const watcher = new FakeWatcher();
- mocks.watch.mockReturnValue(watcher);
- mocks.getUserTasks
- .mockResolvedValueOnce({ tasks: before })
- .mockResolvedValueOnce({ tasks: after });
- await startWatching();
- watcher.emit('change', file);
- await stopWatching();
- return mocks.emit.mock.calls.filter(([name]) => !['tasks:user:changed', 'watcher:started', 'watcher:stopped'].includes(name));
- };
-
- it('emits only the one modified task in a large prompt-heavy history without JSON-encoding it', async () => {
- const before = makeTasks(4000);
- const after = before.map((t) => ({ ...t, metadata: { ...t.metadata } }));
- after[1234] = { ...after[1234], description: 'changed' };
- const stringify = vi.spyOn(JSON, 'stringify');
- const events = await runChange(before, after);
- const bigEncodings = stringify.mock.results.filter((r) => typeof r.value === 'string' && r.value.length >= bigPrompt.length).length;
- expect(bigEncodings).toBe(0);
- expect(events).toEqual([['tasks:user:modified', { tasks: [{ old: before[1234], new: after[1234] }] }]]);
- });
-
- it('detects nested metadata changes but ignores key order and undefined entries', async () => {
- const base = { id: 'a', status: 'pending', description: 'd', metadata: { x: 1, y: [1, { z: null }], gone: undefined } };
- const same = { ...base, metadata: { y: [1, { z: null }], x: 1 } };
- expect(await runChange([base], [same])).toEqual([]);
- vi.clearAllMocks();
- mocks.getConfig.mockResolvedValue({ userTasksFile: 'TASKS.md', cosTasksFile: 'COS-TASKS.md' });
- const nested = { ...base, metadata: { x: 1, y: [1, { z: 0 }] } };
- const events = await runChange([base], [nested]);
- expect(events.map(([n]) => n)).toEqual(['tasks:user:modified']);
- });
-
- it('keeps completed, pending-revival, added and removed event protocol and order', async () => {
- const before = [
- { id: 'c', status: 'pending' }, { id: 'r', status: 'completed' }, { id: 'gone', status: 'pending' },
- ];
- const after = [
- { id: 'c', status: 'completed' }, { id: 'r', status: 'pending' }, { id: 'new', status: 'pending' },
- ];
- const events = await runChange(before, after);
- expect(events.map(([n]) => n)).toEqual([
- 'tasks:user:added', 'tasks:user:completed', 'tasks:user:modified', 'tasks:user:added', 'tasks:user:removed',
- ]);
- });
-});
-
-describe('taskWatcher start/stop lifecycle', () => {
- it('coalesces overlapping starts into one watcher that one stop closes', async () => {
- const watcher = new FakeWatcher();
- mocks.watch.mockReturnValue(watcher);
- const results = await Promise.all([startWatching(), startWatching()]);
- expect(results).toEqual([{ success: true }, { success: true }]);
- expect(mocks.watch).toHaveBeenCalledTimes(1);
- expect(await startWatching()).toEqual({ success: false, error: 'Already watching' });
- expect(await stopWatching()).toEqual({ success: true });
- expect(watcher.close).toHaveBeenCalledTimes(1);
- });
-
- it('stop issued during start waits for it and closes the created watcher', async () => {
- const watcher = new FakeWatcher();
- const gate = deferred();
- mocks.watch.mockReturnValue(watcher);
- mocks.getUserTasks.mockImplementationOnce(async () => { await gate.promise; return { tasks: [] }; });
- const starting = startWatching();
- const stopping = stopWatching();
- gate.resolve();
- await starting;
- expect(await stopping).toEqual({ success: true });
- expect(getWatcherStatus().watching).toBe(false);
- expect(watcher.close).toHaveBeenCalledTimes(1);
- });
-});