ruflo 3.6.27 → 3.6.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/package.json +1 -1
  2. package/src/ruvocal/.claude-flow/daemon-state.json +135 -0
  3. package/src/ruvocal/.claude-flow/data/pending-insights.jsonl +0 -25
  4. package/src/ruvocal/.claude-flow/data/ranked-context.json +5 -0
  5. package/src/ruvocal/.claude-flow/logs/daemon.log +31 -0
  6. package/src/ruvocal/.claude-flow/logs/headless/audit_1777949411822_juxau0_prompt.log +989 -0
  7. package/src/ruvocal/.claude-flow/logs/headless/audit_1777949411822_juxau0_result.log +67 -0
  8. package/src/ruvocal/.claude-flow/logs/headless/audit_1777950042278_jvj5xq_prompt.log +989 -0
  9. package/src/ruvocal/.claude-flow/logs/headless/audit_1777950042278_jvj5xq_result.log +93 -0
  10. package/src/ruvocal/.claude-flow/logs/headless/optimize_1777949531823_yt5yc2_prompt.log +1498 -0
  11. package/src/ruvocal/.claude-flow/logs/headless/optimize_1777949531823_yt5yc2_result.log +93 -0
  12. package/src/ruvocal/.claude-flow/logs/headless/testgaps_1777949771821_elw1j4_prompt.log +1498 -0
  13. package/src/ruvocal/.claude-flow/logs/headless/testgaps_1777949771821_elw1j4_result.log +100 -0
  14. package/src/ruvocal/.claude-flow/metrics/codebase-map.json +11 -0
  15. package/src/ruvocal/.claude-flow/metrics/consolidation.json +6 -0
  16. package/src/ruvocal/.claude-flow/sessions/current.json +13 -0
  17. package/src/ruvocal/.swarm/attestation.db +0 -0
  18. package/src/ruvocal/.swarm/hnsw.index +0 -0
  19. package/src/ruvocal/.swarm/hnsw.metadata.json +1 -0
  20. package/src/ruvocal/.swarm/memory.db +0 -0
  21. package/src/ruvocal/.swarm/schema.sql +305 -0
  22. package/src/ruvocal/src/lib/components/chat/ChatWindow.svelte +8 -8
  23. package/src/ruvocal/src/lib/server/mcp/clientPool.spec.ts +175 -0
  24. package/src/ruvocal/src/lib/server/mcp/clientPool.ts +0 -0
  25. package/src/ruvocal/src/lib/server/textGeneration/index.ts +1 -0
  26. package/src/ruvocal/src/lib/server/textGeneration/mcp/runMcpFlow.ts +10 -1
  27. package/src/ruvocal/src/lib/server/textGeneration/types.ts +3 -1
  28. package/src/ruvocal/src/routes/api/v2/user/settings/+server.ts +7 -0
  29. package/src/ruvocal/src/routes/conversation/[id]/+page.svelte +4 -0
  30. package/src/ruvocal/src/routes/conversation/[id]/+server.ts +4 -0
  31. package/src/ruvocal/src/routes/settings/(nav)/+server.ts +6 -0
@@ -0,0 +1,100 @@
1
+ [2026-05-05T02:58:33.227Z] RESULT
2
+ ============================================================
3
+ {
4
+ "success": true,
5
+ "output": "Here is the full coverage gap analysis with test skeletons for each area.\n\n---\n\n## Test Coverage Gap Analysis\n\n### 1. `src/lib/APIClient.ts` — Zero test coverage\n\n**Untested:** `apiCall` (all branches), `handleResponse`, query param serialization, error body parsing fallback chain, ObjectId superjson registration.\n\n```typescript\n// src/lib/APIClient.spec.ts\nimport { describe, it, expect, vi, afterEach } from \"vitest\";\nimport { handleResponse, useAPIClient } from \"./APIClient\";\n\nconst mockFetch = (status: number, body: string | null, ok = status < 400) =>\n vi.fn().mockResolvedValue({\n ok,\n status,\n text: () => Promise.resolve(body ?? \"\"),\n json: () => (body ? Promise.resolve(JSON.parse(body)) : Promise.reject(new Error(\"no body\"))),\n statusText: \"Error\",\n });\n\ndescribe(\"apiCall — success paths\", () => {\n it(\"sends GET with no body\", async () => {\n const fetch = mockFetch(200, '{\"json\":\"body\"}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client[\"public-config\"].get();\n expect(fetch).toHaveBeenCalledWith(\n expect.stringContaining(\"/api/v2/public-config\"),\n expect.objectContaining({ method: \"GET\" })\n );\n });\n\n it(\"attaches query params to GET URL\", async () => {\n const fetch = mockFetch(200, '{\"x\":1}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client[\"public-config\"].get({ query: { page: 2, skip: undefined } });\n const url = fetch.mock.calls[0][0] as string;\n expect(url).toContain(\"page=2\");\n expect(url).not.toContain(\"skip\"); // undefined values are omitted\n });\n\n it(\"returns null data on empty 200 body\", async () => {\n const fetch = mockFetch(200, \"\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.settings.get();\n expect(res.data).toBeNull();\n expect(res.error).toBeNull();\n });\n\n it(\"serialises body as JSON for POST\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client.user.settings.post({ theme: \"dark\" });\n const init = fetch.mock.calls[0][1] as RequestInit;\n expect(init.headers).toMatchObject({ \"Content-Type\": \"application/json\" });\n expect(JSON.parse(init.body as string)).toEqual({ theme: \"dark\" });\n });\n});\n\ndescribe(\"apiCall — error paths\", () => {\n it(\"returns error with JSON body on 4xx\", async () => {\n const fetch = mockFetch(400, '{\"message\":\"bad request\"}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toEqual({ message: \"bad request\" });\n expect(res.data).toBeNull();\n expect(res.status).toBe(400);\n });\n\n it(\"falls back to text when error body is not JSON\", async () => {\n const fetch = vi.fn().mockResolvedValue({\n ok: false,\n status: 500,\n json: () => Promise.reject(new Error(\"not json\")),\n text: () => Promise.resolve(\"Internal Server Error\"),\n statusText: \"ISE\",\n });\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toBe(\"Internal Server Error\");\n });\n\n it(\"falls back to statusText when text() also fails\", async () => {\n const fetch = vi.fn().mockResolvedValue({\n ok: false,\n status: 503,\n json: () => Promise.reject(new Error()),\n text: () => Promise.reject(new Error()),\n statusText: \"Service Unavailable\",\n });\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toBe(\"Service Unavailable\");\n });\n});\n\ndescribe(\"handleResponse\", () => {\n it(\"returns null when data is null and no error\", () => {\n expect(handleResponse({ data: null, error: null, status: 204 })).toBeNull();\n });\n\n it(\"throws when response contains an error\", () => {\n expect(() =>\n handleResponse({ data: null, error: { message: \"forbidden\" }, status: 403 })\n ).toThrow();\n });\n\n it(\"parses superjson string data\", () => {\n const payload = JSON.stringify({ json: { value: 42 }, meta: undefined });\n expect(handleResponse({ data: payload, error: null, status: 200 })).toEqual({ value: 42 });\n });\n});\n\ndescribe(\"useAPIClient — URL construction\", () => {\n it(\"uses provided origin for SSR base URL\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://api.internal:4000\" });\n await client.export.get();\n expect(fetch.mock.calls[0][0]).toContain(\"http://api.internal:4000\");\n });\n\n it(\"builds nested conversation message endpoint\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client.conversations({ id: \"abc123\" }).message({ messageId: \"msg456\" }).get();\n expect(fetch.mock.calls[0][0]).toContain(\"/conversations/abc123/message/msg456\");\n });\n});\n```\n\n---\n\n### 2. `src/lib/actions/clickOutside.ts` — Zero test coverage\n\n**Untested:** click outside triggers callback, click inside does not, `update` replaces callback, `destroy` removes listener.\n\n```typescript\n// src/lib/actions/clickOutside.spec.ts\nimport { describe, it, expect, vi, beforeEach } from \"vitest\";\nimport { clickOutside } from \"./clickOutside\";\n\ndescribe(\"clickOutside action\", () => {\n let element: HTMLDivElement;\n let outsideElement: HTMLDivElement;\n let callback: ReturnType<typeof vi.fn>;\n\n beforeEach(() => {\n document.body.innerHTML = \"\";\n element = document.createElement(\"div\");\n outsideElement = document.createElement(\"div\");\n document.body.appendChild(element);\n document.body.appendChild(outsideElement);\n callback = vi.fn();\n });\n\n it(\"fires callback when clicking outside the element\", () => {\n clickOutside(element, callback);\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).toHaveBeenCalledOnce();\n });\n\n it(\"does not fire callback when clicking inside the element\", () => {\n clickOutside(element, callback);\n element.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"does not fire when clicking the element itself\", () => {\n clickOutside(element, callback);\n const inner = document.createElement(\"span\");\n element.appendChild(inner);\n inner.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"update() replaces the callback\", () => {\n const action = clickOutside(element, callback);\n const newCallback = vi.fn();\n action.update(newCallback);\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n expect(newCallback).toHaveBeenCalledOnce();\n });\n\n it(\"destroy() stops all future callbacks\", () => {\n const action = clickOutside(element, callback);\n action.destroy();\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"handles multiple destroy() calls without error\", () => {\n const action = clickOutside(element, callback);\n expect(() => { action.destroy(); action.destroy(); }).not.toThrow();\n });\n});\n```\n\n---\n\n### 3. `src/lib/buildPrompt.ts` — Zero test coverage\n\n**Untested:** system message replacement with preprompt, no system message scenario, truncation slicing, zero truncate parameter (no slicing).\n\n```typescript\n// src/lib/buildPrompt.spec.ts\nimport { describe, it, expect } from \"vitest\";\nimport { buildPrompt } from \"./buildPrompt\";\nimport type { BackendModel } from \"./server/models\";\n\nconst makeModel = (truncate?: number): BackendModel =>\n ({\n chatPromptRender: ({ messages, preprompt }: { messages: unknown[]; preprompt?: string }) =>\n messages.map((m: any) => `${m.role}:${m.content}`).join(\" \"),\n parameters: truncate !== undefined ? { truncate } : undefined,\n } as unknown as BackendModel);\n\ndescribe(\"buildPrompt\", () => {\n it(\"replaces system message content with preprompt when first message is system\", async () => {\n const model = makeModel(1000);\n const messages = [\n { from: \"system\" as const, content: \"old system prompt\" },\n { from: \"user\" as const, content: \"hello\" },\n ];\n const result = await buildPrompt({ messages, model, preprompt: \"new system prompt\" });\n expect(result).toContain(\"new system prompt\");\n expect(result).not.toContain(\"old system prompt\");\n });\n\n it(\"does not replace content when first message is not system\", async () => {\n const model = makeModel(1000);\n const messages = [{ from: \"user\" as const, content: \"hello\" }];\n await buildPrompt({ messages, model, preprompt: \"some preprompt\" });\n // original content unchanged\n expect(messages[0].content).toBe(\"hello\");\n });\n\n it(\"does not replace content when preprompt is undefined\", async () => {\n const model = makeModel(1000);\n const messages = [{ from: \"system\" as const, content: \"original\" }];\n await buildPrompt({ messages, model, preprompt: undefined });\n expect(messages[0].content).toBe(\"original\");\n });\n\n it(\"truncates to the last N words when truncate is set\", async () => {\n const model = makeModel(3);\n const messages = [{ from: \"user\" as const, content: \"one two three four five\" }];\n const result = await buildPrompt({ messages, model });\n const wordCount = result.trim().split(/\\s+/).length;\n expect(wordCount).toBeLessThanOrEqual(3);\n });\n\n it(\"returns full prompt when truncate is 0 (no slicing)\", async () => {\n const model = makeModel(0);\n const messages = [{ from: \"user\" as const, content: \"word1 word2 word3\" }];\n const result = await buildPrompt({ messages, model });\n // slice(0) returns empty array → join → empty string\n expect(result).toBe(\"\");\n });\n\n it(\"returns full prompt when model has no truncate parameter\", async () => {\n const model = makeModel(undefined);\n const messages = [{ from: \"user\" as const, content: \"word1 word2 word3\" }];\n const result = await buildPrompt({ messages, model });\n expect(result).toContain(\"word1\");\n });\n\n it(\"maps message from to role field\", async () => {\n const receivedMessages: unknown[] = [];\n const model = {\n chatPromptRender: ({ messages }: { messages: unknown[] }) => {\n receivedMessages.push(...messages);\n return \"\";\n },\n parameters: { truncate: 1000 },\n } as unknown as BackendModel;\n await buildPrompt({\n messages: [{ from: \"assistant\" as const, content: \"hi\" }],\n model,\n });\n expect((receivedMessages[0] as any).role).toBe(\"assistant\");\n });\n});\n```\n\n---\n\n### 4. `src/lib/createShareLink.ts` — Zero test coverage\n\n**Untested:** 7-char ID shortcut path, successful API call, API failure with text body, API failure with empty body, URL prefix priority logic.\n\n```typescript\n// src/lib/createShareLink.spec.ts\nimport { describe, it, expect, vi, beforeEach } from \"vitest\";\n\n// Mock SvelteKit modules before importing\nvi.mock(\"$app/paths\", () => ({ base: \"\" }));\nvi.mock(\"$app/state\", () => ({\n page: {\n data: { publicConfig: { PUBLIC_SHARE_PREFIX: \"\", PUBLIC_ORIGIN: \"https://app.example.com\" } },\n url: { origin: \"https://fallback.example.com\" },\n },\n}));\n\nimport { createShareLink } from \"./createShareLink\";\n\ndescribe(\"createShareLink\", () => {\n beforeEach(() => {\n vi.resetAllMocks();\n });\n\n it(\"returns share URL directly for 7-char id without fetching\", async () => {\n const fetchSpy = vi.spyOn(globalThis, \"fetch\");\n const url = await createShareLink(\"abc1234\");\n expect(fetchSpy).not.toHaveBeenCalled();\n expect(url).toBe(\"https://app.example.com/r/abc1234\");\n });\n\n it(\"calls the share endpoint for non-7-char ids\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: true,\n json: () => Promise.resolve({ shareId: \"xyz9876\" }),\n } as Response);\n const url = await createShareLink(\"longconversationid\");\n expect(url).toBe(\"https://app.example.com/r/xyz9876\");\n });\n\n it(\"throws when the share endpoint returns non-ok with a text body\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: false,\n text: () => Promise.resolve(\"Conversation not found\"),\n } as unknown as Response);\n await expect(createShareLink(\"longconversationid\")).rejects.toThrow(\"Conversation not found\");\n });\n\n it(\"throws generic message when error body is empty\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: false,\n text: () => Promise.resolve(\"\"),\n } as unknown as Response);\n await expect(createShareLink(\"longconversationid\")).rejects.toThrow(\n \"Failed to create share link\"\n );\n });\n\n it(\"uses PUBLIC_SHARE_PREFIX when set\", async () => {\n const { page } = await import(\"$app/state\");\n (page.data.publicConfig as any).PUBLIC_SHARE_PREFIX = \"https://cdn.share.example.com\";\n const url = await createShareLink(\"abc1234\");\n expect(url).toStartWith(\"https://cdn.share.example.com\");\n (page.data.publicConfig as any).PUBLIC_SHARE_PREFIX = \"\";\n });\n});\n```\n\n---\n\n### 5. `src/lib/migrations/lock.ts` — Gaps in existing tests\n\nThe existing suite covers the happy path and concurrent acquisition. Missing:\n\n| Gap | Why it matters |\n|---|---|\n| `releaseLock` with mismatched `lockId` | Should not delete the wrong lock |\n| `refreshLock` on a non-existent lock | Returns `false` — not asserted anywhere |\n| `isDBLocked` with a different key | Isolation between semaphore keys |\n| `releaseLock` when lock already released | Should be idempotent |\n\n```typescript\n// Add to src/lib/migrations/migrations.spec.ts (inside the describe block)\n\nit(\"releaseLock does not delete lock held by different id\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n const fakeId = new ObjectId();\n await releaseLock(Semaphores.TEST_MIGRATION, fakeId); // wrong id\n expect(await isDBLocked(Semaphores.TEST_MIGRATION)).toBe(true);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId); // cleanup\n});\n\nit(\"refreshLock returns false when lock does not exist\", async () => {\n const fakeId = new ObjectId();\n const result = await refreshLock(Semaphores.TEST_MIGRATION, fakeId);\n expect(result).toBe(false);\n});\n\nit(\"isDBLocked is key-scoped — one key locked does not affect another\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n expect(await isDBLocked(\"other-key\")).toBe(false);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId);\n});\n\nit(\"releaseLock is idempotent — releasing twice does not throw\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId);\n await expect(releaseLock(Semaphores.TEST_MIGRATION, lockId)).resolves.not.toThrow();\n});\n```\n\n---\n\n### 6. `src/lib/migrations/migrations.ts` — Zero test coverage for `checkAndRunMigrations`\n\n**Untested:** duplicate GUID detection, lock contention wait-loop, `runEveryTime`, `runForHuggingChat` guards, migration failure recording, lock refresh interval, lock release on completion.\n\n```typescript\n// src/lib/migrations/checkAndRunMigrations.spec.ts\nimport { beforeEach, describe, expect, it, vi } from \"vitest\";\nimport type { Database } from \"$lib/server/database\";\nimport { Semaphores } from \"$lib/types/Semaphore\";\nimport ObjectId from \"bson-objectid\";\n\n// Stubs for all side-effecting modules\nvi.mock(\"./lock\");\nvi.mock(\"$lib/server/logger\", () => ({ logger: { debug: vi.fn(), error: vi.fn() } }));\nvi.mock(\"$lib/server/config\", () => ({ config: { isHuggingChat: false } }));\n\nimport * as lockModule from \"./lock\";\nimport { checkAndRunMigrations } from \"./migrations\";\n\nconst makeDb = (results: unknown[] = []) => ({\n getCollections: () => ({\n migrationResults: {\n find: () => ({ toArray: () => Promise.resolve(results) }),\n updateOne: vi.fn().mockResolvedValue({}),\n },\n }),\n});\n\nvi.mock(\"$lib/server/database\", () => ({\n Database: { getInstance: vi.fn() },\n}));\n\nimport { Database } from \"$lib/server/database\";\n\ndescribe(\"checkAndRunMigrations\", () => {\n beforeEach(() => {\n vi.clearAllMocks();\n vi.mocked(Database.getInstance).mockResolvedValue(makeDb() as unknown as Database);\n vi.mocked(lockModule.acquireLock).mockResolvedValue(new ObjectId());\n vi.mocked(lockModule.releaseLock).mockResolvedValue(undefined);\n vi.mocked(lockModule.isDBLocked).mockResolvedValue(false);\n vi.mocked(lockModule.refreshLock).mockResolvedValue(true);\n });\n\n it(\"throws when duplicate migration GUIDs exist\", async () => {\n // Override routines to inject duplicates — easiest with vi.doMock inside test\n await expect(async () => {\n vi.doMock(\"./routines\", () => {\n const id = new ObjectId();\n return { migrations: [{ _id: id, name: \"dup\", up: vi.fn() }, { _id: id, name: \"dup2\", up: vi.fn() }] };\n });\n const { checkAndRunMigrations: fn } = await import(\"./migrations?dup=1\");\n await fn();\n }).rejects.toThrow(\"Duplicate migration GUIDs found\");\n });\n\n it(\"waits for DB unlock when lock is already held\", async () => {\n vi.mocked(lockModule.acquireLock).mockResolvedValueOnce(false);\n // isDBLocked returns true once, then false to exit the while loop\n vi.mocked(lockModule.isDBLocked)\n .mockResolvedValueOnce(true)\n .mockResolvedValueOnce(false);\n await checkAndRunMigrations();\n expect(lockModule.isDBLocked).toHaveBeenCalledWith(Semaphores.MIGRATION);\n });\n\n it(\"skips migration already in results\", async () => {\n const id = new ObjectId();\n vi.mocked(Database.getInstance).mockResolvedValue(\n makeDb([{ _id: id, name: \"done\", status: \"success\" }]) as unknown as Database\n );\n const upFn = vi.fn().mockResolvedValue(true);\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"done\", up: upFn, runEveryTime: false }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?skip=1\");\n await fn();\n expect(upFn).not.toHaveBeenCalled();\n });\n\n it(\"records failure status when migration throws\", async () => {\n const id = new ObjectId();\n const updateOne = vi.fn().mockResolvedValue({});\n vi.mocked(Database.getInstance).mockResolvedValue({\n getCollections: () => ({\n migrationResults: { find: () => ({ toArray: () => Promise.resolve([]) }), updateOne },\n }),\n } as unknown as Database);\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"failing\", up: vi.fn().mockRejectedValue(new Error(\"boom\")) }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?fail=1\");\n await fn();\n const lastCall = updateOne.mock.calls.at(-1)?.[1].$set;\n expect(lastCall?.status).toBe(\"failure\");\n });\n\n it(\"skips runForHuggingChat=only migration when not HuggingChat\", async () => {\n const { config } = await import(\"$lib/server/config\");\n (config as any).isHuggingChat = false;\n const upFn = vi.fn().mockResolvedValue(true);\n const id = new ObjectId();\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"hc-only\", up: upFn, runForHuggingChat: \"only\" }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?hconly=1\");\n await fn();\n expect(upFn).not.toHaveBeenCalled();\n });\n\n it(\"releases lock and clears interval after all migrations run\", async () => {\n vi.useFakeTimers();\n await checkAndRunMigrations();\n vi.useRealTimers();\n expect(lockModule.releaseLock).toHaveBeenCalledWith(\n Semaphores.MIGRATION,\n expect.any(ObjectId)\n );\n });\n});\n```\n\n---\n\n### 7. `src/lib/constants/rvagentPresets.ts` — Utility functions untested\n\n**Untested:** `getPresetById`, `buildPresetUrl`, `buildPresetCliCommand`.\n\n```typescript\n// src/lib/constants/rvagentPresets.spec.ts\nimport { describe, it, expect } from \"vitest\";\nimport {\n getPresetById,\n buildPresetUrl,\n buildPresetCliCommand,\n RVAGENT_PRESETS,\n} from \"./rvagentPresets\";\n\ndescribe(\"getPresetById\", () => {\n it(\"returns the matching preset\", () => {\n const preset = getPresetById(\"all-tools\");\n expect(preset).toBeDefined();\n expect(preset?.id).toBe(\"all-tools\");\n });\n\n it(\"returns undefined for an unknown id\", () => {\n expect(getPresetById(\"nonexistent\")).toBeUndefined();\n });\n\n it(\"all preset IDs in RVAGENT_PRESETS are findable\", () => {\n for (const p of RVAGENT_PRESETS) {\n expect(getPresetById(p.id)).toBe(p);\n }\n });\n});\n\ndescribe(\"buildPresetUrl\", () => {\n it(\"uses preset defaultPort when no port override is given\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset)).toBe(`http://localhost:${preset.defaultPort}/sse`);\n });\n\n it(\"uses the overridden port\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset, \"localhost\", 9999)).toBe(\"http://localhost:9999/sse\");\n });\n\n it(\"uses custom host\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset, \"192.168.1.10\")).toContain(\"192.168.1.10\");\n });\n});\n\ndescribe(\"buildPresetCliCommand\", () => {\n it(\"uses --all flag for the all-tools preset\", () => {\n const preset = getPresetById(\"all-tools\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(\"--all\");\n expect(cmd).not.toContain(\"--groups\");\n });\n\n it(\"uses --groups for presets with specific tool groups\", () => {\n const preset = getPresetById(\"file-shell\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(\"--groups file,shell\");\n expect(cmd).not.toContain(\"--all\");\n });\n\n it(\"uses custom port in the command\", () => {\n const preset = getPresetById(\"memory-agent\")!;\n const cmd = buildPresetCliCommand(preset, 8888);\n expect(cmd).toContain(\"--port 8888\");\n });\n\n it(\"falls back to defaultPort in the command when no port given\", () => {\n const preset = getPresetById(\"memory-agent\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(`--port ${preset.defaultPort}`);\n });\n});\n```\n\n---\n\n### 8. `src/lib/components/chat/MarkdownRenderer.svelte.test.ts` — Edge case gaps\n\n**Missing:** XSS via `javascript:` links, empty content, bold/italic inside lists, tables, image rendering, multiple code blocks with language labels.\n\n```typescript\n// Add to existing MarkdownRenderer.svelte.test.ts\n\ndescribe(\"MarkdownRenderer — security\", () => {\n it(\"strips javascript: href from links\", () => {\n const { baseElement } = render(MarkdownRenderer, {\n content: \"[click me](javascript:alert(1))\",\n });\n const anchor = baseElement.querySelector(\"a\");\n expect(anchor?.getAttribute(\"href\")).not.toContain(\"javascript:\");\n });\n\n it(\"renders empty content without crashing\", () => {\n expect(() => render(MarkdownRenderer, { content: \"\" })).not.toThrow();\n });\n});\n\ndescribe(\"MarkdownRenderer — tables\", () => {\n it(\"renders a markdown table\", () => {\n const md = \"| A | B |\\n|---|---|\\n| 1 | 2 |\";\n const { baseElement } = render(MarkdownRenderer, { content: md });\n expect(baseElement.querySelector(\"table\")).toBeTruthy();\n expect(baseElement.querySelector(\"thead\")).toBeTruthy();\n expect(baseElement.querySelector(\"tbody\")).toBeTruthy();\n });\n});\n\ndescribe(\"MarkdownRenderer — nested formatting\", () => {\n it(\"renders bold inside a list item\", () => {\n const { baseElement } = render(MarkdownRenderer, { content: \"- **bold item**\" });\n const strong = baseElement.querySelector(\"strong\");\n expect(strong).toBeTruthy();\n expect(strong?.textContent).toBe(\"bold item\");\n });\n\n it(\"renders multiple fenced code blocks with language labels\", () => {\n const md = \"```ts\\nconst x = 1;\\n```\\n\\n```py\\nprint('hi')\\n```\";\n const { baseElement } = render(MarkdownRenderer, { content: md });\n const codeBlocks = baseElement.querySelectorAll(\"code\");\n expect(codeBlocks.length).toBeGreaterThanOrEqual(2);\n });\n});\n\ndescribe(\"MarkdownRenderer — images\", () => {\n it(\"renders an image with alt text\", () => {\n const { baseElement } = render(MarkdownRenderer, {\n content: \"![Alt text](https://example.com/img.png)\",\n });\n const img = baseElement.querySelector(\"img\");\n expect(img).toBeTruthy();\n expect(img?.getAttribute(\"alt\")).toBe(\"Alt text\");\n });\n});\n```\n\n---\n\n### Summary of Gaps\n\n| File | Coverage | Priority gaps |\n|---|---|---|\n| `APIClient.ts` | None | Error fallback chain, empty body, query filtering, `handleResponse` |\n| `clickOutside.ts` | None | All branches: outside, inside, nested, update, destroy |\n| `buildPrompt.ts` | None | Preprompt replacement, truncation edge cases (0, undefined) |\n| `createShareLink.ts` | None | 7-char shortcut, API failure, prefix priority |\n| `lock.ts` | Partial | `refreshLock(false)`, mismatched lockId, key isolation |\n| `migrations.ts` | None | Duplicate GUID, lock contention, `runEveryTime`, HuggingChat guard, failure recording |\n| `rvagentPresets.ts` | None | All three exported functions |\n| `MarkdownRenderer` | Partial | XSS links, empty content, tables, images, nested formatting |\n",
6
+ "parsedOutput": {
7
+ "sections": [
8
+ {
9
+ "title": "Test Coverage Gap Analysis",
10
+ "content": "\n",
11
+ "level": 2
12
+ },
13
+ {
14
+ "title": "1. `src/lib/APIClient.ts` — Zero test coverage",
15
+ "content": "\n**Untested:** `apiCall` (all branches), `handleResponse`, query param serialization, error body parsing fallback chain, ObjectId superjson registration.\n\n```typescript\n// src/lib/APIClient.spec.ts\nimport { describe, it, expect, vi, afterEach } from \"vitest\";\nimport { handleResponse, useAPIClient } from \"./APIClient\";\n\nconst mockFetch = (status: number, body: string | null, ok = status < 400) =>\n vi.fn().mockResolvedValue({\n ok,\n status,\n text: () => Promise.resolve(body ?? \"\"),\n json: () => (body ? Promise.resolve(JSON.parse(body)) : Promise.reject(new Error(\"no body\"))),\n statusText: \"Error\",\n });\n\ndescribe(\"apiCall — success paths\", () => {\n it(\"sends GET with no body\", async () => {\n const fetch = mockFetch(200, '{\"json\":\"body\"}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client[\"public-config\"].get();\n expect(fetch).toHaveBeenCalledWith(\n expect.stringContaining(\"/api/v2/public-config\"),\n expect.objectContaining({ method: \"GET\" })\n );\n });\n\n it(\"attaches query params to GET URL\", async () => {\n const fetch = mockFetch(200, '{\"x\":1}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client[\"public-config\"].get({ query: { page: 2, skip: undefined } });\n const url = fetch.mock.calls[0][0] as string;\n expect(url).toContain(\"page=2\");\n expect(url).not.toContain(\"skip\"); // undefined values are omitted\n });\n\n it(\"returns null data on empty 200 body\", async () => {\n const fetch = mockFetch(200, \"\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.settings.get();\n expect(res.data).toBeNull();\n expect(res.error).toBeNull();\n });\n\n it(\"serialises body as JSON for POST\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client.user.settings.post({ theme: \"dark\" });\n const init = fetch.mock.calls[0][1] as RequestInit;\n expect(init.headers).toMatchObject({ \"Content-Type\": \"application/json\" });\n expect(JSON.parse(init.body as string)).toEqual({ theme: \"dark\" });\n });\n});\n\ndescribe(\"apiCall — error paths\", () => {\n it(\"returns error with JSON body on 4xx\", async () => {\n const fetch = mockFetch(400, '{\"message\":\"bad request\"}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toEqual({ message: \"bad request\" });\n expect(res.data).toBeNull();\n expect(res.status).toBe(400);\n });\n\n it(\"falls back to text when error body is not JSON\", async () => {\n const fetch = vi.fn().mockResolvedValue({\n ok: false,\n status: 500,\n json: () => Promise.reject(new Error(\"not json\")),\n text: () => Promise.resolve(\"Internal Server Error\"),\n statusText: \"ISE\",\n });\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toBe(\"Internal Server Error\");\n });\n\n it(\"falls back to statusText when text() also fails\", async () => {\n const fetch = vi.fn().mockResolvedValue({\n ok: false,\n status: 503,\n json: () => Promise.reject(new Error()),\n text: () => Promise.reject(new Error()),\n statusText: \"Service Unavailable\",\n });\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toBe(\"Service Unavailable\");\n });\n});\n\ndescribe(\"handleResponse\", () => {\n it(\"returns null when data is null and no error\", () => {\n expect(handleResponse({ data: null, error: null, status: 204 })).toBeNull();\n });\n\n it(\"throws when response contains an error\", () => {\n expect(() =>\n handleResponse({ data: null, error: { message: \"forbidden\" }, status: 403 })\n ).toThrow();\n });\n\n it(\"parses superjson string data\", () => {\n const payload = JSON.stringify({ json: { value: 42 }, meta: undefined });\n expect(handleResponse({ data: payload, error: null, status: 200 })).toEqual({ value: 42 });\n });\n});\n\ndescribe(\"useAPIClient — URL construction\", () => {\n it(\"uses provided origin for SSR base URL\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://api.internal:4000\" });\n await client.export.get();\n expect(fetch.mock.calls[0][0]).toContain(\"http://api.internal:4000\");\n });\n\n it(\"builds nested conversation message endpoint\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client.conversations({ id: \"abc123\" }).message({ messageId: \"msg456\" }).get();\n expect(fetch.mock.calls[0][0]).toContain(\"/conversations/abc123/message/msg456\");\n });\n});\n```\n\n---\n\n",
16
+ "level": 3
17
+ },
18
+ {
19
+ "title": "2. `src/lib/actions/clickOutside.ts` — Zero test coverage",
20
+ "content": "\n**Untested:** click outside triggers callback, click inside does not, `update` replaces callback, `destroy` removes listener.\n\n```typescript\n// src/lib/actions/clickOutside.spec.ts\nimport { describe, it, expect, vi, beforeEach } from \"vitest\";\nimport { clickOutside } from \"./clickOutside\";\n\ndescribe(\"clickOutside action\", () => {\n let element: HTMLDivElement;\n let outsideElement: HTMLDivElement;\n let callback: ReturnType<typeof vi.fn>;\n\n beforeEach(() => {\n document.body.innerHTML = \"\";\n element = document.createElement(\"div\");\n outsideElement = document.createElement(\"div\");\n document.body.appendChild(element);\n document.body.appendChild(outsideElement);\n callback = vi.fn();\n });\n\n it(\"fires callback when clicking outside the element\", () => {\n clickOutside(element, callback);\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).toHaveBeenCalledOnce();\n });\n\n it(\"does not fire callback when clicking inside the element\", () => {\n clickOutside(element, callback);\n element.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"does not fire when clicking the element itself\", () => {\n clickOutside(element, callback);\n const inner = document.createElement(\"span\");\n element.appendChild(inner);\n inner.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"update() replaces the callback\", () => {\n const action = clickOutside(element, callback);\n const newCallback = vi.fn();\n action.update(newCallback);\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n expect(newCallback).toHaveBeenCalledOnce();\n });\n\n it(\"destroy() stops all future callbacks\", () => {\n const action = clickOutside(element, callback);\n action.destroy();\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"handles multiple destroy() calls without error\", () => {\n const action = clickOutside(element, callback);\n expect(() => { action.destroy(); action.destroy(); }).not.toThrow();\n });\n});\n```\n\n---\n\n",
21
+ "level": 3
22
+ },
23
+ {
24
+ "title": "3. `src/lib/buildPrompt.ts` — Zero test coverage",
25
+ "content": "\n**Untested:** system message replacement with preprompt, no system message scenario, truncation slicing, zero truncate parameter (no slicing).\n\n```typescript\n// src/lib/buildPrompt.spec.ts\nimport { describe, it, expect } from \"vitest\";\nimport { buildPrompt } from \"./buildPrompt\";\nimport type { BackendModel } from \"./server/models\";\n\nconst makeModel = (truncate?: number): BackendModel =>\n ({\n chatPromptRender: ({ messages, preprompt }: { messages: unknown[]; preprompt?: string }) =>\n messages.map((m: any) => `${m.role}:${m.content}`).join(\" \"),\n parameters: truncate !== undefined ? { truncate } : undefined,\n } as unknown as BackendModel);\n\ndescribe(\"buildPrompt\", () => {\n it(\"replaces system message content with preprompt when first message is system\", async () => {\n const model = makeModel(1000);\n const messages = [\n { from: \"system\" as const, content: \"old system prompt\" },\n { from: \"user\" as const, content: \"hello\" },\n ];\n const result = await buildPrompt({ messages, model, preprompt: \"new system prompt\" });\n expect(result).toContain(\"new system prompt\");\n expect(result).not.toContain(\"old system prompt\");\n });\n\n it(\"does not replace content when first message is not system\", async () => {\n const model = makeModel(1000);\n const messages = [{ from: \"user\" as const, content: \"hello\" }];\n await buildPrompt({ messages, model, preprompt: \"some preprompt\" });\n // original content unchanged\n expect(messages[0].content).toBe(\"hello\");\n });\n\n it(\"does not replace content when preprompt is undefined\", async () => {\n const model = makeModel(1000);\n const messages = [{ from: \"system\" as const, content: \"original\" }];\n await buildPrompt({ messages, model, preprompt: undefined });\n expect(messages[0].content).toBe(\"original\");\n });\n\n it(\"truncates to the last N words when truncate is set\", async () => {\n const model = makeModel(3);\n const messages = [{ from: \"user\" as const, content: \"one two three four five\" }];\n const result = await buildPrompt({ messages, model });\n const wordCount = result.trim().split(/\\s+/).length;\n expect(wordCount).toBeLessThanOrEqual(3);\n });\n\n it(\"returns full prompt when truncate is 0 (no slicing)\", async () => {\n const model = makeModel(0);\n const messages = [{ from: \"user\" as const, content: \"word1 word2 word3\" }];\n const result = await buildPrompt({ messages, model });\n // slice(0) returns empty array → join → empty string\n expect(result).toBe(\"\");\n });\n\n it(\"returns full prompt when model has no truncate parameter\", async () => {\n const model = makeModel(undefined);\n const messages = [{ from: \"user\" as const, content: \"word1 word2 word3\" }];\n const result = await buildPrompt({ messages, model });\n expect(result).toContain(\"word1\");\n });\n\n it(\"maps message from to role field\", async () => {\n const receivedMessages: unknown[] = [];\n const model = {\n chatPromptRender: ({ messages }: { messages: unknown[] }) => {\n receivedMessages.push(...messages);\n return \"\";\n },\n parameters: { truncate: 1000 },\n } as unknown as BackendModel;\n await buildPrompt({\n messages: [{ from: \"assistant\" as const, content: \"hi\" }],\n model,\n });\n expect((receivedMessages[0] as any).role).toBe(\"assistant\");\n });\n});\n```\n\n---\n\n",
26
+ "level": 3
27
+ },
28
+ {
29
+ "title": "4. `src/lib/createShareLink.ts` — Zero test coverage",
30
+ "content": "\n**Untested:** 7-char ID shortcut path, successful API call, API failure with text body, API failure with empty body, URL prefix priority logic.\n\n```typescript\n// src/lib/createShareLink.spec.ts\nimport { describe, it, expect, vi, beforeEach } from \"vitest\";\n\n// Mock SvelteKit modules before importing\nvi.mock(\"$app/paths\", () => ({ base: \"\" }));\nvi.mock(\"$app/state\", () => ({\n page: {\n data: { publicConfig: { PUBLIC_SHARE_PREFIX: \"\", PUBLIC_ORIGIN: \"https://app.example.com\" } },\n url: { origin: \"https://fallback.example.com\" },\n },\n}));\n\nimport { createShareLink } from \"./createShareLink\";\n\ndescribe(\"createShareLink\", () => {\n beforeEach(() => {\n vi.resetAllMocks();\n });\n\n it(\"returns share URL directly for 7-char id without fetching\", async () => {\n const fetchSpy = vi.spyOn(globalThis, \"fetch\");\n const url = await createShareLink(\"abc1234\");\n expect(fetchSpy).not.toHaveBeenCalled();\n expect(url).toBe(\"https://app.example.com/r/abc1234\");\n });\n\n it(\"calls the share endpoint for non-7-char ids\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: true,\n json: () => Promise.resolve({ shareId: \"xyz9876\" }),\n } as Response);\n const url = await createShareLink(\"longconversationid\");\n expect(url).toBe(\"https://app.example.com/r/xyz9876\");\n });\n\n it(\"throws when the share endpoint returns non-ok with a text body\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: false,\n text: () => Promise.resolve(\"Conversation not found\"),\n } as unknown as Response);\n await expect(createShareLink(\"longconversationid\")).rejects.toThrow(\"Conversation not found\");\n });\n\n it(\"throws generic message when error body is empty\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: false,\n text: () => Promise.resolve(\"\"),\n } as unknown as Response);\n await expect(createShareLink(\"longconversationid\")).rejects.toThrow(\n \"Failed to create share link\"\n );\n });\n\n it(\"uses PUBLIC_SHARE_PREFIX when set\", async () => {\n const { page } = await import(\"$app/state\");\n (page.data.publicConfig as any).PUBLIC_SHARE_PREFIX = \"https://cdn.share.example.com\";\n const url = await createShareLink(\"abc1234\");\n expect(url).toStartWith(\"https://cdn.share.example.com\");\n (page.data.publicConfig as any).PUBLIC_SHARE_PREFIX = \"\";\n });\n});\n```\n\n---\n\n",
31
+ "level": 3
32
+ },
33
+ {
34
+ "title": "5. `src/lib/migrations/lock.ts` — Gaps in existing tests",
35
+ "content": "\nThe existing suite covers the happy path and concurrent acquisition. Missing:\n\n| Gap | Why it matters |\n|---|---|\n| `releaseLock` with mismatched `lockId` | Should not delete the wrong lock |\n| `refreshLock` on a non-existent lock | Returns `false` — not asserted anywhere |\n| `isDBLocked` with a different key | Isolation between semaphore keys |\n| `releaseLock` when lock already released | Should be idempotent |\n\n```typescript\n// Add to src/lib/migrations/migrations.spec.ts (inside the describe block)\n\nit(\"releaseLock does not delete lock held by different id\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n const fakeId = new ObjectId();\n await releaseLock(Semaphores.TEST_MIGRATION, fakeId); // wrong id\n expect(await isDBLocked(Semaphores.TEST_MIGRATION)).toBe(true);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId); // cleanup\n});\n\nit(\"refreshLock returns false when lock does not exist\", async () => {\n const fakeId = new ObjectId();\n const result = await refreshLock(Semaphores.TEST_MIGRATION, fakeId);\n expect(result).toBe(false);\n});\n\nit(\"isDBLocked is key-scoped — one key locked does not affect another\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n expect(await isDBLocked(\"other-key\")).toBe(false);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId);\n});\n\nit(\"releaseLock is idempotent — releasing twice does not throw\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId);\n await expect(releaseLock(Semaphores.TEST_MIGRATION, lockId)).resolves.not.toThrow();\n});\n```\n\n---\n\n",
36
+ "level": 3
37
+ },
38
+ {
39
+ "title": "6. `src/lib/migrations/migrations.ts` — Zero test coverage for `checkAndRunMigrations`",
40
+ "content": "\n**Untested:** duplicate GUID detection, lock contention wait-loop, `runEveryTime`, `runForHuggingChat` guards, migration failure recording, lock refresh interval, lock release on completion.\n\n```typescript\n// src/lib/migrations/checkAndRunMigrations.spec.ts\nimport { beforeEach, describe, expect, it, vi } from \"vitest\";\nimport type { Database } from \"$lib/server/database\";\nimport { Semaphores } from \"$lib/types/Semaphore\";\nimport ObjectId from \"bson-objectid\";\n\n// Stubs for all side-effecting modules\nvi.mock(\"./lock\");\nvi.mock(\"$lib/server/logger\", () => ({ logger: { debug: vi.fn(), error: vi.fn() } }));\nvi.mock(\"$lib/server/config\", () => ({ config: { isHuggingChat: false } }));\n\nimport * as lockModule from \"./lock\";\nimport { checkAndRunMigrations } from \"./migrations\";\n\nconst makeDb = (results: unknown[] = []) => ({\n getCollections: () => ({\n migrationResults: {\n find: () => ({ toArray: () => Promise.resolve(results) }),\n updateOne: vi.fn().mockResolvedValue({}),\n },\n }),\n});\n\nvi.mock(\"$lib/server/database\", () => ({\n Database: { getInstance: vi.fn() },\n}));\n\nimport { Database } from \"$lib/server/database\";\n\ndescribe(\"checkAndRunMigrations\", () => {\n beforeEach(() => {\n vi.clearAllMocks();\n vi.mocked(Database.getInstance).mockResolvedValue(makeDb() as unknown as Database);\n vi.mocked(lockModule.acquireLock).mockResolvedValue(new ObjectId());\n vi.mocked(lockModule.releaseLock).mockResolvedValue(undefined);\n vi.mocked(lockModule.isDBLocked).mockResolvedValue(false);\n vi.mocked(lockModule.refreshLock).mockResolvedValue(true);\n });\n\n it(\"throws when duplicate migration GUIDs exist\", async () => {\n // Override routines to inject duplicates — easiest with vi.doMock inside test\n await expect(async () => {\n vi.doMock(\"./routines\", () => {\n const id = new ObjectId();\n return { migrations: [{ _id: id, name: \"dup\", up: vi.fn() }, { _id: id, name: \"dup2\", up: vi.fn() }] };\n });\n const { checkAndRunMigrations: fn } = await import(\"./migrations?dup=1\");\n await fn();\n }).rejects.toThrow(\"Duplicate migration GUIDs found\");\n });\n\n it(\"waits for DB unlock when lock is already held\", async () => {\n vi.mocked(lockModule.acquireLock).mockResolvedValueOnce(false);\n // isDBLocked returns true once, then false to exit the while loop\n vi.mocked(lockModule.isDBLocked)\n .mockResolvedValueOnce(true)\n .mockResolvedValueOnce(false);\n await checkAndRunMigrations();\n expect(lockModule.isDBLocked).toHaveBeenCalledWith(Semaphores.MIGRATION);\n });\n\n it(\"skips migration already in results\", async () => {\n const id = new ObjectId();\n vi.mocked(Database.getInstance).mockResolvedValue(\n makeDb([{ _id: id, name: \"done\", status: \"success\" }]) as unknown as Database\n );\n const upFn = vi.fn().mockResolvedValue(true);\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"done\", up: upFn, runEveryTime: false }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?skip=1\");\n await fn();\n expect(upFn).not.toHaveBeenCalled();\n });\n\n it(\"records failure status when migration throws\", async () => {\n const id = new ObjectId();\n const updateOne = vi.fn().mockResolvedValue({});\n vi.mocked(Database.getInstance).mockResolvedValue({\n getCollections: () => ({\n migrationResults: { find: () => ({ toArray: () => Promise.resolve([]) }), updateOne },\n }),\n } as unknown as Database);\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"failing\", up: vi.fn().mockRejectedValue(new Error(\"boom\")) }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?fail=1\");\n await fn();\n const lastCall = updateOne.mock.calls.at(-1)?.[1].$set;\n expect(lastCall?.status).toBe(\"failure\");\n });\n\n it(\"skips runForHuggingChat=only migration when not HuggingChat\", async () => {\n const { config } = await import(\"$lib/server/config\");\n (config as any).isHuggingChat = false;\n const upFn = vi.fn().mockResolvedValue(true);\n const id = new ObjectId();\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"hc-only\", up: upFn, runForHuggingChat: \"only\" }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?hconly=1\");\n await fn();\n expect(upFn).not.toHaveBeenCalled();\n });\n\n it(\"releases lock and clears interval after all migrations run\", async () => {\n vi.useFakeTimers();\n await checkAndRunMigrations();\n vi.useRealTimers();\n expect(lockModule.releaseLock).toHaveBeenCalledWith(\n Semaphores.MIGRATION,\n expect.any(ObjectId)\n );\n });\n});\n```\n\n---\n\n",
41
+ "level": 3
42
+ },
43
+ {
44
+ "title": "7. `src/lib/constants/rvagentPresets.ts` — Utility functions untested",
45
+ "content": "\n**Untested:** `getPresetById`, `buildPresetUrl`, `buildPresetCliCommand`.\n\n```typescript\n// src/lib/constants/rvagentPresets.spec.ts\nimport { describe, it, expect } from \"vitest\";\nimport {\n getPresetById,\n buildPresetUrl,\n buildPresetCliCommand,\n RVAGENT_PRESETS,\n} from \"./rvagentPresets\";\n\ndescribe(\"getPresetById\", () => {\n it(\"returns the matching preset\", () => {\n const preset = getPresetById(\"all-tools\");\n expect(preset).toBeDefined();\n expect(preset?.id).toBe(\"all-tools\");\n });\n\n it(\"returns undefined for an unknown id\", () => {\n expect(getPresetById(\"nonexistent\")).toBeUndefined();\n });\n\n it(\"all preset IDs in RVAGENT_PRESETS are findable\", () => {\n for (const p of RVAGENT_PRESETS) {\n expect(getPresetById(p.id)).toBe(p);\n }\n });\n});\n\ndescribe(\"buildPresetUrl\", () => {\n it(\"uses preset defaultPort when no port override is given\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset)).toBe(`http://localhost:${preset.defaultPort}/sse`);\n });\n\n it(\"uses the overridden port\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset, \"localhost\", 9999)).toBe(\"http://localhost:9999/sse\");\n });\n\n it(\"uses custom host\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset, \"192.168.1.10\")).toContain(\"192.168.1.10\");\n });\n});\n\ndescribe(\"buildPresetCliCommand\", () => {\n it(\"uses --all flag for the all-tools preset\", () => {\n const preset = getPresetById(\"all-tools\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(\"--all\");\n expect(cmd).not.toContain(\"--groups\");\n });\n\n it(\"uses --groups for presets with specific tool groups\", () => {\n const preset = getPresetById(\"file-shell\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(\"--groups file,shell\");\n expect(cmd).not.toContain(\"--all\");\n });\n\n it(\"uses custom port in the command\", () => {\n const preset = getPresetById(\"memory-agent\")!;\n const cmd = buildPresetCliCommand(preset, 8888);\n expect(cmd).toContain(\"--port 8888\");\n });\n\n it(\"falls back to defaultPort in the command when no port given\", () => {\n const preset = getPresetById(\"memory-agent\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(`--port ${preset.defaultPort}`);\n });\n});\n```\n\n---\n\n",
46
+ "level": 3
47
+ },
48
+ {
49
+ "title": "8. `src/lib/components/chat/MarkdownRenderer.svelte.test.ts` — Edge case gaps",
50
+ "content": "\n**Missing:** XSS via `javascript:` links, empty content, bold/italic inside lists, tables, image rendering, multiple code blocks with language labels.\n\n```typescript\n// Add to existing MarkdownRenderer.svelte.test.ts\n\ndescribe(\"MarkdownRenderer — security\", () => {\n it(\"strips javascript: href from links\", () => {\n const { baseElement } = render(MarkdownRenderer, {\n content: \"[click me](javascript:alert(1))\",\n });\n const anchor = baseElement.querySelector(\"a\");\n expect(anchor?.getAttribute(\"href\")).not.toContain(\"javascript:\");\n });\n\n it(\"renders empty content without crashing\", () => {\n expect(() => render(MarkdownRenderer, { content: \"\" })).not.toThrow();\n });\n});\n\ndescribe(\"MarkdownRenderer — tables\", () => {\n it(\"renders a markdown table\", () => {\n const md = \"| A | B |\\n|---|---|\\n| 1 | 2 |\";\n const { baseElement } = render(MarkdownRenderer, { content: md });\n expect(baseElement.querySelector(\"table\")).toBeTruthy();\n expect(baseElement.querySelector(\"thead\")).toBeTruthy();\n expect(baseElement.querySelector(\"tbody\")).toBeTruthy();\n });\n});\n\ndescribe(\"MarkdownRenderer — nested formatting\", () => {\n it(\"renders bold inside a list item\", () => {\n const { baseElement } = render(MarkdownRenderer, { content: \"- **bold item**\" });\n const strong = baseElement.querySelector(\"strong\");\n expect(strong).toBeTruthy();\n expect(strong?.textContent).toBe(\"bold item\");\n });\n\n it(\"renders multiple fenced code blocks with language labels\", () => {\n const md = \"```ts\\nconst x = 1;\\n```\\n\\n```py\\nprint('hi')\\n```\";\n const { baseElement } = render(MarkdownRenderer, { content: md });\n const codeBlocks = baseElement.querySelectorAll(\"code\");\n expect(codeBlocks.length).toBeGreaterThanOrEqual(2);\n });\n});\n\ndescribe(\"MarkdownRenderer — images\", () => {\n it(\"renders an image with alt text\", () => {\n const { baseElement } = render(MarkdownRenderer, {\n content: \"![Alt text](https://example.com/img.png)\",\n });\n const img = baseElement.querySelector(\"img\");\n expect(img).toBeTruthy();\n expect(img?.getAttribute(\"alt\")).toBe(\"Alt text\");\n });\n});\n```\n\n---\n\n",
51
+ "level": 3
52
+ },
53
+ {
54
+ "title": "Summary of Gaps",
55
+ "content": "| File | Coverage | Priority gaps |\n|---|---|---|\n| `APIClient.ts` | None | Error fallback chain, empty body, query filtering, `handleResponse` |\n| `clickOutside.ts` | None | All branches: outside, inside, nested, update, destroy |\n| `buildPrompt.ts` | None | Preprompt replacement, truncation edge cases (0, undefined) |\n| `createShareLink.ts` | None | 7-char shortcut, API failure, prefix priority |\n| `lock.ts` | Partial | `refreshLock(false)`, mismatched lockId, key isolation |\n| `migrations.ts` | None | Duplicate GUID, lock contention, `runEveryTime`, HuggingChat guard, failure recording |\n| `rvagentPresets.ts` | None | All three exported functions |\n| `MarkdownRenderer` | Partial | XSS links, empty content, tables, images, nested formatting |",
56
+ "level": 3
57
+ }
58
+ ],
59
+ "codeBlocks": [
60
+ {
61
+ "language": "typescript",
62
+ "code": "// src/lib/APIClient.spec.ts\nimport { describe, it, expect, vi, afterEach } from \"vitest\";\nimport { handleResponse, useAPIClient } from \"./APIClient\";\n\nconst mockFetch = (status: number, body: string | null, ok = status < 400) =>\n vi.fn().mockResolvedValue({\n ok,\n status,\n text: () => Promise.resolve(body ?? \"\"),\n json: () => (body ? Promise.resolve(JSON.parse(body)) : Promise.reject(new Error(\"no body\"))),\n statusText: \"Error\",\n });\n\ndescribe(\"apiCall — success paths\", () => {\n it(\"sends GET with no body\", async () => {\n const fetch = mockFetch(200, '{\"json\":\"body\"}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client[\"public-config\"].get();\n expect(fetch).toHaveBeenCalledWith(\n expect.stringContaining(\"/api/v2/public-config\"),\n expect.objectContaining({ method: \"GET\" })\n );\n });\n\n it(\"attaches query params to GET URL\", async () => {\n const fetch = mockFetch(200, '{\"x\":1}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client[\"public-config\"].get({ query: { page: 2, skip: undefined } });\n const url = fetch.mock.calls[0][0] as string;\n expect(url).toContain(\"page=2\");\n expect(url).not.toContain(\"skip\"); // undefined values are omitted\n });\n\n it(\"returns null data on empty 200 body\", async () => {\n const fetch = mockFetch(200, \"\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.settings.get();\n expect(res.data).toBeNull();\n expect(res.error).toBeNull();\n });\n\n it(\"serialises body as JSON for POST\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client.user.settings.post({ theme: \"dark\" });\n const init = fetch.mock.calls[0][1] as RequestInit;\n expect(init.headers).toMatchObject({ \"Content-Type\": \"application/json\" });\n expect(JSON.parse(init.body as string)).toEqual({ theme: \"dark\" });\n });\n});\n\ndescribe(\"apiCall — error paths\", () => {\n it(\"returns error with JSON body on 4xx\", async () => {\n const fetch = mockFetch(400, '{\"message\":\"bad request\"}');\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toEqual({ message: \"bad request\" });\n expect(res.data).toBeNull();\n expect(res.status).toBe(400);\n });\n\n it(\"falls back to text when error body is not JSON\", async () => {\n const fetch = vi.fn().mockResolvedValue({\n ok: false,\n status: 500,\n json: () => Promise.reject(new Error(\"not json\")),\n text: () => Promise.resolve(\"Internal Server Error\"),\n statusText: \"ISE\",\n });\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toBe(\"Internal Server Error\");\n });\n\n it(\"falls back to statusText when text() also fails\", async () => {\n const fetch = vi.fn().mockResolvedValue({\n ok: false,\n status: 503,\n json: () => Promise.reject(new Error()),\n text: () => Promise.reject(new Error()),\n statusText: \"Service Unavailable\",\n });\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n const res = await client.user.get();\n expect(res.error).toBe(\"Service Unavailable\");\n });\n});\n\ndescribe(\"handleResponse\", () => {\n it(\"returns null when data is null and no error\", () => {\n expect(handleResponse({ data: null, error: null, status: 204 })).toBeNull();\n });\n\n it(\"throws when response contains an error\", () => {\n expect(() =>\n handleResponse({ data: null, error: { message: \"forbidden\" }, status: 403 })\n ).toThrow();\n });\n\n it(\"parses superjson string data\", () => {\n const payload = JSON.stringify({ json: { value: 42 }, meta: undefined });\n expect(handleResponse({ data: payload, error: null, status: 200 })).toEqual({ value: 42 });\n });\n});\n\ndescribe(\"useAPIClient — URL construction\", () => {\n it(\"uses provided origin for SSR base URL\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://api.internal:4000\" });\n await client.export.get();\n expect(fetch.mock.calls[0][0]).toContain(\"http://api.internal:4000\");\n });\n\n it(\"builds nested conversation message endpoint\", async () => {\n const fetch = mockFetch(200, \"null\");\n const client = useAPIClient({ fetch, origin: \"http://localhost\" });\n await client.conversations({ id: \"abc123\" }).message({ messageId: \"msg456\" }).get();\n expect(fetch.mock.calls[0][0]).toContain(\"/conversations/abc123/message/msg456\");\n });\n});"
63
+ },
64
+ {
65
+ "language": "typescript",
66
+ "code": "// src/lib/actions/clickOutside.spec.ts\nimport { describe, it, expect, vi, beforeEach } from \"vitest\";\nimport { clickOutside } from \"./clickOutside\";\n\ndescribe(\"clickOutside action\", () => {\n let element: HTMLDivElement;\n let outsideElement: HTMLDivElement;\n let callback: ReturnType<typeof vi.fn>;\n\n beforeEach(() => {\n document.body.innerHTML = \"\";\n element = document.createElement(\"div\");\n outsideElement = document.createElement(\"div\");\n document.body.appendChild(element);\n document.body.appendChild(outsideElement);\n callback = vi.fn();\n });\n\n it(\"fires callback when clicking outside the element\", () => {\n clickOutside(element, callback);\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).toHaveBeenCalledOnce();\n });\n\n it(\"does not fire callback when clicking inside the element\", () => {\n clickOutside(element, callback);\n element.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"does not fire when clicking the element itself\", () => {\n clickOutside(element, callback);\n const inner = document.createElement(\"span\");\n element.appendChild(inner);\n inner.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"update() replaces the callback\", () => {\n const action = clickOutside(element, callback);\n const newCallback = vi.fn();\n action.update(newCallback);\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n expect(newCallback).toHaveBeenCalledOnce();\n });\n\n it(\"destroy() stops all future callbacks\", () => {\n const action = clickOutside(element, callback);\n action.destroy();\n outsideElement.dispatchEvent(new MouseEvent(\"click\", { bubbles: true }));\n expect(callback).not.toHaveBeenCalled();\n });\n\n it(\"handles multiple destroy() calls without error\", () => {\n const action = clickOutside(element, callback);\n expect(() => { action.destroy(); action.destroy(); }).not.toThrow();\n });\n});"
67
+ },
68
+ {
69
+ "language": "typescript",
70
+ "code": "// src/lib/buildPrompt.spec.ts\nimport { describe, it, expect } from \"vitest\";\nimport { buildPrompt } from \"./buildPrompt\";\nimport type { BackendModel } from \"./server/models\";\n\nconst makeModel = (truncate?: number): BackendModel =>\n ({\n chatPromptRender: ({ messages, preprompt }: { messages: unknown[]; preprompt?: string }) =>\n messages.map((m: any) => `${m.role}:${m.content}`).join(\" \"),\n parameters: truncate !== undefined ? { truncate } : undefined,\n } as unknown as BackendModel);\n\ndescribe(\"buildPrompt\", () => {\n it(\"replaces system message content with preprompt when first message is system\", async () => {\n const model = makeModel(1000);\n const messages = [\n { from: \"system\" as const, content: \"old system prompt\" },\n { from: \"user\" as const, content: \"hello\" },\n ];\n const result = await buildPrompt({ messages, model, preprompt: \"new system prompt\" });\n expect(result).toContain(\"new system prompt\");\n expect(result).not.toContain(\"old system prompt\");\n });\n\n it(\"does not replace content when first message is not system\", async () => {\n const model = makeModel(1000);\n const messages = [{ from: \"user\" as const, content: \"hello\" }];\n await buildPrompt({ messages, model, preprompt: \"some preprompt\" });\n // original content unchanged\n expect(messages[0].content).toBe(\"hello\");\n });\n\n it(\"does not replace content when preprompt is undefined\", async () => {\n const model = makeModel(1000);\n const messages = [{ from: \"system\" as const, content: \"original\" }];\n await buildPrompt({ messages, model, preprompt: undefined });\n expect(messages[0].content).toBe(\"original\");\n });\n\n it(\"truncates to the last N words when truncate is set\", async () => {\n const model = makeModel(3);\n const messages = [{ from: \"user\" as const, content: \"one two three four five\" }];\n const result = await buildPrompt({ messages, model });\n const wordCount = result.trim().split(/\\s+/).length;\n expect(wordCount).toBeLessThanOrEqual(3);\n });\n\n it(\"returns full prompt when truncate is 0 (no slicing)\", async () => {\n const model = makeModel(0);\n const messages = [{ from: \"user\" as const, content: \"word1 word2 word3\" }];\n const result = await buildPrompt({ messages, model });\n // slice(0) returns empty array → join → empty string\n expect(result).toBe(\"\");\n });\n\n it(\"returns full prompt when model has no truncate parameter\", async () => {\n const model = makeModel(undefined);\n const messages = [{ from: \"user\" as const, content: \"word1 word2 word3\" }];\n const result = await buildPrompt({ messages, model });\n expect(result).toContain(\"word1\");\n });\n\n it(\"maps message from to role field\", async () => {\n const receivedMessages: unknown[] = [];\n const model = {\n chatPromptRender: ({ messages }: { messages: unknown[] }) => {\n receivedMessages.push(...messages);\n return \"\";\n },\n parameters: { truncate: 1000 },\n } as unknown as BackendModel;\n await buildPrompt({\n messages: [{ from: \"assistant\" as const, content: \"hi\" }],\n model,\n });\n expect((receivedMessages[0] as any).role).toBe(\"assistant\");\n });\n});"
71
+ },
72
+ {
73
+ "language": "typescript",
74
+ "code": "// src/lib/createShareLink.spec.ts\nimport { describe, it, expect, vi, beforeEach } from \"vitest\";\n\n// Mock SvelteKit modules before importing\nvi.mock(\"$app/paths\", () => ({ base: \"\" }));\nvi.mock(\"$app/state\", () => ({\n page: {\n data: { publicConfig: { PUBLIC_SHARE_PREFIX: \"\", PUBLIC_ORIGIN: \"https://app.example.com\" } },\n url: { origin: \"https://fallback.example.com\" },\n },\n}));\n\nimport { createShareLink } from \"./createShareLink\";\n\ndescribe(\"createShareLink\", () => {\n beforeEach(() => {\n vi.resetAllMocks();\n });\n\n it(\"returns share URL directly for 7-char id without fetching\", async () => {\n const fetchSpy = vi.spyOn(globalThis, \"fetch\");\n const url = await createShareLink(\"abc1234\");\n expect(fetchSpy).not.toHaveBeenCalled();\n expect(url).toBe(\"https://app.example.com/r/abc1234\");\n });\n\n it(\"calls the share endpoint for non-7-char ids\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: true,\n json: () => Promise.resolve({ shareId: \"xyz9876\" }),\n } as Response);\n const url = await createShareLink(\"longconversationid\");\n expect(url).toBe(\"https://app.example.com/r/xyz9876\");\n });\n\n it(\"throws when the share endpoint returns non-ok with a text body\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: false,\n text: () => Promise.resolve(\"Conversation not found\"),\n } as unknown as Response);\n await expect(createShareLink(\"longconversationid\")).rejects.toThrow(\"Conversation not found\");\n });\n\n it(\"throws generic message when error body is empty\", async () => {\n vi.spyOn(globalThis, \"fetch\").mockResolvedValueOnce({\n ok: false,\n text: () => Promise.resolve(\"\"),\n } as unknown as Response);\n await expect(createShareLink(\"longconversationid\")).rejects.toThrow(\n \"Failed to create share link\"\n );\n });\n\n it(\"uses PUBLIC_SHARE_PREFIX when set\", async () => {\n const { page } = await import(\"$app/state\");\n (page.data.publicConfig as any).PUBLIC_SHARE_PREFIX = \"https://cdn.share.example.com\";\n const url = await createShareLink(\"abc1234\");\n expect(url).toStartWith(\"https://cdn.share.example.com\");\n (page.data.publicConfig as any).PUBLIC_SHARE_PREFIX = \"\";\n });\n});"
75
+ },
76
+ {
77
+ "language": "typescript",
78
+ "code": "// Add to src/lib/migrations/migrations.spec.ts (inside the describe block)\n\nit(\"releaseLock does not delete lock held by different id\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n const fakeId = new ObjectId();\n await releaseLock(Semaphores.TEST_MIGRATION, fakeId); // wrong id\n expect(await isDBLocked(Semaphores.TEST_MIGRATION)).toBe(true);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId); // cleanup\n});\n\nit(\"refreshLock returns false when lock does not exist\", async () => {\n const fakeId = new ObjectId();\n const result = await refreshLock(Semaphores.TEST_MIGRATION, fakeId);\n expect(result).toBe(false);\n});\n\nit(\"isDBLocked is key-scoped — one key locked does not affect another\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n expect(await isDBLocked(\"other-key\")).toBe(false);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId);\n});\n\nit(\"releaseLock is idempotent — releasing twice does not throw\", async () => {\n const lockId = await acquireLock(Semaphores.TEST_MIGRATION);\n assert(lockId);\n await releaseLock(Semaphores.TEST_MIGRATION, lockId);\n await expect(releaseLock(Semaphores.TEST_MIGRATION, lockId)).resolves.not.toThrow();\n});"
79
+ },
80
+ {
81
+ "language": "typescript",
82
+ "code": "// src/lib/migrations/checkAndRunMigrations.spec.ts\nimport { beforeEach, describe, expect, it, vi } from \"vitest\";\nimport type { Database } from \"$lib/server/database\";\nimport { Semaphores } from \"$lib/types/Semaphore\";\nimport ObjectId from \"bson-objectid\";\n\n// Stubs for all side-effecting modules\nvi.mock(\"./lock\");\nvi.mock(\"$lib/server/logger\", () => ({ logger: { debug: vi.fn(), error: vi.fn() } }));\nvi.mock(\"$lib/server/config\", () => ({ config: { isHuggingChat: false } }));\n\nimport * as lockModule from \"./lock\";\nimport { checkAndRunMigrations } from \"./migrations\";\n\nconst makeDb = (results: unknown[] = []) => ({\n getCollections: () => ({\n migrationResults: {\n find: () => ({ toArray: () => Promise.resolve(results) }),\n updateOne: vi.fn().mockResolvedValue({}),\n },\n }),\n});\n\nvi.mock(\"$lib/server/database\", () => ({\n Database: { getInstance: vi.fn() },\n}));\n\nimport { Database } from \"$lib/server/database\";\n\ndescribe(\"checkAndRunMigrations\", () => {\n beforeEach(() => {\n vi.clearAllMocks();\n vi.mocked(Database.getInstance).mockResolvedValue(makeDb() as unknown as Database);\n vi.mocked(lockModule.acquireLock).mockResolvedValue(new ObjectId());\n vi.mocked(lockModule.releaseLock).mockResolvedValue(undefined);\n vi.mocked(lockModule.isDBLocked).mockResolvedValue(false);\n vi.mocked(lockModule.refreshLock).mockResolvedValue(true);\n });\n\n it(\"throws when duplicate migration GUIDs exist\", async () => {\n // Override routines to inject duplicates — easiest with vi.doMock inside test\n await expect(async () => {\n vi.doMock(\"./routines\", () => {\n const id = new ObjectId();\n return { migrations: [{ _id: id, name: \"dup\", up: vi.fn() }, { _id: id, name: \"dup2\", up: vi.fn() }] };\n });\n const { checkAndRunMigrations: fn } = await import(\"./migrations?dup=1\");\n await fn();\n }).rejects.toThrow(\"Duplicate migration GUIDs found\");\n });\n\n it(\"waits for DB unlock when lock is already held\", async () => {\n vi.mocked(lockModule.acquireLock).mockResolvedValueOnce(false);\n // isDBLocked returns true once, then false to exit the while loop\n vi.mocked(lockModule.isDBLocked)\n .mockResolvedValueOnce(true)\n .mockResolvedValueOnce(false);\n await checkAndRunMigrations();\n expect(lockModule.isDBLocked).toHaveBeenCalledWith(Semaphores.MIGRATION);\n });\n\n it(\"skips migration already in results\", async () => {\n const id = new ObjectId();\n vi.mocked(Database.getInstance).mockResolvedValue(\n makeDb([{ _id: id, name: \"done\", status: \"success\" }]) as unknown as Database\n );\n const upFn = vi.fn().mockResolvedValue(true);\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"done\", up: upFn, runEveryTime: false }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?skip=1\");\n await fn();\n expect(upFn).not.toHaveBeenCalled();\n });\n\n it(\"records failure status when migration throws\", async () => {\n const id = new ObjectId();\n const updateOne = vi.fn().mockResolvedValue({});\n vi.mocked(Database.getInstance).mockResolvedValue({\n getCollections: () => ({\n migrationResults: { find: () => ({ toArray: () => Promise.resolve([]) }), updateOne },\n }),\n } as unknown as Database);\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"failing\", up: vi.fn().mockRejectedValue(new Error(\"boom\")) }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?fail=1\");\n await fn();\n const lastCall = updateOne.mock.calls.at(-1)?.[1].$set;\n expect(lastCall?.status).toBe(\"failure\");\n });\n\n it(\"skips runForHuggingChat=only migration when not HuggingChat\", async () => {\n const { config } = await import(\"$lib/server/config\");\n (config as any).isHuggingChat = false;\n const upFn = vi.fn().mockResolvedValue(true);\n const id = new ObjectId();\n vi.doMock(\"./routines\", () => ({\n migrations: [{ _id: id, name: \"hc-only\", up: upFn, runForHuggingChat: \"only\" }],\n }));\n const { checkAndRunMigrations: fn } = await import(\"./migrations?hconly=1\");\n await fn();\n expect(upFn).not.toHaveBeenCalled();\n });\n\n it(\"releases lock and clears interval after all migrations run\", async () => {\n vi.useFakeTimers();\n await checkAndRunMigrations();\n vi.useRealTimers();\n expect(lockModule.releaseLock).toHaveBeenCalledWith(\n Semaphores.MIGRATION,\n expect.any(ObjectId)\n );\n });\n});"
83
+ },
84
+ {
85
+ "language": "typescript",
86
+ "code": "// src/lib/constants/rvagentPresets.spec.ts\nimport { describe, it, expect } from \"vitest\";\nimport {\n getPresetById,\n buildPresetUrl,\n buildPresetCliCommand,\n RVAGENT_PRESETS,\n} from \"./rvagentPresets\";\n\ndescribe(\"getPresetById\", () => {\n it(\"returns the matching preset\", () => {\n const preset = getPresetById(\"all-tools\");\n expect(preset).toBeDefined();\n expect(preset?.id).toBe(\"all-tools\");\n });\n\n it(\"returns undefined for an unknown id\", () => {\n expect(getPresetById(\"nonexistent\")).toBeUndefined();\n });\n\n it(\"all preset IDs in RVAGENT_PRESETS are findable\", () => {\n for (const p of RVAGENT_PRESETS) {\n expect(getPresetById(p.id)).toBe(p);\n }\n });\n});\n\ndescribe(\"buildPresetUrl\", () => {\n it(\"uses preset defaultPort when no port override is given\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset)).toBe(`http://localhost:${preset.defaultPort}/sse`);\n });\n\n it(\"uses the overridden port\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset, \"localhost\", 9999)).toBe(\"http://localhost:9999/sse\");\n });\n\n it(\"uses custom host\", () => {\n const preset = getPresetById(\"all-tools\")!;\n expect(buildPresetUrl(preset, \"192.168.1.10\")).toContain(\"192.168.1.10\");\n });\n});\n\ndescribe(\"buildPresetCliCommand\", () => {\n it(\"uses --all flag for the all-tools preset\", () => {\n const preset = getPresetById(\"all-tools\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(\"--all\");\n expect(cmd).not.toContain(\"--groups\");\n });\n\n it(\"uses --groups for presets with specific tool groups\", () => {\n const preset = getPresetById(\"file-shell\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(\"--groups file,shell\");\n expect(cmd).not.toContain(\"--all\");\n });\n\n it(\"uses custom port in the command\", () => {\n const preset = getPresetById(\"memory-agent\")!;\n const cmd = buildPresetCliCommand(preset, 8888);\n expect(cmd).toContain(\"--port 8888\");\n });\n\n it(\"falls back to defaultPort in the command when no port given\", () => {\n const preset = getPresetById(\"memory-agent\")!;\n const cmd = buildPresetCliCommand(preset);\n expect(cmd).toContain(`--port ${preset.defaultPort}`);\n });\n});"
87
+ },
88
+ {
89
+ "language": "typescript",
90
+ "code": "// Add to existing MarkdownRenderer.svelte.test.ts\n\ndescribe(\"MarkdownRenderer — security\", () => {\n it(\"strips javascript: href from links\", () => {\n const { baseElement } = render(MarkdownRenderer, {\n content: \"[click me](javascript:alert(1))\",\n });\n const anchor = baseElement.querySelector(\"a\");\n expect(anchor?.getAttribute(\"href\")).not.toContain(\"javascript:\");\n });\n\n it(\"renders empty content without crashing\", () => {\n expect(() => render(MarkdownRenderer, { content: \"\" })).not.toThrow();\n });\n});\n\ndescribe(\"MarkdownRenderer — tables\", () => {\n it(\"renders a markdown table\", () => {\n const md = \"| A | B |\\n|---|---|\\n| 1 | 2 |\";\n const { baseElement } = render(MarkdownRenderer, { content: md });\n expect(baseElement.querySelector(\"table\")).toBeTruthy();\n expect(baseElement.querySelector(\"thead\")).toBeTruthy();\n expect(baseElement.querySelector(\"tbody\")).toBeTruthy();\n });\n});\n\ndescribe(\"MarkdownRenderer — nested formatting\", () => {\n it(\"renders bold inside a list item\", () => {\n const { baseElement } = render(MarkdownRenderer, { content: \"- **bold item**\" });\n const strong = baseElement.querySelector(\"strong\");\n expect(strong).toBeTruthy();\n expect(strong?.textContent).toBe(\"bold item\");\n });\n\n it(\"renders multiple fenced code blocks with language labels\", () => {\n const md = \""
91
+ }
92
+ ]
93
+ },
94
+ "durationMs": 141406,
95
+ "model": "sonnet",
96
+ "sandboxMode": "permissive",
97
+ "workerType": "testgaps",
98
+ "timestamp": "2026-05-05T02:58:33.227Z",
99
+ "executionId": "testgaps_1777949771821_elw1j4"
100
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "timestamp": "2026-05-05T02:48:11.838Z",
3
+ "projectRoot": "/Users/cohen/Projects/ruflo/ruflo/src/ruvocal",
4
+ "structure": {
5
+ "hasPackageJson": true,
6
+ "hasTsConfig": true,
7
+ "hasClaudeConfig": true,
8
+ "hasClaudeFlow": true
9
+ },
10
+ "scannedAt": 1777949291838
11
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "timestamp": "2026-05-05T02:54:11.820Z",
3
+ "patternsConsolidated": 0,
4
+ "memoryCleaned": 0,
5
+ "duplicatesRemoved": 0
6
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "id": "session-1777949290618",
3
+ "startedAt": "2026-05-05T02:48:10.618Z",
4
+ "platform": "darwin",
5
+ "cwd": "/Users/cohen/Projects/ruflo/ruflo/src/ruvocal",
6
+ "context": {},
7
+ "metrics": {
8
+ "edits": 0,
9
+ "commands": 0,
10
+ "tasks": 0,
11
+ "errors": 0
12
+ }
13
+ }
File without changes
Binary file
@@ -0,0 +1 @@
1
+ [["entry_1777949290707_anh69v",{"id":"entry_1777949290707_anh69v","key":"-","namespace":"security_analysis","content":"name: RuVector Security Analysis\ndescription: Completed security audit of ruvector codebase\ntype: project\n---"}],["entry_1777949290871_wdj3j",{"id":"entry_1777949290871_wdj3j","key":"date-2026-04-02","namespace":"security_analysis","content":"Completed comprehensive security analysis of the ruvector npm package codebase across all critical components including CLI, MCP server, and core modules.\n\n**Key Findings:**\n- No hardcoded secrets or API keys detected\n- Good input validation in place for shell commands\n- Path validation is implemented with comprehensive checks\n- Most critical vulnerabilities mitigated through sanitization\n\n**Critical Issues:** 0\n**High Issues:** 2 \n**Medium Issues:** 3\n**Low Issues:** 2\n**Risk Score:** 28/100\n\n"}],["entry_1777949290886_afbl64",{"id":"entry_1777949290886_afbl64","key":"regtest","namespace":"sessions","content":"Session started: regtest"}],["entry_1777949290893_kfjdu",{"id":"entry_1777949290893_kfjdu","key":"test-123","namespace":"tasks","content":"Task completed: test-123"}],["entry_1777949290900_soy8c9",{"id":"entry_1777949290900_soy8c9","key":"test-fix-1","namespace":"tasks","content":"Task completed: test-fix-1"}],["entry_1777949290907_ardoho",{"id":"entry_1777949290907_ardoho","key":"verify-1686-fix","namespace":"tasks","content":"Task completed: Verify dbPath persistence fix increments metrics counts (agent: hooks-tester)"}],["entry_1777949290915_o6ppnq",{"id":"entry_1777949290915_o6ppnq","key":"verify-test-session","namespace":"sessions","content":"Session started: verify-test-session"}],["entry_1777949290922_kdxhgs",{"id":"entry_1777949290922_kdxhgs","key":"test-verify-001","namespace":"tasks","content":"Task completed: test-verify-001"}]]
Binary file
@@ -0,0 +1,305 @@
1
+
2
+ -- RuFlo V3 Memory Database
3
+ -- Version: 3.0.0
4
+ -- Features: Pattern learning, vector embeddings, temporal decay, migration tracking
5
+
6
+ PRAGMA journal_mode = WAL;
7
+ PRAGMA synchronous = NORMAL;
8
+ PRAGMA foreign_keys = ON;
9
+
10
+ -- ============================================
11
+ -- CORE MEMORY TABLES
12
+ -- ============================================
13
+
14
+ -- Memory entries (main storage)
15
+ CREATE TABLE IF NOT EXISTS memory_entries (
16
+ id TEXT PRIMARY KEY,
17
+ key TEXT NOT NULL,
18
+ namespace TEXT DEFAULT 'default',
19
+ content TEXT NOT NULL,
20
+ type TEXT DEFAULT 'semantic' CHECK(type IN ('semantic', 'episodic', 'procedural', 'working', 'pattern')),
21
+
22
+ -- Vector embedding for semantic search (stored as JSON array)
23
+ embedding TEXT,
24
+ embedding_model TEXT DEFAULT 'local',
25
+ embedding_dimensions INTEGER,
26
+
27
+ -- Metadata
28
+ tags TEXT, -- JSON array
29
+ metadata TEXT, -- JSON object
30
+ owner_id TEXT,
31
+
32
+ -- Timestamps
33
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
34
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
35
+ expires_at INTEGER,
36
+ last_accessed_at INTEGER,
37
+
38
+ -- Access tracking for hot/cold detection
39
+ access_count INTEGER DEFAULT 0,
40
+
41
+ -- Status
42
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deleted')),
43
+
44
+ UNIQUE(namespace, key)
45
+ );
46
+
47
+ -- Indexes for memory entries
48
+ CREATE INDEX IF NOT EXISTS idx_memory_namespace ON memory_entries(namespace);
49
+ CREATE INDEX IF NOT EXISTS idx_memory_key ON memory_entries(key);
50
+ CREATE INDEX IF NOT EXISTS idx_memory_type ON memory_entries(type);
51
+ CREATE INDEX IF NOT EXISTS idx_memory_status ON memory_entries(status);
52
+ CREATE INDEX IF NOT EXISTS idx_memory_created ON memory_entries(created_at);
53
+ CREATE INDEX IF NOT EXISTS idx_memory_accessed ON memory_entries(last_accessed_at);
54
+ CREATE INDEX IF NOT EXISTS idx_memory_owner ON memory_entries(owner_id);
55
+
56
+ -- ============================================
57
+ -- PATTERN LEARNING TABLES
58
+ -- ============================================
59
+
60
+ -- Learned patterns with confidence scoring and versioning
61
+ CREATE TABLE IF NOT EXISTS patterns (
62
+ id TEXT PRIMARY KEY,
63
+
64
+ -- Pattern identification
65
+ name TEXT NOT NULL,
66
+ pattern_type TEXT NOT NULL CHECK(pattern_type IN (
67
+ 'task-routing', 'error-recovery', 'optimization', 'learning',
68
+ 'coordination', 'prediction', 'code-pattern', 'workflow'
69
+ )),
70
+
71
+ -- Pattern definition
72
+ condition TEXT NOT NULL, -- Regex or semantic match
73
+ action TEXT NOT NULL, -- What to do when pattern matches
74
+ description TEXT,
75
+
76
+ -- Confidence scoring (0.0 - 1.0)
77
+ confidence REAL DEFAULT 0.5,
78
+ success_count INTEGER DEFAULT 0,
79
+ failure_count INTEGER DEFAULT 0,
80
+
81
+ -- Temporal decay
82
+ decay_rate REAL DEFAULT 0.01, -- How fast confidence decays
83
+ half_life_days INTEGER DEFAULT 30, -- Days until confidence halves without use
84
+
85
+ -- Vector embedding for semantic pattern matching
86
+ embedding TEXT,
87
+ embedding_dimensions INTEGER,
88
+
89
+ -- Versioning
90
+ version INTEGER DEFAULT 1,
91
+ parent_id TEXT REFERENCES patterns(id),
92
+
93
+ -- Metadata
94
+ tags TEXT, -- JSON array
95
+ metadata TEXT, -- JSON object
96
+ source TEXT, -- Where the pattern was learned from
97
+
98
+ -- Timestamps
99
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
100
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
101
+ last_matched_at INTEGER,
102
+ last_success_at INTEGER,
103
+ last_failure_at INTEGER,
104
+
105
+ -- Status
106
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'archived', 'deprecated', 'experimental'))
107
+ );
108
+
109
+ -- Indexes for patterns
110
+ CREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);
111
+ CREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);
112
+ CREATE INDEX IF NOT EXISTS idx_patterns_status ON patterns(status);
113
+ CREATE INDEX IF NOT EXISTS idx_patterns_last_matched ON patterns(last_matched_at);
114
+
115
+ -- Pattern evolution history (for versioning)
116
+ CREATE TABLE IF NOT EXISTS pattern_history (
117
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
118
+ pattern_id TEXT NOT NULL REFERENCES patterns(id),
119
+ version INTEGER NOT NULL,
120
+
121
+ -- Snapshot of pattern state
122
+ confidence REAL,
123
+ success_count INTEGER,
124
+ failure_count INTEGER,
125
+ condition TEXT,
126
+ action TEXT,
127
+
128
+ -- What changed
129
+ change_type TEXT CHECK(change_type IN ('created', 'updated', 'success', 'failure', 'decay', 'merged', 'split')),
130
+ change_reason TEXT,
131
+
132
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
133
+ );
134
+
135
+ CREATE INDEX IF NOT EXISTS idx_pattern_history_pattern ON pattern_history(pattern_id);
136
+
137
+ -- ============================================
138
+ -- LEARNING & TRAJECTORY TABLES
139
+ -- ============================================
140
+
141
+ -- Learning trajectories (SONA integration)
142
+ CREATE TABLE IF NOT EXISTS trajectories (
143
+ id TEXT PRIMARY KEY,
144
+ session_id TEXT,
145
+
146
+ -- Trajectory state
147
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'completed', 'failed', 'abandoned')),
148
+ verdict TEXT CHECK(verdict IN ('success', 'failure', 'partial', NULL)),
149
+
150
+ -- Context
151
+ task TEXT,
152
+ context TEXT, -- JSON object
153
+
154
+ -- Metrics
155
+ total_steps INTEGER DEFAULT 0,
156
+ total_reward REAL DEFAULT 0,
157
+
158
+ -- Timestamps
159
+ started_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
160
+ ended_at INTEGER,
161
+
162
+ -- Reference to extracted pattern (if any)
163
+ extracted_pattern_id TEXT REFERENCES patterns(id)
164
+ );
165
+
166
+ -- Trajectory steps
167
+ CREATE TABLE IF NOT EXISTS trajectory_steps (
168
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
169
+ trajectory_id TEXT NOT NULL REFERENCES trajectories(id),
170
+ step_number INTEGER NOT NULL,
171
+
172
+ -- Step data
173
+ action TEXT NOT NULL,
174
+ observation TEXT,
175
+ reward REAL DEFAULT 0,
176
+
177
+ -- Metadata
178
+ metadata TEXT, -- JSON object
179
+
180
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
181
+ );
182
+
183
+ CREATE INDEX IF NOT EXISTS idx_steps_trajectory ON trajectory_steps(trajectory_id);
184
+
185
+ -- ============================================
186
+ -- MIGRATION STATE TRACKING
187
+ -- ============================================
188
+
189
+ -- Migration state (for resume capability)
190
+ CREATE TABLE IF NOT EXISTS migration_state (
191
+ id TEXT PRIMARY KEY,
192
+ migration_type TEXT NOT NULL, -- 'v2-to-v3', 'pattern', 'memory', etc.
193
+
194
+ -- Progress tracking
195
+ status TEXT DEFAULT 'pending' CHECK(status IN ('pending', 'in_progress', 'completed', 'failed', 'rolled_back')),
196
+ total_items INTEGER DEFAULT 0,
197
+ processed_items INTEGER DEFAULT 0,
198
+ failed_items INTEGER DEFAULT 0,
199
+ skipped_items INTEGER DEFAULT 0,
200
+
201
+ -- Current position (for resume)
202
+ current_batch INTEGER DEFAULT 0,
203
+ last_processed_id TEXT,
204
+
205
+ -- Source/destination info
206
+ source_path TEXT,
207
+ source_type TEXT,
208
+ destination_path TEXT,
209
+
210
+ -- Backup info
211
+ backup_path TEXT,
212
+ backup_created_at INTEGER,
213
+
214
+ -- Error tracking
215
+ last_error TEXT,
216
+ errors TEXT, -- JSON array of errors
217
+
218
+ -- Timestamps
219
+ started_at INTEGER,
220
+ completed_at INTEGER,
221
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
222
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
223
+ );
224
+
225
+ -- ============================================
226
+ -- SESSION MANAGEMENT
227
+ -- ============================================
228
+
229
+ -- Sessions for context persistence
230
+ CREATE TABLE IF NOT EXISTS sessions (
231
+ id TEXT PRIMARY KEY,
232
+
233
+ -- Session state
234
+ state TEXT NOT NULL, -- JSON object with full session state
235
+ status TEXT DEFAULT 'active' CHECK(status IN ('active', 'paused', 'completed', 'expired')),
236
+
237
+ -- Context
238
+ project_path TEXT,
239
+ branch TEXT,
240
+
241
+ -- Metrics
242
+ tasks_completed INTEGER DEFAULT 0,
243
+ patterns_learned INTEGER DEFAULT 0,
244
+
245
+ -- Timestamps
246
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
247
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
248
+ expires_at INTEGER
249
+ );
250
+
251
+ -- ============================================
252
+ -- VECTOR INDEX METADATA (for HNSW)
253
+ -- ============================================
254
+
255
+ -- Track HNSW index state
256
+ CREATE TABLE IF NOT EXISTS vector_indexes (
257
+ id TEXT PRIMARY KEY,
258
+ name TEXT NOT NULL UNIQUE,
259
+
260
+ -- Index configuration
261
+ dimensions INTEGER NOT NULL,
262
+ metric TEXT DEFAULT 'cosine' CHECK(metric IN ('cosine', 'euclidean', 'dot')),
263
+
264
+ -- HNSW parameters
265
+ hnsw_m INTEGER DEFAULT 16,
266
+ hnsw_ef_construction INTEGER DEFAULT 200,
267
+ hnsw_ef_search INTEGER DEFAULT 100,
268
+
269
+ -- Quantization
270
+ quantization_type TEXT CHECK(quantization_type IN ('none', 'scalar', 'product')),
271
+ quantization_bits INTEGER DEFAULT 8,
272
+
273
+ -- Statistics
274
+ total_vectors INTEGER DEFAULT 0,
275
+ last_rebuild_at INTEGER,
276
+
277
+ created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000),
278
+ updated_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now') * 1000)
279
+ );
280
+
281
+ -- ============================================
282
+ -- SYSTEM METADATA
283
+ -- ============================================
284
+
285
+ CREATE TABLE IF NOT EXISTS metadata (
286
+ key TEXT PRIMARY KEY,
287
+ value TEXT NOT NULL,
288
+ updated_at INTEGER DEFAULT (strftime('%s', 'now') * 1000)
289
+ );
290
+
291
+
292
+ INSERT OR REPLACE INTO metadata (key, value) VALUES
293
+ ('schema_version', '3.0.0'),
294
+ ('backend', 'hybrid'),
295
+ ('created_at', '2026-05-05T02:48:10.665Z'),
296
+ ('sql_js', 'true'),
297
+ ('vector_embeddings', 'enabled'),
298
+ ('pattern_learning', 'enabled'),
299
+ ('temporal_decay', 'enabled'),
300
+ ('hnsw_indexing', 'enabled');
301
+
302
+ -- Create default vector index configuration
303
+ INSERT OR IGNORE INTO vector_indexes (id, name, dimensions) VALUES
304
+ ('default', 'default', 768),
305
+ ('patterns', 'patterns', 768);