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

fix: validate and cap POST /api/memory/decay decayRate (#9186)

[._.]/ Adam Eivyaccac0ac9c6a
diff --git a/docs/API.md b/docs/API.md
index 9fec179084..61a989e1ef 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -329,7 +329,7 @@ Context tools remain read-only. Semantic reads and writes are independent, defau
| GET | `/memory/stats` | Memory statistics |
| POST | `/memory/link` | Link two memories |
| POST | `/memory/consolidate` | Merge similar memories |
-| POST | `/memory/decay` | Apply importance decay |
+| POST | `/memory/decay` | Apply importance decay (`decayRate` in (0, 0.02], default 0.01; else 400) |
| DELETE | `/memory/expired` | Clear expired memories |
| GET | `/memory/embeddings/status` | LM Studio connection status |
diff --git a/docs/features/memory-system.md b/docs/features/memory-system.md
index c0ef145494..9d7c7c5b99 100644
--- a/docs/features/memory-system.md
+++ b/docs/features/memory-system.md
@@ -162,7 +162,7 @@ Before agent task execution:
| GET /api/memory/stats | Memory statistics |
| POST /api/memory/link | Link two memories |
| POST /api/memory/consolidate | Merge similar memories |
-| POST /api/memory/decay | Apply importance decay |
+| POST /api/memory/decay | Apply importance decay (`decayRate` in (0, 0.02], default 0.01; else 400) |
| DELETE /api/memory/expired | Clear expired memories |
| GET /api/memory/embeddings/status | LM Studio connection status |
| GET /api/memory/backend/status | Active storage backend status |
diff --git a/server/lib/memoryValidation.js b/server/lib/memoryValidation.js
index 3db5770833..0cd16f02f0 100644
--- a/server/lib/memoryValidation.js
+++ b/server/lib/memoryValidation.js
@@ -178,6 +178,11 @@ export const memoryConsolidateSchema = z.object({
dryRun: z.boolean().optional().default(false)
});
+// Decay request schema — 0.02 is 2x the shipped default; higher rates purge rather than tune
+export const memoryDecaySchema = z.object({
+ decayRate: z.number().positive().max(0.02).optional().default(0.01)
+}).strict();
+
// Link memories request schema
export const memoryLinkSchema = z.object({
sourceId: z.string().guid(),
diff --git a/server/routes/memory.js b/server/routes/memory.js
index 28a3675b16..c6dea7d778 100644
--- a/server/routes/memory.js
+++ b/server/routes/memory.js
@@ -17,6 +17,7 @@ import {
memoryListSchema,
memoryTimelineSchema,
memoryConsolidateSchema,
+ memoryDecaySchema,
memoryLinkSchema,
memorySyncSchema,
memoryIdParamSchema
@@ -151,7 +152,7 @@ router.post('/link', asyncHandler(async (req, res) => {
// POST /api/memory/decay - Apply importance decay
router.post('/decay', asyncHandler(async (req, res) => {
- const decayRate = parseFloat(req.body.decayRate) || 0.01;
+ const { decayRate } = validateRequest(memoryDecaySchema, req.body ?? {});
const result = await memory.applyDecay(decayRate);
res.json(result);
}));
diff --git a/server/routes/memory.test.js b/server/routes/memory.test.js
index af43207692..41b7f3389b 100644
--- a/server/routes/memory.test.js
+++ b/server/routes/memory.test.js
@@ -45,7 +45,7 @@ vi.mock('../services/memorySync.js', () => ({
applyRemoteChanges: vi.fn()
}));
-import { ensureBackend, getMemories, getTimeline, deleteMemory } from '../services/memoryBackend.js';
+import { ensureBackend, getMemories, getTimeline, deleteMemory, applyDecay } from '../services/memoryBackend.js';
import { checkHealth } from '../lib/db.js';
import * as memorySync from '../services/memorySync.js';
@@ -386,4 +386,30 @@ describe('Memory Routes', () => {
expect(getTimeline).not.toHaveBeenCalled();
});
});
+
+ describe('POST /api/memory/decay', () => {
+ it.each([[{ decayRate: 5 }], [{ decayRate: -1 }], [{ decayRate: 'abc' }], [{ decayRate: 0 }], [{ decayRate: null }]])(
+ 'rejects %j without applying decay', async (body) => {
+ const response = await request(app).post('/api/memory/decay').send(body);
+ expect(response.status).toBe(400);
+ expect(response.body.code).toBe('VALIDATION_ERROR');
+ expect(applyDecay).not.toHaveBeenCalled();
+ });
+
+ it('applies the default rate for an empty object or no body', async () => {
+ applyDecay.mockResolvedValue({});
+ expect((await request(app).post('/api/memory/decay').send({})).status).toBe(200);
+ expect((await request(app).post('/api/memory/decay')).status).toBe(200);
+ expect(applyDecay).toHaveBeenCalledTimes(2);
+ expect(applyDecay).toHaveBeenNthCalledWith(1, 0.01);
+ expect(applyDecay).toHaveBeenNthCalledWith(2, 0.01);
+ });
+
+ it('accepts the maximum rate', async () => {
+ applyDecay.mockResolvedValue({});
+ const response = await request(app).post('/api/memory/decay').send({ decayRate: 0.02 });
+ expect(response.status).toBe(200);
+ expect(applyDecay).toHaveBeenCalledWith(0.02);
+ });
+ });
});