Accounted rebrand + swarm-skill cleanup + bank-reconciliation fixes (#643)

* feat(reconciliation): close the bank-feed loop on voucher links and re-tag mis-typed opening balances

Two related fixes to bank reconciliation correctness:

1. Auto-reconcile on voucher link. Linking an invoice or supplier invoice to
   an existing voucher previously advanced only the invoice — the bank
   transaction that paid it kept sitting in the Transactions inbox with a null
   journal_entry_id. linkInvoiceToVoucher / linkSupplierInvoiceToVoucher now
   call autoReconcileTransactionForLinkedVoucher (lib/reconciliation), which
   links the bank transaction to the same verifikat when exactly one unbooked
   line matches it. Best-effort and post-commit: a failure here never fails the
   link. The result surfaces reconciledTransactionId; the inbox row leaves the
   list and the UI shows link_success_tx_reconciled.

2. Re-tag mis-typed opening balances. getReconciliationStatus and the GL-line
   matching RPCs identify a cash account's ingående balans solely by
   journal_entries.source_type='opening_balance'. Companies migrated from other
   systems often booked the bank IB as an ordinary voucher (source_type
   'import' or 'manual'), so it was never excluded and surfaced as a phantom
   reconciliation difference equal to the opening balance. Adds:
   - migration mark_entry_as_opening_balance: a GUC-gated carve-out in the
     immutability trigger plus a SECURITY DEFINER RPC that validates the entry
     (balance-sheet lines only, dated on a fiscal-period boundary), flips the
     source_type, and writes an audit row — no blanket data sweep.
   - POST /api/reconciliation/bank/mark-opening-balance + MarkOpeningBalanceSchema.
   - BankReconciliationView action to trigger it from the IB diff.

The gnubok_create_voucher executor now accepts a typed is_opening_balance flag
and derives source_type='opening_balance' only after validating class 1/2 lines
on the period start, so new IBs land correctly typed.

Covered by lib/reconciliation auto-reconcile tests, voucher-executors tests,
and a mark-entry-as-opening-balance pg-real test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* chore: rebrand gnubok → Accounted and prune swarm agent skills

Product rebrand and skills housekeeping. No runtime behaviour change.

Rebrand: replace user-visible "gnubok" with "Accounted" across docs, READMEs,
in-code comments, doc-site content, MCP skill/resource prose, and the
gnubok-mcp package description. The MCP resource URI scheme is moved gnubok://
→ Accounted:// consistently across resource registrations, the event-type
comment, and the resource/skill tests. Deliberately preserved as stable
identifiers (NOT rebranded): the gnubok-company-id cookie, gnubok_sk_ / gnubok_inv_
token prefixes, the gnubok-mcp npm bridge name, and the AGI <gem:Programnamn>
value (kept 'gnubok' per its source comment — it is the software identifier sent
to Skatteverket and must not churn across visual rebrands).

Skills: remove the 27 swarm-* agent SKILL.md atoms (no longer used; already
absent from the agent_atom_registry in prod), refresh the remaining skill docs,
add the .claude/rules/ path-scoped rule set, and regenerate the
seed_agent_atom_bodies migration + .skill-body-manifest.json via
`npm run skills:generate` so the DB-backed skill bodies match the trimmed set.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-06-03 10:52:01 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 331ae11867
commit c74b19df1b
183 changed files with 19621 additions and 4175 deletions
+61 -4
View File
@@ -1035,6 +1035,7 @@ async function commitLinkInvoiceVoucher(
payment_amount: outcome.result.paymentAmount,
payment_id: outcome.result.paymentId,
journal_entry_id: outcome.result.journalEntryId,
reconciled_transaction_id: outcome.result.reconciledTransactionId,
},
}
}
@@ -1077,6 +1078,7 @@ async function commitLinkSupplierInvoiceVoucher(
payment_amount: outcome.result.paymentAmount,
payment_id: outcome.result.paymentId,
journal_entry_id: outcome.result.journalEntryId,
reconciled_transaction_id: outcome.result.reconciledTransactionId,
},
}
}
@@ -2330,6 +2332,64 @@ async function commitCreateVoucher(
fiscalPeriodId = resolved
}
// source_type is derived here — never trust params.source_type. The MCP tool
// stages a typed boolean (is_opening_balance), not a raw source_type string,
// so a tampered or future direct-staging path can't inject
// 'bank'/'invoice'/etc. and corrupt audit attribution. The default is
// 'manual'. We only upgrade to 'opening_balance' after independently
// re-validating the entry genuinely looks like an ingående balans — this
// matters because bank reconciliation excludes an IB from the period movement
// ONLY when source_type='opening_balance' (lib/reconciliation/bank-reconciliation.ts);
// a mislabelled 'manual' IB shows up as a phantom reconciliation difference.
let sourceType: JournalEntrySourceType = 'manual'
if (params.is_opening_balance === true) {
// Constraint 1: every line must be a balance-sheet account (BAS class 1 or
// 2). Mirrors the canonical opening-balance flow which rejects P&L accounts
// (app/api/import/opening-balance/execute/route.ts). Inlined to avoid
// coupling this executor to the SIE-import module.
const nonBalanceSheet = lines
.map((l) => l.account_number)
.filter((num) => {
const cls = parseInt(num.charAt(0), 10)
return !(cls === 1 || cls === 2)
})
if (nonBalanceSheet.length > 0) {
return {
error:
`Ingående balans får bara innehålla balanskonton (klass 1–2). ` +
`Dessa konton hör inte hemma i en IB: ${[...new Set(nonBalanceSheet)].join(', ')}. ` +
`Bokför resultatkonton som en vanlig verifikation utan is_opening_balance.`,
status: 400,
}
}
// Constraint 2: the entry must be dated on the fiscal period's first day —
// an IB opens the period (same as the canonical flow, which dates the entry
// on period.period_start). We fetch period_start here because the resolved
// fiscalPeriodId may have come from either the explicit param or a date
// lookup; either way the date must line up exactly.
const { data: period, error: periodErr } = await supabase
.from('fiscal_periods')
.select('period_start, name')
.eq('id', fiscalPeriodId)
.eq('company_id', companyId)
.maybeSingle()
if (periodErr || !period) {
return { error: 'Räkenskapsperioden hittades inte.', status: 404 }
}
if (entryDate !== period.period_start) {
return {
error:
`En ingående balans måste dateras på räkenskapsårets första dag ` +
`(${period.period_start}). Angivet datum: ${entryDate}. ` +
`Ändra datumet eller bokför som en vanlig verifikation utan is_opening_balance.`,
status: 400,
}
}
sourceType = 'opening_balance'
}
try {
const entry = await createJournalEntry(
supabase,
@@ -2339,10 +2399,7 @@ async function commitCreateVoucher(
fiscal_period_id: fiscalPeriodId,
entry_date: entryDate,
description,
// source_type is hardcoded — never trust params.source_type. The MCP
// tool stages 'manual', but a future direct-staging path could
// otherwise inject 'bank'/'invoice'/etc. and corrupt audit attribution.
source_type: 'manual' as JournalEntrySourceType,
source_type: sourceType,
voucher_series: (params.voucher_series as string) || undefined,
notes: (params.notes as string) || undefined,
lines,