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

+10
View File
@@ -47,6 +47,16 @@ export function loadConfig(overrides = {}) {
port: Number(overrides.port ?? process.env.PORT ?? 3000),
host: overrides.host ?? process.env.HOST ?? '0.0.0.0',
dbPath: resolveFromRoot(overrides.dbPath ?? process.env.DB_PATH, 'data/jarvise.db'),
messages: {
/**
* Nombre de messages conservés par projet : au-delà, les plus anciens
* sont supprimés à l'insertion. Évite qu'un projet de longue durée fasse
* grossir la base sans borne.
*/
maxPerProject: Number(
overrides.maxMessagesPerProject ?? process.env.MAX_PROJECT_MESSAGES ?? 500,
),
},
cors: {
origin: overrides.corsOrigin ?? parseOrigin(process.env.CORS_ORIGIN),
},
+37 -2
View File
@@ -41,8 +41,12 @@ const SCHEMA = `
/**
* Opens (and initialises) the SQLite database and returns every query helper.
* Each call creates an independent database, which is convenient for tests.
*
* @param {string} dbPath file path, or `:memory:`
* @param {{ maxMessagesPerProject?: number }} [options] retention of a project's
* context; the oldest messages are dropped beyond that count.
*/
export function createDatabase(dbPath) {
export function createDatabase(dbPath, { maxMessagesPerProject = 500 } = {}) {
if (dbPath !== ':memory:') {
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
}
@@ -160,11 +164,41 @@ export function createDatabase(dbPath) {
.prepare('SELECT 1 FROM messages WHERE project_id = ? AND id < ? LIMIT 1')
.get(projectId, oldestId);
/**
* Drops the oldest messages of a project so at most `keep` remain.
* @returns {number} how many rows were deleted.
*/
const trimMessages = (projectId, keep = maxMessagesPerProject) => {
const { total } = db
.prepare('SELECT COUNT(*) AS total FROM messages WHERE project_id = ?')
.get(projectId);
const excess = total - keep;
if (excess <= 0) return 0;
// `idx_messages_project (project_id, id)` makes both statements cheap, and
// the whole project fits in a few hundred rows anyway.
return db
.prepare(
`DELETE FROM messages WHERE project_id = ? AND id IN (
SELECT id FROM messages WHERE project_id = ? ORDER BY id ASC LIMIT ?
)`,
)
.run(projectId, projectId, excess).changes;
};
const addMessage = (projectId, { role, content }) => {
const result = db
.prepare('INSERT INTO messages (project_id, role, content) VALUES (?, ?, ?)')
.run(projectId, role, content);
return db.prepare('SELECT * FROM messages WHERE id = ?').get(Number(result.lastInsertRowid));
const message = db
.prepare('SELECT * FROM messages WHERE id = ?')
.get(Number(result.lastInsertRowid));
// Single choke point for every writer (chat, chat/stream, manual notes), so
// the context of a project can never grow past the configured limit.
trimMessages(projectId);
return message;
};
const deleteMessages = (projectId) =>
@@ -192,6 +226,7 @@ export function createDatabase(dbPath) {
listMessages,
hasOlderMessages,
addMessage,
trimMessages,
deleteMessages,
};
}
+3 -1
View File
@@ -4,7 +4,9 @@ import { createDatabase } from './db/index.js';
import { createOllama } from './services/ollama.js';
const config = loadConfig();
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 });