docs(production): standardiserad produktionsberedskap och gap-analys

Lägger docs/production-readiness.md och siax/production-gaps.json enligt SIAX Master Definition of Done. Underlag: git-, kod- och live-evidens 2026-11-11. Nivån är evidensbaserad, inte ett leveransgodkännande.
This commit is contained in:
2026-11-10 09:15:21 +01:00
parent 0fda8cae48
commit 2bee8eb778
6135 changed files with 4 additions and 1718997 deletions
-62
View File
@@ -1,62 +0,0 @@
import { describe, it, expect, vi } from 'vitest'
import { checkAgentRateLimit, agentRateLimitResponseBody } from '../agent'
import type { SupabaseClient } from '@supabase/supabase-js'
function makeSupabase(rpcResult: { data?: unknown; error?: unknown }) {
return {
rpc: vi.fn().mockResolvedValue(rpcResult),
} as unknown as SupabaseClient
}
describe('checkAgentRateLimit', () => {
it('returns ok=true when the RPC says ok', async () => {
const result = await checkAgentRateLimit(makeSupabase({ data: { ok: true }, error: null }), 'user-1')
expect(result.ok).toBe(true)
expect(result.scope).toBeUndefined()
})
it('returns ok=false with minute scope + retry when the minute window is hit', async () => {
const result = await checkAgentRateLimit(
makeSupabase({ data: { ok: false, scope: 'minute', retry_after_sec: 60 }, error: null }),
'user-1',
)
expect(result.ok).toBe(false)
expect(result.scope).toBe('minute')
expect(result.retryAfterSec).toBe(60)
})
it('returns ok=false with day scope when the day window is hit', async () => {
const result = await checkAgentRateLimit(
makeSupabase({ data: { ok: false, scope: 'day', retry_after_sec: 3600 }, error: null }),
'user-1',
)
expect(result.scope).toBe('day')
expect(result.retryAfterSec).toBe(3600)
})
it('fails open (ok=true) when the RPC errors: never 429 a real user on infra blip', async () => {
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
const result = await checkAgentRateLimit(makeSupabase({ data: null, error: { message: 'boom' } }), 'user-1')
expect(result.ok).toBe(true)
spy.mockRestore()
})
it('passes generous caps keyed per-user', async () => {
const supabase = makeSupabase({ data: { ok: true }, error: null })
await checkAgentRateLimit(supabase, 'user-xyz')
expect(supabase.rpc).toHaveBeenCalledWith('check_and_increment_agent_quota', {
p_user_id: 'user-xyz',
p_minute_max: 30,
p_day_max: 1000,
})
})
})
describe('agentRateLimitResponseBody', () => {
it('day scope → daily-limit message', () => {
expect(agentRateLimitResponseBody({ ok: false, scope: 'day' }).error).toMatch(/dagens gräns/i)
})
it('minute scope → try-again-soon message', () => {
expect(agentRateLimitResponseBody({ ok: false, scope: 'minute' }).error).toMatch(/förfrågningar/i)
})
})
-59
View File
@@ -1,59 +0,0 @@
import { describe, it, expect, vi } from 'vitest'
import { checkInboxUploadRateLimit } from '../inbox'
import type { SupabaseClient } from '@supabase/supabase-js'
function makeSupabase(rpcResult: { data?: unknown; error?: unknown }) {
return {
rpc: vi.fn().mockResolvedValue(rpcResult),
} as unknown as SupabaseClient
}
describe('checkInboxUploadRateLimit', () => {
it('returns ok=true when the RPC says ok', async () => {
const supabase = makeSupabase({ data: { ok: true }, error: null })
const result = await checkInboxUploadRateLimit(supabase, 'company-1')
expect(result.ok).toBe(true)
expect(result.scope).toBeUndefined()
})
it('returns ok=false with minute scope and retry_after when minute window hit', async () => {
const supabase = makeSupabase({
data: { ok: false, scope: 'minute', retry_after_sec: 42 },
error: null,
})
const result = await checkInboxUploadRateLimit(supabase, 'company-1')
expect(result.ok).toBe(false)
expect(result.scope).toBe('minute')
expect(result.retryAfterSec).toBe(42)
})
it('returns ok=false with day scope when day window hit', async () => {
const supabase = makeSupabase({
data: { ok: false, scope: 'day', retry_after_sec: 3600 },
error: null,
})
const result = await checkInboxUploadRateLimit(supabase, 'company-1')
expect(result.ok).toBe(false)
expect(result.scope).toBe('day')
expect(result.retryAfterSec).toBe(3600)
})
it('fails open (ok=true) when the RPC errors: better to accept than 500 a real user', async () => {
const supabase = makeSupabase({ data: null, error: { message: 'boom' } })
// Silence the console.error the helper emits on infra error.
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
const result = await checkInboxUploadRateLimit(supabase, 'company-1')
expect(result.ok).toBe(true)
spy.mockRestore()
})
it('passes the configured minute and day caps to the RPC', async () => {
const supabase = makeSupabase({ data: { ok: true }, error: null })
await checkInboxUploadRateLimit(supabase, 'company-xyz')
expect(supabase.rpc).toHaveBeenCalledWith('check_and_increment_inbox_quota', {
p_company_id: 'company-xyz',
p_minute_max: 30,
p_day_max: 500,
})
})
})
-56
View File
@@ -1,56 +0,0 @@
import type { SupabaseClient } from '@supabase/supabase-js'
/**
* Per-user rate limiter for the in-app AI agent's LLM endpoints
* (/api/agent/invoke, /onboarding/stream, /composer). Backed by the
* `check_and_increment_agent_quota` Postgres RPC (atomic check + increment):
* same shared store the rest of the app uses, no Upstash/env dependency.
*
* Limits are deliberately GENEROUS: a normal heavy user (dozens of turns a day)
* never hits them. The cap exists only to bound runaway Bedrock spend from a
* loop-firing session or reload-spammed onboarding. Keyed per-user.
*/
export interface AgentLimitResult {
ok: boolean
retryAfterSec?: number
scope?: 'minute' | 'day'
}
const MINUTE_MAX = 30
const DAY_MAX = 1000
export async function checkAgentRateLimit(
supabase: SupabaseClient,
userId: string,
): Promise<AgentLimitResult> {
const { data, error } = await supabase.rpc('check_and_increment_agent_quota', {
p_user_id: userId,
p_minute_max: MINUTE_MAX,
p_day_max: DAY_MAX,
})
if (error) {
// Fail open on infra error: the limiter is defense-in-depth; better to
// serve a real user than to 429 them because the RPC blipped.
console.error('[agent-rate-limit] RPC failed:', error)
return { ok: true }
}
const result = (data ?? { ok: true }) as {
ok: boolean
scope?: 'minute' | 'day'
retry_after_sec?: number
}
return { ok: result.ok, scope: result.scope, retryAfterSec: result.retry_after_sec }
}
/**
* Standard 429 JSON body for a rate-limited agent request. The chat client
* surfaces `error` verbatim, so keep it a friendly Swedish sentence.
*/
export function agentRateLimitResponseBody(result: AgentLimitResult): { error: string } {
return {
error:
result.scope === 'day'
? 'Du har nått dagens gräns för förfrågningar till assistenten. Försök igen senare.'
: 'För många förfrågningar till assistenten just nu. Vänta en stund och försök igen.',
}
}
-52
View File
@@ -1,52 +0,0 @@
import type { SupabaseClient } from '@supabase/supabase-js'
/**
* Per-company rate limiter for document-inbox ingestion. Backed by the
* `check_and_increment_inbox_quota` Postgres RPC (atomic check + increment),
* not Upstash: keeps the limiter on the same shared distributed store the
* rest of the app already hits, and works without extra env vars on Vercel
* and Docker self-hosters alike.
*
* Both windows are per-company:
* - MINUTE_MAX: a real user could only ever hit this with a script or by
* holding the upload button. The defense is against burst floods.
* - DAY_MAX: the backstop against slow drip abuse. A legitimate end-of-
* month batch is well under this number.
*/
export interface InboxLimitResult {
ok: boolean
retryAfterSec?: number
scope?: 'minute' | 'day'
}
const MINUTE_MAX = 30
const DAY_MAX = 500
export async function checkInboxUploadRateLimit(
supabase: SupabaseClient,
companyId: string,
): Promise<InboxLimitResult> {
const { data, error } = await supabase.rpc('check_and_increment_inbox_quota', {
p_company_id: companyId,
p_minute_max: MINUTE_MAX,
p_day_max: DAY_MAX,
})
if (error) {
// Fail open on infra error. The limiter is defense-in-depth: per-file
// size + MIME checks still apply on the upload route. Better to accept
// an upload than 500 a real user because Postgres blipped.
console.error('[inbox-rate-limit] RPC failed:', error)
return { ok: true }
}
// RPC returns jsonb_build_object payload. The JS client decodes as object.
const result = (data ?? { ok: true }) as {
ok: boolean
scope?: 'minute' | 'day'
retry_after_sec?: number
}
return {
ok: result.ok,
scope: result.scope,
retryAfterSec: result.retry_after_sec,
}
}