From c1d6593c35756e982c0dc28f4d9684650b63ce5f Mon Sep 17 00:00:00 2001 From: BlackAngelTV Date: Mon, 5 Oct 2026 21:39:28 +0200 Subject: [PATCH] feat(backend): recite the project context to verify what the model receives Storing a context is not enough: the user needs to know it actually reaches Ollama, and that the messages replayed are the ones they expect. The read-only probe answers that by asking the model to recite its context and reporting how many messages were replayed, while persisting nothing. --- backend/src/routes/projects.js | 58 +++++++++++++++++++++++++++++++--- backend/test/projects.test.js | 41 +++++++++++++++++++++++- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/backend/src/routes/projects.js b/backend/src/routes/projects.js index 0592e5b..1020d24 100644 --- a/backend/src/routes/projects.js +++ b/backend/src/routes/projects.js @@ -57,9 +57,23 @@ const projectChatSchema = { }, }; +const recallSchema = { + type: 'object', + additionalProperties: false, + properties: { + message: { type: 'string', minLength: 1, maxLength: 2000 }, + model: { type: 'string', maxLength: 120 }, + }, +}; + /** Max number of project messages fed back to Ollama as context. */ const CONTEXT_LIMIT = 40; +/** Question asked by the client's « vérifier le contexte » button. */ +const RECALL_PROMPT = + "Rappelle en deux ou trois phrases ce que tu retiens du contexte de ce projet. " + + "Si le contexte est vide, dis-le clairement."; + /** * Project routes: each project belongs to the authenticated user and its * messages form the context sent to Jarvis. @@ -76,12 +90,20 @@ export async function projectRoutes(fastify) { return project; } - /** Context sent to Ollama: project history + the new prompt. */ - function buildMessages(projectId, message) { - const history = db + /** + * The project's stored context, oldest first, as Ollama sees it. + * Read on every request: the context is never cached on the client, so what + * the model answers with is always what the database holds right now. + */ + function readContext(projectId) { + return db .listMessages(projectId, { limit: CONTEXT_LIMIT }) .map((m) => ({ role: m.role, content: m.content })); - return [...history, { role: 'user', content: message }]; + } + + /** Context sent to Ollama: project history + the new prompt. */ + function buildMessages(projectId, message) { + return [...readContext(projectId), { role: 'user', content: message }]; } fastify.post('/api/projects', { ...auth, schema: { body: createSchema } }, async (request, reply) => { @@ -201,6 +223,34 @@ export async function projectRoutes(fastify) { }, ); + /** + * Asks the model to recite the context it just received, to verify that the + * stored history really reaches Ollama. Nothing is persisted: this is a + * read-only probe. The `done` event carries `contextMessages`, the exact + * number of stored messages replayed to the model. + */ + fastify.post( + '/api/projects/:id/context/recall', + { ...auth, schema: { params: paramsSchema, body: recallSchema } }, + async (request, reply) => { + const project = requireProject(request); + const history = readContext(project.id); + const messages = [ + ...history, + { role: 'user', content: request.body.message ?? RECALL_PROMPT }, + ]; + + return streamChatToClient({ + ollama: fastify.ollama, + reply, + request, + messages, + model: request.body.model, + onComplete: () => ({ contextMessages: history.length }), + }); + }, + ); + // Same, but streamed: each chunk is sent as soon as Ollama produces it. fastify.post( '/api/projects/:id/chat/stream', diff --git a/backend/test/projects.test.js b/backend/test/projects.test.js index a1204ad..6ad4b0d 100644 --- a/backend/test/projects.test.js +++ b/backend/test/projects.test.js @@ -1,7 +1,7 @@ import assert from 'node:assert/strict'; import { after, before, describe, it } from 'node:test'; -import { api, registerUser, startServer } from './helpers.js'; +import { api, readSse, registerUser, startServer } from './helpers.js'; describe('projects and context', () => { let srv; @@ -177,4 +177,43 @@ describe('projects and context', () => { 0, ); }); + + it('recites the stored context without persisting anything', async () => { + const p = await newProject('Recall'); + for (const content of ['Le frontend est écrit en Rust', 'Le backend répond en SSE']) { + await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, { + token: alice.token, + body: { content }, + }); + } + + const res = await fetch(`${srv.base}/api/projects/${p.id}/context/recall`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${alice.token}` }, + body: JSON.stringify({}), + }); + + assert.equal(res.status, 200); + const events = await readSse(res); + const done = events.find((e) => e.event === 'done'); + + assert.ok(events.some((e) => e.event === 'delta'), 'expected the model to answer'); + // The model really received the two stored messages. + assert.equal(done.data.contextMessages, 2); + assert.equal(done.data.reply, 'Bonjour Tony, je suis Jarvis.'); + + // A probe must leave the context untouched. + const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: alice.token }); + assert.equal(msgs.json.messages.length, 2); + }); + + it('recital needs a token', async () => { + const p = await newProject('Recall auth'); + const res = await fetch(`${srv.base}/api/projects/${p.id}/context/recall`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({}), + }); + assert.equal(res.status, 401); + }); });