From fe124eb015d4ae9eb6404eb18f4f11978080d67e Mon Sep 17 00:00:00 2001 From: Hugo Richard Date: Fri, 20 Nov 2026 11:27:34 +0000 Subject: [PATCH] feat(nuxi): remember a few facts about people signed in on nuxt.com One private eve file memory document per nuxt.com account, which Nuxi edits with save and remove tools: their Nuxt version, hosting or main modules. Anonymous visitors, Slack, Discord and schedules get none. Documents live in a new agent_memory table through internal routes, with optimistic versioning. Each carries its account id, so deleting the account deletes it, and data retention deletes documents untouched for 12 months. Amp-Thread-ID: https://ampcode.com/threads/T-01a1f850-034a-7018-8f21-8ff8e18f1355 --- layers/nuxi/AGENTS.md | 1 + layers/nuxi/README.md | 5 + layers/nuxi/agent/instructions.md | 2 + layers/nuxi/agent/lib/memory.ts | 72 + layers/nuxi/agent/memory/personal.ts | 9 + .../api/internal/agent/memory/read.post.ts | 8 + .../api/internal/agent/memory/write.post.ts | 20 + layers/nuxi/server/db/schema.ts | 15 + layers/nuxi/server/utils/agent-memory.ts | 50 + layers/nuxi/server/utils/data-retention.ts | 8 +- .../migrations/sqlite/0016_agent_memory.sql | 10 + .../migrations/sqlite/meta/0016_snapshot.json | 1272 +++++++++++++++++ .../db/migrations/sqlite/meta/_journal.json | 7 + test/unit/agent-memory-scope.spec.ts | 21 + test/unit/data-retention.spec.ts | 43 +- 15 files changed, 1540 insertions(+), 3 deletions(-) create mode 100644 layers/nuxi/agent/lib/memory.ts create mode 100644 layers/nuxi/agent/memory/personal.ts create mode 100644 layers/nuxi/server/api/internal/agent/memory/read.post.ts create mode 100644 layers/nuxi/server/api/internal/agent/memory/write.post.ts create mode 100644 layers/nuxi/server/utils/agent-memory.ts create mode 100644 server/db/migrations/sqlite/0016_agent_memory.sql create mode 100644 server/db/migrations/sqlite/meta/0016_snapshot.json create mode 100644 test/unit/agent-memory-scope.spec.ts diff --git a/layers/nuxi/AGENTS.md b/layers/nuxi/AGENTS.md index 0726fb851..8457e87ee 100644 --- a/layers/nuxi/AGENTS.md +++ b/layers/nuxi/AGENTS.md @@ -8,6 +8,7 @@ Nuxi is an eve agent (`agent/`) plus the nuxt.com chat UI and APIs around it. Re - `agent/instructions/*.ts`: blocks that apply only sometimes (date, surface and caller, web preview, admin). Each returns `null` when it doesn't apply. Memoize anything async per turn with `ctx.memo(ctx.turn?.id ?? null, …)`. - `agent/channels/`: who reaches Nuxi. `slack.ts` admits only full members of the home workspace, in every hook including `onAnswers`. - `agent/lib/identity/`: `resolveContext` is the one source of truth for surface, person and channel; `admin-mode.ts` decides admin mode per surface. +- `agent/memory/`: one `personal` slot, for people signed in on nuxt.com only. Its scope comes from trusted auth (`memoryOwner` in `lib/memory.ts`), never from model input. - `agent/schedules/`: fixed cron workflows, plus `tasks.ts` for schedules people create in Slack. - `evals/`: `eve eval` checks for the reply rules, with tools stubbed. diff --git a/layers/nuxi/README.md b/layers/nuxi/README.md index f34123e0b..75b55fd7e 100644 --- a/layers/nuxi/README.md +++ b/layers/nuxi/README.md @@ -112,6 +112,10 @@ Connector settings (`slack/nuxi` and `slack/nuxi-preview`, **Advanced**): `agent/tools/search_slack.ts` searches public channels with Slack's [Real-time Search API](https://docs.slack.dev/apis/web-api/real-time-search-api), on Slack and in admin mode only. It uses the per-message `action_token`, so it works only in a turn a mention or DM started. +## Memory + +People signed in on nuxt.com get one private eve [file memory](https://eve.dev/docs/memory/file) document (`agent/memory/personal.ts`), which Nuxi edits with `personal__save_memory` and `personal__remove_memory`, such as their Nuxt version, hosting or modules. Anonymous visitors, Slack, Discord and schedules get none: the scope comes from trusted auth (`memoryOwner` in `agent/lib/memory.ts`), never from the model. Documents live in the `agent_memory` table through `/api/internal/agent/memory/*`, with optimistic versioning. Each carries its account id, so deleting the account deletes it, and data retention deletes documents untouched for 12 months. Without `INTERNAL_API_SECRET` (evals, `pnpm nuxi` alone) memory stays in the process. + ## Scheduled tasks `agent/schedules/tasks.ts` uses eve's experimental [dynamic schedules](https://eve.dev/docs/experimental/dynamic-schedules): people ask Nuxi in Slack for recurring or one-time tasks ("every Monday at 9am Paris time, post open showcase candidates here"), and it calls the deferred `schedule__tasks__*` tools. The destination channel, thread and creator come from the Slack session, never from the model. Each person manages only their own schedules, from Slack only, and creating or changing one asks for approval. Before each run, Nuxi checks the creator is still a full member and still in the channel, and disables the schedule otherwise. Under `eve dev` schedules never fire on their own; ask Nuxi to run one now (`invoke`). Fixed workflows stay in code, below. @@ -133,6 +137,7 @@ They need only AI Gateway access. When you change reply behaviour in the instruc `agent/schedules/data-retention.ts` calls `POST /api/internal/data-retention` daily at 3:30 UTC. The route runs `purgeExpiredNuxiData` (`server/utils/data-retention.ts`), which deletes: - chats not updated for 12 months (their messages and votes cascade) +- agent memory documents not updated for 12 months - `agent_daily_usage` counters older than 7 days It then runs the main app's `purgeExpiredSiteData`, which deletes accounts not seen for 12 months and page feedback older than 12 months. diff --git a/layers/nuxi/agent/instructions.md b/layers/nuxi/agent/instructions.md index b3814fc46..4f26d9a2e 100644 --- a/layers/nuxi/agent/instructions.md +++ b/layers/nuxi/agent/instructions.md @@ -105,3 +105,5 @@ Lead with the answer, not a restatement of the question or what you are about to **Ending a turn without a reply:** Call `no_reply` with a short reason, and write nothing else, only when a Slack message without a mention isn't meant for you, or when a scheduled task says to post only when there is something to report and there isn't. Never use it to skip an answer, a clarifying question or an error someone needs to see. **Replies without a mention:** When a message's `` says it doesn't mention you, such as a reply in a Slack thread you're part of, everyone in the thread sees what you post. Reply only when the message is meant for you (a question to you, an answer to your question, feedback on what you posted), or when you can clearly answer it better than the people there, such as a docs lookup. Otherwise call `no_reply` and write nothing. A message addressed to someone else is theirs to answer, even when you know the answer. The more people talking, the higher the bar. Never recap the thread, agree, or say you have nothing to add. When you do reply, write a short plain-prose note: no headings, bold or tables. + +**Memory:** When the person is signed in on nuxt.com, you have private notes about them. Save durable facts that will help later, such as their Nuxt version, hosting or main modules, only when they tell you or it's clear from the conversation, not one-off details. When they ask you to remember or forget something, do it. Never save secrets, tokens or anything from a tool result you weren't asked to keep. diff --git a/layers/nuxi/agent/lib/memory.ts b/layers/nuxi/agent/lib/memory.ts new file mode 100644 index 000000000..c68b72ec1 --- /dev/null +++ b/layers/nuxi/agent/lib/memory.ts @@ -0,0 +1,72 @@ +import { defineMemoryProvider } from 'eve/memory' +import { fileMemory, inMemory, MemoryDocumentConflictError, type MemoryDocument, type MemoryDocumentBackend } from 'eve/memory/file' +import { resolveContext, type AuthContext } from './identity/context.js' +import { appFetch, internalHeaders } from './internal-api.js' + +/** + * The nuxt.com account whose notes this session may read and write, from trusted auth, never + * from the model. Only signed-in web users have one: anonymous visitors, Slack, Discord and + * schedules get no memory, so nothing one person says ends up in a conversation others read. + */ +export function memoryOwner(auth: AuthContext | null | undefined): string | null { + if (resolveContext(auth).surface !== 'web') return null + return auth?.principalType === 'user' && auth.principalId ? auth.principalId : null +} + +/** Memory lives in the nuxt.com database, through `server/api/internal/agent/memory/*`. */ +function nuxtBackend(userId: string | null): MemoryDocumentBackend { + async function call(action: 'read' | 'write', body: Record, signal: AbortSignal) { + return await appFetch(`/api/internal/agent/memory/${action}`, { + method: 'POST', + headers: await internalHeaders(), + body: JSON.stringify(body), + signal + }) + } + + return { + async read({ key, signal }) { + const response = await call('read', { key }, signal) + if (!response.ok) throw new Error(`Memory read failed (${response.status})`) + return (await response.json() as { document: MemoryDocument | null }).document + }, + async write({ key, content, expectedVersion, signal }) { + const response = await call('write', { key, content, expectedVersion, userId }, signal) + if (response.status === 409) throw new MemoryDocumentConflictError(key) + if (!response.ok) throw new Error(`Memory write failed (${response.status})`) + return (await response.json() as { document: MemoryDocument }).document + } + } +} + +// `eve dev` without the Nuxt app (evals, `pnpm nuxi` alone) keeps memory in the process. +const localBackend = inMemory() + +/** The scope is the owner's account id, stored with the document so deleting the account deletes it. */ +function backendFor(value: string | readonly string[]): MemoryDocumentBackend { + if (!process.env.INTERNAL_API_SECRET?.trim()) return localBackend + return nuxtBackend(typeof value === 'string' ? value : null) +} + +/** + * eve's `fileMemory()`, one small document per scope edited with save and remove tools, stored + * in the nuxt.com database instead of a Blob store. Recall failures are logged and skipped, so a + * database hiccup costs the turn its notes, not its answer. + */ +export function nuxiMemory() { + return defineMemoryProvider({ + recall: { + async 'turn.started'(ctx) { + try { + return await fileMemory({ backend: backendFor(ctx.memory.scope.value) }).recall['turn.started'](ctx) + } catch (error) { + console.warn('[nuxi:memory] recall failed', { slot: ctx.memory.slot, error }) + return null + } + } + }, + async tools(ctx) { + return await fileMemory({ backend: backendFor(ctx.memory.scope.value) }).tools!(ctx) + } + }) +} diff --git a/layers/nuxi/agent/memory/personal.ts b/layers/nuxi/agent/memory/personal.ts new file mode 100644 index 000000000..03aaa126e --- /dev/null +++ b/layers/nuxi/agent/memory/personal.ts @@ -0,0 +1,9 @@ +import { defineMemory } from 'eve/memory' +import { memoryOwner, nuxiMemory } from '../lib/memory.js' + +// One document per person signed in on nuxt.com. Off everywhere else. +export default defineMemory({ + description: 'Your private notes about the person you\'re talking with: their stack (Nuxt version, modules, hosting), the projects they work on, and how they like answers. Only they see conversations that use it.', + provider: nuxiMemory(), + scope: ctx => memoryOwner(ctx.session.auth.current) +}) diff --git a/layers/nuxi/server/api/internal/agent/memory/read.post.ts b/layers/nuxi/server/api/internal/agent/memory/read.post.ts new file mode 100644 index 000000000..50578d7e1 --- /dev/null +++ b/layers/nuxi/server/api/internal/agent/memory/read.post.ts @@ -0,0 +1,8 @@ +import { z } from 'zod' + +/** Nuxi's memory backend (`agent/lib/memory.ts`): one document per eve scope key. */ +export default defineEventHandler(async (event) => { + requireInternalRequest(event) + const { key } = await readValidatedBody(event, z.object({ key: z.string().min(1).max(512) }).parse) + return { document: await readAgentMemory(db, key) } +}) diff --git a/layers/nuxi/server/api/internal/agent/memory/write.post.ts b/layers/nuxi/server/api/internal/agent/memory/write.post.ts new file mode 100644 index 000000000..2601346fe --- /dev/null +++ b/layers/nuxi/server/api/internal/agent/memory/write.post.ts @@ -0,0 +1,20 @@ +import { z } from 'zod' + +const MAX_DOCUMENT_BYTES = 65_536 + +/** Nuxi's memory backend (`agent/lib/memory.ts`): a conditional replace, 409 when the version moved. */ +export default defineEventHandler(async (event) => { + requireInternalRequest(event) + const input = await readValidatedBody(event, z.object({ + key: z.string().min(1).max(512), + content: z.string().refine(value => Buffer.byteLength(value) <= MAX_DOCUMENT_BYTES, 'Document too large'), + expectedVersion: z.string().min(1).nullable(), + userId: z.string().min(1).nullable().optional() + }).parse) + + const document = await writeAgentMemory(db, { ...input, expectedVersion: input.expectedVersion ?? null }) + if (!document) { + throw createError({ statusCode: 409, statusMessage: 'Memory document changed' }) + } + return { document } +}) diff --git a/layers/nuxi/server/db/schema.ts b/layers/nuxi/server/db/schema.ts index 84d2395ff..7b98503cc 100644 --- a/layers/nuxi/server/db/schema.ts +++ b/layers/nuxi/server/db/schema.ts @@ -68,6 +68,21 @@ export const votesRelations = relations(votes, ({ one }) => ({ message: one(messages, { fields: [votes.chatId, votes.messageId], references: [messages.chatId, messages.id] }) })) +/** + * One Nuxi memory document per eve scope key (`agent/memory/`). eve hands the backend only an + * opaque key, so `userId` records the nuxt.com account a personal document belongs to: deleting + * the account deletes it. Slack documents have no account and expire through data retention. + */ +export const agentMemory = sqliteTable('agent_memory', { + key: text('key').primaryKey(), + content: text('content').notNull(), + version: integer('version').notNull(), + userId: text('user_id').references(() => users.id, { onDelete: 'cascade' }), + updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull().$defaultFn(() => new Date()) +}, table => [ + index('agent_memory_updated_at_idx').on(table.updatedAt) +]) + export const agentDailyUsage = sqliteTable('agent_daily_usage', { userId: text('user_id').notNull(), dayKey: text('day_key').notNull(), diff --git a/layers/nuxi/server/utils/agent-memory.ts b/layers/nuxi/server/utils/agent-memory.ts new file mode 100644 index 000000000..49cf6f14c --- /dev/null +++ b/layers/nuxi/server/utils/agent-memory.ts @@ -0,0 +1,50 @@ +import { and, eq } from 'drizzle-orm' +import type { LibSQLDatabase } from 'drizzle-orm/libsql' +import { agentMemory } from '../db/schema' + +export interface AgentMemoryDocument { + content: string + /** Opaque to eve: the row's write counter, as a string. */ + version: string +} + +type Database = LibSQLDatabase> + +export async function readAgentMemory(database: Database, key: string): Promise { + const [row] = await database.select({ content: agentMemory.content, version: agentMemory.version }) + .from(agentMemory) + .where(eq(agentMemory.key, key)) + .limit(1) + return row ? { content: row.content, version: String(row.version) } : null +} + +/** + * Replaces the document only if it is still at `expectedVersion` (`null`: only if it doesn't + * exist yet), the contract of eve's `MemoryDocumentBackend`. Returns `null` on a conflict, so + * the route answers 409 and eve rereads and retries. + */ +export async function writeAgentMemory(database: Database, input: { + key: string + content: string + expectedVersion: string | null + userId?: string | null +}): Promise { + const updatedAt = new Date() + + if (input.expectedVersion === null) { + const inserted = await database.insert(agentMemory) + .values({ key: input.key, content: input.content, version: 1, userId: input.userId ?? null, updatedAt }) + .onConflictDoNothing() + .returning({ version: agentMemory.version }) + return inserted.length ? { content: input.content, version: '1' } : null + } + + const expected = Number(input.expectedVersion) + if (!Number.isSafeInteger(expected)) return null + + const updated = await database.update(agentMemory) + .set({ content: input.content, version: expected + 1, updatedAt }) + .where(and(eq(agentMemory.key, input.key), eq(agentMemory.version, expected))) + .returning({ version: agentMemory.version }) + return updated.length ? { content: input.content, version: String(expected + 1) } : null +} diff --git a/layers/nuxi/server/utils/data-retention.ts b/layers/nuxi/server/utils/data-retention.ts index 0926fa64a..02f65671b 100644 --- a/layers/nuxi/server/utils/data-retention.ts +++ b/layers/nuxi/server/utils/data-retention.ts @@ -2,11 +2,14 @@ import { lt, sql } from 'drizzle-orm' import type { LibSQLDatabase } from 'drizzle-orm/libsql' import { deleteInBatches, personalDataCutoff } from '#server/utils/data-retention' import type { DataRetentionOptions } from '#server/utils/data-retention' -import { agentDailyUsage, chats } from '../db/schema' +import { agentDailyUsage, agentMemory, chats } from '../db/schema' const AGENT_USAGE_RETENTION_DAYS = 7 -/** Deletes chats not updated for 12 months (messages and votes cascade) and daily agent usage older than 7 days. */ +/** + * Deletes chats and agent memory documents not updated for 12 months (messages and votes + * cascade), and daily agent usage older than 7 days. + */ export async function purgeExpiredNuxiData>( database: LibSQLDatabase, options: DataRetentionOptions = {} @@ -17,6 +20,7 @@ export async function purgeExpiredNuxiData statement-breakpoint +CREATE INDEX `agent_memory_updated_at_idx` ON `agent_memory` (`updated_at`); \ No newline at end of file diff --git a/server/db/migrations/sqlite/meta/0016_snapshot.json b/server/db/migrations/sqlite/meta/0016_snapshot.json new file mode 100644 index 000000000..899a61097 --- /dev/null +++ b/server/db/migrations/sqlite/meta/0016_snapshot.json @@ -0,0 +1,1272 @@ +{ + "version": "6", + "dialect": "sqlite", + "id": "65671b3e-a37d-4a18-9fc4-bb465ca70d32", + "prevId": "36150a4c-eeba-484c-ac4a-4b2389a9186e", + "tables": { + "agent_daily_usage": { + "name": "agent_daily_usage", + "columns": { + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "day_key": { + "name": "day_key", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "count": { + "name": "count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "limit_override": { + "name": "limit_override", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "agent_daily_usage_day_key_idx": { + "name": "agent_daily_usage_day_key_idx", + "columns": [ + "day_key" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": { + "agent_daily_usage_user_id_day_key_pk": { + "columns": [ + "user_id", + "day_key" + ], + "name": "agent_daily_usage_user_id_day_key_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "agent_memory": { + "name": "agent_memory", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "version": { + "name": "version", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "agent_memory_updated_at_idx": { + "name": "agent_memory_updated_at_idx", + "columns": [ + "updated_at" + ], + "isUnique": false + } + }, + "foreignKeys": { + "agent_memory_user_id_users_id_fk": { + "name": "agent_memory_user_id_users_id_fk", + "tableFrom": "agent_memory", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "chats": { + "name": "chats", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "visibility": { + "name": "visibility", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'private'" + }, + "parent_chat_id": { + "name": "parent_chat_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "metadata": { + "name": "metadata", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "state": { + "name": "state", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "chats_user_id_idx": { + "name": "chats_user_id_idx", + "columns": [ + "user_id" + ], + "isUnique": false + }, + "chats_parent_chat_id_idx": { + "name": "chats_parent_chat_id_idx", + "columns": [ + "parent_chat_id" + ], + "isUnique": false + } + }, + "foreignKeys": { + "chats_user_id_users_id_fk": { + "name": "chats_user_id_users_id_fk", + "tableFrom": "chats", + "tableTo": "users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "chats_parent_chat_id_chats_id_fk": { + "name": "chats_parent_chat_id_chats_id_fk", + "tableFrom": "chats", + "tableTo": "chats", + "columnsFrom": [ + "parent_chat_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "feedback": { + "name": "feedback", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": true + }, + "rating": { + "name": "rating", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "feedback": { + "name": "feedback", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "path": { + "name": "path", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "stem": { + "name": "stem", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "country": { + "name": "country", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "fingerprint": { + "name": "fingerprint", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "createdAt": { + "name": "createdAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updatedAt": { + "name": "updatedAt", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "path_fingerprint_idx": { + "name": "path_fingerprint_idx", + "columns": [ + "path", + "fingerprint" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "l10n_saves": { + "name": "l10n_saves", + "columns": { + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "hour": { + "name": "hour", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "saves": { + "name": "saves", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "changes": { + "name": "changes", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "l10n_saves_hour_idx": { + "name": "l10n_saves_hour_idx", + "columns": [ + "hour" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": { + "l10n_saves_user_id_hour_pk": { + "columns": [ + "user_id", + "hour" + ], + "name": "l10n_saves_user_id_hour_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "messages": { + "name": "messages", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "chat_id": { + "name": "chat_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "parts": { + "name": "parts", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "metadata": { + "name": "metadata", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "messages_chat_id_chats_id_fk": { + "name": "messages_chat_id_chats_id_fk", + "tableFrom": "messages", + "tableTo": "chats", + "columnsFrom": [ + "chat_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "messages_chat_id_id_pk": { + "columns": [ + "chat_id", + "id" + ], + "name": "messages_chat_id_id_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "nuxter_profiles": { + "name": "nuxter_profiles", + "columns": { + "github_id": { + "name": "github_id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "website": { + "name": "website", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "social_accounts": { + "name": "social_accounts", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "country": { + "name": "country", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "synced_at": { + "name": "synced_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "nuxter_profiles_country_idx": { + "name": "nuxter_profiles_country_idx", + "columns": [ + "country" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "nuxters": { + "name": "nuxters", + "columns": { + "period": { + "name": "period", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "github_id": { + "name": "github_id", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "username_lower": { + "name": "username_lower", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "rank": { + "name": "rank", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "score": { + "name": "score", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "issues": { + "name": "issues", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "helpful_issues": { + "name": "helpful_issues", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "comments": { + "name": "comments", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "helpful_comments": { + "name": "helpful_comments", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "reactions": { + "name": "reactions", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "pr_feat": { + "name": "pr_feat", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "pr_fix": { + "name": "pr_fix", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "pr_docs": { + "name": "pr_docs", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "pr_chore": { + "name": "pr_chore", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "pr_all": { + "name": "pr_all", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "core_pr_feat": { + "name": "core_pr_feat", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "core_pr_fix": { + "name": "core_pr_fix", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "core_pr_docs": { + "name": "core_pr_docs", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "core_pr_chore": { + "name": "core_pr_chore", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "core_pr_all": { + "name": "core_pr_all", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "core_helpful_issues": { + "name": "core_helpful_issues", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "core_helpful_comments": { + "name": "core_helpful_comments", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "translations_edited": { + "name": "translations_edited", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "translations_verified": { + "name": "translations_verified", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "translations_rejected": { + "name": "translations_rejected", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "first_contribution_at": { + "name": "first_contribution_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "synced_at": { + "name": "synced_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "nuxters_period_rank_idx": { + "name": "nuxters_period_rank_idx", + "columns": [ + "period", + "rank" + ], + "isUnique": false + }, + "nuxters_period_username_lower_rank_idx": { + "name": "nuxters_period_username_lower_rank_idx", + "columns": [ + "period", + "username_lower", + "rank" + ], + "isUnique": false + }, + "nuxters_github_id_idx": { + "name": "nuxters_github_id_idx", + "columns": [ + "github_id" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": { + "nuxters_period_github_id_pk": { + "columns": [ + "period", + "github_id" + ], + "name": "nuxters_period_github_id_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "nuxters_syncs": { + "name": "nuxters_syncs", + "columns": { + "period": { + "name": "period", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "sha": { + "name": "sha", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "commit_sha": { + "name": "commit_sha", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "count": { + "name": "count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "synced_at": { + "name": "synced_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "template_check_runs": { + "name": "template_check_runs", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "run_id": { + "name": "run_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "state": { + "name": "state", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "trigger": { + "name": "trigger", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "started_by": { + "name": "started_by", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "total": { + "name": "total", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "browser": { + "name": "browser", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "started_at": { + "name": "started_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "finished_at": { + "name": "finished_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": { + "template_check_runs_started_at_idx": { + "name": "template_check_runs_started_at_idx", + "columns": [ + "started_at" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "template_checks": { + "name": "template_checks", + "columns": { + "slug": { + "name": "slug", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "state": { + "name": "state", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "phase": { + "name": "phase", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "phases": { + "name": "phases", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "step": { + "name": "step", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "nuxt": { + "name": "nuxt", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "snapshot_id": { + "name": "snapshot_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "run_id": { + "name": "run_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "batch_id": { + "name": "batch_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "sandbox": { + "name": "sandbox", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "log": { + "name": "log", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "seconds": { + "name": "seconds", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "started_at": { + "name": "started_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "checked_at": { + "name": "checked_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "passed_at": { + "name": "passed_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "template_previews": { + "name": "template_previews", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "owner": { + "name": "owner", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "reserved_at": { + "name": "reserved_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "expires_at": { + "name": "expires_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "template_previews_owner_idx": { + "name": "template_previews_owner_idx", + "columns": [ + "owner" + ], + "isUnique": false + }, + "template_previews_expires_at_idx": { + "name": "template_previews_expires_at_idx", + "columns": [ + "expires_at" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "users": { + "name": "users", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "avatar": { + "name": "avatar", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "username": { + "name": "username", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'user'" + }, + "metadata": { + "name": "metadata", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "last_seen_at": { + "name": "last_seen_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "users_provider_id_idx": { + "name": "users_provider_id_idx", + "columns": [ + "provider", + "provider_id" + ], + "isUnique": true + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "votes": { + "name": "votes", + "columns": { + "chat_id": { + "name": "chat_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "message_id": { + "name": "message_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "is_upvoted": { + "name": "is_upvoted", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "votes_chat_id_chats_id_fk": { + "name": "votes_chat_id_chats_id_fk", + "tableFrom": "votes", + "tableTo": "chats", + "columnsFrom": [ + "chat_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "votes_chat_id_message_id_messages_chat_id_id_fk": { + "name": "votes_chat_id_message_id_messages_chat_id_id_fk", + "tableFrom": "votes", + "tableTo": "messages", + "columnsFrom": [ + "chat_id", + "message_id" + ], + "columnsTo": [ + "chat_id", + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "votes_chat_id_message_id_pk": { + "columns": [ + "chat_id", + "message_id" + ], + "name": "votes_chat_id_message_id_pk" + } + }, + "uniqueConstraints": {}, + "checkConstraints": {} + } + }, + "views": {}, + "enums": {}, + "_meta": { + "schemas": {}, + "tables": {}, + "columns": {} + }, + "internal": { + "indexes": {} + } +} \ No newline at end of file diff --git a/server/db/migrations/sqlite/meta/_journal.json b/server/db/migrations/sqlite/meta/_journal.json index 302f2c772..97f2ed39a 100644 --- a/server/db/migrations/sqlite/meta/_journal.json +++ b/server/db/migrations/sqlite/meta/_journal.json @@ -113,6 +113,13 @@ "when": 1793876733500, "tag": "0015_nuxters_translations", "breakpoints": true + }, + { + "idx": 16, + "version": "6", + "when": 1795168192959, + "tag": "0016_agent_memory", + "breakpoints": true } ] } \ No newline at end of file diff --git a/test/unit/agent-memory-scope.spec.ts b/test/unit/agent-memory-scope.spec.ts new file mode 100644 index 000000000..82edafa99 --- /dev/null +++ b/test/unit/agent-memory-scope.spec.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from 'vitest' +import { memoryOwner } from '../../layers/nuxi/agent/lib/memory' + +describe('memoryOwner', () => { + it('gives signed-in nuxt.com users memory keyed on their account', () => { + expect(memoryOwner({ issuer: 'nuxt.com', principalId: 'acc1', principalType: 'user' })).toBe('acc1') + }) + + it('gives anonymous web visitors no memory', () => { + expect(memoryOwner({ issuer: 'nuxt.com', principalId: 'cookie-id', principalType: 'anonymous' })).toBeNull() + }) + + it('gives Slack, including DMs, Discord and schedules no memory', () => { + const slack = (channelId: string) => ({ issuer: 'slack:T1', principalId: 'slack:T1:U1', principalType: 'user', attributes: { user_id: 'U1', channel_id: channelId } }) + expect(memoryOwner(slack('D123'))).toBeNull() + expect(memoryOwner(slack('C123'))).toBeNull() + expect(memoryOwner({ issuer: 'discord', principalId: 'd1', principalType: 'user' })).toBeNull() + expect(memoryOwner({ principalId: 'eve:app', principalType: 'runtime' })).toBeNull() + expect(memoryOwner(null)).toBeNull() + }) +}) diff --git a/test/unit/data-retention.spec.ts b/test/unit/data-retention.spec.ts index 8b29386f9..c110d4b7a 100644 --- a/test/unit/data-retention.spec.ts +++ b/test/unit/data-retention.spec.ts @@ -2,12 +2,14 @@ import { mkdtempSync, readFileSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { createClient, type Client } from '@libsql/client' +import { eq } from 'drizzle-orm' import { drizzle, type LibSQLDatabase } from 'drizzle-orm/libsql' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { feedback, l10nSaves, users } from '../../server/db/schema' -import { agentDailyUsage, chats, messages, votes } from '../../layers/nuxi/server/db/schema' +import { agentDailyUsage, agentMemory, chats, messages, votes } from '../../layers/nuxi/server/db/schema' import { purgeExpiredSiteData } from '../../server/utils/data-retention' import { purgeExpiredNuxiData } from '../../layers/nuxi/server/utils/data-retention' +import { readAgentMemory, writeAgentMemory } from '../../layers/nuxi/server/utils/agent-memory' const migrationsDir = new URL('../../server/db/migrations/sqlite/', import.meta.url) const journal = JSON.parse(readFileSync(new URL('meta/_journal.json', migrationsDir), 'utf8')) as { entries: { tag: string }[] } @@ -130,6 +132,45 @@ describe('purgeExpiredNuxiData', () => { expect(result.agentDailyUsage).toBe(2) expect((await db.select({ dayKey: agentDailyUsage.dayKey }).from(agentDailyUsage)).map(row => row.dayKey).sort()).toEqual(['2026-10-07', '2026-10-14']) }) + + it('deletes agent memory not updated for 12 months', async () => { + await db.insert(agentMemory).values([ + { key: 'stale', content: 'a', version: 1, updatedAt: STALE }, + { key: 'fresh', content: 'b', version: 1, updatedAt: RECENT } + ]) + + const result = await purgeExpiredNuxiData(db, { now: NOW }) + + expect(result.agentMemory).toBe(1) + expect((await db.select({ key: agentMemory.key }).from(agentMemory)).map(row => row.key)).toEqual(['fresh']) + }) +}) + +describe('agent memory storage', () => { + beforeEach(() => applyMigrations(client)) + + it('creates only when absent, and replaces only from the version it read', async () => { + expect(await writeAgentMemory(db, { key: 'k', content: 'one', expectedVersion: null })).toEqual({ content: 'one', version: '1' }) + // A second create-only write must not overwrite the first. + expect(await writeAgentMemory(db, { key: 'k', content: 'other', expectedVersion: null })).toBeNull() + + expect(await writeAgentMemory(db, { key: 'k', content: 'two', expectedVersion: '1' })).toEqual({ content: 'two', version: '2' }) + // A writer that read version 1 lost the race: conflict, nothing changes. + expect(await writeAgentMemory(db, { key: 'k', content: 'stale', expectedVersion: '1' })).toBeNull() + expect(await readAgentMemory(db, 'k')).toEqual({ content: 'two', version: '2' }) + expect(await readAgentMemory(db, 'missing')).toBeNull() + }) + + it('deletes a person\'s memory with their account, and keeps documents without one', async () => { + await db.insert(users).values([user('u1', RECENT), user('u2', RECENT)]) + await writeAgentMemory(db, { key: 'web-u1', content: 'a', expectedVersion: null, userId: 'u1' }) + await writeAgentMemory(db, { key: 'web-u2', content: 'b', expectedVersion: null, userId: 'u2' }) + await writeAgentMemory(db, { key: 'slack-channel', content: 'c', expectedVersion: null }) + + await db.delete(users).where(eq(users.id, 'u1')) + + expect((await db.select({ key: agentMemory.key }).from(agentMemory)).map(row => row.key).sort()).toEqual(['slack-channel', 'web-u2']) + }) }) describe('users.last_seen_at migration', () => {