feat(dimensions): PR10 advanced — custom dimensions, hierarchy, account rules, commit enforcement (#886)

* feat(dimensions): PR10 advanced — custom dimensions, hierarchy, account rules, commit enforcement

The final rung of the dimensions ladder
(dev_docs/dimensions_implementation_plan.md §7 row 10):

- custom dimensions: POST /api/dimensions creates registry dims (next free
  SIE number >= 20 when omitted; explicit numbers allowed — SIE import
  already mints reserved ones); register gets a 'Ny dimension' dialog with
  a quiet Avancerat disclosure for the #UNDERDIM parent; GET now carries
  parent_sie_dim_no (the column + SIE round-trip existed since PR1/PR5 —
  this exposes it)
- account_dimension_rules (migration 20260703120000): one rule per
  (account, dimension) — required / default / fixed, per-rule is_active,
  company-scoped RLS, composite FK to the registry, value-presence CHECK
- enforcement, opt-in BY CONSTRUCTION (zero rules = engine byte-identical;
  deliberately NO settings toggle — a rule that exists but is ignored is
  worse than either extreme): default/fixed apply onto line bags at draft
  creation (fixed overwrites, default fills); required asserts at
  commitEntry with a Swedish MANDATORY_DIMENSION_MISSING naming every
  account + dimension; the bulk-book route runs the same policy before its
  RPC; storno/correction paths never pass through commitEntry so history
  always reverses regardless of policy; rule fetches fail open incl.
  thrown exceptions
- chart of accounts: per-account Dimensionsregler section in
  EditAccountDialog (Krävs/Förval/Låst, value picker, pause switch),
  gated on the existing dimensions toggle, quiet when empty
- pickers: LineDimensionFields is registry-driven (one combobox per active
  dimension, cached fetch, hardcoded 1/6 fallback) — every existing mount
  lights up custom dims with zero changes
- agent briefing: per-dimension required_on_accounts/default_on_accounts
  so agents self-correct instead of bouncing off the policy error
- rules CRUD API with existence/active/company validation and qualified
  DTO ids; firm_id FK deferred until the firms table lands (per plan)

39 new tests (pure-fn rules, engine enforcement, both new API surfaces,
pg-real RLS/CHECK/cascade suite); full suite 6,791 green; migration
replayed on a fresh container.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: renumber migration to 20260703200000 — version collision with prod

The concurrent session shipped pending_operations_add_link_document_to_voucher
as 20260703120000 today; the Supabase preview branch (cloned from prod)
rejected the duplicate version key.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix: review round — auto-pick retry on collision, fail-open warnings, query schema

- POST /api/dimensions retries once past a concurrent number claim when the
  number was auto-picked (explicit choices still 409)
- every fail-open skip of the dimension-rules policy now logs a structured
  warning (engine draft/commit paths + bulk-book) — deliberate fail-open,
  but observable
- GET /api/dimensions/rules validates its query through
  ListDimensionRulesQuerySchema instead of an inline regex

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-03 16:50:28 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 70e893b8d4
commit 764348e99c
26 changed files with 2800 additions and 72 deletions
@@ -53,6 +53,7 @@ function mockSupabase(opts: {
// Dimension registry (dimensions PR3). Default: empty → block omitted.
dimensionRows?: Array<{ id: string; sie_dim_no: number; name: string }>
dimensionValueRows?: Array<{ dimension_id: string; code: string; name: string }>
dimensionRuleRows?: Array<{ account_number: string; rule_type: string; dimension_id: string }>
dimensionsEnabled?: boolean
errors?: { profile?: string; memory?: string; atoms?: string }
}) {
@@ -164,6 +165,10 @@ function mockSupabase(opts: {
if (table === 'dimension_values') {
return chainResolving(opts.dimensionValueRows ?? [])
}
if (table === 'account_dimension_rules') {
// PR10: the briefing surfaces active rules; default none.
return chainResolving(opts.dimensionRuleRows ?? [])
}
throw new Error(`Unexpected table in test mock: ${table}`)
}),
}
+34 -1
View File
@@ -2253,6 +2253,16 @@ export const tools: McpTool[] = [
sie_dim_no: { type: 'number' },
name: { type: 'string' },
active_value_count: { type: 'number' },
required_on_accounts: {
type: 'array',
description: 'BAS accounts with an active required-rule: postings there are refused without a value for this dimension.',
items: { type: 'string' },
},
default_on_accounts: {
type: 'array',
description: 'BAS accounts where a default/fixed rule auto-applies a value at draft creation.',
items: { type: 'string' },
},
top_values: {
type: 'array',
description: 'Up to 10 active values; full list via gnubok_list_dimension_values.',
@@ -2267,7 +2277,7 @@ export const tools: McpTool[] = [
},
},
},
required: ['sie_dim_no', 'name', 'active_value_count', 'top_values'],
required: ['sie_dim_no', 'name', 'active_value_count', 'required_on_accounts', 'default_on_accounts', 'top_values'],
},
},
},
@@ -2402,6 +2412,8 @@ export const tools: McpTool[] = [
sie_dim_no: number
name: string
active_value_count: number
required_on_accounts: string[]
default_on_accounts: string[]
top_values: Array<{ code: string; name: string }>
}>
}
@@ -2421,6 +2433,25 @@ export const tools: McpTool[] = [
bucket.push({ code: v.code, name: v.name })
byDimension.set(v.dimension_id, bucket)
}
// Account dimension rules (PR10): tell the agent up front which
// accounts refuse postings without a value (required) and which
// auto-apply one (default/fixed) — so gnubok_create_voucher calls
// self-correct instead of bouncing off MANDATORY_DIMENSION_MISSING.
const requiredByDimension = new Map<string, string[]>()
const defaultByDimension = new Map<string, string[]>()
const { data: ruleRows, error: ruleErr } = await supabase
.from('account_dimension_rules')
.select('account_number, rule_type, dimension_id')
.eq('company_id', companyId)
.eq('is_active', true)
if (!ruleErr) {
for (const r of (ruleRows ?? []) as Array<{ account_number: string; rule_type: string; dimension_id: string }>) {
const target = r.rule_type === 'required' ? requiredByDimension : defaultByDimension
const bucket = target.get(r.dimension_id) ?? []
bucket.push(r.account_number)
target.set(r.dimension_id, bucket)
}
}
dimensionsBlock = {
enabled: settingsRow?.dimensions_enabled === true,
dimensions: dimensionRows.map((d) => {
@@ -2429,6 +2460,8 @@ export const tools: McpTool[] = [
sie_dim_no: d.sie_dim_no,
name: d.name,
active_value_count: values.length,
required_on_accounts: (requiredByDimension.get(d.id) ?? []).sort(),
default_on_accounts: (defaultByDimension.get(d.id) ?? []).sort(),
top_values: values.slice(0, 10),
}
}),