fix(pending-ops): MCP approval of bulk-book works and failed approvals no longer consume the op (#1852)

* fix(pending-ops): MCP approval of bulk-book works and failed approvals no longer consume the op

Feedback seq 261545 (deepCFO): approving a bulk_book_transactions op over
MCP returned BULK_BOOK_UNAUTHORIZED, yet the op vanished from /pending
with nothing booked; the user believed it had been approved.

Two defects:

1. The bulk_book_transactions RPC gates on auth.uid(), which is NULL on
   the cookieless service client every MCP approval runs on, so EVERY
   API-key approval of a samlingsverifikat was refused. New migration
   20260824170000 adds p_user_id, honored only for service_role callers
   (same gate as match_batch_allocate 20260817150000 and undo_sie_import);
   the executor passes the approving user, who is now also the actor
   stamped on the verifikat. pg-real test covers member/spoof/no-JWT/
   grants like the precedent.

2. The dispatcher consumed the op on ANY executor error other than 404/
   409. An authorization refusal happens before any side-effect and says
   nothing about the op, so 401/403 now release the claim back to
   'pending'. The executor maps RPC codes through the structured-error
   registry so 403/404/409 are distinguishable from 400. Every
   CommitResult carries operation_status (pending | committed | rejected
   | failed_partial), exposed on gnubok_approve_pending_operation, so
   agents stop inferring consumption from status 'failed'.

Catalog token ceiling 59.95K -> 60K per the documented ratchet protocol.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScVhg6XsDtNXkiEQNV7LaZ

* fix(pending-ops): revoke anon explicitly on the service-actor bulk_book signature

Default privileges grant EXECUTE on new functions to anon; the pg-real
grants test (mirroring match_batch_allocate) caught it.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01ScVhg6XsDtNXkiEQNV7LaZ

---------

Co-authored-by: Jakob Wennberg <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
Jakob Wennberg
2026-08-25 10:32:12 +02:00
committed by GitHub
co-authored by Claude Fable 5 Jakob Wennberg
parent e35714518f
commit dc92fb5c0c
7 changed files with 801 additions and 4 deletions
+1
View File
File diff suppressed because one or more lines are too long
@@ -198,9 +198,14 @@ describe('tools/list payload size guard', () => {
// so strict clients stop failing successful unmatched uploads (seq
// 261972). Prose trimmed to the floor first; headroom before the
// change was ~19 tokens, so even the trimmed contract crossed.
// * 59.95K to 60K with operation_status on gnubok_approve_pending_operation
// (feedback seq 261545): a failed approve used to consume the op
// silently, and agents inferred "consumed" from status 'failed' both
// ways. The enum is the contract; the description is one clause;
// headroom before the change was ~15 tokens, so even that crossed.
// Long-term answer to growth is leaning harder on gnubok_search_tools: if this
// fires again, prefer trimming descriptions or making a tool opt-in via search
// before bumping further.
expect(approxTokens).toBeLessThan(59_950)
expect(approxTokens).toBeLessThan(60_000)
})
})
+2
View File
@@ -16843,6 +16843,7 @@ export const tools: McpTool[] = [
error: { type: 'string' },
error_code: { type: 'string' },
auto_rejected: { type: 'boolean' },
operation_status: { type: 'string', enum: ['pending', 'committed', 'rejected', 'failed_partial'], description: 'pending = not consumed, re-approvable' },
},
required: ['status', 'operation_id'],
},
@@ -16956,6 +16957,7 @@ export const tools: McpTool[] = [
...(result.error ? { error: result.error } : {}),
...(result.code ? { error_code: result.code } : {}),
...(result.auto_rejected ? { auto_rejected: true } : {}),
...(result.operation_status ? { operation_status: result.operation_status } : {}),
}
},
},
@@ -0,0 +1,121 @@
/**
* Authorization refusals must not consume a pending operation.
*
* Feedback seq 261545: an API-key approve of a bulk_book_transactions op hit
* BULK_BOOK_UNAUTHORIZED (the RPC saw auth.uid() = NULL on the service
* client), and the dispatcher landed the op as 'rejected'. It vanished from
* the /pending queue with nothing booked, and the user believed it had been
* approved. A 401/403 happens before any side-effect and says nothing about
* the op's content, so the claim is released back to 'pending' and the
* result says so explicitly (operation_status) instead of leaving agents to
* infer consumption from status 'failed'.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { eventBus } from '@/lib/events/bus'
import { createQueuedMockSupabase } from '@/tests/helpers'
import type { PendingOperation } from '@/types'
import { commitPendingOperation } from '../commit'
function makeBulkBookOp(): PendingOperation {
return {
id: 'op-bulk-1',
user_id: 'user-1',
company_id: 'company-1',
operation_type: 'bulk_book_transactions',
status: 'pending',
title: 'Samlingsverifikation: 3 transaktioner 2026-07-22',
params: {
tx_ids: ['tx-1', 'tx-2', 'tx-3'],
existing_journal_entry_id: null,
new_entry: { description: 'Dagskassa', lines: [] },
},
preview_data: {},
result_data: null,
actor_type: 'api_key',
actor_id: 'key-1',
actor_label: 'deepCFO',
risk_level: 'medium',
created_at: '2026-08-24T00:00:00Z',
resolved_at: null,
updated_at: '2026-08-24T00:00:00Z',
} as PendingOperation
}
describe('commitPendingOperation: authorization refusal is recoverable', () => {
beforeEach(() => {
vi.clearAllMocks()
eventBus.clear()
})
it('releases the claim back to pending on BULK_BOOK_UNAUTHORIZED and reports operation_status', async () => {
const { supabase, enqueueMany, findCalls } = createQueuedMockSupabase()
enqueueMany([
{ data: { id: 'op-bulk-1' }, error: null }, // atomic claim pending -> committing
{ data: { ok: false, code: 'BULK_BOOK_UNAUTHORIZED' }, error: null }, // RPC refusal
{ data: null, error: null }, // release claim back to pending
])
const result = await commitPendingOperation(
supabase as never,
'user-1',
'company-1',
makeBulkBookOp(),
)
expect(result.status).toBe('failed')
expect(result.http_status).toBe(403)
expect(result.code).toBe('BULK_BOOK_UNAUTHORIZED')
expect(result.operation_status).toBe('pending')
const updates = findCalls('pending_operations', 'update')
expect(updates).toContainEqual([{ status: 'committing' }])
expect(updates).toContainEqual([{ status: 'pending' }])
expect(updates.some((args) => (args[0] as { status?: string }).status === 'rejected')).toBe(false)
})
it('passes the approving user as p_user_id so the service client is attributed', async () => {
const { supabase, enqueueMany } = createQueuedMockSupabase()
enqueueMany([
{ data: { id: 'op-bulk-1' }, error: null },
{ data: { ok: true, journal_entry_id: 'je-1', mode: 'create_new', linked_tx_count: 3 }, error: null },
{ data: null, error: null }, // finalize committed
])
const result = await commitPendingOperation(
supabase as never,
'user-1',
'company-1',
makeBulkBookOp(),
)
expect(result.status).toBe('committed')
expect(result.operation_status).toBe('committed')
expect(supabase.rpc).toHaveBeenCalledTimes(1)
expect(supabase.rpc).toHaveBeenCalledWith(
'bulk_book_transactions',
expect.objectContaining({ p_user_id: 'user-1', p_company_id: 'company-1' }),
)
})
it('still consumes the op as rejected on a genuine input error (400)', async () => {
const { supabase, enqueueMany, findCalls } = createQueuedMockSupabase()
enqueueMany([
{ data: { id: 'op-bulk-1' }, error: null },
{ data: { ok: false, code: 'BULK_BOOK_INVALID_PAYLOAD' }, error: null },
{ data: null, error: null }, // rejected update
])
const result = await commitPendingOperation(
supabase as never,
'user-1',
'company-1',
makeBulkBookOp(),
)
expect(result.status).toBe('failed')
expect(result.operation_status).toBe('rejected')
const updates = findCalls('pending_operations', 'update')
expect(updates.some((args) => (args[0] as { status?: string }).status === 'rejected')).toBe(true)
})
})
+49 -3
View File
@@ -208,6 +208,12 @@ export interface CommitResult {
// callers that do not recognize the code fall back to `error`.
code?: string
account_numbers?: string[]
// Where the pending_operations row landed, independent of `status`:
// 'pending' means the op was NOT consumed and can be approved again
// (recoverable refusal: capability, chart accounts, Skatteverket, or an
// authorization failure that happened before any side-effect). Agents
// used to infer "consumed" from status 'failed' and were wrong both ways.
operation_status?: 'pending' | 'committed' | 'rejected' | 'failed_partial'
}
export interface CommitOptions {
@@ -5874,6 +5880,7 @@ async function commitMatchBatchAllocate(
async function commitBulkBookTransactions(
supabase: SupabaseClient,
userId: string,
companyId: string,
params: Record<string, unknown>
): Promise<ExecutorResult> {
@@ -5910,6 +5917,10 @@ async function commitBulkBookTransactions(
p_existing_journal_entry_id: existingJeId,
p_new_entry: newEntry,
p_company_id: companyId,
// This path runs on the cookieless service client where auth.uid() is
// NULL; the RPC honors p_user_id only for service_role callers
// (migration 20260824170000), so the approving human is the actor.
p_user_id: userId,
})
if (error) {
// Sanitised log (A.8.11, CC7.2): only error code + message.
@@ -5921,9 +5932,15 @@ async function commitBulkBookTransactions(
}
const result = data as { ok: boolean; code?: string; details?: unknown; journal_entry_id?: string; mode?: string; linked_tx_count?: number; docs_linked?: number }
if (!result || !result.ok) {
const code = result?.code
const entry = code ? getErrorEntry(code) : undefined
return {
error: result?.code || 'bulk_book_transactions failed',
status: 400,
error: code || 'bulk_book_transactions failed',
// Registry httpStatus so the dispatcher can tell an authorization
// refusal (403: nothing posted, op must stay pending) from bad input
// (400) or a vanished/already-booked tx (404/409: auto-reject).
status: entry?.httpStatus ?? 400,
...(code ? { errorCode: code } : {}),
data: result?.details as Record<string, unknown> | undefined,
}
}
@@ -6257,6 +6274,7 @@ async function commitPendingOperationInner(
error: CAPABILITY_BLOCKED_MESSAGE_SV,
http_status: 403,
code: 'capability_blocked',
operation_status: 'pending',
}
}
@@ -6472,7 +6490,7 @@ async function commitPendingOperationInner(
result = await commitMatchBatchAllocate(supabase, userId, companyId, pendingOp.params)
break
case 'bulk_book_transactions':
result = await commitBulkBookTransactions(supabase, companyId, pendingOp.params)
result = await commitBulkBookTransactions(supabase, userId, companyId, pendingOp.params)
break
case 'bulk_book_inbox_items':
result = await commitBulkBookInboxItems(supabase, userId, companyId, pendingOp.params)
@@ -6528,6 +6546,7 @@ async function commitPendingOperationInner(
http_status: 500,
code: 'partial_commit',
data: { posted_ids: err.postedIds },
operation_status: 'failed_partial',
}
}
// Accounts-not-in-chart is RECOVERABLE: the booking itself is valid; the
@@ -6546,6 +6565,7 @@ async function commitPendingOperationInner(
http_status: 400,
code: ACCOUNTS_NOT_IN_CHART,
account_numbers: err.accountNumbers,
operation_status: 'pending',
}
}
// Recoverable Skatteverket failure (extension disabled, no connection,
@@ -6562,6 +6582,7 @@ async function commitPendingOperationInner(
error: err.message,
http_status: err.httpStatus,
code: err.code,
operation_status: 'pending',
}
}
const isBkErr = isBookkeepingError(err)
@@ -6581,6 +6602,7 @@ async function commitPendingOperationInner(
status: 'failed',
error: message,
http_status: isBkErr ? 400 : 500,
operation_status: 'rejected',
}
}
@@ -6613,6 +6635,27 @@ async function commitPendingOperationInner(
http_status: result.status ?? 500,
code: 'partial_commit',
data: { posted_ids: partialPostedIds },
operation_status: 'failed_partial',
}
}
// Authorization refusals (401/403) happen BEFORE any side-effect and say
// nothing about the op's content: the credential, not the booking, was
// wrong. Release the claim back to 'pending' so the op survives for a
// caller that IS authorized (the /pending UI, or a key with the scope),
// instead of vanishing as 'rejected'. Feedback seq 261545: three
// samlingsverifikat were consumed this way and the user believed they
// had been approved.
if (result.status === 401 || result.status === 403) {
await supabase
.from('pending_operations')
.update({ status: 'pending' })
.eq('id', pendingOp.id)
return {
status: 'failed',
error: result.error,
http_status: result.status,
...(result.errorCode ? { code: result.errorCode } : {}),
operation_status: 'pending',
}
}
const isAutoReject = result.status === 404 || result.status === 409
@@ -6662,6 +6705,7 @@ async function commitPendingOperationInner(
http_status: result.status,
...(result.errorCode ? { code: result.errorCode } : {}),
...(failureDetails ? { data: failureDetails } : {}),
operation_status: 'rejected',
}
}
return {
@@ -6670,6 +6714,7 @@ async function commitPendingOperationInner(
http_status: result.status ?? 500,
...(result.errorCode ? { code: result.errorCode } : {}),
...(failureDetails ? { data: failureDetails } : {}),
operation_status: 'rejected',
}
}
@@ -6706,5 +6751,6 @@ async function commitPendingOperationInner(
return {
status: 'committed',
data: result.data,
operation_status: 'committed',
}
}
File diff suppressed because one or more lines are too long
@@ -0,0 +1,198 @@
import { randomUUID } from 'node:crypto'
import { describe, expect, it } from 'vitest'
import {
insertAuthUser,
insertCompany,
insertCompanyMember,
insertFiscalPeriod,
} from '@/tests/pg/fixtures'
import { getPool, runAsServiceRole, withUserContext } from '@/tests/pg/setup'
/**
* Covers 20260824170000_bulk_book_transactions_service_actor:
* - service_role caller + p_user_id of a member: the samlingsverifikat
* commits. This is the pending-operations commit path
* (createServiceClientNoCookies), which before the migration ALWAYS got
* BULK_BOOK_UNAUTHORIZED because auth.uid() is NULL on the service
* client, and the dispatcher then consumed the op (feedback seq 261545).
* - p_user_id is an assertion, honored ONLY under auth.role() =
* 'service_role': an authenticated non-member spoofing an owner's UUID
* and a caller with no JWT at all both stay BULK_BOOK_UNAUTHORIZED.
* - Grants: PUBLIC/anon revoked, authenticated + service_role kept; the
* old 4-arg signature is gone (the 5th arg has a DEFAULT, so 4-arg call
* sites still resolve).
*/
async function insertTransaction(params: {
userId: string
companyId: string
amount: number
}): Promise<string> {
const id = randomUUID()
await getPool().query(
`INSERT INTO public.transactions
(id, user_id, company_id, date, description, amount, currency, category)
VALUES ($1, $2, $3, '2026-06-05', 'Swish inbetalning', $4, 'SEK', 'uncategorized')`,
[id, params.userId, params.companyId, params.amount],
)
return id
}
async function seedTenant() {
const userId = await insertAuthUser()
const companyId = await insertCompany({ createdBy: userId })
await insertCompanyMember({ companyId, userId, role: 'owner' })
await insertFiscalPeriod({
userId,
companyId,
periodStart: '2026-01-01',
periodEnd: '2026-12-31',
})
// The RPC validates every line's account_number against the company's
// active chart; seed only the accounts the entry touches.
await getPool().query(
`INSERT INTO public.chart_of_accounts
(user_id, company_id, account_number, account_name, account_class, account_type, normal_balance, is_active)
SELECT $1, $2, n, name, cls, atype, nbal, true
FROM (VALUES
('1930', 'Bankkonto', 1, 'asset', 'debit'),
('2611', 'Utgående moms 25%', 2, 'liability', 'credit'),
('3001', 'Försäljning 25% moms', 3, 'revenue', 'credit')
) AS t(n, name, cls, atype, nbal)`,
[userId, companyId],
)
const tx1 = await insertTransaction({ userId, companyId, amount: 250 })
const tx2 = await insertTransaction({ userId, companyId, amount: 350 })
return { userId, companyId, txIds: [tx1, tx2] }
}
const NEW_ENTRY = {
description: 'Dagskassa Swish',
lines: [
{ account_number: '1930', debit_amount: 600, credit_amount: 0, currency: 'SEK', line_description: 'Inbetalningar Swish' },
{ account_number: '3001', debit_amount: 0, credit_amount: 480, currency: 'SEK', line_description: 'Försäljning' },
{ account_number: '2611', debit_amount: 0, credit_amount: 120, currency: 'SEK', line_description: 'Utgående moms 25%' },
],
}
interface RpcResult {
ok: boolean
code?: string
journal_entry_id?: string
linked_tx_count?: number
}
const CALL = `SELECT bulk_book_transactions($1::uuid[], $2, $3::jsonb, $4, $5) AS result`
describe('bulk_book_transactions service actor', () => {
it('commits for a service_role caller with p_user_id of a member and attributes the verifikat to that user', async () => {
const { userId, companyId, txIds } = await seedTenant()
const result = await runAsServiceRole(async (client) => {
const r = await client.query<{ result: RpcResult }>(CALL, [
txIds,
null,
JSON.stringify(NEW_ENTRY),
companyId,
userId,
])
return r.rows[0]!.result
})
expect(result.ok).toBe(true)
expect(result.journal_entry_id).toBeTruthy()
expect(result.linked_tx_count).toBe(2)
const je = await getPool().query<{ user_id: string; status: string }>(
`SELECT user_id, status FROM public.journal_entries WHERE id = $1`,
[result.journal_entry_id],
)
expect(je.rows[0]!.user_id).toBe(userId)
expect(je.rows[0]!.status).toBe('posted')
})
it('still rejects a service_role caller that passes no p_user_id', async () => {
const { companyId, txIds } = await seedTenant()
const result = await runAsServiceRole(async (client) => {
const r = await client.query<{ result: RpcResult }>(CALL, [
txIds,
null,
JSON.stringify(NEW_ENTRY),
companyId,
null,
])
return r.rows[0]!.result
})
expect(result.ok).toBe(false)
expect(result.code).toBe('BULK_BOOK_UNAUTHORIZED')
})
it('ignores a spoofed p_user_id from an authenticated non-member', async () => {
const { userId, companyId, txIds } = await seedTenant()
const stranger = await insertAuthUser()
const result = await withUserContext(stranger, async (client) => {
const r = await client.query<{ result: RpcResult }>(CALL, [
txIds,
null,
JSON.stringify(NEW_ENTRY),
companyId,
userId,
])
return r.rows[0]!.result
})
expect(result.ok).toBe(false)
expect(result.code).toBe('BULK_BOOK_UNAUTHORIZED')
})
it('ignores p_user_id when there is no JWT context at all', async () => {
const { userId, companyId, txIds } = await seedTenant()
const r = await getPool().query<{ result: RpcResult }>(CALL, [
txIds,
null,
JSON.stringify(NEW_ENTRY),
companyId,
userId,
])
expect(r.rows[0]!.result.ok).toBe(false)
expect(r.rows[0]!.result.code).toBe('BULK_BOOK_UNAUTHORIZED')
})
it('keeps the 4-arg call shape working for authenticated members (web route)', async () => {
const { userId, companyId, txIds } = await seedTenant()
const result = await withUserContext(userId, async (client) => {
const r = await client.query<{ result: RpcResult }>(
`SELECT bulk_book_transactions($1::uuid[], $2, $3::jsonb, $4) AS result`,
[txIds, null, JSON.stringify(NEW_ENTRY), companyId],
)
return r.rows[0]!.result
})
expect(result.ok).toBe(true)
})
it('keeps least-privilege grants and drops the 4-arg overload', async () => {
const { rows } = await getPool().query<{
anon_can: boolean
authenticated_can: boolean
service_role_can: boolean
overloads: string
}>(
`SELECT has_function_privilege('anon', 'public.bulk_book_transactions(uuid[],uuid,jsonb,uuid,uuid)', 'EXECUTE') AS anon_can,
has_function_privilege('authenticated', 'public.bulk_book_transactions(uuid[],uuid,jsonb,uuid,uuid)', 'EXECUTE') AS authenticated_can,
has_function_privilege('service_role', 'public.bulk_book_transactions(uuid[],uuid,jsonb,uuid,uuid)', 'EXECUTE') AS service_role_can,
(SELECT count(*) FROM pg_proc p
JOIN pg_namespace n ON n.oid = p.pronamespace
WHERE n.nspname = 'public' AND p.proname = 'bulk_book_transactions')::text AS overloads`,
)
expect(rows[0]!.anon_can).toBe(false)
expect(rows[0]!.authenticated_can).toBe(true)
expect(rows[0]!.service_role_can).toBe(true)
expect(rows[0]!.overloads).toBe('1')
})
})