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

perf: bound bare GET /api/cos/tasks to non-completed tasks plus the first completed page (#9594)

[._.]/ Adam Eivyb7986c1395b8
diff --git a/docs/API.md b/docs/API.md
index af562ff911..d37949e47e 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -242,7 +242,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` | Get all tasks |
+| GET | `/cos/tasks` | Bounded task list: all non-completed tasks plus the first 25 completed (`completedCount`, `completedNextCursor`); `view=queue`, `view=completed` (paged), `view=full` (raw unbounded store) |
| POST | `/cos/evaluate` | Force task evaluation |
| GET | `/cos/health` | Get health status |
| POST | `/cos/health/check` | Run health check |
diff --git a/docs/features/chief-of-staff.md b/docs/features/chief-of-staff.md
index cdea78bda9..eeadf6dbe8 100644
--- a/docs/features/chief-of-staff.md
+++ b/docs/features/chief-of-staff.md
@@ -129,7 +129,7 @@ Storage, retention and the privacy posture are in [STORAGE.md](../STORAGE.md).
| POST /api/cos/start | Start daemon |
| POST /api/cos/stop | Stop daemon |
| GET/PUT /api/cos/config | Configuration |
-| GET /api/cos/tasks | Get all tasks |
+| GET /api/cos/tasks | Get tasks (bounded: all non-completed + first 25 completed with `completedCount`; `?view=queue`, `?view=completed`, `?view=full` for the raw store) |
| POST /api/cos/evaluate | Force evaluation |
| GET /api/cos/health | Health status |
| POST /api/cos/health/check | Run health check |
diff --git a/server/routes/cos.test.js b/server/routes/cos.test.js
index 4da90275de..27d909c2ac 100644
--- a/server/routes/cos.test.js
+++ b/server/routes/cos.test.js
@@ -323,6 +323,23 @@ describe('CoS Routes', () => {
});
describe('GET /api/cos/tasks', () => {
+ it('bounds the bare form on a large completed history and keeps view=full raw', async () => {
+ const active = { id: 'active', status: 'pending' };
+ const history = Array.from({ length: 2000 }, (_, i) => ({ id: `done-${String(i).padStart(5, '0')}`, status: 'completed', metadata: { prompt: 'x'.repeat(2000) } }));
+ cos.getAllTasks.mockResolvedValue({ user: { tasks: [active, ...history], grouped: { pending: [active], completed: history } }, cos: null });
+ cos.getAgents.mockResolvedValue([]);
+ const bare = await request(app).get('/api/cos/tasks');
+ expect(bare.status).toBe(200);
+ expect(JSON.stringify(bare.body).length).toBeLessThan(1_000_000);
+ expect(bare.body.user.tasks).toHaveLength(26);
+ expect(bare.body.user.tasks[0]).toMatchObject({ id: 'active' });
+ expect(bare.body.user).toMatchObject({ completedCount: 2000, grouped: { pending: [active], completed: [] } });
+ expect(bare.body.user.completedNextCursor).toBe(bare.body.user.tasks[25].id);
+ const full = await request(app).get('/api/cos/tasks?view=full');
+ expect(full.body.user.tasks).toHaveLength(2001);
+ expect(full.body.user.grouped.completed).toHaveLength(2000);
+ });
+
it('should return all tasks', async () => {
const mockTasks = {
user: { tasks: [], grouped: {} },
diff --git a/server/routes/cosTaskRoutes.js b/server/routes/cosTaskRoutes.js
index 6318cdf34b..97a063e01f 100644
--- a/server/routes/cosTaskRoutes.js
+++ b/server/routes/cosTaskRoutes.js
@@ -132,6 +132,8 @@ async function preparePlanOnlyTask(taskData, knownApp = null) {
const router = Router();
+const COMPLETED_FIRST_PAGE = 25;
+
// GET /api/cos/tasks - Get all tasks (user + internal), grouped by source.
//
// Settled for the spawn window (lib/cosSpawnWindow.js): an agent registers as
@@ -143,9 +145,10 @@ const router = Router();
// exists. `cos.getAgents()` is free at this point — `getAllTasks()` has already
// warmed the same `loadState()` snapshot it reads.
//
-// Backward-compatible by default: with no pagination params it returns the full
-// `{ user, cos }` structure every existing consumer expects (tasks + grouped
-// buckets + awaiting/auto-approved derived lists). When a client passes
+// With no params it returns a bounded `{ user, cos }` projection: all non-completed
+// tasks plus grouped/derived lists, and only the first page of completed history
+// (+ `completedCount`/`completedNextCursor`; page on with `view=completed`).
+// `view=full` returns the raw unbounded store. When a client passes
// `limit`/`offset`, each source is reduced to a *genuinely bounded* shape: the
// windowed `tasks` slice plus scalar metadata only. The full-set derived
// collections (`grouped`, `autoApproved`, `awaitingApproval`) are dropped from
@@ -154,7 +157,7 @@ const router = Router();
// the true per-source totals is added so the caller can page.
router.get('/tasks', asyncHandler(async (req, res) => {
const query = validateRequest(z.object({
- view: z.enum(['queue', 'completed']).optional(),
+ view: z.enum(['queue', 'completed', 'full']).optional(),
source: z.enum(['user', 'internal']).default('user'),
selected: z.string().max(512).optional(),
cursor: z.string().max(512).optional(),
@@ -190,7 +193,23 @@ router.get('/tasks', asyncHandler(async (req, res) => {
return res.json({ user: queueSource(tasks.user), cos: queueSource(tasks.cos) });
}
if (!isPaginationRequested(req.query)) {
- return res.json(tasks);
+ if (query.view === 'full') return res.json(tasks);
+ // Bare form: bounded projection. Every non-completed task stays whole; the
+ // completed history (96% of the bytes on a long-lived install, and once
+ // more inside `grouped`) shrinks to the first `view=completed` page plus
+ // `completedCount`. `view=full` is the raw-store escape hatch.
+ const boundSource = source => {
+ if (!source) return source;
+ const isDone = task => task.status === 'completed';
+ const list = source.tasks || [];
+ const firstPage = list.filter(isDone).sort((a, b) => a.id < b.id ? 1 : a.id > b.id ? -1 : 0).slice(0, COMPLETED_FIRST_PAGE);
+ const completedCount = list.filter(isDone).length;
+ return { ...source, tasks: [...list.filter(task => !isDone(task)), ...firstPage], completedCount,
+ completedNextCursor: completedCount > COMPLETED_FIRST_PAGE ? firstPage[firstPage.length - 1].id : null,
+ grouped: Object.fromEntries(Object.entries(source.grouped || {}).map(([key, rows]) => [key, rows.filter(task => !isDone(task))])),
+ autoApproved: source.autoApproved?.filter(task => !isDone(task)), awaitingApproval: source.awaitingApproval?.filter(task => !isDone(task)) };
+ };
+ return res.json({ ...tasks, user: boundSource(tasks.user), cos: boundSource(tasks.cos) });
}
const { limit, offset } = parsePagination(req.query, { defaultLimit: 50, maxLimit: 500 });
const sliceSource = (src) => {