fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s

Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
2026-11-10 09:37:22 +01:00
parent 077251ac94
commit e7f0070e84
6135 changed files with 1718997 additions and 4 deletions
+434
View File
@@ -0,0 +1,434 @@
{
"horizontal/swedish-accounting-compliance": {
"hash": "f63d1bbcda84007716ebcfb805f6399cc51875d4139097281c56b76942e62544",
"version": 5
},
"horizontal/swedish-accounting-compliance/bas-kontoplan": {
"hash": "0d1b05466568344be0afd4b360d582db23331ae4f1b4b05681cc92c50d099bdb",
"version": 2
},
"horizontal/swedish-accounting-compliance/bfl-bfnar": {
"hash": "78744006a40cbaeb4b45510cd95b47b65e13f5c523503ed420a7187cd858c8b2",
"version": 3
},
"horizontal/swedish-accounting-compliance/changes-2025-2026": {
"hash": "ffc4c5d618bb8840f43fd48c8d4a3e464768e84abdaeb49da016e0051b260964",
"version": 3
},
"horizontal/swedish-accounting-compliance/sie4": {
"hash": "0b73718716d3aab442c25bb0d0fa8057087481fe86f6792e28403dd03fb5af11",
"version": 1
},
"horizontal/swedish-accounting-compliance/skatteverket": {
"hash": "9605f2b56989119b3cac2081a84cc7dbf7bc73e10e40d8648b917b4cff06bacf",
"version": 3
},
"horizontal/swedish-asset-accounting": {
"hash": "790a9d8e712167409b9d626e24f01250df81de26e41e19f4b272db37410dea65",
"version": 4
},
"horizontal/swedish-asset-accounting/accounts-and-registry": {
"hash": "b33d044b36d290aebb3677ae82020e0890aff3664070f2d1ec510f38b63793f4",
"version": 1
},
"horizontal/swedish-asset-accounting/depreciation": {
"hash": "f764c19f11c8613ba5014ca2767ddb1ae2926928e04c3abb43444ba27c286aec",
"version": 1
},
"horizontal/swedish-asset-accounting/leasing-and-disposal": {
"hash": "cefd92341d2946c97ae10f456634a7cf569b7d0eab65d9ca9f8ca7017675f66e",
"version": 2
},
"horizontal/swedish-e-invoicing": {
"hash": "59fb8f4ab9de7c907cf054f37ae6b651504987375f440831e199710b2a938223",
"version": 7
},
"horizontal/swedish-e-invoicing/consumer-and-b2c": {
"hash": "ebcb2c7e888497b61a5b25616314ae620f40d220c93c07a48aa4a49d00ac2068",
"version": 2
},
"horizontal/swedish-e-invoicing/european-mandates": {
"hash": "cca73256fac8b3cd4ae614a2d512206ae30764676da88df3dc903e29a189a7d4",
"version": 3
},
"horizontal/swedish-e-invoicing/implementation-guide": {
"hash": "a52c02278e98d7418b2866cf0dd2211629e4b1381d0c5d28ba3df4df83ee4415",
"version": 3
},
"horizontal/swedish-e-invoicing/legal-and-regulatory": {
"hash": "a5efa085631187766375a43b8a740497c4132466c0ec2873d798dcc823db8937",
"version": 3
},
"horizontal/swedish-e-invoicing/market-provider-pricing": {
"hash": "d99359e2b9b1455c6c2df20b266aca4d79b6585e3591125873751e947b5029f7",
"version": 3
},
"horizontal/swedish-e-invoicing/peppol-bis-billing": {
"hash": "2407acacabb60836e1ebfff45f7d443dda92c3c432df83fd447cec6d5375c89f",
"version": 2
},
"horizontal/swedish-e-invoicing/peppol-network": {
"hash": "efb8a84884c84a48bdc68049467d43f40503970bba7659231f7d964f51b5c2a1",
"version": 3
},
"horizontal/swedish-e-invoicing/swedish-cius-and-specifics": {
"hash": "d3cadfe9c435013813edefdb435505f6df8f577352b929a24f156aeb0636ab5e",
"version": 3
},
"horizontal/swedish-financial-reporting": {
"hash": "4fc9d925de42b6a1c4658b960f269f97cf7035cd2041658d17e1883480d9be4b",
"version": 3
},
"horizontal/swedish-financial-reporting/arsredovisning-structure": {
"hash": "ae4789f2e72d8b372aafdf0ff0a0211a871b1dd62be76be836fe3cb66d6fe276",
"version": 1
},
"horizontal/swedish-financial-reporting/bolagsverket-filing": {
"hash": "893fd5c00be8f4c8c4a2cfc4058c071b7821f290f9527240cfba6bccb1760cdc",
"version": 1
},
"horizontal/swedish-financial-reporting/ink2-form-logic": {
"hash": "66b2a41ee2217089af8022c3ca36894a4df727536769ea84f77facb337ebe1d1",
"version": 1
},
"horizontal/swedish-invoice-compliance": {
"hash": "76716727dc49b3c0f79a570039de577c2aa03d266413d8fe0853607540e3397c",
"version": 9
},
"horizontal/swedish-invoice-compliance/invoice-rules": {
"hash": "00f8b5a1e2883595f470216f1200d88481de9dc6aab3520bac7ab4589922a386",
"version": 2
},
"horizontal/swedish-payroll": {
"hash": "b9d3a10d1c949b01185df001b08f150ebbaeca88bb837913bd9edab6f90e3859",
"version": 5
},
"horizontal/swedish-payroll/agi-filing": {
"hash": "91aa490472fafff434088301dd193ed474589b36ae22bdaab640b42bc3b3c255",
"version": 1
},
"horizontal/swedish-payroll/bas-7xxx": {
"hash": "61464f317f81e9bb55d6d65a747a457b85440b076145ed44cd4055b2f0d3c324",
"version": 2
},
"horizontal/swedish-payroll/benefits": {
"hash": "bb4ead2f45691bc84c09b7433e3b2c719c93b54f702978cfd362b125ae9c8ca2",
"version": 1
},
"horizontal/swedish-payroll/deductions-lonevaxling": {
"hash": "9d05b0e114ed2bc10285a0c304e5f488ee37a4977975336a76c91c1f312f0137",
"version": 1
},
"horizontal/swedish-payroll/f-skatt": {
"hash": "f8d2bd87e76497e7c7e5fbb65de5c77e28f75b603608341a8d8f05f94e8bb974",
"version": 1
},
"horizontal/swedish-payroll/ob-overtime": {
"hash": "4d4e079ae3178ab070ebd696c020d2598352e706430354de0f4bcbfcfcdbc471",
"version": 2
},
"horizontal/swedish-payroll/sick-pay": {
"hash": "ba6b5dbdbb8e00edb9c592aae2185980ca52aa20888434bc5761ca307de712dd",
"version": 1
},
"horizontal/swedish-payroll/social-charges": {
"hash": "f5ea138688fc769581d73575c7ec05b15c97a3c3f30ac698631441ba5ed07e77",
"version": 2
},
"horizontal/swedish-payroll/tax-tables": {
"hash": "7fbd2be587104f26caf18d28e058d6e9ac5ea3fb167a0990650442b1ffccd9b5",
"version": 1
},
"horizontal/swedish-payroll/travel-expenses": {
"hash": "3ed4af7d164d2a3877ded04115081ea5c837b1397c966b40c9e09284c8f4b4bf",
"version": 1
},
"horizontal/swedish-payroll/vacation-pay": {
"hash": "9a29f4e333b51cd85074d8650705d09dfcba42301c61a508a7b0967ee9d4a5e8",
"version": 1
},
"horizontal/swedish-project-accounting": {
"hash": "273e3dcce1d4582264694231668fb4c555379cf5f40e19ebc65b34d8bd284c51",
"version": 3
},
"horizontal/swedish-project-accounting/accounts-and-entries": {
"hash": "16dba1ef3553d06dcd34bbb7d95c34f4af1738222fb08145f2ed511d176b1b89",
"version": 1
},
"horizontal/swedish-project-accounting/implementation-patterns": {
"hash": "db59db720c4df2eaacbdcd0b3f36e02172f3763ac6949865123ac29db7966c3b",
"version": 1
},
"horizontal/swedish-project-accounting/k2-k3-revenue-recognition": {
"hash": "6065ee97e019543b89dc42ee422ea3139d6dd84097b882c353423bfb3b25094e",
"version": 1
},
"horizontal/swedish-project-accounting/sie4-project-dimensions": {
"hash": "b92b0c58ee793c15abb46ed81f2fbd47abdbecbc3f0ffe986d19e7331c373e94",
"version": 1
},
"horizontal/swedish-project-accounting/tax-and-grants": {
"hash": "b43e2655a415ad59ebd5b87e8e505adf80b821bc96487fac9e542eaa2cdfbdad",
"version": 1
},
"horizontal/swedish-sie-import-export": {
"hash": "6d003ee8c3439058f60c63ff5f2ca153cf6c1b6657bda56754352a6d409bd831",
"version": 3
},
"horizontal/swedish-sie-import-export/bas-sru": {
"hash": "7fb1f664b69f5cce897938115d730d00e736d81b6216b6aa13990fc3db95259a",
"version": 2
},
"horizontal/swedish-sie-import-export/encoding": {
"hash": "eaa520fe47638fd521d27d0f9d6315d5c15f47eed2b4df408c9e9addba10c7d8",
"version": 1
},
"horizontal/swedish-sie-import-export/record-types": {
"hash": "1a83c6c67eea2476bef8c13e1d96e4817b9313b2566ca4a1c91456975087e84e",
"version": 1
},
"horizontal/swedish-sie-import-export/validation-rules": {
"hash": "8b08c3d09757fe261bbab469e8d30cb51b960f423d57ada622cadd17a92c9598",
"version": 1
},
"horizontal/swedish-sru-filing": {
"hash": "56a1ce1be4908e15b6e3c9dcad64e5a3fb0f4400c47a2f88504d0bf897f4a9da",
"version": 6
},
"horizontal/swedish-sru-filing/sru-codes": {
"hash": "ad5f50531905891aff7bdc2d37a010eeccb8b4df902e3a7ce2b228cd6ad31e7e",
"version": 4
},
"horizontal/swedish-tax-planning": {
"hash": "99b87fac9eacf0ad168434c2bce99f21cdb8a53cb26ccabb64ba291b9059aa24",
"version": 3
},
"horizontal/swedish-tax-planning/312-regler": {
"hash": "f6fd1e6edb45cc31ec990586b08f9e68aa09be15b6e072874c0770bde941a649",
"version": 1
},
"horizontal/swedish-tax-planning/kapitalforsakring": {
"hash": "3119126a58c9c3729d34303cb5f36c27c75e664e5048544fea2e24a6ac71d3fd",
"version": 1
},
"horizontal/swedish-tax-planning/koncernbidrag": {
"hash": "a3870a1cdec023285cb7b6939dab1f31034c878c9dc9b986c737fc7a6852f14a",
"version": 1
},
"horizontal/swedish-tax-planning/overavskrivningar": {
"hash": "77b6f5219e5313c027684271552d799fba8156e6665a9ff16def93ba6caac6de",
"version": 1
},
"horizontal/swedish-tax-planning/periodiseringsfond": {
"hash": "b76ea9e0127040855f722e756ee2bbf4018821c1b8d93496cc2225a09c1a1f38",
"version": 1
},
"horizontal/swedish-tax-planning/ranteavdragsbegransningar": {
"hash": "b34bd57865b790dfa9a8165b04bac06549d197c18fac56894dc4e7fad22745a7",
"version": 1
},
"horizontal/swedish-tax-planning/strategy-and-interactions": {
"hash": "58f4e825c927998b60a7ef47ec9f75ea58bf801727b6c621662cda6654773019",
"version": 1
},
"horizontal/swedish-vat": {
"hash": "adb1b84256e9a17c8c54741530f529f9d0afa645c1f3ff2c77d13d44053544dc",
"version": 4
},
"horizontal/swedish-vat/vat-compliance-reference": {
"hash": "4fa3e4b047b15d3630a7cff41b1a96091daff32bcaa13410f2c4b964b57d0771",
"version": 2
},
"horizontal/swedish-year-end-closing": {
"hash": "ec90a8ecb3b36379350c7d7ae4e93bb50197d41a38a4c2091538320e59fc83b9",
"version": 3
},
"horizontal/swedish-year-end-closing/closing-process": {
"hash": "2d23c29bcf8a7f5d6ce72e677abe518efdf8300ed11360269377a342f51945b5",
"version": 3
},
"horizontal/swedish-year-end-closing/journal-entries": {
"hash": "b4891b88b6cf409f00c76c70b7acb9dbf20e13ac05814886f978b80b878ab0a9",
"version": 3
},
"horizontal/swedish-year-end-closing/k2-vs-k3": {
"hash": "e3fa066d0e1fe4284661df669736241daf05e08599f893b40f5a6f94722d0bce",
"version": 2
},
"horizontal/swedish-year-end-closing/legal-framework": {
"hash": "7624ca77cc9c5fe1830ea6dad2bba52b563238b44414c3cddbece651293ef144",
"version": 2
},
"horizontal/swedish-year-end-closing/pitfalls-and-rates": {
"hash": "2f68d58b2ccf25853452327d7c4e62793e2463f9e0213499e138bdec6bcbc23e",
"version": 2
},
"horizontal/swedish-year-end-closing/reporting-and-filing": {
"hash": "80673a4262cab034451d4daf69ce629cc8a5a1544240cea66afe858fef7526e8",
"version": 2
},
"horizontal/swedish-year-end-closing/tax-calculations": {
"hash": "522dcbc3dcb223482c7fc0cff307e921efcbbc19fa849b99d5e540fbb366e8f6",
"version": 2
},
"modifier/holding-ab": {
"hash": "b8426f432969cf23d10dbe169227d7f8c0b2bbc19f8e8d1a2f3b511fcb25ecbd",
"version": 3
},
"modifier/holding-ab/312-holding": {
"hash": "0df3e7cda2c612c4b9a91e4f5751b16c1567ea9d8628701757eb3756abe83228",
"version": 2
},
"modifier/holding-ab/aktieagartillskott": {
"hash": "96979e5d66f91aca5eee84f19a9370e656c0817796c7d33704423af96f5b3f3a",
"version": 2
},
"modifier/holding-ab/koncernbidrag": {
"hash": "f692b7a0aabe18ca25330acace7b889fcc1a303d928189b2ecdd5110e8e87097",
"version": 2
},
"modifier/holding-ab/kupongskatt-cfc": {
"hash": "e61b29e6be5e77bf0cf48937d3e5a03c9702c0945b69e6065ef83c27e61104ea",
"version": 2
},
"modifier/holding-ab/moms-holdingbolag": {
"hash": "9da07121d466834835b3ed8d5d8ad89c21df38dc89d07832e2337cfacd93b4cd",
"version": 2
},
"modifier/holding-ab/naringsbetingade-andelar": {
"hash": "8d1bb3b9ea02f3a04ca27c8ee7f3c40051255eb4b7fa72918df08a65b16ff97c",
"version": 2
},
"modifier/holding-ab/omstrukturering": {
"hash": "f9f52e8343809f1382e6aaf9882d1ca478cdaa38472c9d53b1b1410c76e26285",
"version": 2
},
"modifier/holding-ab/ranteavdrag": {
"hash": "b240d994a88daa13db9e0a8961ee6ddac57d5fb171b84fda6857c9b6ff85858b",
"version": 2
},
"modifier/mixed-verksamhet": {
"hash": "da6405e557094a7462139a586e3f6a40ccdcaed380990b19230a64a658788cc0",
"version": 3
},
"modifier/mixed-verksamhet/fordelningsnyckel-metoder": {
"hash": "5ca417bc9cbdc1fd3286061f94c3b485b417660beb2910e3fbc23280aa582cb9",
"version": 2
},
"modifier/mixed-verksamhet/frivillig-skattskyldighet-fastighet": {
"hash": "a5db6de348099214497f3c8c782dca57f5cdbc0ecf7c83e5b2cf467be3ca9c3d",
"version": 2
},
"modifier/mixed-verksamhet/holdingbolag-avdragsratt": {
"hash": "21151fb4bcb173e01183e74ca49b62d8774afdb424cae9481c75b1f8275cac39",
"version": 2
},
"modifier/mixed-verksamhet/jamkning-mechanics": {
"hash": "f2d39dd682adcc00f07ec0ee3336a17329883fa973ab096d148e7f2d30c3b723",
"version": 2
},
"modifier/single-shareholder-ab-fmb": {
"hash": "ccf0cb583c7f4ae58801f1e1bc58f2ed78c1d78febd979d882c565534b3e167d",
"version": 4
},
"vertical/bygg-hantverk": {
"hash": "fe046e23d98a4ff6ae35df0e0023ccf9168aa8dc63376b569402a026bb6d9f4a",
"version": 3
},
"vertical/bygg-hantverk/kollektivavtal": {
"hash": "8df2ea51abfc0323bb5cb15fc80f8319d53dface71823e680be3ad818d8e8e4f",
"version": 2
},
"vertical/bygg-hantverk/momsregler": {
"hash": "06d8415d93d25b011d0ebc142652b89c78a71fb204c0b79e5c3a9b32caa02b02",
"version": 2
},
"vertical/bygg-hantverk/pagaende-arbeten": {
"hash": "34fb23d739320cf5751b9a6d62f4346eb5bce2ca8006c5273af003144600d48f",
"version": 2
},
"vertical/bygg-hantverk/praxis": {
"hash": "0205bba6b1ba205c6df8ff557b1d4962e672dac7f07cc8eb7bceb379a2c48b7f",
"version": 2
},
"vertical/bygg-hantverk/rot-faktura-exempel": {
"hash": "9007433f888d8924370a5064eb7f2cef77c2c5c4fdc994cc9c78622a1f31b9d0",
"version": 2
},
"vertical/e-handel": {
"hash": "d1f7fc4c6e9870c2b8112800b85e10057b396a8972c153f3a84a3762343d4adc",
"version": 3
},
"vertical/e-handel/dac7-rapportering": {
"hash": "3ef510c39c1a1f1218e926ca141f0756557f32d7657c5e80846ed7ea5393e0dc",
"version": 2
},
"vertical/e-handel/dropshipping-kedjetransaktioner": {
"hash": "8a449c781bc908dd06524b8f4d337bec2d1cd1693a026bd8dbe8fbde711d2f9e",
"version": 2
},
"vertical/e-handel/kollektivavtal-lager-e-handel": {
"hash": "3130e2f3d04f89b2f55bb9b6af9d6e006d7b97ca8d47a661574911b7ccadca52",
"version": 2
},
"vertical/e-handel/konsumentratt": {
"hash": "66c91b4d36af7ed04f3adc6e9c1ea58cde561a2c223e50120cab8d0716ae3ff8",
"version": 2
},
"vertical/e-handel/marketplace-deemed-supplier": {
"hash": "41f683310ba98d4914f318d3e8b6c99a6f2cc393c9296ab9c4658bd5199c0132",
"version": 2
},
"vertical/e-handel/oss-ioss-mekanik": {
"hash": "94521265f9058f657e9f2f8c02ba129238e2067c1a3691465f75df814a71a655",
"version": 2
},
"vertical/e-handel/payment-providers": {
"hash": "52854656bcde98ce7c5e5896de23fe64cddc4b17cc29b0d01d4f9551dc652232",
"version": 2
},
"vertical/e-handel/skv-stallningstaganden-katalog": {
"hash": "f630352b6a514041bfb12d3780e6a983f0556bca13a5712bba77e1a1abe0e4c5",
"version": 2
},
"vertical/e-handel/vouchers-presentkort": {
"hash": "d7bf91f0eb5aa60f116861f218ee3cd8a2eac6a613126883e0be981d350e577b",
"version": 2
},
"vertical/konsult-it": {
"hash": "af181b70fa0c9ccc993d17b1cf6ed3d66eda16d28026d679ef472286a09162cd",
"version": 3
},
"vertical/konsult-it/3-12-rules": {
"hash": "412c18f873236a941e4c72910ccae174805bbb68ddb220352caf67a451f62433",
"version": 2
},
"vertical/konsult-it/consultant-vs-employee": {
"hash": "e07710aad7ef5b3bce3a83a4b760b4a853f7066ba825bf322b6af3c8a9de7eb9",
"version": 2
},
"vertical/konsult-it/cross-border-payroll": {
"hash": "9d904488b7966ff9af72043483035bc6d18013e00a1fcccc5a17e294432a9eb0",
"version": 2
},
"vertical/konsult-it/electronic-services-classification": {
"hash": "6dbf5448e9985ebfd42e005c4abee834a4adb6b4a3500326af0bfd9332c37241",
"version": 2
},
"vertical/konsult-it/invoice-templates": {
"hash": "924fb88d62abe4a6c211952b40850b39579c6b5a04afd2d23acf9b183dbafddf",
"version": 2
},
"vertical/konsult-it/software-capitalization": {
"hash": "6e87a0e45e58b9d45796c3257a3162f98bd0c9a211c5d2d53e7f1114bf8d5a47",
"version": 2
},
"vertical/reklambyra-marknadsforing": {
"hash": "ef0343360b9c3f2a110bf318505c0001a282d6073dcc15959cd6a22cf341993b",
"version": 3
},
"vertical/software-saas-ai": {
"hash": "a5a8a9a73230e5d0e18c30fe3a3a8a428e21df409deecd782c6683cb9c42f334",
"version": 2
}
}
+62
View File
@@ -0,0 +1,62 @@
import { describe, it, expect } from 'vitest'
import { discoverAtoms, type DiscoveredAtom } from '../lib/atom-discovery'
// Runs against the real .claude/skills tree (deterministic, committed content).
// Vitest's cwd is the repo root.
let atoms: DiscoveredAtom[]
async function load(): Promise<DiscoveredAtom[]> {
if (!atoms) atoms = await discoverAtoms(process.cwd())
return atoms
}
describe('discoverAtoms: reference children', () => {
it('emits top-level skills with parent_atom_id null', async () => {
const all = await load()
const vat = all.find((a) => a.id === 'horizontal/swedish-vat')
expect(vat, 'swedish-vat skill should be discovered').toBeDefined()
expect(vat!.parent_atom_id).toBeNull()
})
it('emits one child atom per references/*.md, linked to its parent', async () => {
const all = await load()
const child = all.find(
(a) => a.id === 'horizontal/swedish-vat/vat-compliance-reference',
)
expect(child, 'reference child should be discovered').toBeDefined()
expect(child!.parent_atom_id).toBe('horizontal/swedish-vat')
expect(child!.tier).toBe('horizontal')
// Child body is the raw reference file (its own heading), not the SKILL.md.
expect(child!.body).toContain('# Swedish VAT (Moms) Complete Compliance Reference')
expect(child!.estimated_tokens).toBeGreaterThan(0)
})
it('appends a Loadable references footer to parents that have references', async () => {
const all = await load()
const vat = all.find((a) => a.id === 'horizontal/swedish-vat')!
expect(vat.body).toContain('## Loadable references')
// The footer bridges the router filename to the loadable child id.
expect(vat.body).toContain(
'gnubok_load_skill("horizontal/swedish-vat/vat-compliance-reference")',
)
})
it('does not put the footer inside reference children themselves', async () => {
const all = await load()
for (const a of all.filter((x) => x.parent_atom_id !== null)) {
expect(a.body, `child ${a.id} should not carry the footer`).not.toContain(
'## Loadable references',
)
}
})
it('every child parent_atom_id resolves to a real top-level skill', async () => {
const all = await load()
const topLevelIds = new Set(all.filter((a) => a.parent_atom_id === null).map((a) => a.id))
const children = all.filter((a) => a.parent_atom_id !== null)
expect(children.length).toBeGreaterThan(0)
for (const c of children) {
expect(topLevelIds.has(c.parent_atom_id!), `${c.id} → ${c.parent_atom_id}`).toBe(true)
}
})
})
+285
View File
@@ -0,0 +1,285 @@
import { describe, it, expect } from 'vitest'
import { readFileSync, readdirSync, existsSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import {
buildCrontab,
readVercelCrons,
scheduleFor,
EXCLUDED_PATHS,
EXTRA_JOBS,
SCHEDULE_OVERRIDES,
VARIANTS,
type CrontabVariant,
type VercelCron,
} from '../generate-crontabs'
const ROOT = dirname(dirname(dirname(fileURLToPath(import.meta.url))))
const crons = readVercelCrons(readFileSync(join(ROOT, 'vercel.json'), 'utf8'))
function crontabText(variant: CrontabVariant): string {
return readFileSync(join(ROOT, 'docker', `crontab.${variant}`), 'utf8')
}
/**
* Independent parser: deliberately NOT the generator's renderer, so a bug in
* the generator cannot hide the very drift this test exists to catch.
*/
function parseCrontab(text: string): { path: string; schedule: string }[] {
return text
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0 && !line.startsWith('#'))
.map((line) => {
const match = line.match(/^(\S+(?:\s+\S+){4})\s+(.*)$/)
if (!match) throw new Error(`crontab line is not "<5-field schedule> <command>": ${line}`)
const [, schedule, command] = match
const pathMatch = command.match(/\$\{APP_URL\}(\/\S*)$/)
if (!pathMatch) throw new Error(`crontab command does not end in \${APP_URL}<path>: ${command}`)
return { schedule, path: pathMatch[1] }
})
}
const expectedPaths = crons.map((c) => c.path).filter((p) => !(p in EXCLUDED_PATHS))
const expectedPathsFor = (variant: CrontabVariant) => [
...expectedPaths,
...EXTRA_JOBS[variant].map((job) => job.path),
]
describe('docker crontabs mirror vercel.json', () => {
it.each(VARIANTS)('crontab.%s covers exactly the vercel.json path set minus exclusions, plus its EXTRA_JOBS', (variant) => {
const actual = parseCrontab(crontabText(variant)).map((job) => job.path)
// Sorted comparison gives a readable diff of what is missing / extra;
// the order assertion below covers sequence separately.
expect([...actual].sort()).toEqual([...expectedPathsFor(variant)].sort())
})
it.each(VARIANTS)('crontab.%s keeps vercel.json order, EXTRA_JOBS last', (variant) => {
expect(parseCrontab(crontabText(variant)).map((job) => job.path)).toEqual(expectedPathsFor(variant))
})
it.each(VARIANTS)('crontab.%s runs its EXTRA_JOBS on their declared cadence', (variant) => {
const actual = new Map(parseCrontab(crontabText(variant)).map((job) => [job.path, job.schedule]))
for (const job of EXTRA_JOBS[variant]) {
expect(actual.get(job.path), `schedule for extra job ${job.path}`).toBe(job.schedule)
}
})
it.each(VARIANTS)('crontab.%s runs every path on its vercel.json cadence', (variant) => {
const actual = new Map(parseCrontab(crontabText(variant)).map((job) => [job.path, job.schedule]))
for (const cron of crons) {
if (cron.path in EXCLUDED_PATHS) continue
expect(actual.get(cron.path), `schedule for ${cron.path} in crontab.${variant}`).toBe(
scheduleFor(cron, variant),
)
}
})
it.each(VARIANTS)('crontab.%s is byte-identical to the generator output', (variant) => {
// Fails when someone hand-edits a crontab, or edits vercel.json without
// running `npm run crontabs:generate`.
expect(crontabText(variant)).toBe(buildCrontab(crons, variant))
})
it('emits LF line endings and a trailing newline (supercronic + .gitattributes)', () => {
for (const variant of VARIANTS) {
const raw = readFileSync(join(ROOT, 'docker', `crontab.${variant}`))
expect(raw.includes(0x0d), `CR byte in crontab.${variant}`).toBe(false)
expect(raw[raw.length - 1], `trailing newline in crontab.${variant}`).toBe(0x0a)
}
})
it('keeps the two variants identical apart from the variant header line and the EXTRA_JOBS tail', () => {
const hosted = crontabText('hosted').split('\n')
const selfHosted = crontabText('self-hosted').split('\n')
const shared = Math.min(hosted.length, selfHosted.length)
const differing = hosted.slice(0, shared).filter((line, i) => line !== selfHosted[i])
// Any real divergence in the shared prefix must come from
// SCHEDULE_OVERRIDES, which is empty today. If that changes, widen this
// expectation deliberately.
expect(differing).toEqual([expect.stringContaining('# Variant: hosted')])
// The self-hosted file may only be longer by its EXTRA_JOBS block: one
// blank line, one comment line, one line per extra job.
const extraLines = EXTRA_JOBS['self-hosted'].length
const hostedExtraLines = EXTRA_JOBS.hosted.length
expect(selfHosted.length - hosted.length).toBe(
(extraLines > 0 ? extraLines + 2 : 0) - (hostedExtraLines > 0 ? hostedExtraLines + 2 : 0),
)
})
})
describe('EXTRA_JOBS', () => {
it('names real cron routes that vercel.json does not schedule, each with a reason', () => {
const scheduled = new Set(crons.map((c) => c.path))
for (const variant of VARIANTS) {
for (const job of EXTRA_JOBS[variant]) {
expect(job.reason.trim().length, `${job.path} needs a reason`).toBeGreaterThan(0)
expect(job.schedule.trim().split(/\s+/).length, `${job.path} needs a 5-field schedule`).toBe(5)
expect(scheduled.has(job.path), `${job.path} is now in vercel.json: drop the extra entry`).toBe(false)
expect(
existsSync(join(ROOT, 'app', ...job.path.split('/').filter(Boolean), 'route.ts')),
`${job.path} has no route.ts`,
).toBe(true)
}
}
})
})
describe('exclusion and override tables', () => {
it('lists no path that vercel.json no longer schedules', () => {
const known = new Set(crons.map((c) => c.path))
for (const path of Object.keys(EXCLUDED_PATHS)) {
expect(known.has(path), `EXCLUDED_PATHS entry ${path} is stale`).toBe(true)
}
for (const variant of VARIANTS) {
for (const path of Object.keys(SCHEDULE_OVERRIDES[variant])) {
expect(known.has(path), `SCHEDULE_OVERRIDES.${variant} entry ${path} is stale`).toBe(true)
}
}
})
it('states a reason for every exclusion', () => {
for (const [path, reason] of Object.entries(EXCLUDED_PATHS)) {
expect(reason.trim().length, `exclusion ${path} needs a stated reason`).toBeGreaterThan(0)
}
})
it('honours an exclusion and a per-variant override', () => {
const sample: VercelCron[] = [
{ path: '/api/keep/cron', schedule: '0 1 * * *' },
{ path: '/api/drop/cron', schedule: '0 2 * * *' },
]
const rendered = buildCrontab(sample, 'self-hosted', {
excluded: { '/api/drop/cron': 'vercel-only, cannot work self-hosted' },
overrides: { hosted: {}, 'self-hosted': { '/api/keep/cron': '*/30 * * * *' } },
extraJobs: { hosted: [], 'self-hosted': [] },
})
const jobs = parseCrontab(rendered)
expect(jobs).toEqual([{ path: '/api/keep/cron', schedule: '*/30 * * * *' }])
expect(rendered).not.toContain('/api/drop/cron')
})
it('renders extra jobs after the vercel.json jobs under their own comment line', () => {
const rendered = buildCrontab([{ path: '/api/keep/cron', schedule: '0 1 * * *' }], 'self-hosted', {
excluded: {},
overrides: { hosted: {}, 'self-hosted': {} },
extraJobs: {
hosted: [],
'self-hosted': [{ path: '/api/only-here/cron', schedule: '17 * * * *', reason: 'test' }],
},
})
expect(parseCrontab(rendered)).toEqual([
{ path: '/api/keep/cron', schedule: '0 1 * * *' },
{ path: '/api/only-here/cron', schedule: '17 * * * *' },
])
expect(rendered).toContain('# self-hosted-only jobs, not in vercel.json')
// hosted gets no tail at all when it has no extra jobs
const hosted = buildCrontab([{ path: '/api/keep/cron', schedule: '0 1 * * *' }], 'hosted', {
excluded: {},
overrides: { hosted: {}, 'self-hosted': {} },
extraJobs: { hosted: [], 'self-hosted': [] },
})
expect(hosted).not.toContain('not in vercel.json')
})
it('renders the curl invocation with unexpanded shell variables', () => {
const rendered = buildCrontab([{ path: '/api/x/cron', schedule: '0 1 * * *' }], 'hosted', {
excluded: {},
overrides: { hosted: {}, 'self-hosted': {} },
})
expect(rendered).toContain(
'curl -sf -H "Authorization: Bearer ${CRON_SECRET}" ${APP_URL}/api/x/cron',
)
})
})
describe('readVercelCrons', () => {
it('rejects a schedule that is not five fields', () => {
expect(() =>
readVercelCrons(JSON.stringify({ crons: [{ path: '/api/x/cron', schedule: '0 3 * *' }] })),
).toThrow(/5-field/)
})
it('rejects a path outside /api/', () => {
expect(() =>
readVercelCrons(JSON.stringify({ crons: [{ path: 'api/x/cron', schedule: '0 3 * * *' }] })),
).toThrow(/must start with \/api\//)
})
it('rejects a duplicate path', () => {
expect(() =>
readVercelCrons(
JSON.stringify({
crons: [
{ path: '/api/x/cron', schedule: '0 3 * * *' },
{ path: '/api/x/cron', schedule: '0 4 * * *' },
],
}),
),
).toThrow(/duplicate/)
})
})
/**
* Ratchet: a cron route that nothing schedules is dead code that looks alive.
* Every app/api/**\/cron route must either be in vercel.json (and therefore in
* both crontabs) or be named here with a reason.
*/
const INTENTIONALLY_UNSCHEDULED: Readonly<Record<string, string>> = {
'/api/invoices/reminders/cron':
'Feature switched off: the route is a tombstone that logs and returns 503 { disabled: true }.',
'/api/extensions/push-notifications/cron':
'Pre-existing gap: the docstring says daily 09:00 UTC but no schedule exists anywhere. Needs a vercel.json entry, which is outside the crontab-parity change.',
'/api/extensions/stripe/sync/cron':
'Pre-existing gap: the docstring says every 15 minutes but no schedule exists anywhere. Needs a vercel.json entry, which is outside the crontab-parity change.',
}
function findCronRoutes(dir: string, urlPrefix: string): string[] {
const found: string[] = []
for (const entry of readdirSync(dir, { withFileTypes: true })) {
if (!entry.isDirectory()) continue
// Dynamic segments cannot be scheduled: a cron needs a concrete URL.
if (entry.name.startsWith('[')) continue
const urlPath = `${urlPrefix}/${entry.name}`
if (entry.name === 'cron' && existsSync(join(dir, entry.name, 'route.ts'))) {
found.push(urlPath)
}
found.push(...findCronRoutes(join(dir, entry.name), urlPath))
}
return found
}
describe('every cron route has a schedule', () => {
it('leaves no unscheduled cron route undocumented', () => {
const routes = findCronRoutes(join(ROOT, 'app', 'api'), '/api')
const scheduled = new Set([
...crons.map((c) => c.path),
...VARIANTS.flatMap((variant) => EXTRA_JOBS[variant].map((job) => job.path)),
])
const orphans = routes.filter((r) => !scheduled.has(r) && !(r in INTENTIONALLY_UNSCHEDULED))
expect(
orphans,
'These cron routes are scheduled nowhere. Add them to vercel.json (then run ' +
'`npm run crontabs:generate`), or list them in INTENTIONALLY_UNSCHEDULED with a reason.',
).toEqual([])
})
it('lists no route in INTENTIONALLY_UNSCHEDULED that has since been scheduled or deleted', () => {
const routes = new Set(findCronRoutes(join(ROOT, 'app', 'api'), '/api'))
const scheduled = new Set(crons.map((c) => c.path))
for (const [path, reason] of Object.entries(INTENTIONALLY_UNSCHEDULED)) {
expect(reason.trim().length, `${path} needs a reason`).toBeGreaterThan(0)
expect(routes.has(path), `${path} no longer exists: drop the entry`).toBe(true)
expect(scheduled.has(path), `${path} is now in vercel.json: drop the entry`).toBe(false)
}
})
})
@@ -0,0 +1,85 @@
import { describe, it, expect } from 'vitest'
import { buildMigrationSql } from '../generate-skill-bodies'
import type { DiscoveredAtom } from '../lib/atom-discovery'
function atom(overrides: Partial<DiscoveredAtom>): DiscoveredAtom {
return {
id: 'horizontal/test',
tier: 'horizontal',
slug: 'test',
title: 'Test',
description: 'desc',
sni_prefixes: [],
trigger_signals: {},
estimated_tokens: 1,
body_path: '.claude/skills/test/SKILL.md',
body: 'body',
parent_atom_id: null,
frontmatter_version: 1,
schema_version: 1,
...overrides,
}
}
describe('buildMigrationSql', () => {
it('dollar-quotes bodies with backticks, $$, quotes, and the default tag: round-trips intact', () => {
const tricky = 'Body with `code`, $$dollars$$, \'single\' and "double" quotes, and a $gb$ tag.'
const sql = buildMigrationSql([atom({ body: tricky, description: tricky })], { 'horizontal/test': 1 })
// The content survives verbatim inside the SQL...
expect(sql).toContain(tricky)
// ...because the tag escalated past the embedded $gb$.
expect(sql).toMatch(/\$gb0\$/)
expect(sql).toContain('ON CONFLICT (id) DO UPDATE')
expect(sql).toContain("NOTIFY pgrst, 'reload schema'")
})
it('emits a text[] literal for sni_prefixes and jsonb for trigger_signals', () => {
const sql = buildMigrationSql(
[atom({ sni_prefixes: ['62.01', '62.02'], trigger_signals: { foo: 'bar' } })],
{ 'horizontal/test': 1 },
)
expect(sql).toContain("ARRAY['62.01', '62.02']::text[]")
expect(sql).toContain('::jsonb')
expect(sql).toContain('{"foo":"bar"}')
})
it('escapes single quotes in short text fields (title/id)', () => {
const sql = buildMigrationSql([atom({ title: "O'Brien & Co" })], { 'horizontal/test': 1 })
expect(sql).toContain("'O''Brien & Co'")
})
it('emits parent_atom_id: NULL for top-level skills, a quoted id for reference children', () => {
const sql = buildMigrationSql(
[
atom({ id: 'horizontal/swedish-vat', parent_atom_id: null }),
atom({
id: 'horizontal/swedish-vat/vat-compliance-reference',
parent_atom_id: 'horizontal/swedish-vat',
}),
],
{
'horizontal/swedish-vat': 1,
'horizontal/swedish-vat/vat-compliance-reference': 1,
},
)
// Column wired into both the INSERT list and the conflict update.
expect(sql).toContain(', body, parent_atom_id, version,')
expect(sql).toContain('parent_atom_id = EXCLUDED.parent_atom_id')
// Parent row carries a literal NULL; child row carries the parent id.
expect(sql).toMatch(/\$gb\$body\$gb\$,\n {4}NULL,/)
expect(sql).toContain("'horizontal/swedish-vat',\n 1,")
})
it('omits is_active / mcp_exposed from the upsert so manual flips survive re-seed', () => {
const sql = buildMigrationSql([atom({})], { 'horizontal/test': 1 })
// The INSERT column list takes column defaults for these on first insert...
const columnList = sql.match(/INSERT INTO public\.agent_atom_registry\s*\n\s*\(([^)]*)\)/)?.[1] ?? ''
expect(columnList).not.toContain('mcp_exposed')
expect(columnList).not.toContain('is_active')
// ...and the DO UPDATE SET must not overwrite them on conflict.
const updateClause = sql.slice(sql.indexOf('DO UPDATE SET'))
expect(updateClause).not.toContain('mcp_exposed')
expect(updateClause).not.toContain('is_active')
})
})
@@ -0,0 +1,40 @@
import { describe, it, expect } from 'vitest'
import { parseLine } from '../import-tax-tables'
// Build a fixed-width SKV 434 record: prefix(5) income_from(7) income_to(7)
// then six 5-char columns = 49 chars.
function record(prefix: string, from: string, to: string, cols: string[] = ['100', '200', '300', '400', '500', '600']): string {
return prefix + from.padStart(7) + to.padStart(7) + cols.map((c) => c.padStart(5)).join('')
}
describe('parseLine', () => {
it('parses a monthly B-row', () => {
const parsed = parseLine(record('30B29', '20001', '20200'))
expect(parsed).toEqual({
table: 29,
row: [20001, 20200, 100, 200, 300, 400, 500, 600, 0],
})
})
it('parses a monthly percent row with a blank open-ended upper bound', () => {
const parsed = parseLine(record('30%29', '80001', '', ['32', '30', '32', '28', '33', '34']))
expect(parsed).toEqual({
table: 29,
row: [80001, 0, 32, 30, 32, 28, 33, 34, 1],
})
})
it('throws loudly on a two-week 14B row instead of merging it into monthly data', () => {
expect(() => parseLine(record('14B29', '20001', '20200'))).toThrow(/Two-week table row/)
expect(() => parseLine(record('14%29', '80001', ''))).toThrow(/Two-week table row/)
})
it('skips rows with an unknown day-count prefix', () => {
expect(parseLine(record('90B29', '20001', '20200'))).toBeNull()
})
it('skips non-table lines (headers, short lines)', () => {
expect(parseLine('RUBRIK ALLMANNA TABELLER 2026')).toBeNull()
expect(parseLine('')).toBeNull()
})
})
+123
View File
@@ -0,0 +1,123 @@
import { describe, it, expect } from 'vitest'
import {
extractRecord,
parseArgs,
percentile,
renderMarkdown,
summarize,
} from '../perf/log-percentiles'
const opLine = (operation: string, durationMs: number, authMs = 2) =>
JSON.stringify({
level: 'info',
module: `api/${operation}`,
msg: 'op completed',
operation,
durationMs,
authMs,
})
describe('extractRecord', () => {
it('parses a bare logger line', () => {
expect(extractRecord(opLine('period.list', 77))).toMatchObject({
operation: 'period.list',
durationMs: 77,
})
})
it('unwraps the JSON embedded in a vercel logs --json envelope', () => {
const envelope = JSON.stringify({
timestamp: 1,
source: 'serverless',
requestPath: '/api/bookkeeping/fiscal-periods',
message: ` ${opLine('period.list', 77)}`,
})
expect(extractRecord(envelope)).toMatchObject({
source: 'serverless',
operation: 'period.list',
durationMs: 77,
})
})
it('keeps a non-JSON message as the envelope only', () => {
expect(extractRecord(JSON.stringify({ message: 'plain text' }))).toEqual({
message: 'plain text',
})
})
it('ignores blank and unparseable lines', () => {
expect(extractRecord('')).toBeNull()
expect(extractRecord('not json')).toBeNull()
expect(extractRecord('{broken')).toBeNull()
})
})
describe('percentile', () => {
it('uses nearest rank', () => {
const sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
expect(percentile(sorted, 50)).toBe(5)
expect(percentile(sorted, 90)).toBe(9)
expect(percentile(sorted, 99)).toBe(10)
expect(percentile([42], 50)).toBe(42)
expect(Number.isNaN(percentile([], 50))).toBe(true)
})
})
describe('summarize', () => {
const records = [
{ operation: 'a', durationMs: 10, authMs: 1 },
{ operation: 'a', durationMs: 30, authMs: 1 },
{ operation: 'b', durationMs: 100, authMs: 50 },
{ operation: 'b', durationMs: 'oops', authMs: 5 },
]
it('groups by the requested keys and ranks slowest first', () => {
const rows = summarize(records, { fields: ['durationMs', 'authMs'], groupBy: ['operation'] })
expect(rows.map((r) => r.group)).toEqual(['b', 'a'])
expect(rows[1].count).toBe(2)
expect(rows[1].fields.durationMs).toEqual({ p50: 10, p90: 30, p99: 30, max: 30 })
// The non-numeric durationMs is skipped for that field but the row still counts the sample.
expect(rows[0].count).toBe(2)
expect(rows[0].fields.durationMs.max).toBe(100)
})
it('applies exact-match filters and min-count', () => {
const rows = summarize(records, {
fields: ['durationMs'],
groupBy: ['operation'],
filters: [{ key: 'operation', value: 'a' }],
})
expect(rows).toHaveLength(1)
expect(rows[0].group).toBe('a')
expect(summarize(records, { fields: ['durationMs'], groupBy: ['operation'], minCount: 3 })).toEqual([])
})
it('produces a single "all" row without grouping', () => {
const rows = summarize(records, { fields: ['durationMs'] })
expect(rows).toHaveLength(1)
expect(rows[0].group).toBe('all')
expect(rows[0].count).toBe(4)
})
})
describe('renderMarkdown + parseArgs', () => {
it('renders a markdown table with one column set per field', () => {
const rows = summarize([{ k: 'x', v: 5 }], { fields: ['v'], groupBy: ['k'] })
const md = renderMarkdown(rows, ['v'])
expect(md.split('\n')[0]).toBe('| group | n | v p50 | v p90 | v p99 | v max |')
expect(md).toContain('| x | 1 | 5 | 5 | 5 | 5 |')
})
it('parses the documented CLI flags', () => {
expect(
parseArgs(['--field', 'a,b', '--group', 'kind,route', '--filter', 'msg=op completed', '--min-count', '5']),
).toEqual({
fields: ['a', 'b'],
groupBy: ['kind', 'route'],
filters: [{ key: 'msg', value: 'op completed' }],
minCount: 5,
})
expect(() => parseArgs([])).toThrow('--field is required')
expect(() => parseArgs(['--bogus'])).toThrow('unknown option')
})
})
+337
View File
@@ -0,0 +1,337 @@
/**
* Deterministic generator for the installable `accounted-api` agent skill
* (skills/accounted-api/), the consumer-side skill that teaches coding
* agents to build against the v1 REST API.
*
* Renders straight from the same endpoint registry that serves the API and
* its OpenAPI spec, so the skill cannot drift from the server. Hand-authored
* knowledge (auth, conventions, domain gotchas) lives in
* scripts/api-skill/overlays/*.md and is stitched in verbatim.
*
* Run with the react-server condition so the `server-only` guards in the
* route import chain resolve (see package.json):
*
* npm run apiskill:generate # write skills/accounted-api/
* npm run apiskill:check # CI staleness gate (no writes)
*
* The per-operation rendering is done by the portable inventory tool that
* ships inside the sibling `openapi-to-skill` skill: the generic tool has to
* be good enough to build our own skill, or it is not good enough to ship.
*/
import { readFileSync, writeFileSync, mkdirSync, readdirSync, rmSync, existsSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import '@/lib/api/v1/load-routes'
import { generateOpenApiSpec } from '@/lib/api/v1/registry'
import { API_V1_VERSION } from '@/lib/api/v1/version'
import {
listOperations,
formatOpLine,
renderOperationMd,
type OperationEntry,
} from '../../skills/openapi-to-skill/scripts/openapi-inventory.mjs'
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..', '..')
const OVERLAY_DIR = join(ROOT, 'scripts', 'api-skill', 'overlays')
const OUT_DIR = join(ROOT, 'skills', 'accounted-api')
/** Base URL shown in examples: the permanent machine host (see lib/api/v1/base-url.ts). */
const MACHINE_BASE = 'https://app.gnubok.se'
/**
* Reference-file layout: every derived path group must be claimed by exactly
* one file. A new v1 resource fails apiskill:check until it is mapped here,
* which is the point: grouping is an editorial decision, not a default.
*/
const GROUPS: Array<{ file: string; title: string; members: string[]; blurb: string }> = [
{
file: 'core.md',
title: 'Core',
members: ['health', 'companies', 'operations', 'settings'],
blurb:
'Connectivity, company discovery, async-operation polling, and company settings. ' +
'Every session starts with GET /companies to resolve the companyId that all other URLs need.',
},
{
file: 'journal-entries.md',
title: 'Journal entries',
members: ['journal-entries', 'voucher-gap-explanations'],
blurb:
'The ledger itself: journal entries follow draft -> commit -> immutable. There is no ' +
'edit or delete after commit; undo via reverse (storno) or correct. Voucher numbers are ' +
'server-assigned and gapless; explain unavoidable gaps via voucher-gap-explanations.',
},
{
file: 'periods.md',
title: 'Periods and registers',
members: ['fiscal-periods', 'accounts', 'compliance', 'dimensions', 'skatteverket'],
blurb:
'Fiscal periods and their lock/close/year-end lifecycle (async operations), the BAS ' +
'chart of accounts, cost-center/project dimensions, the compliance pre-flight check, ' +
'and reading filed VAT declarations (and beslut) from Skatteverket.',
},
{
file: 'invoices.md',
title: 'Invoices (AR)',
members: ['invoices'],
blurb:
'Accounts receivable invoices: draft -> send -> paid/credited; the F-series number is ' +
'assigned at send, not create. Supplier bills you receive are a different resource: see ' +
'suppliers.md. Customer and article registers: customers.md.',
},
{
file: 'customers.md',
title: 'Customers and articles',
members: ['customers', 'articles'],
blurb:
'The customer register (bulk-create supported, archive via DELETE) and the read-only ' +
'article register used for invoice line linkage.',
},
{
file: 'suppliers.md',
title: 'Suppliers (AP)',
members: ['suppliers', 'supplier-invoices'],
blurb:
'Accounts payable: supplier register and received supplier invoices ' +
'(register -> approve -> mark-paid, or credit).',
},
{
file: 'documents.md',
title: 'Documents',
members: ['documents', 'inbox-items'],
blurb:
'The WORM document archive (7-year legal retention: uploads are permanent) and inbox-item stamping. ' +
'Link every uploaded receipt/invoice document to its journal entry.',
},
{
file: 'banking.md',
title: 'Banking',
members: ['transactions', 'cash-accounts', 'bank-connections', 'reconciliation', 'imports'],
blurb:
'Bank transactions (ingest, categorize, match against invoices), cash accounts with the ' +
'bank-reported balance, PSD2 connection health (sync freshness, consent expiry), ' +
'bank reconciliation runs, and file imports (SIE, bank statements).',
},
{
file: 'employees.md',
title: 'Employees',
members: ['employees', 'salary'],
blurb:
'The employee register plus absence (frånvaro), vacation balances and year close, and ' +
'payroll cutover opening balances. Running payroll itself: salary-runs.md.',
},
{
file: 'salary-runs.md',
title: 'Salary runs',
members: ['salary-runs'],
blurb:
'Swedish payroll runs: create -> calculate -> approve -> book/mark-paid -> generate-agi ' +
'(arbetsgivardeklaration), with per-employee payslips and draft-only line edits.',
},
{
file: 'reports.md',
title: 'Reports',
members: ['reports'],
blurb:
'Read-only statutory and management reports: trial balance, balance sheet, income statement, ' +
'general ledger, VAT declaration, AR/AP ledgers, salary journal, and SIE export.',
},
{
file: 'webhooks.md',
title: 'Webhooks',
members: ['webhooks', 'webhook-deliveries'],
blurb:
'HMAC-signed event subscriptions with delivery logs, test pings, retries, and secret rotation.',
},
]
/**
* The standard error line every operation shares; lifted into the
* conventions overlay once instead of repeated 124 times. Operations with
* additional codes keep their (then non-matching) line.
*/
const STANDARD_ERRORS_LINE =
'Errors: `400` (Validation error), `401` (Unauthorized), `403` (Insufficient scope), ' +
'`404` (Not found), `429` (Rate limited), `500` (Internal error)'
const GENERATED_NOTE =
'<!-- GENERATED FILE, do not edit. Source: lib/api/v1 registry + scripts/api-skill/overlays. ' +
'Regenerate with `npm run apiskill:generate`. -->'
function overlay(name: string): string {
return readFileSync(join(OVERLAY_DIR, name), 'utf8').trim()
}
function methodRank(method: string): number {
return ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].indexOf(method)
}
function buildFiles(): Map<string, string> {
const spec = generateOpenApiSpec(MACHINE_BASE)
const ops = listOperations(spec)
const memberToFile = new Map<string, (typeof GROUPS)[number]>()
for (const group of GROUPS) {
for (const member of group.members) {
if (memberToFile.has(member)) throw new Error(`group member mapped twice: ${member}`)
memberToFile.set(member, group)
}
}
const byFile = new Map<string, OperationEntry[]>()
for (const entry of ops) {
const group = memberToFile.get(entry.group)
if (!group) {
throw new Error(
`Unmapped endpoint group "${entry.group}" (${entry.method} ${entry.path}). ` +
'Add it to a reference file in scripts/api-skill/generate.ts GROUPS.',
)
}
if (!byFile.has(group.file)) byFile.set(group.file, [])
byFile.get(group.file)!.push(entry)
}
for (const list of byFile.values()) {
list.sort((a, b) => a.path.localeCompare(b.path) || methodRank(a.method) - methodRank(b.method))
}
const files = new Map<string, string>()
// Reference files.
for (const group of GROUPS) {
const members = byFile.get(group.file) ?? []
if (members.length === 0) throw new Error(`reference file with no operations: ${group.file}`)
const blocks = members.map((entry) =>
renderOperationMd(spec, entry).replace(STANDARD_ERRORS_LINE, '').trimEnd(),
)
files.set(
join('references', group.file),
[
GENERATED_NOTE,
'',
`# ${group.title} endpoints`,
'',
group.blurb,
'',
'Conventions (auth, envelope, pagination, dry-run, idempotency, standard errors)',
'are in SKILL.md and are not repeated per endpoint.',
'',
blocks.join('\n\n---\n\n'),
'',
].join('\n'),
)
}
// SKILL.md: frontmatter + overlays + endpoint index.
const indexSections = GROUPS.map((group) => {
const members = byFile.get(group.file)!
const lines = members.map((entry) =>
formatOpLine(entry).replace(` ${entry.path} `, ` ${entry.path.replace('/api/v1', '')} `),
)
return [
`### ${group.title} (${members.length})`,
'',
`Full detail: [references/${group.file}](references/${group.file})`,
'',
'```text',
...lines,
'```',
].join('\n')
})
const frontmatter = [
'---',
'name: accounted-api',
'description: >-',
' Consume the Accounted REST API (Swedish double-entry bookkeeping SaaS,',
` ${MACHINE_BASE}/api/v1). Use when building an integration, app, backend`,
' job, or agent tool layer against Accounted: invoices, customers,',
' suppliers, supplier invoices, journal entries (bokföring), bank',
' transactions and reconciliation, payroll (lön), VAT/moms and financial',
' reports, SIE import/export, documents, webhooks. Covers auth with',
` gnubok_sk_ API keys, conventions (dry-run, idempotency, cursor`,
` pagination, scopes), and all ${ops.length} endpoints.`,
'---',
].join('\n')
files.set(
'SKILL.md',
[
frontmatter,
'',
GENERATED_NOTE,
'',
overlay('intro.md'),
'',
overlay('quickstart.md'),
'',
overlay('conventions.md'),
'',
'## Endpoint index',
'',
`API version \`${API_V1_VERSION}\`, ${ops.length} operations. Paths are shown without`,
`their \`/api/v1\` prefix (full base URL: \`${MACHINE_BASE}/api/v1\`).`,
'',
indexSections.join('\n\n'),
'',
overlay('gotchas.md'),
'',
overlay('verification.md'),
'',
].join('\n'),
)
return files
}
function listMarkdownFiles(dir: string, prefix = ''): string[] {
if (!existsSync(dir)) return []
const out: string[] = []
for (const dirent of readdirSync(dir, { withFileTypes: true })) {
const rel = prefix ? join(prefix, dirent.name) : dirent.name
if (dirent.isDirectory()) out.push(...listMarkdownFiles(join(dir, dirent.name), rel))
else if (dirent.name.endsWith('.md')) out.push(rel)
}
return out
}
function main() {
const check = process.argv.includes('--check')
const files = buildFiles()
const existing = listMarkdownFiles(OUT_DIR)
const orphans = existing.filter((rel) => !files.has(rel))
if (check) {
const stale: string[] = []
for (const [rel, content] of files) {
const abs = join(OUT_DIR, rel)
if (!existsSync(abs)) stale.push(`${rel} (missing)`)
else if (readFileSync(abs, 'utf8') !== content) stale.push(`${rel} (outdated)`)
}
stale.push(...orphans.map((rel) => `${rel} (orphaned)`))
if (stale.length > 0) {
console.error('skills/accounted-api is stale relative to the endpoint registry/overlays:')
for (const entry of stale) console.error(` - ${entry}`)
console.error('Run `npm run apiskill:generate` and commit the result.')
process.exit(1)
}
console.log(`skills/accounted-api is up to date (${files.size} files).`)
return
}
for (const [rel, content] of files) {
const abs = join(OUT_DIR, rel)
mkdirSync(dirname(abs), { recursive: true })
writeFileSync(abs, content)
}
for (const rel of orphans) {
rmSync(join(OUT_DIR, rel))
console.log(`removed orphan: ${rel}`)
}
console.log(`wrote ${files.size} files to skills/accounted-api/`)
}
main()
+79
View File
@@ -0,0 +1,79 @@
## Conventions
These rules hold across the whole surface; endpoint entries below do not
repeat them.
**Response envelope.** Success: `{ "data": ..., "meta": { "request_id",
"api_version", "next_cursor"?, "audit"?, "partial_expansions"? } }`. Errors
replace `data` with `error` (no `meta`; `request_id` moves inside `error`).
**Errors.** Stable machine codes with agent-oriented remediation:
```json
{ "error": { "code": "PERIOD_LOCKED", "message": "Svenska", "message_en": "English",
"details": {}, "recovery_hint": "next step", "docs_url": "...",
"valid_alternatives": {}, "request_id": "req_..." } }
```
React to `code`, read `message_en` and `recovery_hint`, follow
`valid_alternatives` when present (e.g. `next_open_period`). Standard codes on
every endpoint: `400` validation, `401` bad key, `403` missing scope, `404`,
`429` rate limited (honor `Retry-After`), `500`. Full catalogue:
https://app.gnubok.se/docs/api/errors. Only endpoint-specific codes are
mentioned per endpoint below.
**Cursor pagination.** List endpoints take `?cursor=` and return
`meta.next_cursor`; loop until it is absent/null. A stale or tampered cursor
is NOT an error: the first page is returned again, so terminate on
`next_cursor`, never on "page looks familiar".
**Dry-run on every write that supports it** (`dry-run` badge in the index).
Send `?dry_run=true` (or `X-Dry-Run: true`): the response is always `200` with
`data.dry_run: true` plus a preview (would-be record, journal lines, voucher
number) and the `X-Dry-Run: true` response header; nothing is committed.
Commit by re-issuing without the flag and with the SAME `Idempotency-Key`
(the dry run is not cached against the key). Preview first on any financial
write; it is free.
**Idempotency-Key.** Send a fresh UUID header on every POST/PATCH/DELETE;
several endpoints reject writes without one (`400`). Replaying the same
key+body returns the original response with the `Idempotent-Replayed: true`
header; the same key with a different body returns `409 IDEMPOTENCY_KEY_REUSE`
(24h window). Safe retry loop: keep the key, keep the body.
**Test keys are simulation-only.** With a `gnubok_sk_test_*` key, reads
return real company data (responses carry `X-Gnubok-Mode: test`) and every
write is forced into dry-run; writes that cannot be simulated return
`403 TEST_KEY_WRITE_BLOCKED`. Nothing a test key does ever persists: it is
`?dry_run=true` baked into the credential. Full end-to-end write tests
therefore need a live key against a company you own.
**Atomic writes.** A mutation either commits fully or errors with no side
effects. There is no partial state to clean up after an error response
(`bulk-create` endpoints that do partial success say so explicitly).
**Audit inline.** Successful financial writes include `meta.audit` (voucher
number, audit-trail URL, immutability timestamp). No follow-up read needed to
confirm what was booked.
**Expansion.** Some list/detail endpoints take `?expand=a,b` (documented per
endpoint). If an expansion fails the response still succeeds and names the
failed parts in `meta.partial_expansions`; check it before trusting expanded
fields.
**Async operations.** Long-running actions (fiscal-period lock/close/year-end,
imports) return `202` with an operation id; poll `GET /api/v1/operations/{id}`
until `status` is `succeeded`/`failed`. The response shape is identical
whether the work ran inline or queued.
**Versioning.** Dated versions (current: see `meta.api_version`). Responses
carry the `Gnubok-Version` header; request pinning via a `Gnubok-Version`
request header is reserved for a future breaking change and is not read
today. Additive changes ship without a version bump; see
https://app.gnubok.se/docs/api/versioning.
**Index badges.** Every operation line below carries machine-readable
annotations from the spec: `scope:` (required key scope), `risk:` (low/medium/
high; confirm with a human before unprompted high-risk calls), `idempotent`
(safe to retry), `dry-run` (previewable), `reversible` (a single follow-up
call can undo it, e.g. invoice credit).
+38
View File
@@ -0,0 +1,38 @@
## Gotchas (Swedish accounting domain)
Rules a generic REST integration will violate unless told:
- **Account numbers are strings, not numbers.** BAS accounts (`"1930"`,
`"3001"`) are identifiers; send them as JSON strings. Arithmetic on them,
zero-stripping, or number coercion corrupts postings.
- **Posted journal entries are immutable by law** (Bokföringslagen). There is
no PATCH or DELETE on a committed entry, ever. Undo with
`POST .../journal-entries/{id}/reverse` (storno), fix with
`POST .../journal-entries/{id}/correct`. Design flows around
reverse-and-repost, not edit-in-place.
- **Voucher numbers are gapless and server-assigned.** Never assume or
pre-allocate one; read it from `meta.audit.voucher_number` after commit. A
legally required gap explanation goes through
`POST .../voucher-gap-explanations`.
- **Every entry balances.** `sum(debit) === sum(credit)` to the öre, amounts
are decimal SEK numbers (max 2 decimals). Do rounding with
round-half-away-from-zero on öre; never float-accumulate line totals
client-side and "fix" the difference on a random line.
- **Period locks are a feature, not an error to retry.** Writes into a
locked/closed period return `PERIOD_LOCKED` (with `valid_alternatives`
pointing at open periods). Retrying the same request cannot succeed; either
target an open period or surface the lock to the user.
- **Drafts vs posted.** Invoices are created as drafts with
`invoice_number: null`; the F-series number is assigned atomically on send.
Journal entries follow draft -> commit. Nothing financial exists in the
ledger until the commit/send action.
- **Two invoice worlds.** `invoices` = accounts receivable (you bill
customers); `supplier-invoices` = accounts payable (you receive bills).
They are different resources with different lifecycles.
- **Swedish user-facing text.** `error.message` is Swedish by design; show it
to Swedish end users, and use `message_en` for your own logs/logic. Domain
terms in responses (moms, verifikat, kostnadsställe) are not translatable
labels but legal concepts.
- **Compliance pre-flight.** Before building your own validation for Swedish
rules, call `GET .../compliance/check`: it runs the server's own rule set
(VAT plausibility, sequence integrity, period status) and returns findings.
+17
View File
@@ -0,0 +1,17 @@
# Accounted API integration
Accounted is Swedish double-entry bookkeeping (bokföring) as a service: BAS
chart of accounts, verifikationer with legally immutable audit trails, VAT
(moms), payroll (lön), invoicing, bank reconciliation, and statutory reports,
exposed as a REST API designed for agents and integrations first.
**This skill is for building software against the REST API** (an app, a
backend job, an agent tool layer). If the goal is to *operate* a ledger
conversationally (book receipts, run month close), use the Accounted MCP
connector and its workflow skills instead: install the `accounted` plugin or
see https://app.gnubok.se/docs/api/connect-claude.
If you have used Stripe's API the shape will feel familiar: bearer keys, dated
versions, idempotency keys, webhook signatures, cursor pagination. The domain
rules are Swedish accounting law; the Gotchas section below stops the classic
violations before you ship them.
+28
View File
@@ -0,0 +1,28 @@
## Auth and base URL
Every request sends a bearer key:
```bash
curl https://app.gnubok.se/api/v1/companies \
-H "Authorization: Bearer gnubok_sk_live_..."
```
- Base URL: `https://app.gnubok.se/api/v1` (legacy machine host, permanent).
`https://app.accounted.se/api/v1` serves the identical API.
- Keys are created in the Accounted dashboard under **Settings -> API**
(`/settings/api`). Two prefixes:
- `gnubok_sk_live_*` commits real writes.
- `gnubok_sk_test_*` reads real company data but forces every write into
dry-run (responses carry `X-Gnubok-Mode: test`). Develop and run evals
with a test key; switch to live last.
- Each key carries **scopes** (`invoices:read`, `invoices:write`,
`payroll:write`, `webhooks:manage`, ...). Every endpoint in the index below
is annotated with its required scope; a missing scope returns `403`.
- Rate limit: 100 requests/minute per key. On `429`, honor `Retry-After`.
- URLs carry the company id explicitly
(`/api/v1/companies/{companyId}/invoices`). A key can act on any company its
user is a member of; start every session with `GET /api/v1/companies` to
discover ids. There is no implicit "current company".
First calls, in order: `GET /api/v1/health` (no auth, connectivity), then
`GET /api/v1/companies` (auth works, discover `companyId`).
@@ -0,0 +1,18 @@
## Verification
This skill is generated (`npm run apiskill:generate` in the Accounted repo)
from the same endpoint registry that serves the live API, its OpenAPI spec
(`https://app.gnubok.se/api/v1/openapi.json`), and its runtime request
validators, so schema drift between this text and the server cannot occur for
a matching `api_version`. CI regenerates and diffs it on every change.
Before first use in a new environment, smoke-test:
```bash
curl -s https://app.gnubok.se/api/v1/health
curl -s https://app.gnubok.se/api/v1/companies -H "Authorization: Bearer $ACCOUNTED_API_KEY"
```
If `meta.api_version` in responses is newer than the version in this skill's
index header, refetch the skill (or read the changelog at
https://app.gnubok.se/docs/api/changelog) before relying on endpoint details.
@@ -0,0 +1,198 @@
-- Read-only audit for journal entries whose settlement leg may disagree with
-- the cash account linked to the source bank transaction.
--
-- This query is deliberately diagnostic. A current cash_account_id does not
-- prove that the same link existed when the entry was posted. Every result
-- therefore requires review against the bank feed, the original journal entry,
-- and the transaction history before any correction is staged.
--
-- The query performs no writes, creates no temporary objects, and only returns
-- current posted entries. Reversed originals disappear once their transaction
-- is linked to the posted correction.
with transaction_context as (
select
t.company_id,
t.id as transaction_id,
t.date as transaction_date,
t.amount as transaction_amount,
coalesce(t.currency, 'SEK') as transaction_currency,
t.amount_sek,
t.exchange_rate,
t.cash_account_id,
t.import_source,
t.created_at as transaction_created_at,
t.updated_at as transaction_updated_at,
je.id as journal_entry_id,
je.entry_date,
je.committed_at,
je.voucher_series,
je.voucher_number,
je.fiscal_period_id,
ca.ledger_account as expected_settlement_account,
ca.created_at as cash_account_created_at,
fp.name as fiscal_period_name,
fp.is_closed as fiscal_period_closed,
fp.locked_at as fiscal_period_locked_at,
cs.bookkeeping_locked_through,
round(
abs(
case
when coalesce(t.currency, 'SEK') = 'SEK' then t.amount
when t.amount_sek is not null then t.amount_sek
when t.exchange_rate is not null then t.amount * t.exchange_rate
else null
end
),
2
) as settlement_amount_sek,
exists (
select 1
from public.invoice_payments ip
where ip.transaction_id = t.id
and ip.journal_entry_id = je.id
) as is_customer_invoice_payment,
exists (
select 1
from public.supplier_invoice_payments sip
where sip.transaction_id = t.id
and sip.journal_entry_id = je.id
) as is_supplier_invoice_payment
from public.transactions t
join public.journal_entries je
on je.id = t.journal_entry_id
and je.company_id = t.company_id
and je.status = 'posted'
join public.cash_accounts ca
on ca.id = t.cash_account_id
and ca.company_id = t.company_id
join public.fiscal_periods fp
on fp.id = je.fiscal_period_id
and fp.company_id = t.company_id
left join public.company_settings cs
on cs.company_id = t.company_id
), directional_exact_lines as (
select
tc.*,
jel.id as observed_line_id,
jel.account_number as observed_settlement_account,
jel.debit_amount as observed_debit_amount,
jel.credit_amount as observed_credit_amount,
count(*) over (partition by tc.transaction_id, tc.journal_entry_id) as exact_directional_line_count
from transaction_context tc
join public.journal_entry_lines jel
on jel.journal_entry_id = tc.journal_entry_id
and case
when tc.transaction_amount < 0 then
round(jel.credit_amount, 2) = tc.settlement_amount_sek
and round(jel.debit_amount, 2) = 0
else
round(jel.debit_amount, 2) = tc.settlement_amount_sek
and round(jel.credit_amount, 2) = 0
end
where tc.settlement_amount_sek is not null
), candidates as (
select dl.*
from directional_exact_lines dl
where dl.observed_settlement_account <> dl.expected_settlement_account
and not exists (
select 1
from public.journal_entry_lines expected
where expected.journal_entry_id = dl.journal_entry_id
and expected.account_number = dl.expected_settlement_account
and case
when dl.transaction_amount < 0 then
round(expected.credit_amount, 2) = dl.settlement_amount_sek
and round(expected.debit_amount, 2) = 0
else
round(expected.debit_amount, 2) = dl.settlement_amount_sek
and round(expected.credit_amount, 2) = 0
end
)
)
select
c.company_id,
c.transaction_id,
c.journal_entry_id,
c.observed_line_id,
concat(c.voucher_series, '-', c.voucher_number) as voucher,
c.transaction_date,
c.entry_date,
c.committed_at,
c.transaction_amount,
c.transaction_currency,
c.settlement_amount_sek,
c.expected_settlement_account,
c.observed_settlement_account,
c.observed_debit_amount,
c.observed_credit_amount,
c.exact_directional_line_count,
c.cash_account_id,
c.import_source,
c.transaction_created_at,
c.transaction_updated_at,
c.cash_account_created_at,
c.fiscal_period_id,
c.fiscal_period_name,
c.fiscal_period_closed,
c.fiscal_period_locked_at,
c.bookkeeping_locked_through,
case
when c.fiscal_period_closed then 'closed'
when c.fiscal_period_locked_at is not null then 'period_locked'
when c.bookkeeping_locked_through is not null
and c.entry_date <= c.bookkeeping_locked_through
then 'company_lock_date'
else 'open'
end as effective_lock_status,
(
select jsonb_agg(
jsonb_build_object(
'account_number', original.account_number,
'debit_amount', original.debit_amount,
'credit_amount', original.credit_amount,
'line_description', original.line_description,
'currency', original.currency,
'amount_in_currency', original.amount_in_currency,
'exchange_rate', original.exchange_rate,
'tax_code', original.tax_code,
'dimensions', original.dimensions,
'cost_center', original.cost_center,
'project', original.project
)
order by original.sort_order, original.id
)
from public.journal_entry_lines original
where original.journal_entry_id = c.journal_entry_id
) as original_lines,
case
when c.is_customer_invoice_payment then 'customer_invoice_payment'
when c.is_supplier_invoice_payment then 'supplier_invoice_payment'
else 'categorization_or_manual_link'
end as booking_flow,
case
when c.exact_directional_line_count <> 1 then 'manual_review_multiple_exact_lines'
when c.is_customer_invoice_payment or c.is_supplier_invoice_payment
then 'manual_review_payment_aware_correction_required'
when c.cash_account_created_at > c.committed_at then 'manual_review_cash_account_created_later'
when c.expected_settlement_account <> '1930'
and c.observed_settlement_account = '1930'
and abs(extract(epoch from (c.transaction_updated_at - c.committed_at))) <= 60
then 'high_review_priority_hardcoded_1930_signature'
else 'manual_review_link_history_required'
end as review_priority
from candidates c
order by
case
when c.expected_settlement_account <> '1930'
and c.observed_settlement_account = '1930'
and c.cash_account_created_at <= c.committed_at
and abs(extract(epoch from (c.transaction_updated_at - c.committed_at))) <= 60
then 1
when c.is_customer_invoice_payment or c.is_supplier_invoice_payment then 2
else 3
end,
c.company_id,
c.entry_date,
c.voucher_series,
c.voucher_number;
@@ -0,0 +1,106 @@
-- Read-only evidence-classifier probes for audit-template-misbookings.sql.
-- Expected result: eight rows, each with passed = true.
with probes(
probe,
defect,
source_type,
review_priority,
line_count,
observed_vat_rate,
document_has_12pct_vat,
document_has_25pct_vat,
expected_classification
) as (
values
(
'exact_hotel_template_shape',
'travel_hotel_5820', 'manual',
'high_hotel_counterparty_on_car_hire_account',
3, 0.12::numeric, false, false, 'confirmed_correction'
),
(
'car_hire_with_25pct_vat',
'travel_hotel_5820', 'manual',
'manual_review_5820_no_corroborating_signal',
3, 0.25::numeric, false, false, 'false_positive'
),
(
'hotel_shape_without_hotel_evidence',
'travel_hotel_5820', 'manual',
'medium_12pct_vat_on_car_hire_account',
3, 0.12::numeric, false, false, 'insufficient_evidence'
),
(
'imported_representation_signature',
'representation_25pct_vat', 'import',
'high_vat_is_25pct_of_6072_cost',
3, 0.25::numeric, false, false, 'false_positive'
),
(
'live_representation_without_underlag',
'representation_25pct_vat', 'manual',
'high_vat_is_25pct_of_6072_cost',
3, 0.25::numeric, false, false, 'insufficient_evidence'
),
(
'live_representation_with_25pct_underlag',
'representation_25pct_vat', 'manual',
'high_vat_is_25pct_of_6072_cost',
3, 0.25::numeric, false, true, 'false_positive'
),
(
'live_representation_with_12pct_vat',
'representation_25pct_vat', 'manual',
'manual_review_6072_with_vat',
3, 0.12::numeric, true, false, 'false_positive'
),
(
'mixed_representation_without_underlag',
'representation_25pct_vat', 'manual',
'manual_review_6072_with_vat',
4, 0.18::numeric, false, false, 'insufficient_evidence'
)
),
classified as (
select
probe.*,
case
when source_type in ('import', 'correction') then 'false_positive'
when defect = 'representation_25pct_vat'
and review_priority = 'high_vat_is_25pct_of_6072_cost'
and document_has_25pct_vat
and not document_has_12pct_vat
then 'false_positive'
when defect = 'representation_25pct_vat'
and review_priority = 'high_vat_is_25pct_of_6072_cost'
then 'insufficient_evidence'
when defect = 'representation_25pct_vat'
and observed_vat_rate between 0.119 and 0.121
then 'false_positive'
when defect = 'representation_25pct_vat'
and document_has_12pct_vat
and not document_has_25pct_vat
then 'false_positive'
when defect = 'representation_25pct_vat' then 'insufficient_evidence'
when defect = 'travel_hotel_5820'
and line_count = 3
and observed_vat_rate between 0.119 and 0.121
and review_priority = 'high_hotel_counterparty_on_car_hire_account'
then 'confirmed_correction'
when defect = 'travel_hotel_5820'
and line_count = 3
and observed_vat_rate between 0.119 and 0.121
then 'insufficient_evidence'
else 'false_positive'
end as actual_classification
from probes probe
)
select
probe,
expected_classification,
actual_classification,
actual_classification = expected_classification as passed
from classified
order by probe;
+572
View File
@@ -0,0 +1,572 @@
-- Read-only audit for posted entries that a defective booking template may have
-- mis-classified. Covers the two defects fixed in #1396 and #1397 whose bad
-- postings succeeded and therefore may remain in customers' ledgers.
--
-- Not covered, deliberately: vehicle_parking (5614) and it_cloud_hosting (5421)
-- named accounts that never existed in BAS, so account-backfill could not seed
-- them and every booking through those templates failed. Nothing was posted.
--
-- The query separates template-remediation evidence from broad account
-- signatures. Imported entries and earlier corrections cannot have been
-- generated by either live template in this database. The remaining signatures
-- can still have legitimate shapes: 5820 is correct for actual car hire, and
-- representation can lawfully carry 25% VAT when the supply itself is subject
-- to 25% and the invoice is correct. Review the underlag before acting.
--
-- The query performs no writes, creates no objects, and returns one row per
-- posted journal entry and defect. Companion procedure:
-- docs/TEMPLATE_MISBOOKING_REMEDIATION.md.
with debit_line_groups as (
-- Aggregate before classification so entries with several 6072 or 2641
-- lines are evaluated once. This avoids the previous many-to-many pairing.
select
jel.journal_entry_id,
jel.account_number,
array_agg(jel.id order by jel.sort_order, jel.id) as line_ids,
sum(jel.debit_amount) as debit_amount
from public.journal_entry_lines jel
where jel.account_number in ('5820', '6072', '2641')
and jel.debit_amount > 0
group by jel.journal_entry_id, jel.account_number
),
candidate_entry_ids as (
select distinct journal_entry_id
from debit_line_groups
where account_number in ('5820', '6072')
),
candidate_transactions as (
select
t.journal_entry_id,
t.id as transaction_id,
t.document_id,
concat_ws(' ', nullif(t.merchant_name, ''), nullif(t.description, '')) as transaction_description
from public.transactions t
join candidate_entry_ids candidate on candidate.journal_entry_id = t.journal_entry_id
where t.journal_entry_id is not null
union all
select
tvl.journal_entry_id,
t.id as transaction_id,
t.document_id,
concat_ws(' ', nullif(t.merchant_name, ''), nullif(t.description, '')) as transaction_description
from public.transaction_voucher_links tvl
join candidate_entry_ids candidate on candidate.journal_entry_id = tvl.journal_entry_id
join public.transactions t on t.id = tvl.transaction_id
),
transaction_context as (
select
journal_entry_id,
string_agg(
distinct transaction_description,
' | ' order by transaction_description
) as transaction_description
from candidate_transactions
group by journal_entry_id
),
hotel_candidates as (
-- The Hotell template debited 5820 (Hyrbilskostnader) instead of 5830
-- (Kost och logi) until #1397.
select
'travel_hotel_5820'::text as defect,
je.company_id,
je.id as journal_entry_id,
je.voucher_series,
je.voucher_number,
je.entry_date,
je.committed_at,
je.fiscal_period_id,
je.source_type,
cost.line_ids as cost_line_ids,
coalesce(vat.line_ids, '{}'::uuid[]) as vat_line_ids,
'5820'::text as observed_cost_account,
'5830'::text as expected_cost_account,
case when vat.line_ids is null then null else '2641'::text end as observed_vat_account,
case when vat.line_ids is null then null else '2641'::text end as expected_vat_account,
cost.debit_amount as cost_debit_amount,
vat.debit_amount as vat_debit_amount,
round(vat.debit_amount / nullif(cost.debit_amount, 0), 6) as observed_vat_rate,
0.12::numeric as expected_vat_rate,
tx.transaction_description,
case
when tx.transaction_description ~* '(hotel|hotell|scandic|elite|best western|nordic choice|clarion|quality inn|radisson|booking\.com|airbnb|logi|övernattning)'
then 'high_hotel_counterparty_on_car_hire_account'
when abs(vat.debit_amount - cost.debit_amount * 0.12) < 0.02
then 'medium_12pct_vat_on_car_hire_account'
else 'manual_review_5820_no_corroborating_signal'
end::text as review_priority
from public.journal_entries je
join debit_line_groups cost
on cost.journal_entry_id = je.id and cost.account_number = '5820'
left join debit_line_groups vat
on vat.journal_entry_id = je.id and vat.account_number = '2641'
left join transaction_context tx on tx.journal_entry_id = je.id
where je.status = 'posted'
),
representation_candidates as (
-- The representation template deducted 25% input VAT on what was intended
-- to be a 12% restaurant supply until #1396. Totals are evaluated per entry,
-- never by pairing individual lines.
select
'representation_25pct_vat'::text as defect,
je.company_id,
je.id as journal_entry_id,
je.voucher_series,
je.voucher_number,
je.entry_date,
je.committed_at,
je.fiscal_period_id,
je.source_type,
cost.line_ids as cost_line_ids,
vat.line_ids as vat_line_ids,
'6072'::text as observed_cost_account,
'6072'::text as expected_cost_account,
'2641'::text as observed_vat_account,
'2641'::text as expected_vat_account,
cost.debit_amount as cost_debit_amount,
vat.debit_amount as vat_debit_amount,
round(vat.debit_amount / nullif(cost.debit_amount, 0), 6) as observed_vat_rate,
0.12::numeric as expected_vat_rate,
tx.transaction_description,
case
when abs(vat.debit_amount - cost.debit_amount * 0.25) < 0.02
then 'high_vat_is_25pct_of_6072_cost'
else 'manual_review_6072_with_vat'
end::text as review_priority
from public.journal_entries je
join debit_line_groups cost
on cost.journal_entry_id = je.id and cost.account_number = '6072'
join debit_line_groups vat
on vat.journal_entry_id = je.id and vat.account_number = '2641'
left join transaction_context tx on tx.journal_entry_id = je.id
where je.status = 'posted'
),
all_candidates as (
select
defect,
company_id,
journal_entry_id,
voucher_series,
voucher_number,
entry_date,
committed_at,
fiscal_period_id,
source_type,
cost_line_ids,
vat_line_ids,
observed_cost_account,
expected_cost_account,
observed_vat_account,
expected_vat_account,
cost_debit_amount,
vat_debit_amount,
observed_vat_rate,
expected_vat_rate,
transaction_description,
review_priority
from hotel_candidates
union all
select
defect,
company_id,
journal_entry_id,
voucher_series,
voucher_number,
entry_date,
committed_at,
fiscal_period_id,
source_type,
cost_line_ids,
vat_line_ids,
observed_cost_account,
expected_cost_account,
observed_vat_account,
expected_vat_account,
cost_debit_amount,
vat_debit_amount,
observed_vat_rate,
expected_vat_rate,
transaction_description,
review_priority
from representation_candidates
),
entry_line_snapshots as (
-- Retain every field accepted by gnubok_correct_entry for candidate entries
-- only, without scanning and serializing the entire journal-line table.
select
jel.journal_entry_id,
jsonb_agg(
jsonb_strip_nulls(jsonb_build_object(
'account_number', jel.account_number,
'debit_amount', jel.debit_amount,
'credit_amount', jel.credit_amount,
'line_description', jel.line_description,
'currency', jel.currency,
'amount_in_currency', jel.amount_in_currency,
'exchange_rate', jel.exchange_rate,
'tax_code', jel.tax_code,
'dimensions', jel.dimensions
))
order by jel.sort_order, jel.id
) as original_lines
from public.journal_entry_lines jel
join (
select distinct journal_entry_id
from all_candidates
) candidate on candidate.journal_entry_id = jel.journal_entry_id
group by jel.journal_entry_id
),
candidate_documents as (
-- Resolve every current underlag path that can legally support the entry:
-- direct entry or line links, transaction links, and supplier-invoice
-- references. Return document ids only, never storage paths or file names.
select distinct
c.journal_entry_id,
d.id as document_id,
d.extracted_data
from all_candidates c
join public.document_attachments d
on d.is_current_version = true
and (
d.journal_entry_id = c.journal_entry_id
or exists (
select 1
from public.journal_entry_lines jel
where jel.journal_entry_id = c.journal_entry_id
and jel.id = d.journal_entry_line_id
)
)
union
select distinct
tx.journal_entry_id,
d.id as document_id,
d.extracted_data
from candidate_transactions tx
join public.document_attachments d
on d.id = tx.document_id
and d.is_current_version = true
union
select distinct
c.journal_entry_id,
d.id as document_id,
d.extracted_data
from all_candidates c
join public.supplier_invoices si
on si.registration_journal_entry_id = c.journal_entry_id
or si.payment_journal_entry_id = c.journal_entry_id
or exists (
select 1
from public.supplier_invoice_payments sip
where sip.supplier_invoice_id = si.id
and sip.journal_entry_id = c.journal_entry_id
)
join public.document_attachments d
on d.id = si.document_id
and d.is_current_version = true
),
candidate_document_vat_rates as (
-- Extraction payloads are historical data and may have missing, malformed,
-- or differently scaled rate values. Accept only numeric values, normalizing
-- both 0.25 and 25 to percent before they reach the classifier.
select
cd.journal_entry_id,
cd.document_id,
case
when rate.raw_rate::numeric <= 1 then rate.raw_rate::numeric * 100
else rate.raw_rate::numeric
end as normalized_rate
from candidate_documents cd
cross join lateral (
select vat ->> 'rate' as raw_rate
from jsonb_array_elements(
case
when jsonb_typeof(cd.extracted_data -> 'vatBreakdown') = 'array'
then cd.extracted_data -> 'vatBreakdown'
else '[]'::jsonb
end
) vat
union all
select item ->> 'vatRate' as raw_rate
from jsonb_array_elements(
case
when jsonb_typeof(cd.extracted_data -> 'lineItems') = 'array'
then cd.extracted_data -> 'lineItems'
else '[]'::jsonb
end
) item
) rate
where rate.raw_rate ~ '^\s*[0-9]+(?:\.[0-9]+)?\s*$'
),
document_signals as (
select
cd.journal_entry_id,
array_agg(distinct cd.document_id order by cd.document_id) as document_ids,
count(distinct cd.document_id)::integer as document_count,
count(distinct cd.document_id) filter (
where cd.extracted_data is not null
)::integer as extracted_document_count,
max(
case
when cd.extracted_data ->> 'confidence'
~ '^\s*[0-9]+(?:\.[0-9]+)?\s*$'
then (cd.extracted_data ->> 'confidence')::numeric
end
) as max_extraction_confidence,
bool_or(rate.normalized_rate between 11.9 and 12.1)
as document_has_12pct_vat,
bool_or(rate.normalized_rate between 24.9 and 25.1)
as document_has_25pct_vat
from candidate_documents cd
left join candidate_document_vat_rates rate
on rate.journal_entry_id = cd.journal_entry_id
and rate.document_id = cd.document_id
group by cd.journal_entry_id
),
candidate_output as (
select
c.*,
co.name as company_name,
lines.original_lines,
jsonb_array_length(lines.original_lines) as line_count,
coalesce(doc.document_ids, '{}'::uuid[]) as document_ids,
coalesce(doc.document_count, 0) as document_count,
coalesce(doc.extracted_document_count, 0) as extracted_document_count,
doc.max_extraction_confidence,
coalesce(doc.document_has_12pct_vat, false) as document_has_12pct_vat,
coalesce(doc.document_has_25pct_vat, false) as document_has_25pct_vat,
case
when c.defect = 'representation_25pct_vat'
then 'participants_purpose_and_300_sek_vat_base_cap_required'
else 'not_applicable'
end as independent_representation_review,
c.defect = 'representation_25pct_vat'
and c.cost_debit_amount > 300
as observed_6072_cost_exceeds_300_sek,
reporting.vat_reporting_period,
coalesce(filing.deadline_statuses, '{}'::text[]) as vat_deadline_statuses,
case
when c.defect <> 'representation_25pct_vat' then 'not_applicable'
when reporting.vat_reporting_period is null
then 'unknown_reporting_period'
when coalesce(filing.has_confirmed_record, false)
then 'confirmed_in_app'
when coalesce(filing.has_submitted_record, false)
then 'submitted_in_app'
else 'not_proven_by_in_app_records'
end as vat_filing_status,
case
when fp.id is null then 'missing_fiscal_period'
when fp.is_closed then 'closed'
when fp.locked_at is not null then 'locked'
when cs.bookkeeping_locked_through is not null
and c.entry_date <= cs.bookkeeping_locked_through then 'behind_company_lock_date'
else 'open'
end as effective_lock_status
from all_candidates c
join public.companies co on co.id = c.company_id
join entry_line_snapshots lines on lines.journal_entry_id = c.journal_entry_id
left join document_signals doc on doc.journal_entry_id = c.journal_entry_id
left join public.company_settings cs on cs.company_id = c.company_id
left join public.fiscal_periods fp on fp.id = c.fiscal_period_id
left join lateral (
select case
when c.defect <> 'representation_25pct_vat' then null
when cs.moms_period = 'monthly'
then to_char(c.entry_date, 'YYYY-MM')
when cs.moms_period = 'quarterly'
then concat(
extract(year from c.entry_date)::integer,
'-Q',
ceil(extract(month from c.entry_date) / 3.0)::integer
)
when cs.moms_period = 'yearly' and fp.period_end is not null
then case
when extract(month from fp.period_end) = 12
then extract(year from fp.period_end)::integer::text
else concat(
extract(year from fp.period_end)::integer - 1,
'/',
extract(year from fp.period_end)::integer
)
end
else null
end as vat_reporting_period
) reporting on true
left join lateral (
select
array_agg(distinct d.status order by d.status) as deadline_statuses,
bool_or(d.status = 'submitted') as has_submitted_record,
bool_or(d.status = 'confirmed') as has_confirmed_record
from public.deadlines d
where c.defect = 'representation_25pct_vat'
and d.company_id = c.company_id
and d.tax_deadline_type = case cs.moms_period
when 'monthly' then 'moms_monthly'
when 'quarterly' then 'moms_quarterly'
when 'yearly' then 'moms_yearly'
end
and d.tax_period = reporting.vat_reporting_period
) filing on true
),
classified as (
select
candidate.*,
case
-- Imports and earlier corrections were not generated by either live
-- template in this database. They can have independent accounting
-- issues, but are false positives for this template-remediation audit.
when candidate.source_type in ('import', 'correction')
then 'false_positive'
when candidate.defect = 'representation_25pct_vat'
and candidate.review_priority = 'high_vat_is_25pct_of_6072_cost'
and candidate.document_has_25pct_vat
and not candidate.document_has_12pct_vat
then 'false_positive'
when candidate.defect = 'representation_25pct_vat'
and candidate.review_priority = 'high_vat_is_25pct_of_6072_cost'
then 'insufficient_evidence'
when candidate.defect = 'representation_25pct_vat'
and candidate.observed_vat_rate between 0.119 and 0.121
then 'false_positive'
when candidate.defect = 'representation_25pct_vat'
and candidate.document_has_12pct_vat
and not candidate.document_has_25pct_vat
then 'false_positive'
when candidate.defect = 'representation_25pct_vat'
then 'insufficient_evidence'
-- The defective hotel template deterministically emitted three lines:
-- 5820 cost, 2641 at 12 percent, and one settlement credit. The hotel
-- counterparty signal supplies the final discriminator from car hire.
when candidate.defect = 'travel_hotel_5820'
and candidate.line_count = 3
and candidate.observed_vat_rate between 0.119 and 0.121
and candidate.review_priority = 'high_hotel_counterparty_on_car_hire_account'
then 'confirmed_correction'
when candidate.defect = 'travel_hotel_5820'
and candidate.line_count = 3
and candidate.observed_vat_rate between 0.119 and 0.121
then 'insufficient_evidence'
else 'false_positive'
end as evidence_classification,
case
when candidate.source_type in ('import', 'correction')
then 'entry source cannot be either live template'
when candidate.defect = 'representation_25pct_vat'
and candidate.review_priority = 'high_vat_is_25pct_of_6072_cost'
and candidate.document_has_25pct_vat
and not candidate.document_has_12pct_vat
then 'extracted underlag confirms a 25 percent supply'
when candidate.defect = 'representation_25pct_vat'
and candidate.review_priority = 'high_vat_is_25pct_of_6072_cost'
then 'exact defective signature but no decisive underlag or provenance'
when candidate.defect = 'representation_25pct_vat'
and candidate.observed_vat_rate between 0.119 and 0.121
then 'voucher has the intended 12 percent aggregate VAT signature'
when candidate.defect = 'representation_25pct_vat'
and candidate.document_has_12pct_vat
and not candidate.document_has_25pct_vat
then 'extracted underlag confirms a 12 percent supply'
when candidate.defect = 'representation_25pct_vat'
then 'mixed representation voucher needs decisive underlag or provenance'
when candidate.defect = 'travel_hotel_5820'
and candidate.line_count = 3
and candidate.observed_vat_rate between 0.119 and 0.121
and candidate.review_priority = 'high_hotel_counterparty_on_car_hire_account'
then 'exact defective hotel shape plus hotel counterparty'
when candidate.defect = 'travel_hotel_5820'
and candidate.line_count = 3
and candidate.observed_vat_rate between 0.119 and 0.121
then 'exact defective hotel shape without hotel evidence'
else 'voucher does not have the defective hotel template shape'
end as classification_reason
from candidate_output candidate
)
select
c.defect,
c.evidence_classification,
c.classification_reason,
case
when c.evidence_classification = 'confirmed_correction'
then 'explicit_write_approval_required_after_underlag_review'
when c.evidence_classification = 'insufficient_evidence'
then 'underlag_required_before_approval_request'
else 'no_template_remediation'
end as approval_status,
case
when c.defect = 'representation_25pct_vat'
and c.evidence_classification <> 'false_positive'
then 'potential_if_confirmed'
else 'none_from_this_defect'
end as vat_return_impact,
c.company_id,
c.company_name,
c.journal_entry_id,
c.voucher_series,
c.voucher_number,
c.entry_date,
c.committed_at,
c.fiscal_period_id,
c.source_type,
c.cost_line_ids,
c.vat_line_ids,
c.observed_cost_account,
c.expected_cost_account,
c.observed_vat_account,
c.expected_vat_account,
c.cost_debit_amount,
c.vat_debit_amount,
c.observed_vat_rate,
c.expected_vat_rate,
c.transaction_description,
c.review_priority,
c.line_count,
c.document_ids,
c.document_count,
c.extracted_document_count,
c.max_extraction_confidence,
c.document_has_12pct_vat,
c.document_has_25pct_vat,
c.independent_representation_review,
c.observed_6072_cost_exceeds_300_sek,
c.vat_reporting_period,
c.vat_deadline_statuses,
c.vat_filing_status,
c.original_lines,
c.effective_lock_status
from classified c
order by
case c.evidence_classification
when 'confirmed_correction' then 0
when 'insufficient_evidence' then 1
else 2
end,
c.defect,
case
when c.review_priority like 'high%' then 0
when c.review_priority like 'medium%' then 1
else 2
end,
c.entry_date desc;
+900
View File
@@ -0,0 +1,900 @@
#!/usr/bin/env npx tsx
/**
* ============================================================================
* !! DESTRUCTIVE. READ THIS BEFORE YOU RUN ANYTHING. !!
* ============================================================================
*
* This script MOVES räkenskapsinformation. The objects it touches are kvitton,
* leverantorsfakturor and kontoutdrag held under the Bokforingslagen 7 kap 2 §
* SEVEN-YEAR RETENTION requirement. Losing one is a legal incident, not a bug.
*
* * ALWAYS run against STAGING first and confirm the counts there.
* * NEVER point this at .env.local. That file targets the REAL customer
* database. Pass --env <file> explicitly and read the banner it prints.
* * The default mode is --dry-run. Moving data requires the explicit
* --apply flag. Removing the source copies requires --apply AND
* --delete-source, which should be a SEPARATE run, days after --apply,
* once the app has been observed serving the new keys.
*
* ============================================================================
* WHAT IT DOES (Phase B of the 3-phase rollout)
* ============================================================================
*
* Phase A (migration 20260726092000_documents_bucket_company_scope.sql) added
* company-scoped storage policies for the key layout
*
* documents/{companyId}/{userId}/{timestamp}_{filename}
*
* and switched lib/core/documents/document-service.ts to write that layout.
* Legacy objects still sit at
*
* documents/{userId}/{timestamp}_{filename}
*
* where the RLS policy can only scope on auth.uid(), so a removed company
* member keeps read access to everything they ever uploaded.
*
* This script re-homes those legacy objects. Rows are grouped by storage key
* first: document_attachments.storage_path has NO unique constraint, so
* several rows can point at one object, and the object may only be released
* once every one of those rows has been repointed. Per source key, in order:
*
* 1. resolve the owning company_id(s) from document_attachments
* 2. skip rows whose storage_path is already company-scoped (idempotent /
* resumable)
* 3. COPY the object to the company-scoped key(s) (never move, never
* rename; one copy per owning company)
* 4. VERIFY each new key is readable and byte-identical (SHA-256 compared
* against document_attachments.sha256_hash when present, otherwise
* against the source bytes)
* 5. only then UPDATE storage_path for EVERY row on that key, checking the
* row count of each UPDATE: a row deleted between fetch and migrate
* must not count as migrated, or the fresh scoped copy would resurrect
* an erased document (a copy no row ended up pointing at is rolled
* back; the source is never touched by that rollback)
* 6. only with --delete-source, and only after 3-5 succeeded for ALL rows
* on the key AND the live table confirms zero rows still reference it,
* remove the legacy object
*
* A --delete-source run additionally SWEEPS rows that an earlier --apply run
* already repointed: their legacy key is derived from the scoped one (the
* forward mapping is a pure prefix insertion, so the inverse is exact), the
* scoped copy is re-verified (readable + hash), the live table is checked
* for remaining references, and only then is the leftover legacy object
* removed. Without this sweep the documented two-step workflow (--apply
* first, --delete-source days later) would be a no-op: after --apply no row
* carries a legacy pointer any more, yet every legacy object still sits in
* storage, readable by its uploader (including ex-members: the exact hole
* Phase A exists to close).
*
* The source is NEVER deleted before the new key has been confirmed readable
* and every referencing row repointed. A failure at any step logs the
* document id and the script continues to the next object; it never aborts
* the run on a single bad document.
*
* ============================================================================
* PHASE C GATE
* ============================================================================
*
* The final summary prints two counters:
*
* legacy_prefix_remaining rows still pointing at a legacy key
* legacy_objects_remaining legacy objects still present in storage for
* rows that are already company-scoped (found by
* the storage-level sweep, which runs read-only
* in every mode)
*
* Phase C (the migration that drops `documents_select_own` /
* `documents_insert_own`) may only be applied when a --dry-run of this
* script reports BOTH counters at 0. Dropping those policies earlier makes
* every un-migrated document unreadable to everyone except the service role,
* and leftover legacy objects would stay uploader-readable forever.
*
* ============================================================================
* USAGE
* ============================================================================
*
* # 1. Inspect. Changes nothing. This is the default.
* npx tsx scripts/backfill-document-storage-paths.ts --env .env.staging.local
*
* # 2. Copy + verify + repoint. Leaves the legacy objects in place.
* npx tsx scripts/backfill-document-storage-paths.ts --env .env.staging.local --apply
*
* # 3. Days later, after the app has been observed serving new keys:
* # migrates any stragglers, then sweeps the already-migrated rows and
* # removes their verified leftover legacy objects.
* npx tsx scripts/backfill-document-storage-paths.ts --env .env.staging.local --apply --delete-source
*
* Flags:
* --env <file> dotenv file to load. REQUIRED. No default: an implicit
* .env.local would point at production.
* --apply actually copy/verify/repoint. Without it: dry run.
* --delete-source additionally remove legacy objects whose every row has
* a verified company-scoped copy. Requires --apply.
* --company <uuid> restrict the run to one company. Use this for the first
* production batch.
* --limit <n> process at most n documents this run (resumable: run
* again to continue). A shared-key group is never split,
* so the last group may overshoot the limit. Also caps
* how many leftover legacy objects a --delete-source
* sweep removes.
* --yes skip the interactive confirmation prompt.
*/
import { createHash } from 'node:crypto'
import { basename, resolve } from 'node:path'
import { createInterface } from 'node:readline/promises'
import { config } from 'dotenv'
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
const BUCKET = 'documents'
const PATH_ROOT = 'documents'
const PAGE_SIZE = 500
// ---------------------------------------------------------------------------
// Argument parsing
// ---------------------------------------------------------------------------
function flagValue(name: string): string | undefined {
const idx = process.argv.indexOf(`--${name}`)
if (idx === -1) return undefined
const value = process.argv[idx + 1]
if (!value || value.startsWith('--')) {
console.error(`--${name} requires a value`)
process.exit(1)
}
return value
}
const envFile = flagValue('env')
const apply = process.argv.includes('--apply')
const deleteSource = process.argv.includes('--delete-source')
const skipPrompt = process.argv.includes('--yes')
const onlyCompany = flagValue('company')
const limitRaw = flagValue('limit')
const limit = limitRaw ? Number.parseInt(limitRaw, 10) : Number.POSITIVE_INFINITY
if (!envFile) {
console.error(
'Refusing to run without an explicit --env <file>. An implicit .env.local ' +
'points at the PRODUCTION database. Example: --env .env.staging.local',
)
process.exit(1)
}
// Refuse .env.local no matter how the path is spelled ('./.env.local', an
// absolute path, backslashes on Windows): normalize before comparing. An
// exact string compare here was trivially bypassed by './.env.local'. The
// check runs BEFORE config() so the production env file is never even read.
if (basename(resolve(envFile)).toLowerCase() === '.env.local') {
console.error(
'REFUSING: .env.local points at the production database. If you really ' +
'intend to run against production, copy the credentials into an ' +
'explicitly named file (e.g. .env.production.backfill) so the intent ' +
'is recorded in the command line.',
)
process.exit(1)
}
if (deleteSource && !apply) {
console.error('--delete-source requires --apply.')
process.exit(1)
}
if (limitRaw && (!Number.isFinite(limit) || limit <= 0)) {
console.error('--limit must be a positive integer.')
process.exit(1)
}
config({ path: envFile })
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error(`Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in ${envFile}`)
process.exit(1)
}
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface DocumentRow {
id: string
company_id: string | null
storage_path: string | null
file_name: string | null
mime_type: string | null
sha256_hash: string | null
}
interface Failure {
documentId: string
storagePath: string | null
step: string
reason: string
}
/** All rows (possibly from several companies) that share one legacy key. */
interface PathGroup {
sourcePath: string
rows: DocumentRow[]
}
// ---------------------------------------------------------------------------
// Path helpers.
//
// Deliberately duplicated from lib/core/documents/document-service.ts instead
// of imported: this script runs standalone under tsx and must not drag the
// service module's Next.js/event-bus imports into a plain node process. Keep
// the two in sync if the layout ever changes again.
// ---------------------------------------------------------------------------
function isCompanyScoped(storagePath: string, companyId: string): boolean {
return storagePath.startsWith(`${PATH_ROOT}/${companyId}/`)
}
function isLegacyDocumentPath(storagePath: string): boolean {
return storagePath.startsWith(`${PATH_ROOT}/`)
}
function companyScopedPath(storagePath: string, companyId: string): string | null {
if (isCompanyScoped(storagePath, companyId)) return null
if (!isLegacyDocumentPath(storagePath)) return null
return `${PATH_ROOT}/${companyId}/${storagePath.slice(`${PATH_ROOT}/`.length)}`
}
/**
* Inverse of companyScopedPath: derive the legacy key a company-scoped key
* was (or would have been) migrated from. The forward mapping is a pure
* prefix insertion, so the inverse is exact. Returns null when the key is
* not company-scoped for the given company.
*/
function legacyPathFor(storagePath: string, companyId: string): string | null {
const prefix = `${PATH_ROOT}/${companyId}/`
if (!storagePath.startsWith(prefix)) return null
return `${PATH_ROOT}/${storagePath.slice(prefix.length)}`
}
function sha256Hex(buffer: ArrayBuffer): string {
return createHash('sha256').update(Buffer.from(buffer)).digest('hex')
}
// ---------------------------------------------------------------------------
// Safety banner + confirmation
// ---------------------------------------------------------------------------
function projectRef(url: string): string {
try {
return new URL(url).hostname.split('.')[0] ?? url
} catch {
return url
}
}
async function confirmOrExit(): Promise<void> {
const ref = projectRef(supabaseUrl!)
console.log('')
console.log('='.repeat(78))
console.log(' documents bucket backfill: legacy uploader-scoped keys -> company-scoped')
console.log('='.repeat(78))
console.log(` env file : ${envFile}`)
console.log(` supabase project: ${ref}`)
console.log(` mode : ${apply ? 'APPLY (writes data)' : 'DRY RUN (no changes)'}`)
console.log(` delete source : ${deleteSource ? 'YES (legacy objects removed)' : 'no'}`)
console.log(` company filter : ${onlyCompany ?? '(all companies)'}`)
console.log(` limit : ${Number.isFinite(limit) ? limit : '(none)'}`)
console.log('='.repeat(78))
console.log('')
if (!apply || skipPrompt) return
const rl = createInterface({ input: process.stdin, output: process.stdout })
const answer = await rl.question(
`Type the project ref (${ref}) to proceed, anything else to abort: `,
)
rl.close()
if (answer.trim() !== ref) {
console.error('Aborted.')
process.exit(1)
}
}
// ---------------------------------------------------------------------------
// Data access
// ---------------------------------------------------------------------------
/**
* Page through document_attachments. PostgREST caps a single response at
* 1000 rows, so never rely on an unbounded select here.
*/
async function fetchAllDocuments(supabase: SupabaseClient): Promise<DocumentRow[]> {
const rows: DocumentRow[] = []
let from = 0
for (;;) {
let query = supabase
.from('document_attachments')
.select('id, company_id, storage_path, file_name, mime_type, sha256_hash')
.not('storage_path', 'is', null)
// created_at alone is not unique: without the id tiebreaker, rows
// sharing a timestamp can be skipped or duplicated across .range()
// pages, and a skipped row silently stays on the legacy key.
.order('created_at', { ascending: true })
.order('id', { ascending: true })
.range(from, from + PAGE_SIZE - 1)
if (onlyCompany) query = query.eq('company_id', onlyCompany)
const { data, error } = await query
if (error) throw new Error(`Failed to page document_attachments: ${error.message}`)
if (!data || data.length === 0) break
rows.push(...(data as DocumentRow[]))
if (data.length < PAGE_SIZE) break
from += PAGE_SIZE
}
return rows
}
async function objectExists(supabase: SupabaseClient, path: string): Promise<boolean> {
const segments = path.split('/')
const name = segments.pop()!
const folder = segments.join('/')
const { data, error } = await supabase.storage.from(BUCKET).list(folder, { search: name })
// Surface list failures instead of treating them as "missing": a transient
// error must never make a leftover object look already-swept.
if (error) throw new Error(`Failed to list ${folder}: ${error.message}`)
return !!data?.some((entry) => entry.name === name)
}
/**
* Authoritative check that at least one document_attachments row still points
* at the given storage key. Deliberately queries the LIVE table rather than
* this run's in-memory snapshot: a --company or --limit window, a
* NULL-company row, or a concurrent writer must never be able to hide a row
* whose only object the key is.
*/
async function pathStillReferenced(supabase: SupabaseClient, path: string): Promise<boolean> {
const { data, error } = await supabase
.from('document_attachments')
.select('id')
.eq('storage_path', path)
.limit(1)
if (error) throw new Error(`Failed to check references for ${path}: ${error.message}`)
return (data?.length ?? 0) > 0
}
/**
* Remove a legacy object, but only when the live table confirms zero rows
* still reference it. Every caller has already verified a company-scoped
* copy for every row that pointed here; this function is the ONLY place the
* script deletes a legacy source object. Returns true when the object was
* actually removed.
*/
async function removeLegacySourceIfUnreferenced(
supabase: SupabaseClient,
legacyPath: string,
): Promise<boolean> {
try {
if (await pathStillReferenced(supabase, legacyPath)) {
console.warn(` [${legacyPath}] source kept: still referenced by at least one row`)
return false
}
} catch (err) {
console.warn(
` [${legacyPath}] source kept: reference check failed: ${
err instanceof Error ? err.message : String(err)
}`,
)
return false
}
const { error: removeError } = await supabase.storage.from(BUCKET).remove([legacyPath])
if (removeError) {
console.warn(` [${legacyPath}] source not removed: ${removeError.message}`)
return false
}
return true
}
// ---------------------------------------------------------------------------
// Per-object migration (all rows sharing one legacy key, together)
// ---------------------------------------------------------------------------
interface TargetState {
targetPath: string
copyHash: string
uploadedThisRun: boolean
repointed: number
}
async function migrateGroup(
supabase: SupabaseClient,
group: PathGroup,
failures: Failure[],
): Promise<{ migratedRows: number; failedRows: number; sourceRemoved: boolean }> {
const { sourcePath, rows } = group
const fail = (row: DocumentRow, step: string, reason: string) => {
failures.push({ documentId: row.id, storagePath: sourcePath, step, reason })
}
// ---- 3. COPY (never move: the source must survive until step 6).
// Downloaded once per key: several rows can share the object. ------------
const { data: sourceBlob, error: downloadError } = await supabase.storage
.from(BUCKET)
.download(sourcePath)
if (downloadError || !sourceBlob) {
const reason = downloadError?.message ?? 'no data returned'
for (const row of rows) fail(row, 'download-source', reason)
return { migratedRows: 0, failedRows: rows.length, sourceRemoved: false }
}
const sourceBytes = await sourceBlob.arrayBuffer()
const sourceHash = sha256Hex(sourceBytes)
// One scoped target per company referencing this source key: rows from
// different companies sharing one object each get their own copy.
const companies = [...new Set(rows.map((row) => row.company_id!))]
const targets = new Map<string, TargetState>()
for (const companyId of companies) {
const companyRows = rows.filter((row) => row.company_id === companyId)
const targetPath = companyScopedPath(sourcePath, companyId)!
let uploadedThisRun = false
try {
// An already-present target means a previous run got this far: fall
// through to verification rather than failing on the upsert:false
// conflict.
if (!(await objectExists(supabase, targetPath))) {
const { error: uploadError } = await supabase.storage
.from(BUCKET)
.upload(targetPath, sourceBytes, {
contentType: companyRows[0]!.mime_type ?? 'application/octet-stream',
upsert: false,
})
if (uploadError) {
for (const row of companyRows) fail(row, 'upload-copy', uploadError.message)
continue
}
uploadedThisRun = true
}
} catch (err) {
const reason = err instanceof Error ? err.message : String(err)
for (const row of companyRows) fail(row, 'upload-copy', reason)
continue
}
// ---- 4. VERIFY the new key is readable, once per target --------------
const { data: copyBlob, error: verifyError } = await supabase.storage
.from(BUCKET)
.download(targetPath)
if (verifyError || !copyBlob) {
const reason = verifyError?.message ?? 'copy not readable at the new key'
for (const row of companyRows) fail(row, 'verify-readable', reason)
continue
}
targets.set(companyId, {
targetPath,
copyHash: sha256Hex(await copyBlob.arrayBuffer()),
uploadedThisRun,
repointed: 0,
})
}
let migratedRows = 0
for (const row of rows) {
const target = targets.get(row.company_id!)
if (!target) continue // upload/readability failure already recorded above
// The stored hash is the strongest reference; fall back to the source
// bytes for rows written before sha256_hash was populated.
const expectedHash = row.sha256_hash ?? sourceHash
if (target.copyHash !== expectedHash) {
fail(row, 'verify-hash', `copy hash ${target.copyHash} does not match expected ${expectedHash}`)
continue
}
// ---- 5. Repoint the DB, only now that the copy is proven. The row
// count matters: a row deleted between fetch and migrate makes this a
// 0-row UPDATE, which must NOT count as migrated (the scoped copy would
// resurrect an erased document). ---------------------------------------
const { data: updated, error: updateError } = await supabase
.from('document_attachments')
.update({ storage_path: target.targetPath })
.eq('id', row.id)
.select('id')
if (updateError) {
fail(row, 'update-pointer', updateError.message)
continue
}
if (!updated || updated.length === 0) {
fail(row, 'update-pointer', 'no row matched: deleted between fetch and migrate')
continue
}
target.repointed++
migratedRows++
}
// Roll back any scoped copy created THIS RUN that no row ended up pointing
// at, so it cannot linger as an unreferenced resurrected duplicate. Safe:
// the copy is seconds old, the live table confirms nothing references it,
// and the SOURCE object is never touched here.
for (const target of targets.values()) {
if (!target.uploadedThisRun || target.repointed > 0) continue
try {
if (await pathStillReferenced(supabase, target.targetPath)) continue
const { error: cleanupError } = await supabase.storage
.from(BUCKET)
.remove([target.targetPath])
if (cleanupError) {
console.warn(
` [${target.targetPath}] unreferenced fresh copy not rolled back: ${cleanupError.message}`,
)
}
} catch (err) {
console.warn(
` [${target.targetPath}] unreferenced fresh copy not rolled back: ${
err instanceof Error ? err.message : String(err)
}`,
)
}
}
const failedRows = rows.length - migratedRows
// ---- 6. Optional source removal, strictly last: only when EVERY row on
// this key is repointed, and the live table double-checks that. ----------
let sourceRemoved = false
if (deleteSource && failedRows === 0) {
sourceRemoved = await removeLegacySourceIfUnreferenced(supabase, sourcePath)
}
return { migratedRows, failedRows, sourceRemoved }
}
// ---------------------------------------------------------------------------
// Storage-level sweep over rows that are ALREADY company-scoped
// ---------------------------------------------------------------------------
interface SweepResult {
/** Distinct derived legacy keys whose object still exists in storage. */
keysWithLeftovers: number
objectsRemoved: number
}
/**
* An --apply run repoints rows and leaves the legacy objects behind, so a
* later --delete-source run can no longer find them through the pointers:
* it has to derive each legacy key from the scoped one (legacyPathFor) and
* check storage directly. For every derived key whose object still exists:
*
* - without --delete-source: count it (input to the Phase C gate)
* - with --delete-source: verify the scoped copy is readable and matches
* the expected hash for EVERY row mapping to the key, confirm via the
* live table that no row still references the key, and only then remove
* it (capped by --limit).
*/
async function sweepLegacyObjects(
supabase: SupabaseClient,
scopedRows: DocumentRow[],
skipKeys: Set<string>,
failures: Failure[],
deleteBudget: number,
): Promise<SweepResult> {
// Group scoped rows by derived legacy key: several rows (even from
// different companies) can map to one key, and the key may only be removed
// when every one of them has a verified scoped copy.
const byLegacyKey = new Map<string, DocumentRow[]>()
for (const row of scopedRows) {
const legacyKey = legacyPathFor(row.storage_path!, row.company_id!)
if (!legacyKey) continue
if (skipKeys.has(legacyKey)) continue // handled by this run's migration phase
const list = byLegacyKey.get(legacyKey)
if (list) list.push(row)
else byLegacyKey.set(legacyKey, [row])
}
const result: SweepResult = { keysWithLeftovers: 0, objectsRemoved: 0 }
for (const [legacyKey, rows] of byLegacyKey) {
let counted = false
try {
// Most keys belong to uploads born company-scoped: no legacy object
// ever existed for them, and this check is all they cost.
if (!(await objectExists(supabase, legacyKey))) continue
result.keysWithLeftovers++
counted = true
if (!deleteSource) continue // report-only: feeds legacy_objects_remaining
if (result.objectsRemoved >= deleteBudget) continue
// VERIFY: every row mapping to this key must have a readable scoped
// copy with the expected hash before the legacy object may go.
let allVerified = true
let legacyHash: string | null = null
for (const row of rows) {
const { data: copyBlob, error: copyError } = await supabase.storage
.from(BUCKET)
.download(row.storage_path!)
if (copyError || !copyBlob) {
failures.push({
documentId: row.id,
storagePath: legacyKey,
step: 'sweep-verify-readable',
reason: copyError?.message ?? 'scoped copy not readable',
})
allVerified = false
break
}
const copyHash = sha256Hex(await copyBlob.arrayBuffer())
let expectedHash = row.sha256_hash
if (!expectedHash) {
// No stored hash: the legacy bytes themselves are the reference.
if (legacyHash === null) {
const { data: legacyBlob, error: legacyError } = await supabase.storage
.from(BUCKET)
.download(legacyKey)
if (legacyError || !legacyBlob) {
failures.push({
documentId: row.id,
storagePath: legacyKey,
step: 'sweep-download-legacy',
reason: legacyError?.message ?? 'legacy object listed but not readable',
})
allVerified = false
break
}
legacyHash = sha256Hex(await legacyBlob.arrayBuffer())
}
expectedHash = legacyHash
}
if (copyHash !== expectedHash) {
failures.push({
documentId: row.id,
storagePath: legacyKey,
step: 'sweep-verify-hash',
reason: `scoped copy hash ${copyHash} does not match expected ${expectedHash}`,
})
allVerified = false
break
}
}
if (!allVerified) continue // stays counted as a leftover; gate stays closed
if (await removeLegacySourceIfUnreferenced(supabase, legacyKey)) {
console.log(
` [sweep] removed ${legacyKey} (${rows.length} row(s) verified at scoped keys)`,
)
result.objectsRemoved++
}
} catch (err) {
// Unknown state: count the key as remaining so the gate stays closed.
if (!counted) result.keysWithLeftovers++
failures.push({
documentId: rows[0]!.id,
storagePath: legacyKey,
step: 'sweep-unexpected',
reason: err instanceof Error ? err.message : String(err),
})
}
}
return result
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main(): Promise<void> {
await confirmOrExit()
const supabase = createClient(supabaseUrl!, serviceRoleKey!, {
auth: { persistSession: false, autoRefreshToken: false },
})
const rows = await fetchAllDocuments(supabase)
const alreadyScoped: DocumentRow[] = []
const candidates: DocumentRow[] = []
const unmapped: DocumentRow[] = []
for (const row of rows) {
if (!row.storage_path) continue
if (!row.company_id) {
unmapped.push(row)
continue
}
if (isCompanyScoped(row.storage_path, row.company_id)) {
alreadyScoped.push(row)
} else if (isLegacyDocumentPath(row.storage_path)) {
candidates.push(row)
} else {
// Shapes this backfill deliberately does not touch, e.g. the MCP
// audit-package keys `{userId}/audit-packages/...` and the demo-seed
// `{userId}/{companyId}/inbox/...` keys. They are not covered by the
// documents_*_company policies and are not document_attachments
// underlag in the BFL sense.
unmapped.push(row)
}
}
// Group the legacy rows by source key: storage_path has no unique
// constraint, so several rows can share one object. The object is copied
// once, ALL its rows are repointed, and only then may the source go;
// migrating rows one by one deleted the source after the first row and
// stranded every later row on a dead pointer.
const groupsByPath = new Map<string, DocumentRow[]>()
for (const row of candidates) {
const list = groupsByPath.get(row.storage_path!)
if (list) list.push(row)
else groupsByPath.set(row.storage_path!, [row])
}
const groups: PathGroup[] = [...groupsByPath.entries()].map(([sourcePath, groupRows]) => ({
sourcePath,
rows: groupRows,
}))
console.log(`document_attachments rows with a storage_path : ${rows.length}`)
console.log(` already company-scoped : ${alreadyScoped.length}`)
console.log(
` legacy prefix, need migration : ${candidates.length} (${groups.length} distinct objects)`,
)
console.log(` outside the documents/ layout (skipped) : ${unmapped.length}`)
console.log('')
if (unmapped.length > 0) {
console.log('Skipped rows (first 20):')
for (const row of unmapped.slice(0, 20)) {
console.log(` ${row.id} company=${row.company_id ?? 'NULL'} path=${row.storage_path}`)
}
console.log('')
}
// --limit caps processed rows, but a shared-key group is never split:
// repointing only some of a key's rows would let a later --delete-source
// remove an object the remaining rows still need.
const batch: PathGroup[] = []
let batchRows = 0
for (const group of groups) {
if (batchRows >= limit) break
batch.push(group)
batchRows += group.rows.length
}
const failures: Failure[] = []
let migratedRows = 0
let sourcesRemoved = 0
// Groups fully migrated this run whose legacy object was (deliberately or
// not) left in storage: they count as leftover legacy objects below.
let sourcesKept = 0
if (!apply) {
console.log('DRY RUN: nothing was changed. Planned moves (first 20):')
const preview = batch.flatMap((group) => group.rows).slice(0, 20)
for (const row of preview) {
console.log(` ${row.id}`)
console.log(` from ${row.storage_path}`)
console.log(` to ${companyScopedPath(row.storage_path!, row.company_id!)}`)
}
console.log('')
} else {
for (const [index, group] of batch.entries()) {
const progress = `${index + 1}/${batch.length}`
const rowCount = group.rows.length
console.log(
`[${progress}] ${group.sourcePath} (${rowCount} row${rowCount === 1 ? '' : 's'})`,
)
try {
const outcome = await migrateGroup(supabase, group, failures)
migratedRows += outcome.migratedRows
if (outcome.failedRows === 0) {
if (outcome.sourceRemoved) sourcesRemoved++
else sourcesKept++
}
} catch (err) {
// Never abort the run on one bad object.
const reason = err instanceof Error ? err.message : String(err)
for (const row of group.rows) {
failures.push({
documentId: row.id,
storagePath: group.sourcePath,
step: 'unexpected',
reason,
})
}
}
}
}
// Storage-level sweep over already-migrated rows. Without it, an --apply
// run followed by a later --delete-source run finds zero legacy POINTERS
// and deletes nothing, while every legacy OBJECT still sits in the bucket
// readable by its uploader. Read-only unless --delete-source; the keys
// this run already handled as migration sources are skipped.
console.log('')
console.log(
`Sweeping ${alreadyScoped.length} already-scoped row(s) for leftover legacy objects` +
(deleteSource ? ' (verify + delete)...' : ' (read-only)...'),
)
const batchSourcePaths = new Set(batch.map((group) => group.sourcePath))
const sweep = await sweepLegacyObjects(
supabase,
alreadyScoped,
batchSourcePaths,
failures,
limit,
)
const legacyRowsRemaining = candidates.length - migratedRows
const legacyObjectsRemaining =
sweep.keysWithLeftovers - sweep.objectsRemoved + sourcesKept
console.log('')
console.log('='.repeat(78))
console.log(` rows migrated : ${migratedRows}`)
console.log(` failures : ${failures.length}`)
console.log(` legacy objects removed : ${sourcesRemoved + sweep.objectsRemoved}`)
console.log(` legacy_prefix_remaining : ${legacyRowsRemaining}`)
console.log(` legacy_objects_remaining : ${legacyObjectsRemaining}`)
console.log('='.repeat(78))
if (failures.length > 0) {
console.log('')
console.log('Failures (document id / step / reason):')
for (const failure of failures) {
console.log(` ${failure.documentId} [${failure.step}] ${failure.reason}`)
console.log(` path: ${failure.storagePath}`)
}
}
// The gate needs BOTH counters at zero: rows still on legacy pointers make
// documents unreadable after Phase C, and leftover legacy objects keep the
// uploader-scoped read hole open.
const gateOpen =
legacyRowsRemaining === 0 && legacyObjectsRemaining === 0 && failures.length === 0
console.log('')
if (gateOpen) {
console.log(
apply
? 'PHASE C GATE: OPEN. Re-run with --dry-run to confirm, then apply the Phase C migration.'
: 'PHASE C GATE: OPEN. Zero legacy-prefix rows and zero leftover legacy objects; the Phase C migration may be applied.',
)
} else {
const next =
legacyRowsRemaining > 0
? 'Run with --apply until legacy_prefix_remaining reaches 0.'
: legacyObjectsRemaining > 0
? 'Run with --apply --delete-source to remove the leftover legacy objects.'
: 'Resolve the failures above and re-run.'
console.log(
`PHASE C GATE: CLOSED. Do NOT drop documents_select_own / documents_insert_own yet. ${next}`,
)
}
// Non-zero exit on failures so a CI/ops wrapper notices, but only AFTER the
// full report has been printed.
if (failures.length > 0) process.exitCode = 1
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+106
View File
@@ -0,0 +1,106 @@
/**
* One-off backfill: encrypt any employees.personnummer still stored as
* plaintext (12 digits) into aes-256-gcm ciphertext, matching the format
* written by encryptPersonnummer().
*
* WHY: the v1 REST create route used to store personnummer unencrypted, which
* 500'd every decrypt-on-read path with ERR_CRYPTO_INVALID_AUTH_TAG
* ("Invalid authentication tag length: 6"). The code fix stops new plaintext;
* this repairs the rows already in the DB and closes the GDPR at-rest gap.
* Run AFTER the code fix is deployed.
*
* Idempotent: only touches rows whose personnummer matches /^\d{12}$/, and each
* update is guarded on the exact plaintext value so a re-run or a concurrent
* write can never double-encrypt. Safe to re-run.
*
* Usage:
* npx tsx scripts/backfill-encrypt-personnummer.ts # dry run (read-only)
* npx tsx scripts/backfill-encrypt-personnummer.ts --confirm # performs the writes
*
* Reads NEXT_PUBLIC_SUPABASE_URL + SUPABASE_SERVICE_ROLE_KEY +
* PERSONNUMMER_ENCRYPTION_KEY from .env.local. Treat .env.local as pointing at
* PRODUCTION: the dry run is read-only; --confirm mutates PII.
*/
import { createClient } from '@supabase/supabase-js'
import { config as dotenv } from 'dotenv'
import { resolve } from 'node:path'
import { encryptPersonnummer } from '@/lib/salary/personnummer'
dotenv({ path: resolve(process.cwd(), '.env.local') })
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL
const SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!SUPABASE_URL || !SERVICE_KEY) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
// Refuse to run without the real key: encrypting with the dev fallback key
// would make the values unreadable in production.
if (!process.env.PERSONNUMMER_ENCRYPTION_KEY) {
console.error(
'Missing PERSONNUMMER_ENCRYPTION_KEY. Refusing to run so rows are not encrypted with the dev fallback key.',
)
process.exit(1)
}
const CONFIRM = process.argv.includes('--confirm')
const PLAINTEXT = /^\d{12}$/
const sb = createClient(SUPABASE_URL, SERVICE_KEY, { auth: { persistSession: false } })
async function main() {
const host = new URL(SUPABASE_URL!).host
console.log(`Target: ${host} mode: ${CONFIRM ? 'WRITE (--confirm)' : 'DRY RUN (read-only)'}`)
const { data, error } = await sb
.from('employees')
.select('id, company_id, personnummer, personnummer_last4')
if (error) throw new Error(`select employees: ${error.message}`)
const rows = data ?? []
const plaintextRows = rows.filter((r) => PLAINTEXT.test(String(r.personnummer ?? '')))
const byCompany = new Map<string, number>()
for (const r of plaintextRows) {
byCompany.set(r.company_id, (byCompany.get(r.company_id) ?? 0) + 1)
}
console.log(
`Scanned ${rows.length} employees; ${plaintextRows.length} plaintext across ${byCompany.size} companies.`,
)
for (const [companyId, n] of byCompany) console.log(` company ${companyId}: ${n} row(s)`)
if (plaintextRows.length === 0) {
console.log('Nothing to backfill.')
return
}
if (!CONFIRM) {
console.log('\nDRY RUN: no writes performed. Re-run with --confirm to encrypt these rows.')
return
}
let updated = 0
for (const r of plaintextRows) {
const plaintext = String(r.personnummer)
const last4 = plaintext.slice(-4)
const patch: Record<string, string> = { personnummer: encryptPersonnummer(plaintext) }
if (r.personnummer_last4 !== last4) patch.personnummer_last4 = last4
// Guard on the still-plaintext value: makes the update idempotent and safe
// against a concurrent write (never double-encrypts).
const { error: upErr } = await sb
.from('employees')
.update(patch)
.eq('id', r.id)
.eq('personnummer', plaintext)
if (upErr) throw new Error(`update ${r.id}: ${upErr.message}`)
updated++
}
console.log(`\nEncrypted ${updated} row(s). Re-run without --confirm to verify 0 remain.`)
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/env npx tsx
/**
* Backfill auth.users.app_metadata.has_password for the BankID-MFA lockout fix.
*
* - BankID-linked users (app_metadata.bankid_linked === true) with the flag
* unset → set has_password = false. Banner in SecuritySettings will then
* guide them through /account/set-password before MFA enroll is unlocked.
*
* - All other users with the flag unset (legacy email/password signups) →
* set has_password = true. They have a real password.
*
* - Anyone with the flag already set is left alone: fully idempotent.
*
* Usage:
* npx tsx scripts/backfill-has-password.ts # apply
* npx tsx scripts/backfill-has-password.ts --dry-run # report only
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
const DRY_RUN = process.argv.includes('--dry-run')
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error(
'Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local',
)
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
async function main() {
let page = 1
const perPage = 200
let totalScanned = 0
let setFalse = 0
let setTrue = 0
let skipped = 0
for (;;) {
const { data, error } = await supabase.auth.admin.listUsers({
page,
perPage,
})
if (error) {
console.error('listUsers failed', error)
process.exit(1)
}
if (!data.users || data.users.length === 0) break
for (const user of data.users) {
totalScanned++
const meta = (user.app_metadata ?? {}) as Record<string, unknown>
const flagAlreadySet =
meta.has_password === true || meta.has_password === false
if (flagAlreadySet) {
skipped++
continue
}
const isBankIdLinked = meta.bankid_linked === true
const nextValue = isBankIdLinked ? false : true
if (DRY_RUN) {
if (nextValue) setTrue++
else setFalse++
continue
}
const merged = { ...meta, has_password: nextValue }
const { error: updateError } = await supabase.auth.admin.updateUserById(
user.id,
{ app_metadata: merged },
)
if (updateError) {
console.error(`failed to update user ${user.id}`, updateError)
continue
}
if (nextValue) setTrue++
else setFalse++
}
if (data.users.length < perPage) break
page++
}
console.log(JSON.stringify({
mode: DRY_RUN ? 'dry-run' : 'apply',
scanned: totalScanned,
set_true: setTrue,
set_false: setFalse,
skipped_already_set: skipped,
}, null, 2))
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+268
View File
@@ -0,0 +1,268 @@
#!/usr/bin/env npx tsx
/**
* Backfill missing accounts into chart_of_accounts for ALL tenants.
*
* Finds accounts referenced by journal_entry_lines but not in chart_of_accounts,
* resolves metadata from BAS reference (primary) or SIE account mappings (fallback),
* and inserts them.
*
* Usage: npx tsx scripts/backfill-import-accounts.ts [--dry-run]
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
import { getBASReference } from '../lib/bookkeeping/bas-reference'
import { classifyAccount } from '../lib/bookkeeping/account-classifier'
import { computeSRUCode } from '../lib/bookkeeping/bas-data/sru-mapping'
const DRY_RUN = process.argv.includes('--dry-run')
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
// ---------------------------------------------------------------------------
// Non-BAS account overrides (company-specific sub-accounts not in BAS 2026)
// Only used when BAS reference and SIE source_name both miss.
// ---------------------------------------------------------------------------
interface AccountOverride {
account_name: string
account_type?: 'asset' | 'liability' | 'equity' | 'revenue' | 'expense' | 'untaxed_reserves'
normal_balance?: 'debit' | 'credit'
}
const NON_BAS_OVERRIDES: Record<string, AccountOverride> = {
'1402': { account_name: 'Förråd av varor' },
'1799': { account_name: 'Observationskonto' },
'2662': {
account_name: 'Kortfristig skuld Le comptoir',
account_type: 'liability',
normal_balance: 'credit',
},
'3041': { account_name: 'Försäljning tjänster 25% Sverige' },
'3051': { account_name: 'Försäljning varor 25% Sverige' },
'3052': { account_name: 'Försäljning varor 12% Sverige' },
'4020': { account_name: 'Alkoholskatt' },
'4056': { account_name: 'Inköp varor 25% EU' },
'4057': { account_name: 'Inköp varor 12% EU' },
'4071': { account_name: 'Lagerkostnader' },
'4072': { account_name: 'Inköp frakt 25% EU' },
'4990': { account_name: 'Lagerförändring' },
'4992': { account_name: 'Varor på väg' },
'6561': { account_name: 'GS1' },
'8300': { account_name: 'Ränteintäkter (gruppkonto)' },
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
async function getUsedAccountNumbers(userId: string): Promise<Set<string>> {
const usedSet = new Set<string>()
const PAGE_SIZE = 1000
let offset = 0
let hasMore = true
while (hasMore) {
const { data: batch, error } = await supabase
.from('journal_entry_lines')
.select('account_number, journal_entries!inner(user_id)')
.eq('journal_entries.user_id', userId)
.range(offset, offset + PAGE_SIZE - 1)
if (error) throw new Error(`Failed to fetch lines for ${userId}: ${error.message}`)
for (const row of batch ?? []) {
usedSet.add(row.account_number)
}
hasMore = (batch?.length ?? 0) === PAGE_SIZE
offset += PAGE_SIZE
}
return usedSet
}
async function getSIESourceNames(userId: string): Promise<Map<string, string>> {
const { data, error } = await supabase
.from('sie_account_mappings')
.select('source_account, source_name')
.eq('user_id', userId)
if (error) throw new Error(`Failed to fetch SIE mappings for ${userId}: ${error.message}`)
const map = new Map<string, string>()
for (const row of data ?? []) {
map.set(row.source_account, row.source_name)
}
return map
}
// ---------------------------------------------------------------------------
// Per-tenant backfill
// ---------------------------------------------------------------------------
async function backfillForUser(userId: string): Promise<number> {
console.log(`\n--- User ${userId} ---`)
// Get existing accounts
const { data: existingAccounts, error: existingError } = await supabase
.from('chart_of_accounts')
.select('account_number')
.eq('user_id', userId)
if (existingError) throw new Error(`Failed to fetch accounts: ${existingError.message}`)
const existingSet = new Set(existingAccounts?.map(a => a.account_number) ?? [])
// Get used account numbers from journal entries
const usedSet = await getUsedAccountNumbers(userId)
const missingAccounts = [...usedSet].filter(num => !existingSet.has(num)).sort()
if (missingAccounts.length === 0) {
console.log(' No missing accounts.')
return 0
}
console.log(` Found ${missingAccounts.length} missing accounts`)
// Get SIE source names as fallback for account naming
const sieNames = await getSIESourceNames(userId)
// Build insert rows
const rows = missingAccounts.map(accountNumber => {
const basRef = getBASReference(accountNumber)
if (basRef) {
return {
user_id: userId,
account_number: accountNumber,
account_name: basRef.account_name,
account_class: basRef.account_class,
account_group: basRef.account_group,
account_type: basRef.account_type,
normal_balance: basRef.normal_balance,
sru_code: basRef.sru_code ?? computeSRUCode(accountNumber),
k2_excluded: basRef.k2_excluded,
plan_type: 'full_bas' as const,
is_active: true,
is_system_account: false,
}
}
// Check hardcoded overrides (for company-specific accounts with known metadata)
const override = NON_BAS_OVERRIDES[accountNumber]
if (override) {
const classified = classifyAccount(accountNumber)
const accountType = override.account_type ?? classified.account_type
const normalBalance = override.normal_balance ?? classified.normal_balance
const classNum = parseInt(accountNumber.charAt(0), 10)
return {
user_id: userId,
account_number: accountNumber,
account_name: override.account_name,
account_class: classNum,
account_group: accountNumber.substring(0, 2),
account_type: accountType,
normal_balance: normalBalance,
sru_code: computeSRUCode(accountNumber),
k2_excluded: false,
plan_type: 'full_bas' as const,
is_active: true,
is_system_account: false,
}
}
// Fallback: use SIE source_name if available, otherwise derive
const sieName = sieNames.get(accountNumber)
const classNum = parseInt(accountNumber.charAt(0), 10)
if (sieName) {
console.warn(` INFO: Account ${accountNumber} not in BAS: using SIE name: "${sieName}"`)
} else {
console.warn(` WARNING: Account ${accountNumber} not in BAS or SIE: deriving all metadata`)
}
const classified = classifyAccount(accountNumber)
return {
user_id: userId,
account_number: accountNumber,
account_name: sieName ?? `Konto ${accountNumber}`,
account_class: classNum,
account_group: accountNumber.substring(0, 2),
account_type: classified.account_type,
normal_balance: classified.normal_balance,
sru_code: computeSRUCode(accountNumber),
k2_excluded: false,
plan_type: 'full_bas' as const,
is_active: true,
is_system_account: false,
}
})
// Log summary
const fromBAS = rows.filter(r => getBASReference(r.account_number)).length
const fromOverride = rows.filter(r => !getBASReference(r.account_number) && NON_BAS_OVERRIDES[r.account_number]).length
const fromFallback = rows.length - fromBAS - fromOverride
console.log(` ${fromBAS} from BAS, ${fromOverride} from overrides, ${fromFallback} from SIE/derived`)
for (const row of rows) {
console.log(` ${row.account_number}: ${row.account_name} (${row.account_type}, SRU: ${row.sru_code ?? 'none'})`)
}
if (DRY_RUN) {
console.log(` [DRY RUN] Would insert ${rows.length} accounts`)
return rows.length
}
const { error: insertError } = await supabase
.from('chart_of_accounts')
.insert(rows)
if (insertError) {
console.error(` Insert failed: ${insertError.message}`)
return 0
}
console.log(` Inserted ${rows.length} accounts`)
return rows.length
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
if (DRY_RUN) console.log('=== DRY RUN MODE ===\n')
// Find all users with missing accounts
const { data: allImportUsers, error } = await supabase
.from('sie_imports')
.select('user_id')
if (error) {
console.error('Failed to fetch SIE import users:', error.message)
process.exit(1)
}
const userIds = [...new Set(allImportUsers?.map(r => r.user_id) ?? [])]
console.log(`Found ${userIds.length} users with SIE imports`)
let totalInserted = 0
for (const userId of userIds) {
totalInserted += await backfillForUser(userId)
}
console.log(`\n=== Done: ${totalInserted} accounts ${DRY_RUN ? 'would be' : ''} inserted across ${userIds.length} users ===`)
}
main().catch(err => {
console.error(err)
process.exit(1)
})
+86
View File
@@ -0,0 +1,86 @@
#!/usr/bin/env npx tsx
/**
* Backfill for inbox items stranded as "linked" on an already-booked
* transaction (2026-08-12 report: booked verifikat stuck in "Att göra").
*
* The inbox list now derives "booked" from the matched transaction's own
* state, so the DISPLAY heals by itself. What this script repairs is the
* data left behind by the old write paths:
*
* - document_attachments.journal_entry_id: the underlag of a matched item
* was never linked to the verifikat that booked its transaction
* (BFL 5 kap 6 §). Linked here unless the document is already anchored.
* - invoice_inbox_items.created_journal_entry_id: stamped where the UNIQUE
* constraint allows (on a samlingsverifikat only one of N items can
* carry it; the rest stay derived).
*
* Since #1548 the same pass runs daily from
* app/api/extensions/invoice-inbox/underlag-reconcile/cron; this script is
* the manual, uncapped entry point to the shared implementation in
* lib/transactions/inbox-underlag-reconcile.ts (dry-run, or a full sweep
* after an incident). Idempotent: items already stamped are excluded by the
* query, and the propagation helper skips documents that are already
* anchored.
*
* Usage:
* npx tsx scripts/backfill-inbox-booked-underlag.ts # dry-run (default)
* npx tsx scripts/backfill-inbox-booked-underlag.ts --execute # apply
*
* DRY-RUN IS THE DEFAULT. Point NEXT_PUBLIC_SUPABASE_URL /
* SUPABASE_SERVICE_ROLE_KEY (.env.local) at staging first; prod only after
* explicit confirmation.
*
* Period locks: the document link goes through linkToJournalEntry, whose
* document_attachments.journal_entry_id UPDATE is guarded by the
* enforce_period_lock DB trigger. Table triggers fire for service-role
* writes too (service role bypasses RLS, never triggers), so a locked
* period fails that item with a logged error instead of writing into it.
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
import { reconcileStrandedInboxUnderlag } from '@/lib/transactions/inbox-underlag-reconcile'
const EXECUTE = process.argv.includes('--execute')
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
async function main() {
console.log(`Target: ${supabaseUrl}`)
console.log(EXECUTE ? 'MODE: EXECUTE (writing)' : 'MODE: dry-run (no writes)')
// Uncapped: the cron is bounded per run, a manual sweep is not.
const summary = await reconcileStrandedInboxUnderlag(supabase, {
execute: EXECUTE,
maxItems: Number.POSITIVE_INFINITY,
actorId: 'backfill-inbox-booked-underlag',
})
console.log(`Matched, unconsumed inbox items: ${summary.scanned}`)
console.log(JSON.stringify(summary, null, 2))
console.log(
EXECUTE
? `Repaired ${summary.repaired} item(s) across ${summary.companiesTouched} company/companies; ` +
`${summary.stillUnlinked} still unlinked, ${summary.unlinkedLocked} in locked periods, ` +
`${summary.anchoredElsewhere} anchored elsewhere.`
: `Would link ${summary.stillUnlinked} item(s) across ${summary.companiesTouched} company/companies; ` +
`${summary.unlinkedLocked} in locked periods need an unlock, ` +
`${summary.anchoredElsewhere} anchored elsewhere need a human.`,
)
if (!EXECUTE) console.log('Re-run with --execute to apply.')
if (summary.failures > 0) process.exitCode = 1
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+361
View File
@@ -0,0 +1,361 @@
#!/usr/bin/env npx tsx
/**
* Backfill for issue #2019: customer invoices settled through "Markera som
* betald" (or the Stripe payment sync) before settleInvoicePayment wrote the
* invoice_payments row.
*
* Why it matters: the kontantmetod bokslut cut-off reads invoice_payments
* ONLY (payment DATE, not remaining_amount), so a paid invoice without a row
* is re-booked as a fordran with vilande moms at year end, double-counting
* revenue and VAT. The same gap hides the payment from the "Betalningar"
* view and from the voucher -> invoice reference map.
*
* What it writes: one row per invoice, amount = paid_amount in invoice
* currency, payment_date = the payment voucher's entry_date (paid_at was the
* wall-clock registration time before 2026-08-02 and is not trusted),
* journal_entry_id = the single posted payment voucher, transaction_id NULL,
* notes tagged `backfill:#2019` so the whole run reverts with one statement:
*
* DELETE FROM invoice_payments WHERE notes LIKE 'backfill:#2019%';
*
* That revert is an emergency path for the window in which nothing has
* relied on the rows yet (BFL 5 kap 5 §). Rows are never written into a
* closed or locked fiscal period (see period_closed below), and once a
* bokslut cut-off has been posted from them the correction path is a storno
* of that cut-off verifikat, not a DELETE of the sub-ledger rows.
*
* Never guesses (lib/invoices/backfill-invoice-payment-rows.ts): an invoice
* with zero or several posted payment vouchers is listed, not written, and
* so is one whose existing rows sum to less than paid_amount (a pre-fix
* manual partial next to a bank-matched one) or whose voucher booked a
* different amount than the header says, or whose payment date falls in a
* closed or locked fiscal period (a filed bokslut or deklaration may rely on
* that year's cut-off). Idempotent: invoices whose rows cover paid_amount
* are excluded.
*
* Every executed run is recorded in behandlingshistorik (one
* InvoicePaymentRowBackfilled event per company, BFL 5 kap 11 §): the rows
* feed the bokslut cut-off, so the bulk write is a change to processing.
*
* Usage:
* npx tsx scripts/backfill-invoice-payment-rows.ts # dry-run (default)
* npx tsx scripts/backfill-invoice-payment-rows.ts --execute # apply
* npx tsx scripts/backfill-invoice-payment-rows.ts --company <id> # one company only
* npx tsx scripts/backfill-invoice-payment-rows.ts --verbose # list every skipped invoice
*
* DRY-RUN IS THE DEFAULT. Point NEXT_PUBLIC_SUPABASE_URL /
* SUPABASE_SERVICE_ROLE_KEY (.env.local) at staging first; prod only after
* explicit confirmation. Service role bypasses RLS but not the
* payment_company_consistency trigger, so a row can never land on the wrong
* tenant.
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { randomUUID } from 'node:crypto'
import { createClient } from '@supabase/supabase-js'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { roundOre } from '@/lib/money'
import { appendProcessingHistoryWithClient } from '@/lib/processing-history/append'
import {
BACKFILL_NOTES_TAG,
PAYMENT_VOUCHER_SOURCE_TYPES,
planInvoicePaymentBackfill,
settlementSekFromLines,
type BackfillInvoice,
type BackfillPaymentRow,
type BackfillSkipReason,
type BackfillVoucher,
type ExistingPaymentRows,
} from '@/lib/invoices/backfill-invoice-payment-rows'
const EXECUTE = process.argv.includes('--execute')
const VERBOSE = process.argv.includes('--verbose')
const companyArgIndex = process.argv.indexOf('--company')
const COMPANY_FILTER = companyArgIndex >= 0 ? process.argv[companyArgIndex + 1] : null
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
if (companyArgIndex >= 0 && !COMPANY_FILTER) {
console.error('--company needs a company id')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
const CHUNK = 200
const INSERT_BATCH = 100
function chunk<T>(items: T[], size: number): T[][] {
const out: T[][] = []
for (let i = 0; i < items.length; i += size) out.push(items.slice(i, i + size))
return out
}
async function main() {
console.log(`Target: ${supabaseUrl}`)
console.log(EXECUTE ? 'MODE: EXECUTE (writing)' : 'MODE: dry-run (no writes)')
if (COMPANY_FILTER) console.log(`Company filter: ${COMPANY_FILTER}`)
// 1. Candidate invoices: paid or partially paid with money received.
const invoices = await fetchAllRows<BackfillInvoice>(({ from, to }) => {
let q = supabase
.from('invoices')
.select(
'id, company_id, user_id, invoice_number, status, document_type, currency, exchange_rate, paid_amount, paid_at',
)
.in('status', ['paid', 'partially_paid'])
.gt('paid_amount', 0)
if (COMPANY_FILTER) q = q.eq('company_id', COMPANY_FILTER)
return q.order('id', { ascending: true }).range(from, to)
})
console.log(`Paid / partially paid invoices with paid_amount > 0: ${invoices.length}`)
const invoiceIds = invoices.map((i) => i.id)
// 2. Existing sub-ledger rows and posted payment vouchers, per invoice.
const existingRows = new Map<string, ExistingPaymentRows>()
const vouchersByInvoice = new Map<string, BackfillVoucher[]>()
for (const ids of chunk(invoiceIds, CHUNK)) {
const rows = await fetchAllRows<{ id: string; invoice_id: string; amount: number | null }>(
({ from, to }) =>
supabase
.from('invoice_payments')
.select('id, invoice_id, amount')
.in('invoice_id', ids)
.order('id', { ascending: true })
.range(from, to),
)
for (const r of rows) {
const acc = existingRows.get(r.invoice_id) ?? { count: 0, sum: 0 }
acc.count += 1
acc.sum = roundOre(acc.sum + Number(r.amount ?? 0))
existingRows.set(r.invoice_id, acc)
}
const vouchers = await fetchAllRows<BackfillVoucher>(({ from, to }) =>
supabase
.from('journal_entries')
.select('id, source_id, source_type, status, entry_date')
.in('source_id', ids)
.in('source_type', [...PAYMENT_VOUCHER_SOURCE_TYPES])
.eq('status', 'posted')
.order('id', { ascending: true })
.range(from, to),
)
const voucherIds = vouchers.map((v) => v.id)
const lines = voucherIds.length
? await fetchAllRows<{
journal_entry_id: string
account_number: string
debit_amount: number | null
credit_amount: number | null
}>(({ from, to }) =>
supabase
.from('journal_entry_lines')
.select('journal_entry_id, account_number, debit_amount, credit_amount')
.in('journal_entry_id', voucherIds)
.order('id', { ascending: true })
.range(from, to),
)
: []
const linesByVoucher = new Map<string, typeof lines>()
for (const l of lines) {
const list = linesByVoucher.get(l.journal_entry_id) ?? []
list.push(l)
linesByVoucher.set(l.journal_entry_id, list)
}
for (const v of vouchers) {
if (!v.source_id) continue
v.settlement_sek = settlementSekFromLines(linesByVoucher.get(v.id) ?? [])
const list = vouchersByInvoice.get(v.source_id) ?? []
list.push(v)
vouchersByInvoice.set(v.source_id, list)
}
}
// 3. Closed / locked fiscal periods per company: a row dated into one is
// reported, never written.
const companyIds = Array.from(new Set(invoices.map((i) => i.company_id)))
const closedRanges = new Map<string, Array<{ start: string; end: string }>>()
for (const ids of chunk(companyIds, CHUNK)) {
const periods = await fetchAllRows<{
company_id: string
period_start: string
period_end: string
is_closed: boolean
locked_at: string | null
}>(({ from, to }) =>
supabase
.from('fiscal_periods')
.select('company_id, period_start, period_end, is_closed, locked_at')
.in('company_id', ids)
.order('id', { ascending: true })
.range(from, to),
)
for (const fp of periods) {
if (!fp.is_closed && !fp.locked_at) continue
const list = closedRanges.get(fp.company_id) ?? []
list.push({ start: fp.period_start, end: fp.period_end })
closedRanges.set(fp.company_id, list)
}
}
const isPeriodClosedFor = (companyId: string) => (date: string) =>
(closedRanges.get(companyId) ?? []).some((r) => date >= r.start && date <= r.end)
// 4. Plan.
const toInsert: Array<{ invoice: BackfillInvoice; row: BackfillPaymentRow }> = []
const skipped = new Map<BackfillSkipReason, BackfillInvoice[]>()
for (const invoice of invoices) {
const plan = planInvoicePaymentBackfill(
invoice,
vouchersByInvoice.get(invoice.id) ?? [],
existingRows.get(invoice.id) ?? { count: 0, sum: 0 },
{ isPeriodClosed: isPeriodClosedFor(invoice.company_id) },
)
if (plan.kind === 'insert') {
toInsert.push({ invoice, row: plan.row })
} else {
const list = skipped.get(plan.reason) ?? []
list.push(invoice)
skipped.set(plan.reason, list)
}
}
console.log('')
console.log(`Rows to insert: ${toInsert.length}`)
const perCompany = new Map<string, number>()
for (const { row } of toInsert) perCompany.set(row.company_id, (perCompany.get(row.company_id) ?? 0) + 1)
for (const [companyId, n] of perCompany) console.log(` ${companyId}: ${n}`)
for (const { invoice, row } of toInsert) {
console.log(
` + ${invoice.company_id} ${invoice.invoice_number ?? invoice.id} ` +
`${row.amount} ${row.currency} on ${row.payment_date} -> ${row.journal_entry_id}`,
)
}
console.log('')
console.log('Skipped:')
for (const [reason, list] of skipped) {
console.log(` ${reason}: ${list.length}`)
// The reasons that need a human: a paid invoice with no voucher to hang
// the row on, several vouchers whose split is unknown, or existing rows
// that do not cover paid_amount. On prod the first group is dominated by
// imported history (paid long before the company came here; thousands of
// rows), so it is summarised per company unless --verbose asks for every
// invoice.
if (
reason === 'no_payment_voucher' ||
reason === 'multiple_payment_vouchers' ||
reason === 'rows_short' ||
reason === 'voucher_amount_mismatch' ||
reason === 'voucher_amount_unverifiable' ||
reason === 'period_closed'
) {
if (VERBOSE) {
for (const inv of list) {
console.log(` ${inv.company_id} ${inv.invoice_number ?? inv.id} (${inv.status}, paid ${inv.paid_amount})`)
}
} else {
const byCompany = new Map<string, number>()
for (const inv of list) byCompany.set(inv.company_id, (byCompany.get(inv.company_id) ?? 0) + 1)
for (const [companyId, n] of byCompany) console.log(` ${companyId}: ${n}`)
}
}
}
if (!EXECUTE) {
console.log('')
console.log('Re-run with --execute to apply.')
return
}
// 5. Write in batches. A unique-index collision (a row written between the
// read and this insert) fails the batch loudly rather than being skipped.
let inserted = 0
for (const batch of chunk(toInsert, INSERT_BATCH)) {
const { error } = await supabase.from('invoice_payments').insert(batch.map((b) => b.row))
if (error) {
console.error(`Insert failed after ${inserted} rows: ${error.code ?? ''} ${error.message}`)
process.exitCode = 1
return
}
inserted += batch.length
}
console.log('')
console.log(`Inserted ${inserted} row(s) tagged "${BACKFILL_NOTES_TAG}".`)
console.log(`Rollback: DELETE FROM invoice_payments WHERE notes LIKE '${BACKFILL_NOTES_TAG}%';`)
// 6. Behandlingshistorik: one event per company naming every row written.
// The rows feed the bokslut cut-off, so the run is a change to processing
// (BFL 5 kap 11 §, BFNAR 2013:2 p. 9.16). Rows without their change-log
// entry must not stay: if the append fails, that company's rows from this
// run are deleted again (by id, tag-guarded) so data and audit trail move
// together, and the company is listed for a re-run.
const runId = randomUUID()
const byCompany = new Map<string, Array<{ invoice: BackfillInvoice; row: BackfillPaymentRow }>>()
for (const item of toInsert) {
const list = byCompany.get(item.row.company_id) ?? []
list.push(item)
byCompany.set(item.row.company_id, list)
}
let appended = 0
const rolledBack: string[] = []
for (const [companyId, items] of byCompany) {
try {
await appendProcessingHistoryWithClient(supabase, {
companyId,
correlationId: runId,
aggregateType: 'System',
aggregateId: companyId,
eventType: 'InvoicePaymentRowBackfilled',
payload: {
source: 'backfill-invoice-payment-rows',
issue: 2019,
notes_tag: BACKFILL_NOTES_TAG,
row_count: items.length,
invoice_ids: items.map((i) => i.invoice.id),
journal_entry_ids: items.map((i) => i.row.journal_entry_id),
},
actor: { type: 'system', id: 'backfill-invoice-payment-rows' },
occurredAt: new Date(),
})
appended += 1
} catch (err) {
console.error(
`processing_history append failed for company ${companyId}: ` +
(err instanceof Error ? err.message : String(err)),
)
const { error: rollbackError } = await supabase
.from('invoice_payments')
.delete()
.eq('company_id', companyId)
.in('invoice_id', items.map((i) => i.invoice.id))
.like('notes', `${BACKFILL_NOTES_TAG}%`)
if (rollbackError) {
console.error(
` rollback of ${items.length} row(s) for ${companyId} FAILED: ${rollbackError.message}. ` +
`Delete by hand: DELETE FROM invoice_payments WHERE company_id = '${companyId}' AND notes LIKE '${BACKFILL_NOTES_TAG}%';`,
)
} else {
console.error(` rolled back ${items.length} row(s) for ${companyId}; re-run the script for this company.`)
rolledBack.push(companyId)
}
process.exitCode = 1
}
}
console.log(`Behandlingshistorik: ${appended}/${byCompany.size} company event(s) appended (run ${runId}).`)
if (rolledBack.length > 0) {
console.log(`Rolled back (no audit event): ${rolledBack.join(', ')}`)
}
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env npx tsx
/**
* Backfill SIE file archival for existing imports.
*
* Accepts a directory of SIE files, computes SHA-256 hashes, matches against
* sie_imports.file_hash, uploads to Supabase Storage, and populates
* file_storage_path.
*
* Usage: npx tsx scripts/backfill-sie-files.ts <directory-of-sie-files>
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
import { readdir, readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { createHash } from 'node:crypto'
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
async function calculateFileHash(content: string): Promise<string> {
const hash = createHash('sha256')
hash.update(content)
return hash.digest('hex')
}
async function main() {
const dir = process.argv[2]
if (!dir) {
console.error('Usage: npx tsx scripts/backfill-sie-files.ts <directory-of-sie-files>')
process.exit(1)
}
// 1. Read all SIE files from directory
const files = await readdir(dir)
const sieFiles = files.filter(f => f.toLowerCase().endsWith('.se') || f.toLowerCase().endsWith('.si'))
if (sieFiles.length === 0) {
console.error(`No .se/.si files found in ${dir}`)
process.exit(1)
}
console.log(`Found ${sieFiles.length} SIE files in ${dir}`)
// 2. Get all existing imports without file_storage_path
const { data: imports, error: importError } = await supabase
.from('sie_imports')
.select('id, user_id, file_hash, filename, file_storage_path')
.is('file_storage_path', null)
if (importError) {
console.error('Failed to fetch imports:', importError.message)
process.exit(1)
}
if (!imports || imports.length === 0) {
console.log('No imports need backfilling.')
return
}
console.log(`Found ${imports.length} imports without archived files`)
// Build hash→import mapping
const hashToImport = new Map<string, typeof imports[number]>()
for (const imp of imports) {
if (imp.file_hash) {
hashToImport.set(imp.file_hash, imp)
}
}
// 3. Match files by hash and upload
let matched = 0
let uploaded = 0
for (const filename of sieFiles) {
const filePath = join(dir, filename)
const content = await readFile(filePath, 'utf-8')
const hash = await calculateFileHash(content)
const imp = hashToImport.get(hash)
if (!imp) {
console.log(` ${filename}: no matching import (hash: ${hash.substring(0, 12)}...)`)
continue
}
matched++
console.log(` ${filename} → import ${imp.id} (${imp.filename})`)
// Upload to storage
const storagePath = `${imp.user_id}/${imp.id}.se`
const fileBlob = new Blob([content], { type: 'text/plain; charset=cp437' })
const { error: uploadError } = await supabase.storage
.from('sie-files')
.upload(storagePath, fileBlob, { upsert: false })
if (uploadError) {
console.error(` Upload failed: ${uploadError.message}`)
continue
}
// Update import record
const { error: updateError } = await supabase
.from('sie_imports')
.update({ file_storage_path: storagePath })
.eq('id', imp.id)
if (updateError) {
console.error(` DB update failed: ${updateError.message}`)
continue
}
uploaded++
console.log(` Archived to ${storagePath}`)
}
console.log(`\nDone: ${matched} matched, ${uploaded} uploaded, ${sieFiles.length - matched} unmatched`)
}
main().catch(err => {
console.error(err)
process.exit(1)
})
+160
View File
@@ -0,0 +1,160 @@
#!/usr/bin/env npx tsx
/**
* Backfill automatic tax deadlines for the installed base.
*
* Root cause (bug): tax deadlines only ever regenerated when a tax-relevant
* settings field CHANGED value (app/api/settings/route.ts, gated on
* the since-removed didTaxFieldsChange). Companies fill these fields once at onboarding, so a
* later save changed nothing and generated nothing. The annual cron
* (generateNewYearDeadlines) is the only unconditional trigger and runs Jan 2,
* so the installed base sat empty. Result before this backfill: only ~5 of ~776
* real companies had system-generated deadlines.
*
* This script runs the REAL generator (generateTaxDeadlinesForUser) so the
* backfilled rows are byte-identical to what the app produces. It targets ONLY
* non-sandbox companies that currently have ZERO source='system' deadlines, so
* it can never reset is_completed/status on a company that already has progress.
*
* Known gap (intentionally NOT covered here): moms_period='yearly' has no
* deadline config yet, so ~295 annual VAT filers will not get a momsdeklaration
* deadline until moms_yearly ships. Every other applicable deadline is created.
*
* Usage:
* npx tsx scripts/backfill-tax-deadlines.ts --dry-run # report only (default-safe)
* npx tsx scripts/backfill-tax-deadlines.ts --apply # write to the database
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
import {
DEADLINE_SETTINGS_SELECT,
generateTaxDeadlinesForUser,
toDeadlineSettings,
} from '../lib/tax/deadline-generator'
import type { CompanySettingsForDeadlines } from '../lib/tax/deadline-config'
const APPLY = process.argv.includes('--apply')
const DRY_RUN = !APPLY
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
const PAGE = 1000
interface SettingsRow extends Partial<CompanySettingsForDeadlines> {
company_id: string
is_sandbox: boolean | null
}
async function fetchAllSettings(): Promise<SettingsRow[]> {
const rows: SettingsRow[] = []
let from = 0
for (;;) {
const { data, error } = await supabase
.from('company_settings')
.select(`${DEADLINE_SETTINGS_SELECT}, is_sandbox`)
.order('company_id', { ascending: true })
.range(from, from + PAGE - 1)
if (error) {
console.error('Failed to fetch company_settings', error)
process.exit(1)
}
if (!data || data.length === 0) break
rows.push(...(data as SettingsRow[]))
if (data.length < PAGE) break
from += PAGE
}
return rows
}
async function fetchCompaniesWithSystemDeadlines(): Promise<Set<string>> {
const ids = new Set<string>()
let from = 0
for (;;) {
const { data, error } = await supabase
.from('deadlines')
.select('company_id')
.eq('source', 'system')
.order('company_id', { ascending: true })
.range(from, from + PAGE - 1)
if (error) {
console.error('Failed to fetch companies with system deadlines', error)
process.exit(1)
}
if (!data || data.length === 0) break
for (const row of data as Array<{ company_id: string }>) ids.add(row.company_id)
if (data.length < PAGE) break
from += PAGE
}
return ids
}
async function main() {
const all = await fetchAllSettings()
const realCompanies = all.filter((s) => !s.is_sandbox)
const withDeadlines = await fetchCompaniesWithSystemDeadlines()
let scanned = 0
let alreadyHad = 0
let missingEntityType = 0
let generatedCompanies = 0
let generatedRows = 0
const errors: Array<{ company_id: string; error: string }> = []
for (const s of realCompanies) {
scanned++
if (!s.entity_type) {
// Generator gates every config on entity_type; nothing to create.
missingEntityType++
continue
}
if (withDeadlines.has(s.company_id)) {
alreadyHad++
continue
}
const settings = toDeadlineSettings(s)
if (DRY_RUN) {
// In dry-run we cannot cheaply know the row count without inserting, so we
// just report the company as a target.
generatedCompanies++
continue
}
try {
const result = await generateTaxDeadlinesForUser(supabase, s.company_id, settings)
if (result.created > 0) {
generatedCompanies++
generatedRows += result.created
}
} catch (err) {
errors.push({ company_id: s.company_id, error: err instanceof Error ? err.message : String(err) })
}
}
console.log(JSON.stringify({
mode: DRY_RUN ? 'dry-run' : 'apply',
real_companies_scanned: scanned,
already_had_system_deadlines: alreadyHad,
skipped_missing_entity_type: missingEntityType,
companies_generated: generatedCompanies,
rows_generated: DRY_RUN ? null : generatedRows,
errors,
}, null, 2))
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+199
View File
@@ -0,0 +1,199 @@
/**
* Backtest the auto-booking cascade against REAL, already-booked transactions.
*
* READ-ONLY. For each recent booked expense transaction it: reconstructs the
* candidate slate + underlag from prod, runs the real selector (against the
* configured AI backend), and compares the model's proposed account to the
* account the human actually booked (the expense debit line). Prints per-row
* detail + an aggregate: overall accuracy, and — the honest signal — accuracy
* on the cases where the top deterministic candidate was NOT the answer, i.e.
* where the model had to add value.
*
* cp ~/erp-base/.env.local . # prod DB + Bedrock, read-only
* npx tsx scripts/backtest-categorize.ts [N]
* rm .env.local
*
* Consent: only companies with company_settings.data_analysis_opt_in = true
* are read (#1346). This script goes beyond booking outcomes: it reads each
* transaction's description, merchant name and matched underlag (via
* gatherUnderlag) and sends them to the model again, so the consent copy in
* messages/*.json (data_analysis.settings_toggle_help) explicitly names
* "evaluation runs" with exactly those inputs. Do not add inputs here that
* the copy does not name. Nobody is opted in by default, so an empty run is
* the expected state until an admin flips the toggle in Inställningar > Företag.
*
* Leakage caveat: a known vendor's counterparty template may already reflect
* the very booking under test, inflating the "deterministic nailed it" segment.
* The "model had to decide" segment below is the leakage-free measure.
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
const N = Number(process.argv[2] ?? 50)
const CONCURRENCY = 4
async function main() {
const { createClient } = await import('@supabase/supabase-js')
// Import after dotenv so lib/ai resolves the provider/model from .env.local.
const { gatherCandidates } = await import('../lib/agent/categorize/candidates')
const { gatherUnderlag } = await import('../lib/agent/categorize/underlag')
const { selectAccount } = await import('../lib/agent/categorize/select-account')
const { chunkCompanyIds, listDataAnalysisOptedInCompanyIds } = await import('../lib/company/data-analysis')
const url = process.env.NEXT_PUBLIC_SUPABASE_URL!
const key = process.env.SUPABASE_SERVICE_ROLE_KEY!
const supabase = createClient(url, key)
// Consent gate (#1346): only companies that opted in to data analysis.
const optedInIds = await listDataAnalysisOptedInCompanyIds(supabase)
if (optedInIds.length === 0) {
console.log('\nNo company has opted in to data analysis (company_settings.data_analysis_opt_in). Nothing to backtest.')
return
}
// Recent booked expense transactions with a counterparty. Queried per chunk
// of company ids (`.in()` lives in the GET query string), then merged and
// re-cut to the N most recent overall.
type Tx = {
id: string
company_id: string
merchant_name: string | null
description: string | null
original_description: string | null
amount: number
date: string
currency: string | null
document_id: string | null
journal_entry_id: string | null
created_at: string
}
const candidatesByChunk: Tx[] = []
for (const chunk of chunkCompanyIds(optedInIds)) {
const { data: txs, error } = await supabase
.from('transactions')
.select('id, company_id, merchant_name, description, original_description, amount, date, currency, document_id, journal_entry_id, created_at')
.in('company_id', chunk)
.not('journal_entry_id', 'is', null)
.lt('amount', 0)
.eq('is_business', true)
.not('merchant_name', 'is', null)
.order('created_at', { ascending: false })
.limit(N)
if (error) throw error
candidatesByChunk.push(...((txs ?? []) as Tx[]))
}
const rows = candidatesByChunk
.sort((a, b) => (a.created_at < b.created_at ? 1 : a.created_at > b.created_at ? -1 : 0))
.slice(0, N)
console.log(`\nBacktesting ${rows.length} booked transactions on ${process.env.BEDROCK_MODEL_ID ?? process.env.AI_MODEL ?? 'the configured model'}…\n`)
// Ground-truth debit account per journal entry (expense line, not cash/VAT).
const jeIds = rows.map((r) => r.journal_entry_id).filter(Boolean) as string[]
const truth = new Map<string, string>()
for (let i = 0; i < jeIds.length; i += 100) {
const { data: lines } = await supabase
.from('journal_entry_lines')
.select('journal_entry_id, account_number, debit_amount')
.in('journal_entry_id', jeIds.slice(i, i + 100))
for (const l of (lines ?? []) as { journal_entry_id: string; account_number: string; debit_amount: number | null }[]) {
const acct = l.account_number ?? ''
if (!(Number(l.debit_amount) > 0)) continue
if (acct.startsWith('19') || acct.startsWith('26') || acct.startsWith('264')) continue // cash + VAT
const cur = truth.get(l.journal_entry_id)
if (!cur) truth.set(l.journal_entry_id, acct) // first expense debit line
}
}
const companyCtx = new Map<string, { entityType: string; vatRegistered: boolean }>()
async function ctxFor(companyId: string) {
const hit = companyCtx.get(companyId)
if (hit) return hit
const [{ data: c }, { data: s }] = await Promise.all([
supabase.from('companies').select('entity_type').eq('id', companyId).maybeSingle(),
supabase.from('company_settings').select('vat_registered').eq('company_id', companyId).maybeSingle(),
])
const ctx = { entityType: (c?.entity_type as string) ?? 'enskild_firma', vatRegistered: !!s?.vat_registered }
companyCtx.set(companyId, ctx)
return ctx
}
interface Result {
merchant: string
truth: string | null
proposed: string | null
conf: number
fromCandidate: boolean
topCandidate: string | null
hadUnderlag: boolean
correct: boolean | null
}
const results: Result[] = []
async function run(r: (typeof rows)[number]) {
const gt = r.journal_entry_id ? truth.get(r.journal_entry_id) ?? null : null
if (!gt) return
const ctx = await ctxFor(r.company_id)
const [candidates, underlag] = await Promise.all([
gatherCandidates(supabase as never, r.company_id, r as never),
gatherUnderlag(supabase as never, r.company_id, r.id, r.document_id),
])
const sel = await selectAccount({
transaction: {
merchantName: r.merchant_name,
description: r.description ?? r.original_description ?? '',
amount: r.amount,
date: r.date,
currency: r.currency,
},
underlag,
candidates,
entityType: ctx.entityType as never,
vatRegistered: ctx.vatRegistered,
samples: 1,
})
results.push({
merchant: (r.merchant_name ?? '').slice(0, 22),
truth: gt,
proposed: sel.account,
conf: sel.confidence,
fromCandidate: sel.fromCandidate,
topCandidate: candidates[0]?.account ?? null,
hadUnderlag: underlag.length > 0,
correct: sel.account ? sel.account === gt : null,
})
}
for (let i = 0; i < rows.length; i += CONCURRENCY) {
await Promise.all(rows.slice(i, i + CONCURRENCY).map((r) => run(r).catch((e) => console.error('row failed', e?.message))))
process.stdout.write('.')
}
console.log('\n')
// Per-row.
for (const r of results) {
const mark = r.correct === null ? '·' : r.correct ? '✓' : '✗'
console.log(
`${mark} ${r.merchant.padEnd(22)} truth=${(r.truth ?? '—').padEnd(6)} pick=${(r.proposed ?? 'review').padEnd(6)} ` +
`conf=${r.conf.toFixed(2)} ${r.fromCandidate ? 'cand' : 'cat '} ${r.hadUnderlag ? 'underlag' : ' '} topcand=${r.topCandidate ?? '—'}`,
)
}
const scored = results.filter((r) => r.correct !== null)
const acc = (xs: Result[]) => (xs.length ? (xs.filter((r) => r.correct).length / xs.length) : 0)
const detWrong = scored.filter((r) => r.topCandidate !== r.truth) // deterministic top candidate was NOT the answer
const withU = scored.filter((r) => r.hadUnderlag)
console.log('\n──────── summary ────────')
console.log(`scored: ${scored.length} / ${results.length} (rest = needs_review)`)
console.log(`overall accuracy: ${(acc(scored) * 100).toFixed(1)}%`)
console.log(` model-decided (top candidate ≠ truth): ${(acc(detWrong) * 100).toFixed(1)}% (n=${detWrong.length}) ← leakage-free`)
console.log(` with underlag: ${(acc(withU) * 100).toFixed(1)}% (n=${withU.length})`)
console.log(`needs_review rate: ${(((results.length - scored.length) / Math.max(1, results.length)) * 100).toFixed(1)}%`)
console.log(`reliability (conf ≥0.8): ${(acc(scored.filter((r) => r.conf >= 0.8)) * 100).toFixed(1)}% (n=${scored.filter((r) => r.conf >= 0.8).length})`)
console.log(`reliability (conf <0.5): ${(acc(scored.filter((r) => r.conf < 0.5)) * 100).toFixed(1)}% (n=${scored.filter((r) => r.conf < 0.5).length})`)
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+252
View File
@@ -0,0 +1,252 @@
/**
* One-off cleanup: balance every unbalanced voucher in the BL test company
* behind the given consent, via BL's
* PUT /journal/ledgerentry/{journalId}/{journalEntryId}/{journalEntryDate}.
*
* BL refuses DELETE on anything but the last voucher of a series, so instead:
* method A (preferred): add a counter ledger entry against 0099
* "Konvertering" with amount = -diff (probe: entityId 0 = new line)
* method B (fallback): update the voucher's first ledger entry so the
* voucher sums to zero
*
* Each method is probed on ONE voucher and verified with a GET before the
* mass run. Empty vouchers (0 lines) are left alone: they pass validation.
*
* Run: npx tsx --env-file=.env --env-file=.env.local scripts/bl-balance-broken.ts <consentId>
*/
import { createClient } from '@supabase/supabase-js'
import { fetchBjornLundenToken } from '@/lib/providers/bjornlunden/oauth'
import { fetchProviderSieFiles } from '@/extensions/general/arcim-migration/lib/sie-fetcher'
import { parseSIEFile, validateSIEFile } from '@/lib/import/sie-parser'
const BL_BASE_URL = 'https://apigateway.blinfo.se/bla-api/v1/sp'
const DELAY_MS = 125 // ~8 req/s, under BL's 10 req/s limit
const consentId = process.argv[2]
if (!consentId) {
console.error('Usage: npx tsx --env-file=.env --env-file=.env.local scripts/bl-balance-broken.ts <consentId>')
console.error('Refusing to run without an explicit consentId: this script writes vouchers to a live BL company.')
process.exit(1)
}
interface BLLedgerEntry {
entityId: number
accountId: string
amount: number
costBearerId: string
costCenterId: string
date: string
id: number
line: number
projectId: string
quantity: number
text: string
accrual: boolean
}
interface BLJournalEntry {
entityId: number
journalId: string
journalEntryId: number
journalEntryDate: string
journalEntryText: string
ledgerEntries: BLLedgerEntry[]
}
const round2 = (n: number) => Math.round(n * 100) / 100
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
function entrySum(e: BLJournalEntry): number {
return round2(e.ledgerEntries.reduce((s, l) => s + (l.amount ?? 0), 0))
}
function headers(accessToken: string, userKey: string): Record<string, string> {
return {
Authorization: `Bearer ${accessToken}`,
'User-Key': userKey,
'Content-Type': 'application/json',
Accept: 'application/json',
}
}
async function fetchAllEntries(accessToken: string, userKey: string): Promise<BLJournalEntry[]> {
const all: BLJournalEntry[] = []
let page = 1
let totalPages = 1
while (page <= totalPages) {
const params = new URLSearchParams({ page: String(page), rows: '500' })
const res = await fetch(`${BL_BASE_URL}/journal/entry/batch?${params}`, {
headers: headers(accessToken, userKey),
signal: AbortSignal.timeout(60_000),
})
if (!res.ok) throw new Error(`batch page ${page} failed: HTTP ${res.status}`)
const body = await res.json() as { pageRequested: number; totalPages: number; data: BLJournalEntry[] }
all.push(...(body.data ?? []))
totalPages = body.totalPages ?? 1
page++
await sleep(DELAY_MS)
}
return all
}
async function fetchOne(
accessToken: string,
userKey: string,
v: BLJournalEntry,
): Promise<BLJournalEntry> {
const res = await fetch(
`${BL_BASE_URL}/journal/entry/${encodeURIComponent(v.journalId)}/${v.journalEntryId}/${v.journalEntryDate}`,
{ headers: headers(accessToken, userKey), signal: AbortSignal.timeout(30_000) },
)
if (!res.ok) throw new Error(`GET single entry failed: HTTP ${res.status}`)
return res.json() as Promise<BLJournalEntry>
}
async function putLedgerEntry(
accessToken: string,
userKey: string,
v: BLJournalEntry,
body: Partial<BLLedgerEntry>,
): Promise<{ ok: boolean; status: number; body: string }> {
const res = await fetch(
`${BL_BASE_URL}/journal/ledgerentry/${encodeURIComponent(v.journalId)}/${v.journalEntryId}/${v.journalEntryDate}`,
{
method: 'PUT',
headers: headers(accessToken, userKey),
body: JSON.stringify(body),
signal: AbortSignal.timeout(30_000),
},
)
const text = await res.text().catch(() => '')
return { ok: res.ok, status: res.status, body: text.slice(0, 300) }
}
function addLineBody(v: BLJournalEntry, diff: number): Partial<BLLedgerEntry> {
const maxLine = v.ledgerEntries.reduce((m, l) => Math.max(m, l.line ?? 0), 0)
return {
entityId: 0,
accountId: '0099',
amount: round2(-diff),
costBearerId: '',
costCenterId: '',
date: v.journalEntryDate,
line: maxLine + 1,
projectId: '',
quantity: 0,
text: 'Balansering vid migrering (test)',
accrual: false,
}
}
function adjustFirstLineBody(v: BLJournalEntry, diff: number): Partial<BLLedgerEntry> {
const first = v.ledgerEntries[0]!
return { ...first, amount: round2(first.amount - diff) }
}
async function main() {
// Either a User-Key GUID passed directly as the 2nd arg, or resolved from
// the consent in the 1st arg (consents churn on every wizard reconnect).
let userKey = process.argv[3]
if (!userKey) {
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
)
const { data: tokens, error } = await supabase
.from('provider_consent_tokens')
.select('provider_company_id')
.eq('consent_id', consentId)
.limit(1)
if (error || !tokens?.length) throw new Error(`no tokens for consent: ${error?.message ?? 'not found'}`)
userKey = tokens[0]!.provider_company_id as string
}
const token = await fetchBjornLundenToken(
process.env.BJORN_LUNDEN_CLIENT_ID!,
process.env.BJORN_LUNDEN_CLIENT_SECRET!,
)
const at = token.access_token
console.log('Fetching all journal entries via batch API...')
const entries = await fetchAllEntries(at, userKey)
const unbalanced = entries.filter((e) => e.ledgerEntries.length > 0 && Math.abs(entrySum(e)) > 0.01)
const empty = entries.filter((e) => e.ledgerEntries.length === 0).length
console.log(`${entries.length} vouchers total: ${unbalanced.length} unbalanced (will fix), ${empty} empty (left alone)`)
if (unbalanced.length === 0) {
console.log('Nothing to do.')
return
}
// ── Probe method A (add 0099 line) on one voucher ────────────────
const probe = unbalanced[0]!
const probeDiff = entrySum(probe)
console.log(`Probe: ${probe.journalId}${probe.journalEntryId} (${probe.journalEntryDate}, diff ${probeDiff})`)
let method: 'add' | 'adjust' | null = null
const addResult = await putLedgerEntry(at, userKey, probe, addLineBody(probe, probeDiff))
if (addResult.ok) {
const after = await fetchOne(at, userKey, probe)
if (Math.abs(entrySum(after)) <= 0.01) {
method = 'add'
console.log('Method A (add 0099 counter-line) works: verified balanced via GET.')
} else {
console.log(`Method A responded OK but voucher still sums to ${entrySum(after)}: trying method B.`)
}
} else {
console.log(`Method A refused (HTTP ${addResult.status}): ${addResult.body}: trying method B.`)
}
if (!method) {
const fresh = await fetchOne(at, userKey, probe)
const freshDiff = entrySum(fresh)
if (Math.abs(freshDiff) > 0.01) {
const adjResult = await putLedgerEntry(at, userKey, fresh, adjustFirstLineBody(fresh, freshDiff))
if (!adjResult.ok) {
console.error(`ABORT: method B also refused (HTTP ${adjResult.status}): ${adjResult.body}`)
process.exit(1)
}
const after = await fetchOne(at, userKey, fresh)
if (Math.abs(entrySum(after)) > 0.01) {
console.error(`ABORT: method B responded OK but voucher still sums to ${entrySum(after)}`)
process.exit(1)
}
method = 'adjust'
console.log('Method B (adjust first line) works: verified balanced via GET.')
}
}
// ── Mass run ─────────────────────────────────────────────────────
const failures: { voucher: string; status: number; body: string }[] = []
let done = 1
for (const v of unbalanced.slice(1)) {
await sleep(DELAY_MS)
const diff = entrySum(v)
const body = method === 'add' ? addLineBody(v, diff) : adjustFirstLineBody(v, diff)
const result = await putLedgerEntry(at, userKey, v, body)
if (!result.ok) {
failures.push({ voucher: `${v.journalId}${v.journalEntryId} (${v.journalEntryDate})`, status: result.status, body: result.body })
}
done++
if (done % 100 === 0) console.log(` ${done}/${unbalanced.length} (${failures.length} failed)`)
}
console.log(`\nDone: ${done - failures.length}/${unbalanced.length} balanced via method ${method}, ${failures.length} failed`)
if (failures.length > 0) {
console.log('Failures (first 20):')
for (const f of failures.slice(0, 20)) console.log(` ${f.voucher}: HTTP ${f.status} ${f.body}`)
}
console.log('\nRe-fetching SIE export to validate...')
const after = await fetchProviderSieFiles('bjornlunden', at, userKey)
for (const f of after.files) {
const parsed = parseSIEFile(f.rawContent)
const validation = validateSIEFile(parsed)
console.log(`fiscal year ${f.fiscalYear}: ${parsed.vouchers.length} vouchers, valid=${validation.valid}`)
if (validation.errors.length) console.log('remaining errors:\n- ' + validation.errors.slice(0, 5).join('\n- '))
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+145
View File
@@ -0,0 +1,145 @@
/**
* One-off cleanup: delete every broken voucher (empty or unbalanced) in the
* BL test company behind the given consent, via BL's
* DELETE /journal/entry/{journalId}/{journalEntryId}/{journalEntryDate}.
*
* Probes the first voucher and aborts if BL refuses the delete, then runs the
* full list at ~8 req/s and re-validates the SIE export at the end.
*
* Run: npx tsx --env-file=.env --env-file=.env.local scripts/bl-delete-broken.ts <consentId>
*/
import { createClient } from '@supabase/supabase-js'
import { fetchBjornLundenToken } from '@/lib/providers/bjornlunden/oauth'
import { fetchProviderSieFiles } from '@/extensions/general/arcim-migration/lib/sie-fetcher'
import { parseSIEFile, validateSIEFile } from '@/lib/import/sie-parser'
const BL_BASE_URL = 'https://apigateway.blinfo.se/bla-api/v1/sp'
const DELAY_MS = 125 // ~8 req/s, under BL's 10 req/s limit
const consentId = process.argv[2]
if (!consentId) {
console.error('Usage: npx tsx --env-file=.env --env-file=.env.local scripts/bl-delete-broken.ts <consentId>')
console.error('Refusing to run without an explicit consentId: this script deletes vouchers from a live BL company.')
process.exit(1)
}
interface BrokenVoucher {
series: string
number: number
date: string // yyyy-MM-dd as written in the SIE file
lineCount: number
diff: number
}
function isoLocal(d: Date): string {
const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0')
return `${y}-${m}-${day}`
}
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
async function deleteVoucher(
accessToken: string,
userKey: string,
v: BrokenVoucher,
): Promise<{ ok: boolean; status: number; body: string }> {
const url = `${BL_BASE_URL}/journal/entry/${encodeURIComponent(v.series)}/${v.number}/${v.date}`
const response = await fetch(url, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${accessToken}`,
'User-Key': userKey,
},
signal: AbortSignal.timeout(30_000),
})
const body = response.ok ? '' : await response.text().catch(() => '')
return { ok: response.ok, status: response.status, body }
}
async function main() {
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
)
const { data: tokens, error } = await supabase
.from('provider_consent_tokens')
.select('provider_company_id')
.eq('consent_id', consentId)
.limit(1)
if (error || !tokens?.length) throw new Error(`no tokens for consent: ${error?.message ?? 'not found'}`)
const userKey = tokens[0]!.provider_company_id as string
const token = await fetchBjornLundenToken(
process.env.BJORN_LUNDEN_CLIENT_ID!,
process.env.BJORN_LUNDEN_CLIENT_SECRET!,
)
console.log('Fetching fresh SIE export to compute the broken-voucher list...')
const { files } = await fetchProviderSieFiles('bjornlunden', token.access_token, userKey)
const broken: BrokenVoucher[] = []
for (const f of files) {
const parsed = parseSIEFile(f.rawContent)
for (const v of parsed.vouchers) {
const diff = Math.round(v.lines.reduce((s, l) => s + l.amount, 0) * 100) / 100
if (Math.abs(diff) > 0.01 || v.lines.length === 0) {
broken.push({
series: v.series,
number: v.number,
date: isoLocal(v.date),
lineCount: v.lines.length,
diff,
})
}
}
}
console.log(`${broken.length} broken vouchers to delete`)
if (broken.length === 0) {
console.log('Nothing to do.')
return
}
// Probe with the first voucher: abort if BL refuses deletes
const probe = broken[0]!
console.log(`Probe delete: ${probe.series}${probe.number} (${probe.date})...`)
const probeResult = await deleteVoucher(token.access_token, userKey, probe)
if (!probeResult.ok) {
console.error(`ABORT: BL refused the probe delete (HTTP ${probeResult.status}): ${probeResult.body}`)
process.exit(1)
}
console.log('Probe OK: deleting the rest...')
const failures: { voucher: string; status: number; body: string }[] = []
let done = 1
for (const v of broken.slice(1)) {
await sleep(DELAY_MS)
const result = await deleteVoucher(token.access_token, userKey, v)
if (!result.ok) {
failures.push({ voucher: `${v.series}${v.number} (${v.date})`, status: result.status, body: result.body.slice(0, 200) })
}
done++
if (done % 100 === 0) console.log(` ${done}/${broken.length} (${failures.length} failed)`)
}
console.log(`\nDone: ${done - failures.length}/${broken.length} deleted, ${failures.length} failed`)
if (failures.length > 0) {
console.log('Failures (first 20):')
for (const f of failures.slice(0, 20)) console.log(` ${f.voucher}: HTTP ${f.status} ${f.body}`)
}
console.log('\nRe-fetching SIE export to validate...')
const after = await fetchProviderSieFiles('bjornlunden', token.access_token, userKey)
for (const f of after.files) {
const parsed = parseSIEFile(f.rawContent)
const validation = validateSIEFile(parsed)
console.log(`fiscal year ${f.fiscalYear}: ${parsed.vouchers.length} vouchers, valid=${validation.valid}`)
if (validation.errors.length) console.log('remaining errors:\n- ' + validation.errors.slice(0, 5).join('\n- '))
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+232
View File
@@ -0,0 +1,232 @@
/**
* Docs-freshness check: verifies that the public API docs on
* https://docs.accounted.se match what this repo would generate today.
*
* The docs site is served by the separate gnubok-website repo from snapshot
* files (`*.generated.ts` exported via scripts/export-docs-to-website.mts,
* plus hand-copied duplicates for changelog/versioning/webhooks/cookbook).
* Nothing re-syncs them automatically, so they drift whenever an endpoint,
* error code, or content page changes here. Every docs page has a raw
* markdown mirror route (`/<page>.md`), which makes the check exact: build
* each page from source, fetch the live mirror, diff.
*
* Run with `npx tsx scripts/check-docs-freshness.mts`.
* Exit codes: 0 = in sync, 1 = drift or missing pages, 2 = build/fetch failure.
* Used by the weekly `loop-docs-freshness` skill; keep the output format
* stable (the loop parses the FINGERPRINT lines for issue dedupe).
*/
import { createHash } from 'node:crypto'
import { createRequire } from 'node:module'
// `server-only` throws on import outside a Next.js server-component graph.
// The reference builder pulls it in transitively (lib/api/v1/load-routes ->
// every v1 route -> lib/init). Nothing here executes request-time code: it
// only reads exported markdown builders, so a no-op stub is the honest
// resolution. Same pattern as scripts/export-docs-to-website.mts.
const require = createRequire(import.meta.url)
const ModuleCtor = require('node:module') as {
_load: (request: string, ...rest: unknown[]) => unknown
}
const originalLoad = ModuleCtor._load
ModuleCtor._load = function (request: string, ...rest: unknown[]) {
if (request === 'server-only') return {}
return originalLoad.call(this, request, ...rest)
}
// Explicit types: these are read inside buildExpectedPages(), and TS cannot
// infer a closure-captured let assigned in a try block (implicit-any error
// under next build's type check; the export script gets away with the bare
// pattern only because it reads the variables at top level).
let errors!: typeof import('@/lib/docs/content/errors')
let reference!: typeof import('@/lib/docs/content/reference')
let connectClaude!: typeof import('@/lib/docs/content/connect-claude')
let anslutClaude!: typeof import('@/lib/docs/content/anslut-claude')
let changelog!: typeof import('@/lib/docs/content/changelog')
let versioning!: typeof import('@/lib/docs/content/versioning')
let webhooks!: typeof import('@/lib/docs/content/webhooks')
let cookbook!: typeof import('@/lib/docs/content/cookbook')
try {
errors = await import('@/lib/docs/content/errors')
reference = await import('@/lib/docs/content/reference')
connectClaude = await import('@/lib/docs/content/connect-claude')
anslutClaude = await import('@/lib/docs/content/anslut-claude')
changelog = await import('@/lib/docs/content/changelog')
versioning = await import('@/lib/docs/content/versioning')
webhooks = await import('@/lib/docs/content/webhooks')
cookbook = await import('@/lib/docs/content/cookbook')
} finally {
ModuleCtor._load = originalLoad
}
const DOCS_BASE = process.env.DOCS_BASE_URL ?? 'https://docs.accounted.se'
/**
* Same link-absolutising transform the export script applies: the website
* cannot serve root-relative /api/v1 links. Applied to BOTH sides before
* comparing so the check is insensitive to whether a hand-copied page was
* adapted or not.
*/
const APP_ORIGIN = 'https://app.gnubok.se'
function canonicalise(md: string): string {
return md
.replaceAll('](/api/v1/', `](${APP_ORIGIN}/api/v1/`)
.replaceAll('](/.well-known/', `](${APP_ORIGIN}/.well-known/`)
.replaceAll('\r\n', '\n')
.split('\n')
.map((l) => l.trimEnd())
.join('\n')
.trim()
}
function sha12(s: string): string {
return createHash('sha256').update(s).digest('hex').slice(0, 12)
}
interface PageCheck {
/** Path of the raw-markdown mirror on the docs site, no leading slash. */
path: string
expected: string
}
function buildExpectedPages(): PageCheck[] {
const buildErrorReferenceMd = errors.buildErrorReferenceMd
const buildResourcePages = reference.buildResourcePages
const buildReferenceOverviewMd = reference.buildReferenceOverviewMd
const { COOKBOOK, buildPlaceholderMd } = cookbook
if (!buildErrorReferenceMd || !buildResourcePages || !buildReferenceOverviewMd || !COOKBOOK) {
console.error('Missing builder exports; the content modules changed shape.')
process.exit(2)
}
const pages: PageCheck[] = [
{ path: 'reference.md', expected: buildReferenceOverviewMd() },
{ path: 'errors.md', expected: buildErrorReferenceMd() },
{ path: 'connect-claude.md', expected: connectClaude.CONNECT_CLAUDE_MD },
{ path: 'anslut-claude.md', expected: anslutClaude.ANSLUT_CLAUDE_MD },
{ path: 'changelog.md', expected: changelog.CHANGELOG_MD },
{ path: 'versioning.md', expected: versioning.VERSIONING_MD },
{ path: 'webhooks.md', expected: webhooks.WEBHOOKS_MD },
]
for (const page of buildResourcePages()) {
pages.push({ path: `reference/${page.slug}.md`, expected: page.markdown })
}
for (const entry of COOKBOOK) {
pages.push({
path: `cookbook/${entry.slug}.md`,
expected: entry.markdown ?? buildPlaceholderMd(entry),
})
}
return pages
}
type Verdict =
| { path: string; status: 'ok' }
| { path: string; status: 'missing'; fingerprint: string }
| {
path: string
status: 'drift'
fingerprint: string
firstDiffLine: number
expectedLine: string
liveLine: string
changedLines: number
}
| { path: string; status: 'fetch-error'; detail: string }
async function checkPage(page: PageCheck): Promise<Verdict> {
const url = `${DOCS_BASE}/${page.path}`
let res: Response
try {
res = await fetch(url, { redirect: 'follow' })
} catch (e) {
return { path: page.path, status: 'fetch-error', detail: String(e) }
}
if (res.status === 404) {
// A page this repo generates that the docs site does not serve at all:
// typically a new API resource whose generated snapshot + nav entry were
// never exported to the website repo.
return { path: page.path, status: 'missing', fingerprint: sha12(`missing:${page.path}`) }
}
if (!res.ok) {
return { path: page.path, status: 'fetch-error', detail: `HTTP ${res.status}` }
}
const live = canonicalise(await res.text())
const expected = canonicalise(page.expected)
if (live === expected) return { path: page.path, status: 'ok' }
const expectedLines = expected.split('\n')
const liveLines = live.split('\n')
let firstDiffLine = 0
while (
firstDiffLine < Math.min(expectedLines.length, liveLines.length) &&
expectedLines[firstDiffLine] === liveLines[firstDiffLine]
) {
firstDiffLine++
}
const maxLen = Math.max(expectedLines.length, liveLines.length)
let changedLines = Math.abs(expectedLines.length - liveLines.length)
for (let i = 0; i < Math.min(expectedLines.length, liveLines.length); i++) {
if (expectedLines[i] !== liveLines[i]) changedLines++
}
return {
path: page.path,
status: 'drift',
// Stable while the same pair of contents drifts; changes when either
// side changes, so a NEW drift after a fix reads as a new finding.
fingerprint: sha12(`${page.path}:${sha12(expected)}:${sha12(live)}`),
firstDiffLine: firstDiffLine + 1,
expectedLine: expectedLines[firstDiffLine] ?? '<end of file>',
liveLine: liveLines[firstDiffLine] ?? '<end of file>',
changedLines: Math.min(changedLines, maxLen),
}
}
async function main() {
const pages = buildExpectedPages()
console.log(`Checking ${pages.length} docs pages against ${DOCS_BASE} ...\n`)
const verdicts: Verdict[] = []
const CONCURRENCY = 6
for (let i = 0; i < pages.length; i += CONCURRENCY) {
const batch = pages.slice(i, i + CONCURRENCY)
verdicts.push(...(await Promise.all(batch.map(checkPage))))
}
const ok = verdicts.filter((v) => v.status === 'ok')
const drift = verdicts.filter((v) => v.status === 'drift')
const missing = verdicts.filter((v) => v.status === 'missing')
const failed = verdicts.filter((v) => v.status === 'fetch-error')
for (const v of verdicts) {
if (v.status === 'ok') continue
if (v.status === 'missing') {
console.log(`MISSING ${v.path}`)
console.log(` page exists in erp-base but the docs site returns 404`)
console.log(` FINGERPRINT ${v.fingerprint}`)
} else if (v.status === 'drift') {
console.log(`DRIFT ${v.path}`)
console.log(` ~${v.changedLines} differing line(s), first at line ${v.firstDiffLine}:`)
console.log(` repo builds: ${v.expectedLine.slice(0, 160)}`)
console.log(` site serves: ${v.liveLine.slice(0, 160)}`)
console.log(` FINGERPRINT ${v.fingerprint}`)
} else {
console.log(`ERROR ${v.path}: ${v.detail}`)
}
console.log('')
}
console.log(
`Summary: ${ok.length} in sync, ${drift.length} drifted, ${missing.length} missing, ${failed.length} fetch errors.`,
)
if (failed.length > 0) process.exit(2)
if (drift.length > 0 || missing.length > 0) {
console.log('\nTo fix: run `npx tsx scripts/export-docs-to-website.mts`, then port any')
console.log('hand-copied pages (changelog/versioning/webhooks/cookbook) and nav entries')
console.log('in the gnubok-website repo, and deploy it. See loop-docs-freshness skill.')
process.exit(1)
}
console.log('Docs are up to date.')
}
await main()
+88
View File
@@ -0,0 +1,88 @@
/**
* One-off diagnostic: dump Enable Banking ASPSP metadata for Handelsbanken,
* specifically the available auth_methods (name + approach + psu_types) for
* business vs personal. Answers: does HB expose a DECOUPLED (Mobile BankID)
* method, and which method is first/default when we omit auth_method?
*
* Run: node scripts/check-handelsbanken-aspsp.mjs
* Reads ENABLE_BANKING_* from .env (sandbox or production, whatever is set).
*/
import * as crypto from 'crypto'
import * as fs from 'fs'
// --- minimal .env parser (APP_ID, PRIVATE_KEY, API_URL) ---
const env = {}
for (const raw of fs.readFileSync('.env', 'utf-8').split('\n')) {
const line = raw.replace(/\r$/, '')
const m = line.match(/^([A-Z0-9_]+)=(.*)$/)
if (m) env[m[1]] = m[2].replace(/^["']|["']$/g, '')
}
const APP_ID = env.ENABLE_BANKING_APP_ID_PRODUCTION || env.ENABLE_BANKING_APP_ID
const PRIVATE_KEY_RAW = env.ENABLE_BANKING_PRIVATE_KEY_PRODUCTION || env.ENABLE_BANKING_PRIVATE_KEY
const API_URL =
env.ENABLE_BANKING_API_URL_PRODUCTION || env.ENABLE_BANKING_API_URL || 'https://api.enablebanking.com'
const isSandbox = API_URL.includes('tilisy')
function getPrivateKey() {
const decoded = Buffer.from(PRIVATE_KEY_RAW, 'base64').toString('utf-8')
if (decoded.startsWith('-----BEGIN')) return decoded
const lines = PRIVATE_KEY_RAW.match(/.{1,64}/g) || []
return `-----BEGIN ` + `PRIVATE KEY-----\n${lines.join('\n')}\n-----END ` + `PRIVATE KEY-----`
}
function b64url(d) {
const s = typeof d === 'string' ? d : d.toString('base64')
return s.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
function jwt() {
const now = Math.floor(Date.now() / 1000)
const header = b64url(Buffer.from(JSON.stringify({ typ: 'JWT', alg: 'RS256', kid: APP_ID })))
const payload = b64url(
Buffer.from(JSON.stringify({ iss: 'enablebanking.com', aud: 'api.enablebanking.com', iat: now, exp: now + 600 }))
)
const sign = crypto.createSign('RSA-SHA256')
sign.update(`${header}.${payload}`)
sign.end()
return `${header}.${payload}.${b64url(sign.sign(getPrivateKey()))}`
}
async function aspsps(psuType) {
const params = new URLSearchParams({ country: 'SE', sandbox: String(isSandbox), psu_type: psuType })
const res = await fetch(`${API_URL}/aspsps?${params}`, {
headers: { Authorization: `Bearer ${jwt()}`, 'Content-Type': 'application/json' },
})
if (!res.ok) throw new Error(`/aspsps ${psuType} -> ${res.status}: ${await res.text()}`)
return (await res.json()).aspsps || []
}
console.log(`API: ${API_URL} (sandbox=${isSandbox})\n`)
for (const psuType of ['business', 'personal']) {
console.log(`========== psu_type=${psuType} ==========`)
let list
try {
list = await aspsps(psuType)
} catch (e) {
console.log(` ERROR: ${e.message}\n`)
continue
}
const hb = list.filter((a) => /handels/i.test(a.name))
if (!hb.length) {
console.log(` (no Handelsbanken in ${list.length} SE ASPSPs for ${psuType})`)
console.log(` names: ${list.map((a) => a.name).join(', ')}\n`)
continue
}
for (const a of hb) {
console.log(`\n ${a.name} (${a.country}) bic=${a.bic ?? '-'} beta=${a.beta ?? '-'}`)
console.log(` psu_types: ${JSON.stringify(a.psu_types)}`)
console.log(` max_consent_validity: ${a.maximum_consent_validity ?? a.max_consent_validity ?? '-'}`)
const methods = a.auth_methods || a.available_auth_methods || []
console.log(` auth_methods (${methods.length}), FIRST is the default when we omit auth_method:`)
methods.forEach((m, i) =>
console.log(
` [${i}] name=${m.name} approach=${m.approach ?? '-'} psu_types=${JSON.stringify(
m.psu_types
)} title=${JSON.stringify(m.title)} hidden=${m.hidden_method ?? '-'}`
)
)
}
console.log('')
}
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env sh
# check-masterplan-lock.sh
#
# Fails the build if this repo has drifted from the canonical SIAX Non-Platform
# masterplan (sax3l/siax-masterplan). Dependency-free (POSIX sh + grep only) so
# it runs before any language toolchain is installed.
#
# What it guards against:
# 1. PLAN/MASTERPLAN_INDEX.md being deleted (silent regression to a parallel
# truth with no pointer back to the canonical docs).
# 2. The "locked architecture principles" section being stripped or edited
# away, which is the actual enforcement text (one canonical owner per
# domain/truth, no cross-service SQL, no frontend-tenant-trust, no
# production-mock-fallbacks, API-first).
# 3. The status vocabulary line (MISSING -> ... -> PRODUCTION_READY) going
# missing, which would let a repo invent its own status labels.
#
# See PLAN/MASTERPLAN_INDEX.md and sax3l/siax-masterplan for the source of
# truth. Do not weaken this check to make a red build pass -- fix the drift
# in PLAN/MASTERPLAN_INDEX.md instead (or resync it from siax-masterplan).
set -eu
PLAN_FILE="PLAN/MASTERPLAN_INDEX.md"
fail=0
if [ ! -f "$PLAN_FILE" ]; then
echo "::error::${PLAN_FILE} is missing. This repo must carry a repo-local copy of the SIAX Non-Platform masterplan index (source: sax3l/siax-masterplan). Restore it -- do not remove this check."
exit 1
fi
check_contains() {
needle="$1"
label="$2"
if ! grep -qF "$needle" "$PLAN_FILE"; then
echo "::error::${PLAN_FILE} no longer contains ${label} ('${needle}'). This looks like drift away from the canonical masterplan (sax3l/siax-masterplan). Refresh PLAN/MASTERPLAN_INDEX.md from source instead of editing this check."
fail=1
fi
}
check_contains "Låsta arkitekturprinciper" "the locked-architecture-principles heading"
check_contains "En canonical owner per domän och sanning" "the one-canonical-owner-per-domain principle"
check_contains "aldrig egen SQL" "the no-own-SQL / no-parallel-truth clause"
check_contains "ingen frontend-tenant-trust" "the no-frontend-tenant-trust clause"
check_contains "PRODUCTION_READY" "the status vocabulary"
check_contains "sax3l/siax-masterplan" "the pointer back to the canonical masterplan repo"
if [ "$fail" -ne 0 ]; then
echo "::error::masterplan lock check FAILED -- see above."
exit 1
fi
echo "masterplan lock check OK -- ${PLAN_FILE} still matches the canonical principles."
+113
View File
@@ -0,0 +1,113 @@
#!/usr/bin/env node
/**
* CI gate: a PR that adds or changes a migration touching a trigger,
* function/RPC, RLS policy, or DEFERRABLE constraint must also add or extend
* a *.pg.test.ts.
*
* This enforces the rule documented in .claude/rules/database.md ("pg-real
* tests: any PR touching a trigger/RPC/RLS/DEFERRABLE must include or extend
* a *.pg.test.ts"): previously instruction-only, which means it got skipped.
*
* Escape hatch: a migration may declare, in a SQL comment, either
* -- pg-test: covered-by tests/pg/<file>.pg.test.ts
* -- pg-test: skip (<reason>)
* Both are visible in review and greppable later. Use them sparingly:
* "covered-by" when an existing test already exercises the changed object,
* "skip" when the change is genuinely untestable (e.g. a NOTIFY-only fixup).
*
* Scope: the gate is PR-level, not per-migration: ANY *.pg.test.ts change
* satisfies it. With multiple risky migrations in one PR, reviewers must
* still confirm each one is actually covered (or carries an escape hatch);
* mapping tests to migrations automatically would be guesswork.
*
* Usage: node scripts/check-pg-test-coverage.mjs
* PG_GATE_BASE: git ref to diff against (default: origin/main)
*/
import { execFileSync } from 'node:child_process'
import { existsSync, readFileSync } from 'node:fs'
const base = process.env.PG_GATE_BASE || 'origin/main'
let changed
try {
// Three-dot diff: changes on the PR side since the merge-base with `base`.
// --diff-filter=ACMR skips deletions (a deleted migration has no content to
// scan). execFileSync with an argv array: no shell, so a hostile base-ref
// string can't inject (git just rejects an invalid rev via the catch below).
changed = execFileSync('git', ['diff', '--name-only', '--diff-filter=ACMR', `${base}...HEAD`], {
encoding: 'utf8',
})
.trim()
.split('\n')
.filter(Boolean)
} catch (err) {
console.error(`check-pg-test-coverage: failed to diff against "${base}".`)
console.error('Set PG_GATE_BASE to a fetched ref (CI: origin/${{ github.base_ref }}).')
console.error(String(err))
process.exit(2)
}
const migrations = changed.filter(
(f) => f.startsWith('supabase/migrations/') && f.endsWith('.sql'),
)
const pgTests = changed.filter((f) => f.endsWith('.pg.test.ts'))
// DDL that the database.md rule classifies as requiring real-Postgres coverage.
const RISKY_DDL = [
{ kind: 'trigger', re: /\bCREATE\s+(OR\s+REPLACE\s+)?(CONSTRAINT\s+)?TRIGGER\b/i },
{ kind: 'function/RPC', re: /\bCREATE\s+(OR\s+REPLACE\s+)?FUNCTION\b/i },
{ kind: 'RLS policy', re: /\b(CREATE|ALTER|DROP)\s+POLICY\b/i },
{ kind: 'RLS enable/disable', re: /\b(ENABLE|DISABLE)\s+ROW\s+LEVEL\s+SECURITY\b/i },
{ kind: 'DEFERRABLE constraint', re: /\bDEFERRABLE\b/i },
]
const ESCAPE_HATCH = /^\s*--\s*pg-test:\s*(covered-by\s+\S+|skip\b.*)$/im
const flagged = []
for (const file of migrations) {
if (!existsSync(file)) continue
const raw = readFileSync(file, 'utf8')
if (ESCAPE_HATCH.test(raw)) continue
// Strip SQL line comments so prose mentioning "CREATE POLICY" doesn't trip the gate.
const sql = raw.replace(/--.*$/gm, '')
const kinds = RISKY_DDL.filter(({ re }) => re.test(sql)).map(({ kind }) => kind)
if (kinds.length > 0) flagged.push({ file, kinds })
}
if (flagged.length === 0) {
console.log(
migrations.length === 0
? 'check-pg-test-coverage: no migrations in this diff.'
: `check-pg-test-coverage: ${migrations.length} migration(s) changed, none touch trigger/RPC/RLS/DEFERRABLE.`,
)
process.exit(0)
}
if (pgTests.length > 0) {
console.log(
`check-pg-test-coverage: ${flagged.length} risky migration(s) accompanied by pg-real test change(s):`,
)
for (const t of pgTests) console.log(` test: ${t}`)
process.exit(0)
}
console.error('check-pg-test-coverage: FAILED\n')
console.error(
'These migrations touch trigger/RPC/RLS/DEFERRABLE but the PR adds or extends no *.pg.test.ts:\n',
)
for (const { file, kinds } of flagged) {
console.error(` ${file} (${kinds.join(', ')})`)
}
console.error(`
The repo rule (.claude/rules/database.md) requires real-Postgres coverage for
these objects: mocked Supabase tests cannot exercise them.
Fix one of:
1. Add or extend a *.pg.test.ts covering the changed trigger/RPC/policy
(helpers: tests/pg/setup.ts, tests/pg/fixtures.ts; run: npm run test:pg)
2. If an existing pg test already covers it, annotate the migration:
-- pg-test: covered-by tests/pg/<file>.pg.test.ts
3. If genuinely untestable, annotate with a reason:
-- pg-test: skip (<reason>)
`)
process.exit(1)
+71
View File
@@ -0,0 +1,71 @@
/**
* Open every URL in the portal directory and report the ones that are wrong.
*
* The directory shipped with eighteen hand-written paths, none of them opened.
* The file warned about exactly that and shipped anyway, and a founder then hit
* a 404 on Google Workspace. A sweep found GitHub broken too. This exists so
* the next wrong URL is found by a script rather than by a person who trusted
* the link.
*
* npx tsx scripts/check-portal-urls.mts
*
* A 404 is the signal. 401/403 and a redirect to a login host both mean the
* path exists and is asking who you are, which is the expected answer for a
* billing page. Several hosts (Google, OpenAI, Hetzner) refuse automated
* requests outright and report as UNREACHABLE: that is not proof of a bad URL,
* only proof that this script cannot judge it, and those entries should be kept
* shallow enough that a human landing on them is never lost.
*/
import { PORTAL_DIRECTORY } from '../lib/receipt-hunt/portal-directory'
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
type Verdict = 'OK' | 'BROKEN' | 'UNREACHABLE'
async function check(url: string): Promise<{ verdict: Verdict; detail: string }> {
try {
const res = await fetch(url, {
redirect: 'follow',
signal: AbortSignal.timeout(15_000),
headers: { 'user-agent': UA },
})
const where = res.redirected ? ` -> ${new URL(res.url).host}` : ''
if (res.status === 404 || res.status === 410) {
return { verdict: 'BROKEN', detail: `${res.status}${where}` }
}
return { verdict: 'OK', detail: `${res.status}${where}` }
} catch (err) {
return { verdict: 'UNREACHABLE', detail: err instanceof Error ? err.message.slice(0, 44) : 'okänt fel' }
}
}
const results = await Promise.all(
PORTAL_DIRECTORY.map(async (entry) => ({ entry, ...(await check(entry.url)) })),
)
for (const r of results) {
const mark = r.verdict === 'BROKEN' ? '✗' : r.verdict === 'UNREACHABLE' ? '?' : '✓'
console.log(`${mark} ${r.entry.vendor.padEnd(22)} ${r.detail.padEnd(30)} ${r.entry.url}`)
}
const broken = results.filter((r) => r.verdict === 'BROKEN')
const unreachable = results.filter((r) => r.verdict === 'UNREACHABLE')
console.log(
`\n${results.length} länkar: ${results.length - broken.length - unreachable.length} svarade, ` +
`${unreachable.length} gick inte att nå, ${broken.length} trasiga`,
)
if (unreachable.length > 0) {
console.log(`\nGick inte att nå (kan inte bedömas härifrån, håll dem grunda):`)
for (const r of unreachable) console.log(` ${r.entry.vendor}: ${r.entry.url}`)
}
// Only a genuine 404 fails the run. An unreachable host is a limit of this
// script, not a defect in the directory, and failing on it would train people
// to ignore the output.
if (broken.length > 0) {
console.error(`\nTrasiga länkar som måste rättas:`)
for (const r of broken) console.error(` ${r.entry.vendor}: ${r.entry.url} (${r.detail})`)
process.exit(1)
}
@@ -0,0 +1,411 @@
/**
* Proof that the ambiguous-embed ratchet catches the shape that shipped twice
* and leaves both legitimate hint forms alone. Offending fixtures live only in
* these strings and in an OS temp directory the end-to-end cases create and
* delete.
*/
import { describe, it, expect, afterAll } from 'vitest'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import {
deriveAmbiguousPairs,
findAmbiguousEmbeds,
findAmbiguousEmbedsInSource,
pairKey,
parseEmbeds,
} from '../ambiguous-embed.mjs'
const tempDirs: string[] = []
afterAll(() => {
for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true })
})
const tempRoot = (prefix: string) => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), prefix))
tempDirs.push(root)
return root
}
const write = (root: string, rel: string, content: string) => {
const full = path.join(root, rel)
fs.mkdirSync(path.dirname(full), { recursive: true })
fs.writeFileSync(full, content)
}
// Only the pair this guard exists for. Every other pair the fixtures embed
// (journal_entries -> journal_entry_lines, transactions -> journal_entries) is
// absent from the set, which is exactly how a single-foreign-key pair looks to
// the scanner: it must never be flagged.
const PAIRS = new Set([pairKey('journal_entries', 'fiscal_periods')])
const scan = (source: string) =>
findAmbiguousEmbedsInSource(source, 'fixture.ts', PAIRS).map(
(f: { from: string; target: string }) => `${f.from}->${f.target}`,
)
describe('ambiguous-embed: the shape that shipped', () => {
it('flags an ALIASED embed with no relationship named', () => {
// lib/core/documents/supplier-invoice-underlag.ts as it shipped 2026-07-27.
// A tokenizer that only looks for `<table>(` after a comma, space or paren
// misses this, which is the whole reason the bug survived two reviews.
expect(
scan(
`const { data } = await supabase
.from('journal_entries')
.select('id, status, fiscal_period:fiscal_periods(is_closed, locked_at)')`,
),
).toEqual(['journal_entries->fiscal_periods'])
})
it('flags the bare, unaliased embed too', () => {
expect(scan(`supabase.from('journal_entries').select('id, fiscal_periods(is_closed)')`)).toEqual(
['journal_entries->fiscal_periods'],
)
})
it('flags the embed written in the other direction', () => {
expect(
scan(`supabase.from('fiscal_periods').select('id, journal_entries(voucher_number)')`),
).toEqual(['fiscal_periods->journal_entries'])
})
})
describe('ambiguous-embed: both hint forms PostgREST accepts', () => {
it('accepts the constraint name', () => {
// lib/transactions/inbox-underlag.ts, lib/pending-operations/commit.ts.
expect(
scan(
`supabase.from('journal_entries').select('id, fiscal_period:fiscal_periods!journal_entries_fiscal_period_id_fkey(is_closed, locked_at)')`,
),
).toEqual([])
})
it('accepts the foreign key COLUMN name', () => {
// lib/import/opening-balance/cascade.ts, lib/invoices/invoice-matching.ts.
// A `*_fkey`-only matcher would hard-fail six legitimate lines here.
expect(
scan(
`supabase.from('fiscal_periods').select('id, opening_balance_entry:journal_entries!opening_balance_entry_id(voucher_series, voucher_number)')`,
),
).toEqual([])
})
it('accepts a hint combined with a join modifier, in either order', () => {
expect(
scan(
`supabase.from('journal_entries').select('id, fiscal_periods!journal_entries_fiscal_period_id_fkey!inner(is_closed)')`,
),
).toEqual([])
expect(
scan(
`supabase.from('journal_entries').select('id, fiscal_periods!inner!journal_entries_fiscal_period_id_fkey(is_closed)')`,
),
).toEqual([])
})
it('does NOT accept a bare join modifier: !inner disambiguates nothing', () => {
expect(
scan(`supabase.from('journal_entries').select('id, fiscal_periods!inner(is_closed)')`),
).toEqual(['journal_entries->fiscal_periods'])
expect(
scan(`supabase.from('journal_entries').select('id, fiscal_periods!left(is_closed)')`),
).toEqual(['journal_entries->fiscal_periods'])
})
})
describe('ambiguous-embed: chain resolution', () => {
it('resolves .from() through its OWN chain, not the nearest one above', () => {
// scripts/seed-demo-account.ts: a journal_entries!inner embed from
// journal_entry_lines (a single-foreign-key pair) sits below an unrelated
// .from('journal_entries'). Pairing by proximity flags it wrongly.
const source = `
await supabase.from('journal_entries').select('id, voucher_number')
await supabase
.from('journal_entry_lines')
.select('account_number, debit, journal_entries!inner(entry_date)')
.eq('company_id', companyId)
`
expect(scan(source)).toEqual([])
})
it('reads a select anywhere in the chain, before or after the filters', () => {
expect(
scan(
`supabase.from('journal_entries').select('id, fiscal_periods(is_closed)').eq('company_id', id).in('id', ids)`,
),
).toEqual(['journal_entries->fiscal_periods'])
})
it('ignores a select whose from-table cannot be resolved statically', () => {
expect(scan(`query.select('id, fiscal_periods(is_closed)')`)).toEqual([])
expect(scan(`supabase.from(tableName).select('id, fiscal_periods(is_closed)')`)).toEqual([])
})
})
describe('ambiguous-embed: nesting', () => {
it('resolves a nested embed against its enclosing embed, not the root', () => {
// transactions -> journal_entries is a single-foreign-key pair here, so the
// inner fiscal_periods embed is what must be judged, against journal_entries.
const nested = `supabase.from('transactions').select('id, journal_entries(id, fiscal_periods(is_closed))')`
expect(scan(nested)).toEqual(['journal_entries->fiscal_periods'])
})
it('leaves the enclosing embed alone when the inner one is hinted', () => {
expect(
scan(
`supabase.from('transactions').select('id, journal_entries(id, fiscal_periods!journal_entries_fiscal_period_id_fkey(is_closed))')`,
),
).toEqual([])
})
it('parses plain columns, casts and json paths without inventing embeds', () => {
expect(parseEmbeds('id, amount::text, metadata->>ref, *', 'journal_entries')).toEqual([])
})
})
describe('ambiguous-embed: pair derivation from the migration history', () => {
const migrationsRoot = (files: Record<string, string>) => {
const root = tempRoot('ambiguous-embed-sql-')
for (const [name, sql] of Object.entries(files)) write(root, name, sql)
return root
}
it('counts a pair as ambiguous once a second foreign key joins it', () => {
const dir = migrationsRoot({
'20240101000001_core.sql': `
CREATE TABLE public.journal_entries (
id UUID PRIMARY KEY,
fiscal_period_id UUID REFERENCES public.fiscal_periods(id)
);
`,
})
expect(deriveAmbiguousPairs(dir).size).toBe(0)
const dir2 = migrationsRoot({
'20240101000001_core.sql': `
CREATE TABLE public.journal_entries (
id UUID PRIMARY KEY,
fiscal_period_id UUID REFERENCES public.fiscal_periods(id)
);
`,
'20240101000019_period_closing.sql': `
ALTER TABLE public.fiscal_periods
ADD COLUMN closing_entry_id UUID REFERENCES public.journal_entries(id);
`,
})
expect([...deriveAmbiguousPairs(dir2)]).toEqual([pairKey('fiscal_periods', 'journal_entries')])
})
it('collapses the idempotent re-adds the migration history is full of', () => {
const dir = migrationsRoot({
'20240101000001_a.sql': `
ALTER TABLE public.journal_entry_lines
ADD CONSTRAINT journal_entry_lines_cost_center_id_fkey
FOREIGN KEY (cost_center_id) REFERENCES public.cost_centers(id);
`,
'20240101000002_b.sql': `
ALTER TABLE public.journal_entry_lines
ADD CONSTRAINT journal_entry_lines_cost_center_id_fkey
FOREIGN KEY (cost_center_id) REFERENCES public.cost_centers(id);
`,
})
expect(deriveAmbiguousPairs(dir).size).toBe(0)
})
it('honours DROP COLUMN, DROP CONSTRAINT and DROP TABLE', () => {
const base = `
CREATE TABLE public.a (
id UUID PRIMARY KEY,
b_one_id UUID REFERENCES public.b(id),
b_two_id UUID REFERENCES public.b(id)
);
`
expect([...deriveAmbiguousPairs(migrationsRoot({ '1_a.sql': base }))]).toEqual([pairKey('a', 'b')])
expect(
deriveAmbiguousPairs(
migrationsRoot({ '1_a.sql': base, '2_b.sql': `ALTER TABLE public.a DROP COLUMN b_two_id;` }),
).size,
).toBe(0)
expect(
deriveAmbiguousPairs(
migrationsRoot({
'1_a.sql': base,
'2_b.sql': `ALTER TABLE public.a DROP CONSTRAINT IF EXISTS a_b_two_id_fkey;`,
}),
).size,
).toBe(0)
expect(
deriveAmbiguousPairs(
migrationsRoot({ '1_a.sql': base, '2_b.sql': `DROP TABLE IF EXISTS public.b;` }),
).size,
).toBe(0)
})
it('counts a composite foreign key next to a single-column one', () => {
// 20260902130000_sales_orders.sql + 20260902180000_sales_orders_hardening.sql:
// the composite (sales_order_id, company_id) guard is a second relationship
// in PostgREST's eyes. A single-column-only parser derived one edge here and
// let `items:sales_order_items(*)` ship un-hinted; prod answered PGRST201
// on every kundorder load (2026-09-03).
const dir = migrationsRoot({
'20260902130000_sales_orders.sql': `
CREATE TABLE public.sales_order_items (
id uuid PRIMARY KEY,
company_id uuid NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
sales_order_id uuid NOT NULL REFERENCES public.sales_orders(id) ON DELETE CASCADE
);
`,
'20260902180000_sales_orders_hardening.sql': `
ALTER TABLE public.sales_order_items
DROP CONSTRAINT IF EXISTS sales_order_items_order_company_fkey;
ALTER TABLE public.sales_order_items
ADD CONSTRAINT sales_order_items_order_company_fkey
FOREIGN KEY (sales_order_id, company_id)
REFERENCES public.sales_orders (id, company_id)
ON DELETE CASCADE;
`,
})
expect([...deriveAmbiguousPairs(dir)]).toEqual([pairKey('sales_order_items', 'sales_orders')])
})
it('does not count a composite foreign key that REPLACED the single-column one', () => {
// 20260721101500_harden_tax_assessment_notices.sql shape: the old
// single-column constraint is dropped and the composite added, leaving one
// relationship. Prod agrees: tax_assessment_notices|fiscal_periods is not
// in the pg_constraint list.
const dir = migrationsRoot({
'1_base.sql': `
CREATE TABLE public.tax_assessment_notices (
id uuid PRIMARY KEY,
company_id uuid NOT NULL REFERENCES public.companies(id),
fiscal_period_id uuid REFERENCES public.fiscal_periods(id)
);
`,
'2_harden.sql': `
ALTER TABLE public.tax_assessment_notices
DROP CONSTRAINT IF EXISTS tax_assessment_notices_fiscal_period_id_fkey;
ALTER TABLE public.tax_assessment_notices
ADD CONSTRAINT tax_assessment_notices_fiscal_period_company_fkey
FOREIGN KEY (fiscal_period_id, company_id)
REFERENCES public.fiscal_periods (id, company_id);
`,
})
expect(deriveAmbiguousPairs(dir).size).toBe(0)
})
it('drops a composite edge when DROP COLUMN removes one of its columns', () => {
// Postgres drops every foreign key a column takes part in. Keeping the
// composite edge would arm a pair that no longer exists and reject valid
// embeds (CodeRabbit on PR #2207).
const base = `
CREATE TABLE public.sales_order_items (
id uuid PRIMARY KEY,
company_id uuid NOT NULL,
sales_order_id uuid NOT NULL REFERENCES public.sales_orders(id),
CONSTRAINT sales_order_items_order_company_fkey
FOREIGN KEY (sales_order_id, company_id) REFERENCES public.sales_orders(id, company_id)
);
`
expect([...deriveAmbiguousPairs(migrationsRoot({ '1_a.sql': base }))]).toEqual([
pairKey('sales_order_items', 'sales_orders'),
])
expect(
deriveAmbiguousPairs(
migrationsRoot({
'1_a.sql': base,
'2_b.sql': `ALTER TABLE public.sales_order_items DROP COLUMN company_id;`,
}),
).size,
).toBe(0)
// The dropped constraint's name is released too: a later DROP CONSTRAINT
// by that name must not delete an unrelated edge.
expect(
deriveAmbiguousPairs(
migrationsRoot({
'1_a.sql': base,
'2_b.sql': `ALTER TABLE public.sales_order_items DROP COLUMN company_id;`,
'3_c.sql': `ALTER TABLE public.sales_order_items DROP CONSTRAINT IF EXISTS sales_order_items_order_company_fkey;`,
}),
).size,
).toBe(0)
})
it('reads a composite table-level constraint inside CREATE TABLE', () => {
const dir = migrationsRoot({
'1_a.sql': `
CREATE TABLE public.a (
id uuid PRIMARY KEY,
company_id uuid NOT NULL,
b_id uuid REFERENCES public.b(id),
CONSTRAINT a_b_company_fkey FOREIGN KEY (b_id, company_id) REFERENCES public.b(id, company_id)
);
`,
})
expect([...deriveAmbiguousPairs(dir)]).toEqual([pairKey('a', 'b')])
})
it('matches the live schema on the real migration history', () => {
// Verified against prod (pwxtzglxptnnvjrpixpg) on 2026-09-03 with the
// pg_constraint query in ambiguous-embed.mjs: the same 17 pairs.
const pairs = deriveAmbiguousPairs(
path.join(__dirname, '..', '..', '..', 'supabase', 'migrations'),
)
expect(pairs.has(pairKey('journal_entries', 'fiscal_periods'))).toBe(true)
expect(pairs.has(pairKey('journal_entries', 'salary_runs'))).toBe(true)
expect(pairs.has(pairKey('supplier_invoices', 'transactions'))).toBe(true)
expect(pairs.has(pairKey('sales_order_items', 'sales_orders'))).toBe(true)
// Single-foreign-key pairs that legitimate code embeds without a hint.
expect(pairs.has(pairKey('journal_entries', 'journal_entry_lines'))).toBe(false)
expect(pairs.has(pairKey('supplier_invoice_payments', 'supplier_invoices'))).toBe(false)
expect(pairs.has(pairKey('tax_assessment_notices', 'fiscal_periods'))).toBe(false)
})
})
describe('ambiguous-embed: file scan', () => {
it('reports offenders relative to the root and skips test files', () => {
const root = tempRoot('ambiguous-embed-')
write(
root,
'supabase/migrations/20240101000019_period_closing.sql',
`
CREATE TABLE public.journal_entries (
id UUID PRIMARY KEY,
fiscal_period_id UUID REFERENCES public.fiscal_periods(id)
);
ALTER TABLE public.fiscal_periods
ADD COLUMN closing_entry_id UUID REFERENCES public.journal_entries(id);
`,
)
const bad = `supabase.from('journal_entries').select('id, fiscal_period:fiscal_periods(is_closed)')`
const good = `supabase.from('journal_entries').select('id, fiscal_periods!journal_entries_fiscal_period_id_fkey(is_closed)')`
write(root, 'lib/bad.ts', bad)
write(root, 'lib/good.ts', good)
write(root, 'lib/__tests__/bad.test.ts', bad)
write(root, 'lib/bad.test.ts', bad)
write(root, 'app/api/x/route.ts', `${good}\n${bad}`)
expect(findAmbiguousEmbeds(root)).toEqual([
{ where: 'app/api/x/route.ts:2', from: 'journal_entries', target: 'fiscal_periods' },
{ where: 'lib/bad.ts:1', from: 'journal_entries', target: 'fiscal_periods' },
])
})
it('finds nothing when no pair is ambiguous, whatever the code embeds', () => {
const root = tempRoot('ambiguous-embed-single-')
write(
root,
'supabase/migrations/1_core.sql',
`CREATE TABLE public.journal_entries (id UUID PRIMARY KEY, fiscal_period_id UUID REFERENCES public.fiscal_periods(id));`,
)
write(
root,
'lib/fine.ts',
`supabase.from('journal_entries').select('id, fiscal_period:fiscal_periods(is_closed)')`,
)
expect(findAmbiguousEmbeds(root)).toEqual([])
})
})
@@ -0,0 +1,58 @@
/**
* Proof that the client-node-builtin guard follows static imports from a
* 'use client' module to a Node builtin, and only those. Fixtures live in an
* OS temp directory the test creates and deletes.
*/
import { describe, it, expect, afterAll } from 'vitest'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { findClientNodeBuiltins } from '../client-node-builtin.mjs'
const tempDirs: string[] = []
afterAll(() => {
for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true })
})
function fixture(files: Record<string, string>) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'client-builtin-'))
tempDirs.push(root)
for (const [rel, content] of Object.entries(files)) {
const full = path.join(root, rel)
fs.mkdirSync(path.dirname(full), { recursive: true })
fs.writeFileSync(full, content)
}
return root
}
describe('client-node-builtin guard', () => {
it('flags a client component whose lib import chain reaches crypto, with the chain', () => {
const root = fixture({
'lib/auth/hashing.ts': `import crypto from 'crypto'\nexport const hash = (s: string) => crypto.createHash('sha256').update(s).digest('hex')\nexport const isEnabled = () => true\n`,
'components/Login.tsx': `'use client'\nimport { isEnabled } from '@/lib/auth/hashing'\nexport default function Login() { return isEnabled() ? null : null }\n`,
})
const findings = findClientNodeBuiltins(root)
expect(findings).toHaveLength(1)
expect(findings[0]).toMatchObject({ file: 'components/Login.tsx', builtin: 'crypto' })
expect(findings[0].chain).toEqual(['components/Login.tsx', 'lib/auth/hashing.ts', 'bare:crypto'])
})
it('ignores server modules, type-only imports and dynamic imports', () => {
const root = fixture({
'lib/auth/hashing.ts': `import crypto from 'crypto'\nexport type Digest = string\nexport const hash = (s: string) => crypto.createHash('sha256').update(s).digest('hex')\n`,
'lib/server-only.ts': `import { hash } from './auth/hashing'\nexport const h = hash\n`,
'components/TypeOnly.tsx': `'use client'\nimport type { Digest } from '@/lib/auth/hashing'\nexport const d: Digest = ''\n`,
'components/Lazy.tsx': `'use client'\nexport async function load() { const m = await import('@/lib/auth/hashing'); return m.hash('x') }\n`,
})
expect(findClientNodeBuiltins(root)).toEqual([])
})
it('resolves the pure sibling pattern as clean', () => {
const root = fixture({
'lib/auth/flags.ts': `export const isEnabled = () => true\n`,
'lib/auth/hashing.ts': `import crypto from 'crypto'\nexport { isEnabled } from './flags'\nexport const hash = (s: string) => crypto.createHash('sha256').update(s).digest('hex')\n`,
'components/Login.tsx': `'use client'\nimport { isEnabled } from '@/lib/auth/flags'\nexport default function Login() { return isEnabled() ? null : null }\n`,
})
expect(findClientNodeBuiltins(root)).toEqual([])
})
})
@@ -0,0 +1,137 @@
/**
* Proof that the sek-labelled-amount ratchet guard actually catches the thing.
*
* The offending fixtures live only in these strings and in an OS temp directory
* the end-to-end case creates and deletes: nothing offending is committed, so
* the guard cannot start failing on its own test data.
*/
import { describe, it, expect, afterAll } from 'vitest'
import { execFileSync } from 'node:child_process'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { findSekLabelledFxAmountsInSource as scan } from '../format-currency-sek-label.mjs'
const GUARD = path.resolve(__dirname, '..', 'format-currency-sek-label.mjs')
const tempDirs: string[] = []
afterAll(() => {
for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true })
})
describe('sek-labelled-amount guard: catches a foreign amount printed as SEK', () => {
it('flags a single-argument call on a record the file reads .currency from', () => {
const source = `
export function InvoiceTotal({ invoice }: { invoice: Invoice }) {
if (invoice.currency !== 'SEK') return null
return (
<span>
{formatCurrency(invoice.total)}
</span>
)
}
`
const findings = scan('components/invoices/Offender.tsx', source)
expect(findings).toHaveLength(1)
expect(findings[0].expr).toBe('invoice.total')
expect(findings[0].where).toBe('components/invoices/Offender.tsx:6')
})
it('sees through Number() / Math.abs() / unary minus wrappers', () => {
const source = `
const label = invoice.currency === 'EUR' ? formatCurrency(-Math.abs(Number(invoice.subtotal))) : ''
`
expect(scan('components/invoices/Wrapped.tsx', source).map((f) => f.expr)).toEqual([
'invoice.subtotal',
])
})
it('flags a ?? fallback whose right side is the foreign figure', () => {
const source = `
const label = invoice.currency ? formatCurrency(invoice.total_sek ?? invoice.total) : ''
`
expect(scan('components/invoices/Fallback.tsx', source).map((f) => f.expr)).toEqual([
'invoice.total',
])
})
it('flags amount_in_currency even with no .currency read in the file', () => {
const source = `const label = formatCurrency(line.amount_in_currency)`
expect(scan('components/bookkeeping/Foreign.tsx', source).map((f) => f.expr)).toEqual([
'line.amount_in_currency',
])
})
})
describe('sek-labelled-amount guard: leaves correct code alone', () => {
it.each([
['the currency is passed', `const s = invoice.currency ? formatCurrency(invoice.total, invoice.currency) : ''`],
['the SEK twin is formatted', `const s = invoice.currency !== 'SEK' && invoice.total_sek ? formatCurrency(invoice.total_sek) : ''`],
['a camelCase SEK twin is formatted', `const s = invoice.currency ? formatCurrency(invoice.totalSek) : ''`],
// journal_entry_lines.debit_amount is ALWAYS SEK; line.currency labels the
// document. See lib/bookkeeping/ledger-line-amount.ts (structural root 4.1).
['a ledger column is formatted', `const s = line.currency !== 'SEK' ? formatCurrency(line.debit_amount) : ''`],
['the owner has no currency in the file', `const s = formatCurrency(row.amount)`],
['a different owner carries the currency', `const s = invoice.currency ? formatCurrency(candidate.amount) : ''`],
// amount * rate is a conversion INTO kronor, so the result is SEK.
['the expression converts to SEK', `const s = invoice.currency ? formatCurrency(invoice.total * rate) : ''`],
['there is no formatCurrency call at all', `const s = invoice.currency + invoice.total`],
])('stays silent when %s', (_label, source) => {
expect(scan('components/invoices/Clean.tsx', source)).toEqual([])
})
})
describe('sek-labelled-amount guard: end to end through the CI entry point', () => {
function fixtureTree(files: Record<string, string>): string {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'sek-label-guard-'))
tempDirs.push(root)
for (const [relPath, text] of Object.entries(files)) {
const full = path.join(root, relPath)
fs.mkdirSync(path.dirname(full), { recursive: true })
fs.writeFileSync(full, text)
}
return root
}
function run(root: string): { status: number; findings: { where: string; expr: string }[] } {
try {
const stdout = execFileSync('node', [GUARD, root, '--json'], { encoding: 'utf8' })
return { status: 0, findings: JSON.parse(stdout) }
} catch (e) {
const err = e as { status: number; stdout: string }
return { status: err.status, findings: JSON.parse(err.stdout) }
}
}
const CLEAN = `
export function SupplierInvoiceRow({ invoice }: { invoice: SupplierInvoice }) {
return <td>{formatCurrency(invoice.total, invoice.currency)}</td>
}
`
const OFFENDING = `
export function SupplierInvoiceRow({ invoice }: { invoice: SupplierInvoice }) {
if (invoice.currency !== 'SEK') return null
return <td>{formatCurrency(invoice.total)}</td>
}
`
it('exits 0 on a tree with only correct calls', () => {
const result = run(fixtureTree({ 'components/suppliers/Row.tsx': CLEAN }))
expect(result.findings).toEqual([])
expect(result.status).toBe(0)
})
it('exits 1 and names the site once the offending call is introduced', () => {
const result = run(fixtureTree({ 'components/suppliers/Row.tsx': OFFENDING }))
expect(result.status).toBe(1)
expect(result.findings).toHaveLength(1)
expect(result.findings[0].where).toBe('components/suppliers/Row.tsx:4')
expect(result.findings[0].expr).toBe('invoice.total')
})
it('ignores test files, so a fixture in a spec cannot fail the build', () => {
const result = run(fixtureTree({ 'components/suppliers/__tests__/Row.test.tsx': OFFENDING }))
expect(result.findings).toEqual([])
expect(result.status).toBe(0)
})
})
@@ -0,0 +1,114 @@
/**
* Proof that the raw-reference-fetch ratchet catches the thing and leaves the
* legitimate shapes alone. Offending fixtures live only in these strings and
* in an OS temp directory the end-to-end case creates and deletes.
*/
import { describe, it, expect, afterAll } from 'vitest'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import {
findRawReferenceFetches,
findRawReferenceFetchesInSource as scan,
isClientSource,
} from '../raw-reference-fetch.mjs'
const tempDirs: string[] = []
afterAll(() => {
for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true })
})
const kinds = (source: string) => scan(source).map((f: { kind: string }) => f.kind)
describe('raw-reference-fetch: GET-shaped API calls', () => {
it('flags a bare GET of each reference path, with or without a query string', () => {
expect(kinds(`const res = await fetch('/api/settings')`)).toEqual(['api'])
expect(kinds('fetch(`/api/bookkeeping/fiscal-periods`)')).toEqual(['api'])
expect(kinds(`fetch('/api/bookkeeping/accounts?active=false')`)).toEqual(['api'])
expect(kinds(`fetch('/api/settings/booking-templates', { signal })`)).toEqual(['api'])
expect(kinds(`fetch('/api/cash-accounts?enabled_only=true', { headers: { a: '1' } })`)).toEqual(['api'])
expect(kinds(`fetch(\n '/api/customers',\n { signal: controller.signal },\n)`)).toEqual(['api'])
})
it('ignores writes: they go through the API and then invalidate the cache', () => {
expect(kinds(`fetch('/api/settings', { method: 'PUT', body })`)).toEqual([])
expect(kinds(`fetch('/api/customers', { method: 'POST', headers: { 'content-type': 'x' }, body })`)).toEqual([])
expect(kinds("fetch(`/api/settings/booking-templates/${id}/touch`, { method: 'POST' })")).toEqual([])
expect(kinds(`fetch('/api/articles/' + id, { method: 'DELETE' })`)).toEqual([])
})
it('does not confuse sub-resources or lookalike paths with the reference lists', () => {
expect(kinds("fetch(`/api/settings/booking-templates/${id}`)")).toEqual([])
expect(kinds(`fetch('/api/settings/banking')`)).toEqual([])
expect(kinds(`fetch('/api/customers/abc')`)).toEqual([])
expect(kinds(`fetch('/api/bookkeeping/accounts/bas-catalog')`)).toEqual([])
})
})
describe('raw-reference-fetch: regex safety', () => {
it('scans a pathological near-miss in linear time (no catastrophic backtracking)', () => {
// A fetch call that never closes, padded with the whitespace/comma mix
// the old `\s*,?\s*\)` tail was ambiguous on.
const source = `fetch('/api/settings'${' ,'.repeat(5000)}${' '.repeat(5000)}X`
const start = performance.now()
expect(scan(source)).toEqual([])
expect(performance.now() - start).toBeLessThan(200)
})
})
describe('raw-reference-fetch: use-client detection is linear too', () => {
it('handles a long run of unclosed block comments without backtracking', () => {
const source = `${'/* '.repeat(3000)}'use client'\n`
const start = performance.now()
expect(isClientSource(source)).toBe(false)
expect(performance.now() - start).toBeLessThan(200)
})
it('still sees the directive behind closed comments', () => {
expect(isClientSource(`/* a */ /* b */\n// c\n'use client'\n`)).toBe(true)
})
})
describe('raw-reference-fetch: browser-side table reads', () => {
const clientRead = `'use client'\nimport x from 'y'\nconst { data } = await supabase.from('fiscal_periods').select('*').eq('company_id', id)`
it('flags a select on a reference table only in a use-client file', () => {
expect(kinds(clientRead)).toEqual(['table'])
expect(kinds(clientRead.replace(`'use client'\n`, ''))).toEqual([])
})
it('recognises the directive behind leading comments and double quotes', () => {
expect(isClientSource(`// header\n/* block */\n"use client"\n`)).toBe(true)
expect(isClientSource(`import a from 'b'\n'use client'`)).toBe(false)
})
it('leaves inserts and updates alone', () => {
expect(kinds(`'use client'\nawait supabase.from('fiscal_periods').insert(row)`)).toEqual([])
expect(kinds(`'use client'\nawait supabase.from('company_settings').update(patch).eq('company_id', id)`)).toEqual([])
})
})
describe('raw-reference-fetch: file scan', () => {
it('reports offending files relative to the root and skips sanctioned, api and test files', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'raw-ref-'))
tempDirs.push(root)
const write = (rel: string, content: string) => {
const full = path.join(root, rel)
fs.mkdirSync(path.dirname(full), { recursive: true })
fs.writeFileSync(full, content)
}
write('components/Bad.tsx', `'use client'\nfetch('/api/settings')`)
write('components/Fine.tsx', `'use client'\nfetch('/api/settings', { method: 'PUT' })`)
write('components/__tests__/Bad.test.tsx', `fetch('/api/settings')`)
write('components/Bad.test.ts', `fetch('/api/settings')`)
write('lib/reference-data/fetchers.ts', `fetch('/api/settings')`)
write('app/api/x/route.ts', `fetch('/api/settings')`)
write('app/(dashboard)/reports/page.tsx', `'use client'\nconst r = await fetch('/api/bookkeeping/fiscal-periods')`)
write('lib/server-thing.ts', `await supabase.from('company_settings').select('*')`)
expect(findRawReferenceFetches(root)).toEqual([
'app/(dashboard)/reports/page.tsx',
'components/Bad.tsx',
])
})
})
+349
View File
@@ -0,0 +1,349 @@
#!/usr/bin/env node
/**
* Guard: an un-hinted PostgREST embed between two tables that share more than
* one foreign key.
*
* PostgREST resolves `.from('a').select('b(...)')` by looking for THE foreign
* key between a and b, in either direction. When several exist it refuses the
* request with PGRST201 ("Could not embed because more than one relationship
* was found") instead of picking one. The embed must then name the
* relationship, and PostgREST accepts two spellings for that:
*
* b!a_b_id_fkey(...) the constraint name
* b!b_column_id(...) the foreign key COLUMN name
*
* Both are in use here (`fiscal_periods!journal_entries_fiscal_period_id_fkey`
* in lib/transactions/inbox-underlag.ts, `journal_entries!opening_balance_entry_id`
* in lib/import/opening-balance/cascade.ts), so either counts as hinted.
* `!inner` / `!left` are join modifiers, not hints, and do not disambiguate.
*
* Why a static guard is the only thing that can catch this: the failure is a
* runtime PostgREST decision. Unit tests mock the Supabase client and hand back
* an already-joined row shape, so they never resolve a relationship; pg-real
* tests talk to Postgres directly and never go through PostgREST at all. The
* class has now been reasoned about three times and shipped broken twice, both
* on journal_entries -> fiscal_periods, ambiguous since migration
* 20240101000019 added fiscal_periods.closing_entry_id and
* fiscal_periods.opening_balance_entry_id:
* - lib/transactions/inbox-underlag.ts (fixed 2026-08-31, PR #2022): the
* nightly underlag cron read an empty locked-set and then tried to link
* documents into locked periods, ~60 trigger rejections a night.
* - lib/core/documents/supplier-invoice-underlag.ts (fixed 2026-09-01): the
* error was dropped on the floor, so supplier-invoice underlag never once
* anchored in production and users kept seeing "Underlag saknas" on a
* verifikat that plainly showed the invoice PDF.
*
* The ambiguous pairs are DERIVED from supabase/migrations/*.sql rather than
* hardcoded, so a migration that adds a second foreign key between two tables
* arms the guard for that pair on the same commit. To check the derivation
* against the live schema:
*
* select least(s.relname, t.relname) a, greatest(s.relname, t.relname) b, count(*)
* from pg_constraint c
* join pg_class s on s.oid = c.conrelid
* join pg_class t on t.oid = c.confrelid
* join pg_namespace ns on ns.oid = s.relnamespace and ns.nspname = 'public'
* join pg_namespace nt on nt.oid = t.relnamespace and nt.nspname = 'public'
* where c.contype = 'f'
* group by 1, 2 having count(*) > 1 order by 1, 2;
*
* On 2026-09-03 that returned the same 17 pairs the parser derives. Composite
* foreign keys count: until 2026-09-03 the parser only read single-column
* `FOREIGN KEY (col)`, so the composite (sales_order_id, company_id) guard in
* 20260902180000_sales_orders_hardening.sql was invisible to it and the
* un-hinted `items:sales_order_items(*)` embeds shipped, taking every
* kundorder list and detail load down with PGRST201 (fixed 2026-09-03).
*
* No baseline: the count is 0, any new un-hinted ambiguous embed is a hard
* failure.
*/
import fs from 'node:fs'
import path from 'node:path'
import ts from 'typescript'
const SCAN_DIRS = ['app', 'components', 'contexts', 'extensions', 'lib', 'scripts']
const IGNORE_DIRS = new Set([
'node_modules',
'.next',
'.git',
'dist',
'build',
'coverage',
'__tests__',
])
/** Undirected pair key: PostgREST considers foreign keys in both directions. */
export const pairKey = (a, b) => (a < b ? `${a}|${b}` : `${b}|${a}`)
const stripSql = (sql) => sql.replace(/--[^\n]*/g, '').replace(/\/\*[\s\S]*?\*\//g, '')
const normIdent = (t) => t.replace(/"/g, '').replace(/^public\./i, '').toLowerCase()
/**
* Foreign keys declared across the migration history, as a map of
* `<table>.<column>` -> target table. Keying on the column (rather than
* counting statements) makes the repeated idempotent re-adds this repo is full
* of collapse onto one edge, which is what Postgres ends up with.
*
* Only the shapes that actually appear in supabase/migrations are parsed:
* inline `REFERENCES` in a CREATE TABLE column, table-level
* `FOREIGN KEY (col) REFERENCES`, `ADD COLUMN ... REFERENCES`, and
* `ADD CONSTRAINT ... FOREIGN KEY (col) REFERENCES`. Drops are honoured so a
* relationship that was removed again does not keep a pair armed forever.
*/
export function deriveForeignKeys(migrationsDir) {
const edges = new Map()
// Constraint name -> edge key, so DROP CONSTRAINT can find what it removes.
// Unnamed foreign keys get Postgres's default `<table>_<column>_fkey`.
const byConstraint = new Map()
// `column` is one column or a composite list ("sales_order_id, company_id").
// A composite foreign key is its own edge, distinct from a single-column one
// on its leading column: PostgREST counts both, which is what made
// sales_order_items -> sales_orders ambiguous (migration 20260902180000)
// while this parser, then single-column only, still derived one edge.
const addEdge = (table, column, target, constraintName) => {
const columns = column.split(',').map((c) => normIdent(c.trim())).filter(Boolean)
const key = `${table}.${columns.join(',')}`
edges.set(key, target)
byConstraint.set(constraintName ?? `${table}_${columns.join('_')}_fkey`, key)
}
let files
try {
files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith('.sql')).sort()
} catch {
return edges
}
for (const file of files) {
const sql = stripSql(fs.readFileSync(path.join(migrationsDir, file), 'utf8'))
for (const raw of sql.split(';')) {
const stmt = raw.trim()
if (!stmt) continue
const created = stmt.match(/create\s+table\s+(?:if\s+not\s+exists\s+)?([\w".]+)\s*\(/i)
const altered = stmt.match(/alter\s+table\s+(?:if\s+exists\s+)?(?:only\s+)?([\w".]+)/i)
const dropped = stmt.match(/drop\s+table\s+(?:if\s+exists\s+)?([\w".]+)/i)
if (created) {
const table = normIdent(created[1])
// `col uuid references public.other(id)` inside the column list.
for (const m of stmt.matchAll(/(?:^|,)\s*([\w"]+)\s+[^,()]*?references\s+([\w".]+)/gis)) {
addEdge(table, normIdent(m[1]), normIdent(m[2]))
}
// `foreign key (col[, col]) references public.other(id[, id])` as a
// table constraint, named or not.
for (const m of stmt.matchAll(
/(?:constraint\s+([\w"]+)\s+)?foreign\s+key\s*\(\s*([\w",\s]+?)\s*\)\s*references\s+([\w".]+)/gi,
)) {
addEdge(table, m[2], normIdent(m[3]), m[1] && normIdent(m[1]))
}
continue
}
if (altered) {
const table = normIdent(altered[1])
for (const m of stmt.matchAll(
/(?:add\s+constraint\s+([\w"]+)\s+)?foreign\s+key\s*\(\s*([\w",\s]+?)\s*\)\s*references\s+([\w".]+)/gi,
)) {
addEdge(table, m[2], normIdent(m[3]), m[1] && normIdent(m[1]))
}
for (const m of stmt.matchAll(
/add\s+column\s+(?:if\s+not\s+exists\s+)?([\w"]+)\s+[^,;]*?references\s+([\w".]+)/gi,
)) {
addEdge(table, normIdent(m[1]), normIdent(m[2]))
}
for (const m of stmt.matchAll(/drop\s+column\s+(?:if\s+exists\s+)?([\w"]+)/gi)) {
// Postgres drops every foreign key the column takes part in, so a
// composite edge listing it goes too, not only the single-column key.
const column = normIdent(m[1])
for (const key of [...edges.keys()]) {
if (!key.startsWith(`${table}.`)) continue
if (!key.slice(table.length + 1).split(',').includes(column)) continue
edges.delete(key)
for (const [name, edge] of [...byConstraint]) {
if (edge === key) byConstraint.delete(name)
}
}
}
for (const m of stmt.matchAll(/drop\s+constraint\s+(?:if\s+exists\s+)?([\w"]+)/gi)) {
const edge = byConstraint.get(normIdent(m[1]))
if (edge) edges.delete(edge)
}
continue
}
if (dropped) {
const table = normIdent(dropped[1])
for (const [key, target] of [...edges]) {
if (key.startsWith(`${table}.`) || target === table) edges.delete(key)
}
}
}
}
return edges
}
/** Table pairs joined by more than one foreign key, as `pairKey` strings. */
export function deriveAmbiguousPairs(migrationsDir) {
const counts = new Map()
for (const [key, target] of deriveForeignKeys(migrationsDir)) {
const source = key.slice(0, key.indexOf('.'))
// auth.users and other non-public tables are never embedded through PostgREST here.
if (source.includes('.') || target.includes('.')) continue
const pair = pairKey(source, target)
counts.set(pair, (counts.get(pair) ?? 0) + 1)
}
return new Set([...counts].filter(([, n]) => n > 1).map(([pair]) => pair))
}
const HEAD_CHARS = /[\w!]/
// An embed head is preceded by the start of the select string, a separator, or
// the `:` of an alias. `fiscal_period:fiscal_periods(...)` is the shape both
// production bugs were written in, so `:` MUST be a delimiter here.
const HEAD_DELIMITERS = new Set([',', ':', '(', ' ', '\t', '\n', '\r'])
const JOIN_MODIFIERS = new Set(['inner', 'left'])
/**
* The embed head immediately before `index` (which points at its `(`), or null
* when the paren does not open an embed.
*/
function headBefore(select, index) {
let end = index
while (end > 0 && /\s/.test(select[end - 1])) end--
let start = end
while (start > 0 && HEAD_CHARS.test(select[start - 1])) start--
if (start === end) return null
if (start > 0 && !HEAD_DELIMITERS.has(select[start - 1])) return null
const [target, ...modifiers] = select.slice(start, end).split('!')
if (!target) return null
return { target: target.toLowerCase(), hinted: modifiers.some((m) => !JOIN_MODIFIERS.has(m)) }
}
/**
* Every embed in one select string, paired with the table it is embedded FROM.
* Nested embeds resolve against their enclosing embed, not against the root.
*/
export function parseEmbeds(select, rootTable) {
const embeds = []
const stack = [rootTable]
for (let i = 0; i < select.length; i++) {
if (select[i] === '(') {
const head = headBefore(select, i)
const from = stack[stack.length - 1]
if (head && from) embeds.push({ from, target: head.target, hinted: head.hinted })
stack.push(head ? head.target : null)
} else if (select[i] === ')') {
if (stack.length > 1) stack.pop()
}
}
return embeds
}
/**
* The table a `.select()` call reads from, resolved by walking ITS OWN method
* chain back to `.from()`. Never the nearest preceding `.from()` in the file:
* `.from('journal_entry_lines').select('... journal_entries!inner(...)')` sits
* a few lines below an unrelated `.from('journal_entries')` in
* scripts/seed-demo-account.ts, and pairing by proximity flags it wrongly.
*/
function fromTableOfChain(selectCall) {
let node = selectCall.expression.expression
while (node) {
if (ts.isCallExpression(node)) {
const callee = node.expression
if (ts.isPropertyAccessExpression(callee) && callee.name.text === 'from') {
const arg = node.arguments[0]
return arg && ts.isStringLiteralLike(arg) ? arg.text.toLowerCase() : null
}
node = ts.isPropertyAccessExpression(callee) ? callee.expression : callee
continue
}
if (ts.isPropertyAccessExpression(node)) {
node = node.expression
continue
}
return null
}
return null
}
/**
* Un-hinted embeds of an ambiguous pair in one file's source, as
* `{ line, from, target }`. Exported for the unit test.
*/
export function findAmbiguousEmbedsInSource(sourceText, fileName, ambiguousPairs) {
const source = ts.createSourceFile(
fileName,
sourceText,
ts.ScriptTarget.Latest,
true,
fileName.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
)
const findings = []
const visit = (node) => {
if (
ts.isCallExpression(node) &&
ts.isPropertyAccessExpression(node.expression) &&
node.expression.name.text === 'select' &&
node.arguments.length > 0 &&
ts.isStringLiteralLike(node.arguments[0])
) {
const from = fromTableOfChain(node)
if (from) {
for (const embed of parseEmbeds(node.arguments[0].text, from)) {
if (embed.hinted) continue
if (!ambiguousPairs.has(pairKey(embed.from, embed.target))) continue
const pos = source.getLineAndCharacterOfPosition(node.getStart(source))
findings.push({ line: pos.line + 1, from: embed.from, target: embed.target })
}
}
}
ts.forEachChild(node, visit)
}
visit(source)
return findings
}
function walk(dir, out) {
let entries
try {
entries = fs.readdirSync(dir, { withFileTypes: true })
} catch {
return out
}
for (const entry of entries) {
if (entry.name.startsWith('.') || IGNORE_DIRS.has(entry.name)) continue
const full = path.join(dir, entry.name)
if (entry.isDirectory()) walk(full, out)
else if (/\.tsx?$/.test(entry.name) && !/\.test\.tsx?$/.test(entry.name)) out.push(full)
}
return out
}
/** Findings across the repo, as `{ where, from, target }`, sorted. */
export function findAmbiguousEmbeds(root) {
const ambiguousPairs = deriveAmbiguousPairs(path.join(root, 'supabase', 'migrations'))
if (ambiguousPairs.size === 0) return []
const findings = []
for (const dir of SCAN_DIRS) {
for (const file of walk(path.join(root, dir), [])) {
const sourceText = fs.readFileSync(file, 'utf8')
if (!sourceText.includes('.select(')) continue
const relPath = path.relative(root, file).split(path.sep).join('/')
for (const finding of findAmbiguousEmbedsInSource(sourceText, file, ambiguousPairs)) {
findings.push({
where: `${relPath}:${finding.line}`,
from: finding.from,
target: finding.target,
})
}
}
}
return findings.sort((a, b) => a.where.localeCompare(b.where))
}
+67
View File
@@ -0,0 +1,67 @@
{
"_comment": "Ratchet baseline for scripts/checks/no-new-antipatterns.mjs. These counts may only decrease. Re-run with --update after a migration lowers them. Goal: both reach 0 (A1 route-auth campaign, D1 rounding codemod).",
"rawRouteAuth": {
"count": 1,
"files": [
"app/api/mcp-oauth/authorize/route.ts"
]
},
"naiveOreRound": {
"count": 615
},
"handRolledInvariants": {
"count": 113
},
"ledgerScanningReports": {
"count": 4,
"files": [
"lib/bokslut/assets/asset-service.ts",
"lib/bokslut/reserves/periodiseringsfond-service.ts",
"lib/bokslut/tax-provision/bolagsskatt-calculator.ts",
"lib/bokslut/tax-provision/sarskild-loneskatt-calculator.ts"
]
},
"dialogOverflowRisk": {
"count": 7,
"files": [
"app/(dashboard)/supplier-invoices/[id]/page.tsx",
"components/bookkeeping/JournalEntryForm.tsx",
"components/invoices/PaymentBookingDialog.tsx",
"components/invoices/SendInvoiceDialog.tsx",
"components/reports/views/index.tsx",
"components/salary/NewEmployeeDialog.tsx",
"components/transactions/InvoiceMatchDialog.tsx"
]
},
"rawReferenceFetch": {
"count": 0,
"files": []
},
"providerHosts": {
"count": 22,
"files": [
"app/api/connect/bank/[...path]/route.ts",
"extensions/general/bolagsverket/lib/client.ts",
"extensions/general/enable-banking/index.ts",
"extensions/general/enable-banking/lib/api-client.ts",
"extensions/general/mail/lib/gmail-client.ts",
"extensions/general/skatteverket/index.ts",
"extensions/general/skatteverket/lib/connector-mode.ts",
"extensions/general/skatteverket/lib/ombud-client.ts",
"extensions/general/skatteverket/lib/skattekonto-client.ts",
"extensions/general/tic/index.ts",
"extensions/general/tic/lib/bankid-client.ts",
"extensions/general/tic/lib/bankid-types.ts",
"extensions/general/whatsapp-inbox/lib/graph-api.ts",
"lib/connect/upstreams/enable-banking-jwt.ts",
"lib/invoices/transports/qvalia.ts",
"lib/providers/bjornlunden/config.ts",
"lib/providers/bjornlunden/oauth.ts",
"lib/providers/bokio/client.ts",
"lib/providers/bokio/config.ts",
"lib/providers/briox/config.ts",
"lib/providers/fortnox/config.ts",
"lib/providers/visma/config.ts"
]
}
}
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/env node
/**
* Guard: a 'use client' module whose static import closure reaches a Node
* builtin (crypto, buffer, vm, stream, fs, ...).
*
* Turbopack polyfills those for the browser (crypto-browserify, vm-browserify,
* Buffer: ~327 KB uncompressed) the moment ANY client module can reach them,
* and the polyfill chunk then ships with every route that renders the
* component. Before the 2026-08-26 split, lib/auth/bankid.ts (login,
* register, security settings), lib/import/bank-file/parser.ts (bank import
* history), lib/salary/personnummer.ts (via tax-column, the employee forms)
* and lib/auth/api-keys.ts (the API key panel) each did this for a function
* that never touched crypto. The fix is always the same: move the pure part
* into a sibling module without the Node import and import that from the
* client (see bankid-flags.ts, bank-file/formats.ts, personnummer-format.ts,
* api-key-scopes.ts).
*
* No baseline: the count is 0, any new reacher is a hard failure. The walk
* is the same static closure scripts/perf/client-import-closure.mjs prints.
*/
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { buildGraph, clientReachers } from '../perf/client-import-closure.mjs'
export const NODE_BUILTINS = ['crypto', 'node:crypto', 'buffer', 'node:buffer', 'vm', 'node:vm', 'stream', 'node:stream', 'fs', 'node:fs', 'path', 'node:path', 'child_process', 'node:child_process']
/** [{ file, builtin, chain }] for every client file reaching a builtin. */
export function findClientNodeBuiltins(root) {
const graph = buildGraph(root)
const findings = []
for (const builtin of NODE_BUILTINS) {
for (const [file, chain] of clientReachers(graph, `bare:${builtin}`, root)) {
findings.push({ file, builtin, chain })
}
}
return findings.sort((a, b) => a.file.localeCompare(b.file) || a.builtin.localeCompare(b.builtin))
}
const isMain = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
if (isMain) {
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
const findings = findClientNodeBuiltins(root)
for (const f of findings) console.log(`${f.file} -> ${f.builtin}\n ${f.chain.join('\n > ')}`)
console.log(`${findings.length} client file(s) reach a Node builtin`)
process.exit(findings.length ? 1 : 0)
}
+4
View File
@@ -0,0 +1,4 @@
{
"totalErrors": 0,
"perRule": {}
}
+113
View File
@@ -0,0 +1,113 @@
/**
* Guards for the app/api/extensions carve-out.
*
* Physical routes under `app/api/extensions/<extensionId>/` are the ONE
* sanctioned place where app code may import from `@/extensions/` (see the
* "Check no core imports from extensions" step in
* .github/workflows/core-build.yml, which exempts exactly this directory).
* They exist because Vercel crons and OAuth callbacks need concrete file
* routes; the `ext/[...path]` dispatcher covers everything else.
*
* The carve-out has two invariants this guard enforces:
*
* 1. cross-extension-import (hard fail, count 0 today): a route under
* app/api/extensions/<id>/ may only import from its own extension,
* `@/extensions/<sector>/<id>/`. Reaching into another extension from
* here would create hidden coupling the registry cannot see.
*
* 2. ungated-extension-route (ratchet, may only shrink): these routes
* compile into EVERY build, including the core-with-zero-extensions CI
* build; disabling an extension in extensions.config.json only removes
* it from the runtime registry, never from the filesystem. So a route
* that executes extension code must first check
* `extensionRegistry.get('<id>')` (after loadExtensions()) and refuse
* when the extension is not registered: otherwise a disabled extension
* still exposes a live, invokable surface (this is exactly how the
* disabled push-notifications extension shipped an always-armed cron).
* Pre-existing routes are allowlisted below; the set may only shrink as
* gates are added. A NEW route must ship with the gate.
*/
import fs from 'node:fs'
import path from 'node:path'
/**
* Routes that predate the enablement-gate requirement. Remove an entry once
* its route checks `extensionRegistry.get('<id>')`; never add one.
*/
export const UNGATED_EXTENSION_ROUTES = new Set([
'app/api/extensions/cloud-backup/auto-sync/cron/route.ts',
'app/api/extensions/enable-banking/callback/route.ts',
'app/api/extensions/enable-banking/sync/cron/route.ts',
'app/api/extensions/skatteverket/agi/kvittenser/cron/route.ts',
'app/api/extensions/skatteverket/skattekonto/sync/cron/route.ts',
'app/api/extensions/skatteverket/vat/kvittenser/cron/route.ts',
'app/api/extensions/stripe/callback/route.ts',
'app/api/extensions/stripe/sync/cron/route.ts',
'app/api/extensions/stripe/transactions/cron/route.ts',
])
const EXTENSION_IMPORT_RE = /from\s+['"]@\/extensions\/[^/'"]+\/([^/'"]+)\//g
function walkRouteFiles(dir, out = []) {
let entries
try {
entries = fs.readdirSync(dir, { withFileTypes: true })
} catch {
return out
}
for (const e of entries) {
const full = path.join(dir, e.name)
if (e.isDirectory()) {
if (e.name === '__tests__') continue
walkRouteFiles(full, out)
} else if (e.name === 'route.ts') {
out.push(full)
}
}
return out
}
/**
* Scan app/api/extensions/<id>/ physical routes.
* Returns { crossImports: [{ file, imported }], ungated: [file] } with
* repo-relative, forward-slash paths.
*/
export function findExtensionRouteFindings(ROOT) {
const base = path.join(ROOT, 'app', 'api', 'extensions')
const rel = (p) => path.relative(ROOT, p).split(path.sep).join('/')
const crossImports = []
const ungated = []
for (const file of walkRouteFiles(base)) {
const relPath = rel(file)
const segments = relPath.split('/')
const extensionId = segments[3] // app/api/extensions/<id>/...
// The dispatcher and the generic dynamic-segment routes go through the
// registry already and import nothing from @/extensions/.
if (!extensionId || extensionId === 'ext' || extensionId.startsWith('[')) continue
const src = fs.readFileSync(file, 'utf8')
let importsOwnExtension = false
for (const match of src.matchAll(EXTENSION_IMPORT_RE)) {
if (match[1] === extensionId) {
importsOwnExtension = true
} else {
crossImports.push({ file: relPath, imported: match[1] })
}
}
if (!importsOwnExtension && crossImports.every((c) => c.file !== relPath)) continue
const hasGate =
src.includes(`extensionRegistry.get('${extensionId}')`) ||
src.includes(`extensionRegistry.get("${extensionId}")`)
if (!hasGate) ungated.push(relPath)
}
crossImports.sort((a, b) => a.file.localeCompare(b.file))
ungated.sort()
return { crossImports, ungated }
}
@@ -0,0 +1,277 @@
#!/usr/bin/env node
/**
* Guard: a foreign amount printed with the SEK symbol.
*
* `formatCurrency(amount, currency = 'SEK')` (lib/utils.ts) defaults its second
* argument, and that default is deliberate: `formatCurrency(amount)` is the
* right call on the ~hundreds of values that ARE kronor, and sv-SE/SEK output
* in both locales is a Swedish accounting convention rather than a UI string
* (.claude/rules/i18n.md). What the default cannot do is notice when the number
* handed to it came off a record that carries its own currency: there the
* omitted argument silently relabels 100 EUR as "100,00 kr".
*
* So this check does not touch the signature; it fails CI on the ONE shape the
* default cannot defend against:
*
* 1. A single-argument call whose amount is read off an owner that the SAME
* file also reads `.currency` from. The file demonstrably knows the unit
* and dropped it: `formatCurrency(invoice.total)` two lines under
* `invoice.currency !== 'SEK'`. The currency argument (or the `*_sek`
* twin) is right there.
* 2. A single-argument call on `amount_in_currency`, which is by definition
* the foreign figure on a journal entry line and never kronor.
*
* NOT flagged, on purpose:
* - `debit_amount` / `credit_amount` (and the bare `debit` / `credit` used by
* preview rows). Journal entry line amounts are ALWAYS SEK; `line.currency`
* labels the DOCUMENT, not the figure (lib/bookkeeping/ledger-line-amount.ts).
* A file that correctly reads both must not be accused.
* - Any `*_sek` / `*Sek` field: that IS the kronor twin, and rendering it with
* the SEK symbol is exactly right (see the five call sites this rule was
* calibrated against, e.g. components/invoices/SendInvoiceDialog.tsx).
* - A multiplicative expression (`total * exchangeRate`): that is a
* conversion INTO kronor, so the result is SEK.
* - Owners the file never reads a currency from. Judging a bare local like
* `formatCurrency(totalDebit)` would need whole-program dataflow, and
* guessing there is how a guard earns its way onto an ignore list.
*
* Escape hatch: add `<relative path>#<owner>.<field>` to SANCTIONED below with
* a justification. Line numbers are deliberately not part of the key, so an
* unrelated edit above the call cannot break the entry.
*
* Usage:
* node scripts/checks/format-currency-sek-label.mjs # scan the repo
* node scripts/checks/format-currency-sek-label.mjs <dir> # scan a tree
* node scripts/checks/format-currency-sek-label.mjs <dir> --json
*
* Wired into `npm run check:guards` (scripts/checks/no-new-antipatterns.mjs).
*/
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import ts from 'typescript'
/** Subtrees scanned, relative to the root passed in. */
const SCAN_DIRS = ['lib', 'app', 'components', 'extensions']
const IGNORE_DIRS = new Set(['node_modules', '.next', '.git', 'dist', 'build', 'coverage'])
/**
* Deliberate single-argument calls on a currency-bearing owner, keyed
* `<relative path>#<owner>.<field>`. Empty today: every such call in the repo
* formats a `*_sek` twin or a ledger column and is excluded by rule.
*/
const SANCTIONED = new Set()
/** Fields whose value is kronor no matter what currency the owner carries. */
const ALWAYS_SEK_FIELDS = new Set([
// Journal entry line columns. See lib/bookkeeping/ledger-line-amount.ts.
'debit_amount',
'credit_amount',
'debit',
'credit',
'total_debit',
'total_credit',
])
/** By definition the foreign figure: never kronor, so never single-argument. */
const NEVER_SEK_FIELDS = new Set(['amount_in_currency'])
/** Calls that pass the unit through unchanged, so we look at their argument. */
const UNIT_PRESERVING_CALLS = new Set([
'Number',
'parseFloat',
'parseInt',
'abs',
'round',
'trunc',
'roundOre',
'roundToOre',
])
const UNWRAP_KINDS = new Set([
ts.SyntaxKind.ParenthesizedExpression,
ts.SyntaxKind.AsExpression,
ts.SyntaxKind.SatisfiesExpression,
ts.SyntaxKind.NonNullExpression,
ts.SyntaxKind.TypeAssertionExpression,
])
function walk(dir, out = []) {
let entries
try {
entries = fs.readdirSync(dir, { withFileTypes: true })
} catch {
return out
}
for (const entry of entries) {
if (entry.name.startsWith('.')) continue
const full = path.join(dir, entry.name)
if (entry.isDirectory()) {
if (!IGNORE_DIRS.has(entry.name)) walk(full, out)
} else if (/\.tsx?$/.test(entry.name) && !/\.test\.tsx?$/.test(entry.name)) {
out.push(full)
}
}
return out
}
/** `a.b.c` -> { owner: 'a.b', field: 'c' }; anything not identifier-rooted -> null. */
function accessChain(node) {
const parts = []
let current = node
while (ts.isPropertyAccessExpression(current)) {
parts.unshift(current.name.text)
current = current.expression
}
if (!ts.isIdentifier(current) || parts.length === 0) return null
const field = parts.pop()
return { owner: [current.text, ...parts].join('.'), field }
}
/**
* Every property access whose value the expression forwards in the SAME unit.
* Both branches of `??` / `||` / `?:` and both operands of `+` / `-` count: a
* `total_sek ?? total` fallback is precisely the shape that prints a foreign
* number when the SEK twin is missing.
*/
function unitBearingAccesses(node, out = []) {
if (!node) return out
if (UNWRAP_KINDS.has(node.kind) && node.expression) return unitBearingAccesses(node.expression, out)
if (
ts.isPrefixUnaryExpression(node) &&
(node.operator === ts.SyntaxKind.MinusToken || node.operator === ts.SyntaxKind.PlusToken)
) {
return unitBearingAccesses(node.operand, out)
}
if (ts.isConditionalExpression(node)) {
unitBearingAccesses(node.whenTrue, out)
unitBearingAccesses(node.whenFalse, out)
return out
}
if (ts.isBinaryExpression(node)) {
const kind = node.operatorToken.kind
const forwards =
kind === ts.SyntaxKind.QuestionQuestionToken ||
kind === ts.SyntaxKind.BarBarToken ||
kind === ts.SyntaxKind.PlusToken ||
kind === ts.SyntaxKind.MinusToken
// Multiplication / division is a CONVERSION (amount * exchangeRate), so the
// result is no longer the operand's unit and must not be judged as one.
if (!forwards) return out
unitBearingAccesses(node.left, out)
unitBearingAccesses(node.right, out)
return out
}
if (ts.isCallExpression(node) && node.arguments.length >= 1) {
const callee = node.expression
const name = ts.isIdentifier(callee)
? callee.text
: ts.isPropertyAccessExpression(callee)
? callee.name.text
: ''
if (UNIT_PRESERVING_CALLS.has(name)) return unitBearingAccesses(node.arguments[0], out)
return out
}
if (ts.isPropertyAccessExpression(node)) {
const chain = accessChain(node)
if (chain) out.push(chain)
return out
}
return out
}
function isSekTwin(field) {
return /(?:_sek|sek)$/i.test(field)
}
/** Findings for one source file. `relPath` is only used to build the message. */
export function findSekLabelledFxAmountsInSource(relPath, text) {
if (!text.includes('formatCurrency(')) return []
const source = ts.createSourceFile(
relPath,
text,
ts.ScriptTarget.Latest,
true,
relPath.endsWith('.tsx') ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
)
// Owners this file reads a currency off: `invoice.currency`, `tx?.currency`,
// `data.invoice.currency`. Matched as a full owner path, not just the root
// identifier, so knowing `invoice.currency` says nothing about `line.amount`.
const currencyOwners = new Set()
const calls = []
const collect = (node) => {
if (ts.isPropertyAccessExpression(node) && node.name.text === 'currency') {
const chain = accessChain(node)
if (chain) currencyOwners.add(chain.owner)
}
if (ts.isCallExpression(node) && node.arguments.length === 1) {
const callee = node.expression
const name = ts.isIdentifier(callee)
? callee.text
: ts.isPropertyAccessExpression(callee)
? callee.name.text
: ''
if (name === 'formatCurrency') calls.push(node)
}
ts.forEachChild(node, collect)
}
collect(source)
const findings = []
for (const call of calls) {
for (const { owner, field } of unitBearingAccesses(call.arguments[0])) {
if (SANCTIONED.has(`${relPath}#${owner}.${field}`)) continue
const foreignByName = NEVER_SEK_FIELDS.has(field)
if (!foreignByName) {
if (!currencyOwners.has(owner)) continue
if (ALWAYS_SEK_FIELDS.has(field) || isSekTwin(field)) continue
}
const line = source.getLineAndCharacterOfPosition(call.getStart(source)).line + 1
findings.push({
where: `${relPath}:${line}`,
expr: `${owner}.${field}`,
reason: foreignByName
? `${field} is the amount in the document's own currency, never SEK`
: `${relPath.split('/').pop()} reads ${owner}.currency, so ${owner}.${field} may not be SEK`,
})
}
}
return findings
}
/** Findings across `SCAN_DIRS` under `root`, sorted and de-duplicated. */
export function findSekLabelledFxAmounts(root) {
const files = SCAN_DIRS.flatMap((dir) => walk(path.join(root, dir)))
const findings = []
for (const file of files) {
const relPath = path.relative(root, file).split(path.sep).join('/')
findings.push(...findSekLabelledFxAmountsInSource(relPath, fs.readFileSync(file, 'utf8')))
}
const seen = new Set()
return findings
.filter((f) => {
const key = `${f.where}|${f.expr}`
if (seen.has(key)) return false
seen.add(key)
return true
})
.sort((a, b) => `${a.where}|${a.expr}`.localeCompare(`${b.where}|${b.expr}`))
}
// Standalone entry point: also what the unit test drives, so the CI path and
// the tested path are the same code.
if (process.argv[1] && fileURLToPath(import.meta.url) === path.resolve(process.argv[1])) {
const args = process.argv.slice(2)
const json = args.includes('--json')
const root = args.find((a) => !a.startsWith('--')) ?? path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
const findings = findSekLabelledFxAmounts(root)
if (json) {
console.log(JSON.stringify(findings, null, 2))
} else if (findings.length) {
for (const f of findings) console.error(`${f.where} formatCurrency(${f.expr}): ${f.reason}`)
} else {
console.log('✓ no SEK-labelled foreign amounts found.')
}
process.exit(findings.length ? 1 : 0)
}
File diff suppressed because it is too large Load Diff
+123
View File
@@ -0,0 +1,123 @@
#!/usr/bin/env node
/**
* Ratchet guard for ESLint errors (sibling of no-new-antipatterns.mjs).
*
* `npm run lint` was never wired into CI, so ~60 pre-existing errors
* accumulated across the repo. Fixing them all in one PR is churn; gating raw
* `eslint` would break every PR until then. So: ratchet. Error counts are
* tracked per rule in a committed baseline and can only go DOWN, never up:
* a PR introducing a NEW error of any rule fails CI, while legacy errors are
* burned down independently.
*
* Warnings stay advisory (only `--quiet` errors are counted).
*
* Known tradeoff: counts are per-rule repo-wide, not per-location: a PR that
* fixes one legacy error of a rule can absorb one NEW error of the same rule
* without tripping the gate. Acceptable for a burn-down ratchet; tighten to
* per-file fingerprints if that ever bites.
*
* Usage:
* node scripts/checks/no-new-lint-errors.mjs # check (CI)
* node scripts/checks/no-new-lint-errors.mjs --update # re-baseline after fixing legacy errors
*
* Exit code 1 if any rule's error count exceeds its baseline.
*/
import fs from 'node:fs'
import path from 'node:path'
import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
const BASELINE_PATH = path.join(ROOT, 'scripts', 'checks', 'eslint-baseline.json')
function runEslint() {
const eslintBin = path.join(ROOT, 'node_modules', 'eslint', 'bin', 'eslint.js')
const result = spawnSync(
process.execPath,
[eslintBin, '.', '--quiet', '-f', 'json'],
{ cwd: ROOT, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 },
)
// ESLint exits 1 when errors exist: that's expected; only treat a missing/
// unparsable report as fatal.
if (!result.stdout) {
console.error('no-new-lint-errors: eslint produced no JSON output')
if (result.error) console.error(result.error)
console.error(result.stderr ?? '')
process.exit(2)
}
try {
return JSON.parse(result.stdout)
} catch {
console.error('no-new-lint-errors: failed to parse eslint JSON output')
process.exit(2)
}
}
function collectCounts(report) {
/** @type {Record<string, number>} */
const perRule = {}
/** @type {Record<string, string[]>} */
const locations = {}
for (const file of report) {
for (const msg of file.messages) {
if (msg.severity !== 2) continue
const rule = msg.ruleId ?? 'fatal'
perRule[rule] = (perRule[rule] ?? 0) + 1
const rel = path.relative(ROOT, file.filePath)
;(locations[rule] ??= []).push(`${rel}:${msg.line}:${msg.column}`)
}
}
return { perRule, locations }
}
const { perRule, locations } = collectCounts(runEslint())
const total = Object.values(perRule).reduce((a, b) => a + b, 0)
if (process.argv.includes('--update')) {
const sorted = Object.fromEntries(Object.entries(perRule).sort(([a], [b]) => a.localeCompare(b)))
fs.writeFileSync(
BASELINE_PATH,
JSON.stringify({ totalErrors: total, perRule: sorted }, null, 2) + '\n',
)
console.log(`no-new-lint-errors: baseline updated: ${total} error(s) across ${Object.keys(perRule).length} rule(s).`)
process.exit(0)
}
if (!fs.existsSync(BASELINE_PATH)) {
console.error(`no-new-lint-errors: baseline missing at ${path.relative(ROOT, BASELINE_PATH)}.`)
console.error('Run: node scripts/checks/no-new-lint-errors.mjs --update')
process.exit(2)
}
const baseline = JSON.parse(fs.readFileSync(BASELINE_PATH, 'utf8'))
const baselineRules = baseline.perRule ?? {}
const regressions = []
for (const [rule, count] of Object.entries(perRule)) {
const allowed = baselineRules[rule] ?? 0
if (count > allowed) regressions.push({ rule, count, allowed })
}
if (regressions.length > 0) {
console.error('no-new-lint-errors: FAILED: new ESLint errors beyond the baseline:\n')
for (const { rule, count, allowed } of regressions) {
console.error(` ${rule}: ${count} (baseline ${allowed})`)
for (const loc of (locations[rule] ?? []).slice(0, 10)) {
console.error(` ${loc}`)
}
}
console.error(`
Fix the new error(s): run \`npx eslint . --quiet\` locally to see them.
(If you fixed MORE legacy errors than you added and the rule still trips,
re-baseline with: node scripts/checks/no-new-lint-errors.mjs --update)
`)
process.exit(1)
}
const improved = total < (baseline.totalErrors ?? 0)
console.log(
`no-new-lint-errors: OK: ${total} error(s), baseline ${baseline.totalErrors}.` +
(improved
? ' Count went DOWN: ratchet it: node scripts/checks/no-new-lint-errors.mjs --update'
: ''),
)
+139
View File
@@ -0,0 +1,139 @@
#!/usr/bin/env node
/**
* Ratchet guard for TypeScript errors (sibling of no-new-lint-errors.mjs).
*
* ## Why this exists
*
* `npm test` does not typecheck. Vitest transpiles and throws the types away,
* so a type error passes the entire 18 000-test suite and only surfaces in
* `npm run build`, several minutes later. That happened twice on 2026-08-27
* alone: a widened union in the MCP server that a second declaration in
* lib/events/types.ts still contradicted, and an `interface` that would not
* assign into `Record<string, unknown>[]` because interfaces have no implicit
* index signature. Both were caught by the build. Neither was caught by 18 000
* green tests, which is exactly the wrong order to learn it in.
*
* `tsc --noEmit` finds both in about two minutes, and unlike the build it also
* covers `__tests__` files, which the Next.js build never compiles.
*
* ## Why the baseline is keyed by FILE, not by error code
*
* The lint ratchet counts per rule, and accepts the tradeoff that fixing one
* legacy error of a rule lets a new one in. For types that tradeoff is worse:
* the pre-existing errors are concentrated in a handful of old test files, and
* TS2322 ("not assignable") is common enough that a per-code budget would
* silently absorb a real regression somewhere else entirely. Keyed by file, a
* new error in a previously-clean file trips immediately, which is the case
* that actually matters.
*
* Usage:
* node scripts/checks/no-new-type-errors.mjs # check
* node scripts/checks/no-new-type-errors.mjs --update # re-baseline
*
* Exit code 1 if any file's error count exceeds its baseline.
*/
import fs from 'node:fs'
import path from 'node:path'
import { spawnSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
const BASELINE_PATH = path.join(ROOT, 'scripts', 'checks', 'typecheck-baseline.json')
/**
* This project's graph does not fit in Node's default heap: a bare
* `tsc --noEmit` dies with "Ineffective mark-compacts near heap limit" after
* about two minutes of work, which reads like a hang rather than a
* misconfiguration. The build sets the same flag for the same reason.
*/
const HEAP_MB = 8192
function runTsc() {
const tscBin = path.join(ROOT, 'node_modules', 'typescript', 'bin', 'tsc')
const result = spawnSync(process.execPath, [tscBin, '--noEmit', '--pretty', 'false'], {
cwd: ROOT,
encoding: 'utf8',
maxBuffer: 64 * 1024 * 1024,
env: { ...process.env, NODE_OPTIONS: `--max-old-space-size=${HEAP_MB}` },
})
const output = `${result.stdout ?? ''}${result.stderr ?? ''}`
if (/Ineffective mark-compacts|JavaScript heap out of memory/.test(output)) {
console.error(`no-new-type-errors: tsc ran out of memory at ${HEAP_MB} MB. Raise HEAP_MB.`)
process.exit(2)
}
// tsc exits non-zero when errors exist, which is the normal case here.
return output
}
/** `path/to/file.ts(12,34): error TS2322: ...` */
const ERROR_RE = /^(.+?)\((\d+),(\d+)\): error (TS\d+): (.*)$/
function collect(output) {
/** @type {Record<string, number>} */
const perFile = {}
/** @type {Record<string, string[]>} */
const locations = {}
for (const line of output.split('\n')) {
const match = ERROR_RE.exec(line.trim())
if (!match) continue
const [, file, lineNo, col, code, message] = match
const rel = path.relative(ROOT, path.resolve(ROOT, file)).split(path.sep).join('/')
perFile[rel] = (perFile[rel] ?? 0) + 1
;(locations[rel] ??= []).push(`${rel}:${lineNo}:${col} ${code} ${message}`)
}
return { perFile, locations }
}
const { perFile, locations } = collect(runTsc())
const total = Object.values(perFile).reduce((a, b) => a + b, 0)
if (process.argv.includes('--update')) {
const sorted = Object.fromEntries(Object.entries(perFile).sort(([a], [b]) => a.localeCompare(b)))
fs.writeFileSync(
BASELINE_PATH,
JSON.stringify({ totalErrors: total, perFile: sorted }, null, 2) + '\n',
)
console.log(
`no-new-type-errors: baseline updated: ${total} error(s) across ${Object.keys(perFile).length} file(s).`,
)
process.exit(0)
}
if (!fs.existsSync(BASELINE_PATH)) {
console.error(`no-new-type-errors: baseline missing at ${path.relative(ROOT, BASELINE_PATH)}.`)
console.error('Run: node scripts/checks/no-new-type-errors.mjs --update')
process.exit(2)
}
const baseline = JSON.parse(fs.readFileSync(BASELINE_PATH, 'utf8'))
const baselineFiles = baseline.perFile ?? {}
const regressions = []
for (const [file, count] of Object.entries(perFile)) {
const allowed = baselineFiles[file] ?? 0
if (count > allowed) regressions.push({ file, count, allowed })
}
if (regressions.length > 0) {
console.error('no-new-type-errors: FAILED: new TypeScript errors beyond the baseline:\n')
for (const { file, count, allowed } of regressions) {
console.error(` ${file}: ${count} (baseline ${allowed})`)
for (const loc of (locations[file] ?? []).slice(0, 10)) {
console.error(` ${loc}`)
}
}
console.error(`
Fix the new error(s): run \`NODE_OPTIONS=--max-old-space-size=${HEAP_MB} npx tsc --noEmit\` to see them all.
(If you fixed MORE legacy errors than you added and a file still trips,
re-baseline with: node scripts/checks/no-new-type-errors.mjs --update)
`)
process.exit(1)
}
const improved = total < (baseline.totalErrors ?? 0)
console.log(
`no-new-type-errors: OK: ${total} error(s), baseline ${baseline.totalErrors}.` +
(improved
? ' Count went DOWN: ratchet it: node scripts/checks/no-new-type-errors.mjs --update'
: ''),
)
+136
View File
@@ -0,0 +1,136 @@
#!/usr/bin/env node
/**
* Guard: reference data fetched outside lib/reference-data.
*
* Fiscal periods, company settings, the chart of accounts, cash accounts,
* dimensions, booking templates, customers, suppliers and articles are
* session-cached behind the hooks in lib/reference-data/hooks.ts (seeded
* from the dashboard layout, invalidated after writes). Before that layer
* existed the same lists were fetched raw from 47 / 27 / 14 / 8 / 12 / 5
* independent call sites, uncached, on every mount and every dialog open,
* which is what a customer described as "it takes time before all fields
* load when clicking around" (2026-08-26). This check keeps that number
* going down: an existing raw call site is grandfathered in the baseline,
* a NEW one fails CI.
*
* Two shapes are flagged:
* 1. A GET-shaped `fetch('/api/<reference path>')` anywhere under app/,
* components/, extensions/ or lib/ (a relative URL is client code by
* definition). Writes (`method: 'POST' | 'PUT' | ...`) are fine: they
* go through the API and then call invalidateReferenceData().
* 2. A browser-side `.from('<reference table>').select(` in a file that
* carries the 'use client' directive. Server code reading those tables
* is legitimate and is not scanned.
*
* Sanctioned (RAW_REFERENCE_SANCTIONED): the fetchers themselves, the
* pre-existing SWR settings hook, and the static BAS catalog loader (a
* different, module-cached data set).
*/
import fs from 'node:fs'
import path from 'node:path'
export const REFERENCE_API_PATHS = [
'bookkeeping/fiscal-periods',
'settings/booking-templates',
'settings',
'bookkeeping/accounts',
'cash-accounts',
'dimensions',
'customers',
'suppliers',
'articles',
]
export const REFERENCE_TABLES = [
'fiscal_periods',
'company_settings',
'chart_of_accounts',
'cash_accounts',
]
export const RAW_REFERENCE_SANCTIONED = new Set([
'lib/reference-data/fetchers.ts',
'components/settings/useSettings.ts',
'lib/bookkeeping/bas-catalog-client.ts',
])
const SCAN_DIRS = ['app/(dashboard)', 'components', 'extensions', 'lib']
const IGNORE_DIRS = new Set(['node_modules', '.next', '.git', 'dist', 'build', 'coverage', '__tests__'])
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')
// fetch(`/api/settings`), fetch('/api/settings?x=1'), fetch('/api/settings', { signal })
// The init object may nest one level ({ headers: { ... } }); its text is
// captured so a write method can be excluded. Trailing commas (prettier's
// multi-line call style) are tolerated before the closing paren. Every
// optional whitespace run is anchored by a literal (`,` or `)`) so the
// pattern has no ambiguous backtracking (CodeQL js/redos on the earlier
// `\s*,?\s*\)` shape).
const REFERENCE_URL = String.raw`[\x60'"]/api/(?:${REFERENCE_API_PATHS.map(escape).join('|')})(?:\?[^\x60'"]*)?[\x60'"]`
const INIT_OBJECT = String.raw`\{(?:[^{}]|\{[^{}]*\})*\}`
const API_FETCH_RE = new RegExp(
String.raw`fetch\(\s*${REFERENCE_URL}(?:\s*,\s*(${INIT_OBJECT}))?(?:\s*,)?\s*\)`,
'g',
)
const WRITE_METHOD_RE = /method\s*:\s*[\x60'"](?!GET\b)/i
const TABLE_SELECT_RE = new RegExp(
String.raw`\.from\(\s*['"](?:${REFERENCE_TABLES.map(escape).join('|')})['"]\s*\)\s*\.\s*select\(`,
'g',
)
// Leading whitespace and comments before the directive. A block comment body
// is `(?:[^*]|\*(?!\/))*`, which cannot cross a `*/`, so each iteration of the
// outer star has exactly one parse: the lazy `[\s\S]*?` form let an unclosed
// `/*` be re-split at every later `/*` (CodeQL js/redos).
// Single-character whitespace alternative (not \s+): a `+` inside the outer
// `*` is a nested quantifier on the same character (CodeQL js/redos).
const USE_CLIENT_RE = /^(?:\s|\/\/[^\n]*\n|\/\*(?:[^*]|\*(?!\/))*\*\/)*['"]use client['"]/
export function isClientSource(source) {
return USE_CLIENT_RE.test(source)
}
/**
* Findings for one file's source: `{ kind: 'api' | 'table', index }`.
* Exported for the unit test; the file-level scan below uses it.
*/
export function findRawReferenceFetchesInSource(source) {
const findings = []
for (const match of source.matchAll(API_FETCH_RE)) {
const init = match[1]
if (init && WRITE_METHOD_RE.test(init)) continue
findings.push({ kind: 'api', index: match.index ?? 0 })
}
if (isClientSource(source)) {
for (const match of source.matchAll(TABLE_SELECT_RE)) {
findings.push({ kind: 'table', index: match.index ?? 0 })
}
}
return findings
}
function walk(dir, out) {
if (!fs.existsSync(dir)) return
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (IGNORE_DIRS.has(entry.name)) continue
const full = path.join(dir, entry.name)
if (entry.isDirectory()) walk(full, out)
else if (/\.(?:ts|tsx)$/.test(entry.name) && !/\.test\.tsx?$/.test(entry.name)) out.push(full)
}
}
/** Sorted repo-relative paths of files with at least one raw reference fetch. */
export function findRawReferenceFetches(root) {
const files = []
for (const dir of SCAN_DIRS) walk(path.join(root, dir), files)
const offenders = []
for (const file of files) {
const rel = path.relative(root, file).split(path.sep).join('/')
if (rel.startsWith('app/api/') || RAW_REFERENCE_SANCTIONED.has(rel)) continue
const source = fs.readFileSync(file, 'utf8')
if (findRawReferenceFetchesInSource(source).length) offenders.push(rel)
}
return [...new Set(offenders)].sort()
}
+87
View File
@@ -0,0 +1,87 @@
{
"totalErrors": 533,
"perFile": {
"app/api/assets/__tests__/id.test.ts": 9,
"app/api/auth/email-hook/__tests__/route.test.ts": 1,
"app/api/auth/heartbeat/__tests__/route.test.ts": 1,
"app/api/bookkeeping/fiscal-periods/__tests__/route.test.ts": 23,
"app/api/bookkeeping/fiscal-periods/[id]/bokslutsdispositioner/__tests__/route.test.ts": 4,
"app/api/bookkeeping/journal-entries/__tests__/route.test.ts": 26,
"app/api/bookkeeping/journal-entries/[id]/chain/__tests__/route.test.ts": 2,
"app/api/bookkeeping/no-doc-required/batch/__tests__/route.test.ts": 6,
"app/api/bookkeeping/no-doc-required/bulk-missing/__tests__/route.test.ts": 9,
"app/api/bookkeeping/voucher-gaps/__tests__/route.test.ts": 9,
"app/api/customers/__tests__/viewer.test.ts": 2,
"app/api/documents/counts/__tests__/route.test.ts": 11,
"app/api/export/articles/__tests__/route.test.ts": 3,
"app/api/export/suppliers/__tests__/route.test.ts": 2,
"app/api/extensions/shopify/orders/cron/__tests__/route.test.ts": 2,
"app/api/extensions/skatteverket/agi/kvittenser/cron/__tests__/route.test.ts": 4,
"app/api/import/articles/__tests__/execute.test.ts": 23,
"app/api/import/bank-file/__tests__/route.test.ts": 1,
"app/api/import/bank-file/check-duplicates/__tests__/route.test.ts": 8,
"app/api/import/opening-balance/__tests__/correct.test.ts": 28,
"app/api/import/opening-balance/__tests__/execute.test.ts": 18,
"app/api/import/opening-balance/correct/__tests__/route.test.ts": 10,
"app/api/invoices/__tests__/route.test.ts": 24,
"app/api/invoices/[id]/peppol/__tests__/route.test.ts": 1,
"app/api/invoices/[id]/send-payment-confirmation/__tests__/route.test.ts": 8,
"app/api/invoices/bulk-book/__tests__/route.test.ts": 1,
"app/api/invoices/self-billed/__tests__/route.test.ts": 8,
"app/api/onboarding/state/__tests__/route.test.ts": 5,
"app/api/pending-operations/bulk-commit/__tests__/route.test.ts": 11,
"app/api/pending-operations/bulk-reject/__tests__/route.test.ts": 10,
"app/api/reports/audit-trail/__tests__/route.test.ts": 4,
"app/api/reports/bokslutsbilagor/__tests__/route.test.ts": 8,
"app/api/reports/full-archive/__tests__/route.test.ts": 11,
"app/api/rot-rut/__tests__/routes.test.ts": 13,
"app/api/rot-rut/beslut/__tests__/route.test.ts": 4,
"app/api/settings/api-keys/__tests__/route.test.ts": 6,
"app/api/settings/booking-templates/sync/cron/__tests__/route.test.ts": 1,
"app/api/settings/eu-trade-signal/__tests__/route.test.ts": 3,
"app/api/settings/ku-signal/__tests__/route.test.ts": 4,
"app/api/settings/rot-rut-signal/__tests__/route.test.ts": 4,
"app/api/supplier-invoices/__tests__/route.test.ts": 39,
"app/api/supplier-invoices/payment-batches/__tests__/route.test.ts": 12,
"app/api/tax-assessment-notices/__tests__/route.test.ts": 5,
"app/api/transactions/bulk-book/__tests__/route.test.ts": 16,
"app/api/v1/companies/[companyId]/customers/__tests__/route.test.ts": 1,
"app/api/webshop-orders/__tests__/bulk-book.test.ts": 1,
"app/api/webshop-orders/__tests__/list-and-settings.test.ts": 11,
"extensions/general/enable-banking/__tests__/session-expired.test.ts": 4,
"extensions/general/enable-banking/__tests__/supersede.test.ts": 1,
"extensions/general/invoice-inbox/__tests__/match-transaction.test.ts": 1,
"extensions/general/invoice-inbox/__tests__/retry-extraction.test.ts": 1,
"extensions/general/invoice-inbox/__tests__/sandbox-skip-extraction.test.ts": 1,
"extensions/general/invoice-inbox/__tests__/upload-page-count-gate.test.ts": 1,
"extensions/general/mcp-server/__tests__/account-tools.test.ts": 2,
"extensions/general/mcp-server/__tests__/connect-links.test.ts": 1,
"extensions/general/mcp-server/__tests__/payroll-staged-tools.test.ts": 21,
"extensions/general/mcp-server/__tests__/salary-tools.test.ts": 4,
"extensions/general/mcp-server/__tests__/set-voucher-note.test.ts": 1,
"extensions/general/mcp-server/__tests__/skills.test.ts": 2,
"extensions/general/skatteverket/__tests__/api-client.test.ts": 5,
"extensions/general/skatteverket/__tests__/skattekonto-mappers.test.ts": 1,
"lib/__tests__/logger.test.ts": 1,
"lib/api/v1/__tests__/with-api-v1.test.ts": 9,
"lib/auth/__tests__/require-write.test.ts": 15,
"lib/bokslut/__tests__/k3-framework-dispositions.test.ts": 2,
"lib/bokslut/__tests__/readiness-aggregator.test.ts": 6,
"lib/bookkeeping/__tests__/supplier-payment-lines.test.ts": 14,
"lib/branding/__tests__/public-brand.test.ts": 1,
"lib/email/__tests__/brand-mail-snapshots.test.ts": 1,
"lib/email/__tests__/brand-sender.test.ts": 1,
"lib/email/__tests__/reminder-templates.test.ts": 6,
"lib/import/__tests__/account-mapper.test.ts": 1,
"lib/import/__tests__/sie-import.test.ts": 1,
"lib/invoices/__tests__/pdf-template-amounts.test.ts": 1,
"lib/invoices/__tests__/peppol-bis-billing.test.ts": 1,
"lib/invoices/__tests__/reminder-processor.test.ts": 3,
"lib/invoices/__tests__/supplier-invoice-matching.test.ts": 1,
"lib/pending-operations/__tests__/commit-authorization-recoverable.test.ts": 1,
"lib/reports/__tests__/vat-declaration.test.ts": 5,
"lib/supabase/__tests__/middleware.test.ts": 1,
"tests/pg/categorize-calibration-samples.pg.test.ts": 2,
"tests/pg/match-batch-allocate.pg.test.ts": 1
}
}
+76
View File
@@ -0,0 +1,76 @@
/**
* Delete orphan [SKV-TEST] journal entries from a company.
*
* Use this to recover from a failed seed-skv-test-data.ts run that inserted
* draft entries but failed to commit them (e.g., constraint violation).
*
* Usage: npx tsx scripts/clean-skv-test-drafts.ts <COMPANY_ID>
*/
import { createClient } from '@supabase/supabase-js'
import { config } from 'dotenv'
import { resolve } from 'node:path'
config({ path: resolve(process.cwd(), '.env.local') })
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
{ auth: { persistSession: false } },
)
const companyId = process.argv[2]
if (!companyId) {
console.error('Usage: npx tsx scripts/clean-skv-test-drafts.ts <COMPANY_ID>')
process.exit(1)
}
async function main() {
// Find every [SKV-TEST] entry on this company.
const { data: entries, error: fetchErr } = await supabase
.from('journal_entries')
.select('id, status, description, voucher_number')
.eq('company_id', companyId)
.like('description', '[SKV-TEST]%')
if (fetchErr) throw new Error(`fetch: ${fetchErr.message}`)
if (!entries || entries.length === 0) {
console.log('No [SKV-TEST] entries found.')
return
}
console.log(`Found ${entries.length} [SKV-TEST] entries:`)
for (const e of entries) {
console.log(` ${e.status.padEnd(10)} A${e.voucher_number ?? '?'} ${e.description}`)
}
// journal_entry_lines cascades on journal_entries delete, but we still need
// to handle the immutability trigger for status='posted'. Drafts only.
const drafts = entries.filter(e => e.status === 'draft')
const posted = entries.filter(e => e.status !== 'draft')
if (posted.length > 0) {
console.log(`\n⚠ ${posted.length} entries are status='posted' or 'reversed': those are immutable per BFL.`)
console.log(' If you really want to remove them, you have to reverse them first or hard-delete via psql with triggers disabled.')
console.log(' Skipping those here.')
}
if (drafts.length === 0) {
console.log('\nNo draft entries to delete.')
return
}
// BFL compliance trigger blocks DELETE on journal_entries: soft-delete via
// status='cancelled' instead. Cancelled entries are filtered out by the VAT
// calculator (which only reads 'posted' and 'reversed').
console.log(`\nMarking ${drafts.length} draft entries as cancelled...`)
const ids = drafts.map(d => d.id)
const { error: cancelErr } = await supabase
.from('journal_entries')
.update({ status: 'cancelled' })
.in('id', ids)
if (cancelErr) throw new Error(`cancel update: ${cancelErr.message}`)
console.log(` ✓ ${drafts.length} drafts cancelled.`)
}
main().catch(err => { console.error(err); process.exit(1) })
+149
View File
@@ -0,0 +1,149 @@
-- ============================================================
-- clear-user-data.sql
-- Deletes ALL data for a given user from erp-base, including
-- the auth.users row. Handles circular FKs and temporarily
-- disables enforcement/audit triggers.
--
-- Usage:
-- 1. Set the target email below
-- 2. Run via Supabase SQL Editor or MCP execute_sql
-- ============================================================
-- Step 0: Look up the user ID (run this first to verify)
-- SELECT id, email FROM auth.users WHERE email = 'user@example.com';
DO $$
DECLARE
-- >>> SET THE TARGET USER EMAIL HERE <<<
target_email TEXT := 'user@example.com'; -- change this to the target user's email
target_user_id UUID;
BEGIN
-- Resolve email to user ID
SELECT id INTO target_user_id FROM auth.users WHERE email = target_email;
IF target_user_id IS NULL THEN
RAISE EXCEPTION 'No user found with email: %', target_email;
END IF;
RAISE NOTICE 'Clearing all data for user % (%)', target_email, target_user_id;
-- Disable all user-defined triggers (enforcement, audit, updated_at)
-- Does NOT disable system FK triggers
ALTER TABLE public.journal_entries DISABLE TRIGGER USER;
ALTER TABLE public.journal_entry_lines DISABLE TRIGGER USER;
ALTER TABLE public.document_attachments DISABLE TRIGGER USER;
ALTER TABLE public.fiscal_periods DISABLE TRIGGER USER;
ALTER TABLE public.transactions DISABLE TRIGGER USER;
ALTER TABLE public.receipts DISABLE TRIGGER USER;
ALTER TABLE public.invoices DISABLE TRIGGER USER;
ALTER TABLE public.sie_imports DISABLE TRIGGER USER;
ALTER TABLE public.cost_centers DISABLE TRIGGER USER;
ALTER TABLE public.supplier_invoices DISABLE TRIGGER USER;
ALTER TABLE public.customers DISABLE TRIGGER USER;
ALTER TABLE public.suppliers DISABLE TRIGGER USER;
ALTER TABLE public.chart_of_accounts DISABLE TRIGGER USER;
ALTER TABLE public.audit_log DISABLE TRIGGER USER;
ALTER TABLE public.company_settings DISABLE TRIGGER USER;
ALTER TABLE public.profiles DISABLE TRIGGER USER;
ALTER TABLE public.chat_sessions DISABLE TRIGGER USER;
ALTER TABLE public.invoice_inbox_items DISABLE TRIGGER USER;
-- Temporarily drop fiscal period constraints (migrations 042-043).
-- The NOT VALID CHECK constraints are still enforced on UPDATE, so
-- the circular-FK-breaking UPDATEs below will fail on rows with
-- non-conforming dates. The EXCLUDE constraint can also interfere.
-- We re-add them all after deletion.
ALTER TABLE public.fiscal_periods DROP CONSTRAINT IF EXISTS no_overlapping_fiscal_periods;
ALTER TABLE public.fiscal_periods DROP CONSTRAINT IF EXISTS fiscal_period_start_first_of_month;
ALTER TABLE public.fiscal_periods DROP CONSTRAINT IF EXISTS fiscal_period_end_last_of_month;
-- Break circular / self-referencing FK constraints
UPDATE public.fiscal_periods SET closing_entry_id = NULL, opening_balance_entry_id = NULL, previous_period_id = NULL WHERE user_id = target_user_id;
UPDATE public.transactions SET receipt_id = NULL, journal_entry_id = NULL, invoice_id = NULL, potential_invoice_id = NULL, supplier_invoice_id = NULL, bank_connection_id = NULL WHERE user_id = target_user_id;
UPDATE public.receipts SET matched_transaction_id = NULL, document_id = NULL WHERE user_id = target_user_id;
UPDATE public.invoices SET credited_invoice_id = NULL, converted_from_id = NULL WHERE user_id = target_user_id;
UPDATE public.document_attachments SET original_id = NULL, superseded_by_id = NULL, journal_entry_id = NULL, journal_entry_line_id = NULL WHERE user_id = target_user_id;
UPDATE public.sie_imports SET opening_balance_entry_id = NULL, fiscal_period_id = NULL WHERE user_id = target_user_id;
UPDATE public.cost_centers SET parent_id = NULL WHERE user_id = target_user_id;
-- Delete child/leaf tables first, then parent tables
DELETE FROM public.receipt_line_items WHERE receipt_id IN (SELECT id FROM public.receipts WHERE user_id = target_user_id);
DELETE FROM public.invoice_items WHERE invoice_id IN (SELECT id FROM public.invoices WHERE user_id = target_user_id);
DELETE FROM public.invoice_reminders WHERE user_id = target_user_id;
DELETE FROM public.supplier_invoice_items WHERE supplier_invoice_id IN (SELECT id FROM public.supplier_invoices WHERE user_id = target_user_id);
DELETE FROM public.supplier_invoice_payments WHERE supplier_invoice_id IN (SELECT id FROM public.supplier_invoices WHERE user_id = target_user_id);
DELETE FROM public.invoice_inbox_items WHERE user_id = target_user_id;
DELETE FROM public.document_attachments WHERE user_id = target_user_id;
DELETE FROM public.journal_entry_lines WHERE journal_entry_id IN (SELECT id FROM public.journal_entries WHERE user_id = target_user_id);
DELETE FROM public.journal_entries WHERE user_id = target_user_id;
DELETE FROM public.receipts WHERE user_id = target_user_id;
DELETE FROM public.transactions WHERE user_id = target_user_id;
DELETE FROM public.invoices WHERE user_id = target_user_id;
DELETE FROM public.supplier_invoices WHERE user_id = target_user_id;
DELETE FROM public.suppliers WHERE user_id = target_user_id;
DELETE FROM public.customers WHERE user_id = target_user_id;
DELETE FROM public.sie_imports WHERE user_id = target_user_id;
DELETE FROM public.sie_account_mappings WHERE user_id = target_user_id;
DELETE FROM public.voucher_sequences WHERE user_id = target_user_id;
DELETE FROM public.fiscal_periods WHERE user_id = target_user_id;
DELETE FROM public.chart_of_accounts WHERE user_id = target_user_id;
DELETE FROM public.bank_connections WHERE user_id = target_user_id;
DELETE FROM public.mapping_rules WHERE user_id = target_user_id;
DELETE FROM public.deadlines WHERE user_id = target_user_id;
DELETE FROM public.calendar_feeds WHERE user_id = target_user_id;
DELETE FROM public.push_subscriptions WHERE user_id = target_user_id;
DELETE FROM public.notification_log WHERE user_id = target_user_id;
DELETE FROM public.notification_settings WHERE user_id = target_user_id;
DELETE FROM public.cost_centers WHERE user_id = target_user_id;
DELETE FROM public.projects WHERE user_id = target_user_id;
DELETE FROM public.bank_file_imports WHERE user_id = target_user_id;
DELETE FROM public.chat_messages WHERE user_id = target_user_id;
DELETE FROM public.chat_sessions WHERE user_id = target_user_id;
DELETE FROM public.extension_data WHERE user_id = target_user_id;
DELETE FROM public.audit_log WHERE user_id = target_user_id;
DELETE FROM public.company_settings WHERE user_id = target_user_id;
DELETE FROM public.profiles WHERE id = target_user_id;
-- Re-enable all user-defined triggers
ALTER TABLE public.journal_entries ENABLE TRIGGER USER;
ALTER TABLE public.journal_entry_lines ENABLE TRIGGER USER;
ALTER TABLE public.document_attachments ENABLE TRIGGER USER;
ALTER TABLE public.fiscal_periods ENABLE TRIGGER USER;
ALTER TABLE public.transactions ENABLE TRIGGER USER;
ALTER TABLE public.receipts ENABLE TRIGGER USER;
ALTER TABLE public.invoices ENABLE TRIGGER USER;
ALTER TABLE public.sie_imports ENABLE TRIGGER USER;
ALTER TABLE public.cost_centers ENABLE TRIGGER USER;
ALTER TABLE public.supplier_invoices ENABLE TRIGGER USER;
ALTER TABLE public.customers ENABLE TRIGGER USER;
ALTER TABLE public.suppliers ENABLE TRIGGER USER;
ALTER TABLE public.chart_of_accounts ENABLE TRIGGER USER;
ALTER TABLE public.audit_log ENABLE TRIGGER USER;
ALTER TABLE public.company_settings ENABLE TRIGGER USER;
ALTER TABLE public.profiles ENABLE TRIGGER USER;
ALTER TABLE public.chat_sessions ENABLE TRIGGER USER;
ALTER TABLE public.invoice_inbox_items ENABLE TRIGGER USER;
-- Re-add fiscal period constraints
ALTER TABLE public.fiscal_periods
ADD CONSTRAINT no_overlapping_fiscal_periods
EXCLUDE USING gist (
user_id WITH =,
daterange(period_start, period_end, '[]') WITH &&
);
ALTER TABLE public.fiscal_periods
ADD CONSTRAINT fiscal_period_start_first_of_month
CHECK (EXTRACT(DAY FROM period_start) = 1)
NOT VALID;
ALTER TABLE public.fiscal_periods
ADD CONSTRAINT fiscal_period_end_last_of_month
CHECK (period_end = (date_trunc('month', period_end) + interval '1 month - 1 day')::date)
NOT VALID;
-- Delete the auth user
DELETE FROM auth.users WHERE id = target_user_id;
RAISE NOTICE 'Done. User % and all associated data have been deleted.', target_email;
END $$;
+346
View File
@@ -0,0 +1,346 @@
#!/usr/bin/env npx tsx
/**
* Scaffold a new extension with all required files.
*
* Usage:
* npx tsx scripts/create-extension.ts \
* --name my-extension \
* --sector general \
* --category operations \
* --description "Short description of the extension"
*
* This will:
* 1. Create extensions/<sector>/<name>/ directory
* 2. Generate manifest.json, index.ts, and api-routes.ts
* 3. Add the extension ID to extensions.schema.json enum array
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
// ── Constants ────────────────────────────────────────────────
const VALID_SECTORS = [
'general',
'restaurant',
'construction',
'hotel',
'tech',
'ecommerce',
'export',
] as const
const VALID_CATEGORIES = [
'import',
'operations',
'reports',
'accounting',
] as const
type Sector = (typeof VALID_SECTORS)[number]
type Category = (typeof VALID_CATEGORIES)[number]
// ── Helpers ──────────────────────────────────────────────────
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const ROOT = path.resolve(__dirname, '..')
function usage(): never {
console.error(`
Usage:
npx tsx scripts/create-extension.ts \\
--name <extension-name> \\
--sector <${VALID_SECTORS.join(' | ')}> \\
--category <${VALID_CATEGORIES.join(' | ')}> \\
--description "Short description"
Options:
--name Extension slug (kebab-case, e.g. "my-extension")
--sector Business sector for the extension
--category Extension category
--description Short description of the extension
Example:
npx tsx scripts/create-extension.ts \\
--name inventory-tracker \\
--sector restaurant \\
--category operations \\
--description "Track inventory levels for restaurant supplies"
`)
process.exit(1)
}
/**
* Parse CLI arguments into a key-value map.
* Supports --key value pairs.
*/
function parseArgs(argv: string[]): Record<string, string> {
const args: Record<string, string> = {}
for (let i = 0; i < argv.length; i++) {
const arg = argv[i]
if (arg.startsWith('--')) {
const key = arg.slice(2)
const value = argv[i + 1]
if (!value || value.startsWith('--')) {
console.error(`Error: Missing value for --${key}`)
usage()
}
args[key] = value
i++ // skip the value
}
}
return args
}
/**
* Convert a kebab-case slug to camelCase.
* e.g. "my-extension" -> "myExtension"
*/
function toCamelCase(slug: string): string {
return slug.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase())
}
/**
* Convert a kebab-case slug to a camelCase export name.
* e.g. "my-extension" -> "myExtensionExtension"
*/
function toExportName(slug: string): string {
return `${toCamelCase(slug)}Extension`
}
/**
* Convert a kebab-case slug to a Title Case display name.
* e.g. "my-extension" -> "My Extension"
*/
function toDisplayName(slug: string): string {
return slug
.split('-')
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ')
}
/**
* Validate that the extension name is a valid kebab-case slug.
*/
function validateName(name: string): void {
if (!/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/.test(name)) {
console.error(
`Error: Extension name "${name}" is not valid kebab-case.`
)
console.error(' Must start with a lowercase letter, use only lowercase letters, digits, and hyphens.')
console.error(' Example: "my-extension", "pos-import", "billable-hours"')
process.exit(1)
}
}
// ── File generators ──────────────────────────────────────────
function generateManifest(
name: string,
sector: Sector,
category: Category,
description: string,
exportName: string,
entryPoint: string
): string {
const manifest = {
id: name,
sector,
exportName,
entryPoint,
workspace: null,
requiredEnvVars: [] as string[],
optionalEnvVars: [] as string[],
npmDependencies: [] as string[],
definition: {
name: toDisplayName(name),
category,
icon: 'Box',
dataPattern: 'core',
description,
longDescription: description,
},
}
return JSON.stringify(manifest, null, 2) + '\n'
}
function generateIndexTs(
name: string,
sector: Sector,
exportName: string,
displayName: string
): string {
const apiRoutesVar = `${toCamelCase(name)}ApiRoutes`
return `import type { Extension } from '@/lib/extensions/types'
import { ${apiRoutesVar} } from './api-routes'
/**
* ${displayName} Extension
*
* TODO: Add extension description here.
*/
export const ${exportName}: Extension = {
id: '${name}',
name: '${displayName}',
version: '0.1.0',
sector: '${sector}',
apiRoutes: ${apiRoutesVar},
}
`
}
function generateApiRoutesTs(name: string): string {
const apiRoutesVar = `${toCamelCase(name)}ApiRoutes`
return `import type { ApiRouteDefinition } from '@/lib/extensions/types'
/**
* API routes for the ${toDisplayName(name)} extension.
*
* Add route definitions here as the extension grows.
* Each route will be served under /api/extensions/${name}/<path>.
*/
export const ${apiRoutesVar}: ApiRouteDefinition[] = []
`
}
/**
* Add the new extension ID to extensions.schema.json enum array.
*/
function updateSchemaJson(name: string): void {
const schemaPath = path.join(ROOT, 'extensions.schema.json')
if (!fs.existsSync(schemaPath)) {
console.warn(' Warning: extensions.schema.json not found, skipping enum update.')
return
}
const content = fs.readFileSync(schemaPath, 'utf-8')
const schema = JSON.parse(content)
const enumArray: string[] = schema?.properties?.extensions?.items?.enum
if (!Array.isArray(enumArray)) {
console.warn(' Warning: Could not find enum array in extensions.schema.json, skipping.')
return
}
if (enumArray.includes(name)) {
console.log(` extensions.schema.json already contains "${name}", skipping.`)
return
}
enumArray.push(name)
fs.writeFileSync(schemaPath, JSON.stringify(schema, null, 2) + '\n', 'utf-8')
console.log(` Updated extensions.schema.json: added "${name}" to enum array.`)
}
// ── Main ─────────────────────────────────────────────────────
function main(): void {
const args = parseArgs(process.argv.slice(2))
const name = args['name']
const sector = args['sector'] as Sector | undefined
const category = args['category'] as Category | undefined
const description = args['description']
// Validate required arguments
if (!name || !sector || !category || !description) {
const missing: string[] = []
if (!name) missing.push('--name')
if (!sector) missing.push('--sector')
if (!category) missing.push('--category')
if (!description) missing.push('--description')
console.error(`Error: Missing required arguments: ${missing.join(', ')}`)
usage()
}
validateName(name)
if (!VALID_SECTORS.includes(sector)) {
console.error(`Error: Invalid sector "${sector}".`)
console.error(` Valid sectors: ${VALID_SECTORS.join(', ')}`)
process.exit(1)
}
if (!VALID_CATEGORIES.includes(category)) {
console.error(`Error: Invalid category "${category}".`)
console.error(` Valid categories: ${VALID_CATEGORIES.join(', ')}`)
process.exit(1)
}
const exportName = toExportName(name)
const displayName = toDisplayName(name)
const entryPoint = `@/extensions/${sector}/${name}`
const extensionDir = path.join(ROOT, 'extensions', sector, name)
// Check if extension already exists
if (fs.existsSync(extensionDir)) {
console.error(`Error: Extension directory already exists: ${extensionDir}`)
process.exit(1)
}
console.log(`\nScaffolding extension: ${displayName}`)
console.log(` ID: ${name}`)
console.log(` Sector: ${sector}`)
console.log(` Category: ${category}`)
console.log(` Export: ${exportName}`)
console.log(` Entry: ${entryPoint}`)
console.log()
// Create directory
fs.mkdirSync(extensionDir, { recursive: true })
console.log(` Created directory: extensions/${sector}/${name}/`)
// Write manifest.json
const manifestPath = path.join(extensionDir, 'manifest.json')
fs.writeFileSync(
manifestPath,
generateManifest(name, sector, category, description, exportName, entryPoint),
'utf-8'
)
console.log(` Created: extensions/${sector}/${name}/manifest.json`)
// Write index.ts
const indexPath = path.join(extensionDir, 'index.ts')
fs.writeFileSync(
indexPath,
generateIndexTs(name, sector, exportName, displayName),
'utf-8'
)
console.log(` Created: extensions/${sector}/${name}/index.ts`)
// Write api-routes.ts
const apiRoutesPath = path.join(extensionDir, 'api-routes.ts')
fs.writeFileSync(apiRoutesPath, generateApiRoutesTs(name), 'utf-8')
console.log(` Created: extensions/${sector}/${name}/api-routes.ts`)
// Update extensions.schema.json
updateSchemaJson(name)
// Summary
console.log(`
Done! Next steps:
1. Edit the manifest.json to customize icon, dataPattern, and longDescription:
extensions/${sector}/${name}/manifest.json
2. Implement extension logic in index.ts:
extensions/${sector}/${name}/index.ts
3. Add API routes if needed in api-routes.ts:
extensions/${sector}/${name}/api-routes.ts
4. Add a static import to FIRST_PARTY_EXTENSIONS in lib/extensions/loader.ts:
import { ${exportName} } from '@/extensions/${sector}/${name}'
5. Add extension metadata to the sector registry in lib/extensions/sectors.ts
6. Enable the extension in extensions.config.json:
Add "${name}" to the extensions array
`)
}
main()
+143
View File
@@ -0,0 +1,143 @@
/**
* Create a dedicated SKV test company in gnubok.
*
* Why this exists: testing the Skatteverket sandbox APIs against Arcim's real
* orgnummer would put real revenue/VAT figures in SKV's test logs under the
* real entity. Better hygiene: a separate gnubok company that uses one of
* SKV's *published* test orgnummer (which are already pre-wired in their
* test registry), seeded with synthetic data only.
*
* Inserts:
* - companies row with name `[TEST] SKV Sandbox`, org_number=1128000013,
* entity_type=aktiebolag, created_by=<user_id>
* - company_members row giving <user_id> owner role
* - company_settings row with the same org_number + entity_type
* - chart_of_accounts seeded via the seed_chart_of_accounts RPC
* - flips user_preferences.active_company_id to the new company so the UI
* starts using it immediately
*
* To revert: delete the company row (CASCADE removes members + settings),
* then update user_preferences.active_company_id back to the previous value.
*
* Usage: npx tsx scripts/create-skv-test-company.ts <USER_ID>
*/
import { createClient } from '@supabase/supabase-js'
import { config } from 'dotenv'
import { resolve } from 'node:path'
config({ path: resolve(process.cwd(), '.env.local') })
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
{ auth: { persistSession: false } },
)
const TEST_NAME = '[TEST] SKV Sandbox'
const TEST_ORG_NUMBER = '1128000013' // → 161128000013, registered for moms in SKV test
const TEST_ENTITY_TYPE = 'aktiebolag'
const userId = process.argv[2]
if (!userId) {
console.error('Usage: npx tsx scripts/create-skv-test-company.ts <USER_ID>')
process.exit(1)
}
async function main() {
// Sanity check: don't create duplicates if the script is rerun.
const { data: existing } = await supabase
.from('companies')
.select('id, name, org_number')
.eq('created_by', userId)
.eq('name', TEST_NAME)
.maybeSingle()
if (existing) {
console.log(`Test company already exists:`)
console.log(` id: ${existing.id}`)
console.log(` name: ${existing.name}`)
console.log(` org_number: ${existing.org_number}`)
console.log(`\nIf you want a fresh one, delete it first:`)
console.log(` delete from companies where id = '${existing.id}';`)
return
}
console.log(`Creating test company for user ${userId}...`)
// 1. Insert the company.
const { data: company, error: companyErr } = await supabase
.from('companies')
.insert({
name: TEST_NAME,
org_number: TEST_ORG_NUMBER,
entity_type: TEST_ENTITY_TYPE,
created_by: userId,
})
.select('id')
.single()
if (companyErr || !company) throw new Error(`companies insert: ${companyErr?.message}`)
const companyId = company.id
console.log(` ✓ companies.id = ${companyId}`)
// 2. Owner membership.
const { error: memberErr } = await supabase
.from('company_members')
.insert({ company_id: companyId, user_id: userId, role: 'owner' })
if (memberErr) throw new Error(`company_members insert: ${memberErr.message}`)
console.log(` ✓ owner membership created`)
// 3. company_settings (the validate handler reads org_number from here).
const { error: settingsErr } = await supabase
.from('company_settings')
.insert({
company_id: companyId,
org_number: TEST_ORG_NUMBER,
entity_type: TEST_ENTITY_TYPE,
})
if (settingsErr) throw new Error(`company_settings insert: ${settingsErr.message}`)
console.log(` ✓ company_settings created`)
// 4. Seed the chart of accounts.
const { error: seedErr } = await supabase.rpc('seed_chart_of_accounts', {
p_company_id: companyId,
p_entity_type: TEST_ENTITY_TYPE,
})
if (seedErr) throw new Error(`seed_chart_of_accounts: ${seedErr.message}`)
console.log(` ✓ chart of accounts seeded`)
// 5. Make this the user's active company so the UI uses it on next load.
const { data: prevPref } = await supabase
.from('user_preferences')
.select('active_company_id')
.eq('user_id', userId)
.maybeSingle()
const previousActive = prevPref?.active_company_id ?? null
const { error: prefErr } = await supabase
.from('user_preferences')
.upsert(
{ user_id: userId, active_company_id: companyId },
{ onConflict: 'user_id' },
)
if (prefErr) throw new Error(`user_preferences upsert: ${prefErr.message}`)
console.log(` ✓ active_company_id flipped to test company`)
console.log(`\nDone.\n`)
console.log(`Test company id: ${companyId}`)
console.log(`Test company name: ${TEST_NAME}`)
console.log(`org_number (10-digit): ${TEST_ORG_NUMBER}`)
console.log(`SKV redovisare (12-digit): 16${TEST_ORG_NUMBER}`)
console.log(`Previous active_company_id: ${previousActive ?? '(none)'}`)
console.log(`\nNext step: seed VAT fixtures for SKV's pre-wired periods:`)
console.log(` npx tsx scripts/seed-skv-test-data.ts ${companyId} 2024 1`)
console.log(` npx tsx scripts/seed-skv-test-data.ts ${companyId} 2024 2`)
console.log(`\nWhen done testing, switch back via the UI's company switcher,`)
console.log(`or run:`)
console.log(` update user_preferences set active_company_id = '${previousActive}' where user_id = '${userId}';`)
}
main().catch(err => {
console.error(err)
process.exit(1)
})
+30
View File
@@ -0,0 +1,30 @@
import { createClient } from '@supabase/supabase-js'
async function main() {
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
)
const { data: consents, error } = await supabase
.from('provider_consents')
.select('id, provider, company_name, status, created_at')
.eq('provider', 'bjornlunden')
.order('created_at', { ascending: false })
.limit(5)
if (error) throw error
console.log(JSON.stringify(consents, null, 2))
for (const c of consents ?? []) {
const { data: tokens } = await supabase
.from('provider_consent_tokens')
.select('consent_id, provider_company_id, token_expires_at')
.eq('consent_id', c.id)
.limit(1)
console.log(`consent ${c.id}: tokens=${tokens?.length ? 'yes' : 'NO'} userKey=${tokens?.[0]?.provider_company_id?.slice(0, 8) ?? '-'}…`)
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+102
View File
@@ -0,0 +1,102 @@
/**
* Temporary diagnostic: list every unbalanced voucher in the BL SIE export
* with its lines, write a CSV report, and print summary statistics.
* Run: npx tsx --env-file=.env --env-file=.env.local scripts/debug-bl-unbalanced.ts
*/
import { writeFileSync } from 'node:fs'
import { createClient } from '@supabase/supabase-js'
import { fetchBjornLundenToken } from '@/lib/providers/bjornlunden/oauth'
import { fetchProviderSieFiles } from '@/extensions/general/arcim-migration/lib/sie-fetcher'
import { parseSIEFile } from '@/lib/import/sie-parser'
const consentId = process.argv[2]
if (!consentId) {
console.error('Usage: npx tsx --env-file=.env --env-file=.env.local scripts/debug-bl-unbalanced.ts <consentId>')
console.error('Refusing to run without an explicit consentId: this script reads a live BL company and writes a report.')
process.exit(1)
}
function isoLocal(d: Date): string {
const y = d.getFullYear()
const m = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0')
return `${y}-${m}-${day}`
}
async function main() {
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
)
const { data: tokens, error } = await supabase
.from('provider_consent_tokens')
.select('provider_company_id')
.eq('consent_id', consentId)
.limit(1)
if (error || !tokens?.length) throw new Error(`no tokens for consent: ${error?.message ?? 'not found'}`)
const userKey = tokens[0]!.provider_company_id as string
const token = await fetchBjornLundenToken(
process.env.BJORN_LUNDEN_CLIENT_ID!,
process.env.BJORN_LUNDEN_CLIENT_SECRET!,
)
const { files } = await fetchProviderSieFiles('bjornlunden', token.access_token, userKey)
for (const f of files) {
const parsed = parseSIEFile(f.rawContent)
const bad = parsed.vouchers
.map((v) => ({
voucher: `${v.series}${v.number}`,
date: isoLocal(v.date),
description: v.description,
lineCount: v.lines.length,
diff: Math.round(v.lines.reduce((s, l) => s + l.amount, 0) * 100) / 100,
lines: v.lines.map((l) => `${l.account}:${l.amount}`).join(' '),
}))
.filter((v) => Math.abs(v.diff) > 0.01 || v.lineCount === 0)
const total = parsed.vouchers.length
const empty = bad.filter((v) => v.lineCount === 0).length
const single = bad.filter((v) => v.lineCount === 1).length
const multi = bad.filter((v) => v.lineCount > 1).length
console.log(`fiscal year ${f.fiscalYear}: ${total} vouchers total, ${bad.length} broken`)
console.log(` empty (0 lines): ${empty}`)
console.log(` one-sided (1 line): ${single}`)
console.log(` multi-line unbalanced: ${multi}`)
// Distribution of diffs to spot recurring patterns (e.g. 1.50 bank fees)
const byDiff = new Map<string, number>()
for (const v of bad.filter((b) => b.lineCount > 0)) {
const key = Math.abs(v.diff).toFixed(2)
byDiff.set(key, (byDiff.get(key) ?? 0) + 1)
}
const topDiffs = [...byDiff.entries()].sort((a, b) => b[1] - a[1]).slice(0, 10)
console.log(' most common |diff| amounts:')
for (const [amount, count] of topDiffs) console.log(` ${amount} kr × ${count}`)
// Per-series breakdown
const bySeries = new Map<string, number>()
for (const v of bad) {
const series = v.voucher.replace(/\d+$/, '')
bySeries.set(series, (bySeries.get(series) ?? 0) + 1)
}
console.log(' broken per series:', [...bySeries.entries()].map(([s, n]) => `${s}=${n}`).join(' '))
const csvEscape = (s: string) => `"${s.replace(/"/g, '""')}"`
const csv = [
'voucher;date;diff;line_count;description;lines',
...bad.map((v) =>
[v.voucher, v.date, v.diff.toFixed(2), v.lineCount, csvEscape(v.description), csvEscape(v.lines)].join(';'),
),
].join('\n')
const outPath = `scripts/bl-unbalanced-${f.fiscalYear}.csv`
writeFileSync(outPath, '' + csv, 'utf8')
console.log(` full list written to ${outPath}`)
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+121
View File
@@ -0,0 +1,121 @@
/**
* One-shot script that exports the registry-derived docs content (errors +
* reference) plus the static Connect-with-Claude page as TypeScript modules
* into the gnubok-website repo. Connect-with-Claude ships in both languages:
* the docs site has no locale routing, so each language is its own page.
*
* Run with `npx tsx scripts/export-docs-to-website.mts`. Re-run whenever
* structured-errors, the v1 endpoint registry, or connect-claude materially
* changes.
*/
import { writeFileSync, mkdirSync } from 'node:fs'
import { createRequire } from 'node:module'
import { dirname, resolve } from 'node:path'
// `server-only` throws on import outside a Next.js server-component graph. The
// reference builder pulls it in transitively (lib/api/v1/load-routes -> every
// v1 route -> lib/init -> lib/analytics/posthog-observability ->
// posthog-server), which broke this script the moment PostHog landed. Nothing
// here executes request-time code: it only reads exported markdown builders,
// so a no-op stub is the honest resolution.
const require = createRequire(import.meta.url)
const ModuleCtor = require('node:module') as {
_load: (request: string, ...rest: unknown[]) => unknown
}
const originalLoad = ModuleCtor._load
ModuleCtor._load = function (request: string, ...rest: unknown[]) {
if (request === 'server-only') return {}
return originalLoad.call(this, request, ...rest)
}
let errors, reference, connectClaude, anslutClaude
try {
errors = await import('@/lib/docs/content/errors')
reference = await import('@/lib/docs/content/reference')
connectClaude = await import('@/lib/docs/content/connect-claude')
anslutClaude = await import('@/lib/docs/content/anslut-claude')
} finally {
// Scope the stub to the imports that need it: leaving a global loader hook
// patched for the rest of the process would silently disarm the guard for
// anything imported later (compliance swarm, ISO 27001 A.8.28).
ModuleCtor._load = originalLoad
}
const buildErrorReferenceMd = errors.buildErrorReferenceMd ?? (errors as { default?: typeof errors }).default?.buildErrorReferenceMd
const buildResourcePages = reference.buildResourcePages ?? (reference as { default?: typeof reference }).default?.buildResourcePages
const buildReferenceOverviewMd = reference.buildReferenceOverviewMd ?? (reference as { default?: typeof reference }).default?.buildReferenceOverviewMd
if (!buildErrorReferenceMd || !buildResourcePages || !buildReferenceOverviewMd) {
console.error('Missing builder exports. Inspect:', {
errorsKeys: Object.keys(errors),
referenceKeys: Object.keys(reference),
})
process.exit(1)
}
const WEBSITE = resolve('/Users/jakobwennberg/gnubok-website')
/**
* The website is served from www.accounted.se while the API (and everything
* under /api/v1 and /.well-known) lives on app.gnubok.se — root-relative links
* to app-served resources would 404 on the website, so absolutise them.
*/
const APP_ORIGIN = 'https://app.gnubok.se'
function adaptForWebsite(md: string): string {
return md
.replaceAll('](/api/v1/', `](${APP_ORIGIN}/api/v1/`)
.replaceAll('](/.well-known/', `](${APP_ORIGIN}/.well-known/`)
}
function write(rel: string, content: string) {
const out = resolve(WEBSITE, rel)
mkdirSync(dirname(out), { recursive: true })
writeFileSync(out, content)
console.log(`wrote ${out} (${content.length} chars)`)
}
const errorsMd = adaptForWebsite(buildErrorReferenceMd())
write(
'lib/docs/content/errors.generated.ts',
`// AUTO-GENERATED from erp-base: do not hand-edit.\n// Regenerate via \`npx tsx scripts/export-docs-to-website.mts\` in erp-base.\nexport const ERRORS_MD = ${JSON.stringify(errorsMd)}\n`,
)
const refOverview = adaptForWebsite(buildReferenceOverviewMd())
const refPages = buildResourcePages()
const slugs = refPages.map((p: { slug: string }) => p.slug)
const pagesPayload = refPages.map((p: { slug: string; label: string; description: string; markdown: string }) => ({
slug: p.slug,
label: p.label,
description: p.description,
markdown: adaptForWebsite(p.markdown),
}))
write(
'lib/docs/content/reference.generated.ts',
`// AUTO-GENERATED from erp-base: do not hand-edit.\n// Regenerate via \`npx tsx scripts/export-docs-to-website.mts\` in erp-base.\n\nexport const REFERENCE_OVERVIEW_MD = ${JSON.stringify(refOverview)}\n\nexport interface ResourcePage {\n slug: string\n label: string\n description: string\n markdown: string\n}\n\nexport const RESOURCE_SLUGS: readonly string[] = ${JSON.stringify(
slugs,
)} as const\n\nexport const RESOURCE_PAGES: ResourcePage[] = ${JSON.stringify(pagesPayload, null, 2)}\n\nexport function findResourcePage(slug: string): ResourcePage | undefined {\n return RESOURCE_PAGES.find((p) => p.slug === slug)\n}\n`,
)
const connectClaudeMd = connectClaude.CONNECT_CLAUDE_MD && adaptForWebsite(connectClaude.CONNECT_CLAUDE_MD)
if (!connectClaudeMd) {
console.error('Missing CONNECT_CLAUDE_MD export. Inspect:', { connectClaudeKeys: Object.keys(connectClaude) })
process.exit(1)
}
write(
'lib/docs/content/connect-claude.generated.ts',
`// AUTO-GENERATED from erp-base: do not hand-edit.\n// Regenerate via \`npx tsx scripts/export-docs-to-website.mts\` in erp-base.\nexport const CONNECT_CLAUDE_MD = ${JSON.stringify(connectClaudeMd)}\n`,
)
const anslutClaudeMd = anslutClaude.ANSLUT_CLAUDE_MD && adaptForWebsite(anslutClaude.ANSLUT_CLAUDE_MD)
if (!anslutClaudeMd) {
console.error('Missing ANSLUT_CLAUDE_MD export. Inspect:', { anslutClaudeKeys: Object.keys(anslutClaude) })
process.exit(1)
}
write(
'lib/docs/content/anslut-claude.generated.ts',
`// AUTO-GENERATED from erp-base: do not hand-edit.\n// Regenerate via \`npx tsx scripts/export-docs-to-website.mts\` in erp-base.\nexport const ANSLUT_CLAUDE_MD = ${JSON.stringify(anslutClaudeMd)}\n`,
)
console.log('done.')
+109
View File
@@ -0,0 +1,109 @@
/**
* Fit and report the auto-booking confidence calibration (RIP-4 step 4).
*
* READ-ONLY. Reads categorize_calibration_samples, prints the reliability
* diagram + expected calibration error, fits an isotonic calibrator, and shows
* what the auto-book / suggest / review bands would look like on the calibrated
* probability. Run this once real outcomes have accumulated (>= a few hundred);
* it changes nothing on its own.
*
* npx tsx scripts/fit-categorize-calibration.ts
*
* Note: .env.local points at production; this only SELECTs, so it is safe, but
* it is still the prod corpus you are reading.
*
* Consent: samples are only written for, and only read from, companies with
* company_settings.data_analysis_opt_in = true (#1346). The write side is
* gated in POST /api/agent/categorize/outcome; the read side filters again
* here so a company that opted out after contributing drops out of the fit.
*/
import { createClient } from '@supabase/supabase-js'
import {
reliabilityByBucket,
expectedCalibrationError,
fitIsotonic,
calibrate,
bandFor,
type Sample,
} from '@/lib/agent/categorize/calibration'
import { chunkCompanyIds, listDataAnalysisOptedInCompanyIds } from '@/lib/company/data-analysis'
const url = process.env.NEXT_PUBLIC_SUPABASE_URL
const key = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!url || !key) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(url, key)
async function main() {
// Consent gate (#1346): only companies that opted in to data analysis.
const optedInIds = await listDataAnalysisOptedInCompanyIds(supabase)
if (optedInIds.length === 0) {
console.log('\nNo company has opted in to data analysis (company_settings.data_analysis_opt_in). Nothing to fit.')
return
}
// Query per chunk of company ids: `.in()` goes into the GET query string, so
// one request per few hundred opted-in companies would hit URL limits.
const rows: { confidence: number; was_correct: boolean }[] = []
const PAGE = 1000
for (const chunk of chunkCompanyIds(optedInIds)) {
for (let from = 0; ; from += PAGE) {
const { data, error } = await supabase
.from('categorize_calibration_samples')
.select('confidence, was_correct')
.in('company_id', chunk)
.order('id', { ascending: true })
.range(from, from + PAGE - 1)
if (error) throw error
if (!data || data.length === 0) break
rows.push(...(data as { confidence: number; was_correct: boolean }[]))
if (data.length < PAGE) break
}
}
const samples: Sample[] = rows.map((r) => ({ confidence: Number(r.confidence), correct: r.was_correct }))
console.log(`\nSamples: ${samples.length}`)
if (samples.length === 0) {
console.log('No calibration samples yet. Let people book AI proposals first.')
return
}
const overall = samples.filter((s) => s.correct).length / samples.length
console.log(`Overall accuracy (proposal booked unedited): ${(overall * 100).toFixed(1)}%`)
console.log(`Expected calibration error (ECE): ${expectedCalibrationError(samples).toFixed(4)}\n`)
console.log('Reliability diagram (raw confidence bucket → empirical accuracy):')
for (const b of reliabilityByBucket(samples)) {
if (b.n === 0) continue
const bar = '#'.repeat(Math.round(b.accuracy * 20))
console.log(
` ${b.lo.toFixed(1)}-${b.hi.toFixed(1)} n=${String(b.n).padStart(5)} ` +
`conf=${b.meanConfidence.toFixed(2)} acc=${b.accuracy.toFixed(2)} ${bar}`,
)
}
const cal = fitIsotonic(samples)
if (!cal) {
console.log(`\nNot enough data to fit a calibrator yet (need >= 200). Bands stay uncalibrated (no auto-book).`)
return
}
console.log(`\nFitted isotonic calibrator on ${cal.fittedOn} samples.`)
console.log('Raw → calibrated (and the band for a small, routine amount):')
for (const raw of [0.3, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 0.99]) {
const p = calibrate(raw, cal)
const band = bandFor(raw, cal, { amount: 499 })
console.log(` ${raw.toFixed(2)} → ${p.toFixed(2)} ${band}`)
}
console.log(
`\nNext: store this calibrator (or its thresholds) where bandFor reads it, ` +
`then enable auto-book for the top band once the empirical accuracy there is acceptable.`,
)
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+186
View File
@@ -0,0 +1,186 @@
#!/usr/bin/env npx tsx
/**
* Re-decode mojibake'd Swedish characters in supplier and customer text fields.
*
* Background: prior to the CSV-encoding fix in lib/import/shared/workbook-reader.ts,
* the supplier/customer CSV importer passed raw bytes to xlsx with `type: 'array'`,
* which decodes UTF-8 multi-byte sequences as Latin-1: turning "GÖTEBORG" into
* "GÖTEBORG" (and similar for å, ä, å, Å, Ä, Ö). This script repairs those rows.
*
* Idempotent: lib/import/shared/encoding.ts:decodeStringContent is a no-op on
* strings that already contain correct Swedish characters, so it is safe to
* re-run.
*
* Usage:
* # Preview every company
* npx tsx scripts/fix-import-mojibake.ts
*
* # Preview a single company
* npx tsx scripts/fix-import-mojibake.ts --company-id <uuid>
*
* # Apply
* npx tsx scripts/fix-import-mojibake.ts --commit
* npx tsx scripts/fix-import-mojibake.ts --company-id <uuid> --commit
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import { decodeStringContent, hasEncodingIssues } from '../lib/import/shared/encoding'
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
const COMPANY_ID = arg('company-id') ?? null
const COMMIT = process.argv.includes('--commit')
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey) as SupabaseClient
console.log('─────────────────────────────────────────────────────────')
console.log('Mojibake repair: suppliers + customers')
console.log('─────────────────────────────────────────────────────────')
console.log('Supabase URL :', supabaseUrl)
console.log('Company :', COMPANY_ID ?? '(all)')
console.log('Mode :', COMMIT ? 'COMMIT (writes)' : 'DRY RUN (no writes)')
console.log('─────────────────────────────────────────────────────────\n')
const SUPPLIER_TEXT_FIELDS = [
'name',
'address_line1',
'address_line2',
'city',
'country',
'category',
'notes',
] as const
const CUSTOMER_TEXT_FIELDS = [
'name',
'address_line1',
'address_line2',
'city',
'country',
'notes',
] as const
const PAGE_SIZE = 500
async function fetchAll(table: 'suppliers' | 'customers', columns: string[]) {
const all: Record<string, unknown>[] = []
let from = 0
for (;;) {
let query = supabase
.from(table)
.select(['id', 'company_id', ...columns].join(','))
.order('id', { ascending: true })
.range(from, from + PAGE_SIZE - 1)
if (COMPANY_ID) {
query = query.eq('company_id', COMPANY_ID)
}
const { data, error } = await query
if (error) throw new Error(`Failed to read ${table}: ${error.message}`)
const rows = ((data ?? []) as unknown) as Record<string, unknown>[]
all.push(...rows)
if (rows.length < PAGE_SIZE) break
from += PAGE_SIZE
}
return all
}
interface RepairResult {
scanned: number
affected: number
fields: number
applied: number
failed: number
}
async function repair(
table: 'suppliers' | 'customers',
textFields: readonly string[],
): Promise<RepairResult> {
console.log(`[${table}]`)
const rows = await fetchAll(table, [...textFields])
console.log(` · Scanned ${rows.length} rows`)
let affected = 0
let fieldsFixed = 0
let applied = 0
let failed = 0
for (const row of rows) {
const updates: Record<string, string | null> = {}
const before: Record<string, string> = {}
for (const field of textFields) {
const value = row[field]
if (typeof value !== 'string' || value === '') continue
if (!hasEncodingIssues(value)) continue
const fixed = decodeStringContent(value)
if (fixed === value) continue
updates[field] = fixed
before[field] = value
}
if (Object.keys(updates).length === 0) continue
affected++
fieldsFixed += Object.keys(updates).length
const id = row.id as string
const companyId = row.company_id as string
console.log(`\n · ${table}.${id} (company ${companyId})`)
for (const [field, fixed] of Object.entries(updates)) {
console.log(` ${field}:`)
console.log(` before: ${JSON.stringify(before[field])}`)
console.log(` after : ${JSON.stringify(fixed)}`)
}
if (!COMMIT) continue
const { error } = await supabase.from(table).update(updates).eq('id', id)
if (error) {
console.error(` FAILED: ${error.message}`)
failed++
} else {
applied++
}
}
return { scanned: rows.length, affected, fields: fieldsFixed, applied, failed }
}
async function main() {
try {
const sup = await repair('suppliers', SUPPLIER_TEXT_FIELDS)
const cus = await repair('customers', CUSTOMER_TEXT_FIELDS)
console.log('\n─────────────────────────────────────────────────────────')
console.log('Summary')
console.log('─────────────────────────────────────────────────────────')
console.log(`suppliers : ${sup.affected}/${sup.scanned} rows affected (${sup.fields} fields)`)
console.log(`customers : ${cus.affected}/${cus.scanned} rows affected (${cus.fields} fields)`)
if (COMMIT) {
console.log(`Applied : ${sup.applied + cus.applied}`)
console.log(`Failed : ${sup.failed + cus.failed}`)
} else {
console.log('\nRe-run with --commit to apply.')
}
} catch (err) {
console.error('\nFATAL:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
main()
+352
View File
@@ -0,0 +1,352 @@
#!/usr/bin/env npx tsx
/**
* Find and repair U+FFFD (replacement character) corruption in user-facing
* text columns across the production database.
*
* Cause: prior to the fix in lib/import/sie-parser.ts:decodeBuffer, the SIE
* importer's encoding detector sampled only the first 4 KB of each file. When
* Swedish characters appeared only deeper in the file, the detector defaulted
* to UTF-8. Decoding a Windows-1252 byte like 0xD6 (Ö) as UTF-8 produces the
* replacement character U+FFFD: silently, because TextDecoder defaults to
* `fatal: false`. The mangled strings ended up in customers, suppliers,
* journal_entries, etc.
*
* Recovery is not deterministic: the original byte is lost. This script does
* heuristic substitution against a small Swedish-word dictionary:
*
* 1. Find rows where any text column contains U+FFFD.
* 2. For each U+FFFD-containing word, try substituting Å/Ä/Ö (uppercase
* context) or å/ä/ö (lowercase). Use the surrounding word casing to
* pick the case.
* 3. If exactly one substitution matches a known Swedish stem, apply it.
* 4. Otherwise log the row for manual review and leave it untouched.
*
* Idempotent: re-running matches zero rows once successful repairs are applied.
*
* Usage:
* # Preview every company
* npx tsx scripts/fix-replacement-chars.ts
*
* # Preview a single company
* npx tsx scripts/fix-replacement-chars.ts --company-id <uuid>
*
* # Apply
* npx tsx scripts/fix-replacement-chars.ts --commit
* npx tsx scripts/fix-replacement-chars.ts --company-id <uuid> --commit
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import { recoverStringWithFFFD } from '../lib/import/shared/encoding'
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
const COMPANY_ID = arg('company-id') ?? null
const COMMIT = process.argv.includes('--commit')
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey) as SupabaseClient
const REPLACEMENT = '\uFFFD'
interface ColumnSpec {
table: string
/** Column for tenant filtering. Most have company_id; legacy tables may use user_id. */
tenantColumn: 'company_id' | 'user_id' | null
/** Free-text columns to scan for U+FFFD. */
columns: readonly string[]
/** Optional row filter: used to skip immutable/posted rows. */
filter?: (row: Record<string, unknown>) => boolean
/** Extra SELECTs needed by the filter: pulled but not scanned for U+FFFD. */
extraSelect?: readonly string[]
}
const TARGETS: readonly ColumnSpec[] = [
{
table: 'customers',
tenantColumn: 'company_id',
columns: ['name', 'address_line1', 'address_line2', 'city', 'country', 'notes'],
},
{
table: 'suppliers',
tenantColumn: 'company_id',
columns: ['name', 'address_line1', 'address_line2', 'city', 'country', 'category', 'notes'],
},
{
table: 'transactions',
tenantColumn: 'company_id',
columns: ['description', 'merchant_name', 'notes'],
},
{
table: 'chart_of_accounts',
tenantColumn: 'company_id',
columns: ['account_name', 'description'],
},
{
table: 'journal_entries',
tenantColumn: 'company_id',
columns: ['description'],
extraSelect: ['status'],
// Skip posted entries: they're legally immutable per BFL.
filter: (row) => row.status !== 'posted',
},
// Note: journal_entry_lines.line_description is intentionally NOT scanned.
// The table has no direct company_id column, and posted lines are immutable
// per the engine's enforcement triggers. If line descriptions need repair,
// run a targeted SQL query against drafts only.
{
table: 'voucher_gap_explanations',
tenantColumn: 'company_id',
columns: ['explanation'],
},
{
table: 'cost_centers',
tenantColumn: 'company_id',
columns: ['name'],
},
{
table: 'projects',
tenantColumn: 'company_id',
columns: ['name'],
},
{
table: 'receipts',
tenantColumn: 'company_id',
columns: ['merchant_name', 'representation_purpose'],
},
{
table: 'receipt_line_items',
tenantColumn: 'company_id',
columns: ['description'],
},
{
table: 'employees',
tenantColumn: 'company_id',
columns: ['first_name', 'last_name', 'address_line1', 'postal_code', 'city'],
},
{
table: 'invoices',
tenantColumn: 'company_id',
columns: ['your_reference', 'our_reference', 'notes', 'reverse_charge_text'],
},
{
table: 'invoice_items',
tenantColumn: 'company_id',
columns: ['description'],
},
{
table: 'supplier_invoices',
tenantColumn: 'company_id',
columns: ['notes'],
extraSelect: ['status'],
filter: (row) => !['paid', 'credited', 'reversed'].includes(String(row.status)),
},
{
table: 'supplier_invoice_items',
tenantColumn: 'company_id',
columns: ['description'],
},
{
table: 'categorization_templates',
tenantColumn: 'company_id',
columns: ['counterparty_name'],
},
{
table: 'companies',
tenantColumn: null, // top-level: filter directly on id when --company-id is given
columns: ['name', 'address_line1', 'address_line2', 'city'],
},
]
const PAGE_SIZE = 500
async function fetchAll(spec: ColumnSpec): Promise<Record<string, unknown>[]> {
const selectCols = ['id', ...spec.columns, ...(spec.extraSelect ?? [])]
if (spec.tenantColumn) selectCols.push(spec.tenantColumn)
const all: Record<string, unknown>[] = []
let from = 0
for (;;) {
let query = supabase
.from(spec.table)
.select(selectCols.join(','))
.order('id', { ascending: true })
.range(from, from + PAGE_SIZE - 1)
if (COMPANY_ID) {
if (spec.tenantColumn === 'company_id') {
query = query.eq('company_id', COMPANY_ID)
} else if (spec.table === 'companies') {
query = query.eq('id', COMPANY_ID)
}
}
const { data, error } = await query
if (error) throw new Error(`Failed to read ${spec.table}: ${error.message}`)
const rows = (data ?? []) as unknown as Record<string, unknown>[]
all.push(...rows)
if (rows.length < PAGE_SIZE) break
from += PAGE_SIZE
}
return all
}
interface Report {
table: string
scanned: number
affected: number
recovered: number
ambiguous: number
applied: number
failed: number
}
async function scanTable(spec: ColumnSpec): Promise<Report> {
const rows = await fetchAll(spec)
const filtered = spec.filter ? rows.filter(spec.filter) : rows
let affected = 0
let recovered = 0
let ambiguous = 0
let applied = 0
let failed = 0
for (const row of filtered) {
const updates: Record<string, string> = {}
const ambiguousFields: { field: string; value: string }[] = []
let rowHasFFFD = false
for (const col of spec.columns) {
const value = row[col]
if (typeof value !== 'string' || !value.includes(REPLACEMENT)) continue
rowHasFFFD = true
const fixed = recoverStringWithFFFD(value)
if (fixed !== null && fixed !== value) {
updates[col] = fixed
} else {
ambiguousFields.push({ field: col, value })
}
}
if (!rowHasFFFD) continue
affected++
const id = row.id as string
const tenantId = spec.tenantColumn ? row[spec.tenantColumn] : null
if (Object.keys(updates).length > 0) {
recovered++
console.log(`\n · ${spec.table}.${id}${tenantId ? ` (${spec.tenantColumn}=${tenantId})` : ''}`)
for (const [field, fixed] of Object.entries(updates)) {
const before = row[field] as string
console.log(` ${field}:`)
console.log(` before: ${JSON.stringify(before)}`)
console.log(` after : ${JSON.stringify(fixed)}`)
}
if (COMMIT) {
const { error } = await supabase.from(spec.table).update(updates).eq('id', id)
if (error) {
console.error(` FAILED: ${error.message}`)
failed++
} else {
applied++
}
}
}
if (ambiguousFields.length > 0) {
ambiguous++
console.log(
`\n · ${spec.table}.${id}${tenantId ? ` (${spec.tenantColumn}=${tenantId})` : ''}: AMBIGUOUS (manual review):`
)
for (const { field, value } of ambiguousFields) {
console.log(` ${field}: ${JSON.stringify(value)}`)
}
}
}
return {
table: spec.table,
scanned: filtered.length,
affected,
recovered,
ambiguous,
applied,
failed,
}
}
async function main() {
console.log('─────────────────────────────────────────────────────────')
console.log('U+FFFD repair across user-facing text columns')
console.log('─────────────────────────────────────────────────────────')
console.log('Supabase URL :', supabaseUrl)
console.log('Company :', COMPANY_ID ?? '(all)')
console.log('Mode :', COMMIT ? 'COMMIT (writes)' : 'DRY RUN (no writes)')
console.log('─────────────────────────────────────────────────────────\n')
const reports: Report[] = []
for (const spec of TARGETS) {
console.log(`[${spec.table}]`)
try {
const report = await scanTable(spec)
reports.push(report)
console.log(
` · Scanned ${report.scanned} rows; ${report.affected} affected (${report.recovered} recovered, ${report.ambiguous} ambiguous)`
)
} catch (err) {
console.error(` · FAILED: ${err instanceof Error ? err.message : err}`)
}
}
console.log('\n─────────────────────────────────────────────────────────')
console.log('Summary')
console.log('─────────────────────────────────────────────────────────')
const totals = reports.reduce(
(acc, r) => ({
scanned: acc.scanned + r.scanned,
affected: acc.affected + r.affected,
recovered: acc.recovered + r.recovered,
ambiguous: acc.ambiguous + r.ambiguous,
applied: acc.applied + r.applied,
failed: acc.failed + r.failed,
}),
{ scanned: 0, affected: 0, recovered: 0, ambiguous: 0, applied: 0, failed: 0 }
)
for (const r of reports) {
if (r.affected === 0) continue
console.log(
`${r.table.padEnd(28)}: ${r.affected.toString().padStart(4)} affected, ${r.recovered.toString().padStart(4)} recovered, ${r.ambiguous.toString().padStart(4)} ambiguous`
)
}
console.log('─────────────────────────────────────────────────────────')
console.log(
`TOTAL : ${totals.affected.toString().padStart(4)} affected, ${totals.recovered.toString().padStart(4)} recovered, ${totals.ambiguous.toString().padStart(4)} ambiguous`
)
if (COMMIT) {
console.log(`Applied : ${totals.applied}`)
console.log(`Failed : ${totals.failed}`)
} else {
console.log('\nDry run: no changes written. Re-run with --commit to apply.')
console.log(
'Ambiguous rows are NOT touched: review them by hand and update manually if needed.'
)
}
}
main().catch((err) => {
console.error('\nFATAL:', err instanceof Error ? err.message : err)
process.exit(1)
})
+56
View File
@@ -0,0 +1,56 @@
/**
* Regenerates lib/bookkeeping/bas-account-numbers.ts from the BAS chart.
*
* npx tsx scripts/generate-bas-account-numbers.ts # write
* npx tsx scripts/generate-bas-account-numbers.ts --check # exit 1 if stale
*
* The generated module is a sorted list of the ~1,276 standard account
* numbers (~9 KB) so client components can answer "is this a standard BAS
* account?" without the 315 KB data chunk. A unit test pins parity too.
*/
import fs from 'node:fs'
import path from 'node:path'
import { BAS_REFERENCE } from '../lib/bookkeeping/bas-data'
const OUT = path.resolve(__dirname, '..', 'lib', 'bookkeeping', 'bas-account-numbers.ts')
export function renderBasAccountNumbers(numbers: readonly string[]): string {
const sorted = [...new Set(numbers)].sort()
const rows: string[] = []
for (let i = 0; i < sorted.length; i += 12) {
rows.push(' ' + sorted.slice(i, i + 12).map((n) => `'${n}'`).join(', ') + ',')
}
return `// GENERATED by scripts/generate-bas-account-numbers.ts from lib/bookkeeping/bas-data.
// Do not edit by hand: run \`npx tsx scripts/generate-bas-account-numbers.ts\`.
//
// The sorted list of standard BAS account numbers (~9 KB) so client code can
// check membership without importing the full chart (315 KB uncompressed).
// lib/bookkeeping/__tests__/bas-account-numbers.test.ts pins parity.
export const BAS_ACCOUNT_NUMBERS: readonly string[] = [
${rows.join('\n')}
]
const BAS_ACCOUNT_NUMBER_SET: ReadonlySet<string> = new Set(BAS_ACCOUNT_NUMBERS)
/** Whether the number exists in the BAS chart (same answer as isStandardBASAccount). */
export function isStandardBASAccountNumber(accountNumber: string): boolean {
return BAS_ACCOUNT_NUMBER_SET.has(accountNumber)
}
`
}
if (require.main === module) {
const rendered = renderBasAccountNumbers(BAS_REFERENCE.map((a) => a.account_number))
if (process.argv.includes('--check')) {
const current = fs.existsSync(OUT) ? fs.readFileSync(OUT, 'utf8') : ''
if (current !== rendered) {
console.error('lib/bookkeeping/bas-account-numbers.ts is stale: run npx tsx scripts/generate-bas-account-numbers.ts')
process.exit(1)
}
console.log('bas-account-numbers.ts is up to date')
} else {
fs.writeFileSync(OUT, rendered)
console.log(`wrote ${OUT}`)
}
}
+301
View File
@@ -0,0 +1,301 @@
#!/usr/bin/env npx tsx
/**
* Crontab generator: emits docker/crontab.hosted and docker/crontab.self-hosted
* from the `crons` array in vercel.json.
*
* vercel.json is the single source of truth for what runs on a schedule. The
* Docker deployments run the same HTTP endpoints through supercronic, so their
* crontabs are generated from that array instead of hand-maintained.
*
* Hand-maintaining them is exactly what went wrong: seven of sixteen jobs were
* missing from Docker entirely (recurring invoices never sent, webhooks never
* dispatched, idempotency_keys grew unbounded), /api/tax-deadlines/cron had
* degraded from daily to "0 0 2 1 *" (once a year, on 2 January), and
* /api/documents/verify/cron ran weekly against a daily vercel.json.
*
* Usage:
* npx tsx scripts/generate-crontabs.ts # rewrite both crontabs
* npm run crontabs:generate # same, via package.json
*
* scripts/__tests__/generate-crontabs.test.ts fails CI if the committed files
* drift from vercel.json again.
*/
import { readFileSync, writeFileSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
const __filename = fileURLToPath(import.meta.url)
const ROOT = dirname(dirname(__filename))
const VERCEL_JSON_PATH = join(ROOT, 'vercel.json')
const DOCKER_DIR = join(ROOT, 'docker')
export type CrontabVariant = 'hosted' | 'self-hosted'
export const VARIANTS: readonly CrontabVariant[] = ['hosted', 'self-hosted']
export interface VercelCron {
path: string
schedule: string
}
/** The compose file that bind-mounts each variant, quoted in the file header. */
const MOUNTED_BY: Record<CrontabVariant, string> = {
hosted: 'docker-compose.hosted.yml',
'self-hosted': 'docker-compose.yml',
}
/**
* The invocation. `${CRON_SECRET}` and `${APP_URL}` are expanded by the shell
* supercronic runs each job under; both come from the cron service's
* environment in docker-compose.yml. Not a template literal on purpose: the
* `${...}` must reach the file verbatim.
*/
const CURL_PREFIX = 'curl -sf -H "Authorization: Bearer ${CRON_SECRET}" ${APP_URL}'
/**
* Paths in vercel.json deliberately NOT emitted into the Docker crontabs, each
* with its reason. A silent omission is drift; this table is the only
* sanctioned way to leave a scheduled endpoint out of a self-hosted install.
*
* Empty, and that is the reviewed answer rather than an oversight:
*
* - Extension endpoints stay in, including extensions the self-hosted preset
* does not enable. docker/extensions.self-hosted.json turns on eight
* extensions, but the preset only drives the runtime registry: the
* Dockerfile builds the whole app/ tree, so every extension cron route is
* compiled into the image either way. And every one of them answers HTTP
* 200 with a no-op body when its extension is unconfigured (skatteverket
* checks SKATTEVERKET_ENABLED, stripe checks STRIPE_SECRET_KEY plus
* STRIPE_CONNECT_CLIENT_ID, cloud-backup and enable-banking simply find no
* rows). An unused extension therefore costs one cheap request at its
* cadence, and a self-hoster who enables it later does not have to discover
* that the schedule was never there. This also matches the pre-existing
* intent: the hand-written crontabs already listed enable-banking and
* skattekonto, both of which are now in the self-hosted preset (unconfigured extensions no-op).
* - /api/sandbox/cleanup/cron stays in. The sandbox is a database flag
* (company_settings.is_sandbox), not a hosted-only build flag, and the
* cleanup_expired_sandbox_users RPC ships in supabase/migrations
* (20260311120000_sandbox_support.sql), so a self-hosted database has it.
* With no sandbox users it deletes 0 rows and returns 200.
*/
export const EXCLUDED_PATHS: Readonly<Record<string, string>> = {}
/**
* Per-variant schedule overrides, path to cron expression. This is where a
* deliberate hosted/self-hosted divergence gets recorded, so it reads as a
* decision with a reason instead of as drift in a hand-edited file.
*
* Empty on both variants: every job currently runs the vercel.json cadence
* everywhere. Two candidates were considered and rejected:
*
* - /api/documents/verify/cron ran weekly ("0 3 * * 0") in the hand-written
* crontabs against a daily vercel.json. Not a load concession: the run is
* capped at 200 documents (DOCUMENT_VERIFY_BATCH_SIZE) and walks a
* nulls-first queue, so weekly drains the WORM integrity queue seven times
* slower on a check that exists to satisfy BFL 7-year retention. The weekly
* cadence also sat in crontab.hosted, which is not a small box (the two
* files were byte-identical), so it cannot have been a self-hosted
* concession. Treated as drift and realigned to daily.
* - /api/webhooks/dispatch/cron at "* * * * *" is 1440 requests/day that
* self-hosted was not making before. Left at parity anyway: the retry
* ladder in lib/webhooks/dispatcher.ts opens with a 60-second first retry,
* which a coarser tick would silently stretch, and each tick is one indexed
* query that returns immediately when nothing is due. See the load note in
* the generated file header.
*/
export const SCHEDULE_OVERRIDES: Readonly<
Record<CrontabVariant, Readonly<Record<string, string>>>
> = {
hosted: {},
'self-hosted': {},
}
/**
* Jobs that exist in ONE variant only and therefore have no vercel.json entry
* (vercel.json is the hosted schedule). Each carries its reason, the same
* discipline as EXCLUDED_PATHS: a self-hosted-only endpoint that silently
* lacked a schedule would be dead code that looks alive.
*
* Rendered after the vercel.json jobs under their own comment line. The
* crontab drift test checks both halves: the vercel.json mirror AND that
* every EXTRA_JOBS path is a real cron route that vercel.json does NOT
* schedule (the moment it does, the entry must go).
*/
export interface ExtraJob {
path: string
schedule: string
reason: string
}
export const EXTRA_JOBS: Readonly<Record<CrontabVariant, readonly ExtraJob[]>> = {
hosted: [],
'self-hosted': [
{
path: '/api/connector/sync/cron',
schedule: '17 * * * *',
reason:
'Self-hosted only: refreshes the source=connector capability grants from the instance\'s ' +
'GNUBOK_CONNECTOR_KEY (hourly; grants carry a 72h offline grace). Hosted has no connector ' +
'key, so the route is not in vercel.json; an instance without a key answers not_configured.',
},
],
}
/** Read and shape-check the `crons` array. */
export function readVercelCrons(vercelJson: string): VercelCron[] {
const parsed = JSON.parse(vercelJson) as { crons?: VercelCron[] }
const crons = parsed.crons ?? []
for (const cron of crons) {
if (typeof cron.path !== 'string' || !cron.path.startsWith('/api/')) {
throw new Error(`vercel.json: cron path must start with /api/, got ${JSON.stringify(cron.path)}`)
}
if (typeof cron.schedule !== 'string' || cron.schedule.trim().split(/\s+/).length !== 5) {
throw new Error(
`vercel.json: ${cron.path} needs a 5-field cron expression, got ${JSON.stringify(cron.schedule)}`,
)
}
}
const seen = new Set<string>()
for (const cron of crons) {
if (seen.has(cron.path)) throw new Error(`vercel.json: duplicate cron path ${cron.path}`)
seen.add(cron.path)
}
return crons
}
/** The schedule a given variant should run a path on. */
export function scheduleFor(cron: VercelCron, variant: CrontabVariant): string {
return SCHEDULE_OVERRIDES[variant][cron.path] ?? cron.schedule
}
function buildHeader(variant: CrontabVariant): string[] {
return [
'# AUTO-GENERATED: do not edit. Run `npm run crontabs:generate` to regenerate.',
'#',
`# Variant: ${variant} (bind-mounted by ${MOUNTED_BY[variant]}).`,
'# Source of truth: the `crons` array in vercel.json. Paths, order and',
'# cadence mirror it exactly; scripts/__tests__/generate-crontabs.test.ts',
'# fails CI if this file drifts from it.',
'#',
'# Consumed by supercronic (docker/cron.Dockerfile), bind-mounted read-only',
'# at /etc/supercronic/crontab. Times are UTC: the cron container sets no TZ',
'# and Alpine defaults to UTC, same as Vercel Cron. supercronic skips a tick',
'# whose previous run is still going (overlapping is off by default), so a',
'# slow endpoint cannot pile up.',
'#',
'# Extension endpoints are listed unconditionally, including extensions this',
"# image's preset does not enable. Each answers 200 with a no-op body when",
'# its extension is unconfigured, so an unused one costs a single cheap',
'# request, and enabling it later needs no crontab change.',
'#',
'# Load note: /api/webhooks/dispatch/cron runs every minute, 1440 requests a',
'# day. Each tick is one indexed query that returns immediately when no',
'# delivery is due, and the per-minute cadence is what makes the dispatcher',
'# 60-second first retry actually happen after 60 seconds. Change it only',
'# through SCHEDULE_OVERRIDES in scripts/generate-crontabs.ts, never by',
'# editing this file.',
'',
]
}
/** Render one crontab. Always LF, always newline-terminated. */
export function buildCrontab(
crons: VercelCron[],
variant: CrontabVariant,
options: {
excluded?: Readonly<Record<string, string>>
overrides?: Readonly<Record<CrontabVariant, Readonly<Record<string, string>>>>
extraJobs?: Readonly<Record<CrontabVariant, readonly ExtraJob[]>>
} = {},
): string {
const excluded = options.excluded ?? EXCLUDED_PATHS
const overrides = options.overrides ?? SCHEDULE_OVERRIDES
const jobs = crons
.filter((cron) => !(cron.path in excluded))
.map((cron) => ({
path: cron.path,
schedule: overrides[variant][cron.path] ?? cron.schedule,
}))
const extraJobs = (options.extraJobs ?? EXTRA_JOBS)[variant]
// Align the commands: pad to the widest schedule plus two spaces, the same
// column convention the hand-written files used.
const width = [...jobs, ...extraJobs].reduce((max, job) => Math.max(max, job.schedule.length), 0) + 2
const lines = [
...buildHeader(variant),
...jobs.map((job) => `${job.schedule.padEnd(width)}${CURL_PREFIX}${job.path}`),
...(extraJobs.length > 0
? [
'',
`# ${variant}-only jobs, not in vercel.json: see EXTRA_JOBS in scripts/generate-crontabs.ts`,
...extraJobs.map((job) => `${job.schedule.padEnd(width)}${CURL_PREFIX}${job.path}`),
]
: []),
]
return `${lines.join('\n')}\n`
}
function main(): void {
const crons = readVercelCrons(readFileSync(VERCEL_JSON_PATH, 'utf8'))
for (const path of Object.keys(EXCLUDED_PATHS)) {
if (!crons.some((cron) => cron.path === path)) {
throw new Error(`EXCLUDED_PATHS lists ${path}, which is not in vercel.json. Remove the stale entry.`)
}
}
for (const variant of VARIANTS) {
for (const path of Object.keys(SCHEDULE_OVERRIDES[variant])) {
if (!crons.some((cron) => cron.path === path)) {
throw new Error(
`SCHEDULE_OVERRIDES.${variant} lists ${path}, which is not in vercel.json. Remove the stale entry.`,
)
}
}
}
for (const variant of VARIANTS) {
for (const job of EXTRA_JOBS[variant]) {
if (crons.some((cron) => cron.path === job.path)) {
throw new Error(
`EXTRA_JOBS.${variant} lists ${job.path}, which vercel.json now schedules. Remove the extra entry.`,
)
}
}
}
for (const variant of VARIANTS) {
const target = join(DOCKER_DIR, `crontab.${variant}`)
writeFileSync(target, buildCrontab(crons, variant), 'utf8')
const emitted = crons.filter((cron) => !(cron.path in EXCLUDED_PATHS)).length + EXTRA_JOBS[variant].length
const overridden = Object.keys(SCHEDULE_OVERRIDES[variant]).length
console.log(
`Wrote docker/crontab.${variant}: ${emitted} jobs` +
(overridden > 0 ? `, ${overridden} schedule override(s)` : ''),
)
}
const skipped = Object.keys(EXCLUDED_PATHS)
if (skipped.length > 0) {
console.log(`Excluded ${skipped.length} path(s): ${skipped.join(', ')}`)
} else {
console.log('No exclusions: every vercel.json cron runs on Docker too.')
}
}
// Only run when invoked directly (not when imported by tests).
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
try {
main()
} catch (err) {
console.error(err instanceof Error ? err.message : err)
process.exit(1)
}
}
+295
View File
@@ -0,0 +1,295 @@
/**
* Extension Registry Generator
*
* Reads extensions.config.json and manifest.json files to generate:
* - lib/extensions/_generated/extension-list.ts
* - lib/extensions/_generated/workspace-map.tsx
* - lib/extensions/_generated/sector-definitions.ts
*
* Usage:
* npx tsx scripts/generate-extension-registry.ts # Generate files
* npx tsx scripts/generate-extension-registry.ts --list # List available extensions
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const ROOT = path.resolve(__dirname, '..')
const CONFIG_PATH = path.join(ROOT, 'extensions.config.json')
const EXTENSIONS_DIR = path.join(ROOT, 'extensions')
const OUTPUT_DIR = path.join(ROOT, 'lib', 'extensions', '_generated')
// ── Types ────────────────────────────────────────────────────
interface ManifestDefinition {
name: string
category: string
icon: string
dataPattern: string
readsCoreTables?: string[]
hasOwnData?: boolean
description: string
longDescription: string
quickAction?: {
label: string
description: string
icon: string
href?: string
event?: string
order?: number
}
subscriptionNotice?: string
}
interface Manifest {
id: string
sector: string
exportName: string | null
entryPoint: string | null
workspace: string | null
requiredEnvVars: string[]
optionalEnvVars: string[]
npmDependencies: string[]
definition: ManifestDefinition
}
interface Config {
extensions: string[]
}
// ── Helpers ──────────────────────────────────────────────────
function findManifests(dir: string): string[] {
const results: string[] = []
const entries = fs.readdirSync(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
results.push(...findManifests(fullPath))
} else if (entry.name === 'manifest.json') {
results.push(fullPath)
}
}
return results
}
function loadAllManifests(): Map<string, Manifest> {
const manifestPaths = findManifests(EXTENSIONS_DIR)
const map = new Map<string, Manifest>()
for (const mp of manifestPaths) {
const manifest: Manifest = JSON.parse(fs.readFileSync(mp, 'utf-8'))
if (map.has(manifest.id)) {
console.error(`ERROR: Duplicate extension ID "${manifest.id}" found in:\n ${mp}\n (already defined elsewhere)`)
process.exit(1)
}
map.set(manifest.id, manifest)
}
return map
}
function loadConfig(): Config {
if (!fs.existsSync(CONFIG_PATH)) {
console.warn('Warning: extensions.config.json not found, using empty config')
return { extensions: [] }
}
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'))
}
// ── Generators ───────────────────────────────────────────────
function generateExtensionList(manifests: Manifest[]): string {
const withRuntime = manifests.filter(m => m.exportName && m.entryPoint)
const imports = withRuntime.map(
m => `import { ${m.exportName} } from '${m.entryPoint}'`
)
const entries = withRuntime.map(m => ` ${m.exportName},`)
return [
`// AUTO-GENERATED: do not edit. Run \`npm run setup:extensions\` to regenerate.`,
`import type { Extension } from '../types'`,
...imports,
``,
`export const FIRST_PARTY_EXTENSIONS: Extension[] = [`,
...entries,
`]`,
``,
].join('\n')
}
function generateWorkspaceMap(manifests: Manifest[]): string {
const withWorkspace = manifests.filter(m => m.workspace)
const dynamicImports = withWorkspace.map(m => {
const key = `${m.sector}/${m.id}`
return ` '${key}': dynamic(() => import('${m.workspace}')),`
})
return [
`// AUTO-GENERATED: do not edit. Run \`npm run setup:extensions\` to regenerate.`,
`import dynamic from 'next/dynamic'`,
`import type { ComponentType } from 'react'`,
`import type { WorkspaceComponentProps } from '../workspace-registry'`,
``,
`export const WORKSPACES: Record<string, ComponentType<WorkspaceComponentProps>> = {`,
...dynamicImports,
`}`,
``,
].join('\n')
}
function generateSectorDefinitions(manifests: Manifest[]): string {
// Group by sector
const bySector = new Map<string, Manifest[]>()
for (const m of manifests) {
const existing = bySector.get(m.sector) ?? []
existing.push(m)
bySector.set(m.sector, existing)
}
const sectorEntries: string[] = []
for (const [sector, sectorManifests] of bySector) {
const defs = sectorManifests.map(m => {
const def: Record<string, unknown> = {
slug: m.id,
name: m.definition.name,
sector: m.sector,
category: m.definition.category,
icon: m.definition.icon,
dataPattern: m.definition.dataPattern,
description: m.definition.description,
longDescription: m.definition.longDescription,
}
if (m.definition.readsCoreTables) def.readsCoreTables = m.definition.readsCoreTables
if (m.definition.hasOwnData) def.hasOwnData = m.definition.hasOwnData
if (m.definition.quickAction) def.quickAction = m.definition.quickAction
if (m.definition.subscriptionNotice) def.subscriptionNotice = m.definition.subscriptionNotice
return ` ${JSON.stringify(def, null, 6).split('\n').join('\n ')},`
})
sectorEntries.push(` '${sector}': [\n${defs.join('\n')}\n ],`)
}
return [
`// AUTO-GENERATED: do not edit. Run \`npm run setup:extensions\` to regenerate.`,
`import type { ExtensionDefinition } from '../types'`,
``,
`export const EXTENSION_DEFINITIONS: Record<string, ExtensionDefinition[]> = {`,
...sectorEntries,
`}`,
``,
].join('\n')
}
function generateEnabledExtensions(manifests: Manifest[]): string {
const ids = manifests.map(m => ` '${m.id}',`)
return [
`// AUTO-GENERATED: do not edit. Run \`npm run setup:extensions\` to regenerate.`,
``,
`export const ENABLED_EXTENSION_IDS: ReadonlySet<string> = new Set([`,
...ids,
`])`,
``,
].join('\n')
}
// ── Env var check ────────────────────────────────────────────
function checkEnvVars(manifests: Manifest[]): void {
const warnings: string[] = []
for (const m of manifests) {
for (const envVar of m.requiredEnvVars) {
if (!process.env[envVar]) {
warnings.push(` ${envVar} (required by ${m.id})`)
}
}
}
if (warnings.length > 0) {
console.warn('\nWarning: Missing environment variables:')
for (const w of warnings) {
console.warn(w)
}
console.warn('Extensions will be loaded but may not function correctly.\n')
}
}
// ── Main ─────────────────────────────────────────────────────
function main(): void {
const args = process.argv.slice(2)
const allManifests = loadAllManifests()
// --list mode: print all available extensions
if (args.includes('--list')) {
console.log('\nAvailable extensions:\n')
const sorted = [...allManifests.values()].sort((a, b) => {
if (a.sector !== b.sector) return a.sector.localeCompare(b.sector)
return a.id.localeCompare(b.id)
})
let currentSector = ''
for (const m of sorted) {
if (m.sector !== currentSector) {
currentSector = m.sector
console.log(`\n [${currentSector}]`)
}
const envNote = m.requiredEnvVars.length > 0
? ` (requires: ${m.requiredEnvVars.join(', ')})`
: ''
console.log(` ${m.id.padEnd(25)} ${m.definition.name}${envNote}`)
}
console.log('')
return
}
// Normal mode: generate registry files
const config = loadConfig()
// Validate enabled IDs
for (const id of config.extensions) {
if (!allManifests.has(id)) {
console.error(`ERROR: Unknown extension ID "${id}" in extensions.config.json`)
console.error(`Available IDs: ${[...allManifests.keys()].sort().join(', ')}`)
process.exit(1)
}
}
const enabledManifests = config.extensions.map(id => allManifests.get(id)!)
// Ensure output directory exists
fs.mkdirSync(OUTPUT_DIR, { recursive: true })
// Generate files
fs.writeFileSync(
path.join(OUTPUT_DIR, 'extension-list.ts'),
generateExtensionList(enabledManifests),
)
fs.writeFileSync(
path.join(OUTPUT_DIR, 'workspace-map.tsx'),
generateWorkspaceMap(enabledManifests),
)
fs.writeFileSync(
path.join(OUTPUT_DIR, 'sector-definitions.ts'),
generateSectorDefinitions(enabledManifests),
)
fs.writeFileSync(
path.join(OUTPUT_DIR, 'enabled-extensions.ts'),
generateEnabledExtensions(enabledManifests),
)
// Summary
const enabledNames = enabledManifests.map(m => m.id)
if (enabledNames.length > 0) {
console.log(`Enabled: ${enabledNames.join(', ')}`)
} else {
console.log('No extensions enabled (core-only mode)')
}
// Check env vars
checkEnvVars(enabledManifests)
}
main()
+253
View File
@@ -0,0 +1,253 @@
#!/usr/bin/env npx tsx
/**
* Generate a Supabase seed migration that inlines every atom SKILL.md body into
* agent_atom_registry.body.
*
* Why a generated migration (not a runtime/seed-script write):
* - Vercel's build has no DB, so prebuild seeding can't write.
* - Skill bodies must reach prod via the one deploy path the project trusts:
* supabase/migrations applied on deploy. The SQL is generated, never hand-authored.
* - It also fixes the "registry never seeded" case (the manual seed may never have
* run in prod): the migration populates the rows on deploy.
*
* Determinism:
* - Atoms are emitted sorted by id; bodies are dollar-quoted with a collision-proof
* tag; `version` is derived from a committed content-hash manifest
* (scripts/.skill-body-manifest.json) so it only bumps when a SKILL.md changes.
* - A no-change run emits NOTHING (byte-identical repo). A change emits ONE new
* timestamped migration (append-only: we never edit an existing migration).
*
* Usage:
* npx tsx scripts/generate-skill-bodies.ts # emit a migration if skills changed
* npx tsx scripts/generate-skill-bodies.ts --check # CI guard: exit 1 if a skill changed
* # without a regenerated migration
*/
import { createHash } from 'node:crypto'
import { readdir, writeFile } from 'node:fs/promises'
import { existsSync, readFileSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { discoverAtoms, type DiscoveredAtom } from './lib/atom-discovery'
const __filename = fileURLToPath(import.meta.url)
const ROOT = dirname(dirname(__filename))
const MIGRATIONS_DIR = join(ROOT, 'supabase', 'migrations')
const MANIFEST_PATH = join(ROOT, 'scripts', '.skill-body-manifest.json')
const checkOnly = process.argv.includes('--check')
interface ManifestEntry {
hash: string
version: number
}
type Manifest = Record<string, ManifestEntry>
function sha256(s: string): string {
return createHash('sha256').update(s, 'utf8').digest('hex')
}
function loadManifest(): Manifest {
if (!existsSync(MANIFEST_PATH)) return {}
try {
return JSON.parse(readFileSync(MANIFEST_PATH, 'utf8')) as Manifest
} catch {
return {}
}
}
async function saveManifest(manifest: Manifest): Promise<void> {
// Sorted keys for stable diffs.
const sorted: Manifest = {}
for (const id of Object.keys(manifest).sort()) sorted[id] = manifest[id]
await writeFile(MANIFEST_PATH, JSON.stringify(sorted, null, 2) + '\n', 'utf8')
}
/** Wrap a string in a dollar-quote tag guaranteed not to appear inside it. */
function dollarQuote(s: string): string {
let tag = '$gb$'
let n = 0
while (s.includes(tag)) {
tag = `$gb${n}$`
n++
}
return `${tag}${s}${tag}`
}
/** Single-quoted SQL text literal (apostrophes doubled). For short, simple values. */
function sqlStr(s: string): string {
return `'${s.replace(/'/g, "''")}'`
}
function sqlTextArray(arr: string[]): string {
if (arr.length === 0) return `'{}'::text[]`
return `ARRAY[${arr.map(sqlStr).join(', ')}]::text[]`
}
/** Next migration timestamp = max existing 14-digit prefix + 1 (guarantees ordering). */
async function nextMigrationTimestamp(): Promise<string> {
const files = await readdir(MIGRATIONS_DIR)
// 14-digit timestamps (max ~1e14) are well within Number.MAX_SAFE_INTEGER (~9e15).
let max = 0
for (const f of files) {
const m = /^(\d{14})_/.exec(f)
if (m) {
const n = Number(m[1])
if (n > max) max = n
}
}
return String(max + 1).padStart(14, '0')
}
function buildValuesRow(atom: DiscoveredAtom, version: number): string {
const triggerJson = JSON.stringify(atom.trigger_signals ?? {})
return [
' (',
` ${sqlStr(atom.id)},`,
` ${sqlStr(atom.tier)},`,
` ${sqlStr(atom.title)},`,
` ${dollarQuote(atom.description)},`,
` ${sqlTextArray(atom.sni_prefixes)},`,
` ${dollarQuote(triggerJson)}::jsonb,`,
` ${atom.estimated_tokens},`,
` ${sqlStr(atom.body_path)},`,
` ${dollarQuote(atom.body)},`,
` ${atom.parent_atom_id ? sqlStr(atom.parent_atom_id) : 'NULL'},`,
` ${version},`,
` ${atom.schema_version}`,
' )',
].join('\n')
}
export function buildMigrationSql(atoms: DiscoveredAtom[], versions: Record<string, number>): string {
const header = `-- AUTO-GENERATED by scripts/generate-skill-bodies.ts: DO NOT EDIT BY HAND.
-- Regenerate with \`npm run skills:generate\` after editing any .claude/skills/**/SKILL.md.
--
-- Seeds agent_atom_registry rows with their SKILL.md body so the MCP server and the
-- in-app agent read skill content from the DB (works on Vercel, Docker, self-hosted).
-- Idempotent: ON CONFLICT refreshes content fields but leaves is_active and
-- mcp_exposed under manual control (they take column defaults on first insert).
-- The WHERE guard skips rows whose registry version is newer than this seed's:
-- two branches can each carry a full seed, and the one that applies last can
-- no longer downgrade atoms the other already bumped.
`
const values = atoms.map((a) => buildValuesRow(a, versions[a.id])).join(',\n')
const insert = `INSERT INTO public.agent_atom_registry
(id, tier, title, description, sni_prefixes, trigger_signals, estimated_tokens, body_path, body, parent_atom_id, version, schema_version)
VALUES
${values}
ON CONFLICT (id) DO UPDATE SET
tier = EXCLUDED.tier,
title = EXCLUDED.title,
description = EXCLUDED.description,
sni_prefixes = EXCLUDED.sni_prefixes,
trigger_signals = EXCLUDED.trigger_signals,
estimated_tokens = EXCLUDED.estimated_tokens,
body_path = EXCLUDED.body_path,
body = EXCLUDED.body,
parent_atom_id = EXCLUDED.parent_atom_id,
version = EXCLUDED.version,
schema_version = EXCLUDED.schema_version,
updated_at = now()
WHERE public.agent_atom_registry.version <= EXCLUDED.version;
`
return `${header}\n${insert}\nNOTIFY pgrst, 'reload schema';\n`
}
async function main() {
const atoms = await discoverAtoms(ROOT)
if (atoms.length === 0) {
console.error('No atoms discovered under .claude/skills/: refusing to emit an empty seed.')
process.exit(1)
}
// Dangling references/ links (mcp_optimization_plan P2-2): an atom body
// pointing at a references/ file that does not exist ships a 404 to every
// agent that follows it. Fails BOTH modes so a dangling pointer can never
// reach the registry. Top-level atoms only: reference children live inside
// references/ themselves, so relative links would double-resolve.
const refLinkRe = /(?:\]\(|\b)\.?\/?(references\/[A-Za-z0-9._/-]+\.md)/g
const danglingRefs: string[] = []
for (const atom of atoms) {
if (atom.parent_atom_id) continue
const baseDir = dirname(join(ROOT, atom.body_path))
const seen = new Set<string>()
for (const m of atom.body.matchAll(refLinkRe)) {
const rel = m[1]
if (seen.has(rel)) continue
seen.add(rel)
if (!existsSync(join(baseDir, rel))) danglingRefs.push(`${atom.id}: ${rel}`)
}
}
if (danglingRefs.length > 0) {
console.error('✗ dangling references/ link(s) in atom bodies: create the file or remove the pointer:')
for (const d of danglingRefs) console.error(` ${d}`)
process.exit(1)
}
const manifest = loadManifest()
const onDisk = new Map(atoms.map((a) => [a.id, sha256(a.body)]))
// Drift = added, changed, or removed atoms vs. the committed manifest.
const added: string[] = []
const changed: string[] = []
for (const [id, hash] of onDisk) {
const prev = manifest[id]
if (!prev) added.push(id)
else if (prev.hash !== hash) changed.push(id)
}
const removed = Object.keys(manifest).filter((id) => !onDisk.has(id))
const hasDrift = added.length > 0 || changed.length > 0 || removed.length > 0
if (checkOnly) {
if (!hasDrift) {
console.log(`✓ skill bodies up to date (${atoms.length} atoms).`)
return
}
console.error('✗ skill bodies are STALE: a SKILL.md changed without regenerating the seed migration.')
if (added.length) console.error(` added: ${added.join(', ')}`)
if (changed.length) console.error(` changed: ${changed.join(', ')}`)
if (removed.length) console.error(` removed: ${removed.join(', ')}`)
console.error('\nRun `npm run skills:generate` and commit the emitted migration.')
process.exit(1)
}
if (!hasDrift) {
console.log(`✓ no skill changes (${atoms.length} atoms): nothing to generate.`)
return
}
// Compute versions: bump only changed atoms; new atoms start at 1.
const newManifest: Manifest = {}
const versions: Record<string, number> = {}
for (const atom of atoms) {
const hash = onDisk.get(atom.id)!
const prev = manifest[atom.id]
const version = !prev ? 1 : prev.hash === hash ? prev.version : prev.version + 1
versions[atom.id] = version
newManifest[atom.id] = { hash, version }
}
const ts = await nextMigrationTimestamp()
const fileName = `${ts}_seed_agent_atom_bodies.sql`
const filePath = join(MIGRATIONS_DIR, fileName)
await writeFile(filePath, buildMigrationSql(atoms, versions), 'utf8')
await saveManifest(newManifest)
console.log(`Wrote supabase/migrations/${fileName} (${atoms.length} atoms).`)
if (added.length) console.log(` added: ${added.join(', ')}`)
if (changed.length) console.log(` changed: ${changed.join(', ')}`)
if (removed.length) console.log(` removed (dropped from manifest, row left as-is in DB): ${removed.join(', ')}`)
}
// Only run when invoked directly (not when imported by tests).
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
main().catch((err) => {
console.error(err)
process.exit(1)
})
}
+374
View File
@@ -0,0 +1,374 @@
#!/usr/bin/env npx tsx
/**
* Generate the iXBRL taxonomy concept registry from the official Bolagsverket/
* taxonomier.se element lists in dev_docs/bokslut/taxonomi/dokumentation/.
*
* Why generated (not hand-authored):
* - The element lists (xlsx) are the official mapping spec for which concepts
* exist per delrapport, their namespace, balance (debit/credit) and period
* type. Hand-copying ~1 300 rows guarantees drift; the registry must be
* reproducible from the committed spec files.
* - The September 2026 taxonomy generation replaces tuples with dimensions:
* taxonomy versions are data, not code. New versions become new generated
* JSON files + a new entry in lib/bokslut/ixbrl/taxonomy/entry-points.ts,
* leaving emitters for older versions untouched.
*
* Sources (committed in dev_docs/bokslut/):
* - k2-ab-arsredovisning-elementlista-2024-09-12_rev20250312_sv.xlsx
* → one sheet per delrapport (allmän info, FB, RR, BR, noter, underskrifter…)
* - tuple-innehallsmodell-arsredovisning-k2-2024-09-12.xlsx
* → tuple → ordered member model with per-member "obligatorisk" flag
* - taxonomi-paket-2024-09-12_rev20250312.zip
* → se-comp-base-2020-12-01.xsd parsed for the fastställelseintyg concepts
* (se-bol-base), which are not part of any element list
*
* Output: lib/bokslut/ixbrl/taxonomy/generated/k2-ab-2024-09-12.json
* Deterministic: concepts sorted by name, no timestamps; --check regenerates
* in memory and fails (exit 1) when the committed file is stale, mirroring
* skills:check.
*
* Usage:
* npx tsx scripts/generate-taxonomy-registry.ts # write registry
* npx tsx scripts/generate-taxonomy-registry.ts --check # CI guard
*/
import { createHash } from 'node:crypto'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import * as XLSX from 'xlsx'
import JSZip from 'jszip'
const __filename = fileURLToPath(import.meta.url)
const ROOT = dirname(dirname(__filename))
const DOC_DIR = join(ROOT, 'dev_docs', 'bokslut', 'taxonomi', 'dokumentation')
const PACKAGE_ZIP = join(
ROOT,
'dev_docs',
'bokslut',
'taxonomi',
'taxonomi-paket-2024-09-12_rev20250312.zip',
)
const ELEMENT_LIST = join(
DOC_DIR,
'k2-ab-arsredovisning-elementlista-2024-09-12_rev20250312_sv.xlsx',
)
const TUPLE_MODEL = join(
DOC_DIR,
'tuple-innehallsmodell-arsredovisning-k2-2024-09-12.xlsx',
)
const COMP_BASE_XSD_ENTRY =
'taxonomi-paket-2024-09-12_rev20250312/xbrl.taxonomier.se/se/common/base/se-comp-base/2020-12-01/se-comp-base-2020-12-01.xsd'
const OUT_PATH = join(
ROOT,
'lib',
'bokslut',
'ixbrl',
'taxonomy',
'generated',
'k2-ab-2024-09-12.json',
)
const checkOnly = process.argv.includes('--check')
/** Sheet name (as it appears in the workbook) → registry section key. */
const SHEET_SECTIONS: Record<string, string> = {
'Allmän information': 'allman-information',
'Förvaltningsberättelse': 'forvaltningsberattelse',
'Kostnadsslagsindelad resultatr': 'rr-kostnadsslagsindelad',
'Förkortad kostnadsslagsindelad': 'rr-kostnadsslagsindelad-forkortad',
'Balansräkning': 'balansrakning',
'Förkortad balansräkning': 'balansrakning-forkortad',
'Kassaflödesanalys indirekt met': 'kassaflodesanalys',
'Noter': 'noter',
// Trailing space is present in the workbook sheet name.
'Undertecknande av företrädare ': 'undertecknande',
}
interface ConceptOut {
/** Namespace prefix, e.g. "se-gen-base". Resolved to a URI in entry-points.ts. */
ns: string
/** Standardrubrik: the official presentation label. */
label: string
abstract: boolean
/** XBRL datatype, e.g. "xbrli:monetaryItemType". Null for tuples. */
dataType: string | null
balance: 'debit' | 'credit' | null
periodType: 'duration' | 'instant' | null
kind: 'item' | 'tuple'
/** Which delrapporter (sheets) list the concept. */
sections: string[]
}
interface TupleMemberOut {
name: string
ns: string
required: boolean
}
interface RegistryOut {
_meta: {
taxonomy: 'k2-ab'
version: '2024-09-12'
revision: '2025-03-12'
generator: 'scripts/generate-taxonomy-registry.ts'
sources: Record<string, string>
conceptCount: number
tupleCount: number
}
concepts: Record<string, ConceptOut>
tuples: Record<string, { ns: string; members: TupleMemberOut[] }>
}
function sha256(buf: Buffer | string): string {
return createHash('sha256').update(buf).digest('hex')
}
function cellStr(v: unknown): string | null {
if (v === null || v === undefined) return null
const s = String(v).trim()
return s.length > 0 ? s : null
}
function parseElementList(): Record<string, ConceptOut> {
const wb = XLSX.read(readFileSync(ELEMENT_LIST), { type: 'buffer' })
const concepts: Record<string, ConceptOut> = {}
for (const sheetName of wb.SheetNames) {
const section = SHEET_SECTIONS[sheetName]
if (!section) {
throw new Error(
`Unknown sheet "${sheetName}" in element list: add it to SHEET_SECTIONS`,
)
}
const rows = XLSX.utils.sheet_to_json<unknown[]>(wb.Sheets[sheetName], {
header: 1,
blankrows: false,
})
const header = rows[0] as (string | null)[]
const col = (label: string): number => {
const i = header.indexOf(label)
if (i === -1)
throw new Error(`Column "${label}" missing on sheet "${sheetName}"`)
return i
}
const iEl = col('Elementnamn')
const iNs = col('Tillhör')
const iLabel = col('Standardrubrik')
const iAbstract = col('Abstrakt')
const iDataType = col('Datatyp')
const iBalance = col('Saldo')
const iPeriodType = col('Periodtyp')
const iKind = col('Typ')
for (let r = 1; r < rows.length; r++) {
const row = rows[r]
const name = cellStr(row[iEl])
if (!name) continue
const ns = cellStr(row[iNs])
if (!ns) throw new Error(`Row ${r} on "${sheetName}": missing Tillhör for ${name}`)
const balanceRaw = cellStr(row[iBalance])
const periodRaw = cellStr(row[iPeriodType])
const kindRaw = cellStr(row[iKind])
const parsed: ConceptOut = {
ns,
label: cellStr(row[iLabel]) ?? name,
abstract: cellStr(row[iAbstract]) === 'true',
dataType: cellStr(row[iDataType]),
balance:
balanceRaw === 'debit' || balanceRaw === 'credit' ? balanceRaw : null,
periodType:
periodRaw === 'duration' || periodRaw === 'instant' ? periodRaw : null,
kind: kindRaw === 'tuple' ? 'tuple' : 'item',
sections: [section],
}
const existing = concepts[name]
if (existing) {
// Same element listed on several sheets (shared concepts). Attribute
// mismatches would mean the spec disagrees with itself: fail loudly.
for (const key of ['ns', 'dataType', 'balance', 'periodType', 'kind'] as const) {
if (existing[key] !== parsed[key]) {
throw new Error(
`Concept ${name}: "${String(key)}" differs between sheets (${String(existing[key])} vs ${String(parsed[key])})`,
)
}
}
if (!existing.sections.includes(section)) existing.sections.push(section)
} else {
concepts[name] = parsed
}
}
}
return concepts
}
function parseTupleModel(
concepts: Record<string, ConceptOut>,
): Record<string, { ns: string; members: TupleMemberOut[] }> {
const wb = XLSX.read(readFileSync(TUPLE_MODEL), { type: 'buffer' })
const rows = XLSX.utils.sheet_to_json<unknown[]>(wb.Sheets[wb.SheetNames[0]], {
header: 1,
blankrows: false,
})
// Row 0 is a super-header ("Obligatoriskt värde i Tuple" lives at index 12);
// row 1 holds the real column labels.
const header = rows[1] as (string | null)[]
const iNs = header.indexOf('Tillhör')
const iEl = header.indexOf('Elementnamn')
const iKind = header.indexOf('Typ')
const iAb = header.indexOf('AB')
const iRequired = 12
if (iNs === -1 || iEl === -1 || iKind === -1 || iAb === -1) {
throw new Error('Tuple model columns moved: update parseTupleModel')
}
const tuples: Record<string, { ns: string; members: TupleMemberOut[] }> = {}
let current: { name: string; inAb: boolean } | null = null
for (let r = 2; r < rows.length; r++) {
const row = rows[r]
const name = cellStr(row[iEl])
if (!name) continue
const kind = cellStr(row[iKind])
const ns = cellStr(row[iNs]) ?? 'se-gaap-ext'
if (kind === 'xbrli:tuple') {
const inAb = cellStr(row[iAb]) === 'JA'
current = { name, inAb }
if (inAb) tuples[name] = { ns, members: [] }
} else if (kind === 'xbrli:item' && current?.inAb) {
tuples[current.name].members.push({
name,
ns,
required: cellStr(row[iRequired]) === 'JA',
})
}
}
// Tuples used by K2 AB must also exist as concepts (the element list carries
// the ones that appear in the K2 AB report; the tuple model spans all
// company forms). Members referenced but missing from the element list are
// added as items so the registry is self-contained.
for (const [name, t] of Object.entries(tuples)) {
if (!concepts[name]) {
concepts[name] = {
ns: t.ns,
label: name,
abstract: false,
dataType: null,
balance: null,
periodType: null,
kind: 'tuple',
sections: ['tuple-model'],
}
}
for (const m of t.members) {
if (!concepts[m.name]) {
concepts[m.name] = {
ns: m.ns,
label: m.name,
abstract: false,
dataType: 'xbrli:stringItemType',
balance: null,
periodType: 'duration',
kind: 'item',
sections: ['tuple-model'],
}
}
}
}
return tuples
}
/**
* The fastställelseintyg concepts (se-bol-base) come from Bolagsverket's
* comp-base schema inside the taxonomy package: they are not in any element
* list. Parse the XSD element declarations directly.
*/
async function parseCompBase(concepts: Record<string, ConceptOut>): Promise<void> {
const zip = await JSZip.loadAsync(readFileSync(PACKAGE_ZIP))
const file = zip.file(COMP_BASE_XSD_ENTRY)
if (!file) throw new Error(`${COMP_BASE_XSD_ENTRY} missing from taxonomy package`)
const xsd = await file.async('string')
const decls = xsd.matchAll(/<xsd:element ([^>]*)\/>/g)
for (const [, attrText] of decls) {
const attr = (n: string): string | null => {
const m = attrText.match(new RegExp(`${n}="([^"]*)"`))
return m ? m[1] : null
}
const name = attr('name')
if (!name) continue
const subst = attr('substitutionGroup')
const periodType = attr('xbrli:periodType')
concepts[name] = {
ns: 'se-bol-base',
label: name,
abstract: attr('abstract') === 'true',
dataType: attr('type'),
balance: null,
periodType:
periodType === 'duration' || periodType === 'instant' ? periodType : null,
kind: subst === 'xbrli:tuple' ? 'tuple' : 'item',
sections: ['faststallelseintyg'],
}
}
}
function sortedRecord<T>(rec: Record<string, T>): Record<string, T> {
const out: Record<string, T> = {}
for (const key of Object.keys(rec).sort()) out[key] = rec[key]
return out
}
async function buildRegistry(): Promise<string> {
const concepts = parseElementList()
const tuples = parseTupleModel(concepts)
await parseCompBase(concepts)
const registry: RegistryOut = {
_meta: {
taxonomy: 'k2-ab',
version: '2024-09-12',
revision: '2025-03-12',
generator: 'scripts/generate-taxonomy-registry.ts',
sources: {
'k2-ab-arsredovisning-elementlista-2024-09-12_rev20250312_sv.xlsx':
sha256(readFileSync(ELEMENT_LIST)),
'tuple-innehallsmodell-arsredovisning-k2-2024-09-12.xlsx':
sha256(readFileSync(TUPLE_MODEL)),
'se-comp-base-2020-12-01.xsd': COMP_BASE_XSD_ENTRY,
},
conceptCount: Object.keys(concepts).length,
tupleCount: Object.keys(tuples).length,
},
concepts: sortedRecord(concepts),
tuples: sortedRecord(tuples),
}
return JSON.stringify(registry, null, 2) + '\n'
}
async function main(): Promise<void> {
const json = await buildRegistry()
if (checkOnly) {
const committed = existsSync(OUT_PATH) ? readFileSync(OUT_PATH, 'utf8') : null
if (committed !== json) {
console.error(
`Taxonomy registry is stale: ${OUT_PATH} does not match the element lists.\n` +
'Run: npx tsx scripts/generate-taxonomy-registry.ts',
)
process.exit(1)
}
console.log('Taxonomy registry is up to date.')
return
}
mkdirSync(dirname(OUT_PATH), { recursive: true })
writeFileSync(OUT_PATH, json, 'utf8')
const parsed = JSON.parse(json) as RegistryOut
console.log(
`Wrote ${OUT_PATH} (${parsed._meta.conceptCount} concepts, ${parsed._meta.tupleCount} tuples)`,
)
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+235
View File
@@ -0,0 +1,235 @@
/**
* Parse Skatteverket SKV 434 monthly tax tables (fixed-width TXT) and emit a
* TypeScript module used as an emergency fallback when Skatteverket's open-data
* API is unavailable.
*
* Input: data/tax-tables/{year}/allmanna-tabeller-manad.txt
* Output: lib/salary/tax-tables-fallback.ts
*
* Record format (49 chars per line):
* chars 0-4 (width 5): prefix : "30B29" = monthly/belopp, table 29;
* "30%29" = monthly/percent, table 29
* chars 5-11 (width 7): income_from
* chars 12-18 (width 7): income_to (blank on the open-ended top %-row)
* chars 19-23 (width 5): column 1 (SEK on B-rows, percent on %-rows)
* chars 24-28 (width 5): column 2
* chars 29-33 (width 5): column 3
* chars 34-38 (width 5): column 4
* chars 39-43 (width 5): column 5
* chars 44-48 (width 5): column 6
*
* Both B-rows (absolute amounts, incomes up to 80 000 kr/month) and %-rows
* (percent of the whole income, above 80 000 kr/month) are imported. The
* emitted tuple carries an isPercent flag as its last element.
*
* Usage:
* npx tsx scripts/import-tax-tables.ts --year 2026
*/
import { readFileSync, writeFileSync } from 'fs'
import { resolve } from 'path'
import { fileURLToPath } from 'url'
type TaxRow = readonly [number, number, number, number, number, number, number, number, number]
interface ParsedTable {
tableNumber: number
rows: TaxRow[]
}
function parseArgs(): { year: number } {
const args = process.argv.slice(2)
const yearIdx = args.indexOf('--year')
if (yearIdx === -1 || !args[yearIdx + 1]) {
throw new Error('Missing --year argument')
}
const year = parseInt(args[yearIdx + 1], 10)
if (!Number.isInteger(year) || year < 2000 || year > 2100) {
throw new Error(`Invalid year: ${args[yearIdx + 1]}`)
}
return { year }
}
export function parseLine(line: string): { table: number; row: TaxRow } | null {
// Strip BOM if present on the first line
const clean = line.replace(/^\uFEFF/, '')
if (clean.length < 49) return null
const prefix = clean.slice(0, 5)
// B-rows carry absolute SEK amounts, %-rows carry percentages for incomes
// above the highest B-row bracket. Both are needed for correct withholding.
if (prefix[2] !== 'B' && prefix[2] !== '%') return null
// The day-count prefix must be "30" (monthly). Skatteverket also publishes
// two-week tables whose rows differ only in this prefix ("14B29" vs "30B29"):
// a two-week row must fail the import loudly, never merge silently into the
// monthly fallback data.
const dayCount = prefix.slice(0, 2)
if (dayCount === '14') {
throw new Error(
`Two-week table row (prefix "${prefix}") in monthly import: wrong source file? Line: ${clean}`
)
}
if (dayCount !== '30') return null
const isPercent = prefix[2] === '%'
const tableStr = prefix.slice(3, 5)
const table = parseInt(tableStr, 10)
if (!Number.isInteger(table)) return null
// Column values must be well-formed whole numbers. A malformed value falling
// back to 0 would bake 0 kr / 0 % withholding into the emitted fallback data,
// so fail the import instead.
const parseColumn = (start: number): number => {
const raw = clean.slice(start, start + 5).trim()
if (!/^\d+$/.test(raw)) {
throw new Error(`Malformed column value "${raw}" in line: ${clean}`)
}
return parseInt(raw, 10)
}
// Income boundaries get the same digits-only rule: parseInt would accept
// "100abc" and turn other garbage into 0, silently corrupting bracket
// ranges. A blank income_to is legal only on the open-ended top %-row
// (emitted as 0, mapped to the open-ended sentinel by the loader).
const parseIncome = (start: number, allowBlank: boolean): number => {
const raw = clean.slice(start, start + 7).trim()
if (raw === '') {
if (!allowBlank) throw new Error(`Missing income boundary in line: ${clean}`)
return 0
}
if (!/^\d+$/.test(raw)) {
throw new Error(`Malformed income boundary "${raw}" in line: ${clean}`)
}
return parseInt(raw, 10)
}
const incomeFrom = parseIncome(5, false)
const incomeTo = parseIncome(12, isPercent)
const c1 = parseColumn(19)
const c2 = parseColumn(24)
const c3 = parseColumn(29)
const c4 = parseColumn(34)
const c5 = parseColumn(39)
const c6 = parseColumn(44)
return {
table,
row: [incomeFrom, incomeTo, c1, c2, c3, c4, c5, c6, isPercent ? 1 : 0] as const,
}
}
function parseFile(path: string): ParsedTable[] {
const content = readFileSync(path, 'utf-8')
const byTable = new Map<number, TaxRow[]>()
for (const rawLine of content.split(/\r?\n/)) {
if (!rawLine.trim()) continue
const parsed = parseLine(rawLine)
if (!parsed) continue
const existing = byTable.get(parsed.table)
if (existing) {
existing.push(parsed.row)
} else {
byTable.set(parsed.table, [parsed.row])
}
}
const tables = Array.from(byTable.entries())
.map(([tableNumber, rows]) => ({
tableNumber,
rows: rows.sort((a, b) => a[0] - b[0]),
}))
.sort((a, b) => a.tableNumber - b.tableNumber)
return tables
}
function formatRow(row: TaxRow): string {
return `[${row.join(', ')}]`
}
function emitModule(year: number, tables: ParsedTable[]): string {
const totalRows = tables.reduce((sum, t) => sum + t.rows.length, 0)
const tableNumbers = tables.map(t => t.tableNumber).join(', ')
const entries = tables
.map(t => {
const rows = t.rows.map(formatRow).join(',\n ')
return ` ${t.tableNumber}: [\n ${rows},\n ]`
})
.join(',\n')
return `/**
* AUTO-GENERATED: do not edit by hand.
*
* Source: data/tax-tables/${year}/allmanna-tabeller-manad.txt (Skatteverket SKV 434)
* Generator: scripts/import-tax-tables.ts
*
* Emergency fallback for lib/salary/tax-tables.ts when the Skatteverket
* open-data API is unreachable. Do not use as the primary source: the API
* is authoritative.
*
* Rows: ${totalRows} across tables ${tableNumbers}
*/
/**
* [incomeFrom, incomeTo, col1, col2, col3, col4, col5, col6, isPercent]
*
* isPercent 0: columns are SEK amounts (incomes up to 80 000 kr/month).
* isPercent 1: columns are percent of the whole monthly income (above
* 80 000 kr/month). incomeTo 0 marks the open-ended top row.
*/
export type FallbackTaxRow = readonly [
number, number, number, number, number, number, number, number, number,
]
/** Tables keyed by municipal tax rate number (29-42). */
export type FallbackTaxYear = Readonly<Record<number, readonly FallbackTaxRow[]>>
export const FALLBACK_TAX_TABLES_${year}: FallbackTaxYear = {
${entries},
}
export const FALLBACK_TAX_TABLES: Readonly<Record<number, FallbackTaxYear>> = {
${year}: FALLBACK_TAX_TABLES_${year},
}
export const FALLBACK_TAX_TABLE_YEARS: ReadonlySet<number> = new Set([${year}])
`
}
function main() {
const { year } = parseArgs()
const inputPath = resolve(process.cwd(), `data/tax-tables/${year}/allmanna-tabeller-manad.txt`)
const outputPath = resolve(process.cwd(), 'lib/salary/tax-tables-fallback.ts')
console.log(`Reading ${inputPath}`)
const tables = parseFile(inputPath)
if (tables.length === 0) {
throw new Error('No rows parsed: check input file format')
}
// Every table must have both sections: a B-only table would clamp high
// incomes to the last krona bracket and silently under-withhold.
for (const t of tables) {
const percentRows = t.rows.filter(r => r[8] === 1).length
if (percentRows === 0 || percentRows === t.rows.length) {
throw new Error(`Table ${t.tableNumber}: expected both B-rows and %-rows, got ${percentRows}/${t.rows.length} percent rows`)
}
}
const totalRows = tables.reduce((sum, t) => sum + t.rows.length, 0)
const percentTotal = tables.reduce((sum, t) => sum + t.rows.filter(r => r[8] === 1).length, 0)
console.log(`Parsed ${tables.length} tables (${tables.map(t => t.tableNumber).join(', ')}), ${totalRows} rows total (${percentTotal} percent rows)`)
const moduleSource = emitModule(year, tables)
writeFileSync(outputPath, moduleSource, 'utf-8')
console.log(`Wrote ${outputPath} (${moduleSource.length.toLocaleString()} bytes)`)
}
// Run only when executed directly (npx tsx scripts/import-tax-tables.ts), not
// when parseLine is imported by tests. Same pattern as generate-crontabs.ts.
if (process.argv[1] && fileURLToPath(import.meta.url) === resolve(process.argv[1])) {
main()
}
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env node
/**
* Stamps public/sw.js from public/sw.template.js with the runtime brand name.
*
* The service worker is served as a static file (Next.js does not bundle
* public/), so NEXT_PUBLIC_* inlining doesn't reach it. We generate sw.js
* from a template at build time (Vercel/local) so the deployed file shows
* the configured brand name.
*
* Docker uses a different strategy: the builder stage exports
* NEXT_PUBLIC_BRANDING_APP_NAME=__NEXT_PUBLIC_BRANDING_APP_NAME__ so the
* placeholder survives the build, and docker-entrypoint.sh substitutes the
* runtime value via sed at container start.
*
* public/sw.js is gitignored: the source of truth is the template.
*/
import { readFileSync, writeFileSync, existsSync } from 'node:fs'
import { join } from 'node:path'
const TEMPLATE_PATH = join(process.cwd(), 'public', 'sw.template.js')
const OUTPUT_PATH = join(process.cwd(), 'public', 'sw.js')
const PLACEHOLDER = '__NEXT_PUBLIC_BRANDING_APP_NAME__'
const value = process.env.NEXT_PUBLIC_BRANDING_APP_NAME || 'Gnubok'
if (!existsSync(TEMPLATE_PATH)) {
console.log(`[inject-public-branding] ${TEMPLATE_PATH} not found, skipping`)
process.exit(0)
}
const template = readFileSync(TEMPLATE_PATH, 'utf8')
const output = template.split(PLACEHOLDER).join(value)
writeFileSync(OUTPUT_PATH, output)
console.log(`[inject-public-branding] generated public/sw.js with brand "${value}"`)
+166
View File
@@ -0,0 +1,166 @@
/**
* READ-ONLY inspection: shows what Jakob's account looks like in prod and
* whether kontrollera can be run safely without seeding any test data.
*
* Verifies:
* 1. The user exists and active_company_id is set
* 2. The active company has a usable org_number + entity_type
* → so formatRedovisare() can produce the 12-digit SKV redovisare
* 3. There's at least one closed fiscal period with VAT-relevant journal
* entries we could pick for the kontrollera call
* 4. There are no leftover skatteverket_tokens that would surprise us
*
* Writes nothing. Safe to run against prod.
*
* Usage: npx tsx scripts/inspect-skv-readiness.ts <EMAIL>
*/
import { createClient } from '@supabase/supabase-js'
import { config } from 'dotenv'
import { resolve } from 'node:path'
config({ path: resolve(process.cwd(), '.env.local') })
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL
const SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!SUPABASE_URL || !SERVICE_KEY) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY')
process.exit(1)
}
const supabase = createClient(SUPABASE_URL, SERVICE_KEY, {
auth: { persistSession: false },
})
const email = process.argv[2]
if (!email) {
console.error('Usage: npx tsx scripts/inspect-skv-readiness.ts <EMAIL>')
process.exit(1)
}
function formatRedovisareLocal(orgNumber: string, entityType: string): string {
const digits = orgNumber.replace(/[-\s]/g, '')
if (digits.length === 12) return digits
if (digits.length !== 10) return `(invalid: ${orgNumber})`
if (entityType === 'aktiebolag') return `16${digits}`
// EF: prefix century. For births 19XX vs 20XX we'd need the actual logic.
const centuryByte = digits.substring(0, 2)
const yearByte = parseInt(centuryByte, 10)
// Heuristic mirrors lib/skatteverket/format.ts
return yearByte < 50 ? `20${digits}` : `19${digits}`
}
async function main() {
console.log(`Inspecting SKV readiness for ${email}\n`)
// 1. user_id
const { data: usersData, error: userErr } = await supabase.auth.admin.listUsers({ page: 1, perPage: 200 })
if (userErr) throw new Error(`listUsers: ${userErr.message}`)
const user = usersData.users.find(u => u.email === email)
if (!user) {
console.error(`User ${email} not found.`)
process.exit(1)
}
console.log(`✓ user_id = ${user.id}`)
// 2. user_preferences
const { data: prefs } = await supabase
.from('user_preferences')
.select('active_company_id')
.eq('user_id', user.id)
.maybeSingle()
console.log(` active_company_id = ${prefs?.active_company_id ?? '(none)'}`)
// 3. company memberships
const { data: memberships, error: memErr } = await supabase
.from('company_members')
.select('company_id, role, companies(id, name, org_number, entity_type, archived_at)')
.eq('user_id', user.id)
if (memErr) throw new Error(`memberships: ${memErr.message}`)
if (!memberships?.length) {
console.error('User has no company memberships. Sign up flow may be incomplete.')
process.exit(1)
}
console.log(`\nCompanies:`)
for (const m of memberships) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const c = (Array.isArray(m.companies) ? m.companies[0] : m.companies) as any
if (!c) continue
const archived = c.archived_at ? ' [ARCHIVED]' : ''
const active = c.id === prefs?.active_company_id ? ' ← ACTIVE' : ''
const redovisare = c.org_number ? formatRedovisareLocal(c.org_number, c.entity_type) : '(no org_number)'
console.log(` ${c.id} ${c.name}${archived}${active}`)
console.log(` role=${m.role} org=${c.org_number} entity=${c.entity_type} → redovisare=${redovisare}`)
}
const activeCompany = memberships.find(m => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const c = (Array.isArray(m.companies) ? m.companies[0] : m.companies) as any
return c?.id === prefs?.active_company_id
})
if (!activeCompany) {
console.warn('\n⚠ No active company set. UI will pick the first one.')
process.exit(0)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ac = (Array.isArray(activeCompany.companies) ? activeCompany.companies[0] : activeCompany.companies) as any
console.log(`\n--- Active company: ${ac.name} ---`)
// 4. Fiscal periods with closed status
const { data: fps } = await supabase
.from('fiscal_periods')
.select('id, name, period_start, period_end, is_closed')
.eq('company_id', ac.id)
.order('period_start', { ascending: false })
.limit(5)
console.log(`\nMost recent fiscal periods:`)
if (!fps?.length) {
console.log(' (none)')
} else {
for (const fp of fps) {
console.log(` ${fp.period_start} → ${fp.period_end} closed=${fp.is_closed} ${fp.name}`)
}
}
// 5. Count VAT-relevant journal_entry_lines for the most recent month
const today = new Date()
const lastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1)
const lastMonthEnd = new Date(today.getFullYear(), today.getMonth(), 0)
const startStr = `${lastMonth.getFullYear()}-${String(lastMonth.getMonth() + 1).padStart(2, '0')}-01`
const endStr = `${lastMonthEnd.getFullYear()}-${String(lastMonthEnd.getMonth() + 1).padStart(2, '0')}-${String(lastMonthEnd.getDate()).padStart(2, '0')}`
const vatAccounts = ['2611', '2621', '2631', '2614', '2641', '2645', '3001', '3002', '3003', '3308', '3105']
const { data: lines, error: lineErr } = await supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount, journal_entries!inner(company_id, entry_date, status)')
.in('account_number', vatAccounts)
.eq('journal_entries.company_id', ac.id)
.eq('journal_entries.status', 'posted')
.gte('journal_entries.entry_date', startStr)
.lte('journal_entries.entry_date', endStr)
.limit(200)
if (lineErr) throw new Error(`lines: ${lineErr.message}`)
console.log(`\nVAT activity in ${startStr} → ${endStr}: ${lines?.length ?? 0} lines on ${vatAccounts.join('/')}`)
// 6. Existing skatteverket_tokens?
const { data: tokens } = await supabase
.from('skatteverket_tokens')
.select('user_id, expires_at, refresh_count, scope, created_at')
.eq('user_id', user.id)
if (tokens?.length) {
console.log(`\n⚠ Existing skatteverket_tokens row(s):`)
for (const t of tokens) {
console.log(` expires_at=${t.expires_at} refresh_count=${t.refresh_count} scope=${t.scope}`)
}
} else {
console.log('\n✓ No existing skatteverket_tokens: clean slate for OAuth.')
}
console.log('\nDone (read-only).')
}
main().catch(err => {
console.error(err)
process.exit(1)
})
+51
View File
@@ -0,0 +1,51 @@
/**
* READ-ONLY: inspect the actual schema of public.skatteverket_tokens in prod.
* Confirms whether the UNIQUE(user_id) constraint exists and under what name.
*/
import { createClient } from '@supabase/supabase-js'
import { config } from 'dotenv'
import { resolve } from 'node:path'
config({ path: resolve(process.cwd(), '.env.local') })
const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY!,
{ auth: { persistSession: false } },
)
async function main() {
// Try a row-count query to see if the table exists at all
const { count, error: countErr } = await supabase
.from('skatteverket_tokens')
.select('*', { count: 'exact', head: true })
console.log('Table reachable:', !countErr, 'row count:', count, 'error:', countErr?.message ?? 'none')
// Use the postgrest schema endpoint to introspect via the OpenAPI spec
const { data: openapiResp, error: openapiErr } = await supabase
.from('skatteverket_tokens')
.select('id, user_id, expires_at, refresh_count, scope')
.limit(1)
console.log('Sample select error:', openapiErr?.message ?? 'none', ', rows:', openapiResp?.length ?? 0)
// Fetch from pg_constraint via a dedicated RPC if available, else via raw query
// Supabase JS doesn't expose raw SQL, so we use a workaround: try to provoke
// the constraint name from the upsert error itself with a dummy row.
console.log('\nProbing existing rows to count duplicates per user_id…')
const { data: rows, error: rowsErr } = await supabase
.from('skatteverket_tokens')
.select('id, user_id, created_at')
.order('created_at', { ascending: false })
if (rowsErr) {
console.error('rows fetch failed:', rowsErr.message)
return
}
const byUser = new Map<string, number>()
for (const r of rows ?? []) byUser.set(r.user_id, (byUser.get(r.user_id) ?? 0) + 1)
console.log(` ${rows?.length ?? 0} total rows across ${byUser.size} distinct user_ids`)
const dupes = [...byUser.entries()].filter(([, n]) => n > 1)
if (dupes.length) console.log(' duplicates:', dupes)
}
main().catch(err => { console.error(err); process.exit(1) })
+147
View File
@@ -0,0 +1,147 @@
#!/usr/bin/env npx tsx
/**
* Issue a connector key for a self-hosted instance (manual sales, v1).
*
* Writes a connector_keys row through the service role and prints the key
* ONCE together with the .env lines the operator pastes into their instance.
* The key is never stored: only its SHA-256.
*
* Usage:
* npx tsx scripts/issue-connector-key.ts --org 5561234567 --name "Byrå AB" \
* --instance https://bokforing.byra.se [--months 12] \
* [--scopes bank_sync,skatteverket,org_lookup,migration,peppol] \
* [--peppol-participants 5561234567,7350000000000] [--notes "..."] --confirm
*
* --peppol-participants lists the participant identifiers (org numbers, GLNs)
* the licensee may register and send as through Arcim's Peppol access point;
* the --org number is always allowed. Only issue the peppol scope where the
* Qvalia brokering terms permit it.
*
* Reads .env.local (which points at PRODUCTION in this repo: the script
* refuses to write without --confirm and prints the target host first).
*/
import { config } from 'dotenv'
import { resolve } from 'node:path'
config({ path: resolve(process.cwd(), '.env.local') })
import { createServiceRoleClient } from '../lib/supabase/service-client'
import { CONNECTOR_CAPABILITIES } from '../lib/entitlements/keys'
import { generateConnectorKey } from '../lib/connect/hosted/keys'
function arg(name: string): string | undefined {
const idx = process.argv.indexOf(`--${name}`)
if (idx === -1) return undefined
const value = process.argv[idx + 1]
return value && !value.startsWith('--') ? value : ''
}
function flag(name: string): boolean {
return process.argv.includes(`--${name}`)
}
async function main(): Promise<void> {
const org = (arg('org') ?? '').replace(/\D/g, '')
const name = arg('name') ?? ''
const instance = arg('instance') ?? ''
const months = Number(arg('months') ?? '12')
const scopes = (arg('scopes') ?? 'bank_sync,skatteverket').split(',').map((s) => s.trim()).filter(Boolean)
const bankPerCompany = Number(arg('bank-connections-per-company') ?? '1')
const skvPerCompany = Number(arg('skv-connections-per-company') ?? '1')
const peppolPerCompany = Number(arg('peppol-connections-per-company') ?? '1')
const peppolParticipants = (arg('peppol-participants') ?? '')
.split(',')
.map((v) => v.replace(/\s/g, ''))
.filter(Boolean)
const syncMinInterval = Number(arg('sync-min-interval') ?? '0')
const notes = arg('notes') ?? null
const problems: string[] = []
if (!/^\d{10}$/.test(org)) problems.push('--org must be a 10-digit Swedish organisation number')
if (!name) problems.push('--name is required (licensee name)')
try {
const u = new URL(instance)
if (u.protocol !== 'https:') problems.push('--instance must be an https:// origin')
} catch {
problems.push('--instance must be a valid https:// URL')
}
if (!Number.isInteger(months) || months <= 0 || months > 120) problems.push('--months must be an integer 1..120')
const unknown = scopes.filter((s) => !(CONNECTOR_CAPABILITIES as readonly string[]).includes(s))
if (unknown.length) problems.push(`unknown scopes: ${unknown.join(', ')} (allowed: ${CONNECTOR_CAPABILITIES.join(', ')})`)
for (const [name, v] of [
['bank-connections-per-company', bankPerCompany],
['skv-connections-per-company', skvPerCompany],
['peppol-connections-per-company', peppolPerCompany],
] as const) {
if (!Number.isFinite(v) || v < 0 || v > 100) problems.push(`--${name} must be 0..100`)
}
const badParticipants = peppolParticipants.filter((v) => !/^[0-9]{6,20}$/.test(v))
if (badParticipants.length) problems.push(`--peppol-participants must be digit-only identifiers: ${badParticipants.join(', ')}`)
if (!Number.isFinite(syncMinInterval) || syncMinInterval < 0) problems.push('--sync-min-interval must be >= 0 seconds')
if (problems.length) {
console.error(problems.map((p) => ` x ${p}`).join('\n'))
process.exit(2)
}
const url = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!url || !serviceKey) {
console.error('NEXT_PUBLIC_SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are required (.env.local)')
process.exit(2)
}
const periodEnd = new Date()
periodEnd.setUTCMonth(periodEnd.getUTCMonth() + months)
console.log(`Target: ${new URL(url).host}`)
console.log(`Licensee: ${name} (${org})`)
console.log(`Instance: ${new URL(instance).origin}`)
console.log(`Scopes: ${scopes.join(', ')}`)
console.log(`Limits: ${bankPerCompany} bank + ${skvPerCompany} SKV + ${peppolPerCompany} Peppol connection(s)/company, min sync interval ${syncMinInterval}s`)
console.log(`Peppol: ${peppolParticipants.length ? peppolParticipants.join(', ') : '(own org number only)'}`)
console.log(`Period: until ${periodEnd.toISOString().slice(0, 10)} (${months} months)`)
if (!flag('confirm')) {
console.log('\nDry run. Re-run with --confirm to issue the key.')
return
}
const { key, hash, prefix } = generateConnectorKey()
const supabase = createServiceRoleClient(url, serviceKey)
const { data, error } = await supabase
.from('connector_keys')
.insert({
key_hash: hash,
key_prefix: prefix,
org_number: org,
licensee_name: name,
instance_url: new URL(instance).origin,
scopes,
status: 'active',
current_period_end: periodEnd.toISOString(),
limits: {
bank_connections_per_company: Math.floor(bankPerCompany),
skv_connections_per_company: Math.floor(skvPerCompany),
peppol_connections_per_company: Math.floor(peppolPerCompany),
sync_min_interval_s: Math.floor(syncMinInterval),
},
peppol_participants: peppolParticipants,
notes,
})
.select('id')
.single()
if (error || !data) {
console.error('insert failed:', error?.message)
process.exit(1)
}
console.log(`\nIssued connector key ${prefix}… (id ${(data as { id: string }).id}). Shown ONCE, not stored:\n`)
console.log(` ${key}\n`)
console.log('Paste into the instance .env, then restart the app and cron containers:')
console.log(` GNUBOK_CONNECTOR_KEY=${key}`)
console.log(' # GNUBOK_CONNECT_URL=https://app.gnubok.se (default)')
console.log('\nThe hourly connector sync writes the capability grants; run it once by hand to check:')
console.log(' curl -sf -H "Authorization: Bearer $CRON_SECRET" http://localhost:3000/api/connector/sync/cron')
}
main().catch((err) => {
console.error(err instanceof Error ? err.message : String(err))
process.exit(1)
})
+378
View File
@@ -0,0 +1,378 @@
/**
* Shared atom discovery + SKILL.md frontmatter parsing.
*
* Used by both:
* - scripts/seed-agent-atom-registry.ts (dev/manual: writes the registry directly)
* - scripts/generate-skill-bodies.ts (production: emits a seed migration)
*
* Keeping discovery in one place means the two paths can never drift on which
* skills count as atoms, how titles/tokens are derived, or how frontmatter is read.
*
* Tiers discovered (the curated set: swarm-* and other Claude-Code-only skills
* are intentionally NOT matched here, so they never become atoms):
* horizontal: `.claude/skills/swedish-*\/SKILL.md` (regulatory)
* vertical : `.claude/skills/industry/<slug>\/SKILL.md` (industry)
* modifier : `.claude/skills/modifier/<slug>\/SKILL.md` (cross-cutting)
*/
import { readdir, readFile, stat } from 'node:fs/promises'
import { join, relative, sep } from 'node:path'
export type Tier = 'horizontal' | 'vertical' | 'modifier'
export interface DiscoveredAtom {
/** Stable id shaped as "<tier>/<slug>" (e.g. "horizontal/swedish-vat"). */
id: string
tier: Tier
slug: string
title: string
description: string
sni_prefixes: string[]
trigger_signals: Record<string, unknown>
/**
* Token estimate over the SKILL.md content ONLY: the unit actually loaded
* into the system prompt / returned by gnubok_load_skill. (We deliberately do
* NOT count references/*.md, which are not read at runtime.)
*/
estimated_tokens: number
/**
* Repo-relative path to the body source: SKILL.md for top-level skills,
* the references/*.md file for reference children (provenance + dev-fallback).
*/
body_path: string
/**
* Body inlined into the DB. For a top-level skill: the raw SKILL.md content
* (frontmatter included) with a "Loadable references" footer appended when the
* skill has any. For a reference child: the raw references/*.md content.
*/
body: string
/**
* NULL for a top-level skill; the parent skill's id for a reference child.
* Reference rows are hidden from every catalog (the metadata index, the MCP
* skill list, the composer atom index, the settings panel) by a
* `parent_atom_id IS NULL` filter: they reach the model only via an explicit
* gnubok_load_skill(<child id>) call after the parent SKILL.md is loaded.
*/
parent_atom_id: string | null
/** Version declared in frontmatter, or 1. The generator may override this. */
frontmatter_version: number
schema_version: number
}
// Normalize CRLF → LF so frontmatter parsing and body inlining are
// platform-independent (Windows checkouts ship .md files with CRLF unless
// .gitattributes forces LF, which it doesn't for *.md).
function normalizeLineEndings(text: string): string {
return text.replace(/\r\n/g, '\n')
}
// ── Frontmatter parsing ────────────────────────────────────────────────
// SKILL.md files use YAML frontmatter with `name`, `description`, and optionally
// `tier`, `sni_prefixes`, `trigger_signals`, `estimated_tokens`, `version`. We
// parse only the keys we care about: js-yaml is not in deps.
interface Frontmatter {
raw: string
name?: string
title?: string
description?: string
tier?: Tier
sniPrefixes?: string[]
triggerSignals?: Record<string, unknown>
estimatedTokens?: number
version?: number
}
function extractFrontmatter(content: string): Frontmatter | null {
const match = content.match(/^---\n([\s\S]*?)\n---/)
if (!match) return null
const raw = match[1]
return {
raw,
name: parseScalar(raw, 'name'),
title: parseScalar(raw, 'title'),
description: parseScalar(raw, 'description'),
tier: parseScalar(raw, 'tier') as Tier | undefined,
sniPrefixes: parseArray(raw, 'sni_prefixes'),
triggerSignals: parseInlineObject(raw, 'trigger_signals'),
estimatedTokens: parseNumber(raw, 'estimated_tokens'),
version: parseNumber(raw, 'version'),
}
}
// Handles `key: value`, `key: "quoted"`, `key: >`+folded, `key: |`+literal.
function parseScalar(yaml: string, key: string): string | undefined {
const inline = new RegExp(`^${escapeKey(key)}:\\s*(.*)$`, 'm').exec(yaml)
if (!inline) return undefined
const head = inline[1].trim()
if (head === '>' || head === '|' || head === '>-' || head === '|-') {
const after = yaml.slice(inline.index + inline[0].length).split('\n')
const lines: string[] = []
for (const line of after) {
if (line.length === 0) continue
if (/^\s/.test(line)) {
lines.push(line.trim())
} else {
break
}
}
return head.startsWith('>') ? lines.join(' ') : lines.join('\n')
}
return unquote(head)
}
function parseNumber(yaml: string, key: string): number | undefined {
const v = parseScalar(yaml, key)
if (v == null) return undefined
const n = Number(v)
return Number.isFinite(n) ? n : undefined
}
function parseArray(yaml: string, key: string): string[] | undefined {
const inline = new RegExp(`^${escapeKey(key)}:\\s*\\[(.*)\\]\\s*$`, 'm').exec(yaml)
if (inline) {
return inline[1]
.split(',')
.map((s) => unquote(s.trim()))
.filter(Boolean)
}
return undefined
}
function parseInlineObject(yaml: string, key: string): Record<string, unknown> | undefined {
// POC: only recognize `trigger_signals: {}` or absent. Deep parsing is deferred.
const line = new RegExp(`^${escapeKey(key)}:\\s*\\{\\s*\\}\\s*$`, 'm').exec(yaml)
if (line) return {}
return undefined
}
function escapeKey(key: string): string {
return key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
function unquote(s: string): string {
if ((s.startsWith('"') && s.endsWith('"')) || (s.startsWith("'") && s.endsWith("'"))) {
return s.slice(1, -1)
}
return s
}
// ── Token estimation ──────────────────────────────────────────────────
// Chars/4 baseline (Anthropic guidance for English). Swedish text inflates on
// Opus 4.7's tokenizer: re-measure post-POC.
export function estimateTokens(text: string): number {
return Math.ceil(text.length / 4)
}
// ── Title derivation ──────────────────────────────────────────────────
export function deriveTitle(slug: string): string {
// 'swedish-vat' → 'Swedish VAT'; 'swedish-year-end-closing' → 'Swedish Year-End Closing'
return slug
.split('-')
.map((w) => (w === 'vat' || w === 'sru' || w === 'sie' ? w.toUpperCase() : capitalize(w)))
.join(' ')
}
function capitalize(s: string): string {
return s.length === 0 ? s : s[0].toUpperCase() + s.slice(1)
}
// ── Discovery ─────────────────────────────────────────────────────────
/**
* Scan `<rootDir>/.claude/skills/` and return one DiscoveredAtom per skill,
* sorted by id for deterministic output. Skills without frontmatter or without
* a description are skipped (with a warning).
*/
export async function discoverAtoms(rootDir: string): Promise<DiscoveredAtom[]> {
const skillsDir = join(rootDir, '.claude', 'skills')
const rows: DiscoveredAtom[] = []
const entries = await readdir(skillsDir, { withFileTypes: true })
for (const entry of entries) {
if (!entry.isDirectory()) continue
// Horizontal: top-level swedish-* directory
if (entry.name.startsWith('swedish-')) {
rows.push(...(await readAtom(rootDir, 'horizontal', entry.name, join(skillsDir, entry.name))))
continue
}
// Vertical / modifier: subdirectories under those names
if (entry.name === 'industry' || entry.name === 'modifier') {
const tier: Tier = entry.name === 'industry' ? 'vertical' : 'modifier'
const tierDir = join(skillsDir, entry.name)
const subs = await readdir(tierDir, { withFileTypes: true })
for (const sub of subs) {
if (!sub.isDirectory()) continue
rows.push(...(await readAtom(rootDir, tier, sub.name, join(tierDir, sub.name))))
}
}
}
rows.sort((a, b) => a.id.localeCompare(b.id))
return rows
}
/**
* Read one skill directory into a top-level atom plus one child atom per
* references/*.md file. Returns an empty array if the SKILL.md is missing or
* lacks the frontmatter we require.
*/
async function readAtom(
rootDir: string,
tier: Tier,
slug: string,
dir: string
): Promise<DiscoveredAtom[]> {
const skillPath = join(dir, 'SKILL.md')
try {
await stat(skillPath)
} catch {
return []
}
const content = normalizeLineEndings(await readFile(skillPath, 'utf8'))
const fm = extractFrontmatter(content)
if (!fm) {
console.warn(` skipped ${relative(rootDir, skillPath)}: no frontmatter`)
return []
}
if (!fm.description) {
console.warn(` skipped ${relative(rootDir, skillPath)}: missing description`)
return []
}
const parentId = `${tier}/${slug}`
const childTier: Tier = fm.tier ?? tier
const refs = await readReferenceFiles(dir)
// Bridge the SKILL.md router (which points at dead `references/*.md` paths at
// runtime) to the loadable child ids the model can actually call. Appended to
// the parent body so it ships in the seeded DB body, visible only once the
// skill itself is loaded.
const body = refs.length > 0 ? content + buildReferencesFooter(parentId, refs) : content
const parent: DiscoveredAtom = {
id: parentId,
tier: childTier,
slug,
title: fm.title ?? deriveTitle(slug),
description: fm.description,
sni_prefixes: fm.sniPrefixes ?? [],
trigger_signals: fm.triggerSignals ?? {},
// Estimate over the loaded unit (SKILL.md + footer), not the whole
// directory: references are loaded separately and budgeted on their own row.
estimated_tokens: fm.estimatedTokens ?? estimateTokens(body),
body_path: relative(rootDir, skillPath),
body,
parent_atom_id: null,
frontmatter_version: fm.version ?? 1,
schema_version: 1,
}
const children: DiscoveredAtom[] = refs.map((r) => ({
id: `${parentId}/${r.slug}`,
tier: childTier,
slug: `${slug}/${r.slug}`,
title: r.descriptor || deriveTitle(r.slug),
description: r.descriptor
? `${r.descriptor}: reference for ${parent.title}`
: `Reference for ${parent.title}`,
sni_prefixes: [],
trigger_signals: {},
estimated_tokens: estimateTokens(r.body),
body_path: relative(rootDir, r.absPath),
body: r.body,
parent_atom_id: parentId,
frontmatter_version: 1,
schema_version: 1,
}))
return [parent, ...children]
}
// ── Reference discovery ───────────────────────────────────────────────
// A skill's deep material lives in <skillDir>/references/**.md. Each file
// becomes a hidden child atom loadable by id; the bytes never count toward the
// parent's token budget and never appear in any catalog listing.
interface ReferenceFile {
/** Absolute path on disk (provenance + dev-fallback anchor). */
absPath: string
/** Path as the SKILL.md router writes it, e.g. "references/bfl-bfnar.md". */
relPath: string
/** Child-id suffix derived from the path, e.g. "bfl-bfnar". */
slug: string
/** Raw file content: the child's DB-inlined body. */
body: string
/** First ATX heading (or first line), used as a human-readable label. */
descriptor: string
}
async function readReferenceFiles(skillDir: string): Promise<ReferenceFile[]> {
const refsDir = join(skillDir, 'references')
try {
await stat(refsDir)
} catch {
return []
}
const files = (await walkMarkdown(refsDir)).sort()
const out: ReferenceFile[] = []
for (const absPath of files) {
const body = normalizeLineEndings(await readFile(absPath, 'utf8'))
const relFromRefs = relative(refsDir, absPath).split(sep).join('/')
out.push({
absPath,
relPath: `references/${relFromRefs}`,
slug: relFromRefs.replace(/\.md$/i, '').replace(/\//g, '-'),
body,
descriptor: firstHeadingOrLine(body),
})
}
return out
}
async function walkMarkdown(dir: string): Promise<string[]> {
const out: string[] = []
const entries = await readdir(dir, { withFileTypes: true })
for (const e of entries) {
const p = join(dir, e.name)
if (e.isDirectory()) out.push(...(await walkMarkdown(p)))
else if (e.isFile() && e.name.toLowerCase().endsWith('.md')) out.push(p)
}
return out
}
function firstHeadingOrLine(md: string): string {
const lines = md.split('\n')
for (const line of lines) {
const h = /^#{1,3}\s+(.+?)\s*#*$/.exec(line.trim())
if (h) return h[1].trim()
}
for (const line of lines) {
const t = line.trim()
if (t.length > 0) return t.slice(0, 100)
}
return ''
}
function buildReferencesFooter(parentId: string, refs: ReferenceFile[]): string {
const lines = [
'',
'---',
'',
'## Loadable references',
'',
'The reference files named above are NOT included in this body. When a question genuinely needs that depth, load the specific one on demand with `gnubok_load_skill`, and only then:',
'',
]
for (const r of refs) {
const label = r.descriptor ? `: ${r.descriptor}` : ''
lines.push(`- \`${r.relPath}\` → \`gnubok_load_skill("${parentId}/${r.slug}")\`${label}`)
}
return '\n' + lines.join('\n') + '\n'
}
+45
View File
@@ -0,0 +1,45 @@
/**
* List recent gnubok dev users. Quick sanity-check tool: given a hosted
* Supabase project, the easiest way to see who has signed up.
*
* Usage: npx tsx scripts/list-dev-users.ts
*/
import { createClient } from '@supabase/supabase-js'
import { config } from 'dotenv'
import { resolve } from 'node:path'
config({ path: resolve(process.cwd(), '.env.local') })
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL
const SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!SUPABASE_URL || !SERVICE_KEY) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(SUPABASE_URL, SERVICE_KEY, {
auth: { persistSession: false },
})
async function main() {
const { data, error } = await supabase.auth.admin.listUsers({ page: 1, perPage: 20 })
if (error) {
console.error('listUsers failed:', error.message)
process.exit(1)
}
if (!data.users.length) {
console.log('No users yet. Sign up at /register first.')
return
}
console.log(`Found ${data.users.length} users (most recent first):`)
for (const u of data.users) {
const last = u.last_sign_in_at ? new Date(u.last_sign_in_at).toISOString() : '(never)'
console.log(` ${u.id} ${u.email?.padEnd(40) ?? '(no email)'} last: ${last}`)
}
}
main().catch(err => {
console.error(err)
process.exit(1)
})
+104
View File
@@ -0,0 +1,104 @@
/**
* Read-only lister for #2058: employees whose jämkningsbeslut is stored but
* can never be applied because jamkning_valid_to is null.
*
* WHY: until #2058 the v1 API and MCP accepted jamkning_percentage +
* jamkning_valid_from without jamkning_valid_to. isJamkningValid in
* lib/salary/calculation-engine.ts applies a beslut only when BOTH dates are
* set, so these rows fall back to the tax table on every payslip and AGI
* while the stored beslut says otherwise. Every write path now rejects the
* shape; this script finds the rows that were stored before that.
*
* WHAT IT DOES: lists the rows per company with employee id, percentage and
* start date. No names or other PII are selected: the employee page shows the
* name once you open the id. It writes NOTHING. The repair is a per-company
* decision (set an end date, or clear the beslut): either changes the next
* payslip, so it is made by a human through the employee page or the API,
* not by this script.
*
* Usage:
* npx tsx scripts/list-incomplete-jamkning.ts
*
* Reads NEXT_PUBLIC_SUPABASE_URL + SUPABASE_SERVICE_ROLE_KEY from .env.local.
* Treat .env.local as pointing at PRODUCTION (read-only here, still: be sure
* which project you are looking at).
*/
import { createClient } from '@supabase/supabase-js'
import { config as dotenv } from 'dotenv'
import { resolve } from 'node:path'
dotenv({ path: resolve(process.cwd(), '.env.local') })
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL
const SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!SUPABASE_URL || !SERVICE_KEY) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(SUPABASE_URL, SERVICE_KEY, { auth: { persistSession: false } })
interface Row {
id: string
company_id: string
is_active: boolean
jamkning_percentage: number
jamkning_valid_from: string | null
jamkning_valid_to: string | null
}
async function main() {
const PAGE = 1000
const rows: Row[] = []
for (let from = 0; ; from += PAGE) {
const { data, error } = await supabase
.from('employees')
.select('id, company_id, is_active, jamkning_percentage, jamkning_valid_from, jamkning_valid_to')
.not('jamkning_percentage', 'is', null)
.is('jamkning_valid_to', null)
.order('company_id')
.order('id')
.range(from, from + PAGE - 1)
if (error) {
console.error('employees query failed:', error.message)
process.exit(1)
}
rows.push(...((data ?? []) as Row[]))
if (!data || data.length < PAGE) break
}
if (rows.length === 0) {
console.log('No employees with a jämkning percentage but no valid_to. Nothing to decide.')
return
}
const companyIds = [...new Set(rows.map((r) => r.company_id))]
const { data: companies, error: companiesError } = await supabase
.from('companies')
.select('id, name')
.in('id', companyIds)
if (companiesError) {
console.error('companies query failed:', companiesError.message)
process.exit(1)
}
const companyName = new Map((companies ?? []).map((c) => [c.id as string, c.name as string]))
console.log(`${rows.length} employee row(s) across ${companyIds.length} company(ies) with an inert jämkningsbeslut:\n`)
for (const companyId of companyIds) {
console.log(`${companyName.get(companyId) ?? '(unknown company)'} ${companyId}`)
for (const r of rows.filter((x) => x.company_id === companyId)) {
const state = r.is_active ? 'active ' : 'inactive'
const from = r.jamkning_valid_from ?? '(no start date)'
console.log(` ${r.id} ${state} ${r.jamkning_percentage} % from ${from} to (null)`)
}
console.log('')
}
console.log('Decide per company: set jamkning_valid_to (a beslut normally runs to 31 December of')
console.log('the from-year) or clear jamkning_percentage. Either changes the next payslip; the')
console.log('engine ignores these rows until then. Inactive employees can usually be cleared.')
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+94
View File
@@ -0,0 +1,94 @@
#!/usr/bin/env node
/**
* Stdio-to-HTTP bridge for Claude Desktop.
* Reads JSON-RPC from stdin, POSTs to the gnubok MCP endpoint, writes response to stdout.
*
* Usage in claude_desktop_config.json:
* {
* "mcpServers": {
* "gnubok": {
* "command": "node",
* "args": ["/path/to/erp-base/scripts/mcp-bridge.mjs"],
* "env": {
* "GNUBOK_API_KEY": "gnubok_sk_...",
* "GNUBOK_URL": "http://localhost:3000/api/extensions/ext/mcp-server/mcp"
* }
* }
* }
* }
*/
const API_KEY = process.env.GNUBOK_API_KEY
const URL = process.env.GNUBOK_URL || 'http://localhost:3000/api/extensions/ext/mcp-server/mcp'
if (!API_KEY) {
process.stderr.write('Error: GNUBOK_API_KEY environment variable is required\n')
process.exit(1)
}
let buffer = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', (chunk) => {
buffer += chunk
// JSON-RPC messages are newline-delimited
let newlineIdx
while ((newlineIdx = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, newlineIdx).trim()
buffer = buffer.slice(newlineIdx + 1)
if (!line) continue
handleMessage(line).catch((err) => {
process.stderr.write(`Bridge error: ${err.message}\n`)
})
}
})
process.stdin.on('end', () => {
process.exit(0)
})
async function handleMessage(line) {
let parsed
try {
parsed = JSON.parse(line)
} catch {
process.stderr.write(`Invalid JSON: ${line}\n`)
return
}
// Notifications (no id) don't expect a response, but still forward them
const isNotification = parsed.id === undefined || parsed.id === null
try {
const res = await fetch(URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
body: line,
})
if (res.status === 204) {
// No content (e.g. notifications/initialized): nothing to write back
return
}
const text = await res.text()
if (text) {
process.stdout.write(text + '\n')
}
} catch (err) {
if (!isNotification) {
const errorResponse = JSON.stringify({
jsonrpc: '2.0',
id: parsed.id,
error: { code: -32000, message: `Bridge error: ${err.message}` },
})
process.stdout.write(errorResponse + '\n')
}
}
}
@@ -0,0 +1,143 @@
#!/usr/bin/env npx tsx
/**
* One-off migration: re-home SIE archive files from {user_id}/ paths into
* {company_id}/ paths so they match the new sie_files_* storage policies
* (see supabase/migrations/20260416120000_sie_files_and_fiscal_period_sync.sql).
*
* Pre multi-tenant refactor (commit 1534979) SIE archives were uploaded to
* {user_id}/{import_id}.se. After the refactor the upload path switched to
* {company_id}/{import_id}.se, but the production storage policies weren't
* updated: so every post-refactor archive silently failed RLS. The new
* policies scope access by company; legacy files still live under user_id
* prefixes and would become unreadable for anyone except the original
* uploader. This script moves them to the canonical company_id prefix.
*
* Idempotent: skips rows whose file_storage_path already starts with the
* company_id, and skips the copy step if the target object already exists.
*
* Usage:
* npx tsx scripts/migrate-sie-files-to-company-paths.ts --dry-run
* npx tsx scripts/migrate-sie-files-to-company-paths.ts
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const dryRun = process.argv.includes('--dry-run')
const supabase = createClient(supabaseUrl, serviceRoleKey)
interface ImportRow {
id: string
company_id: string
file_storage_path: string
}
async function objectExists(path: string): Promise<boolean> {
const { data } = await supabase.storage.from('sie-files').list(
path.split('/').slice(0, -1).join('/'),
{ search: path.split('/').pop() }
)
return !!data && data.length > 0
}
async function main() {
const { data: rows, error } = await supabase
.from('sie_imports')
.select('id, company_id, file_storage_path')
.not('file_storage_path', 'is', null)
if (error) {
console.error('Failed to query sie_imports:', error.message)
process.exit(1)
}
const candidates: ImportRow[] = (rows ?? []).filter(
(r): r is ImportRow =>
!!r.file_storage_path &&
!r.file_storage_path.startsWith(`${r.company_id}/`)
)
console.log(`${rows?.length ?? 0} archived imports total; ${candidates.length} need migration.`)
if (dryRun) console.log('(dry run: no changes will be made)')
let migrated = 0
let skipped = 0
let failed = 0
for (const row of candidates) {
const oldPath = row.file_storage_path
const newPath = `${row.company_id}/${row.id}.se`
console.log(`\n${row.id}`)
console.log(` old: ${oldPath}`)
console.log(` new: ${newPath}`)
if (dryRun) continue
const { data: downloaded, error: dlError } = await supabase.storage
.from('sie-files')
.download(oldPath)
if (dlError || !downloaded) {
console.error(` download failed: ${dlError?.message ?? 'no data'}`)
failed++
continue
}
if (await objectExists(newPath)) {
console.log(` target already exists: just updating DB row`)
} else {
const { error: upError } = await supabase.storage
.from('sie-files')
.upload(newPath, downloaded, {
upsert: false,
contentType: 'text/plain',
})
if (upError) {
console.error(` upload failed: ${upError.message}`)
failed++
continue
}
}
const { error: updateError } = await supabase
.from('sie_imports')
.update({ file_storage_path: newPath })
.eq('id', row.id)
if (updateError) {
console.error(` DB update failed: ${updateError.message}`)
failed++
continue
}
const { error: rmError } = await supabase.storage
.from('sie-files')
.remove([oldPath])
if (rmError) {
console.warn(` old file not removed (DB already points at new path): ${rmError.message}`)
skipped++
}
migrated++
console.log(` migrated`)
}
console.log(`\nDone: ${migrated} migrated, ${skipped} partial, ${failed} failed.`)
if (failed > 0) process.exit(1)
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+137
View File
@@ -0,0 +1,137 @@
# Parties, phase 0: measure before building
Phase 0 of the Kontakter plan (design doc: the "Vem du gör affärer med"
artifact, 2026-09). Nothing in this phase changes product behaviour. It makes
the resolver measurable so the model steps in later phases can be judged
against labelled data instead of opinions.
## What ships in this phase
- Migration `20260902120000`: enables `pg_trgm` (trigram blocking of
counterparty keys) and drops the two context-graph tables whose feature code
was never merged and which prod no longer has. Test:
`tests/pg/parties-phase0.pg.test.ts`.
- `draw-golden-set.sql`: the reproducible draw of the labelling sample and the
payee-change base rate. Read-only.
## The golden set (never in git)
The draw returns customer voucher text, which can contain person names. The
repository is public, so the drawn rows and the labels live in
`dev_docs/parties/golden/` (gitignored). Only aggregates and this vocabulary
are versioned.
Label vocabulary for the pre-classifier, one per key:
| label | meaning | example |
|---|---|---|
| `party` | a real counterpart the company pays or invoices | `Levfakt Beijer Byggmaterial AB (2089)`, `Loopia` |
| `category` | an expense description with no counterpart in it | `Inköp av varor`, `Banktjänster`, `Fika` |
| `payroll` | salary, benefits, expense claims to a person | `Löneutbetalning: 2024.M.01 - anställd: 5` |
| `adjustment` | periodisering, kostnadsföring, omföring, lagerförändring | `Periodisering av leverantörsfaktura 1311` |
| `authority` | Skatteverket, Bolagsverket, Transportstyrelsen, kommun | `Bolagsverket ändra bolagsordning` |
| `bank` | bank fees and bank products | `Baspaket Bank`, `Avgift Amex` |
| `intermediary` | payment rail or marketplace, not the counterpart | `Klarna`, `Stripe`, `Zettle`, `AmazonMktplc` |
| `unsure` | cannot tell from the text alone | |
A key whose text names a vendor but is booked as a category still gets
`party`: the resolver decides identity, the account comes from the ledger.
Edge rules settled with the founder on 2026-09-02, after the first labelling
round:
- State fees and taxes where the counterpart is fixed (Bolagsverket
registration fees, Skatteverket, Transportstyrelsen vehicle tax) are
`authority`. A kommun or a state company that invoices for a service is a
`party` like any supplier.
- A marketplace or payment processor that the company actually pays (Amazon
Marketplace purchases, Stripe or Amex fees) is a `party`. `intermediary` is
reserved for a rail that only carries someone else's money: Klarna, Swish
and Zettle payouts, "försäljning via" lines.
- A card-platform line that carries an employee's name (Pleo, Google Ads with
the cardholder appended) is a `party`; the name is the cardholder, not the
counterpart.
- Insurance and pension premiums are `party`: the insurer is the counterpart,
whatever the 74xx account says.
## Measured on prod, 2026-09-02
- 113,032 imported expense vouchers across 385 real companies; 99.5% yield a
key; 71% belong to a key that repeats within the company.
- Payee-identity base rate: 365 suppliers have an established bankgiro on two
or more documents; 13 of them (3.6%) carry two established bankgiro numbers,
with an average span of 29 days between them, which reads as parallel
accounts or OCR variance rather than changes over time. Any distinct value
at all: 8.4%. Consequence for the signal: a new bankgiro is rare enough to
warrant a "verify" sentence, never a block, and a value must be seen on two
documents before it counts as known.
## Pre-classifier shadow evaluation, 2026-09-02
`eval-preclassifier.ts` scores three routers on the same 180 held-out keys
(20 rows, chosen by md5 of the key, serve as few-shot examples and are never
scored). Rows the founder marked `unsure` always receive a label, so the
honest number is the one that excludes them.
| Router | Strict agreement | Excluding founder-unsure | Party TPR | Party TNR |
|---|---|---|---|---|
| Rules v0, lexicon from BAS account names | 0.917 | 0.965 | 0.992 | 0.930 |
| Rules v0, lexicon incl. BAS descriptions | 0.911 | 0.959 | 0.984 | 0.930 |
| Sonnet 5 on Bedrock EU, zero-shot | 0.861 | 0.906 | 0.906 | 0.977 |
| Sonnet 5 on Bedrock EU, 20 founder examples | 0.878 | 0.924 | 0.930 | 0.977 |
Read with two caveats. The rules were written after the labels were seen,
so their score is optimistic; the model scores are honest. And most of the
remaining disagreements are definitions, not errors: whether a Bolagsverket
fee is `authority` or `intermediary`, whether a marketplace or payment
processor (Amazon Marketplace, Stripe, Amex fees) is `party` or
`intermediary`, whether a card-platform line with a person's name (Pleo,
Google Ads) is `party` or `payroll`, and whether a pension insurance premium
is `party` or `payroll`. Settle those in the vocabulary above, relabel the
handful of rows, and re-run.
BAS description tokens in the lexicon make Google and Facebook look generic
because the descriptions name them as examples; the account-name lexicon is
the default for that reason. The model's remaining party misses are text
with a person's name or a card descriptor, exactly where a hard key from a
document would decide instead.
## Selection-step evaluation against document-anchored truth, 2026-09-02
`eval-selection.ts` scores the selection step (which similar keys in the same
company are the same party) without human labels: every key in the set has an
org number read by OCR from an invoice linked to its voucher, so two keys are
the same party exactly when the org numbers agree. Keys mapping to several
org numbers are excluded. 225 anchors, 1,038 candidate pairs (747 same, 291
different), 44 anchors with no true match. Draw: `draw-selection-gold.sql`.
The model selector is opt-in (`--llm`): it sends the same voucher text the
production categorizer already sends to the configured AI provider (Bedrock EU
on hosted), so run it only with an env file whose AI settings you have checked.
| Selector | Pair precision | Pair recall | Non-match recall | Anchors fully right | "None" precision / recall |
|---|---|---|---|---|---|
| Rules (same core name) | 0.909 | 0.776 | 0.801 | 0.547 | 0.40 / 0.86 |
| Sonnet 5 on Bedrock EU, zero-shot | 0.910 | 0.904 | 0.770 | 0.756 | 0.68 / 0.77 |
What the false merges are: both selectors merge "Levfakt Fortnox (325)" with
"Levfakt Fortnox (154)", "Vattenfall Kundservice" with "Vattenfall
Eldistribution", "Jämtkraft" with "Jämtkraft" under another org number. Those
are different legal entities sharing a trade name, or OCR variance in the org
number. Text cannot separate them and should not try: the hard key from the
document is the referee, which is exactly the resolver's precedence order.
What the model catches that the rules miss: "lån themax" versus "lån från
themax", and paraphrases with a changed word order.
Read with one caveat: sales-side vouchers ("kundbet ...") contribute noisy
truth because the org number on the linked document can be the customer's;
the next draw restricts to expense-side vouchers.
Cost: about 1,300 input tokens per anchor, 298k tokens for the run.
## Still to confirm in this phase
- SCB access: the free Företagsregistret API carries F-skattstatus,
Momsstatus and Arbetsgivarstatus (verified on scb.se 2026-09-02); access
requested by email, credentials pending.
- The pre-classifier and selection agreement bars (0.85 on the labelled set)
are enforced by the shadow harness in phase 1, not here.
+77
View File
@@ -0,0 +1,77 @@
-- Parties, phase 0: draw the golden set for the counterparty resolver.
--
-- Read-only. Run against prod through the Supabase MCP (execute_sql) or psql.
-- The OUTPUT contains customer voucher text, which can include person names
-- (salary vouchers, expense claims). It must never be committed to this
-- public repository: write it to dev_docs/parties/golden/ (gitignored) and
-- label it there. Only aggregate results and the labelling vocabulary belong
-- in the repo.
--
-- Scope: posted SIE-imported vouchers (source_type = 'import') with at least
-- one expense debit line (4xxx-7xxx), sandbox tenants excluded. Keys are the
-- shipped normalize_counterparty_key() so the sample matches what the
-- resolver will see. Three strata so the set is not dominated by one tenant:
-- fleet = key seen in 5+ companies (60 rows)
-- heavy = 20+ vouchers in fewer companies (60 rows)
-- tail = everything else with 3+ vouchers (80 rows)
-- Ordering by md5(key) makes the draw deterministic and re-runnable.
-- 1. Keys for the pre-classifier (party / category / payroll / adjustment /
-- authority / bank / intermediary / unsure).
WITH real_co AS (SELECT id FROM companies WHERE name NOT ILIKE '%sandl%'),
scope AS (
SELECT je.id, je.company_id, je.description, public.normalize_counterparty_key(je.description) AS k,
(SELECT l.account_number FROM journal_entry_lines l
WHERE l.journal_entry_id = je.id AND l.account_number ~ '^[4-7][0-9]{3}$'
ORDER BY l.debit_amount DESC LIMIT 1) AS acct,
(SELECT sum(l.debit_amount) FROM journal_entry_lines l
WHERE l.journal_entry_id = je.id AND l.account_number ~ '^[4-7][0-9]{3}$') AS sek
FROM journal_entries je JOIN real_co c ON c.id = je.company_id
WHERE je.status = 'posted' AND je.source_type = 'import'
AND EXISTS (SELECT 1 FROM journal_entry_lines l
WHERE l.journal_entry_id = je.id AND l.account_number ~ '^[4-7][0-9]{3}$' AND l.debit_amount > 0)
),
keys AS (
SELECT k, count(*) AS n, count(DISTINCT company_id) AS cos,
mode() WITHIN GROUP (ORDER BY description) AS example,
mode() WITHIN GROUP (ORDER BY acct) AS acct, round(sum(sek)) AS sek,
(array_agg(DISTINCT left(description, 60)))[1:3] AS variants
FROM scope WHERE k <> '' GROUP BY k HAVING count(*) >= 3
),
strata AS (
SELECT *,
CASE WHEN cos >= 5 THEN 'fleet' WHEN n >= 20 THEN 'heavy' ELSE 'tail' END AS stratum,
row_number() OVER (PARTITION BY (CASE WHEN cos >= 5 THEN 'fleet' WHEN n >= 20 THEN 'heavy' ELSE 'tail' END)
ORDER BY md5(k)) AS rn
FROM keys
)
SELECT stratum, k, example, n, cos, acct, sek, variants
FROM strata
WHERE (stratum = 'fleet' AND rn <= 60) OR (stratum = 'heavy' AND rn <= 60) OR (stratum = 'tail' AND rn <= 80)
ORDER BY stratum, rn;
-- 2. Base rate of supplier bankgiro changes in our own documents, to set the
-- severity of the payee-identity signal. "Established" = a bankgiro seen on
-- at least two documents for the same org number; a single differing value
-- is treated as OCR noise or a one-off account.
WITH real_co AS (SELECT id FROM companies WHERE name NOT ILIKE '%sandl%'),
docs AS (
SELECT d.company_id,
regexp_replace(coalesce(d.extracted_data->'supplier'->>'orgNumber',''), '[^0-9]', '', 'g') AS org,
regexp_replace(coalesce(d.extracted_data->'supplier'->>'bankgiro',''), '[^0-9]', '', 'g') AS bg,
d.created_at::date AS seen
FROM document_attachments d JOIN real_co c ON c.id = d.company_id
WHERE length(regexp_replace(coalesce(d.extracted_data->'supplier'->>'bankgiro',''), '[^0-9]', '', 'g')) BETWEEN 7 AND 8
AND length(regexp_replace(coalesce(d.extracted_data->'supplier'->>'orgNumber',''), '[^0-9]', '', 'g')) = 10
),
bg_counts AS (SELECT company_id, org, bg, count(*) AS n, min(seen) AS first_seen, max(seen) AS last_seen FROM docs GROUP BY 1,2,3),
established AS (SELECT * FROM bg_counts WHERE n >= 2),
per_supplier AS (
SELECT company_id, org, count(*) AS established_bgs, sum(n) AS docs, max(last_seen) - min(first_seen) AS span_days
FROM established GROUP BY 1,2
)
SELECT count(*) AS suppliers_with_established_bg,
count(*) FILTER (WHERE established_bgs >= 2) AS suppliers_with_2_established_bgs,
round(100.0 * count(*) FILTER (WHERE established_bgs >= 2) / nullif(count(*),0), 1) AS pct,
round(avg(span_days) FILTER (WHERE established_bgs >= 2)) AS avg_span_days_when_changed
FROM per_supplier;
+44
View File
@@ -0,0 +1,44 @@
-- Parties, phase 0/1: draw the document-anchored ground truth for the
-- SELECTION step. Read-only. Output contains customer voucher text: keep it
-- in dev_docs/parties/golden/, never in this public repository.
--
-- Truth without a human: a key whose voucher is linked to a document with an
-- OCR-read supplier org number is a known party; two keys in the same company
-- are the same party exactly when their org numbers agree. Keys that map to
-- several org numbers are dropped as ambiguous; documents whose "supplier"
-- org number is the company's own are dropped as the company's sales side.
WITH real_co AS (SELECT id, regexp_replace(coalesce(org_number,''), '[^0-9]', '', 'g') AS own_org FROM companies WHERE name NOT ILIKE '%sandl%'),
linked AS (
SELECT d.company_id, je.description,
regexp_replace(coalesce(d.extracted_data->'supplier'->>'orgNumber',''), '[^0-9]', '', 'g') AS org,
public.normalize_counterparty_key(je.description) AS k,
(SELECT l.account_number FROM journal_entry_lines l WHERE l.journal_entry_id = je.id AND l.account_number ~ '^[4-7][0-9]{3}$' ORDER BY l.debit_amount DESC LIMIT 1) AS acct
FROM document_attachments d
JOIN real_co c ON c.id = d.company_id
JOIN journal_entries je ON je.id = d.journal_entry_id AND je.status = 'posted'
WHERE d.extracted_data->'supplier'->>'orgNumber' IS NOT NULL
),
keys AS (
SELECT l.company_id, l.org, l.k, count(*) AS n, mode() WITHIN GROUP (ORDER BY l.description) AS example, mode() WITHIN GROUP (ORDER BY l.acct) AS acct,
btrim(regexp_replace(regexp_replace(l.k, '^(levfakt|levfkt|lev\.?fakt\.?|leverantörsfaktura från|leverantörsfaktura|levbet\.?|kvitto|faktura|utgift|inköp)\s+', '', 'g'), '\m\d+\M', '', 'g')) AS core
FROM linked l JOIN real_co c ON c.id = l.company_id
WHERE length(l.org) = 10 AND l.k <> '' AND l.org <> c.own_org
GROUP BY l.company_id, l.org, l.k
),
uniq AS (SELECT company_id, k, min(org) AS org, sum(n) AS n, min(example) AS example, min(acct) AS acct, min(core) AS core FROM keys GROUP BY company_id, k HAVING count(DISTINCT org) = 1),
co_ok AS (SELECT company_id FROM uniq GROUP BY 1 HAVING count(DISTINCT org) >= 2 AND count(*) >= 6),
anchors AS (SELECT u.*, row_number() OVER (ORDER BY md5(u.company_id::text || u.k)) AS rn FROM uniq u JOIN co_ok USING (company_id) WHERE length(u.core) >= 4),
sample AS (SELECT * FROM anchors WHERE rn <= 320),
cands AS (
SELECT s.rn AS anchor_rn, o.k, o.example, o.n, o.acct, o.org, extensions.similarity(s.core, o.core) AS sim,
row_number() OVER (PARTITION BY s.rn ORDER BY extensions.similarity(s.core, o.core) DESC, o.n DESC) AS cr
FROM sample s JOIN uniq o ON o.company_id = s.company_id AND o.k <> s.k
WHERE extensions.similarity(s.core, o.core) >= 0.25
),
agg AS (
SELECT s.rn, s.k AS anchor_k, s.example AS anchor_example, s.n AS anchor_n, s.acct AS anchor_acct, s.org AS anchor_org,
(SELECT json_agg(json_build_object('k', c.k, 'example', c.example, 'n', c.n, 'acct', c.acct, 'org', c.org, 'sim', round(c.sim::numeric, 2)) ORDER BY c.sim DESC)
FROM cands c WHERE c.anchor_rn = s.rn AND c.cr <= 6) AS candidates
FROM sample s
)
SELECT * FROM agg WHERE candidates IS NOT NULL ORDER BY rn LIMIT 300;
+276
View File
@@ -0,0 +1,276 @@
/**
* Parties, phase 0: shadow evaluation of the key pre-classifier against the
* founder-labelled golden set.
*
* WHAT: every counterparty key from the books must be routed before entity
* resolution: a real party, a category text, payroll, an adjustment, an
* authority, a bank product, or an intermediary. This script scores two
* candidate routers against the founder's labels on the same held-out rows:
* 1. a deterministic rule set (prefixes, lexicon, dominant account), and
* 2. the model behind getAiService().generateStructured, zero-shot and with
* twenty founder examples in the prompt.
* Agreement is reported as strict label match plus, for the routing decision
* that matters (party vs not), true-positive and true-negative rates, because
* failures are rare and raw agreement flatters.
*
* SAFETY: read-only. Reads a gitignored JSONL, calls the AI service for the
* model variants, writes a JSON report next to the input. It never opens a
* database connection. The golden rows contain customer voucher text; keep
* the report in dev_docs as well.
*
* Usage:
* npx tsx scripts/parties/eval-preclassifier.ts \
* --golden dev_docs/parties/golden/golden-2026-09-02.jsonl \
* --env .env.local [--out <report.json>] [--no-llm] [--batch 25]
*/
import { createHash } from 'node:crypto'
import { readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { config as dotenv } from 'dotenv'
import { z } from 'zod'
const LABELS = ['party', 'category', 'payroll', 'adjustment', 'authority', 'bank', 'intermediary', 'unsure'] as const
type Label = (typeof LABELS)[number]
interface GoldenRow {
id: number
stratum: string
k: string
example: string
n: number
cos: number
acct: string | null
sek: number
label: Label
}
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
const flag = (name: string) => process.argv.includes(`--${name}`)
const goldenPath = resolve(arg('golden') ?? 'dev_docs/parties/golden/golden-2026-09-02.jsonl')
const envPath = resolve(arg('env') ?? '.env.local')
const outPath = resolve(arg('out') ?? goldenPath.replace(/\.jsonl$/, '') + '.eval.json')
const batchSize = Number(arg('batch') ?? 25)
const runLlm = !flag('no-llm')
dotenv({ path: envPath })
// ── Golden set ──────────────────────────────────────────────────────────────
const rows: GoldenRow[] = readFileSync(goldenPath, 'utf8')
.split('\n')
.filter(Boolean)
.map((line) => JSON.parse(line) as GoldenRow)
.filter((r) => LABELS.includes(r.label))
// Deterministic split: the 20 rows with the lowest md5(key) are few-shot
// examples; every classifier is scored on the remaining rows only.
const md5 = (s: string) => createHash('md5').update(s).digest('hex')
const ordered = [...rows].sort((a, b) => md5(a.k).localeCompare(md5(b.k)))
const examples = ordered.slice(0, 20)
const exampleIds = new Set(examples.map((r) => r.id))
const evalRows = rows.filter((r) => !exampleIds.has(r.id))
// ── Deterministic router ────────────────────────────────────────────────────
// The rules live in lib/parties/classify.ts so the product and this
// evaluation share one implementation. --vocab is kept for the report's
// history: 'names' is what the library does; 'full' is no longer supported.
import { classifyKey } from '@/lib/parties/classify'
import { BAS_REFERENCE } from '@/lib/bookkeeping/bas-reference'
export function ruleLabel(row: { k: string; acct: string | null }): Label {
return classifyKey({ key: row.k, acct: row.acct })
}
// ── Model router ────────────────────────────────────────────────────────────
const SYSTEM = `Du sorterar nycklar från svensk bokföring innan de går vidare till motpartsmatchning.
Varje nyckel är en normaliserad verifikationstext från importerade verifikat, med exempeltext, dominerande BAS-konto, antal verifikat och antal bolag.
Sätt exakt en etikett per nyckel:
- party: en riktig motpart som bolaget betalar eller fakturerar (leverantör, butik, tjänst, kommun som fakturerar). Prefix som "levfakt", "leverantörsfaktura från", "levbet" betyder alltid party.
- category: bara en kostnadstext utan motpart i sig ("inköp av varor", "banktjänster", "fika", "diesel", "hyra momspliktig").
- payroll: lön, förmån, utlägg eller ersättning till en person.
- adjustment: periodisering, kostnadsföring, omföring, lagerförändring, nedskrivning, rättelse, valutaomräkning.
- authority: myndighet som mottagare av en avgift eller skatt (Skatteverket, Transportstyrelsen).
- bank: bankavgifter och bankprodukter.
- intermediary: betalväg eller marknadsplats som inte är den egentliga motparten.
- unsure: går inte att avgöra från texten.
En nyckel som nämner en leverantör men bokförts på ett kategorikonto är ändå party: identiteten avgörs här, kontot kommer från bokföringen.
Svara med exakt de id som frågan innehåller, inga andra.`
const ResponseSchema = z.object({
labels: z.array(z.object({ id: z.number().int(), label: z.enum(LABELS) })),
})
const jsonSchema = {
type: 'object',
properties: {
labels: {
type: 'array',
items: {
type: 'object',
properties: { id: { type: 'integer' }, label: { type: 'string', enum: [...LABELS] } },
required: ['id', 'label'],
additionalProperties: false,
},
},
},
required: ['labels'],
additionalProperties: false,
}
function basName(acct: string | null): string {
if (!acct) return ''
const hit = BAS_REFERENCE.find((a) => a.account_number === acct)
return hit ? hit.account_name : ''
}
function describe(r: GoldenRow): string {
return `id ${r.id}: nyckel "${r.k}" | exempel "${r.example}" | konto ${r.acct ?? '?'} ${basName(r.acct)} | ${r.n} verifikat | ${r.cos} bolag`
}
async function modelLabels(
batch: GoldenRow[],
fewShot: GoldenRow[] | null,
): Promise<{ labels: Map<number, Label>; usage: unknown; model: string }> {
const { getAiService } = await import('@/lib/ai')
const service = getAiService()
const shots = fewShot
? `Så här har grundaren märkt tjugo andra nycklar; följ samma bedömning:\n${fewShot
.map((r) => `${describe(r)} => ${r.label}`)
.join('\n')}\n\n`
: ''
const prompt = `${shots}Märk följande ${batch.length} nycklar:\n${batch.map(describe).join('\n')}`
let lastError: unknown
for (let attempt = 0; attempt < 2; attempt++) {
try {
const result = await service.generateStructured({
tier: 'assistant',
system: SYSTEM,
prompt,
maxTokens: 4096,
schema: { name: 'key_labels', description: 'One label per key id', jsonSchema },
})
const parsed = ResponseSchema.parse(result.value)
const valid = new Set(batch.map((r) => r.id))
const labels = new Map<number, Label>()
for (const l of parsed.labels) if (valid.has(l.id) && !labels.has(l.id)) labels.set(l.id, l.label)
return { labels, usage: result.usage, model: result.model }
} catch (err) {
lastError = err
}
}
throw lastError
}
// ── Scoring ─────────────────────────────────────────────────────────────────
interface Score {
n: number
strict_agreement: number
agreement_excluding_founder_unsure: number
party_tpr: number
party_tnr: number
per_label: Record<string, { precision: number | null; recall: number | null; support: number }>
confusions: { id: number; k: string; founder: Label; predicted: Label | null }[]
}
function score(pred: Map<number, Label | null>, subset: GoldenRow[]): Score {
let strict = 0
const decided = subset.filter((r) => r.label !== 'unsure')
let strictDecided = 0
let tp = 0, fn = 0, tn = 0, fp = 0
const per: Record<string, { tp: number; fp: number; fn: number }> = {}
for (const l of LABELS) per[l] = { tp: 0, fp: 0, fn: 0 }
const confusions: Score['confusions'] = []
for (const r of subset) {
const p = pred.get(r.id) ?? null
if (p === r.label) strict++
else confusions.push({ id: r.id, k: r.k, founder: r.label, predicted: p })
if (p) {
if (p === r.label) per[p].tp++
else {
per[p].fp++
per[r.label].fn++
}
} else per[r.label].fn++
}
for (const r of decided) {
const p = pred.get(r.id) ?? null
if (p === r.label) strictDecided++
const isParty = r.label === 'party'
const predParty = p === 'party'
if (isParty && predParty) tp++
else if (isParty && !predParty) fn++
else if (!isParty && !predParty) tn++
else fp++
}
const r4 = (x: number) => Math.round(x * 10000) / 10000
const perLabel: Score['per_label'] = {}
for (const l of LABELS) {
const { tp: a, fp: b, fn: c } = per[l]
perLabel[l] = {
precision: a + b > 0 ? r4(a / (a + b)) : null,
recall: a + c > 0 ? r4(a / (a + c)) : null,
support: subset.filter((r) => r.label === l).length,
}
}
return {
n: subset.length,
strict_agreement: r4(strict / subset.length),
agreement_excluding_founder_unsure: r4(strictDecided / decided.length),
party_tpr: r4(tp / Math.max(1, tp + fn)),
party_tnr: r4(tn / Math.max(1, tn + fp)),
per_label: perLabel,
confusions,
}
}
// ── Main ────────────────────────────────────────────────────────────────────
async function main() {
console.log(`golden rows ${rows.length}, few-shot examples ${examples.length}, scored rows ${evalRows.length}`)
const rulePred = new Map<number, Label | null>(evalRows.map((r) => [r.id, ruleLabel(r)]))
const report: Record<string, unknown> = {
golden: goldenPath,
scored_rows: evalRows.length,
example_ids: [...exampleIds],
rules_v0: score(rulePred, evalRows),
}
if (runLlm) {
for (const variant of ['zero_shot', 'few_shot'] as const) {
const pred = new Map<number, Label | null>()
const usages: unknown[] = []
let model = ''
for (let i = 0; i < evalRows.length; i += batchSize) {
const batch = evalRows.slice(i, i + batchSize)
const res = await modelLabels(batch, variant === 'few_shot' ? examples : null)
for (const r of batch) pred.set(r.id, res.labels.get(r.id) ?? null)
usages.push(res.usage)
model = res.model
console.log(`${variant}: batch ${i / batchSize + 1} done (${res.labels.size}/${batch.length} labelled)`)
}
report[`model_${variant}`] = { model, usages, ...score(pred, evalRows) }
}
}
writeFileSync(outPath, JSON.stringify(report, null, 2))
const line = (name: string, s: Score) =>
`${name.padEnd(16)} strict ${s.strict_agreement} excl-unsure ${s.agreement_excluding_founder_unsure} party TPR ${s.party_tpr} TNR ${s.party_tnr} (n=${s.n})`
console.log(line('rules_v0', report.rules_v0 as Score))
if (runLlm) {
console.log(line('model_zero_shot', report.model_zero_shot as Score))
console.log(line('model_few_shot', report.model_few_shot as Score))
}
console.log(`report: ${outPath}`)
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
+226
View File
@@ -0,0 +1,226 @@
/**
* Parties, phase 0/1: shadow evaluation of the SELECTION step against
* document-anchored ground truth.
*
* WHAT: after blocking, the resolver must decide which of at most six
* similar keys in the same company are the same party as an anchor key, or
* none. Ground truth here needs no human: every key in the set carries an
* org number read by OCR from an invoice linked to its voucher, so two keys
* are the same party exactly when their org numbers agree. The draw is
* `draw-selection-gold.sql`; keys that map to several org numbers are
* excluded because they are ambiguous by construction.
*
* Two selectors are scored on the same anchors:
* 1. rules: a candidate is "same" when its core (AP prefix, supplier number
* and digit runs stripped) equals the anchor's core;
* 2. the model behind getAiService().generateStructured, asked to return the
* indices of candidates that are the same organisation, or none.
* Metrics are pair-level (same / different) precision and recall, plus the
* anchor-level "none" decision, because a resolver that merges where it
* should not is the failure mode the July design warned about.
*
* SAFETY: read-only. Reads a gitignored JSONL, writes a report next to the
* input. Never opens a database connection.
*
* DATA PROCESSING: the model selector is opt-in (--llm). It sends the voucher
* key text and account numbers in the gold file to getAiService(), which is
* the same configured provider the production categorizer already sends the
* same voucher text to (Claude on AWS Bedrock in the EU on hosted). Run it
* only with an env file whose AI configuration you have checked; without
* --llm the script scores the rules selector alone and makes no network call.
*
* Usage:
* npx tsx scripts/parties/eval-selection.ts \
* --gold dev_docs/parties/golden/selection-2026-09-02.jsonl --env .env.local \
* [--out <report.json>] [--llm] [--limit 300]
*/
import { readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { config as dotenv } from 'dotenv'
import { z } from 'zod'
interface Cand {
k: string
example: string
n: number
acct: string | null
org: string
sim: number
}
interface Anchor {
rn: number
anchor_k: string
anchor_example: string
anchor_n: number
anchor_acct: string | null
anchor_org: string
candidates: Cand[]
}
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
const flag = (name: string) => process.argv.includes(`--${name}`)
const goldPath = resolve(arg('gold') ?? 'dev_docs/parties/golden/selection-2026-09-02.jsonl')
const envPath = resolve(arg('env') ?? '.env.local')
const outPath = resolve(arg('out') ?? goldPath.replace(/\.jsonl$/, '') + '.eval.json')
const limit = Number(arg('limit') ?? 300)
const runLlm = flag('llm')
dotenv({ path: envPath })
const anchors: Anchor[] = readFileSync(goldPath, 'utf8')
.split('\n')
.filter(Boolean)
.map((l) => JSON.parse(l) as Anchor)
.slice(0, limit)
// ── Rules selector ──────────────────────────────────────────────────────────
const AP = /^(levfakt|levfkt|lev\.?fakt\.?|leverantörsfaktura från|leverantörsfaktura|levbet\.?|kvitto|faktura|utgift|inköp)\s+/
const SUFFIX = /\b(ab|aktiebolag|hb|kb|sverige|sweden|ltd|limited|oy|gmbh|inc|sarl|publ|filial)\b/g
export function core(k: string): string {
return k
.toLowerCase()
.replace(AP, '')
.replace(/\b\d+\b/g, '')
.replace(SUFFIX, '')
.replace(/[^a-zåäöé ]+/g, ' ')
.split(/\s+/)
.filter(Boolean)
.join(' ')
}
function rulesSelect(a: Anchor): number[] {
const ac = core(a.anchor_k)
if (!ac) return []
return a.candidates.map((c, i) => (core(c.k) === ac ? i : -1)).filter((i) => i >= 0)
}
// ── Model selector ──────────────────────────────────────────────────────────
const SYSTEM = `Du avgör identitet mellan motparter i svensk bokföring.
Du får ett ankare (en normaliserad verifikationstext från ett bolags bokföring) och upp till sex kandidater ur samma bolag som liknar det.
Svara med index för varje kandidat som är SAMMA organisation som ankaret, alltså samma juridiska person som skulle ha samma organisationsnummer. Olika bolag i samma koncern, franchisetagare med samma kedjenamn, eller ett företag och dess inkassobolag är INTE samma.
Leverantörsnummer inom parentes, fakturanummer och prefix som "levfakt" eller "leverantörsfaktura från" är brus, inte identitet.
Om ingen kandidat är samma organisation, svara med en tom lista. Gissa inte: hellre tom lista än en felaktig sammanslagning.`
const Out = z.object({ same: z.array(z.number().int().min(1).max(6)) })
const jsonSchema = {
type: 'object',
properties: { same: { type: 'array', items: { type: 'integer', minimum: 1, maximum: 6 } } },
required: ['same'],
additionalProperties: false,
}
async function modelSelect(a: Anchor): Promise<{ picks: number[]; usage: unknown; model: string }> {
const { getAiService } = await import('@/lib/ai')
const service = getAiService()
const prompt =
`Ankare: "${a.anchor_k}" (exempel "${a.anchor_example}", konto ${a.anchor_acct ?? '?'}, ${a.anchor_n} verifikat)\n` +
`Kandidater:\n` +
a.candidates
.map((c, i) => `[${i + 1}] "${c.k}" (exempel "${c.example}", konto ${c.acct ?? '?'}, ${c.n} verifikat, textlikhet ${c.sim})`)
.join('\n') +
`\nVilka kandidater är samma organisation som ankaret? Svara med index, eller tom lista.`
let lastErr: unknown
for (let attempt = 0; attempt < 2; attempt++) {
try {
const r = await service.generateStructured({
tier: 'assistant',
system: SYSTEM,
prompt,
maxTokens: 512,
schema: { name: 'same_party', description: 'Indices of candidates that are the same organisation', jsonSchema },
})
const parsed = Out.parse(r.value)
const picks = [...new Set(parsed.same.map((i) => i - 1).filter((i) => i >= 0 && i < a.candidates.length))]
return { picks, usage: r.usage, model: r.model }
} catch (e) {
lastErr = e
}
}
throw lastErr
}
// ── Scoring ─────────────────────────────────────────────────────────────────
interface Score {
anchors: number
pairs: number
pair_precision: number
pair_recall: number
pair_tnr: number
anchor_exact: number
none_precision: number
none_recall: number
false_merges: { anchor: string; candidate: string; sim: number }[]
missed: { anchor: string; candidate: string; sim: number }[]
}
function score(picksByAnchor: Map<number, number[]>): Score {
let tp = 0, fp = 0, fn = 0, tn = 0, exact = 0, pairs = 0
let noneTp = 0, noneFp = 0, noneFn = 0
const falseMerges: Score['false_merges'] = []
const missed: Score['missed'] = []
for (const a of anchors) {
const picks = new Set(picksByAnchor.get(a.rn) ?? [])
const truth = new Set(a.candidates.map((c, i) => (c.org === a.anchor_org ? i : -1)).filter((i) => i >= 0))
let ok = true
a.candidates.forEach((c, i) => {
pairs++
const p = picks.has(i), t = truth.has(i)
if (p && t) tp++
else if (p && !t) { fp++; falseMerges.push({ anchor: a.anchor_k, candidate: c.k, sim: c.sim }) }
else if (!p && t) { fn++; missed.push({ anchor: a.anchor_k, candidate: c.k, sim: c.sim }) }
else tn++
if (p !== t) ok = false
})
if (ok) exact++
const predNone = picks.size === 0, truthNone = truth.size === 0
if (predNone && truthNone) noneTp++
else if (predNone && !truthNone) noneFp++
else if (!predNone && truthNone) noneFn++
}
const r4 = (x: number) => Math.round(x * 10000) / 10000
return {
anchors: anchors.length,
pairs,
pair_precision: r4(tp / Math.max(1, tp + fp)),
pair_recall: r4(tp / Math.max(1, tp + fn)),
pair_tnr: r4(tn / Math.max(1, tn + fp)),
anchor_exact: r4(exact / anchors.length),
none_precision: r4(noneTp / Math.max(1, noneTp + noneFp)),
none_recall: r4(noneTp / Math.max(1, noneTp + noneFn)),
false_merges: falseMerges.slice(0, 40),
missed: missed.slice(0, 40),
}
}
async function main() {
const truthNone = anchors.filter((a) => !a.candidates.some((c) => c.org === a.anchor_org)).length
console.log(`anchors ${anchors.length}, pairs ${anchors.reduce((s, a) => s + a.candidates.length, 0)}, anchors with no true match ${truthNone}`)
const rules = new Map<number, number[]>(anchors.map((a) => [a.rn, rulesSelect(a)]))
const report: Record<string, unknown> = { gold: goldPath, rules_v0: score(rules) }
if (runLlm) {
const picks = new Map<number, number[]>()
const usages: unknown[] = []
let model = ''
let i = 0
for (const a of anchors) {
const r = await modelSelect(a)
picks.set(a.rn, r.picks)
usages.push(r.usage)
model = r.model
if (++i % 25 === 0) console.log(`model: ${i}/${anchors.length}`)
}
report.model_zero_shot = { model, usages, ...score(picks) }
}
writeFileSync(outPath, JSON.stringify(report, null, 2))
const line = (name: string, s: Score) =>
`${name.padEnd(16)} pair P ${s.pair_precision} R ${s.pair_recall} TNR ${s.pair_tnr} | anchor exact ${s.anchor_exact} | none P ${s.none_precision} R ${s.none_recall} (anchors ${s.anchors}, pairs ${s.pairs})`
console.log(line('rules_v0', report.rules_v0 as Score))
if (runLlm) console.log(line('model_zero_shot', report.model_zero_shot as Score))
console.log(`report: ${outPath}`)
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+131
View File
@@ -0,0 +1,131 @@
/**
* Operator tool for Peppol access (service role; .env.local points at prod).
*
* npx tsx --env-file=.env.local scripts/peppol/access.ts list
* Open requests first, then every company with a row (status, cap, sends used).
* npx tsx --env-file=.env.local scripts/peppol/access.ts enable <company_id|orgnr> [--max-sends N] [--receive] [--by you@accounted.se] [--note "..."]
* Grants sending (capped at N transmissions, omit for no cap) and, with
* --receive, the right to publish the company's identifier (one contracted
* tenant slot). Re-running adjusts the same row.
* npx tsx --env-file=.env.local scripts/peppol/access.ts disable <company_id|orgnr> [--note "..."]
* npx tsx --env-file=.env.local scripts/peppol/access.ts show <company_id|orgnr>
*
* Nothing here talks to Qvalia; it only flips what the product allows.
*/
import { createClient } from '@supabase/supabase-js'
import {
countPeppolSends,
getPeppolAccess,
setPeppolAccess,
} from '@/lib/invoices/peppol-access'
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY (run with --env-file=.env.local)')
process.exit(2)
}
const service = createClient(supabaseUrl, serviceRoleKey)
const [, , command = 'list', target, ...rest] = process.argv
function flag(name: string): string | null {
const index = rest.indexOf(`--${name}`)
if (index === -1) return null
return rest[index + 1] ?? ''
}
async function resolveCompanyId(input: string | undefined): Promise<string> {
if (!input) throw new Error('company id or organisation number required')
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(input)) return input
const digits = input.replace(/\D/g, '')
const { data, error } = await service
.from('company_settings')
.select('company_id, company_name, org_number')
.limit(2000)
if (error) throw error
const hits = ((data ?? []) as Array<{ company_id: string; company_name: string | null; org_number: string | null }>)
.filter((row) => (row.org_number ?? '').replace(/\D/g, '') === digits)
if (hits.length !== 1) throw new Error(`${hits.length} companies match org number ${input}`)
return hits[0].company_id
}
async function describe(companyId: string): Promise<string> {
const { data } = await service
.from('company_settings')
.select('company_name, org_number')
.eq('company_id', companyId)
.maybeSingle()
const row = data as { company_name: string | null; org_number: string | null } | null
return `${row?.company_name ?? '?'} (${row?.org_number ?? '?'}) ${companyId}`
}
async function main(): Promise<void> {
switch (command) {
case 'list': {
const { data, error } = await service
.from('peppol_access')
.select('*')
.order('status', { ascending: true })
.order('requested_at', { ascending: true })
if (error) throw error
const rows = (data ?? []) as Array<{ company_id: string; status: string; max_sends: number | null; receive_enabled: boolean; requested_at: string | null; enabled_at: string | null; request_note: string | null }>
if (rows.length === 0) { console.log('No Peppol access rows.'); return }
for (const row of rows) {
const sends = row.status === 'enabled' ? await countPeppolSends(service, row.company_id) : 0
console.log(
`${row.status.padEnd(9)} ${await describe(row.company_id)} sends ${sends}/${row.max_sends ?? '∞'} receive=${row.receive_enabled}`
+ (row.requested_at ? ` requested ${row.requested_at}` : '')
+ (row.request_note ? `\n note: ${row.request_note}` : ''),
)
}
return
}
case 'show': {
const companyId = await resolveCompanyId(target)
console.log(await describe(companyId))
console.log(JSON.stringify(await getPeppolAccess(service, companyId), null, 2))
console.log('sends used:', await countPeppolSends(service, companyId))
return
}
case 'enable': {
const companyId = await resolveCompanyId(target)
const maxSendsRaw = flag('max-sends')
const maxSends = maxSendsRaw === null ? undefined : (maxSendsRaw === '' || maxSendsRaw === 'none' ? null : Number.parseInt(maxSendsRaw, 10))
if (maxSends !== undefined && maxSends !== null && !Number.isFinite(maxSends)) throw new Error('--max-sends expects a number or "none"')
const row = await setPeppolAccess({
service,
companyId,
status: 'enabled',
maxSends,
receiveEnabled: rest.includes('--receive') ? true : (rest.includes('--no-receive') ? false : undefined),
by: flag('by') ?? `scripts/peppol/access.ts (${process.env.USER ?? 'operator'})`,
note: flag('note') ?? undefined,
})
console.log(`enabled: ${await describe(companyId)}`)
console.log(JSON.stringify(row, null, 2))
return
}
case 'disable': {
const companyId = await resolveCompanyId(target)
const row = await setPeppolAccess({
service,
companyId,
status: 'disabled',
by: flag('by') ?? `scripts/peppol/access.ts (${process.env.USER ?? 'operator'})`,
note: flag('note') ?? undefined,
})
console.log(`disabled: ${await describe(companyId)}`)
console.log(JSON.stringify(row, null, 2))
return
}
default:
throw new Error(`Unknown command "${command}". See the header comment.`)
}
}
main().catch((error: unknown) => {
console.error(error instanceof Error ? error.message : String(error))
process.exit(1)
})
+208
View File
@@ -0,0 +1,208 @@
/**
* Qvalia sandbox probe: the first live contact with the Access Point.
*
* Reads QVALIA_* from the environment (npx dotenv -e .env.local, or export)
* and runs one of:
*
* npx tsx scripts/peppol/qvalia-probe.ts auth
* Exchanges the API key for a JWT (GET /token/{partnerRegNo}) with both
* header schemes, so we learn which one this key accepts.
* npx tsx scripts/peppol/qvalia-probe.ts lookup 0007:5567321707
* Recipient lookup through the adapter (Qvalia's own id by default).
* npx tsx scripts/peppol/qvalia-probe.ts accounts
* Lists child accounts under the partner (tells us consolidated vs
* multi-tenant as Qvalia set it up).
* npx tsx scripts/peppol/qvalia-probe.ts peppol-ids
* Lists the Peppol identifiers registered on the partner account.
* npx tsx scripts/peppol/qvalia-probe.ts outgoing
* Lists the three latest outgoing invoice statuses (read-only).
* npx tsx scripts/peppol/qvalia-probe.ts incoming [integrationId]
* Lists the latest incoming invoice statuses, or prints one incoming
* invoice as XML (read-only: uses the non-marking endpoint).
* npx tsx scripts/peppol/qvalia-probe.ts send path/to/invoice.xml
* Submits a BIS Billing 3 XML through the adapter. Sandbox only: the
* script refuses a production base URL.
* npx tsx scripts/peppol/qvalia-probe.ts webhook
* Shows the partner webhook subscription (URL, event types, auth type).
* npx tsx scripts/peppol/qvalia-probe.ts webhook-configure https://host/api/webhooks/peppol/qvalia
* Creates or updates the partner webhook subscription for all three
* event types and attaches QVALIA_WEBHOOK_SECRET as the api_key header
* (QVALIA_WEBHOOK_HEADER) that our route verifies. Requires the secret
* in the environment; prints the webhook id.
*
* Nothing here touches Accounted's database. The API key is never printed.
*/
import { readFileSync } from 'node:fs'
import {
QVALIA_PRODUCTION_BASE_URL,
createQvaliaTransport,
readQvaliaConfigFromEnv,
} from '@/lib/invoices/transports/qvalia'
import {
PEPPOL_BIS_BILLING_INVOICE_DOCUMENT_TYPE_ID,
PEPPOL_BIS_BILLING_PROFILE_ID,
} from '@/lib/invoices/peppol-bis-billing'
import { sha256Hex } from '@/lib/invoices/peppol-delivery'
const [, , command = 'auth', argument] = process.argv
const config = readQvaliaConfigFromEnv()
if (!config) {
console.error('Set QVALIA_API_KEY, QVALIA_PARTNER_REG_NO and QVALIA_BASE_URL first (see .env.example).')
process.exit(2)
}
const partner = encodeURIComponent(config.partnerRegNo)
const account = encodeURIComponent(config.accountRegNo)
function headerFor(scheme: 'apikey' | 'raw'): string {
return scheme === 'raw' ? config!.apiKey : `ApiKey ${config!.apiKey}`
}
async function show(label: string, response: Response): Promise<unknown> {
const text = await response.text()
let body: unknown = text
try { body = JSON.parse(text) } catch { /* keep text */ }
console.log(`\n== ${label}: HTTP ${response.status}`)
const integrationId = response.headers.get('integrationid')
if (integrationId) console.log(`integrationid header: ${integrationId}`)
console.log(typeof body === 'string' ? body.slice(0, 2000) : JSON.stringify(body, null, 2).slice(0, 4000))
return body
}
async function rawGet(path: string, scheme: 'apikey' | 'raw' = config!.authScheme): Promise<Response> {
return fetch(`${config!.baseUrl}${path}`, {
headers: { Authorization: headerFor(scheme), accept: 'application/json' },
})
}
async function rawSend(method: 'PUT' | 'POST', path: string, body: unknown): Promise<Response> {
return fetch(`${config!.baseUrl}${path}`, {
method,
headers: {
Authorization: headerFor(config!.authScheme),
accept: 'application/json',
'content-type': 'application/json',
},
body: JSON.stringify(body),
})
}
async function main(): Promise<void> {
console.log(`Qvalia probe against ${config!.baseUrl} as partner ${config!.partnerRegNo} (account ${config!.accountRegNo})`)
switch (command) {
case 'auth': {
for (const scheme of ['apikey', 'raw'] as const) {
await show(`GET /token/{partnerRegNo} with Authorization scheme "${scheme}"`, await rawGet(`/token/${partner}`, scheme))
}
return
}
case 'accounts': {
await show('GET /partner/{p}/account', await rawGet(`/partner/${partner}/account?limit=25`))
return
}
case 'peppol-ids': {
await show('GET /partner/{p}/account/{a}/peppol', await rawGet(`/partner/${partner}/account/${account}/peppol`))
return
}
case 'outgoing': {
await show(
'GET /partner/{p}/transaction/{a}/invoices/outgoing/status',
await rawGet(`/partner/${partner}/transaction/${account}/invoices/outgoing/status?includeRead=true&limit=3`),
)
return
}
case 'incoming': {
if (argument) {
const response = await fetch(
`${config!.baseUrl}/partner/${partner}/transaction/${account}/invoices/incoming?integrationId=${encodeURIComponent(argument)}&includeRead=true&limit=1`,
{ headers: { Authorization: headerFor(config!.authScheme), accept: 'application/xml' } },
)
console.log(`HTTP ${response.status}`)
console.log((await response.text()).slice(0, 6000))
return
}
await show(
'GET /partner/{p}/transaction/{a}/invoices/incoming/status',
await rawGet(`/partner/${partner}/transaction/${account}/invoices/incoming/status?includeRead=true&limit=5`),
)
return
}
case 'webhook': {
await show('GET /partner/{p}/webhook/configure', await rawGet(`/partner/${partner}/webhook/configure`))
return
}
case 'webhook-configure': {
if (!argument || !/^https:\/\//.test(argument)) {
throw new Error('webhook-configure expects an https URL, e.g. https://app.accounted.se/api/webhooks/peppol/qvalia')
}
if (!config!.webhookSecret) throw new Error('Set QVALIA_WEBHOOK_SECRET first (openssl rand -hex 32)')
const configured = await show(
'PUT /partner/{p}/webhook/configure',
await rawSend('PUT', `/partner/${partner}/webhook/configure`, {
url: argument,
types: ['new_document', 'document_delivery', 'document_error'],
}),
)
const webhookId = configured && typeof configured === 'object' && 'id' in configured
? String((configured as { id: unknown }).id)
: null
if (!webhookId) throw new Error('Qvalia did not return a webhook id')
await show(
'POST /partner/{p}/webhook/{id}/auth (api_key header)',
await rawSend('POST', `/partner/${partner}/webhook/${encodeURIComponent(webhookId)}/auth`, {
type: 'api_key',
header: config!.webhookHeader,
value: config!.webhookSecret,
}),
)
return
}
case 'lookup': {
const peppolId = argument ?? '0007:5567321707'
const [scheme, identifier] = peppolId.split(':')
if (!scheme || !identifier) throw new Error('lookup expects scheme:identifier, e.g. 0007:5567321707')
const transport = createQvaliaTransport(config!)
console.log(JSON.stringify(await transport.lookupRecipient({ scheme, identifier }), null, 2))
return
}
case 'send': {
if (config!.baseUrl === QVALIA_PRODUCTION_BASE_URL) {
throw new Error('Refusing to send through the probe against production. Use the product flow.')
}
if (!argument) throw new Error('send expects a path to a BIS Billing 3 XML file')
const xml = readFileSync(argument, 'utf8')
const sender = /AccountingSupplierParty[\s\S]*?<cbc:EndpointID schemeID="(\d{4})">([^<]+)</.exec(xml)
const recipient = /AccountingCustomerParty[\s\S]*?<cbc:EndpointID schemeID="(\d{4})">([^<]+)</.exec(xml)
if (!sender || !recipient) throw new Error('Could not read EndpointID for seller and buyer from the XML')
const transport = createQvaliaTransport(config!)
const receipt = await transport.submit({
idempotencyKey: crypto.randomUUID(),
tenantReference: 'probe',
sender: { scheme: sender[1], identifier: sender[2] },
recipient: { scheme: recipient[1], identifier: recipient[2] },
documentTypeId: PEPPOL_BIS_BILLING_INVOICE_DOCUMENT_TYPE_ID,
processId: PEPPOL_BIS_BILLING_PROFILE_ID,
filename: argument.split('/').pop() ?? 'invoice.xml',
contentType: 'application/xml',
document: xml,
documentSha256: sha256Hex(xml),
})
console.log(JSON.stringify(receipt, null, 2))
console.log('\nEvidence after submit:')
console.log(JSON.stringify(await transport.retrieveEvidence(receipt.providerSubmissionId), null, 2).slice(0, 4000))
return
}
default:
throw new Error(`Unknown command "${command}". See the header comment for the list.`)
}
}
main().catch((error: unknown) => {
console.error(error instanceof Error ? `${error.name}: ${error.message}` : String(error))
if (error && typeof error === 'object' && 'detail' in error) {
console.error('detail:', (error as { detail?: unknown }).detail)
}
process.exit(1)
})
+69
View File
@@ -0,0 +1,69 @@
# Request latency measurement (auth proxy + route wrapper)
Why this exists: a customer reported that "it takes time before all fields load when clicking around" (2026-08-26). The API handlers themselves are fast (p50 38 ms); the cost is the number of sequential calls a page makes and the fixed per-request tax in front of each one (auth proxy + route wrapper). This page is the protocol for measuring that tax before and after every change in the responsiveness plan, so no PR claims a win without a number.
## What is instrumented
| Surface | Where | Header | Log line |
|---|---|---|---|
| Auth proxy (every page, RSC, prefetch, `/api` request) | `lib/supabase/middleware.ts` via `lib/supabase/proxy-timing.ts` | `Server-Timing: mw-auth, mw-session, mw-company, mw-mfa, mw-total` on page/RSC/prefetch responses; `X-Proxy-Timing` (same value) on `/api` responses | `proxy completed` with `kind` (`page`, `rsc`, `prefetch`, `api`), `route` (ids and tokens collapsed), `status`, `authMs`, `sessionMs`, `companyMs`, `mfaMs`, `totalMs` |
| Route wrapper (`withRouteContext`, 367 of 529 routes) | `lib/api/with-route-context.ts` | `Server-Timing: auth, company, handler` | `op completed` with `operation`, `status`, `durationMs`, `authMs`, `companyMs`, `handlerMs` |
| Browser | `@vercel/speed-insights` mounted in `app/layout.tsx` | n/a | Vercel dashboard > Speed Insights > Routes (p75 TTFB, FCP, LCP, INP, CLS per route) |
Phase meanings for the proxy: `authMs` = `supabase.auth.getUser()` (a network call to Supabase Auth); `sessionMs` = session-timeout cookie state (`getClaims`, HMAC verify, `auto_logout` read when re-minting); `companyMs` = `resolve_active_company` RPC including the write-back on fallback; `mfaMs` = assurance-level check plus `listFactors()` (a second network call) on the enforced-MFA path.
## Reading the numbers
Percentiles per route from production logs (the `vercel` CLI is linked to the project; `--limit` defaults to 100, raise it):
```bash
# route wrapper, grouped by operation
vercel logs --environment production --since 24h --limit 1000 --json --query "op completed" \
| npx tsx scripts/perf/log-percentiles.ts --field durationMs,authMs,companyMs,handlerMs --group operation
# auth proxy, grouped by request kind and route
vercel logs --environment production --since 24h --limit 1000 --json --query "proxy completed" \
| npx tsx scripts/perf/log-percentiles.ts --field totalMs,authMs,sessionMs,companyMs,mfaMs --group kind,route
# auth proxy, one row per kind (the headline fixed cost)
vercel logs --environment production --since 24h --limit 1000 --json --query "proxy completed" \
| npx tsx scripts/perf/log-percentiles.ts --field totalMs,authMs,companyMs,mfaMs --group kind
```
If `--limit` above 100 is not honoured by the installed CLI, loop `--since`/`--until` windows and concatenate before piping. The Vercel MCP `get_runtime_logs` tool (`group_by: route`) gives request counts per route, not percentiles; use it for prefetch volume (`kind=prefetch` per page load).
In the browser: DevTools > Network, pick a document or `_rsc` request, Timing tab, the `mw-*` metrics show the proxy phases; `/api` responses show `auth`/`company`/`handler` from the route wrapper and the proxy numbers in the `X-Proxy-Timing` response header.
Request count per interaction (the number that maps to "fields load late"), in the console after each action on a production build (`next build && next start`):
```js
performance.getEntriesByType('resource')
.filter((r) => /\/api\/|\/rest\/v1\//.test(r.name))
.map((r) => `${Math.round(r.startTime)} ${Math.round(r.duration)}ms ${r.name}`)
```
Interactions to record every time: `/transactions` soft navigation; open Bokför on a row; `/bookkeeping` then Nytt verifikat; `/invoices` then Ny faktura; `/reports`; `/supplier-invoices/new`; a list row to its detail page on customers and invoices. Note the request count and how many dependent rounds (start times that wait on an earlier response).
## Targets (p75 unless stated)
| Metric | Target | Why |
|---|---|---|
| Proxy `page`/`rsc` totalMs | p50 < 40 ms, p90 < 100 ms | Two parallel DB waves at most, no auth network call |
| Proxy `api` totalMs | p50 < 5 ms | Local JWT verification only |
| Route wrapper auth + company | p50 < 45 ms (read), same for write | One company resolution per call, never two |
| TTFB (hard load) | < 400 ms | Layout on two DB waves |
| LCP | < 1.5 s | |
| INP | < 200 ms | The metric "clicking around" maps to |
| Reference-data requests per form open | 0 blocking | Seeded and cached client-side |
## Protocol per PR
1. Before merging: record the current numbers for the interactions above on a production build, and the last 24 h of `proxy completed` / `op completed` percentiles, in the PR description.
2. 24 h after the production deploy: re-run the same three commands and the same interactions; append one row per PR to the table below.
3. A PR that claims a latency win without a before/after row is not done.
## Baseline and results
| Date | Change | Proxy page p50/p90 | Proxy api p50/p90 | Route auth+company p50 | Notes |
|---|---|---|---|---|---|
| 2026-08-26 | Route wrapper only (proxy unmeasured) | n/a | n/a | 44 ms (auth 3, company 41) | 100-call sample; handler p50 38 ms, total p50 96 ms, p90 274 ms |
+139
View File
@@ -0,0 +1,139 @@
#!/usr/bin/env node
/**
* Static import closure of every 'use client' module.
*
* Answers "which client components pull module X into the browser bundle,
* and through which path?" without running `next build`. Walks static
* `import ... from` / `export ... from` edges (NOT dynamic `import()`, which
* splits a chunk, and NOT `import type`, which is erased), resolving `@/`
* and relative specifiers to .ts/.tsx/.js/.mjs files or directory indexes.
* Bare specifiers (packages, Node builtins) are recorded as leaves.
*
* node scripts/perf/client-import-closure.mjs lib/bookkeeping/bas-data/index.ts
* node scripts/perf/client-import-closure.mjs crypto node:crypto buffer vm
*
* Prints, per target, the client files whose closure reaches it and the
* shortest import path for each. Used by the responsiveness plan (B7) and
* by the client-node-builtin guard in scripts/checks.
*/
import fs from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..')
const SCAN_DIRS = ['app', 'components', 'contexts', 'extensions', 'lib', 'i18n']
const IGNORE_DIRS = new Set(['node_modules', '.next', '.git', 'dist', 'build', 'coverage', '__tests__'])
const EXTS = ['.ts', '.tsx', '.js', '.mjs', '.jsx']
// Block comment bodies are `(?:[^*]|\*(?!\/))*` so an unclosed `/*` cannot be
// re-split at every later `/*` (CodeQL js/redos on the lazy form).
// Single-character whitespace alternative (not \s+): a `+` inside the outer
// `*` is a nested quantifier on the same character, which CodeQL js/redos
// flags as exponential on long runs of spaces.
const USE_CLIENT_RE = /^(?:\s|\/\/[^\n]*\n|\/\*(?:[^*]|\*(?!\/))*\*\/)*['"]use client['"]/
// Static edges only. `import type {...} from` and `export type {...} from`
// are skipped; `import x, { type Y } from` still counts (x is a value).
// One quantifier per span (a greedy `[^'"]*` up to the specifier's opening
// quote, which it cannot cross) so a run of whitespace has a single parse:
// the earlier `\s+ ... [^'"]*? ... \s` shape backtracked exponentially
// (CodeQL js/redos).
const EDGE_RE = /^[ \t]*(?:import|export) (?!type\b)[^'"]*from[ \t]*['"]([^'"]+)['"]/gm
const SIDE_EFFECT_IMPORT_RE = /^\s*import\s+['"]([^'"]+)['"]/gm
export function walkFiles(root = ROOT) {
const out = []
const visit = (dir) => {
if (!fs.existsSync(dir)) return
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (IGNORE_DIRS.has(entry.name)) continue
const full = path.join(dir, entry.name)
if (entry.isDirectory()) visit(full)
else if (/\.(?:ts|tsx|js|mjs|jsx)$/.test(entry.name) && !/\.(?:test|pg\.test)\.tsx?$/.test(entry.name)) out.push(full)
}
}
for (const d of SCAN_DIRS) visit(path.join(root, d))
return out
}
export function resolveSpecifier(spec, fromFile, root = ROOT) {
let base
if (spec.startsWith('@/')) base = path.join(root, spec.slice(2))
else if (spec.startsWith('.')) base = path.resolve(path.dirname(fromFile), spec)
else return { bare: spec }
const candidates = [base, ...EXTS.map((e) => base + e), ...EXTS.map((e) => path.join(base, 'index' + e))]
for (const c of candidates) {
if (fs.existsSync(c) && fs.statSync(c).isFile()) return { file: c }
}
return { missing: spec }
}
export function importsOf(source) {
const specs = new Set()
for (const m of source.matchAll(EDGE_RE)) specs.add(m[1])
for (const m of source.matchAll(SIDE_EFFECT_IMPORT_RE)) specs.add(m[1])
return [...specs]
}
/** Build the graph once: file -> { edges: [file|bare], client: boolean }. */
export function buildGraph(root = ROOT) {
const graph = new Map()
for (const file of walkFiles(root)) {
const source = fs.readFileSync(file, 'utf8')
const edges = []
for (const spec of importsOf(source)) {
const r = resolveSpecifier(spec, file, root)
if (r.file) edges.push(r.file)
else if (r.bare) edges.push(`bare:${r.bare}`)
}
graph.set(file, { edges, client: USE_CLIENT_RE.test(source) })
}
return graph
}
/**
* For each client file, BFS its closure; return { clientFile -> path[] } for
* closures that contain `target` (a repo-relative file path or `bare:<spec>`).
*/
export function clientReachers(graph, target, root = ROOT) {
const targetKey = target.startsWith('bare:') ? target : path.join(root, target)
const hits = new Map()
for (const [file, node] of graph) {
if (!node.client) continue
const prev = new Map([[file, null]])
const queue = [file]
let found = null
while (queue.length && !found) {
const cur = queue.shift()
const edges = graph.get(cur)?.edges ?? []
for (const next of edges) {
if (prev.has(next)) continue
prev.set(next, cur)
if (next === targetKey) { found = next; break }
if (!next.startsWith('bare:')) queue.push(next)
}
}
if (found) {
const chain = []
for (let n = found; n; n = prev.get(n)) chain.unshift(n)
hits.set(path.relative(root, file), chain.map((n) => (n.startsWith('bare:') ? n : path.relative(root, n))))
}
}
return hits
}
const isMain = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
if (isMain) {
const targets = process.argv.slice(2)
if (!targets.length) {
console.error('usage: node scripts/perf/client-import-closure.mjs <repo-relative file | bare specifier> ...')
process.exit(1)
}
const graph = buildGraph()
for (const t of targets) {
const key = t.includes('/') || t.endsWith('.ts') || t.endsWith('.tsx') ? t : `bare:${t}`
const hits = clientReachers(graph, key)
console.log(`\n== ${t}: ${hits.size} client file(s) reach it`)
for (const [file, chain] of [...hits].sort()) console.log(` ${file}\n ${chain.join('\n > ')}`)
}
}
+205
View File
@@ -0,0 +1,205 @@
/**
* Percentiles over structured log lines.
*
* Reads JSON Lines on stdin (the shape `vercel logs --json` emits, or raw
* logger output) and prints a markdown table of count / p50 / p90 / p99 /
* max per group for the numeric fields asked for. Used for the
* "op completed" lines from lib/api/with-route-context.ts and the
* "proxy completed" lines from lib/supabase/middleware.ts.
*
* vercel logs --environment production --since 24h --limit 1000 --json \
* --query "op completed" \
* | npx tsx scripts/perf/log-percentiles.ts \
* --field durationMs,authMs,companyMs,handlerMs --group operation
*
* vercel logs --environment production --since 24h --limit 1000 --json \
* --query "proxy completed" \
* | npx tsx scripts/perf/log-percentiles.ts \
* --field totalMs,authMs,companyMs,mfaMs --group kind,route
*
* Options: --field a,b (required), --group x,y (default: none, one row),
* --filter key=value (repeatable; exact match on the parsed record),
* --min-count N (drop groups with fewer samples, default 1).
*
* No dependencies on purpose: this must run from a clean checkout.
*/
export type LogRecord = Record<string, unknown>
/**
* Turn one input line into a flat record. `vercel logs --json` wraps the
* application line in a `message` (or `text`) string, so an embedded JSON
* object inside that string is parsed and merged over the envelope; a bare
* JSON logger line is used as-is. Unparseable lines yield null.
*/
export function extractRecord(line: string): LogRecord | null {
const trimmed = line.trim()
if (!trimmed.startsWith('{')) return null
let envelope: LogRecord
try {
envelope = JSON.parse(trimmed) as LogRecord
} catch {
return null
}
const message = envelope.message ?? envelope.text
if (typeof message === 'string') {
const start = message.indexOf('{')
if (start >= 0) {
try {
const embedded = JSON.parse(message.slice(start)) as LogRecord
return { ...envelope, ...embedded }
} catch {
// Not a JSON payload: fall through and use the envelope alone.
}
}
}
return envelope
}
/** Nearest-rank percentile on an ascending-sorted array. */
export function percentile(sorted: number[], p: number): number {
if (sorted.length === 0) return Number.NaN
const rank = Math.ceil((p / 100) * sorted.length)
return sorted[Math.min(sorted.length, Math.max(1, rank)) - 1]
}
export interface FieldStats {
p50: number
p90: number
p99: number
max: number
}
export interface GroupRow {
group: string
count: number
fields: Record<string, FieldStats>
}
export interface SummarizeOptions {
fields: string[]
groupBy?: string[]
filters?: Array<{ key: string; value: string }>
minCount?: number
}
function matchesFilters(record: LogRecord, filters: SummarizeOptions['filters']): boolean {
if (!filters || filters.length === 0) return true
return filters.every(({ key, value }) => String(record[key]) === value)
}
export function summarize(records: LogRecord[], options: SummarizeOptions): GroupRow[] {
const groupBy = options.groupBy ?? []
const minCount = options.minCount ?? 1
const buckets = new Map<string, { count: number; values: Record<string, number[]> }>()
for (const record of records) {
if (!matchesFilters(record, options.filters)) continue
const groupKey = groupBy.length
? groupBy.map((key) => String(record[key] ?? '')).join(' / ')
: 'all'
let bucket = buckets.get(groupKey)
if (!bucket) {
bucket = {
count: 0,
values: Object.fromEntries(options.fields.map((f) => [f, [] as number[]])),
}
buckets.set(groupKey, bucket)
}
bucket.count += 1
for (const field of options.fields) {
const value = record[field]
if (typeof value === 'number' && Number.isFinite(value)) bucket.values[field].push(value)
}
}
const rows: GroupRow[] = []
for (const [group, bucket] of buckets) {
const count = bucket.count
if (count < minCount) continue
const fields: Record<string, FieldStats> = {}
for (const field of options.fields) {
const sorted = [...bucket.values[field]].sort((a, b) => a - b)
fields[field] = {
p50: percentile(sorted, 50),
p90: percentile(sorted, 90),
p99: percentile(sorted, 99),
max: sorted.length ? sorted[sorted.length - 1] : Number.NaN,
}
}
rows.push({ group, count, fields })
}
// Slowest first by the first field's p50 so the table reads as a ranking.
const primary = options.fields[0]
rows.sort((a, b) => (b.fields[primary]?.p50 ?? 0) - (a.fields[primary]?.p50 ?? 0))
return rows
}
function fmt(n: number): string {
return Number.isNaN(n) ? '-' : String(Math.round(n))
}
export function renderMarkdown(rows: GroupRow[], fields: string[]): string {
const header = ['group', 'n', ...fields.flatMap((f) => [`${f} p50`, `${f} p90`, `${f} p99`, `${f} max`])]
const lines = [
`| ${header.join(' | ')} |`,
`|${header.map(() => '---').join('|')}|`,
]
for (const row of rows) {
const cells = [row.group, String(row.count)]
for (const field of fields) {
const s = row.fields[field]
cells.push(fmt(s.p50), fmt(s.p90), fmt(s.p99), fmt(s.max))
}
lines.push(`| ${cells.join(' | ')} |`)
}
return lines.join('\n')
}
export function parseArgs(argv: string[]): SummarizeOptions {
const options: SummarizeOptions = { fields: [], groupBy: [], filters: [], minCount: 1 }
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i]
const next = () => {
i += 1
const value = argv[i]
if (value === undefined) throw new Error(`${arg} needs a value`)
return value
}
if (arg === '--field') options.fields = next().split(',').filter(Boolean)
else if (arg === '--group') options.groupBy = next().split(',').filter(Boolean)
else if (arg === '--filter') {
const [key, ...rest] = next().split('=')
options.filters!.push({ key, value: rest.join('=') })
} else if (arg === '--min-count') options.minCount = Number(next())
else throw new Error(`unknown option ${arg}`)
}
if (options.fields.length === 0) throw new Error('--field is required')
return options
}
async function readStdin(): Promise<string> {
const chunks: Buffer[] = []
for await (const chunk of process.stdin) chunks.push(chunk as Buffer)
return Buffer.concat(chunks).toString('utf8')
}
async function main(): Promise<void> {
const options = parseArgs(process.argv.slice(2))
const input = await readStdin()
const records = input
.split('\n')
.map(extractRecord)
.filter((r): r is LogRecord => r !== null)
const rows = summarize(records, options)
process.stdout.write(`${records.length} records parsed\n\n`)
process.stdout.write(`${renderMarkdown(rows, options.fields)}\n`)
}
if (process.argv[1] && /log-percentiles\.(?:ts|mts|js|mjs)$/.test(process.argv[1])) {
main().catch((err) => {
console.error(err instanceof Error ? err.message : err)
process.exit(1)
})
}
+90
View File
@@ -0,0 +1,90 @@
/**
* Provkörning of the receipt hunt against a real company.
*
* npx tsx scripts/receipt-hunt-dryrun.mts <company_id> # Underlag only
* npx tsx scripts/receipt-hunt-dryrun.mts <company_id> --mail # also search mailboxes
* npx tsx scripts/receipt-hunt-dryrun.mts <company_id> --mail --live # actually fetch and stage
*
* Without --live nothing is written: the hunt scores and reports, and with
* --mail it lists what the mailboxes hold without copying any of it.
*
* Everything is imported dynamically, after .env.local is read into
* process.env. Static imports are hoisted and would run before the environment
* exists, which makes the Supabase client fail inside the extension that
* extracts uploaded documents: the failure is silent apart from one event-bus
* error, and it leaves every fetched receipt without an amount.
*/
import { readFileSync } from 'node:fs'
const companyId = process.argv[2]
const withMail = process.argv.includes('--mail')
const live = process.argv.includes('--live')
// --sweep searches every candidate purchase instead of the nightly top slice.
// For a company with a backlog the first run is a backfill, not a nightly tick.
const sweep = process.argv.includes('--sweep')
if (!companyId) throw new Error('usage: npx tsx scripts/receipt-hunt-dryrun.mts <company_id> [--mail] [--live]')
const env = new Map<string, string>()
for (const line of readFileSync('.env.local', 'utf8').split('\n')) {
const m = line.match(/^([A-Z0-9_]+)=(.*)$/)
if (m) env.set(m[1], m[2].trim())
}
for (const [k, v] of env) if (!process.env[k]) process.env[k] = v
const url = env.get('NEXT_PUBLIC_SUPABASE_URL')
const key = env.get('SUPABASE_SERVICE_ROLE_KEY')
if (!url || !key) throw new Error('missing Supabase credentials in .env.local')
// --live writes to whatever database .env.local points at, which for this repo
// is production. Requiring the company id to be named again is the cheapest
// guard that a stray --live on a recalled command cannot pass.
if (live && process.env.RECEIPT_HUNT_CONFIRM !== companyId) {
throw new Error(
`--live writes documents and proposals to the database in .env.local.\n` +
`Re-run with RECEIPT_HUNT_CONFIRM=${companyId} to confirm.`,
)
}
const { createClient } = await import('@supabase/supabase-js')
// Wiring the event bus is what lets document.uploaded reach the extraction
// extension. Importing lib/init is not enough; it has to be called.
const { ensureInitialized } = await import('@/lib/init')
ensureInitialized()
const { huntCompany } = await import('@/lib/receipt-hunt/hunt')
const supabase = createClient(url, key, { auth: { persistSession: false } })
const result = await huntCompany(supabase, companyId, live ? `live-${Date.now()}` : 'dryrun', {
dryRun: !live,
searchMail: withMail,
...(sweep ? { mailSearchLimit: 500 } : {}),
})
if (live) {
console.log(`\n*** SKARP KÖRNING: ${result.mail?.ingested ?? 0} underlag hämtade, ${result.proposed} förslag lagda ***`)
}
console.log(`\n=== ${live ? 'KÖRNING' : 'PROVKÖRNING (inget skrivet)'} ===`)
console.log(`obokförda köp utan kvitto : ${result.candidates}`)
console.log(`kvitton i Underlag : ${result.poolSize}`)
console.log(`förslag : ${result.proposed}\n`)
for (const p of result.proposals ?? []) {
console.log(` ${p.merchant_name ?? '(okänd)'} ${p.total_amount} ${p.currency ?? ''}`)
console.log(` confidence ${p.confidence} [${p.matchReasons.join(', ')}]`)
}
if (result.mail) {
console.log(`\n=== BREVLÅDOR ===`)
console.log(`köp genomsökta : ${result.mail.searched}`)
console.log(`underlag hittade : ${result.mail.withCandidates}`)
console.log(`hämtade : ${result.mail.ingested}\n`)
for (const c of result.mail.candidates) {
console.log(` [${c.merchant}] ${c.fileName ?? '(bilaga)'}`)
console.log(` ur ${c.mailbox}: "${c.subject ?? '(utan ämne)'}" från ${c.from ?? '?'}`)
console.log(` ${c.reason}`)
}
if (result.mail.candidates.length === 0) console.log(' (inga underlag hittade)')
} else if (withMail) {
console.log('\n(ingen brevlåda kopplad, eller Gmail inte konfigurerat)')
}
console.log('')
+290
View File
@@ -0,0 +1,290 @@
/**
* One-off prod repair for Arcim Technology AB (2026-06-11).
*
* Context: two supplier invoices were left in inconsistent half-states:
* - 20250928 (TIC): match-supplier-invoice marked it paid but the payment
* voucher failed (AccountsNotInChartError: 3740 missing) and the failure
* was swallowed. No payment JE, payments row + tx unlinked to any JE.
* - 18299 (RosholmDell): bank sync auto-linked transactions.supplier_invoice_id
* at high confidence without booking a payment; invoice stuck 'registered'.
* - Both registration vouchers (A64/A65) booked the expense on 5010 (form
* default) instead of 5420 / 6580.
*
* Runs through the real engine (createJournalEntry / correctEntry) so voucher
* numbering, balance triggers, and correction links behave exactly as in-app.
* Idempotent: every step checks its precondition and skips if already done.
*
* Usage: npx tsx scripts/repair-arcim-supplier-payments.ts [--execute]
* Without --execute it only prints the plan and preconditions (dry run).
*/
import fs from 'fs'
import path from 'path'
import { createClient } from '@supabase/supabase-js'
import { createJournalEntry, findFiscalPeriod } from '@/lib/bookkeeping/engine'
import { correctEntry } from '@/lib/core/bookkeeping/storno-service'
const COMPANY_ID = 'ed461bc1-dbb5-4568-ae20-9337515878e2'
const USER_ID = '9762dd12-7009-4ba2-aa9f-f9966d53e077'
const TIC_INVOICE_ID = '580b3c81-ced8-4a5a-8757-97ddef7919d5'
const TIC_PAYMENT_ROW_ID = 'bcfc57d3-2a8a-47cc-8b70-a0650089d9d1'
const TIC_TX_ID = '8e608944-a82c-4a1b-8807-bb7d2d6092b6'
const TIC_REGISTRATION_JE = '667db3a5-c388-42eb-a05f-0b45f26fb3db' // A64
const RD_INVOICE_ID = 'bbb2ecd4-e373-4b1e-9908-927d9c906fbc'
const RD_TX_ID = 'b5339acc-d47c-406a-8f3d-d1d9b4d82f93'
const RD_REGISTRATION_JE = 'f1a06d39-331b-4eb5-92cb-5f47d5e52881' // A65
const PAYMENT_DATE = '2026-06-08'
// BAS 2026 metadata, copied verbatim from lib/bookkeeping/bas-data/
const MISSING_ACCOUNTS = [
{
account_number: '3740',
account_name: 'Öres- och kronutjämning',
account_class: 3,
account_group: '37',
account_type: 'revenue',
normal_balance: 'debit',
description: 'Öresskillnad som uppstår vid avrundning av betalningar (öret).',
sru_code: '7310',
k2_excluded: false,
},
{
account_number: '6580',
account_name: 'Advokat- och rättegångskostnader',
account_class: 6,
account_group: '65',
account_type: 'expense',
normal_balance: 'debit',
description: 'Advokat- och rättegångskostnader',
sru_code: '7321',
k2_excluded: false,
},
]
function loadEnv(): { url: string; key: string } {
const envPath = path.resolve(process.cwd(), '.env.local')
const lines = fs.readFileSync(envPath, 'utf8').split('\n')
const vars: Record<string, string> = {}
for (const line of lines) {
const m = line.match(/^([A-Z0-9_]+)=(.*)$/)
if (m) vars[m[1]] = m[2].trim()
}
const url = vars.NEXT_PUBLIC_SUPABASE_URL
const key = vars.SUPABASE_SERVICE_ROLE_KEY
if (!url || !key) throw new Error('Missing Supabase env in .env.local')
if (!url.includes('pwxtzglxptnnvjrpixpg')) {
throw new Error(`Refusing to run against unexpected project: ${url}`)
}
return { url, key }
}
const EXECUTE = process.argv.includes('--execute')
async function main() {
const { url, key } = loadEnv()
const supabase = createClient(url, key, { auth: { persistSession: false } })
const mode = EXECUTE ? 'EXECUTE' : 'DRY RUN'
console.log(`=== Arcim supplier payment repair: ${mode} ===\n`)
// ---------- Step 0: verify preconditions ----------
const { data: tic } = await supabase
.from('supplier_invoices')
.select('status, paid_amount, remaining_amount, payment_journal_entry_id')
.eq('id', TIC_INVOICE_ID).eq('company_id', COMPANY_ID).single()
const { data: rd } = await supabase
.from('supplier_invoices')
.select('status, paid_amount, remaining_amount, payment_journal_entry_id')
.eq('id', RD_INVOICE_ID).eq('company_id', COMPANY_ID).single()
if (!tic || !rd) throw new Error('Could not load invoices')
console.log('TIC invoice:', tic)
console.log('RD invoice :', rd)
const fiscalPeriodId = await findFiscalPeriod(supabase, COMPANY_ID, PAYMENT_DATE)
if (!fiscalPeriodId) throw new Error(`No fiscal period for ${PAYMENT_DATE}`)
console.log('Fiscal period:', fiscalPeriodId, '\n')
// ---------- Step 1: ensure 3740 + 6580 exist ----------
for (const acc of MISSING_ACCOUNTS) {
const { data: existing } = await supabase
.from('chart_of_accounts')
.select('id')
.eq('company_id', COMPANY_ID)
.eq('account_number', acc.account_number)
.maybeSingle()
if (existing) {
console.log(`[skip] account ${acc.account_number} already in chart`)
continue
}
console.log(`[plan] add account ${acc.account_number} ${acc.account_name}`)
if (EXECUTE) {
const { error } = await supabase.from('chart_of_accounts').insert({
...acc,
company_id: COMPANY_ID,
user_id: USER_ID,
is_active: true,
})
if (error) throw new Error(`Insert ${acc.account_number} failed: ${error.message}`)
console.log(`[done] account ${acc.account_number} added`)
}
}
// ---------- Step 2: TIC payment voucher + link backfill ----------
if (tic.payment_journal_entry_id) {
console.log('[skip] TIC already has payment_journal_entry_id')
} else {
console.log('[plan] TIC payment voucher: D 2440 11231.25 / K 1930 11231.00 / K 3740 0.25')
if (EXECUTE) {
const je = await createJournalEntry(supabase, COMPANY_ID, USER_ID, {
fiscal_period_id: fiscalPeriodId,
entry_date: PAYMENT_DATE,
description:
'Utbetalning leverantörsfaktura 20250928, The Intelligence Company AB (publ)',
source_type: 'supplier_invoice_paid',
source_id: TIC_INVOICE_ID,
lines: [
{ account_number: '2440', debit_amount: 11231.25, credit_amount: 0, line_description: 'Kvittning leverantörsskuld' },
{ account_number: '1930', debit_amount: 0, credit_amount: 11231, line_description: 'Utbetalning från bank' },
{ account_number: '3740', debit_amount: 0, credit_amount: 0.25, line_description: 'Öresavrundning' },
],
})
console.log(`[done] TIC payment voucher ${je.voucher_series}-${je.voucher_number} (${je.id})`)
const upd1 = await supabase.from('supplier_invoices')
.update({ payment_journal_entry_id: je.id, transaction_id: TIC_TX_ID })
.eq('id', TIC_INVOICE_ID).eq('company_id', COMPANY_ID)
if (upd1.error) throw new Error(`TIC invoice backfill failed: ${upd1.error.message}`)
const upd2 = await supabase.from('supplier_invoice_payments')
.update({ journal_entry_id: je.id })
.eq('id', TIC_PAYMENT_ROW_ID).eq('company_id', COMPANY_ID)
if (upd2.error) throw new Error(`TIC payment row backfill failed: ${upd2.error.message}`)
const upd3 = await supabase.from('transactions')
.update({ journal_entry_id: je.id })
.eq('id', TIC_TX_ID).eq('company_id', COMPANY_ID)
if (upd3.error) throw new Error(`TIC tx backfill failed: ${upd3.error.message}`)
console.log('[done] TIC links backfilled (invoice, payment row, transaction)')
}
}
// ---------- Step 3: RosholmDell payment voucher + full settle ----------
if (rd.status === 'paid') {
console.log('[skip] RD invoice already paid')
} else {
console.log('[plan] RD payment voucher: D 2440 29890 / K 1930 29890; settle invoice')
if (EXECUTE) {
const je = await createJournalEntry(supabase, COMPANY_ID, USER_ID, {
fiscal_period_id: fiscalPeriodId,
entry_date: PAYMENT_DATE,
description: 'Utbetalning leverantörsfaktura 18299, RosholmDell Advokatbyrå AB',
source_type: 'supplier_invoice_paid',
source_id: RD_INVOICE_ID,
lines: [
{ account_number: '2440', debit_amount: 29890, credit_amount: 0, line_description: 'Kvittning leverantörsskuld' },
{ account_number: '1930', debit_amount: 0, credit_amount: 29890, line_description: 'Utbetalning från bank' },
],
})
console.log(`[done] RD payment voucher ${je.voucher_series}-${je.voucher_number} (${je.id})`)
const upd1 = await supabase.from('supplier_invoices')
.update({
status: 'paid',
paid_amount: 29890,
remaining_amount: 0,
paid_at: new Date().toISOString(),
payment_journal_entry_id: je.id,
transaction_id: RD_TX_ID,
})
.eq('id', RD_INVOICE_ID).eq('company_id', COMPANY_ID).eq('status', 'registered')
.select('id')
if (upd1.error || !upd1.data?.length) {
throw new Error(`RD invoice settle failed: ${upd1.error?.message ?? 'status changed concurrently'}`)
}
const ins = await supabase.from('supplier_invoice_payments').insert({
user_id: USER_ID,
company_id: COMPANY_ID,
supplier_invoice_id: RD_INVOICE_ID,
payment_date: PAYMENT_DATE,
amount: 29890,
currency: 'SEK',
exchange_rate_difference: 0,
journal_entry_id: je.id,
transaction_id: RD_TX_ID,
})
if (ins.error) throw new Error(`RD payment row insert failed: ${ins.error.message}`)
const upd2 = await supabase.from('transactions')
.update({ journal_entry_id: je.id, is_business: true })
.eq('id', RD_TX_ID).eq('company_id', COMPANY_ID)
if (upd2.error) throw new Error(`RD tx backfill failed: ${upd2.error.message}`)
console.log('[done] RD invoice settled + links backfilled')
}
}
// ---------- Step 4: corrections A64 (5010→5420) and A65 (5010→6580) ----------
const corrections = [
{
label: 'A64 (TIC): 5010 → 5420 Programvaror',
entryId: TIC_REGISTRATION_JE,
lines: [
{ account_number: '5420', debit_amount: 8985, credit_amount: 0, line_description: 'Leverantörsfaktura 20250928, The Intelligence Company AB (publ) (ankomst 1)' },
{ account_number: '2641', debit_amount: 2246.25, credit_amount: 0, line_description: 'Ingående moms 25% Leverantörsfaktura 20250928, The Intelligence Company AB (publ) (ankomst 1)' },
{ account_number: '2440', debit_amount: 0, credit_amount: 11231.25, line_description: 'Leverantörsfaktura 20250928, The Intelligence Company AB (publ) (ankomst 1)' },
],
},
{
label: 'A65 (RosholmDell): 5010 → 6580 Advokat- och rättegångskostnader',
entryId: RD_REGISTRATION_JE,
lines: [
{ account_number: '6580', debit_amount: 23912, credit_amount: 0, line_description: 'Leverantörsfaktura 18299, RosholmDell Advokatbyrå AB (ankomst 2)' },
{ account_number: '2641', debit_amount: 5978, credit_amount: 0, line_description: 'Ingående moms 25% Leverantörsfaktura 18299, RosholmDell Advokatbyrå AB (ankomst 2)' },
{ account_number: '2440', debit_amount: 0, credit_amount: 29890, line_description: 'Leverantörsfaktura 18299, RosholmDell Advokatbyrå AB (ankomst 2)' },
],
},
]
for (const c of corrections) {
const { data: orig } = await supabase
.from('journal_entries')
.select('status')
.eq('id', c.entryId).eq('company_id', COMPANY_ID).single()
if (!orig) throw new Error(`Original entry not found for ${c.label}`)
if (orig.status !== 'posted') {
console.log(`[skip] ${c.label}: original status is '${orig.status}' (already corrected?)`)
continue
}
console.log(`[plan] correct ${c.label}`)
if (EXECUTE) {
const { reversal, corrected } = await correctEntry(
supabase, COMPANY_ID, USER_ID, c.entryId, c.lines,
)
console.log(
`[done] ${c.label}: storno ${reversal.voucher_series}-${reversal.voucher_number}, ` +
`corrected ${corrected.voucher_series}-${corrected.voucher_number}`,
)
}
}
// ---------- Step 5: verify ----------
if (EXECUTE) {
const { data: ap } = await supabase
.from('journal_entry_lines')
.select('debit_amount, credit_amount, journal_entry:journal_entries!inner(company_id, status)')
.eq('account_number', '2440')
.eq('journal_entry.company_id', COMPANY_ID)
.in('journal_entry.status', ['posted', 'reversed'])
const apNet = (ap ?? []).reduce((s, l) => s + (l.credit_amount ?? 0) - (l.debit_amount ?? 0), 0)
console.log(`\n2440 net balance over posted entries: ${Math.round(apNet * 100) / 100} (expect 0)`)
}
console.log('\nDone.')
}
main().catch((err) => {
console.error('\nREPAIR FAILED:', err)
process.exit(1)
})
+208
View File
@@ -0,0 +1,208 @@
#!/usr/bin/env npx tsx
/**
* Repair the Capelix AB invoice-001 double-booking (2026-05-29).
*
* Incident: v1 mark-paid read invoiceAlreadyBooked from a column its select
* never fetched (fixed in PR #713). Capelix (kontantmetoden) had invoice 001
* registered at send (voucher A6: Dr 1510 / Cr 3001 1500 / Cr 2611 375), so
* mark-paid should have CLEARED 1510: instead it booked a cash entry
* (voucher A41: Dr 1930 / Cr 3001 1500 / Cr 2611 375). Net damage: revenue
* 3001 and VAT 2611 double-counted, 1510 carries an orphaned 1 875 kr debit.
*
* Fix (engine-faithful, storno-only per BFL/BFNAR 2013:2, period is open,
* moms_period=yearly with no declaration filed, so no rättelse needed):
* 1. reverseEntry(A41): storno the wrong cash entry per 2026-05-29.
* 2. createInvoicePaymentJournalEntry(...): the correct clearing entry
* (Dr 1930 / Cr 1510, 1 875 kr) per 2026-05-29.
* 3. Relink the bank transaction and invoice_payments row from A41 to the
* new clearing entry (same economics, keeps reconciliation intact).
*
* Net account effect: 3001 −1500, 2611 −375, 1510 −1875 (cleared), 1930
* unchanged (matches the real bank inflow).
*
* Every step checks preconditions and skips completed work: safe to re-run.
*
* Usage:
* npx tsx scripts/repair-capelix-invoice-payment.ts # dry run
* npx tsx scripts/repair-capelix-invoice-payment.ts --commit # apply
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import { reverseEntry } from '../lib/bookkeeping/engine'
import { createInvoicePaymentJournalEntry } from '../lib/bookkeeping/invoice-entries'
import type { Invoice } from '../types'
const COMPANY_ID = 'c02c8b65-c7c3-4830-b099-853ad174fbd0' // Capelix AB
const OWNER_USER_ID = '81526a33-df2d-41a6-a3dd-98bea1efa80d'
const INVOICE_ID = '2a9ba8c4-8c2b-45b6-a492-8367b881b968' // invoice 001
const REGISTRATION_ENTRY_ID = '3f721508-5ab4-468f-9f35-36b41a8d26a5' // A6
const WRONG_CASH_ENTRY_ID = 'b25b0f6f-d88e-465e-a966-f3a57638638d' // A41
const PAYMENT_DATE = '2026-05-29'
const COMMIT = process.argv.includes('--commit')
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey) as SupabaseClient
async function entryStatus(id: string): Promise<{ status: string; voucher: string } | null> {
const { data } = await supabase
.from('journal_entries')
.select('status, voucher_series, voucher_number')
.eq('company_id', COMPANY_ID)
.eq('id', id)
.maybeSingle()
if (!data) return null
return { status: data.status, voucher: `${data.voucher_series}${data.voucher_number}` }
}
async function main() {
console.log('─────────────────────────────────────────────────────────')
console.log(`Capelix invoice-001 repair: ${COMMIT ? 'COMMIT' : 'DRY RUN'}`)
console.log('─────────────────────────────────────────────────────────')
// ── Preconditions ──────────────────────────────────────────────
const registration = await entryStatus(REGISTRATION_ENTRY_ID)
const wrongCash = await entryStatus(WRONG_CASH_ENTRY_ID)
if (!registration || registration.status !== 'posted') {
throw new Error(`Registration entry A6 not found/posted (got ${registration?.status}), aborting`)
}
console.log(`✓ Registration ${registration.voucher}: posted`)
const { data: invoiceRow } = await supabase
.from('invoices')
.select('*, customer:customers(name)')
.eq('company_id', COMPANY_ID)
.eq('id', INVOICE_ID)
.maybeSingle()
if (!invoiceRow) throw new Error('Invoice not found, aborting')
if (invoiceRow.journal_entry_id !== REGISTRATION_ENTRY_ID) {
throw new Error(
`invoice.journal_entry_id is ${invoiceRow.journal_entry_id}, expected the A6 registration entry, aborting`,
)
}
console.log(`✓ Invoice ${invoiceRow.invoice_number}: status=${invoiceRow.status}, total=${invoiceRow.total}, linked to A6`)
// Existing correct clearing entry? (idempotency for step 2)
const { data: existingClearing } = await supabase
.from('journal_entries')
.select('id, status, voucher_series, voucher_number')
.eq('company_id', COMPANY_ID)
.eq('source_type', 'invoice_paid')
.eq('source_id', INVOICE_ID)
.eq('status', 'posted')
.maybeSingle()
// ── Step 1: storno A41 ─────────────────────────────────────────
let stornoDone = false
if (!wrongCash) throw new Error('Wrong cash entry A41 not found, aborting')
if (wrongCash.status === 'reversed') {
console.log(`✓ Step 1 already done: ${wrongCash.voucher} is reversed`)
stornoDone = true
} else if (wrongCash.status === 'posted') {
if (COMMIT) {
const storno = await reverseEntry(supabase, COMPANY_ID, OWNER_USER_ID, WRONG_CASH_ENTRY_ID, PAYMENT_DATE)
console.log(`✓ Step 1: stornoed ${wrongCash.voucher} → reversal voucher ${storno.voucher_series}${storno.voucher_number}`)
stornoDone = true
} else {
console.log(`→ Step 1 (dry run): would storno ${wrongCash.voucher} (Dr 1930 / Cr 3001 / Cr 2611) per ${PAYMENT_DATE}`)
}
} else {
throw new Error(`Unexpected A41 status: ${wrongCash.status}, aborting`)
}
// ── Step 2: correct clearing entry ─────────────────────────────
let clearingId: string | null = existingClearing?.id ?? null
if (existingClearing) {
console.log(`✓ Step 2 already done: clearing entry ${existingClearing.voucher_series}${existingClearing.voucher_number} exists`)
} else if (COMMIT) {
if (!stornoDone) throw new Error('Refusing to book clearing before storno, aborting')
const clearing = await createInvoicePaymentJournalEntry(
supabase,
COMPANY_ID,
OWNER_USER_ID,
invoiceRow as unknown as Invoice,
PAYMENT_DATE,
undefined,
(invoiceRow as { customer?: { name?: string } }).customer?.name,
// Full invoice total: the engine would otherwise read remaining_amount,
// which is 0 on this already-paid row.
Number(invoiceRow.total),
)
if (!clearing) throw new Error('Clearing entry was not created (no open fiscal period?), aborting')
clearingId = clearing.id
console.log(`✓ Step 2: booked clearing Dr 1930 / Cr 1510 ${invoiceRow.total} kr → voucher ${clearing.voucher_series}${clearing.voucher_number}`)
} else {
console.log(`→ Step 2 (dry run): would book clearing entry Dr 1930 / Cr 1510 ${invoiceRow.total} kr per ${PAYMENT_DATE}`)
}
// ── Step 3: relink bank transaction + payment row ──────────────
if (COMMIT && clearingId) {
const { data: relinkTx } = await supabase
.from('transactions')
.update({ journal_entry_id: clearingId })
.eq('company_id', COMPANY_ID)
.eq('journal_entry_id', WRONG_CASH_ENTRY_ID)
.select('id')
console.log(`✓ Step 3a: relinked ${relinkTx?.length ?? 0} bank transaction(s) A41 → clearing`)
const { data: relinkPay } = await supabase
.from('invoice_payments')
.update({ journal_entry_id: clearingId })
.eq('company_id', COMPANY_ID)
.eq('journal_entry_id', WRONG_CASH_ENTRY_ID)
.select('id')
console.log(`✓ Step 3b: relinked ${relinkPay?.length ?? 0} invoice_payments row(s) A41 → clearing`)
} else if (!COMMIT) {
const { count: txCount } = await supabase
.from('transactions')
.select('id', { count: 'exact', head: true })
.eq('company_id', COMPANY_ID)
.eq('journal_entry_id', WRONG_CASH_ENTRY_ID)
console.log(`→ Step 3 (dry run): would relink ${txCount ?? 0} bank transaction(s) + invoice_payments row(s) to the clearing entry`)
}
// ── Verify ─────────────────────────────────────────────────────
if (COMMIT) {
// True ledger net: include 'reversed' entries too, a reversed entry's
// lines stay in the GL and its storno (which carries the same source_id)
// cancels them. Filtering to 'posted' only would count the storno without
// its counterpart and report a false imbalance.
const { data: lines } = await supabase
.from('journal_entry_lines')
.select('account_number, debit_amount, credit_amount, journal_entry:journal_entries!inner(company_id, source_id, status)')
.eq('journal_entry.company_id', COMPANY_ID)
.eq('journal_entry.source_id', INVOICE_ID)
.in('journal_entry.status', ['posted', 'reversed'])
const net = new Map<string, number>()
for (const l of lines ?? []) {
const acct = l.account_number as string
net.set(acct, (net.get(acct) ?? 0) + Number(l.debit_amount) - Number(l.credit_amount))
}
console.log('Net per account over posted entries for this invoice:')
for (const [acct, sum] of [...net.entries()].sort()) {
console.log(` ${acct}: ${Math.round(sum * 100) / 100}`)
}
const ok =
Math.abs(net.get('1510') ?? 0) < 0.005 &&
Math.round(Math.abs(net.get('3001') ?? 0)) === 1500 &&
Math.round(Math.abs(net.get('2611') ?? 0)) === 375 &&
Math.round(net.get('1930') ?? 0) === 1875
console.log(ok ? '✓ VERIFIED: 1510 cleared, revenue/VAT single-counted, 1930 matches bank inflow' : '✗ VERIFY FAILED: inspect manually')
if (!ok) process.exit(1)
}
console.log('─────────────────────────────────────────────────────────')
console.log(COMMIT ? 'Repair complete.' : 'Dry run complete: re-run with --commit to apply.')
}
main().catch((err) => {
console.error('Repair failed:', err)
process.exit(1)
})
+194
View File
@@ -0,0 +1,194 @@
/**
* Comprehensive charset repair for chart_of_accounts account names.
*
* Background: account names were corrupted for ~888 companies in the seeding /
* import window 2026-03-30 → 2026-06-12, in three signatures (see
* lib/bookkeeping/charset-repair.ts): stripped diacritics, lost-byte U+FFFD, and
* double-encoded UTF-8. The root causes are CLOSED: the runtime
* seed_chart_of_accounts() function on prod now carries correct diacritics (the
* fix migration 20260516130000 was applied to prod ~2026-06-12; classic
* prod-migration-drift, so companies created during the drift window were
* seeded by the old stripped function). This script repairs the legacy rows.
*
* Strategy (per row): resolveCorrectName() against CANDIDATE correct names for
* the same account number: the clean sibling rows already in the table plus the
* BAS reference name. Double-encoded reverses losslessly (recovers customs too);
* stripped/lost-byte require an unambiguous clean sibling, else the row is left
* untouched and reported. Never clobbers user-renamed accounts.
*
* Usage: npx tsx scripts/repair-chart-of-accounts-charset.ts [--execute]
* Without --execute it prints the plan only (dry run). Idempotent: clean rows
* resolve to no-op, so re-running is safe.
*/
import fs from 'fs'
import path from 'path'
import { createClient } from '@supabase/supabase-js'
import { getBASReference } from '@/lib/bookkeeping/bas-reference'
import {
resolveCorrectName,
hasLostByte,
hasMojibakeSignature,
hasCp1252Artifact,
isClean,
type RepairResult,
} from '@/lib/bookkeeping/charset-repair'
const isCorrupt = (s: string) =>
hasLostByte(s) || hasMojibakeSignature(s) || hasCp1252Artifact(s)
function loadEnv(): { url: string; key: string } {
const envPath = path.resolve(process.cwd(), '.env.local')
const vars: Record<string, string> = {}
for (const line of fs.readFileSync(envPath, 'utf8').split('\n')) {
const m = line.match(/^([A-Z0-9_]+)=(.*)$/)
if (m) vars[m[1]] = m[2].trim()
}
const url = vars.NEXT_PUBLIC_SUPABASE_URL
const key = vars.SUPABASE_SERVICE_ROLE_KEY
if (!url || !key) throw new Error('Missing Supabase env in .env.local')
if (!url.includes('pwxtzglxptnnvjrpixpg')) {
throw new Error(`Refusing to run against unexpected project: ${url}`)
}
return { url, key }
}
const EXECUTE = process.argv.includes('--execute')
interface Row {
id: string
company_id: string
account_number: string
account_name: string
}
async function main() {
const { url, key } = loadEnv()
const supabase = createClient(url, key, { auth: { persistSession: false } })
console.log(`=== chart_of_accounts charset repair: ${EXECUTE ? 'EXECUTE' : 'DRY RUN'} ===\n`)
// Closure over the inferred client so we don't annotate (and mismatch) the
// supabase-js generic parameters.
const fetchAll = async (): Promise<Row[]> => {
const out: Row[] = []
const PAGE = 1000
for (let from = 0; ; from += PAGE) {
const { data, error } = await supabase
.from('chart_of_accounts')
.select('id, company_id, account_number, account_name')
.order('id', { ascending: true })
.range(from, from + PAGE - 1)
if (error) throw new Error(`fetch chart_of_accounts failed: ${error.message}`)
const batch = (data ?? []) as Row[]
out.push(...batch)
if (batch.length < PAGE) break
}
return out
}
const rows = await fetchAll()
console.log(`Scanned ${rows.length} chart_of_accounts rows across ${new Set(rows.map((r) => r.company_id)).size} companies.\n`)
// Candidate correct names per account number = clean sibling names in the
// table + the BAS reference name. (Clean = no lost-byte, no mojibake.)
const candidates = new Map<string, Set<string>>()
for (const r of rows) {
if (isClean(r.account_name)) {
const set = candidates.get(r.account_number) ?? new Set<string>()
set.add(r.account_name)
candidates.set(r.account_number, set)
}
}
for (const r of rows) {
const bas = getBASReference(r.account_number)?.account_name
if (bas) {
const set = candidates.get(r.account_number) ?? new Set<string>()
set.add(bas)
candidates.set(r.account_number, set)
}
}
const fixes: Array<Row & { result: RepairResult }> = []
const unresolved: Row[] = []
for (const r of rows) {
const cand = [...(candidates.get(r.account_number) ?? new Set<string>())]
const result = resolveCorrectName(r.account_name, cand)
if (result && result.corrected !== r.account_name) {
fixes.push({ ...r, result })
} else if (!result && isCorrupt(r.account_name)) {
unresolved.push(r) // carries a corruption signature but no confident fix
}
}
// ── Report ──────────────────────────────────────────────────────
const byMethod = (m: RepairResult['method']) => fixes.filter((f) => f.result.method === m)
const sample = (arr: Array<Row & { result: RepairResult }>) =>
arr.slice(0, 8).map((f) => ` ${f.account_number} "${f.account_name}" → "${f.result.corrected}"`)
for (const m of ['sibling_stripped', 'reverse', 'reverse_cp437', 'sibling_lostbyte'] as const) {
const g = byMethod(m)
console.log(`[${m}] ${g.length} rows across ${new Set(g.map((f) => f.company_id)).size} companies`)
if (g.length) console.log(sample(g).join('\n'))
console.log()
}
console.log(`Total fixes: ${fixes.length} rows across ${new Set(fixes.map((f) => f.company_id)).size} companies.`)
if (unresolved.length) {
console.log(`\n[unresolved] ${unresolved.length} rows carry a corruption signature but have no unambiguous clean sibling: left untouched:`)
console.log(
[...new Map(unresolved.map((u) => [`${u.account_number}|${u.account_name}`, u])).values()]
.slice(0, 20)
.map((u) => ` ${u.account_number} "${u.account_name}"`)
.join('\n'),
)
}
if (!EXECUTE) {
console.log('\nDRY RUN: no rows changed. Re-run with --execute to apply.')
return
}
// ── Apply (chunked) ─────────────────────────────────────────────
let applied = 0
let skipped = 0
let errors = 0
const CHUNK = 25
for (let i = 0; i < fixes.length; i += CHUNK) {
const chunk = fixes.slice(i, i + CHUNK)
const results = await Promise.all(
chunk.map((f) =>
supabase
.from('chart_of_accounts')
.update({ account_name: f.result.corrected })
.eq('id', f.id)
.eq('company_id', f.company_id)
// TOCTOU guard: only write if the row still holds the exact corrupted
// value we read. A concurrent rename → 0 rows changed (skipped, not
// clobbered). Also makes the script strictly idempotent.
.eq('account_name', f.account_name)
.select('id')
.then(({ data, error }) =>
error
? { ok: false as const, msg: error.message }
: { ok: true as const, changed: (data?.length ?? 0) > 0 }),
),
)
for (const r of results) {
if (!r.ok) {
errors++
console.error(` update failed: ${r.msg}`)
} else if (r.changed) {
applied++
} else {
skipped++ // row changed under us since the read: left as-is
}
}
if ((i / CHUNK) % 10 === 0) console.log(` …${applied}/${fixes.length} applied`)
}
console.log(`\nDone. Applied ${applied}, skipped ${skipped}, errors ${errors}.`)
}
main().catch((err) => {
console.error('\nREPAIR FAILED:', err)
process.exit(1)
})
+295
View File
@@ -0,0 +1,295 @@
#!/usr/bin/env npx tsx
/**
* Repair redundant opening-balance (IB) entries from multi-year SIE imports.
*
* Problem: the pre-fix SIE import created one posted journal entry with
* source_type='opening_balance' for every year imported. Year N+1's IB
* equals year N's UB, which is already the sum of year N's transactions,
* so summing prior lines to derive a cumulative balance double-counts one
* year of movements per extra IB. Result: cash and other balance-sheet
* accounts drift upward each year.
*
* Fix (per-company): keep the earliest IB entry as the company's pre-system
* starting capital; storno (reverse) every later IB entry. The immutability
* trigger on journal_entries blocks DELETE of posted entries, so storno is
* the only legally-compliant path (BFL / BFNAR 2013:2).
*
* Side effect: the storno'd period's fiscal_periods.opening_balance_entry_id
* link is cleared so getOpeningBalances() falls through to the duplicate-
* safe compute_prior_opening_balances RPC for that period.
*
* Also handles the "start over" case via --purge-imports:
* - Storno every SIE-origin journal entry for the company.
* - Delete sie_imports rows so their (company_id, file_hash) pairs free up.
* - Leaves fiscal periods in place (they may host manual entries too).
*
* Usage:
* # Preview IB dedup only
* npx tsx scripts/repair-company-ib-duplicates.ts \
* --company-id <uuid> --user-id <uuid>
*
* # Apply IB dedup
* npx tsx scripts/repair-company-ib-duplicates.ts \
* --company-id <uuid> --user-id <uuid> --commit
*
* # Preview full purge (IBs + all SIE-origin entries + sie_imports rows)
* npx tsx scripts/repair-company-ib-duplicates.ts \
* --company-id <uuid> --user-id <uuid> --purge-imports
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import { reverseEntry } from '../lib/bookkeeping/engine'
// ──────────────────────────────────────────────────────────────────
// Args
// ──────────────────────────────────────────────────────────────────
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
const COMPANY_ID = arg('company-id')
const USER_ID = arg('user-id')
const COMMIT = process.argv.includes('--commit')
const PURGE_IMPORTS = process.argv.includes('--purge-imports')
if (!COMPANY_ID || !USER_ID) {
console.error(
'Usage: npx tsx scripts/repair-company-ib-duplicates.ts --company-id <uuid> --user-id <uuid> [--purge-imports] [--commit]'
)
process.exit(1)
}
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey) as SupabaseClient
// ──────────────────────────────────────────────────────────────────
// Banner
// ──────────────────────────────────────────────────────────────────
console.log('─────────────────────────────────────────────────────────')
console.log('IB Duplicate Repair')
console.log('─────────────────────────────────────────────────────────')
console.log('Supabase URL :', supabaseUrl)
console.log('Company :', COMPANY_ID)
console.log('User :', USER_ID)
console.log('Mode :', COMMIT ? 'COMMIT (writes)' : 'DRY RUN (no writes)')
console.log('Purge imports:', PURGE_IMPORTS ? 'YES (also storno SIE-origin entries)' : 'NO')
console.log('─────────────────────────────────────────────────────────\n')
// ──────────────────────────────────────────────────────────────────
// IB dedup
// ──────────────────────────────────────────────────────────────────
interface IbRow {
id: string
fiscal_period_id: string | null
entry_date: string
created_at: string
voucher_series: string | null
voucher_number: number | null
}
async function listPostedIbEntries(): Promise<IbRow[]> {
const { data, error } = await supabase
.from('journal_entries')
.select('id, fiscal_period_id, entry_date, created_at, voucher_series, voucher_number')
.eq('company_id', COMPANY_ID!)
.eq('source_type', 'opening_balance')
.eq('status', 'posted')
.order('entry_date', { ascending: true })
.order('created_at', { ascending: true })
if (error) throw new Error(`Failed to list IB entries: ${error.message}`)
return (data as IbRow[]) ?? []
}
async function unlinkFromFiscalPeriod(entryId: string): Promise<void> {
const { error } = await supabase
.from('fiscal_periods')
.update({ opening_balance_entry_id: null, opening_balances_set: false })
.eq('company_id', COMPANY_ID!)
.eq('opening_balance_entry_id', entryId)
if (error) throw new Error(`Failed to unlink fiscal_periods.opening_balance_entry_id: ${error.message}`)
}
async function stornoIbDuplicates(): Promise<{ kept: IbRow | null; stornoed: number; failed: number }> {
console.log('[1/2] IB dedup')
const ibs = await listPostedIbEntries()
console.log(` · Found ${ibs.length} posted opening_balance entries`)
if (ibs.length === 0) {
console.log(' · Nothing to do.')
return { kept: null, stornoed: 0, failed: 0 }
}
const [earliest, ...redundant] = ibs
console.log(
` · Keeping earliest IB: ${earliest.id} on ${earliest.entry_date} ` +
`(series ${earliest.voucher_series ?? '?'} #${earliest.voucher_number ?? '?'})`
)
for (const r of redundant) {
console.log(
` · Will storno: ${r.id} on ${r.entry_date} ` +
`(series ${r.voucher_series ?? '?'} #${r.voucher_number ?? '?'})`
)
}
if (!COMMIT) {
console.log(' · [dry-run] skipping storno and unlink')
return { kept: earliest, stornoed: 0, failed: 0 }
}
let stornoed = 0
let failed = 0
for (const r of redundant) {
try {
await unlinkFromFiscalPeriod(r.id)
await reverseEntry(supabase, COMPANY_ID!, USER_ID!, r.id)
console.log(` · Stornoed ${r.id}`)
stornoed++
} catch (err) {
console.error(` · FAILED to storno ${r.id}:`, err instanceof Error ? err.message : err)
failed++
}
}
return { kept: earliest, stornoed, failed }
}
// ──────────────────────────────────────────────────────────────────
// Optional full SIE purge (for "start over" scenario)
// ──────────────────────────────────────────────────────────────────
interface SieImportRow {
id: string
filename: string | null
file_hash: string | null
fiscal_period_id: string | null
status: string | null
imported_at: string | null
}
async function listSieImports(): Promise<SieImportRow[]> {
const { data, error } = await supabase
.from('sie_imports')
.select('id, filename, file_hash, fiscal_period_id, status, imported_at')
.eq('company_id', COMPANY_ID!)
.order('imported_at', { ascending: true })
if (error) throw new Error(`Failed to list sie_imports: ${error.message}`)
return (data as SieImportRow[]) ?? []
}
async function listSieOriginEntries(periodIds: string[]): Promise<{ id: string; voucher_number: number | null; entry_date: string }[]> {
if (periodIds.length === 0) return []
const { data, error } = await supabase
.from('journal_entries')
.select('id, voucher_number, entry_date, fiscal_period_id, source_type, status')
.eq('company_id', COMPANY_ID!)
.in('fiscal_period_id', periodIds)
.eq('status', 'posted')
.in('source_type', ['import', 'opening_balance'])
if (error) throw new Error(`Failed to list SIE-origin entries: ${error.message}`)
return (data as { id: string; voucher_number: number | null; entry_date: string }[]) ?? []
}
async function purgeSieImports(): Promise<void> {
console.log('\n[2/2] SIE import purge')
const imports = await listSieImports()
console.log(` · Found ${imports.length} sie_imports rows`)
if (imports.length === 0) {
console.log(' · Nothing to purge.')
return
}
const periodIds = Array.from(new Set(imports.map((i) => i.fiscal_period_id).filter((p): p is string => !!p)))
const entries = await listSieOriginEntries(periodIds)
console.log(` · Found ${entries.length} posted entries in affected fiscal periods (${periodIds.length} periods)`)
for (const imp of imports) {
console.log(
` · Will remove sie_imports row ${imp.id} (${imp.filename ?? 'unnamed'}, file_hash ${imp.file_hash?.slice(0, 12) ?? '?'}…)`
)
}
if (!COMMIT) {
console.log(' · [dry-run] skipping storno and sie_imports delete')
return
}
let stornoed = 0
let failed = 0
for (const e of entries) {
try {
await supabase
.from('fiscal_periods')
.update({ opening_balance_entry_id: null, opening_balances_set: false })
.eq('company_id', COMPANY_ID!)
.eq('opening_balance_entry_id', e.id)
await reverseEntry(supabase, COMPANY_ID!, USER_ID!, e.id)
stornoed++
} catch (err) {
console.error(` · FAILED to storno entry ${e.id}:`, err instanceof Error ? err.message : err)
failed++
}
}
console.log(` · Stornoed ${stornoed}/${entries.length} entries (${failed} failed)`)
const { error: delErr } = await supabase
.from('sie_imports')
.delete()
.eq('company_id', COMPANY_ID!)
if (delErr) {
console.error(` · FAILED to delete sie_imports rows: ${delErr.message}`)
} else {
console.log(` · Deleted ${imports.length} sie_imports rows`)
}
}
// ──────────────────────────────────────────────────────────────────
// Main
// ──────────────────────────────────────────────────────────────────
async function main() {
try {
const ibResult = await stornoIbDuplicates()
if (PURGE_IMPORTS) {
await purgeSieImports()
}
console.log('\n─────────────────────────────────────────────────────────')
console.log('Summary')
console.log('─────────────────────────────────────────────────────────')
if (ibResult.kept) {
console.log(`Kept IB entry : ${ibResult.kept.id} (${ibResult.kept.entry_date})`)
}
console.log(`IBs stornoed : ${ibResult.stornoed}`)
console.log(`IB storno fails : ${ibResult.failed}`)
console.log(`Mode : ${COMMIT ? 'COMMIT' : 'DRY RUN'}`)
if (!COMMIT) {
console.log('\nRe-run with --commit to apply.')
}
} catch (err) {
console.error('\nFATAL:', err instanceof Error ? err.message : err)
process.exit(1)
}
}
main()
@@ -0,0 +1,169 @@
/**
* One-off repair: move an individual customer's personnummer out of
* customers.org_number into customers.personal_number (encrypted).
*
* WHY: until 2026-08-21 the MCP gnubok_create_customer tool had no
* personal_number input, the v1 REST docs said org_number was "accepted as
* input" for individuals, and nothing masks org_number. So a customer_type=
* 'individual' row could carry its personnummer in org_number, where the
* web customer list, the v1 detail endpoint and the MCP customer list showed
* it raw (GDPR art. 5.1 c). The code fix moves such a value into
* personal_number on every write path; this repairs the rows already in the
* DB. Run AFTER the code fix is deployed (the read paths mask these rows in
* the meantime, but the data should not stay there).
*
* Selection: customer_type='individual' AND org_number has Swedish personnummer
* shape (lib/customers/personal-number-shape.ts). Legal-entity org numbers
* never match (month position >= 20), so an individual carrying a real
* organisationsnummer is left alone.
*
* Per row:
* - personal_number NULL -> personal_number = encrypt(org_number), org_number = NULL
* - personal_number already set -> org_number = NULL only (the stored
* personnummer wins; the duplicate is reported)
*
* Idempotent: a repaired row no longer matches the selection, and every
* update is guarded on the exact org_number value so a re-run or a concurrent
* edit can never double-apply. Safe to re-run.
*
* Usage:
* npx tsx scripts/repair-customer-personal-number-in-org-number.ts # dry run (read-only)
* npx tsx scripts/repair-customer-personal-number-in-org-number.ts --confirm # performs the writes
* add --company <uuid> to limit either mode to one company
*
* Reads NEXT_PUBLIC_SUPABASE_URL + SUPABASE_SERVICE_ROLE_KEY from .env.local;
* --confirm additionally needs PERSONNUMMER_ENCRYPTION_KEY (the production
* key, same one the app encrypts with). Treat .env.local as pointing at
* PRODUCTION: the dry run is read-only; --confirm mutates PII. Never prints a
* personnummer; rows are reported by id and company.
*/
import { createClient } from '@supabase/supabase-js'
import { config as dotenv } from 'dotenv'
import { resolve } from 'node:path'
import { encryptCustomerPersonalNumber } from '@/lib/customers/protect-personal-number'
import {
normalizeReroutedPersonalNumber,
orgNumberHoldsPersonalNumber,
} from '@/lib/customers/personal-number-shape'
dotenv({ path: resolve(process.cwd(), '.env.local') })
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL
const SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!SUPABASE_URL || !SERVICE_KEY) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const CONFIRM = process.argv.includes('--confirm')
// Refuse to WRITE without the real key: encrypting with the dev fallback key
// would make the values unreadable in production. The dry run encrypts
// nothing, so it runs without it (the key lives in the Vercel env, not in
// every local .env.local).
if (CONFIRM && !process.env.PERSONNUMMER_ENCRYPTION_KEY) {
console.error(
'Missing PERSONNUMMER_ENCRYPTION_KEY. Refusing to write so rows are not encrypted with the dev fallback key '
+ '(vercel env pull, or set it for this run).',
)
process.exit(1)
}
const companyFlag = process.argv.indexOf('--company')
const ONLY_COMPANY = companyFlag >= 0 ? process.argv[companyFlag + 1] : undefined
const sb = createClient(SUPABASE_URL, SERVICE_KEY, { auth: { persistSession: false } })
type Row = {
id: string
company_id: string
customer_type: string
org_number: string | null
personal_number: string | null
}
async function main() {
const host = new URL(SUPABASE_URL!).host
console.log(
`Target: ${host} mode: ${CONFIRM ? 'WRITE (--confirm)' : 'DRY RUN (read-only)'}`
+ (ONLY_COMPANY ? ` company: ${ONLY_COMPANY}` : ''),
)
// Page on the PK: PostgREST caps an unranged select at 1000 rows.
const PAGE = 1000
const candidates: Row[] = []
for (let from = 0; ; from += PAGE) {
let query = sb
.from('customers')
.select('id, company_id, customer_type, org_number, personal_number')
.eq('customer_type', 'individual')
.not('org_number', 'is', null)
.order('id', { ascending: true })
.range(from, from + PAGE - 1)
if (ONLY_COMPANY) query = query.eq('company_id', ONLY_COMPANY)
const { data, error } = await query
if (error) throw new Error(`select customers: ${error.message}`)
const rows = (data ?? []) as Row[]
candidates.push(...rows)
if (rows.length < PAGE) break
}
const affected = candidates.filter((r) => orgNumberHoldsPersonalNumber(r.customer_type, r.org_number))
const toMove = affected.filter((r) => !r.personal_number)
const toClear = affected.filter((r) => r.personal_number)
const companies = new Set(affected.map((r) => r.company_id))
console.log(
`Individual rows with org_number: ${candidates.length}; personnummer-shaped: ${affected.length} `
+ `across ${companies.size} companies (move: ${toMove.length}, clear-duplicate: ${toClear.length}).`,
)
for (const r of affected) {
console.log(` ${r.personal_number ? 'CLEAR' : 'MOVE '} customer ${r.id} company ${r.company_id}`)
}
if (!CONFIRM) {
console.log('Dry run: no writes performed. Re-run with --confirm to apply.')
return
}
let moved = 0
let cleared = 0
let skipped = 0
for (const r of affected) {
// Guard on the exact current value: a concurrent edit or a re-run cannot
// double-apply, and a row that changed underneath is skipped, not clobbered.
// Two literal payloads rather than one built at runtime, so the
// no-phantom-columns scanner can resolve both.
const { data, error } = r.personal_number
? await sb
.from('customers')
.update({ org_number: null })
.eq('id', r.id)
.eq('company_id', r.company_id)
.eq('org_number', r.org_number!)
.select('id')
: await sb
.from('customers')
.update({
org_number: null,
personal_number: encryptCustomerPersonalNumber(normalizeReroutedPersonalNumber(r.org_number!)),
})
.eq('id', r.id)
.eq('company_id', r.company_id)
.eq('org_number', r.org_number!)
.select('id')
if (error) {
console.error(` FAILED customer ${r.id}: ${error.message}`)
continue
}
if (!data || data.length === 0) {
skipped += 1
continue
}
if (r.personal_number) cleared += 1
else moved += 1
}
console.log(`Done. moved: ${moved}, cleared duplicate org_number: ${cleared}, skipped (changed underneath): ${skipped}.`)
}
main().catch((err) => {
console.error(err instanceof Error ? err.message : err)
process.exit(1)
})
+340
View File
@@ -0,0 +1,340 @@
#!/usr/bin/env npx tsx
/**
* One-off repair for a company whose fiscal period chain was broken by
* the pre-fix "change fiscal year" flow: a newer period was created
* without previous_period_id and without an opening_balance_entry_id,
* so the balance sheet falls back to a full-history scan (and times out
* on production with 8k+ prior lines).
*
* Target state expected (validated before running):
*
* Prior period (e.g. 2024/2025): open, has entries, no closing_entry_id.
* Gap: Sep-Dec 2025, zero entries.
* Orphan period (e.g. 2026): previous_period_id=NULL,
* opening_balance_entry_id=NULL,
* zero entries.
*
* End state:
*
* Prior period: year-end-closed (locked, closing_entry_id, is_closed).
* Short period: Sep-Dec 2025, previous_period_id=prior,
* opening_balance_entry_id set, locked.
* Orphan period: previous_period_id=short, opening_balance_entry_id set.
*
* Usage:
* npx tsx scripts/repair-fiscal-period-chain.ts \
* --company-id <uuid> --user-id <uuid> --prior <uuid> --orphan <uuid> \
* [--short-start 2025-09-01] [--short-end 2025-12-31] \
* [--commit] # default is --dry-run
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
import {
previewYearEndClosing,
generateOpeningBalances,
} from '../lib/core/bookkeeping/year-end-service'
import { validateBalanceContinuity } from '../lib/reports/continuity-check'
import { lockPeriod } from '../lib/core/bookkeeping/period-service'
import { executeCurrencyRevaluation } from '../lib/bookkeeping/currency-revaluation'
import { createJournalEntry } from '../lib/bookkeeping/engine'
// ────────────────────────────────────────────────────────────────────
// Args
// ────────────────────────────────────────────────────────────────────
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
const COMPANY_ID = arg('company-id')
const USER_ID = arg('user-id')
const PRIOR_ID = arg('prior')
const ORPHAN_ID = arg('orphan')
const SHORT_START = arg('short-start') ?? '2025-09-01'
const SHORT_END = arg('short-end') ?? '2025-12-31'
const COMMIT = process.argv.includes('--commit')
if (!COMPANY_ID || !USER_ID || !PRIOR_ID || !ORPHAN_ID) {
console.error(
'Usage: npx tsx scripts/repair-fiscal-period-chain.ts --company-id <uuid> --user-id <uuid> --prior <uuid> --orphan <uuid> [--short-start 2025-09-01] [--short-end 2025-12-31] [--commit]'
)
process.exit(1)
}
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
// ────────────────────────────────────────────────────────────────────
// Banner
// ────────────────────────────────────────────────────────────────────
console.log('─────────────────────────────────────────────────────────')
console.log('Fiscal Period Chain Repair')
console.log('─────────────────────────────────────────────────────────')
console.log('Supabase URL :', supabaseUrl)
console.log('Company :', COMPANY_ID)
console.log('User :', USER_ID)
console.log('Prior period :', PRIOR_ID)
console.log('Orphan period:', ORPHAN_ID)
console.log('Short period :', `${SHORT_START} → ${SHORT_END}`)
console.log('Mode :', COMMIT ? 'COMMIT (writes)' : 'DRY RUN (no writes)')
console.log('─────────────────────────────────────────────────────────\n')
// ────────────────────────────────────────────────────────────────────
// Validate state
// ────────────────────────────────────────────────────────────────────
async function validateState() {
const { data: prior } = await supabase
.from('fiscal_periods')
.select('*')
.eq('id', PRIOR_ID)
.eq('company_id', COMPANY_ID)
.single()
if (!prior) throw new Error(`Prior period ${PRIOR_ID} not found for company ${COMPANY_ID}`)
if (prior.is_closed) throw new Error('Prior period is already closed')
if (prior.closing_entry_id) throw new Error('Prior period already has closing_entry_id')
const { data: orphan } = await supabase
.from('fiscal_periods')
.select('*')
.eq('id', ORPHAN_ID)
.eq('company_id', COMPANY_ID)
.single()
if (!orphan) throw new Error(`Orphan period ${ORPHAN_ID} not found for company ${COMPANY_ID}`)
if (orphan.previous_period_id)
throw new Error(`Orphan period already has previous_period_id = ${orphan.previous_period_id}`)
if (orphan.opening_balance_entry_id)
throw new Error('Orphan period already has opening_balance_entry_id')
// Gap check: zero entries between prior end and short end
const { count: gapCount } = await supabase
.from('journal_entries')
.select('id', { count: 'exact', head: true })
.eq('company_id', COMPANY_ID)
.gt('entry_date', prior.period_end)
.lte('entry_date', SHORT_END)
.in('status', ['posted', 'reversed'])
if ((gapCount ?? 0) > 0)
throw new Error(
`Gap between ${prior.period_end} and ${SHORT_END} has ${gapCount} entries: repair assumes zero activity in gap`
)
// Orphan must be empty
const { count: orphanEntries } = await supabase
.from('journal_entries')
.select('id', { count: 'exact', head: true })
.eq('company_id', COMPANY_ID)
.eq('fiscal_period_id', ORPHAN_ID)
.in('status', ['posted', 'reversed'])
if ((orphanEntries ?? 0) > 0)
throw new Error(`Orphan period has ${orphanEntries} posted entries: not safe to repair automatically`)
// Short period dates must be contiguous with prior
const nextAfterPrior = new Date(prior.period_end + 'T12:00:00Z')
nextAfterPrior.setUTCDate(nextAfterPrior.getUTCDate() + 1)
const expected = nextAfterPrior.toISOString().split('T')[0]
if (expected !== SHORT_START)
throw new Error(`--short-start ${SHORT_START} must equal ${expected} (day after prior.period_end)`)
// Short end must be day before orphan start
const beforeOrphan = new Date(orphan.period_start + 'T12:00:00Z')
beforeOrphan.setUTCDate(beforeOrphan.getUTCDate() - 1)
const expectedEnd = beforeOrphan.toISOString().split('T')[0]
if (expectedEnd !== SHORT_END)
throw new Error(`--short-end ${SHORT_END} must equal ${expectedEnd} (day before orphan.period_start)`)
return { prior, orphan }
}
// ────────────────────────────────────────────────────────────────────
// Steps
// ────────────────────────────────────────────────────────────────────
async function step1YearEndPrior(priorEnd: string) {
console.log('\n[1/7] Year-end closing on prior period')
console.log(' · Currency revaluation preview')
if (COMMIT) {
await executeCurrencyRevaluation(supabase, COMPANY_ID!, priorEnd, PRIOR_ID!, USER_ID!)
}
console.log(' · Building closing entry preview')
const preview = await previewYearEndClosing(supabase, COMPANY_ID!, USER_ID!, PRIOR_ID!)
console.log(` net result: ${preview.netResult} → ${preview.closingAccount}`)
console.log(` ${preview.closingLines.length} closing lines`)
if (preview.closingLines.length === 0) {
throw new Error('No result accounts to close: prior period has no activity')
}
if (!COMMIT) {
console.log(' · [dry-run] skipping createJournalEntry, update, lock, close')
return { closingEntryId: '<dry-run>' }
}
console.log(' · Creating closing entry')
const closingEntry = await createJournalEntry(supabase, COMPANY_ID!, USER_ID!, {
fiscal_period_id: PRIOR_ID!,
entry_date: priorEnd,
description: 'Årsbokslut (repair)',
source_type: 'year_end',
voucher_series: 'A',
lines: preview.closingLines,
})
await supabase
.from('fiscal_periods')
.update({ closing_entry_id: closingEntry.id })
.eq('id', PRIOR_ID!)
.eq('company_id', COMPANY_ID!)
console.log(' · Locking and closing prior period')
await lockPeriod(supabase, COMPANY_ID!, USER_ID!, PRIOR_ID!)
await supabase
.from('fiscal_periods')
.update({ is_closed: true, closed_at: new Date().toISOString() })
.eq('id', PRIOR_ID!)
.eq('company_id', COMPANY_ID!)
return { closingEntryId: closingEntry.id }
}
async function step2InsertShortPeriod() {
console.log('\n[2/7] Inserting short transition period')
console.log(` · ${SHORT_START} → ${SHORT_END}`)
if (!COMMIT) {
console.log(' · [dry-run] skipping insert')
return '<dry-run>'
}
const { data, error } = await supabase
.from('fiscal_periods')
.insert({
company_id: COMPANY_ID!,
user_id: USER_ID!,
name: `Transition ${SHORT_START.slice(0, 7)}-${SHORT_END.slice(0, 7)}`,
period_start: SHORT_START,
period_end: SHORT_END,
previous_period_id: PRIOR_ID!,
})
.select()
.single()
if (error || !data) throw new Error(`Failed to insert short period: ${error?.message}`)
console.log(` · Short period id: ${data.id}`)
return data.id as string
}
async function step3GenerateShortOb(shortPeriodId: string) {
console.log('\n[3/7] Generating OB entry on short period (carries forward from prior)')
if (!COMMIT) {
console.log(' · [dry-run] skipping generateOpeningBalances')
return '<dry-run>'
}
const ob = await generateOpeningBalances(supabase, COMPANY_ID!, USER_ID!, PRIOR_ID!, shortPeriodId)
console.log(` · OB entry id: ${ob.id}`)
return ob.id
}
async function step4LockShort(shortPeriodId: string) {
console.log('\n[4/7] Locking short period (no period activity to close)')
if (!COMMIT) {
console.log(' · [dry-run] skipping lockPeriod')
return
}
await lockPeriod(supabase, COMPANY_ID!, USER_ID!, shortPeriodId)
}
async function step5LinkOrphan(shortPeriodId: string) {
console.log('\n[5/7] Linking orphan period to short period')
if (!COMMIT) {
console.log(' · [dry-run] skipping update orphan.previous_period_id')
return
}
const { error } = await supabase
.from('fiscal_periods')
.update({ previous_period_id: shortPeriodId })
.eq('id', ORPHAN_ID!)
.eq('company_id', COMPANY_ID!)
if (error) throw new Error(`Failed to link orphan: ${error.message}`)
}
async function step6GenerateOrphanOb(shortPeriodId: string) {
console.log('\n[6/7] Generating OB entry on orphan period (carries forward from short)')
if (!COMMIT) {
console.log(' · [dry-run] skipping generateOpeningBalances')
return '<dry-run>'
}
const ob = await generateOpeningBalances(supabase, COMPANY_ID!, USER_ID!, shortPeriodId, ORPHAN_ID!)
console.log(` · OB entry id: ${ob.id}`)
return ob.id
}
async function step7Continuity() {
console.log('\n[7/7] Validating IB/UB continuity on orphan period')
if (!COMMIT) {
console.log(' · [dry-run] skipping continuity check')
return
}
const result = await validateBalanceContinuity(supabase, COMPANY_ID!, ORPHAN_ID!)
console.log(` · valid: ${result.valid}, checked: ${result.checked_accounts} accounts`)
if (!result.valid) {
console.log(' · discrepancies:')
for (const d of result.discrepancies) {
console.log(` ${d.account_number}: UB=${d.previous_ub_net}, IB=${d.current_ib_net}, diff=${d.difference}`)
}
}
await supabase
.from('fiscal_periods')
.update({ continuity_verified: result.valid })
.eq('id', ORPHAN_ID!)
.eq('company_id', COMPANY_ID!)
}
// ────────────────────────────────────────────────────────────────────
// Run
// ────────────────────────────────────────────────────────────────────
async function main() {
const { prior } = await validateState()
console.log(`✓ Validated state: prior '${prior.name}' (${prior.period_start} → ${prior.period_end})`)
const { closingEntryId } = await step1YearEndPrior(prior.period_end)
const shortPeriodId = await step2InsertShortPeriod()
const shortObId = await step3GenerateShortOb(shortPeriodId)
await step4LockShort(shortPeriodId)
await step5LinkOrphan(shortPeriodId)
const orphanObId = await step6GenerateOrphanOb(shortPeriodId)
await step7Continuity()
console.log('\n─────────────────────────────────────────────────────────')
console.log('Summary')
console.log('─────────────────────────────────────────────────────────')
console.log('Prior closing entry :', closingEntryId)
console.log('Short period id :', shortPeriodId)
console.log('Short OB entry id :', shortObId)
console.log('Orphan OB entry id :', orphanObId)
console.log('Mode :', COMMIT ? 'COMMITTED' : 'DRY RUN (no writes)')
console.log('─────────────────────────────────────────────────────────')
}
main().catch((err) => {
console.error('\n✗ Repair failed:', err instanceof Error ? err.message : err)
process.exit(1)
})
+172
View File
@@ -0,0 +1,172 @@
/**
* One-off repair: transactions booked with the pre-2026-07-05 hardcoded
* fallback exchange rates (EUR 11.5, USD 10.5, GBP 13.5) or with NULL rates.
*
* WHY: before PR #892, a Riksbanken 429 during bank sync silently booked
* hardcoded rates into transactions.exchange_rate/amount_sek. PR #892 fixed
* the code path (null-not-fallback); this repairs the rows left behind.
*
* WHAT IT DOES:
* - UNBOOKED rows (journal_entry_id IS NULL): re-fetches the correct
* per-date rate from Riksbanken (via the exchange_rates cache) and
* overwrites exchange_rate / exchange_rate_date / amount_sek.
* - BOOKED rows (posted to journal entries): NEVER touched. Reported as a
* per-company materiality summary so corrections can be decided per
* company (storno/correctEntry, never edit posted entries).
*
* Idempotent: updates are guarded on the exact old rate value (or NULL) plus
* journal_entry_id IS NULL, so re-runs and concurrent bookings are safe.
* Once a row is repaired it no longer matches the poisoned pattern.
*
* Usage:
* npx tsx scripts/repair-fx-fallback-rates.ts # dry run (read-only)
* npx tsx scripts/repair-fx-fallback-rates.ts --execute # performs the writes
*
* Reads NEXT_PUBLIC_SUPABASE_URL + SUPABASE_SERVICE_ROLE_KEY from .env.local.
* Treat .env.local as pointing at PRODUCTION.
*/
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import { config as dotenv } from 'dotenv'
import { resolve } from 'node:path'
import { fetchExchangeRate } from '@/lib/currency/riksbanken'
import type { Currency, ExchangeRate } from '@/types'
dotenv({ path: resolve(process.cwd(), '.env.local') })
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL
const SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!SUPABASE_URL || !SERVICE_KEY) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const EXECUTE = process.argv.includes('--execute')
/** The exact sentinel rates the old getFallbackRate() booked. */
const POISONED: Array<{ currency: Currency; rate: number }> = [
{ currency: 'EUR', rate: 11.5 },
{ currency: 'USD', rate: 10.5 },
{ currency: 'GBP', rate: 13.5 },
]
interface TxRow {
id: string
company_id: string
currency: Currency
amount: number
amount_sek: number | null
exchange_rate: number | null
exchange_rate_date: string | null
date: string
journal_entry_id: string | null
}
const sb: SupabaseClient = createClient(SUPABASE_URL, SERVICE_KEY, {
auth: { persistSession: false },
})
/** In-process memo so repeated (currency, date) pairs cost one lookup. */
const rateMemo = new Map<string, Promise<ExchangeRate | null>>()
function getRate(currency: Currency, date: string): Promise<ExchangeRate | null> {
const key = `${currency}|${date}`
let p = rateMemo.get(key)
if (!p) {
p = fetchExchangeRate(currency, new Date(date), sb)
rateMemo.set(key, p)
}
return p
}
const roundMoney = (x: number) => Math.round(x * 100) / 100
async function main() {
const host = new URL(SUPABASE_URL!).host
console.log(`Target: ${host} mode: ${EXECUTE ? 'WRITE (--execute)' : 'DRY RUN (read-only)'}`)
const orFilter = POISONED.map(
(p) => `and(currency.eq.${p.currency},exchange_rate.eq.${p.rate})`,
).join(',')
const { data: poisonedRows, error: e1 } = await sb
.from('transactions')
.select('id, company_id, currency, amount, amount_sek, exchange_rate, exchange_rate_date, date, journal_entry_id')
.or(orFilter)
if (e1) throw new Error(`select poisoned: ${e1.message}`)
const { data: nullRows, error: e2 } = await sb
.from('transactions')
.select('id, company_id, currency, amount, amount_sek, exchange_rate, exchange_rate_date, date, journal_entry_id')
.not('currency', 'is', null)
.neq('currency', 'SEK')
.is('exchange_rate', null)
if (e2) throw new Error(`select null-rate: ${e2.message}`)
const all: TxRow[] = [...(poisonedRows ?? []), ...(nullRows ?? [])] as TxRow[]
const unbooked = all.filter((r) => r.journal_entry_id === null)
const booked = all.filter((r) => r.journal_entry_id !== null)
console.log(
`Found ${all.length} rows: ${unbooked.length} unbooked (repairable), ${booked.length} booked (report only).`,
)
// ---- Repair (or plan) unbooked rows, sequentially to be gentle on Riksbanken.
let repaired = 0
let skipped = 0
for (const r of unbooked) {
const rate = await getRate(r.currency, r.date)
if (!rate) {
console.log(` SKIP ${r.id} ${r.currency} ${r.date}: no rate available`)
skipped++
continue
}
const newSek = roundMoney(r.amount * rate.rate)
const oldRate = r.exchange_rate === null ? 'NULL' : r.exchange_rate
console.log(
` ${EXECUTE ? 'FIX ' : 'PLAN'} ${r.id} ${r.currency} ${r.date}: rate ${oldRate} -> ${rate.rate} (obs ${rate.date}), amount_sek ${r.amount_sek} -> ${newSek}`,
)
if (!EXECUTE) continue
let q = sb
.from('transactions')
.update({ exchange_rate: rate.rate, exchange_rate_date: rate.date, amount_sek: newSek })
.eq('id', r.id)
.is('journal_entry_id', null)
q = r.exchange_rate === null ? q.is('exchange_rate', null) : q.eq('exchange_rate', r.exchange_rate)
const { error: upErr } = await q
if (upErr) throw new Error(`update ${r.id}: ${upErr.message}`)
repaired++
}
// ---- Materiality report for booked rows (never modified).
if (booked.length > 0) {
console.log('\nBOOKED rows (NOT touched; correct via storno/correctEntry per company if material):')
const byCompany = new Map<string, { n: number; storedSek: number; correctSek: number; missing: number }>()
for (const r of booked) {
const agg = byCompany.get(r.company_id) ?? { n: 0, storedSek: 0, correctSek: 0, missing: 0 }
agg.n++
const rate = await getRate(r.currency, r.date)
if (rate) {
agg.storedSek += r.amount_sek ?? 0
agg.correctSek += roundMoney(r.amount * rate.rate)
} else {
agg.missing++
}
byCompany.set(r.company_id, agg)
}
for (const [companyId, a] of byCompany) {
const diff = roundMoney(a.storedSek - a.correctSek)
console.log(
` company ${companyId}: ${a.n} booked row(s), stored SEK ${roundMoney(a.storedSek)}, correct SEK ${roundMoney(a.correctSek)}, overstatement ${diff}${a.missing ? `, ${a.missing} row(s) without fetchable rate` : ''}`,
)
}
}
console.log(
`\n${EXECUTE ? `Repaired ${repaired} unbooked row(s), skipped ${skipped}.` : `DRY RUN: would repair ${unbooked.length} unbooked row(s). Re-run with --execute.`}`,
)
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
+276
View File
@@ -0,0 +1,276 @@
#!/usr/bin/env npx tsx
/**
* Conservative historical catch-up for the year-open result transfer
* (2099 -> 2098).
*
* Normal year-end closing already posts this transfer immediately after it
* creates the next period's opening balance. This script is only for periods
* created before that behavior existed.
*
* Historical data cannot be repaired from the frozen opening balance alone.
* A user or SIE import may already have disposed of 2099, and replaying the
* opening amount would then move equity twice. The script therefore requires:
*
* - an open and unlocked aktiebolag period,
* - an explicit, posted opening_balance entry,
* - active 2099 and 2098 accounts,
* - no existing posted result_appropriation entry,
* - current posted 2099 equal to the explicit opening 2099, and
* - no other entry touching 2099 in the period.
*
* Everything else is skipped or printed for manual review. Periods without an
* explicit opening-balance entry are never reconstructed from cumulative
* history. All writes use the bookkeeping engine and commit mode re-assesses
* the period immediately before posting. The --user-id the entry is
* attributed to must be a member of the company.
*
* Usage:
* # Preview every company. Read-only.
* npx tsx scripts/repair-result-appropriation.ts
*
* # Preview one company or one exact period.
* npx tsx scripts/repair-result-appropriation.ts --company-id <uuid>
* npx tsx scripts/repair-result-appropriation.ts --company-id <uuid> --period-id <uuid>
*
* # Apply one reviewed period only.
* npx tsx scripts/repair-result-appropriation.ts --commit \
* --company-id <uuid> --period-id <uuid> --user-id <uuid>
*
* Run a reviewed dry-run against staging first. Production writes require
* explicit approval for the exact company, period, amount, and attribution.
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import { fetchAllRows } from '../lib/supabase/fetch-all'
import {
assessHistoricalResultRepair,
getHistoricalResultRepairScopeError,
postHistoricalResultRepair,
type HistoricalResultRepairAssessment,
type HistoricalResultRepairReason,
} from '../lib/core/bookkeeping/result-appropriation-repair'
function arg(name: string): string | undefined {
const index = process.argv.indexOf(`--${name}`)
if (index < 0) return undefined
const value = process.argv[index + 1]
if (value === undefined || value.startsWith('--')) {
console.error(`--${name} requires a value`)
process.exit(1)
}
return value
}
const ONLY_COMPANY_ID = arg('company-id')
const ONLY_PERIOD_ID = arg('period-id')
const USER_ID = arg('user-id')
const COMMIT = process.argv.includes('--commit')
const scopeError = getHistoricalResultRepairScopeError({
commit: COMMIT,
companyId: ONLY_COMPANY_ID,
periodId: ONLY_PERIOD_ID,
userId: USER_ID,
})
if (scopeError) {
console.error(scopeError)
process.exit(1)
}
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey) as SupabaseClient
const REASON_LABELS: Record<HistoricalResultRepairReason, string> = {
ready: 'safe to repair',
non_aktiebolag: 'not an aktiebolag',
period_closed: 'period is closed',
period_locked: 'period is locked',
already_corrected: 'posted result appropriation already exists',
missing_explicit_opening_balance: 'no explicit opening-balance entry',
invalid_opening_balance_entry: 'opening-balance pointer is not a posted opening_balance entry',
missing_required_accounts: 'active 2099 and 2098 accounts are required',
no_result_to_move: 'opening and current 2099 are zero',
already_disposed: 'current 2099 is zero, so the result has already been disposed',
current_balance_differs: 'current 2099 differs from the explicit opening amount',
intervening_2099_activity: 'another entry touched 2099 in the period',
}
console.log('---------------------------------------------------------')
console.log('Historical Result Appropriation Repair (2099 -> 2098)')
console.log('---------------------------------------------------------')
console.log('Supabase URL :', supabaseUrl)
console.log('Company scope:', ONLY_COMPANY_ID ?? 'ALL companies')
console.log('Period scope :', ONLY_PERIOD_ID ?? 'all open periods')
console.log('Attribution :', USER_ID ?? 'not needed for dry-run')
console.log('Mode :', COMMIT ? 'COMMIT (writes one reviewed period)' : 'DRY RUN (no writes)')
console.log('---------------------------------------------------------\n')
async function listCompanyIds(): Promise<string[]> {
if (ONLY_COMPANY_ID) return [ONLY_COMPANY_ID]
const rows = await fetchAllRows<{ id: string }>(({ from, to }) =>
supabase
.from('companies')
.select('id')
.order('created_at', { ascending: true })
.order('id', { ascending: true })
.range(from, to),
)
return rows.map((company) => company.id)
}
async function listOpenPeriods(
companyId: string,
): Promise<Array<{ id: string; name: string }>> {
const rows = await fetchAllRows<{ id: string; name: string }>(({ from, to }) => {
let query = supabase
.from('fiscal_periods')
.select('id, name')
.eq('company_id', companyId)
.eq('is_closed', false)
.is('locked_at', null)
if (ONLY_PERIOD_ID) query = query.eq('id', ONLY_PERIOD_ID)
return query.order('period_start', { ascending: true }).range(from, to)
})
return rows
}
function describeAssessment(assessment: HistoricalResultRepairAssessment): string {
const amounts =
`opening 2099=${assessment.openingNet}, current 2099=${assessment.currentNet}, ` +
`other 2099 entries=${assessment.nonOpeningActivityEntries}`
return `${REASON_LABELS[assessment.reason]} (${amounts})`
}
async function main() {
let companiesScanned = 0
let periodsScanned = 0
let safe = 0
let manualReview = 0
let skipped = 0
let posted = 0
let revalidationBlocked = 0
let failed = 0
const companyIds = await listCompanyIds()
console.log(`Scanning ${companyIds.length} company(ies)...\n`)
for (const companyId of companyIds) {
companiesScanned++
let periods: Array<{ id: string; name: string }>
try {
periods = await listOpenPeriods(companyId)
} catch (error) {
console.error(
` ${companyId}: FAILED to list periods:`,
error instanceof Error ? error.message : error,
)
failed++
continue
}
for (const period of periods) {
periodsScanned++
let assessment: HistoricalResultRepairAssessment
try {
assessment = await assessHistoricalResultRepair(supabase, companyId, period.id)
} catch (error) {
console.error(
` ${companyId} / ${period.name}: FAILED to assess:`,
error instanceof Error ? error.message : error,
)
failed++
continue
}
if (assessment.status !== 'safe') {
if (assessment.status === 'skipped') {
skipped++
continue
}
manualReview++
console.warn(
` REVIEW ${companyId} / ${period.name} (${period.id}): ${describeAssessment(assessment)}`,
)
continue
}
safe++
console.log(
` SAFE ${companyId} / ${period.name} (${period.id}): ` +
`${assessment.plan.direction} ${assessment.plan.amount} kr, ` +
assessment.plan.lines
.map((line) =>
`${line.account_number} ${line.debit_amount ? `D ${line.debit_amount}` : `K ${line.credit_amount}`}`,
)
.join(' / '),
)
if (!COMMIT) continue
try {
const result = await postHistoricalResultRepair(
supabase,
companyId,
USER_ID!,
period.id,
)
if (!result.entry) {
revalidationBlocked++
console.warn(
` NOT POSTED after revalidation: ${describeAssessment(result.assessment)}`,
)
continue
}
posted++
console.log(
` posted ${result.entry.voucher_series}${result.entry.voucher_number} (${result.entry.id})`,
)
} catch (error) {
console.error(
` ${companyId} / ${period.name}: FAILED to post:`,
error instanceof Error ? error.message : error,
)
failed++
}
}
}
console.log('\n---------------------------------------------------------')
console.log('Summary')
console.log('---------------------------------------------------------')
console.log(`Companies scanned : ${companiesScanned}`)
console.log(`Periods scanned : ${periodsScanned}`)
console.log(`Safe candidates : ${safe}`)
console.log(`Manual review : ${manualReview}`)
console.log(`Skipped : ${skipped}`)
console.log(`Posted : ${posted}`)
console.log(`Blocked on recheck : ${revalidationBlocked}`)
console.log(`Failed : ${failed}`)
console.log(`Mode : ${COMMIT ? 'COMMIT' : 'DRY RUN'}`)
if (!COMMIT) {
console.log(
'\nCommit mode requires one reviewed --company-id, --period-id, and --user-id.',
)
}
if (failed > 0) {
process.exitCode = 1
}
}
main().catch((error) => {
console.error('\nFATAL:', error instanceof Error ? error.message : error)
process.exit(1)
})
@@ -0,0 +1,293 @@
#!/usr/bin/env npx tsx
/**
* Issue #2057: repair bank rows that were marked as business before #1990
* (categorize fails closed) but never got a verifikat. They sit as
* is_business = true, journal_entry_id NULL, is_ignored = false with no
* anchor in any of the three booking locations, and the worklist predicate
* (is_business IS NULL) hides them: unbooked and invisible.
*
* The work happens in the repair_stranded_transactions RPC (migration
* 20260906170107): the UPDATE re-asserts the full predicate in the same
* statement, resets the same triple the engine's storno path resets
* (is_business, category, reconciliation_method -> NULL) so the rows return
* to "Att bokfora", never touches a journal entry, and writes one
* BankTransactionStrandedRepaired behandlingshistorik event per row in the
* same transaction. This script only lists, confirms and drives it.
*
* Dry run by default, for every company or one:
*
* npx tsx scripts/repair-stranded-categorized-transactions.ts
* npx tsx scripts/repair-stranded-categorized-transactions.ts --company <uuid>
*
* A write needs ONE company, the actor to record, and a typed confirmation
* that repeats the row count of a fresh dry run:
*
* npx tsx scripts/repair-stranded-categorized-transactions.ts --company <uuid> --actor-user-id <uuid> --execute [--include-locked]
*
* Flags:
* --env <file> env file to load (default .env.local; the banner
* prints the URL so the target is never a guess)
* --company <uuid> restrict to one company (required with --execute)
* --actor-user-id <id> the person running the repair, recorded as the actor
* --execute write; without it nothing is changed
* --include-locked also reset rows whose date falls in a locked or
* closed period, or behind the company lock date; by
* default those are listed and left alone (they could
* not be booked in place anyway, BFL 5 kap 5 §)
* --include-sandbox allow a sandbox company (is_sandbox = true); those are
* normally left to cleanup_sandbox_user
* --verbose print every row in the dry run
*
* Never run by a loop: the founder decides per company (issue #2057).
*/
import { config } from 'dotenv'
import { createInterface } from 'node:readline/promises'
import { randomUUID } from 'node:crypto'
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
function flag(name: string): boolean {
return process.argv.includes(`--${name}`)
}
const ENV_FILE = arg('env') ?? '.env.local'
config({ path: ENV_FILE })
const COMPANY_ID = arg('company') ?? null
const ACTOR_USER_ID = arg('actor-user-id') ?? null
const EXECUTE = flag('execute')
const INCLUDE_LOCKED = flag('include-locked')
const INCLUDE_SANDBOX = flag('include-sandbox')
const VERBOSE = flag('verbose')
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error(`Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in ${ENV_FILE}`)
process.exit(1)
}
if (COMPANY_ID && !UUID_RE.test(COMPANY_ID)) {
console.error('--company must be a uuid')
process.exit(1)
}
if (EXECUTE && !COMPANY_ID) {
console.error('--execute needs --company <uuid>: the repair is decided one company at a time')
process.exit(1)
}
if (EXECUTE && (!ACTOR_USER_ID || !UUID_RE.test(ACTOR_USER_ID))) {
console.error('--execute needs --actor-user-id <uuid> (recorded in behandlingshistorik)')
process.exit(1)
}
interface RepairRow {
transaction_id: string
company_id: string
is_sandbox: boolean
transaction_date: string
amount: number | string
currency: string
previous_category: string | null
lock_state: 'open' | 'locked' | 'closed' | 'company_lock_date' | 'no_period'
repaired: boolean
}
const supabase: SupabaseClient = createClient(supabaseUrl, serviceRoleKey, {
auth: { persistSession: false, autoRefreshToken: false },
})
/** Set-returning RPCs are paged by PostgREST like tables: read every page. */
async function callRepair(params: {
companyId: string | null
dryRun: boolean
actor?: { type: 'user'; id: string; label: string }
correlationId?: string
}): Promise<RepairRow[]> {
const PAGE = 1000
const rows: RepairRow[] = []
// A write is a single call: paging a data-modifying RPC would re-run it.
const maxPages = params.dryRun ? 1000 : 1
for (let page = 0; page < maxPages; page++) {
const from = page * PAGE
const { data, error } = await supabase
.rpc('repair_stranded_transactions', {
p_company_id: params.companyId,
p_dry_run: params.dryRun,
p_skip_locked: !INCLUDE_LOCKED,
p_actor: params.actor ?? null,
p_correlation_id: params.correlationId ?? null,
})
.range(from, from + PAGE - 1)
if (error) throw new Error(`repair_stranded_transactions failed: ${error.message}`)
const chunk = (data ?? []) as RepairRow[]
rows.push(...chunk)
if (chunk.length < PAGE) break
}
return rows
}
interface CompanySummary {
companyId: string
isSandbox: boolean
rows: number
firstDate: string
lastDate: string
byLock: Record<string, number>
}
function summarize(rows: RepairRow[]): CompanySummary[] {
const map = new Map<string, CompanySummary>()
for (const r of rows) {
let s = map.get(r.company_id)
if (!s) {
s = {
companyId: r.company_id,
isSandbox: r.is_sandbox,
rows: 0,
firstDate: r.transaction_date,
lastDate: r.transaction_date,
byLock: {},
}
map.set(r.company_id, s)
}
s.rows += 1
if (r.transaction_date < s.firstDate) s.firstDate = r.transaction_date
if (r.transaction_date > s.lastDate) s.lastDate = r.transaction_date
s.byLock[r.lock_state] = (s.byLock[r.lock_state] ?? 0) + 1
}
return [...map.values()].sort((a, b) => b.rows - a.rows)
}
function lockLine(byLock: Record<string, number>): string {
return Object.entries(byLock)
.sort(([a], [b]) => a.localeCompare(b))
.map(([k, v]) => `${k}=${v}`)
.join(' ')
}
async function companyNames(ids: string[]): Promise<Map<string, string>> {
const names = new Map<string, string>()
for (let i = 0; i < ids.length; i += 100) {
const { data, error } = await supabase
.from('companies')
.select('id, name')
.in('id', ids.slice(i, i + 100))
if (error) throw new Error(`companies lookup failed: ${error.message}`)
for (const c of (data ?? []) as Array<{ id: string; name: string | null }>) {
names.set(c.id, c.name ?? '')
}
}
return names
}
function printBreakdown(title: string, summaries: CompanySummary[], names: Map<string, string>) {
const total = summaries.reduce((n, s) => n + s.rows, 0)
console.log(`\n${title}: ${total} rows in ${summaries.length} companies`)
for (const s of summaries) {
console.log(
` ${s.companyId} ${String(s.rows).padStart(4)} rows ${s.firstDate}..${s.lastDate} ` +
`[${lockLine(s.byLock)}] ${names.get(s.companyId) ?? ''}`,
)
}
}
async function confirm(expectedCount: number): Promise<void> {
const rl = createInterface({ input: process.stdin, output: process.stdout })
try {
const answer = await rl.question(
`\nType "REPAIR ${expectedCount}" to reset these ${expectedCount} rows to Att bokfora: `,
)
if (answer.trim() !== `REPAIR ${expectedCount}`) {
console.log('Aborted, nothing written.')
process.exit(2)
}
} finally {
rl.close()
}
}
async function main() {
console.log('---------------------------------------------------------')
console.log('Stranded categorized transactions repair (issue #2057)')
console.log('---------------------------------------------------------')
console.log('Env file :', ENV_FILE)
console.log('Supabase URL:', supabaseUrl)
console.log('Company :', COMPANY_ID ?? '(all)')
console.log('Locked rows :', INCLUDE_LOCKED ? 'INCLUDED' : 'listed, left alone')
console.log('Mode :', EXECUTE ? 'EXECUTE (writes)' : 'DRY RUN (no writes)')
console.log('---------------------------------------------------------')
const dry = await callRepair({ companyId: COMPANY_ID, dryRun: true })
const inScope = INCLUDE_LOCKED ? dry : dry.filter((r) => r.lock_state === 'open')
const real = summarize(inScope.filter((r) => !r.is_sandbox))
const sandbox = summarize(inScope.filter((r) => r.is_sandbox))
const names = await companyNames([...real, ...sandbox].map((s) => s.companyId))
printBreakdown('Non-sandbox companies', real, names)
printBreakdown('Sandbox companies (left to cleanup_sandbox_user)', sandbox, names)
if (!INCLUDE_LOCKED) {
const skipped = dry.length - inScope.length
if (skipped > 0) {
console.log(`
${skipped} rows sit in locked/closed periods or behind the company lock date and are left alone (pass --include-locked to reset them too).`)
}
}
if (VERBOSE) {
console.log('\nRows:')
for (const r of inScope) {
console.log(
` ${r.company_id} ${r.transaction_id} ${r.transaction_date} ${r.amount} ${r.currency} ` +
`${r.previous_category ?? '-'} ${r.lock_state}`,
)
}
}
if (!EXECUTE) {
console.log('\nDry run only. Re-run with --company <uuid> --actor-user-id <uuid> --execute to write.')
return
}
const target = inScope
if (target.length === 0) {
console.log('\nNothing to repair for this company.')
return
}
if (target.some((r) => r.is_sandbox) && !INCLUDE_SANDBOX) {
console.error('\nThis is a sandbox company. Pass --include-sandbox to repair it anyway.')
process.exit(1)
}
await confirm(target.length)
const correlationId = randomUUID()
const written = await callRepair({
companyId: COMPANY_ID,
dryRun: false,
actor: {
type: 'user',
id: ACTOR_USER_ID as string,
label: 'scripts/repair-stranded-categorized-transactions.ts (#2057)',
},
correlationId,
})
const repaired = written.filter((r) => r.repaired)
const skippedByRace = written.filter((r) => !r.repaired && (INCLUDE_LOCKED || r.lock_state === 'open'))
console.log(`\nRepaired ${repaired.length} rows (correlation ${correlationId}).`)
if (skippedByRace.length > 0) {
console.log(`${skippedByRace.length} rows were booked or changed between the dry run and the write and were left alone.`)
}
const after = await callRepair({ companyId: COMPANY_ID, dryRun: true })
const remaining = INCLUDE_LOCKED ? after : after.filter((r) => r.lock_state === 'open')
console.log(`Remaining stranded rows for this company: ${remaining.length}`)
}
main().catch((err) => {
console.error(err instanceof Error ? err.message : err)
process.exit(1)
})
+44
View File
@@ -0,0 +1,44 @@
/**
* Print what SCB's current API exposes on the Je layout: the variable list,
* the category code tables, and one company looked up by org number, so the
* names in lib/parties/scb/client.ts are checked against the live API.
*
* Usage:
* SCB_API_CERT_PFX_BASE64=... SCB_API_CERT_PASSWORD=... \
* npx tsx scripts/scb/discover.ts [--org 5560125790] [--env <file>]
*
* Read-only against SCB. Prints to stdout; never writes to a database.
*/
import { config as dotenv } from 'dotenv'
import { createScbClient } from '@/lib/parties/scb/client'
import { scbConfigFromEnv } from '@/lib/parties/scb/config'
function arg(name: string): string | undefined {
const i = process.argv.indexOf(`--${name}`)
return i >= 0 ? process.argv[i + 1] : undefined
}
async function main() {
const envFile = arg('env')
if (envFile) dotenv({ path: envFile })
const client = createScbClient(scbConfigFromEnv())
const org = arg('org') ?? '5560125790'
const show = (label: string, v: unknown) => console.log(`\n=== ${label}\n${JSON.stringify(v, null, 2).slice(0, 12000)}`)
try {
show('Variabler (Je)', await client.variables())
} catch (e) {
console.error('Variabler failed:', e instanceof Error ? e.message : e)
}
try {
show('KategorierMedKodtabeller (Je)', await client.categories())
} catch (e) {
console.error('Kategorier failed:', e instanceof Error ? e.message : e)
}
try {
show(`HamtaForetag ${org}`, await client.lookupByOrgNumber(org))
} catch (e) {
console.error('HamtaForetag failed:', e instanceof Error ? e.message : e)
}
}
void main()
+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/env npx tsx
/**
* Seed / sync the agent_atom_registry table from SKILL.md files on disk.
*
* DEV / MANUAL path only. It writes the registry directly with the service role,
* which needs DB access: so it is NOT part of prebuild. Production gets skill
* bodies via the generated seed migration (scripts/generate-skill-bodies.ts),
* which ships in supabase/migrations and runs on deploy.
*
* Discovery + frontmatter parsing live in scripts/lib/atom-discovery.ts so this
* and the generator can never drift on which skills count as atoms.
*
* Usage:
* npx tsx scripts/seed-agent-atom-registry.ts # apply
* npx tsx scripts/seed-agent-atom-registry.ts --dry # print plan only
*/
import { config } from 'dotenv'
config({ path: '.env.local' })
import { createClient } from '@supabase/supabase-js'
import { fileURLToPath } from 'node:url'
import { dirname, relative, join } from 'node:path'
import { discoverAtoms } from './lib/atom-discovery'
const __filename = fileURLToPath(import.meta.url)
const ROOT = dirname(dirname(__filename))
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env.local')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
const dryRun = process.argv.includes('--dry')
async function main() {
console.log(`Scanning ${relative(process.cwd(), join(ROOT, '.claude', 'skills'))}`)
const atoms = await discoverAtoms(ROOT)
if (atoms.length === 0) {
console.log('No atoms discovered.')
return
}
// Map discovery → registry rows. We intentionally OMIT mcp_exposed so that a
// manual kill-switch flip survives a re-seed (the column default applies on
// first insert; ON CONFLICT leaves it untouched). `body` is inlined so runtime
// reads from the DB rather than disk.
const rows = atoms.map((a) => ({
id: a.id,
tier: a.tier,
title: a.title,
description: a.description,
sni_prefixes: a.sni_prefixes,
trigger_signals: a.trigger_signals,
estimated_tokens: a.estimated_tokens,
body_path: a.body_path,
body: a.body,
parent_atom_id: a.parent_atom_id,
version: a.frontmatter_version,
is_active: true,
schema_version: a.schema_version,
}))
console.log(`\nFound ${rows.length} atoms:\n`)
for (const r of rows) {
console.log(` [${r.tier.padEnd(10)}] ${r.id.padEnd(40)} ${r.estimated_tokens.toString().padStart(6)} tokens`)
}
if (dryRun) {
console.log('\n--dry: skipping write.')
return
}
console.log('\nUpserting...')
const { error } = await supabase.from('agent_atom_registry').upsert(rows, { onConflict: 'id' })
if (error) {
console.error('Upsert failed:', error)
process.exit(1)
}
console.log(`Upserted ${rows.length} rows into agent_atom_registry.`)
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
File diff suppressed because it is too large Load Diff
+568
View File
@@ -0,0 +1,568 @@
/**
* Seed script: populate data for export extensions
*
* Creates EU customers, foreign-currency invoices, and journal entries
* so that all 4 export extensions (EU Sales List, VAT Monitor, Intrastat,
* Currency Receivables) have data to display.
*
* Usage: node scripts/seed-export-data.mjs
*/
import { createClient } from '@supabase/supabase-js'
import 'dotenv/config'
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!supabaseUrl || !serviceRoleKey) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY in .env')
process.exit(1)
}
const supabase = createClient(supabaseUrl, serviceRoleKey)
// ── Helpers ─────────────────────────────────────────────────
function round2(n) {
return Math.round(n * 100) / 100
}
function randomId() {
return crypto.randomUUID()
}
function today() {
return new Date().toISOString().split('T')[0]
}
function daysAgo(n) {
const d = new Date()
d.setDate(d.getDate() - n)
return d.toISOString().split('T')[0]
}
function daysFromNow(n) {
const d = new Date()
d.setDate(d.getDate() + n)
return d.toISOString().split('T')[0]
}
// ── Main ────────────────────────────────────────────────────
async function main() {
// 1. Find the user
const { data: { users }, error: usersError } = await supabase.auth.admin.listUsers()
if (usersError) {
console.error('Failed to list users:', usersError.message)
process.exit(1)
}
if (users.length === 0) {
console.error('No users found. Please sign up first.')
process.exit(1)
}
const user = users[0]
const userId = user.id
console.log(`Using user: ${user.email} (${userId})`)
// 2. Ensure company settings exist
const { data: company, error: companyError } = await supabase
.from('company_settings')
.select('*')
.eq('user_id', userId)
.single()
if (companyError || !company) {
console.error('No company_settings found. Complete onboarding first.')
process.exit(1)
}
console.log(`Company: ${company.company_name || '(unnamed)'}`)
// 3. Ensure fiscal period exists for current year
const year = new Date().getFullYear()
const periodStart = `${year}-01-01`
const periodEnd = `${year}-12-31`
let { data: fiscalPeriod } = await supabase
.from('fiscal_periods')
.select('*')
.eq('user_id', userId)
.lte('period_start', today())
.gte('period_end', today())
.limit(1)
.single()
if (!fiscalPeriod) {
console.log(`Creating fiscal period for ${year}...`)
const { data: newPeriod, error: periodError } = await supabase
.from('fiscal_periods')
.insert({
id: randomId(),
user_id: userId,
name: `Räkenskapsår ${year}`,
period_start: periodStart,
period_end: periodEnd,
is_closed: false,
})
.select()
.single()
if (periodError) {
console.error('Failed to create fiscal period:', periodError.message)
process.exit(1)
}
fiscalPeriod = newPeriod
}
console.log(`Fiscal period: ${fiscalPeriod.name} (${fiscalPeriod.period_start} to ${fiscalPeriod.period_end})`)
// 4. Create EU customers
const customers = [
{
id: randomId(),
user_id: userId,
name: 'TechHaus GmbH',
customer_type: 'eu_business',
email: 'billing@techhaus.de',
country: 'Germany',
org_number: 'HRB 12345',
vat_number: 'DE123456789',
vat_number_validated: true,
default_payment_terms: 30,
address_line1: 'Friedrichstraße 42',
postal_code: '10117',
city: 'Berlin',
},
{
id: randomId(),
user_id: userId,
name: 'Suomen Softworks Oy',
customer_type: 'eu_business',
email: 'invoices@suomensoftworks.fi',
country: 'Finland',
org_number: '1234567-8',
vat_number: 'FI12345678',
vat_number_validated: true,
default_payment_terms: 30,
address_line1: 'Mannerheimintie 10',
postal_code: '00100',
city: 'Helsinki',
},
{
id: randomId(),
user_id: userId,
name: 'Oranje Logistics B.V.',
customer_type: 'eu_business',
email: 'finance@oranjelogistics.nl',
country: 'Netherlands',
org_number: 'KvK 87654321',
vat_number: 'NL123456789B01',
vat_number_validated: true,
default_payment_terms: 14,
address_line1: 'Keizersgracht 120',
postal_code: '1015 AA',
city: 'Amsterdam',
},
]
console.log('\nCreating 3 EU customers...')
const { error: custError } = await supabase.from('customers').insert(customers)
if (custError) {
console.error('Failed to create customers:', custError.message)
process.exit(1)
}
for (const c of customers) {
console.log(` ✓ ${c.name} (${c.vat_number})`)
}
// 5. Create invoices
// Mix of: SEK reverse_charge, EUR reverse_charge, USD
const nextNum = company.next_invoice_number || 1
const invoices = [
// Invoice 1: SEK reverse_charge to German customer (for EU Sales List: goods)
{
id: randomId(),
user_id: userId,
customer_id: customers[0].id,
invoice_number: `${company.invoice_prefix || 'F'}${String(nextNum).padStart(4, '0')}`,
invoice_date: daysAgo(20),
due_date: daysFromNow(10),
status: 'sent',
currency: 'SEK',
document_type: 'invoice',
vat_treatment: 'reverse_charge',
vat_rate: 0,
subtotal: 85000,
subtotal_sek: 85000,
vat_amount: 0,
vat_amount_sek: 0,
total: 85000,
total_sek: 85000,
exchange_rate: null,
moms_ruta: '35',
reverse_charge_text: 'Reverse charge: VAT to be accounted for by the recipient according to article 196 Council Directive 2006/112/EC',
},
// Invoice 2: EUR reverse_charge to Finnish customer (for EU Sales List: services + Currency Receivables)
{
id: randomId(),
user_id: userId,
customer_id: customers[1].id,
invoice_number: `${company.invoice_prefix || 'F'}${String(nextNum + 1).padStart(4, '0')}`,
invoice_date: daysAgo(15),
due_date: daysFromNow(15),
status: 'sent',
currency: 'EUR',
document_type: 'invoice',
vat_treatment: 'reverse_charge',
vat_rate: 0,
subtotal: 5000,
subtotal_sek: 57500,
vat_amount: 0,
vat_amount_sek: 0,
total: 5000,
total_sek: 57500,
exchange_rate: 11.50,
exchange_rate_date: daysAgo(15),
moms_ruta: '39',
reverse_charge_text: 'Reverse charge: VAT to be accounted for by the recipient according to article 196 Council Directive 2006/112/EC',
},
// Invoice 3: EUR reverse_charge to Dutch customer: goods (for EU Sales List + Currency Receivables)
{
id: randomId(),
user_id: userId,
customer_id: customers[2].id,
invoice_number: `${company.invoice_prefix || 'F'}${String(nextNum + 2).padStart(4, '0')}`,
invoice_date: daysAgo(10),
due_date: daysFromNow(20),
status: 'sent',
currency: 'EUR',
document_type: 'invoice',
vat_treatment: 'reverse_charge',
vat_rate: 0,
subtotal: 12000,
subtotal_sek: 138000,
vat_amount: 0,
vat_amount_sek: 0,
total: 12000,
total_sek: 138000,
exchange_rate: 11.50,
exchange_rate_date: daysAgo(10),
moms_ruta: '35',
reverse_charge_text: 'Reverse charge: VAT to be accounted for by the recipient according to article 196 Council Directive 2006/112/EC',
},
// Invoice 4: USD to Dutch customer: overdue (for Currency Receivables)
{
id: randomId(),
user_id: userId,
customer_id: customers[2].id,
invoice_number: `${company.invoice_prefix || 'F'}${String(nextNum + 3).padStart(4, '0')}`,
invoice_date: daysAgo(45),
due_date: daysAgo(15),
status: 'overdue',
currency: 'USD',
document_type: 'invoice',
vat_treatment: 'reverse_charge',
vat_rate: 0,
subtotal: 8500,
subtotal_sek: 91800,
vat_amount: 0,
vat_amount_sek: 0,
total: 8500,
total_sek: 91800,
exchange_rate: 10.80,
exchange_rate_date: daysAgo(45),
moms_ruta: '35',
reverse_charge_text: 'Reverse charge: VAT to be accounted for by the recipient according to article 196 Council Directive 2006/112/EC',
},
// Invoice 5: Paid SEK reverse_charge to Finnish customer (for EU Sales List history)
{
id: randomId(),
user_id: userId,
customer_id: customers[1].id,
invoice_number: `${company.invoice_prefix || 'F'}${String(nextNum + 4).padStart(4, '0')}`,
invoice_date: daysAgo(60),
due_date: daysAgo(30),
status: 'paid',
currency: 'SEK',
document_type: 'invoice',
vat_treatment: 'reverse_charge',
vat_rate: 0,
subtotal: 42000,
subtotal_sek: 42000,
vat_amount: 0,
vat_amount_sek: 0,
total: 42000,
total_sek: 42000,
exchange_rate: null,
moms_ruta: '39',
reverse_charge_text: 'Reverse charge: VAT to be accounted for by the recipient according to article 196 Council Directive 2006/112/EC',
},
]
console.log('\nCreating 5 invoices...')
const { error: invError } = await supabase.from('invoices').insert(invoices)
if (invError) {
console.error('Failed to create invoices:', invError.message)
process.exit(1)
}
for (const inv of invoices) {
const cust = customers.find(c => c.id === inv.customer_id)
console.log(` ✓ ${inv.invoice_number}, ${cust.name}, ${inv.currency} ${inv.total} (${inv.status})`)
}
// 6. Create invoice items
const invoiceItems = [
// Invoice 1 items (SEK goods to Germany)
{ id: randomId(), invoice_id: invoices[0].id, description: 'Industrial sensors batch', quantity: 50, unit: 'st', unit_price: 1200, line_total: 60000, sort_order: 1 },
{ id: randomId(), invoice_id: invoices[0].id, description: 'Installation & calibration', quantity: 10, unit: 'tim', unit_price: 2500, line_total: 25000, sort_order: 2 },
// Invoice 2 items (EUR services to Finland)
{ id: randomId(), invoice_id: invoices[1].id, description: 'Software consulting', quantity: 40, unit: 'tim', unit_price: 125, line_total: 5000, sort_order: 1 },
// Invoice 3 items (EUR goods to Netherlands)
{ id: randomId(), invoice_id: invoices[2].id, description: 'Steel components CN:72163100', quantity: 200, unit: 'st', unit_price: 45, line_total: 9000, sort_order: 1 },
{ id: randomId(), invoice_id: invoices[2].id, description: 'Aluminium fittings CN:76169990', quantity: 100, unit: 'st', unit_price: 30, line_total: 3000, sort_order: 2 },
// Invoice 4 items (USD goods to Netherlands)
{ id: randomId(), invoice_id: invoices[3].id, description: 'Custom machine parts', quantity: 25, unit: 'st', unit_price: 340, line_total: 8500, sort_order: 1 },
// Invoice 5 items (SEK services to Finland)
{ id: randomId(), invoice_id: invoices[4].id, description: 'IT architecture review', quantity: 24, unit: 'tim', unit_price: 1750, line_total: 42000, sort_order: 1 },
]
const { error: itemsError } = await supabase.from('invoice_items').insert(invoiceItems)
if (itemsError) {
console.error('Failed to create invoice items:', itemsError.message)
process.exit(1)
}
console.log(` ✓ ${invoiceItems.length} invoice items created`)
// Update next_invoice_number
await supabase
.from('company_settings')
.update({ next_invoice_number: nextNum + 5 })
.eq('user_id', userId)
// 7. Create journal entries for the invoices (reverse charge: debit 1510, credit 3305/3308)
// These are needed for VAT Monitor and EU Sales List cross-check
const journalEntries = []
const journalLines = []
// Get current max voucher number
const { data: maxVoucher } = await supabase
.from('journal_entries')
.select('voucher_number')
.eq('user_id', userId)
.order('voucher_number', { ascending: false })
.limit(1)
.single()
let voucherNum = (maxVoucher?.voucher_number || 0) + 1
for (const inv of invoices) {
const entryId = randomId()
const totalSEK = inv.total_sek
// Determine revenue account: goods = 3305, services = 3308
// moms_ruta 35 = goods, 39 = services
const revenueAccount = inv.moms_ruta === '35' ? '3305' : '3308'
journalEntries.push({
id: entryId,
user_id: userId,
fiscal_period_id: fiscalPeriod.id,
voucher_number: voucherNum++,
voucher_series: 'A',
entry_date: inv.invoice_date,
description: `Faktura ${inv.invoice_number}: ${customers.find(c => c.id === inv.customer_id).name}`,
source_type: 'invoice_created',
source_id: inv.id,
// Draft until the lines are inserted; posted in the batch UPDATE below.
status: 'draft',
committed_at: new Date().toISOString(),
})
// Debit 1510 (accounts receivable)
journalLines.push({
id: randomId(),
journal_entry_id: entryId,
account_number: '1510',
debit_amount: round2(totalSEK),
credit_amount: 0,
currency: inv.currency,
amount_in_currency: inv.currency !== 'SEK' ? inv.total : null,
exchange_rate: inv.exchange_rate,
line_description: `Kundfordran ${inv.invoice_number}`,
sort_order: 1,
})
// Credit revenue account (3305 export goods or 3308 EU services)
journalLines.push({
id: randomId(),
journal_entry_id: entryId,
account_number: revenueAccount,
debit_amount: 0,
credit_amount: round2(totalSEK),
currency: 'SEK',
line_description: `Intäkt ${inv.invoice_number}`,
sort_order: 2,
})
}
// Add a payment entry for invoice 5 (paid): debit 1930, credit 1510
const paymentEntryId = randomId()
journalEntries.push({
id: paymentEntryId,
user_id: userId,
fiscal_period_id: fiscalPeriod.id,
voucher_number: voucherNum++,
voucher_series: 'A',
entry_date: daysAgo(25),
description: `Betalning ${invoices[4].invoice_number}: Suomen Softworks Oy`,
source_type: 'invoice_paid',
source_id: invoices[4].id,
status: 'draft',
committed_at: new Date().toISOString(),
})
journalLines.push({
id: randomId(),
journal_entry_id: paymentEntryId,
account_number: '1930',
debit_amount: 42000,
credit_amount: 0,
currency: 'SEK',
line_description: `Inbetalning ${invoices[4].invoice_number}`,
sort_order: 1,
})
journalLines.push({
id: randomId(),
journal_entry_id: paymentEntryId,
account_number: '1510',
debit_amount: 0,
credit_amount: 42000,
currency: 'SEK',
line_description: `Reglering ${invoices[4].invoice_number}`,
sort_order: 2,
})
// Add a small FX gain entry (for Currency Receivables realized FX)
const fxEntryId = randomId()
journalEntries.push({
id: fxEntryId,
user_id: userId,
fiscal_period_id: fiscalPeriod.id,
voucher_number: voucherNum++,
voucher_series: 'A',
entry_date: daysAgo(25),
description: 'Kursdifferens vid betalning',
source_type: 'invoice_paid',
status: 'draft',
committed_at: new Date().toISOString(),
})
journalLines.push({
id: randomId(),
journal_entry_id: fxEntryId,
account_number: '1930',
debit_amount: 450,
credit_amount: 0,
currency: 'SEK',
line_description: 'Valutavinst',
sort_order: 1,
})
journalLines.push({
id: randomId(),
journal_entry_id: fxEntryId,
account_number: '3960',
debit_amount: 0,
credit_amount: 450,
currency: 'SEK',
line_description: 'Valutakursvinst',
sort_order: 2,
})
// Headers go in as drafts and are posted after the lines land: supabase-js
// autocommits each request, and check_balance_on_posted_insert rejects a
// posted header whose transaction carries no lines. The draft-to-posted
// UPDATE fires check_balance_on_post against the finished verifikat instead.
console.log(`\nCreating ${journalEntries.length} journal entries...`)
const { error: jeError } = await supabase.from('journal_entries').insert(journalEntries)
if (jeError) {
console.error('Failed to create journal entries:', jeError.message)
process.exit(1)
}
const { error: jlError } = await supabase.from('journal_entry_lines').insert(journalLines)
if (jlError) {
console.error('Failed to create journal entry lines:', jlError.message)
console.error('Cleaning up journal entries...')
await supabase.from('journal_entries').delete().in('id', journalEntries.map(e => e.id))
process.exit(1)
}
const { error: postError } = await supabase
.from('journal_entries')
.update({ status: 'posted' })
.in('id', journalEntries.map(e => e.id))
if (postError) {
console.error('Failed to post journal entries:', postError.message)
process.exit(1)
}
for (const je of journalEntries) {
console.log(` ✓ A${je.voucher_number}: ${je.description}`)
}
// 8. Add Intrastat product metadata via extension_data
console.log('\nCreating Intrastat product registry...')
const extensionId = 'export/intrastat'
const extensionData = [
{
user_id: userId,
extension_id: extensionId,
key: 'product:STEEL-COMP',
value: { description: 'Steel components', cn_code: '72163100', net_weight_kg: 2.4, country_of_origin: 'SE' },
},
{
user_id: userId,
extension_id: extensionId,
key: 'product:ALU-FIT',
value: { description: 'Aluminium fittings', cn_code: '76169990', net_weight_kg: 0.8, country_of_origin: 'SE' },
},
{
user_id: userId,
extension_id: extensionId,
key: 'product:IND-SENSOR',
value: { description: 'Industrial sensors', cn_code: '90318080', net_weight_kg: 0.35, country_of_origin: 'SE' },
},
]
const { error: extError } = await supabase.from('extension_data').insert(extensionData)
if (extError) {
console.error('Warning: Failed to create extension_data (Intrastat products):', extError.message)
console.log(' (Export extensions will still work, just Intrastat product registry will be empty)')
} else {
for (const ed of extensionData) {
console.log(` ✓ ${ed.key}: ${ed.value.description} (CN: ${ed.value.cn_code})`)
}
}
// Done
console.log('\n════════════════════════════════════════════════════')
console.log(' Seed data created successfully!')
console.log('════════════════════════════════════════════════════')
console.log('\nYou should now see data in:')
console.log(' • Periodisk sammanställning (EU Sales List): 3 EU customers, 5 invoices')
console.log(' • Exportmoms-monitor (VAT Monitor): journal entries on 3305/3308')
console.log(' • Intrastat: goods invoices + product registry')
console.log(' • Valutafordringar (Currency Receivables): 3 open EUR/USD invoices')
console.log('\nSelect the current month/quarter to see the data.')
}
main().catch(err => {
console.error('Unexpected error:', err)
process.exit(1)
})
+317
View File
@@ -0,0 +1,317 @@
/**
* Seed VAT test data for Skatteverket momsdeklaration kontrollera testing.
*
* Creates a fiscal period (if missing) and a set of balanced posted journal
* entries that exercise every Ruta the calculator populates. After running
* this you can call /api/extensions/ext/skatteverket/declaration/validate
* for the same period and Skatteverket should return a non-empty
* kontrollresultat covering the full SKV 4700 form.
*
* Usage:
* npx tsx scripts/seed-skv-test-data.ts <COMPANY_ID> <YEAR> <MONTH>
*
* Example:
* npx tsx scripts/seed-skv-test-data.ts 11111111-aaaa-bbbb-cccc-222222222222 2026 3
*
* Idempotency: every entry's description is prefixed `[SKV-TEST]` so reruns
* are easy to identify and clean up:
* delete from journal_entries
* where company_id = '<id>' and description like '[SKV-TEST]%';
*
* Requires: SUPABASE_SERVICE_ROLE_KEY in .env.local (already set if you've
* been running the dev server).
*/
import { createClient } from '@supabase/supabase-js'
import { config } from 'dotenv'
import { resolve } from 'node:path'
config({ path: resolve(process.cwd(), '.env.local') })
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL
const SERVICE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY
if (!SUPABASE_URL || !SERVICE_KEY) {
console.error('Missing NEXT_PUBLIC_SUPABASE_URL or SUPABASE_SERVICE_ROLE_KEY')
process.exit(1)
}
const supabase = createClient(SUPABASE_URL, SERVICE_KEY, {
auth: { persistSession: false },
})
interface Line {
account: string
debit?: number
credit?: number
description?: string
}
interface Scenario {
description: string
lines: Line[]
expectedRutor: string
}
const [, , companyIdArg, yearArg, monthArg] = process.argv
if (!companyIdArg || !yearArg || !monthArg) {
console.error('Usage: npx tsx scripts/seed-skv-test-data.ts <COMPANY_ID> <YEAR> <MONTH>')
process.exit(1)
}
const companyId = companyIdArg
const year = Number(yearArg)
const month = Number(monthArg)
const entryDate = `${year}-${String(month).padStart(2, '0')}-15`
// Scenarios chosen to populate every Ruta the calculator now reads from the
// ledger. Each one balances debits = credits.
const scenarios: Scenario[] = [
{
description: 'Domestic invoice, 25% rate (Acme Konsult AB)',
expectedRutor: 'Ruta 05 + 10',
lines: [
{ account: '1510', debit: 12500, description: 'Kundfordran' },
{ account: '3001', credit: 10000, description: 'Försäljning 25%' },
{ account: '2611', credit: 2500, description: 'Utgående moms 25%' },
],
},
{
description: 'Domestic invoice, 12% rate (restaurang)',
expectedRutor: 'Ruta 05 + 11',
lines: [
{ account: '1510', debit: 11200 },
{ account: '3002', credit: 10000, description: 'Försäljning 12%' },
{ account: '2621', credit: 1200, description: 'Utgående moms 12%' },
],
},
{
description: 'Domestic invoice, 6% rate (transport)',
expectedRutor: 'Ruta 05 + 12',
lines: [
{ account: '1510', debit: 10600 },
{ account: '3003', credit: 10000, description: 'Försäljning 6%' },
{ account: '2631', credit: 600, description: 'Utgående moms 6%' },
],
},
{
description: 'EU services purchase (German consulting)',
expectedRutor: 'Ruta 21 + 30 + 48',
lines: [
{ account: '4535', debit: 5000, description: 'Inköp tjänster EU 25%' },
{ account: '2645', debit: 1250, description: 'Beräknad ingående moms' },
{ account: '2614', credit: 1250, description: 'Utgående moms omv. skattskyldighet' },
{ account: '2440', credit: 5000, description: 'Leverantörsskuld' },
],
},
{
description: 'Non-EU services purchase (Anthropic)',
expectedRutor: 'Ruta 22 + 30 + 48',
lines: [
{ account: '4531', debit: 3000, description: 'Inköp tjänster utanför EU' },
{ account: '2645', debit: 750 },
{ account: '2614', credit: 750 },
{ account: '2440', credit: 3000 },
],
},
{
description: 'Domestic mobile reverse charge (electronics >100k)',
expectedRutor: 'Ruta 23 + 30 + 48',
lines: [
{ account: '4415', debit: 100000, description: 'Inköp mobiler omv. skattskyldighet' },
{ account: '2647', debit: 25000, description: 'Ingående moms omv. skattskyldighet i SE' },
{ account: '2614', credit: 25000 },
{ account: '2440', credit: 100000 },
],
},
{
description: 'Domestic services reverse charge (byggtjänst)',
expectedRutor: 'Ruta 24 + 30 + 48',
lines: [
{ account: '4425', debit: 8000, description: 'Inköp byggtjänster omv.' },
{ account: '2647', debit: 2000 },
{ account: '2614', credit: 2000 },
{ account: '2440', credit: 8000 },
],
},
{
description: 'EU goods sale (intra-community supply, zero-rated)',
expectedRutor: 'Ruta 35',
lines: [
{ account: '1510', debit: 4000 },
{ account: '3108', credit: 4000, description: 'Varuförsäljning till EU' },
],
},
{
description: 'Export sale (non-EU)',
expectedRutor: 'Ruta 36',
lines: [
{ account: '1510', debit: 5000 },
{ account: '3105', credit: 5000, description: 'Varuförsäljning export' },
],
},
{
description: 'EU services sale (B2B, buyer self-assesses)',
expectedRutor: 'Ruta 39',
lines: [
{ account: '1510', debit: 8000 },
{ account: '3308', credit: 8000, description: 'Tjänsteförsäljning till EU' },
],
},
{
description: 'Office supplies purchase (regular input VAT)',
expectedRutor: 'Ruta 48',
lines: [
{ account: '5410', debit: 800, description: 'Förbrukningsinventarier' },
{ account: '2641', debit: 200, description: 'Ingående moms 25%' },
{ account: '2440', credit: 1000 },
],
},
{
description: 'Import from non-EU (paid via Tullverket, VAT to SKV)',
expectedRutor: 'Ruta 50 + 60 + 48',
lines: [
{ account: '4545', debit: 10000, description: 'Beskattningsunderlag import 25%' },
{ account: '2641', debit: 2500, description: 'Ingående moms import' },
{ account: '2615', credit: 2500, description: 'Utgående moms import 25%' },
{ account: '2440', credit: 10000 },
],
},
{
description: 'Owner uttag in kind (only for EF): comment out if AB',
expectedRutor: 'Ruta 06 + 10',
lines: [
{ account: '2013', debit: 1250, description: 'Egna uttag' },
{ account: '3401', credit: 1000, description: 'Uttag 25%' },
{ account: '2612', credit: 250, description: 'Utgående moms uttag 25%' },
],
},
]
async function ensureFiscalPeriod(): Promise<{ id: string; userId: string }> {
const periodStart = `${year}-01-01`
const periodEnd = `${year}-12-31`
// Reuse if it exists
const { data: existing, error: fetchErr } = await supabase
.from('fiscal_periods')
.select('id, user_id')
.eq('company_id', companyId)
.eq('period_start', periodStart)
.eq('period_end', periodEnd)
.maybeSingle()
if (fetchErr) throw new Error(`fiscal_periods select: ${fetchErr.message}`)
if (existing) return { id: existing.id, userId: existing.user_id }
const { data: ownerRow, error: ownerErr } = await supabase
.from('company_members')
.select('user_id')
.eq('company_id', companyId)
.eq('role', 'owner')
.limit(1)
.single()
if (ownerErr || !ownerRow) {
throw new Error(`No owner found for company ${companyId}: ${ownerErr?.message}`)
}
const userId = ownerRow.user_id
const { data: created, error: insertErr } = await supabase
.from('fiscal_periods')
.insert({
company_id: companyId,
user_id: userId,
name: `${year} (SKV test)`,
period_start: periodStart,
period_end: periodEnd,
})
.select('id, user_id')
.single()
if (insertErr || !created) throw new Error(`fiscal_periods insert: ${insertErr?.message}`)
console.log(`Created fiscal period ${created.id} (${periodStart} → ${periodEnd})`)
return { id: created.id, userId: created.user_id }
}
async function seedScenario(
scenario: Scenario,
fiscalPeriodId: string,
userId: string,
): Promise<void> {
const { description, lines } = scenario
const debit = lines.reduce((s, l) => s + (l.debit ?? 0), 0)
const credit = lines.reduce((s, l) => s + (l.credit ?? 0), 0)
if (Math.abs(debit - credit) > 0.005) {
throw new Error(`Scenario unbalanced: ${description} (D=${debit}, C=${credit})`)
}
// 1. Insert draft entry
const { data: draft, error: draftErr } = await supabase
.from('journal_entries')
.insert({
company_id: companyId,
user_id: userId,
fiscal_period_id: fiscalPeriodId,
voucher_number: 0, // placeholder; commit_journal_entry assigns the real one
voucher_series: 'A',
entry_date: entryDate,
description: `[SKV-TEST] ${description}`,
source_type: 'manual',
status: 'draft',
})
.select('id')
.single()
if (draftErr || !draft) throw new Error(`draft insert (${description}): ${draftErr?.message}`)
// 2. Insert lines
const lineRows = lines.map((l, idx) => ({
journal_entry_id: draft.id,
account_number: l.account,
debit_amount: l.debit ?? 0,
credit_amount: l.credit ?? 0,
line_description: l.description ?? null,
sort_order: idx,
}))
const { error: linesErr } = await supabase.from('journal_entry_lines').insert(lineRows)
if (linesErr) throw new Error(`lines insert (${description}): ${linesErr.message}`)
// 3. Commit via RPC (assigns sequential voucher number, sets status='posted').
// commit_method enum: 'user_accept' | 'bulk_accept' | 'timing_ceiling' |
// 'migration' | 'legacy'. 'migration' is the closest fit for synthetic test
// data inserted outside the normal user-accept flow.
const { data: voucherRow, error: commitErr } = await supabase.rpc('commit_journal_entry', {
p_company_id: companyId,
p_entry_id: draft.id,
p_commit_method: 'migration',
p_rubric_version: null,
})
if (commitErr) throw new Error(`commit (${description}): ${commitErr.message}`)
const voucherNumber = Array.isArray(voucherRow) ? voucherRow[0]?.voucher_number : voucherRow?.voucher_number
console.log(` ✓ A${voucherNumber} ${description.padEnd(60)} → ${scenario.expectedRutor}`)
}
async function main() {
console.log(`Seeding SKV test data for company ${companyId}, period ${year}-${String(month).padStart(2, '0')}`)
const { id: fiscalPeriodId, userId } = await ensureFiscalPeriod()
for (const scenario of scenarios) {
try {
await seedScenario(scenario, fiscalPeriodId, userId)
} catch (err) {
console.error(` ✗ ${scenario.description}: ${(err as Error).message}`)
}
}
console.log('\nDone. Verify in /reports → Momsdeklaration:')
console.log(` • Period: ${year}-${String(month).padStart(2, '0')}`)
console.log(' • Expected non-zero Rutor: 05, 06, 10, 11, 12, 21, 22, 23, 24, 30, 35, 36, 39, 48, 50, 60')
console.log('\nClean up later with:')
console.log(` delete from journal_entry_lines where journal_entry_id in (select id from journal_entries where company_id = '${companyId}' and description like '[SKV-TEST]%');`)
console.log(` delete from journal_entries where company_id = '${companyId}' and description like '[SKV-TEST]%';`)
}
main().catch(err => {
console.error(err)
process.exit(1)
})
@@ -0,0 +1,223 @@
import { describe, it, expect } from 'vitest'
import { execFileSync, type ExecFileSyncOptions } from 'node:child_process'
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
// The self-host backup/restore scripts are shipped product (docs/SOVEREIGN.md
// tells operators to run them on a schedule), so at least their syntax and
// their refusal paths are checked in CI. Behaviour against a real bucket and
// database is exercised by the operator's first dry run per the runbook.
const DIR = join(__dirname, '..')
const SCRIPTS = ['backup.sh', 'restore.sh']
// A clean environment: none of the BACKUP_* / RESTORE_* variables, so the
// scripts' own guards are what runs. Typed as ProcessEnv (the repo's
// augmentation makes NODE_ENV a required key) so it can be spread into the
// per-test environments below without a cast.
const BARE_PROCESS_ENV: NodeJS.ProcessEnv = { PATH: process.env.PATH ?? '', NODE_ENV: 'test' }
const BARE_ENV: ExecFileSyncOptions = { env: BARE_PROCESS_ENV, stdio: 'pipe' }
// Enough environment for backup.sh to get past its required-variable guards;
// nothing here is ever contacted (the tests below stop before any tool runs).
const BACKUP_REQUIRED_ENV: NodeJS.ProcessEnv = {
...BARE_PROCESS_ENV,
BACKUP_DATABASE_URL: 'postgresql://postgres:x@127.0.0.1:1/postgres',
BACKUP_S3_ENDPOINT: 'https://s3.invalid',
BACKUP_S3_BUCKET: 'bucket',
AWS_ACCESS_KEY_ID: 'key',
AWS_SECRET_ACCESS_KEY: 'secret',
}
function runBash(args: string[], env: NodeJS.ProcessEnv): { status: number; stderr: string } {
try {
execFileSync('bash', args, { env, stdio: 'pipe' })
return { status: 0, stderr: '' }
} catch (err) {
return {
status: (err as { status?: number }).status ?? -1,
stderr: String((err as { stderr?: Buffer }).stderr ?? ''),
}
}
}
describe('self-host shell scripts', () => {
for (const name of SCRIPTS) {
it(`${name} parses under bash -n`, () => {
expect(() => execFileSync('bash', ['-n', join(DIR, name)])).not.toThrow()
})
it(`${name} sets strict mode and a private umask`, () => {
const src = readFileSync(join(DIR, name), 'utf8')
expect(src).toContain('set -euo pipefail')
expect(src).toContain('umask 077')
})
it(`${name} keeps ACLs in the dump (no --no-privileges), drops ownership (--no-owner), uses the ACL manifest`, () => {
// The migrations REVOKE hardened SECURITY DEFINER RPCs from anon and
// authenticated (REVOKE ... FROM PUBLIC, anon; GRANT ... TO service_role).
// --no-privileges would strip that from the dump and a restored self-host
// would re-expose those RPCs over PostgREST; --no-owner is what lets the
// dump land in a stack whose roles were created by that stack.
const src = readFileSync(join(DIR, name), 'utf8')
// The invocation lines, not the comments that explain them.
const commands = src.split('\n').filter((line) => /^\s*pg_(dump|restore) /.test(line))
expect(commands.length).toBeGreaterThan(0)
for (const command of commands) {
expect(command).toContain('--no-owner')
expect(command).not.toContain('--no-privileges')
}
expect(src).toContain('acl-manifest.sql')
})
}
it('restore.sh neutralizes the restoring role default privileges before pg_restore and diffs the ACL manifest after it', () => {
// pg_dump writes ACLs as a diff against acldefault(), so the dump never
// says "REVOKE FROM anon"; a Supabase target's ALTER DEFAULT PRIVILEGES
// would hand anon EXECUTE back to every restored function unless those
// defaults are removed first. The manifest diff is what makes a restore
// that lost the hardening fail instead of pass silently.
const src = readFileSync(join(DIR, 'restore.sh'), 'utf8')
const neutralize = src.indexOf('ALTER DEFAULT PRIVILEGES')
const restore = src.indexOf('pg_restore --clean')
const check = src.lastIndexOf('acl-manifest.sql')
expect(neutralize).toBeGreaterThan(-1)
expect(restore).toBeGreaterThan(neutralize)
expect(check).toBeGreaterThan(restore)
// Exactly the built-in default, not "no grants at all": a global entry
// that took EXECUTE on functions away from PUBLIC gets it back, otherwise
// a function the source never touched (NULL ACL, PUBLIC-executable)
// restores as non-executable.
expect(src).toContain('TO PUBLIC')
expect(src).toContain('ACL MISMATCH')
})
it('acl-manifest.sql states every PostgREST role for functions, relations and sequences in public', () => {
const sql = readFileSync(join(DIR, 'acl-manifest.sql'), 'utf8')
for (const role of ['anon', 'authenticated', 'service_role']) {
expect(sql).toContain(`has_function_privilege('${role}'`)
expect(sql).toContain(`has_table_privilege('${role}'`)
// Sequences have their own privilege set (USAGE, SELECT, UPDATE) that
// has_table_privilege does not see; a restore that hands anon nextval
// on an id sequence must show up in the diff like any other object.
for (const privilege of ['USAGE', 'SELECT', 'UPDATE']) {
expect(sql).toContain(`has_sequence_privilege('${role}', c.oid, '${privilege}')`)
}
}
expect(sql).toMatch(/relkind = 'S'/)
expect(sql).toContain("'public'::regnamespace")
// Byte-identical output on both sides regardless of database collation.
expect(sql).toContain('collate "C"')
// restore.sh shows the mismatching lines by object kind; a sequence line
// must not be filtered out of that excerpt.
const restore = readFileSync(join(DIR, 'restore.sh'), 'utf8')
expect(restore).toContain('(function|relation|sequence) ')
})
it('restore.sh does not require RESTORE_DATABASE_URL for the db-config pass (RESTORE_SKIP_DATABASE=1)', () => {
let status = 0
let stderr = ''
try {
execFileSync('bash', [join(DIR, 'restore.sh'), 'nightly-20260101T000000Z', '--yes'], {
...BARE_ENV,
env: { ...BARE_PROCESS_ENV, RESTORE_SKIP_DATABASE: '1' },
})
} catch (err) {
status = (err as { status?: number }).status ?? -1
stderr = String((err as { stderr?: Buffer }).stderr ?? '')
}
expect(status).not.toBe(0)
expect(stderr).not.toContain('RESTORE_DATABASE_URL')
expect(stderr).toContain('BACKUP_S3_ENDPOINT is required')
})
it('backup.sh refuses to run without the required environment', () => {
let status = 0
try {
execFileSync('bash', [join(DIR, 'backup.sh')], BARE_ENV)
} catch (err) {
status = (err as { status?: number }).status ?? -1
}
expect(status).not.toBe(0)
})
it('backup.sh refuses a quiesce hook without its resume hook, and the reverse, before running anything', () => {
// A quiesce command with no resume command would leave the operator's app
// stopped after every run; refusing up front is the only safe answer.
const quiesceOnly = runBash([join(DIR, 'backup.sh')], { ...BACKUP_REQUIRED_ENV, BACKUP_QUIESCE_CMD: 'true' })
expect(quiesceOnly.status).toBe(2)
expect(quiesceOnly.stderr).toContain('BACKUP_QUIESCE_CMD is set but BACKUP_RESUME_CMD is not')
const resumeOnly = runBash([join(DIR, 'backup.sh')], { ...BACKUP_REQUIRED_ENV, BACKUP_RESUME_CMD: 'true' })
expect(resumeOnly.status).toBe(2)
expect(resumeOnly.stderr).toContain('BACKUP_RESUME_CMD is set but BACKUP_QUIESCE_CMD is not')
})
it('backup.sh runs the resume hook when the quiesce hook fails part-way', () => {
// `docker compose stop app cron` can stop `app` and then fail on `cron`;
// set -e ends the script right there, and the EXIT trap must still run
// the resume hook or the operator's app stays down after a failed backup.
// The required tools are stubbed on PATH so the script reaches the hook;
// none of them is ever executed because the hook fails first.
const stubs = mkdtempSync(join(tmpdir(), 'accounted-backup-stubs-'))
try {
for (const tool of ['pg_dump', 'psql', 'tar', 'gzip', 'aws']) {
const stub = join(stubs, tool)
writeFileSync(stub, '#!/bin/sh\nexit 0\n')
chmodSync(stub, 0o755)
}
const marker = join(stubs, 'resumed')
const result = runBash([join(DIR, 'backup.sh')], {
...BACKUP_REQUIRED_ENV,
PATH: `${stubs}:${process.env.PATH ?? ''}`,
BACKUP_QUIESCE_CMD: 'exit 3',
BACKUP_RESUME_CMD: `touch "${marker}"`,
})
expect(result.status).not.toBe(0)
expect(existsSync(marker)).toBe(true)
} finally {
rmSync(stubs, { recursive: true, force: true })
}
})
it('backup.sh marks the quiesce attempt before running the hook, not after it succeeds', () => {
const src = readFileSync(join(DIR, 'backup.sh'), 'utf8')
const attempted = src.indexOf('QUIESCE_ATTEMPTED=1')
const hook = src.indexOf('bash -c "$BACKUP_QUIESCE_CMD"')
expect(attempted).toBeGreaterThan(-1)
expect(hook).toBeGreaterThan(attempted)
})
it('restore.sh rejects a backup name with shell metacharacters before doing anything', () => {
let status = 0
let stderr = ''
try {
execFileSync('bash', [join(DIR, 'restore.sh'), 'nightly-2026; rm -rf /', '--yes'], BARE_ENV)
} catch (err) {
status = (err as { status?: number }).status ?? -1
stderr = String((err as { stderr?: Buffer }).stderr ?? '')
}
expect(status).toBe(2)
expect(stderr).toContain('invalid backup name')
})
it('neither script runs a shell inside the docker helper container', () => {
// `docker run ... sh -c "<string with ${NAME}>"` would let a crafted
// backup name execute inside the container; tar must get the path as a
// direct argument. (The operator-supplied quiesce/resume hooks in
// backup.sh run through bash on the host by design; they are config.)
const dockerShell = /docker run[^\n]*(\\\n[^\n]*)*?\bsh -c/
expect(readFileSync(join(DIR, 'restore.sh'), 'utf8')).not.toMatch(dockerShell)
expect(readFileSync(join(DIR, 'backup.sh'), 'utf8')).not.toMatch(dockerShell)
})
it('restore.sh refuses to run without --yes', () => {
let status = 0
let stderr = ''
try {
execFileSync('bash', [join(DIR, 'restore.sh'), 'nightly-20260101T000000Z'], BARE_ENV)
} catch (err) {
status = (err as { status?: number }).status ?? -1
stderr = String((err as { stderr?: Buffer }).stderr ?? '')
}
expect(status).toBe(2)
expect(stderr).toContain('--yes')
})
})
+67
View File
@@ -0,0 +1,67 @@
-- ACL manifest of the application schema: one line per function, relation
-- and sequence in `public`, stating what each PostgREST role (anon,
-- authenticated, service_role) may do with it. backup.sh writes this next to the dump;
-- restore.sh runs it again after pg_restore and diffs the two. Any difference
-- means the restored database exposes an object differently than the source
-- did, which is exactly the failure a restore drill must surface (the
-- migrations lock SECURITY DEFINER RPCs away from anon/authenticated, and a
-- restore that loses those REVOKEs is silent otherwise).
--
-- Deliberately owner-independent: --no-owner changes who owns the objects,
-- never what these three roles may do. Run with psql -X -A -t -q. Ordered
-- under the C collation so a source and a target with different database
-- collations still produce byte-identical files.
set search_path = public, pg_catalog;
select line from (
select 'function ' || p.oid::regprocedure::text
|| ' anon=' || has_function_privilege('anon', p.oid, 'EXECUTE')::int
|| ' authenticated=' || has_function_privilege('authenticated', p.oid, 'EXECUTE')::int
|| ' service_role=' || has_function_privilege('service_role', p.oid, 'EXECUTE')::int as line
from pg_proc p
where p.pronamespace = 'public'::regnamespace
union all
-- Four digits per role: SELECT INSERT UPDATE DELETE.
select 'relation ' || c.oid::regclass::text
|| ' anon='
|| has_table_privilege('anon', c.oid, 'SELECT')::int
|| has_table_privilege('anon', c.oid, 'INSERT')::int
|| has_table_privilege('anon', c.oid, 'UPDATE')::int
|| has_table_privilege('anon', c.oid, 'DELETE')::int
|| ' authenticated='
|| has_table_privilege('authenticated', c.oid, 'SELECT')::int
|| has_table_privilege('authenticated', c.oid, 'INSERT')::int
|| has_table_privilege('authenticated', c.oid, 'UPDATE')::int
|| has_table_privilege('authenticated', c.oid, 'DELETE')::int
|| ' service_role='
|| has_table_privilege('service_role', c.oid, 'SELECT')::int
|| has_table_privilege('service_role', c.oid, 'INSERT')::int
|| has_table_privilege('service_role', c.oid, 'UPDATE')::int
|| has_table_privilege('service_role', c.oid, 'DELETE')::int
from pg_class c
where c.relnamespace = 'public'::regnamespace
and c.relkind in ('r', 'p', 'v', 'm')
union all
-- Sequences carry their own privilege set (has_table_privilege does not
-- see them), and the Supabase defaults grant them to every PostgREST role:
-- a restore that changes them (nextval on an id sequence for anon, say)
-- must fail the diff like any other object. Three digits per role:
-- USAGE SELECT UPDATE.
select 'sequence ' || c.oid::regclass::text
|| ' anon='
|| has_sequence_privilege('anon', c.oid, 'USAGE')::int
|| has_sequence_privilege('anon', c.oid, 'SELECT')::int
|| has_sequence_privilege('anon', c.oid, 'UPDATE')::int
|| ' authenticated='
|| has_sequence_privilege('authenticated', c.oid, 'USAGE')::int
|| has_sequence_privilege('authenticated', c.oid, 'SELECT')::int
|| has_sequence_privilege('authenticated', c.oid, 'UPDATE')::int
|| ' service_role='
|| has_sequence_privilege('service_role', c.oid, 'USAGE')::int
|| has_sequence_privilege('service_role', c.oid, 'SELECT')::int
|| has_sequence_privilege('service_role', c.oid, 'UPDATE')::int
from pg_class c
where c.relnamespace = 'public'::regnamespace
and c.relkind = 'S'
) t
order by line collate "C";
+199
View File
@@ -0,0 +1,199 @@
#!/usr/bin/env bash
# Accounted self-host backup: logical database dump + document storage, shipped
# to an S3-compatible bucket, optionally under S3 Object Lock (WORM).
#
# Self-hosted Supabase has no managed backups or PITR, and Swedish bookkeeping
# law (BFL 7 kap) requires the ledger and its underlag (receipts, invoices) to
# be kept for seven years after the end of the fiscal year. This script is the
# minimum that satisfies both: one restorable dump per run, and the documents
# bucket alongside it, on storage the operator controls. See
# docs/SOVEREIGN.md ("Backup and restore") for the runbook and the cron line.
#
# Requirements on the host running it: bash, pg_dump and psql (matching the
# server's major version), tar, gzip, sha256sum (or shasum), AWS CLI v2 (works
# against any S3-compatible endpoint: Safespring, GleSYS, Elastx via
# --endpoint-url).
#
# Environment (required unless marked optional):
# BACKUP_DATABASE_URL postgresql://postgres:<password>@<host>:<port>/postgres
# Use the Supabase session-mode pooler port or the
# db container's port; never a public address.
# BACKUP_S3_ENDPOINT e.g. https://s3.sto2.safedc.net
# BACKUP_S3_BUCKET bucket name (create it with Object Lock enabled:
# Object Lock can only be turned on at creation)
# AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY credentials for that bucket
# BACKUP_S3_REGION optional, default us-east-1 (what most S3-compatible
# endpoints expect for SigV4; Elastx requires it)
# BACKUP_S3_PREFIX optional, default "accounted"
# BACKUP_STORAGE_DIR optional: path of the Supabase storage volume
# (STORAGE_BACKEND=file: <supabase-dir>/volumes/storage).
# Omit when storage-api writes straight to S3; then
# back that bucket up bucket-to-bucket instead.
# BACKUP_DB_CONFIG_VOLUME optional: name of the Supabase `db-config` Docker
# volume (e.g. supabase_db-config). It holds the
# pgsodium root key; a dump restored without it
# cannot decrypt Vault secrets. Requires docker.
# BACKUP_OBJECT_LOCK_DAYS optional: when set, every uploaded object gets
# COMPLIANCE-mode retention for this many days
# (immutable even for the bucket owner). Suggested:
# a long value (>= 2600, seven years plus margin)
# for the yearly post-bokslut run, a short one for
# nightly runs, or unset and rely on bucket default
# retention. COMPLIANCE retention cannot be shortened.
# BACKUP_WORKDIR optional: scratch directory, default mktemp.
# BACKUP_LABEL optional: name fragment, default "nightly".
# BACKUP_QUIESCE_CMD optional: a command run BEFORE the dump and
# BACKUP_RESUME_CMD AFTER the upload (also on failure), e.g.
# "docker compose -f /opt/accounted/docker-compose.yml stop app cron"
# and the matching "start". The database dump and
# the storage tar are taken one after the other;
# an upload landing between them leaves a document
# row without its file (or the reverse) in that
# set. Stopping the app for the window makes the
# set consistent; recommended for the yearly
# archive run, optional for nightly runs where the
# next night's set covers the gap. Set both or
# neither: one without the other is refused
# before anything runs.
#
# Prints a short progress log on stdout; exit status is non-zero on any
# failure, which is what a cron wrapper should alert on.
set -euo pipefail
# Dumps, archives and manifests are the whole ledger: never world-readable,
# whatever the operator's default umask is.
umask 077
: "${BACKUP_DATABASE_URL:?BACKUP_DATABASE_URL is required}"
: "${BACKUP_S3_ENDPOINT:?BACKUP_S3_ENDPOINT is required}"
: "${BACKUP_S3_BUCKET:?BACKUP_S3_BUCKET is required}"
: "${AWS_ACCESS_KEY_ID:?AWS_ACCESS_KEY_ID is required}"
: "${AWS_SECRET_ACCESS_KEY:?AWS_SECRET_ACCESS_KEY is required}"
# The hooks are a pair. A quiesce command without its resume command would
# leave the operator's app stopped after every run (and a resume command
# without a quiesce command would start containers nobody stopped), so refuse
# before anything else happens.
if [ -n "${BACKUP_QUIESCE_CMD:-}" ] && [ -z "${BACKUP_RESUME_CMD:-}" ]; then
echo "backup: BACKUP_QUIESCE_CMD is set but BACKUP_RESUME_CMD is not; set both or neither" >&2
exit 2
fi
if [ -n "${BACKUP_RESUME_CMD:-}" ] && [ -z "${BACKUP_QUIESCE_CMD:-}" ]; then
echo "backup: BACKUP_RESUME_CMD is set but BACKUP_QUIESCE_CMD is not; set both or neither" >&2
exit 2
fi
export AWS_DEFAULT_REGION="${BACKUP_S3_REGION:-us-east-1}"
PREFIX="${BACKUP_S3_PREFIX:-accounted}"
LABEL="${BACKUP_LABEL:-nightly}"
# The label becomes S3 keys, file names and container arguments: keep it to
# the same character set restore.sh accepts.
if ! [[ "$LABEL" =~ ^[A-Za-z0-9._-]+$ ]]; then
echo "backup: invalid BACKUP_LABEL \"$LABEL\" (letters, digits, . _ - only)" >&2
exit 2
fi
STAMP="$(date -u +%Y%m%dT%H%M%SZ)"
NAME="${LABEL}-${STAMP}"
for bin in pg_dump psql tar gzip aws; do
command -v "$bin" >/dev/null 2>&1 || { echo "backup: missing required tool: $bin" >&2; exit 2; }
done
if command -v sha256sum >/dev/null 2>&1; then SHA="sha256sum"; else SHA="shasum -a 256"; fi
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
WORK="${BACKUP_WORKDIR:-$(mktemp -d "${TMPDIR:-/tmp}/accounted-backup.XXXXXX")}"
mkdir -p "$WORK"
chmod 700 "$WORK"
# Flipped to 1 BEFORE the quiesce hook runs, not after it succeeds: a hook
# that stops one container and then fails ends the script (set -e), and the
# EXIT trap must still run the resume hook, otherwise a failed backup leaves
# the app stopped.
QUIESCE_ATTEMPTED=0
cleanup() {
if [ "$QUIESCE_ATTEMPTED" = 1 ] && [ -n "${BACKUP_RESUME_CMD:-}" ]; then
bash -c "$BACKUP_RESUME_CMD" || echo "backup: BACKUP_RESUME_CMD failed; check that the app is running" >&2
fi
[ -z "${BACKUP_WORKDIR:-}" ] && rm -rf "$WORK"
}
trap cleanup EXIT
upload() {
# upload <local-file> <key>
local file="$1" key="$2"
local args=(s3api put-object --endpoint-url "$BACKUP_S3_ENDPOINT" --bucket "$BACKUP_S3_BUCKET" --key "$key" --body "$file")
if [ -n "${BACKUP_OBJECT_LOCK_DAYS:-}" ]; then
local until
# GNU date and BSD date differ; try GNU first.
until="$(date -u -d "+${BACKUP_OBJECT_LOCK_DAYS} days" +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || date -u -v+"${BACKUP_OBJECT_LOCK_DAYS}"d +%Y-%m-%dT%H:%M:%SZ)"
args+=(--object-lock-mode COMPLIANCE --object-lock-retain-until-date "$until")
fi
aws "${args[@]}" >/dev/null
echo "backup: uploaded s3://${BACKUP_S3_BUCKET}/${key}"
}
echo "backup: starting ${NAME}"
if [ -n "${BACKUP_QUIESCE_CMD:-}" ]; then
QUIESCE_ATTEMPTED=1
bash -c "$BACKUP_QUIESCE_CMD"
echo "backup: application quiesced for a consistent set"
fi
# 1. Database: custom-format dump (compressed, selective restore possible).
# --no-owner so it restores into a fresh Supabase stack whose roles were
# created by that stack, not by us. ACLs (GRANT/REVOKE) are kept (no
# --no-privileges): the migrations lock SECURITY DEFINER RPCs away from
# anon/authenticated, and the dump is the only record of that. Keeping them
# is necessary but not sufficient: pg_dump writes ACLs as a diff against
# PostgreSQL's built-in defaults, so a role-specific "REVOKE ... FROM anon"
# is not in the dump at all, and a Supabase target's ALTER DEFAULT
# PRIVILEGES would hand anon/authenticated EXECUTE back to every restored
# function. restore.sh neutralizes those default privileges before
# pg_restore and then diffs the ACL manifest taken in step 1b against the
# restored database, so a re-exposure fails the restore instead of hiding.
DB_FILE="${WORK}/${NAME}.db.dump"
pg_dump --format=custom --no-owner --file "$DB_FILE" "$BACKUP_DATABASE_URL"
echo "backup: database dump $(du -h "$DB_FILE" | cut -f1)"
# 1b. ACL manifest: what anon/authenticated/service_role may do with every
# function, relation and sequence in public
# (scripts/self-host/acl-manifest.sql).
# restore.sh compares the restored database against this file.
ACL_FILE="${WORK}/${NAME}.acl.txt"
psql -X -A -t -q -v ON_ERROR_STOP=1 -f "${SCRIPT_DIR}/acl-manifest.sql" "$BACKUP_DATABASE_URL" > "$ACL_FILE"
echo "backup: ACL manifest $(wc -l < "$ACL_FILE" | tr -d ' ') objects"
# 2. Documents (the BFL underlag) when storage-api uses the file backend.
STORAGE_FILE=""
if [ -n "${BACKUP_STORAGE_DIR:-}" ]; then
if [ ! -d "$BACKUP_STORAGE_DIR" ]; then
echo "backup: BACKUP_STORAGE_DIR does not exist: $BACKUP_STORAGE_DIR" >&2
exit 2
fi
STORAGE_FILE="${WORK}/${NAME}.storage.tar.gz"
tar -C "$BACKUP_STORAGE_DIR" -czf "$STORAGE_FILE" .
echo "backup: storage archive $(du -h "$STORAGE_FILE" | cut -f1)"
fi
# 3. Supabase db-config volume (pgsodium root key), optional but strongly
# recommended: without it Vault-encrypted columns in the dump are
# unreadable after a restore.
DBCONFIG_FILE=""
if [ -n "${BACKUP_DB_CONFIG_VOLUME:-}" ]; then
command -v docker >/dev/null 2>&1 || { echo "backup: BACKUP_DB_CONFIG_VOLUME set but docker not found" >&2; exit 2; }
DBCONFIG_FILE="${WORK}/${NAME}.db-config.tar.gz"
docker run --rm -v "${BACKUP_DB_CONFIG_VOLUME}:/src:ro" -v "${WORK}:/out" alpine:3 \
tar -C /src -czf "/out/$(basename "$DBCONFIG_FILE")" .
echo "backup: db-config archive $(du -h "$DBCONFIG_FILE" | cut -f1)"
fi
# 4. Checksums, then upload everything under one prefix.
MANIFEST="${WORK}/${NAME}.sha256"
( cd "$WORK" && $SHA "$(basename "$DB_FILE")" "$(basename "$ACL_FILE")" \
${STORAGE_FILE:+"$(basename "$STORAGE_FILE")"} \
${DBCONFIG_FILE:+"$(basename "$DBCONFIG_FILE")"} > "$MANIFEST" )
for f in "$DB_FILE" "$ACL_FILE" ${STORAGE_FILE:+"$STORAGE_FILE"} ${DBCONFIG_FILE:+"$DBCONFIG_FILE"} "$MANIFEST"; do
upload "$f" "${PREFIX}/${NAME}/$(basename "$f")"
done
echo "backup: done ${NAME}"
+258
View File
@@ -0,0 +1,258 @@
#!/usr/bin/env bash
# Accounted self-host restore: the counterpart of backup.sh.
#
# Restores one backup set (database dump, ACL manifest, optional storage
# archive, optional db-config archive) from the S3-compatible bucket into a
# target Postgres and storage directory. Destructive by design: it drops and
# recreates the objects in the target database (--clean). Run it against a
# FRESH Supabase stack, or one you are prepared to overwrite, and pass --yes.
#
# Usage:
# scripts/self-host/restore.sh <backup-name> --yes
# e.g. scripts/self-host/restore.sh nightly-20260820T020000Z --yes
#
# Environment:
# RESTORE_DATABASE_URL target postgresql://... (required unless
# RESTORE_SKIP_DATABASE=1). Connect as the stack's
# `postgres` role, the one the migrations ran as.
# BACKUP_S3_ENDPOINT, BACKUP_S3_BUCKET, AWS_ACCESS_KEY_ID,
# AWS_SECRET_ACCESS_KEY, BACKUP_S3_REGION, BACKUP_S3_PREFIX as in backup.sh
# RESTORE_STORAGE_DIR optional: where to unpack the storage archive
# (the new stack's <supabase-dir>/volumes/storage).
# RESTORE_DB_CONFIG_VOLUME optional: Docker volume name to unpack the
# db-config archive into. The database container
# must be STOPPED for this and started afterwards,
# so do it as its own pass with
# RESTORE_SKIP_DATABASE=1 (see the runbook).
# RESTORE_SKIP_DATABASE optional: set to 1 to skip pg_restore and the ACL
# check (the db-config pass above).
# RESTORE_WORKDIR optional scratch dir.
# RESTORE_TOLERATE_ERRORS optional: set to 1 to continue when pg_restore
# reports errors (exit status 1). Default is to
# STOP before the ACL check and storage and show
# the error log. On a Supabase target these
# classes are routine (the stack already owns and
# grants those objects; the restoring role is not a
# superuser): "already exists"; "does not exist"
# from DROP ... IF EXISTS of policies/triggers on
# a fresh target; "must be member of role
# supabase_*", "must be owner of ...", "permission
# denied ..." and "grant options cannot be granted
# back" on GRANT/REVOKE/ALTER for objects in
# auth, storage, realtime, extensions, cron, vault,
# graphql*, pgbouncer. Errors on public.* objects
# are NOT expected: investigate before continuing.
# A partial restore must be a decision you take
# knowingly, not a default.
#
# Order that works: (1) bring up a fresh Supabase stack with the SAME
# JWT_SECRET / ANON_KEY / SERVICE_ROLE_KEY as the old one (or re-issue keys to
# your Accounted .env), (2) stop the database container and restore db-config
# (RESTORE_DB_CONFIG_VOLUME + RESTORE_SKIP_DATABASE=1), (3) start it and
# restore the database and storage (second run), (4) restart storage-api,
# (5) run `scripts/smoke-ai-provider.ts`-style checks and log in.
set -euo pipefail
# Downloaded dumps are the whole ledger: never world-readable.
umask 077
NAME="${1:-}"
CONFIRM="${2:-}"
if [ -z "$NAME" ] || [ "$NAME" = "--help" ]; then
sed -n '2,/^set -euo pipefail/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//'
exit 2
fi
if [ "$CONFIRM" != "--yes" ]; then
echo "restore: refusing to run without --yes (this overwrites the target database)" >&2
exit 2
fi
# The name becomes S3 keys, local file names and container arguments: only
# the characters backup.sh can produce are accepted, so nothing shell- or
# path-like ever reaches those places.
if ! [[ "$NAME" =~ ^[A-Za-z0-9._-]+$ ]]; then
echo "restore: invalid backup name \"$NAME\" (expected e.g. nightly-20260820T020000Z)" >&2
exit 2
fi
SKIP_DB="${RESTORE_SKIP_DATABASE:-0}"
if [ "$SKIP_DB" != "1" ]; then
: "${RESTORE_DATABASE_URL:?RESTORE_DATABASE_URL is required (or set RESTORE_SKIP_DATABASE=1)}"
fi
: "${BACKUP_S3_ENDPOINT:?BACKUP_S3_ENDPOINT is required}"
: "${BACKUP_S3_BUCKET:?BACKUP_S3_BUCKET is required}"
: "${AWS_ACCESS_KEY_ID:?AWS_ACCESS_KEY_ID is required}"
: "${AWS_SECRET_ACCESS_KEY:?AWS_SECRET_ACCESS_KEY is required}"
export AWS_DEFAULT_REGION="${BACKUP_S3_REGION:-us-east-1}"
PREFIX="${BACKUP_S3_PREFIX:-accounted}"
for bin in pg_restore psql tar gzip aws; do
command -v "$bin" >/dev/null 2>&1 || { echo "restore: missing required tool: $bin" >&2; exit 2; }
done
if command -v sha256sum >/dev/null 2>&1; then SHA="sha256sum"; else SHA="shasum -a 256"; fi
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
WORK="${RESTORE_WORKDIR:-$(mktemp -d "${TMPDIR:-/tmp}/accounted-restore.XXXXXX")}"
mkdir -p "$WORK"
chmod 700 "$WORK"
cleanup() { [ -z "${RESTORE_WORKDIR:-}" ] && rm -rf "$WORK"; }
trap cleanup EXIT
fetch() {
# fetch <file-name> -> downloads into $WORK, returns 1 when the key is absent
aws s3api get-object --endpoint-url "$BACKUP_S3_ENDPOINT" --bucket "$BACKUP_S3_BUCKET" \
--key "${PREFIX}/${NAME}/$1" "${WORK}/$1" >/dev/null 2>&1
}
echo "restore: fetching ${NAME} from s3://${BACKUP_S3_BUCKET}/${PREFIX}/${NAME}/"
fetch "${NAME}.sha256" || { echo "restore: no manifest found for ${NAME}" >&2; exit 1; }
fetch "${NAME}.db.dump" || { echo "restore: database dump missing" >&2; exit 1; }
HAVE_ACL=0; fetch "${NAME}.acl.txt" && HAVE_ACL=1
HAVE_STORAGE=0; fetch "${NAME}.storage.tar.gz" && HAVE_STORAGE=1
HAVE_DBCONFIG=0; fetch "${NAME}.db-config.tar.gz" && HAVE_DBCONFIG=1
# Verify every file the manifest lists before touching anything.
( cd "$WORK" && $SHA -c "${NAME}.sha256" )
echo "restore: checksums verified"
if [ "$HAVE_DBCONFIG" = 1 ] && [ -n "${RESTORE_DB_CONFIG_VOLUME:-}" ]; then
command -v docker >/dev/null 2>&1 || { echo "restore: RESTORE_DB_CONFIG_VOLUME set but docker not found" >&2; exit 2; }
# No shell inside the container: tar receives the path as an argument.
docker run --rm -v "${RESTORE_DB_CONFIG_VOLUME}:/dst" -v "${WORK}:/in:ro" alpine:3 \
tar -C /dst -xzf "/in/${NAME}.db-config.tar.gz"
echo "restore: db-config volume restored (${RESTORE_DB_CONFIG_VOLUME}); start the database container before the database pass"
fi
if [ "$SKIP_DB" = "1" ]; then
echo "restore: RESTORE_SKIP_DATABASE=1, database not touched"
else
# Log only what follows the last "@" (host, port, database): never the
# credentials part of the URL.
echo "restore: restoring database into ${RESTORE_DATABASE_URL##*@} (objects are dropped and recreated)"
# The dump carries ACLs, but pg_dump writes them as a diff against
# PostgreSQL's built-in defaults (acldefault), never against the target's
# ALTER DEFAULT PRIVILEGES. A Supabase stack grants anon, authenticated and
# service_role on every new function/table/sequence the postgres role
# creates, so restoring into it would hand anon/authenticated EXECUTE back
# to every hardened RPC: the dump only says "REVOKE FROM PUBLIC; GRANT TO
# service_role" and never "REVOKE FROM anon". Put the restoring role's
# default privileges back to exactly PostgreSQL's built-in defaults right
# before pg_restore (revoke every added grantee; PUBLIC back to EXECUTE on
# functions and USAGE on types, nothing on tables and sequences), so the
# dump's explicit GRANT/REVOKE statements apply to the base they were
# computed against. The dump's own last section (DEFAULT ACL) re-creates
# the stack's default privileges once every object exists, so migrations
# applied later still get the grants PostgREST needs; the check after
# pg_restore confirms that.
psql -X -q -v ON_ERROR_STOP=1 "$RESTORE_DATABASE_URL" <<'SQL'
DO $$
DECLARE
r record;
scope text;
kind text;
BEGIN
-- 1. Every grantee the stack added to the defaults (anon, authenticated,
-- service_role, PUBLIC on tables, ...): revoke it. The role's own
-- privileges are left alone. IN SCHEMA entries are additions on top of
-- the built-in default and PostgreSQL drops them once they are empty.
FOR r IN
SELECT DISTINCT d.defaclobjtype AS objtype, d.defaclnamespace AS nsp, a.grantee
FROM pg_default_acl d
CROSS JOIN LATERAL aclexplode(d.defaclacl) a
WHERE d.defaclrole = (SELECT oid FROM pg_roles WHERE rolname = current_user)
AND d.defaclnamespace IN (0, 'public'::regnamespace)
AND a.grantee <> d.defaclrole
LOOP
scope := CASE WHEN r.nsp = 0 THEN '' ELSE 'IN SCHEMA public' END;
kind := CASE r.objtype
WHEN 'r' THEN 'TABLES' WHEN 'S' THEN 'SEQUENCES' WHEN 'f' THEN 'FUNCTIONS'
WHEN 'T' THEN 'TYPES' WHEN 'n' THEN 'SCHEMAS'
END;
EXECUTE format('ALTER DEFAULT PRIVILEGES %s REVOKE ALL ON %s FROM %s', scope, kind,
CASE WHEN r.grantee = 0 THEN 'PUBLIC' ELSE r.grantee::regrole::text END);
END LOOP;
-- 2. A global entry (no schema) REPLACES the built-in default, so one that
-- took EXECUTE on functions or USAGE on types away from PUBLIC would
-- still be in force. Grant those back: an entry equal to the built-in
-- default is removed by PostgreSQL itself. (The Supabase image has no
-- global entries for postgres; an operator-hardened stack may.)
FOR r IN
SELECT DISTINCT d.defaclobjtype AS objtype
FROM pg_default_acl d
WHERE d.defaclrole = (SELECT oid FROM pg_roles WHERE rolname = current_user)
AND d.defaclnamespace = 0
AND d.defaclobjtype IN ('f', 'T')
LOOP
EXECUTE format('ALTER DEFAULT PRIVILEGES GRANT %s ON %s TO PUBLIC',
CASE WHEN r.objtype = 'f' THEN 'EXECUTE' ELSE 'USAGE' END,
CASE WHEN r.objtype = 'f' THEN 'FUNCTIONS' ELSE 'TYPES' END);
END LOOP;
END $$;
SQL
echo "restore: default privileges of the restoring role neutralized for the restore"
# --clean --if-exists: drop objects before recreating them. --no-owner: the
# fresh stack owns its roles. No --no-privileges: the ACLs are the point.
# pg_restore exits 1 when any restore operation failed; on a Supabase
# target the classes listed in the header are routine (stack-owned objects
# the non-superuser postgres role may not drop or re-grant), a missing
# table or an error on a public.* object is not, and the script cannot
# tell which. Default: stop here, show the log, restore nothing further.
# The operator reads the log and re-runs with RESTORE_TOLERATE_ERRORS=1 if
# the errors are the expected kind.
PG_LOG="${WORK}/pg_restore.log"
set +e
pg_restore --clean --if-exists --no-owner --dbname "$RESTORE_DATABASE_URL" \
"${WORK}/${NAME}.db.dump" 2> "$PG_LOG"
rc=$?
set -e
if [ "$rc" -ne 0 ]; then
echo "restore: pg_restore exited with status ${rc}; errors reported:" >&2
grep -E "^pg_restore: (error|warning)" "$PG_LOG" | head -40 >&2 || tail -40 "$PG_LOG" >&2
if [ "$rc" -ne 1 ] || [ "${RESTORE_TOLERATE_ERRORS:-0}" != "1" ]; then
echo "restore: stopping before the ACL check and storage (the db-config volume, if requested, is already unpacked). Inspect the errors above; if they are the expected kind on a Supabase target (\"already exists\", \"does not exist\" from DROP IF EXISTS, and permission/ownership/grant errors on objects in auth, storage, realtime, extensions, cron, vault, graphql*, pgbouncer, never on public.*), re-run with RESTORE_TOLERATE_ERRORS=1." >&2
exit "$rc"
fi
echo "restore: continuing despite pg_restore errors (RESTORE_TOLERATE_ERRORS=1)" >&2
fi
echo "restore: database restored (pg_restore status ${rc})"
# ACL check: the manifest backup.sh took from the source must match the
# restored database line for line. A difference means an object is now
# reachable by a PostgREST role that could not reach it before (or the
# reverse), and there is no override: fix the ACLs by hand from the diff
# (GRANT/REVOKE on the named objects) or re-run into a fresh stack.
if [ "$HAVE_ACL" = 1 ]; then
ACL_NOW="${WORK}/${NAME}.acl.restored.txt"
psql -X -A -t -q -v ON_ERROR_STOP=1 -f "${SCRIPT_DIR}/acl-manifest.sql" "$RESTORE_DATABASE_URL" > "$ACL_NOW"
if ! diff -u "${WORK}/${NAME}.acl.txt" "$ACL_NOW" > "${WORK}/acl.diff"; then
echo "restore: ACL MISMATCH between the source manifest (-) and the restored database (+):" >&2
grep -E "^[-+](function|relation|sequence) " "${WORK}/acl.diff" | head -60 >&2
echo "restore: stopping before storage. The restored database does not grant the PostgREST roles what the source did; see the lines above." >&2
exit 1
fi
echo "restore: ACL manifest verified ($(wc -l < "$ACL_NOW" | tr -d ' ') objects, identical to the source)"
else
echo "restore: no ACL manifest in this set (older backup.sh); ACL verification skipped" >&2
fi
# The dump's DEFAULT ACL section should have re-created the stack's default
# privileges for the restoring role. If it did not (restored as a role the
# source never set defaults for), later migrations would create objects the
# PostgREST roles cannot reach: say so.
DEFACL_ROWS="$(psql -X -A -t -q -v ON_ERROR_STOP=1 "$RESTORE_DATABASE_URL" \
-c "select count(*) from pg_default_acl where defaclrole = (select oid from pg_roles where rolname = current_user) and defaclnamespace = 'public'::regnamespace")"
if [ "${DEFACL_ROWS:-0}" = "0" ]; then
echo "restore: WARNING: no default privileges for the restoring role in schema public after the restore; re-apply the stack's ALTER DEFAULT PRIVILEGES (tables, functions, sequences to anon, authenticated, service_role) before running further migrations" >&2
fi
fi
if [ "$HAVE_STORAGE" = 1 ] && [ -n "${RESTORE_STORAGE_DIR:-}" ]; then
mkdir -p "$RESTORE_STORAGE_DIR"
tar -C "$RESTORE_STORAGE_DIR" -xzf "${WORK}/${NAME}.storage.tar.gz"
echo "restore: storage unpacked into ${RESTORE_STORAGE_DIR} (restart storage-api)"
elif [ "$HAVE_STORAGE" = 1 ]; then
echo "restore: storage archive present but RESTORE_STORAGE_DIR unset; skipped"
fi
echo "restore: done ${NAME}"

Some files were not shown because too many files have changed in this diff Show More