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([brain-bucket-chip-reorder]): tighten reorder schema to UUIDs + reject unknown ids

[._.]/ Adam Eivy8e2ad71e3c8a
diff --git a/server/lib/brainValidation.js b/server/lib/brainValidation.js
index dd1fd6f4d0..c73d4d97b2 100644
--- a/server/lib/brainValidation.js
+++ b/server/lib/brainValidation.js
@@ -437,8 +437,8 @@ export const bucketReorderSchema = z.object({
// concurrent single-record PUTs can.
export const linkReorderSchema = z.object({
updates: z.array(z.object({
- id: z.string().min(1),
- bucketId: z.string().min(1).nullable(),
+ id: z.string().uuid(),
+ bucketId: z.string().uuid().nullable(),
bucketOrder: z.number().int()
})).min(1)
});
diff --git a/server/lib/brainValidation.test.js b/server/lib/brainValidation.test.js
index 7b4123e8cd..d6c2ea746b 100644
--- a/server/lib/brainValidation.test.js
+++ b/server/lib/brainValidation.test.js
@@ -616,18 +616,22 @@ describe('brainValidation.js', () => {
});
describe('linkReorderSchema', () => {
+ const idA = '11111111-1111-4111-8111-111111111111';
+ const idB = '22222222-2222-4222-8222-222222222222';
+ const bucket = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';
+
it('should accept a batch of { id, bucketId, bucketOrder }', () => {
const result = linkReorderSchema.safeParse({
updates: [
- { id: 'l1', bucketId: 'b1', bucketOrder: 0 },
- { id: 'l2', bucketId: 'b1', bucketOrder: 1 }
+ { id: idA, bucketId: bucket, bucketOrder: 0 },
+ { id: idB, bucketId: bucket, bucketOrder: 1 }
]
});
expect(result.success).toBe(true);
});
it('should accept a null bucketId (ungrouped landing)', () => {
- expect(linkReorderSchema.safeParse({ updates: [{ id: 'l1', bucketId: null, bucketOrder: 0 }] }).success).toBe(true);
+ expect(linkReorderSchema.safeParse({ updates: [{ id: idA, bucketId: null, bucketOrder: 0 }] }).success).toBe(true);
});
it('should reject an empty batch', () => {
@@ -635,11 +639,19 @@ describe('brainValidation.js', () => {
});
it('should reject a non-integer bucketOrder', () => {
- expect(linkReorderSchema.safeParse({ updates: [{ id: 'l1', bucketId: 'b1', bucketOrder: 1.5 }] }).success).toBe(false);
+ expect(linkReorderSchema.safeParse({ updates: [{ id: idA, bucketId: bucket, bucketOrder: 1.5 }] }).success).toBe(false);
+ });
+
+ it('should reject a non-uuid id (parity with the link update schema)', () => {
+ expect(linkReorderSchema.safeParse({ updates: [{ id: 'l1', bucketId: bucket, bucketOrder: 0 }] }).success).toBe(false);
+ });
+
+ it('should reject a non-uuid bucketId', () => {
+ expect(linkReorderSchema.safeParse({ updates: [{ id: idA, bucketId: 'b1', bucketOrder: 0 }] }).success).toBe(false);
});
it('should reject an entry missing its id', () => {
- expect(linkReorderSchema.safeParse({ updates: [{ bucketId: 'b1', bucketOrder: 0 }] }).success).toBe(false);
+ expect(linkReorderSchema.safeParse({ updates: [{ bucketId: bucket, bucketOrder: 0 }] }).success).toBe(false);
});
});
diff --git a/server/routes/brain.js b/server/routes/brain.js
index 1699cdb3ac..a7bf44fd3a 100644
--- a/server/routes/brain.js
+++ b/server/routes/brain.js
@@ -540,6 +540,18 @@ router.get('/links', asyncHandler(async (req, res) => {
*/
router.post('/links/reorder', asyncHandler(async (req, res) => {
const { updates } = validateRequest(linkReorderSchema, req.body);
+ // All-or-nothing: reject before any write if a batch references a link that
+ // no longer exists, so the response can't report success after a partial
+ // apply (mirrors the single-link PUT's 404 on an unknown id).
+ const known = new Set((await brainService.getLinks()).map(l => l.id));
+ const missing = updates.filter(u => !known.has(u.id)).map(u => u.id);
+ if (missing.length) {
+ throw new ServerError('Unknown link id in reorder batch', {
+ status: 404,
+ code: 'NOT_FOUND',
+ context: { missing }
+ });
+ }
const links = await brainService.reorderLinks(updates);
res.json({ links });
}));
diff --git a/server/routes/brain.test.js b/server/routes/brain.test.js
index 9d48646cd3..f20deec783 100644
--- a/server/routes/brain.test.js
+++ b/server/routes/brain.test.js
@@ -1247,11 +1247,16 @@ describe('Brain Routes', () => {
});
describe('POST /api/brain/links/reorder', () => {
+ const idA = '11111111-1111-4111-8111-111111111111';
+ const idB = '22222222-2222-4222-8222-222222222222';
+ const bucket = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';
+
it('applies the whole batch in one atomic reorderLinks call', async () => {
const updates = [
- { id: 'l2', bucketId: 'b1', bucketOrder: 0 },
- { id: 'l1', bucketId: 'b1', bucketOrder: 1 }
+ { id: idB, bucketId: bucket, bucketOrder: 0 },
+ { id: idA, bucketId: bucket, bucketOrder: 1 }
];
+ brainService.getLinks.mockResolvedValue([{ id: idA }, { id: idB }]);
brainService.reorderLinks.mockResolvedValue(updates.map(u => ({ ...u, url: 'x' })));
const res = await request(app).post('/api/brain/links/reorder').send({ updates });
expect(res.status).toBe(200);
@@ -1268,11 +1273,24 @@ describe('Brain Routes', () => {
expect(brainService.reorderLinks).not.toHaveBeenCalled();
});
+ it('rejects the whole batch (no write) when any id is unknown', async () => {
+ brainService.getLinks.mockResolvedValue([{ id: idA }]); // idB no longer exists
+ const res = await request(app).post('/api/brain/links/reorder').send({
+ updates: [
+ { id: idA, bucketId: bucket, bucketOrder: 0 },
+ { id: idB, bucketId: bucket, bucketOrder: 1 }
+ ]
+ });
+ expect(res.status).toBe(404);
+ expect(brainService.reorderLinks).not.toHaveBeenCalled();
+ });
+
it('is matched before /links/:id so "reorder" is not treated as an id', async () => {
+ brainService.getLinks.mockResolvedValue([{ id: idA }]);
brainService.reorderLinks.mockResolvedValue([]);
const res = await request(app)
.post('/api/brain/links/reorder')
- .send({ updates: [{ id: 'l1', bucketId: 'b1', bucketOrder: 0 }] });
+ .send({ updates: [{ id: idA, bucketId: bucket, bucketOrder: 0 }] });
expect(res.status).toBe(200);
expect(brainService.reorderLinks).toHaveBeenCalledTimes(1);
});