* feat(agent): telemetry completeness + durability, CI gates, commit_method provenance Quick wins from the "Building AI systems that ship" audit: - mcp.tool_called gains errorMessage (message_sv, truncated 500 chars) on all failure exits; new mcp.skill_loaded event on every gnubok_load_skill (all tiers) so atom usage is finally measurable - event_log: (event_type, created_at) index; cleanup cron keeps mcp.*/agent.* telemetry 180 days (delivery events stay 30) - CI: lint ratchet (npm run check:lint — 60 legacy errors baselined, fails only on NEW errors) and a pg-real coverage gate (migrations touching trigger/RPC/RLS/DEFERRABLE require a *.pg.test.ts change; escape hatch: -- pg-test: covered-by/skip) - journal_entries.commit_method CHECK widened with 'api_key'/'agent'; the MCP approve path records 'api_key' truthfully instead of 'user_accept' (agent_first_vision §8 P0-1). 'agent' is reserved — ALL MCP traffic (incl. claude.ai OAuth, whose access_token is a minted API key) authenticates as api_key today Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(import): derive opening balances from prior-year #UB when SIE lacks #IB (#675) SIE files exported without #IB 0 rows (only #UB -1) previously imported with zero opening balances. getEffectiveOpeningBalances() now derives IB from prior-year UB for balance-sheet accounts when explicit #IB is absent, surfaces the derivation as an info issue in the import preview, and excludes share-capital vouchers from opening-balance detection. Detection regexes are shared between parser and importer so the two checks cannot drift. 507 lib/import tests pass. (Authored in a parallel session in this checkout; included per request.) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(review): address PR #677 bot findings — RoPA entry, execFileSync, gate scope note Triage of the compliance-swarm + Greptile findings: Applied: - .compliance/ropa.yaml: new mcp.telemetry processing activity declaring the 180-day mcp.*/agent.* retention, lawful basis, data categories, and the no-args/no-results minimisation (ISO A.8.10, GDPR Art.5(1)(c) — the retention split is now formally documented, referenced from the cron) - check-pg-test-coverage.mjs: execFileSync with argv array — no shell, so a hostile base-ref can't inject (ASVS V13.2.1); verified an injection attempt exits 2 without executing - check-pg-test-coverage.mjs: documented the PR-level (not per-migration) scope of the gate so reviewers know to check coverage per migration when a PR carries several risky migrations (Greptile P2) Acknowledged, no change: - errorMessage PII risk: messages are domain-mapped strings; event_log already persists far richer delivery payloads under the same RLS; now declared in ropa.yaml - cron error envelope: errorResponse maps to the canonical safe envelope and the endpoint is CRON_SECRET-gated - two-pass delete "partial state": TTL deletes are idempotent — the next daily run sweeps whatever a failed pass left behind - skill_loaded actorLabel/sessionId: mirrors the pre-existing mcp.tool_called payload; sessionId is the join key the analytics exist for Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
266 lines
9.7 KiB
TypeScript
266 lines
9.7 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
|
import { createQueuedMockSupabase } from '@/tests/helpers'
|
|
import { TOOL_SCOPE_MAP } from '@/lib/auth/api-keys'
|
|
|
|
const commitSpy = vi.fn()
|
|
|
|
vi.mock('@/lib/pending-operations/commit', () => ({
|
|
commitPendingOperation: (...args: unknown[]) => commitSpy(...args),
|
|
}))
|
|
|
|
// Import after mock so the tool registry binds to the mocked module
|
|
import { tools } from '../server'
|
|
|
|
const listTool = tools.find((t) => t.name === 'gnubok_list_pending_operations')!
|
|
const approveTool = tools.find((t) => t.name === 'gnubok_approve_pending_operation')!
|
|
const rejectTool = tools.find((t) => t.name === 'gnubok_reject_pending_operation')!
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
describe('pending_operations MCP tools — registration', () => {
|
|
it('all three tools are registered', () => {
|
|
expect(listTool).toBeDefined()
|
|
expect(approveTool).toBeDefined()
|
|
expect(rejectTool).toBeDefined()
|
|
})
|
|
|
|
it('list is gated by pending_operations:read', () => {
|
|
expect(TOOL_SCOPE_MAP['gnubok_list_pending_operations']).toBe('pending_operations:read')
|
|
})
|
|
|
|
it('approve and reject are gated by pending_operations:approve', () => {
|
|
expect(TOOL_SCOPE_MAP['gnubok_approve_pending_operation']).toBe('pending_operations:approve')
|
|
expect(TOOL_SCOPE_MAP['gnubok_reject_pending_operation']).toBe('pending_operations:approve')
|
|
})
|
|
|
|
it('input schemas have additionalProperties: false', () => {
|
|
for (const t of [listTool, approveTool, rejectTool]) {
|
|
expect((t.inputSchema as Record<string, unknown>).additionalProperties).toBe(false)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('gnubok_list_pending_operations', () => {
|
|
it('returns operations with pagination envelope', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
const ops = [
|
|
{ id: 'op-1', operation_type: 'create_invoice', status: 'pending', risk_level: 'medium', created_at: '2026-05-01T00:00:00Z' },
|
|
{ id: 'op-2', operation_type: 'create_voucher', status: 'pending', risk_level: 'high', created_at: '2026-05-02T00:00:00Z' },
|
|
]
|
|
enqueue({ data: ops, error: null, count: 2 })
|
|
|
|
const result = (await listTool.execute(
|
|
{},
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never,
|
|
{ type: 'api_key' }
|
|
)) as {
|
|
operations: Array<{ id: string }>
|
|
count: number
|
|
total_count: number
|
|
has_more: boolean
|
|
}
|
|
|
|
expect(result.operations).toHaveLength(2)
|
|
expect(result.count).toBe(2)
|
|
expect(result.total_count).toBe(2)
|
|
expect(result.has_more).toBe(false)
|
|
})
|
|
|
|
it('signals has_more + next_offset when more rows exist beyond the page', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: [{ id: 'op-1' }], error: null, count: 50 })
|
|
|
|
const result = (await listTool.execute(
|
|
{ limit: 1, offset: 0 },
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never,
|
|
{ type: 'api_key' }
|
|
)) as { has_more: boolean; next_offset?: number }
|
|
|
|
expect(result.has_more).toBe(true)
|
|
expect(result.next_offset).toBe(1)
|
|
})
|
|
})
|
|
|
|
describe('gnubok_approve_pending_operation', () => {
|
|
it('fetches the op then delegates to commitPendingOperation', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
const op = { id: 'op-1', operation_type: 'create_invoice', company_id: 'company-1', status: 'pending', risk_level: 'medium', params: {} }
|
|
enqueue({ data: op, error: null }) // fetch
|
|
commitSpy.mockResolvedValue({ status: 'committed', data: { invoice_id: 'inv-1' } })
|
|
|
|
const result = (await approveTool.execute(
|
|
{ operation_id: 'op-1' },
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never,
|
|
{ type: 'api_key' }
|
|
)) as { status: string; operation_id: string; data?: { invoice_id: string } }
|
|
|
|
expect(commitSpy).toHaveBeenCalledTimes(1)
|
|
expect(commitSpy.mock.calls[0][3]).toMatchObject({ id: 'op-1' })
|
|
// commit options always include commitMethod; userEmail is added when
|
|
// the supabase mock supports auth.admin.getUserById (it doesn't here, so
|
|
// the resolution silently fails and we fall back to just commitMethod).
|
|
// An api_key actor records 'api_key' in the immutable layer — MCP-relayed
|
|
// acknowledgment, not a first-party human session (vision §8 P0-1).
|
|
expect(commitSpy.mock.calls[0][4]).toMatchObject({ commitMethod: 'api_key' })
|
|
expect(result.status).toBe('committed')
|
|
expect(result.operation_id).toBe('op-1')
|
|
expect(result.data?.invoice_id).toBe('inv-1')
|
|
})
|
|
|
|
// No 'mcp_oauth' row: handleMcpRequest hardcodes actor.type='api_key' for
|
|
// ALL MCP traffic (the OAuth connector's access_token is a minted API key),
|
|
// so 'api_key' is the only agent-credential value a live request produces.
|
|
it.each([
|
|
{ actorType: 'api_key', expected: 'api_key' },
|
|
{ actorType: 'user', expected: 'user_accept' },
|
|
] as const)(
|
|
'records commit_method=$expected when the approving actor is $actorType',
|
|
async ({ actorType, expected }) => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
const op = { id: 'op-1', operation_type: 'create_invoice', company_id: 'company-1', status: 'pending', risk_level: 'medium', params: {} }
|
|
enqueue({ data: op, error: null }) // fetch
|
|
commitSpy.mockResolvedValue({ status: 'committed' })
|
|
|
|
await approveTool.execute(
|
|
{ operation_id: 'op-1' },
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never,
|
|
{ type: actorType }
|
|
)
|
|
|
|
expect(commitSpy.mock.calls[0][4]).toMatchObject({ commitMethod: expected })
|
|
}
|
|
)
|
|
|
|
it('refuses to approve a risk_level=high op without confirmed=true', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
const op = {
|
|
id: 'op-1',
|
|
operation_type: 'create_voucher',
|
|
company_id: 'company-1',
|
|
status: 'pending',
|
|
risk_level: 'high',
|
|
params: {},
|
|
}
|
|
enqueue({ data: op, error: null }) // fetch
|
|
|
|
await expect(
|
|
approveTool.execute(
|
|
{ operation_id: 'op-1' },
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never,
|
|
{ type: 'api_key' }
|
|
)
|
|
).rejects.toThrow(/confirmed=true/i)
|
|
expect(commitSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('approves a risk_level=high op when confirmed=true is supplied', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
const op = {
|
|
id: 'op-1',
|
|
operation_type: 'create_voucher',
|
|
company_id: 'company-1',
|
|
status: 'pending',
|
|
risk_level: 'high',
|
|
params: {},
|
|
}
|
|
enqueue({ data: op, error: null }) // fetch
|
|
commitSpy.mockResolvedValue({ status: 'committed' })
|
|
|
|
const result = (await approveTool.execute(
|
|
{ operation_id: 'op-1', confirmed: true },
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never,
|
|
{ type: 'api_key' }
|
|
)) as { status: string; operation_id: string }
|
|
|
|
expect(commitSpy).toHaveBeenCalledTimes(1)
|
|
expect(result.status).toBe('committed')
|
|
})
|
|
|
|
it('throws when the operation is not found', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: null, error: { message: 'not found' } })
|
|
|
|
await expect(
|
|
approveTool.execute({ operation_id: 'missing' }, 'company-1', 'user-1', supabase as never)
|
|
).rejects.toThrow(/not found/i)
|
|
expect(commitSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('surfaces failed status from the executor', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1', operation_type: 'create_voucher', company_id: 'company-1', status: 'pending', params: {} }, error: null })
|
|
commitSpy.mockResolvedValue({ status: 'failed', error: 'Period locked', http_status: 423 })
|
|
|
|
const result = (await approveTool.execute(
|
|
{ operation_id: 'op-1' },
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never
|
|
)) as { status: string; error?: string }
|
|
|
|
expect(result.status).toBe('failed')
|
|
expect(result.error).toBe('Period locked')
|
|
})
|
|
})
|
|
|
|
describe('gnubok_reject_pending_operation', () => {
|
|
it('flips status to rejected and never invokes the executor', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1', status: 'pending' }, error: null }) // fetch
|
|
enqueue({ data: [{ id: 'op-1' }], error: null }) // update CAS — returns rows
|
|
|
|
const result = (await rejectTool.execute(
|
|
{ operation_id: 'op-1' },
|
|
'company-1',
|
|
'user-1',
|
|
supabase as never
|
|
)) as { status: string; operation_id: string }
|
|
|
|
expect(result.status).toBe('rejected')
|
|
expect(result.operation_id).toBe('op-1')
|
|
expect(commitSpy).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('throws when the CAS update affects 0 rows (concurrent claim)', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1', status: 'pending' }, error: null }) // fetch
|
|
enqueue({ data: [], error: null }) // update CAS — 0 rows (lost race)
|
|
|
|
await expect(
|
|
rejectTool.execute({ operation_id: 'op-1' }, 'company-1', 'user-1', supabase as never)
|
|
).rejects.toThrow(/no longer pending/i)
|
|
})
|
|
|
|
it('throws 409-style error if the op is already resolved', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: { id: 'op-1', status: 'committed' }, error: null })
|
|
|
|
await expect(
|
|
rejectTool.execute({ operation_id: 'op-1' }, 'company-1', 'user-1', supabase as never)
|
|
).rejects.toThrow(/already committed/i)
|
|
})
|
|
|
|
it('throws when the op is missing', async () => {
|
|
const { supabase, enqueue } = createQueuedMockSupabase()
|
|
enqueue({ data: null, error: { message: 'not found' } })
|
|
|
|
await expect(
|
|
rejectTool.execute({ operation_id: 'missing' }, 'company-1', 'user-1', supabase as never)
|
|
).rejects.toThrow(/not found/i)
|
|
})
|
|
})
|