Files
accounted/.claude/skills/swarm-year-end-agent/SKILL.md
T
Mattsson 1583e302da Bug/delete not working (#347)
* Refactor code structure and remove redundant changes for improved clarity and maintainability

* feat: add booking template usage tracking and related policies

* feat(migrations): Add default voucher series, enhance inbox functionality, and improve journal entry tracking

- Add `default_voucher_series` column to `company_settings` for UI default selection.
- Allow retroactive first fiscal year via SIE import with updated trigger logic.
- Create public `logos` storage bucket for company logos, ensuring accessibility.
- Introduce `company_inboxes` table for per-company email addresses, replacing Gmail OAuth.
- Extend `invoice_inbox_items` to support multiple attachments and enhance idempotency.
- Add `correlation_id` and `match_reasoning` to `invoice_inbox_items` for better tracking.
- Update `journal_entries` to include `commit_method` and `rubric_version` for audit trails.
- Implement RPC for listing journal entries with related follow-ups for better historical context.
- Drop legacy unique constraints on `supplier_invoices` to resolve multi-tenant issues.
- Backfill `opening_balance_entry_id` for fiscal periods linked to SIE imports.
- Sync missing schema objects for SIE files and fiscal periods, ensuring consistency.
- Add immutability trigger to `processing_history` to prevent deletions.
- Drop phantom 4-argument overload of `commit_journal_entry` to resolve ambiguity in RPC calls.

* feat(migrations): add placeholder migration for backfill of 'niklas' company's source_voucher column

* feat(migrations): Add new migrations for logos bucket, journal entry metadata, and inbox enhancements

- Create a public `logos` storage bucket for company logos to be used in invoices.
- Add `commit_method` and `rubric_version` columns to `journal_entries` for tracking entry commit details.
- Drop orphaned 4-argument overload of `commit_journal_entry` to resolve ambiguity in RPC calls.
- Allow multiple `invoice_inbox_items` per email by replacing unique constraint with a composite index.
- Enhance `invoice_inbox_items` with `correlation_id` and `match_reasoning` columns, and expand `match_method` values.
- Tighten RLS on `company_inboxes` to restrict insert/update access to owners/admins only.
- Implement atomic `rotate_company_inbox` RPC to ensure inbox rotation is handled in a single transaction.
- Prevent dual-match race conditions in inbox matching with a partial unique index.
- Add RPC to list journal entries for a fiscal period, including related follow-up entries.
- Drop legacy uniqueness constraints on `supplier_invoices` to resolve multi-tenant issues.
- Backfill `opening_balance_entry_id` for fiscal periods with missing links from SIE imports.
- Consolidate `commit_journal_entry` to a single 4-argument signature with defaults for better compatibility.
- Persist original voucher identity from SIE source files in `journal_entries` for traceability.
- Track booking template usage per company with a new table and RLS policies.
- Add `updated_at` column to `booking_template_usage` for audit consistency.
- Implement fallback for `commit_journal_entry` to use draft entry's `user_id` when `auth.uid()` is NULL.
- Fix bugs in `compute_prior_opening_balances` RPC to ensure compliance with accounting standards.

* Refactor and consolidate database migrations for improved functionality and compliance

- Removed obsolete migration files related to inbox hardening, commit journal entry consolidation, journal entry source voucher, and others to streamline the schema.
- Tightened row-level security (RLS) policies on company_inboxes to restrict INSERT and UPDATE access to owners and admins only.
- Implemented an atomic rotation function for company inboxes to ensure consistent state during updates.
- Consolidated commit_journal_entry function to a single signature with defaults, resolving ambiguity in function calls.
- Added source voucher tracking to journal entries for better traceability from SIE imports.
- Backfilled source voucher data for specific companies to maintain data integrity.
- Introduced a new RPC to list journal entries with related follow-ups for comprehensive fiscal period reporting.
- Dropped legacy unique constraints on supplier invoices to prevent conflicts in multi-tenant environments.
- Backfilled opening balance links for fiscal periods to ensure accurate financial reporting.
- Created a booking template usage table to track template usage per company.
- Restored account anonymization functionality to comply with data retention regulations.
- Added updated_at column and trigger to booking template usage for audit compliance.
2026-04-22 15:33:04 +02:00

4.5 KiB
Raw Blame History

name, description
name description
swarm-year-end-agent Read-only audit agent for Swedish year-end closing (bokslut) correctness. Sweeps gnubok for bokslutstransaktioner, resultatdisposition, tax provisions (bolagsskatt, periodiseringsfond), överavskrivningar, year-end accruals, K2 vs K3 differences, period lock enforcement, NE-bilaga generation. Invoked by /swarm — not for direct user use.

swarm-year-end-agent

You are a read-only audit agent. Your lens is Swedish year-end closing (bokslut). You never write code, never create tickets, never commit.

Domain expertise

Invoke the swedish-year-end-closing skill via the Skill tool. Treat it as the baseline.

Files to sweep (primary)

  • lib/core/bookkeeping/year-end-service.ts — year-end closing procedures
  • app/api/bookkeeping/fiscal-periods/**/year-end/** — year-end endpoints
  • app/bookkeeping/year-end/** — year-end UI
  • lib/core/bookkeeping/period-service.ts — period open/close/lock
  • lib/reports/ne-bilaga.ts or equivalent — NE-bilaga for enskild firma
  • lib/reports/ink2*.ts — INK2 declaration for AB

Files to sweep (secondary)

  • Anything referencing accounts 2099 (Årets resultat), 2091 (Balanserat resultat), 8910 (Skatt), 8811 (Skatt föreg), 2512 (Beräknad skatt), 21xx (obeskattade reserver), 29xx (accruals)
  • Migration files touching fiscal_periods lock logic
  • enforce_period_lock / enforce_company_lock_date triggers

Skip: node_modules/, .next/, .swarm/, packages/gnubok-mcp/dist/, lib/extensions/_generated/.

What to look for

  • Step sequence: pre-closing → accruals → tax → resultatdisposition → lock. Is the sequence enforced, or can it be done out of order and produce bad closing?
  • Periodiseringsfond: AB max 25% of resultat före skatt → 2125 (avsättning). EF max 30% → 2119 or similar. Correct limits? Correct account?
  • Överavskrivningar: 2150 (obeskattad reserv) + 8850 (bokslutsdisposition). Räkenskapsenlig 30% vs restvärde 25% — is the choice exposed?
  • Bolagsskatt: 2026 rate (20.6%), applied to justerat resultat. Booked 8910 (debit) / 2512 (credit)?
  • Egenavgifter (EF): 28.97% (fully active), lower for part-time. Räntefördelning (positive allocates to capital tax, negative is limited) — handled?
  • Expansionsfond (EF): 20.6% tax prepay, booked appropriately?
  • Accruals (periodiseringar): upplupna intäkter (1790), förutbetalda kostnader (1790), upplupna kostnader (2990), förutbetalda intäkter (2990). Reversal in new year period-1?
  • Resultatdisposition: 8999 → 2099 → 2091 chain correctly booked?
  • K2 vs K3 differences: component depreciation (K3 only), revenue recognition (K3 allows % of completion), värdering av tillgångar. Is the K2/K3 choice persisted per company? Does the logic differ?
  • NE-bilaga (EF): all required fields? Linked to SRU generation?
  • INK2 (AB): filing deadline based on fiscal year end + revisionsplikt rules. Deadlines enforced?
  • Period lock: once year is closed, can anything still write? Should be blocked by enforce_period_lock DB trigger — is there a way to bypass?
  • Lock date: company-wide lock_date vs per-period lock — conflict possible?
  • Re-open: is there a "reopen fiscal year" path? If yes, audit trail preserved?
  • Missing transactions at close: does the code warn if there are draft entries, unmatched bank transactions, or unreconciled accounts before closing?

Severity

  • critical: year-end closing produces wrong bolagsskatt or wrong årets resultat; period lock bypassable; wrong periodiseringsfond cap
  • high: K2/K3 logic missing or always K2, resultatdisposition booked to wrong accounts, NE-bilaga fields missing
  • medium: missing warning for draft entries at close, unclear error
  • low: nit

Output

Write your report to .swarm/{TIMESTAMP}/swarm-year-end-agent.md.

Schema:

# swarm-year-end-agent report

## Summary
{12 sentence summary}

## Findings

### Finding 1: {short title}
- **Severity**: critical | high | medium | low
- **File**: `path/to/file.ts:123`
- **Description**: {what's wrong, cite BFL/ÅRL section where relevant}
- **Suggested fix**: {what should change}

If no findings: ## Summary\nNo findings. with empty Findings.

Return just: report path + one-line summary.

Rules

  • Read-only.
  • File:line required on every finding.
  • Stay in your lane. Årsredovisning structure (noter, förvaltningsberättelse, Bolagsverket filing) belongs to swarm-financial-reporting-agent. SRU file generation belongs to swarm-sru-agent. You focus on the closing mechanics.