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

address review (round 17): lock ID regex to fixed widths across server, client, and tests

Adam Eivy6abacf74fa4e
diff --git a/client/src/pages/Ask.jsx b/client/src/pages/Ask.jsx
index cabd071d05..e32277d2dd 100644
--- a/client/src/pages/Ask.jsx
+++ b/client/src/pages/Ask.jsx
@@ -4,10 +4,11 @@ import { Send, Loader2, MessageCircle, Trash2, Pin, Brain, Calendar, Target, Boo
import * as api from '../services/api';
import toast from '../components/ui/Toast';
-// Mirrors the server-side ID_RE in askConversations.js — used to skip
-// forwarding the optimistic 'pending' placeholder (which would 400) while
-// still letting any real persisted id pass through.
-const CONV_ID_RE = /^ask_[a-z0-9]+_[a-f0-9]+$/;
+// Mirrors the server-side ID_RE in askConversations.js (9-char base36 ms +
+// 8-char hex suffix) so this regex stays in lockstep with the production
+// generator. Used to skip forwarding the optimistic 'pending' placeholder
+// (which would 400) while still letting any real persisted id pass through.
+const CONV_ID_RE = /^ask_[a-z0-9]{9}_[a-f0-9]{8}$/;
export const ASK_MODES = [
{ id: 'ask', label: 'Ask', help: 'Answer as yourself, grounded in your own notes and goals.' },
diff --git a/server/routes/ask.js b/server/routes/ask.js
index 613ba6f646..88de495cbd 100644
--- a/server/routes/ask.js
+++ b/server/routes/ask.js
@@ -17,6 +17,7 @@ import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
import * as convs from '../services/askConversations.js';
import { runAsk, VALID_MODES } from '../services/askService.js';
+import { ID_RE as CONV_ID_RE } from '../services/askConversations.js';
const router = Router();
@@ -25,7 +26,11 @@ const router = Router();
// against multi-turn token blowup.
const PROMPT_HISTORY_TURNS = 12;
-const idSchema = z.string().regex(/^ask_[a-z0-9]+_[a-f0-9]+$/, 'invalid conversation id');
+// Sourced from `askConversations.ID_RE` so the route, the storage layer, and
+// the generator are guaranteed to agree on the canonical id shape — variable-
+// width regexes here let non-canonical ids through and break listConversations'
+// lexical-sort = chronological-sort invariant.
+const idSchema = z.string().regex(CONV_ID_RE, 'invalid conversation id');
const askBodySchema = z.object({
conversationId: idSchema.optional(),
diff --git a/server/routes/ask.test.js b/server/routes/ask.test.js
index c10f67b225..28bf7aecd4 100644
--- a/server/routes/ask.test.js
+++ b/server/routes/ask.test.js
@@ -10,6 +10,10 @@ vi.mock('../services/askConversations.js', () => ({
appendTurn: vi.fn(),
deleteConversation: vi.fn(),
setPromoted: vi.fn(),
+ // The route's idSchema is built from this regex — keep it in lockstep with
+ // the real export (9-char base36 ms + 8-char hex suffix) so route-level
+ // validation in tests matches production.
+ ID_RE: /^ask_[a-z0-9]{9}_[a-f0-9]{8}$/,
}));
vi.mock('../services/askService.js', () => ({
@@ -38,7 +42,7 @@ beforeEach(() => {
describe('GET /api/ask', () => {
it('returns a list of conversation summaries', async () => {
convs.listConversations.mockResolvedValue([
- { id: 'ask_a_b', title: 'Old', mode: 'ask', turnCount: 2, createdAt: 'x', updatedAt: 'x', promoted: false },
+ { id: 'ask_lwg2x4abc_abcdef12', title: 'Old', mode: 'ask', turnCount: 2, createdAt: 'x', updatedAt: 'x', promoted: false },
]);
const res = await request(makeApp()).get('/api/ask');
expect(res.status).toBe(200);
@@ -49,47 +53,47 @@ describe('GET /api/ask', () => {
describe('GET /api/ask/:id', () => {
it('404s for missing conversation', async () => {
convs.getConversation.mockResolvedValue(null);
- const res = await request(makeApp()).get('/api/ask/ask_x_dead');
+ const res = await request(makeApp()).get('/api/ask/ask_lwg2x4abc_deadbeef');
expect(res.status).toBe(404);
});
it('returns the conversation', async () => {
- convs.getConversation.mockResolvedValue({ id: 'ask_x_a1', turns: [], mode: 'ask' });
- const res = await request(makeApp()).get('/api/ask/ask_x_a1');
+ convs.getConversation.mockResolvedValue({ id: 'ask_lwg2x4abc_a1b2c3d4', turns: [], mode: 'ask' });
+ const res = await request(makeApp()).get('/api/ask/ask_lwg2x4abc_a1b2c3d4');
expect(res.status).toBe(200);
- expect(res.body.conversation.id).toBe('ask_x_a1');
+ expect(res.body.conversation.id).toBe('ask_lwg2x4abc_a1b2c3d4');
});
});
describe('DELETE /api/ask/:id', () => {
it('deletes when the service confirms', async () => {
convs.deleteConversation.mockResolvedValue(true);
- const res = await request(makeApp()).delete('/api/ask/ask_x_a1');
+ const res = await request(makeApp()).delete('/api/ask/ask_lwg2x4abc_a1b2c3d4');
expect(res.status).toBe(200);
expect(res.body.ok).toBe(true);
});
it('404s when nothing was removed', async () => {
convs.deleteConversation.mockResolvedValue(false);
- const res = await request(makeApp()).delete('/api/ask/ask_x_a1');
+ const res = await request(makeApp()).delete('/api/ask/ask_lwg2x4abc_a1b2c3d4');
expect(res.status).toBe(404);
});
});
describe('POST /api/ask/:id/promote', () => {
it('promotes the conversation', async () => {
- convs.setPromoted.mockResolvedValue({ id: 'ask_x_a1', promoted: true });
+ convs.setPromoted.mockResolvedValue({ id: 'ask_lwg2x4abc_a1b2c3d4', promoted: true });
const res = await request(makeApp())
- .post('/api/ask/ask_x_a1/promote')
+ .post('/api/ask/ask_lwg2x4abc_a1b2c3d4/promote')
.send({ promoted: true });
expect(res.status).toBe(200);
- expect(convs.setPromoted).toHaveBeenCalledWith('ask_x_a1', true);
+ expect(convs.setPromoted).toHaveBeenCalledWith('ask_lwg2x4abc_a1b2c3d4', true);
});
it('404s when the conversation does not exist', async () => {
convs.setPromoted.mockResolvedValue(null);
const res = await request(makeApp())
- .post('/api/ask/ask_x_a1/promote')
+ .post('/api/ask/ask_lwg2x4abc_a1b2c3d4/promote')
.send({ promoted: true });
expect(res.status).toBe(404);
});
@@ -121,14 +125,14 @@ describe('POST /api/ask (validation)', () => {
convs.getConversation.mockResolvedValue(null);
const res = await request(makeApp())
.post('/api/ask')
- .send({ question: 'hi', conversationId: 'ask_x_aaaaaa' });
+ .send({ question: 'hi', conversationId: 'ask_lwg2x4abc_aaaaaaaa' });
expect(res.status).toBe(404);
});
});
describe('POST /api/ask (streaming happy path)', () => {
it('streams open → sources → delta → done as SSE', async () => {
- convs.createConversation.mockResolvedValue({ id: 'ask_new_abcdef', mode: 'ask', turns: [] });
+ convs.createConversation.mockResolvedValue({ id: 'ask_lwgnewabc_abcdef00', mode: 'ask', turns: [] });
convs.appendTurn.mockImplementation((id, turn) => Promise.resolve({
conversation: { id, mode: 'ask', turns: [{ ...turn, id: 'tid' }] },
turn: { ...turn, id: 'tid' },
@@ -151,7 +155,7 @@ describe('POST /api/ask (streaming happy path)', () => {
// by string matching rather than parsing each frame.
const body = res.text || '';
expect(body).toContain('event: open');
- expect(body).toContain('"conversationId":"ask_new_abcdef"');
+ expect(body).toContain('"conversationId":"ask_lwgnewabc_abcdef00"');
expect(body).toContain('event: sources');
expect(body).toContain('event: delta');
expect(body).toContain('"text":"Hi "');
diff --git a/server/services/askConversations.js b/server/services/askConversations.js
index a32eea6dfe..a6b8e3500e 100644
--- a/server/services/askConversations.js
+++ b/server/services/askConversations.js
@@ -37,13 +37,16 @@ export const TITLE_MAX_LENGTH = 120;
// the retrieval/provider stack via askService.
export const VALID_MODES = new Set(['ask', 'advise', 'draft']);
-const ID_RE = /^ask_[a-z0-9]+_[a-f0-9]+$/;
-
// Pad base36-ms to 9 chars (35^9 > 5e13, valid until year ~3700) so
// filename lexical order always matches chronological order — without
// padding, a digit rollover would make a newer id sort *before* an older
-// one and the newest-first listing would silently flip.
+// one and the newest-first listing would silently flip. The hex suffix is
+// the first 8 chars of a UUID. The regex is locked to those exact widths
+// so non-canonical ids (which would land on disk with the wrong sort key)
+// never pass validation at any layer.
const BASE36_TS_WIDTH = 9;
+const HEX_SUFFIX_WIDTH = 8;
+export const ID_RE = new RegExp(`^ask_[a-z0-9]{${BASE36_TS_WIDTH}}_[a-f0-9]{${HEX_SUFFIX_WIDTH}}$`);
function generateId() {
const ts = Date.now().toString(36).padStart(BASE36_TS_WIDTH, '0');
diff --git a/server/services/askConversations.test.js b/server/services/askConversations.test.js
index 6540910660..a5a888591d 100644
--- a/server/services/askConversations.test.js
+++ b/server/services/askConversations.test.js
@@ -35,7 +35,7 @@ describe('askConversations', () => {
it('accepts a canonical literal id', () => {
// Literal so the regex breaks if the format changes silently —
// generator round-trips alone wouldn't catch a regex relaxation.
- expect(convs.isValidId('ask_lwg2x4_abcdef12')).toBe(true);
+ expect(convs.isValidId('ask_lwg2x4abc_abcdef12')).toBe(true);
});
it('accepts ids produced by the generator', () => {
@@ -111,7 +111,7 @@ describe('askConversations', () => {
});
it('rejects unknown conversation ids', async () => {
- await expect(convs.appendTurn('ask_zzz_dead', { role: 'user', content: 'x' })).rejects.toThrow(/not found/);
+ await expect(convs.appendTurn('ask_zzzzzzzzz_deadbeef', { role: 'user', content: 'x' })).rejects.toThrow(/not found/);
});
});