feat(backend): bound the messages kept per project

A project that lives for months would otherwise grow the SQLite file without
limit, even though the model only ever reads the most recent messages. Pruning
the oldest rows on every write keeps a project to a fixed budget without
touching what the context is built from.
This commit is contained in:
BlackAngelTV committed 2026-10-05 21:39:45 +02:00
1 parent c1d6593c35
commit 6ebf550eb0
6 files changed
+150 -5

No files matched your search

+4 -1
View File
@@ -19,9 +19,12 @@ export async function startServer(options = {}) {
dbPath: ':memory:',
ollamaUrl: mock.url,
ollamaModel: 'mock:1',
maxMessagesPerProject: options.maxMessagesPerProject,
});
const db = createDatabase(config.dbPath);
const db = createDatabase(config.dbPath, {
maxMessagesPerProject: config.messages.maxPerProject,
});
const ollama = createOllama(config);
const app = await buildApp({ config, db, ollama });
+79
View File
@@ -217,3 +217,82 @@ describe('projects and context', () => {
assert.equal(res.status, 401);
});
});
describe('context retention', () => {
let srv;
let user;
before(async () => {
// A tiny cap so the pruning is observable without writing 500 messages.
srv = await startServer({ maxMessagesPerProject: 4 });
user = await registerUser(srv.base, 'carol@x.io');
});
after(async () => {
await srv.close();
});
async function cappedProject() {
const res = await api(srv.base, 'POST', '/api/projects', {
token: user.token,
body: { name: 'Capped' },
});
return res.json.project;
}
async function append(projectId, content) {
const res = await api(srv.base, 'POST', `/api/projects/${projectId}/messages`, {
token: user.token,
body: { content },
});
assert.equal(res.status, 201);
}
it('keeps only the last messages of a project', async () => {
const p = await cappedProject();
for (let i = 1; i <= 7; i++) await append(p.id, `message ${i}`);
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages?limit=50`, {
token: user.token,
});
assert.deepEqual(
msgs.json.messages.map((m) => m.content),
['message 4', 'message 5', 'message 6', 'message 7'],
);
// Nothing older remains, so pagination stops there.
assert.equal(msgs.json.hasMore, false);
const project = await api(srv.base, 'GET', `/api/projects/${p.id}`, { token: user.token });
assert.equal(project.json.project.messageCount, 4);
});
it('also caps what a chat exchange appends', async () => {
const p = await cappedProject();
for (let i = 1; i <= 4; i++) await append(p.id, `message ${i}`);
// One exchange adds a question and an answer: the two oldest go away.
const chat = await api(srv.base, 'POST', `/api/projects/${p.id}/chat`, {
token: user.token,
body: { message: 'Hi' },
});
assert.equal(chat.status, 200);
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages?limit=50`, {
token: user.token,
});
assert.deepEqual(
msgs.json.messages.map((m) => m.content),
['message 3', 'message 4', 'Hi', 'Bonjour Tony, je suis Jarvis.'],
);
});
it('never trims another project', async () => {
const a = await cappedProject();
const b = await cappedProject();
for (let i = 1; i <= 6; i++) await append(a.id, `a${i}`);
await append(b.id, 'b1');
const kept = await api(srv.base, 'GET', `/api/projects/${b.id}/messages`, { token: user.token });
assert.equal(kept.json.messages.length, 1);
});
});