Skip to content

feat(idv): auto-upgrade Bridge-KYC-approved accounts to Level 2 - #80

Merged
islandbitcoin merged 2 commits into
mainfrom
feat/bridge-kyc-auto-l2
Oct 6, 2026
Merged

islandbitcoin merged 2 commits into
mainfrom
feat/bridge-kyc-auto-l2

Conversation

@islandbitcoin

@islandbitcoin islandbitcoin commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Policy (2026-10-06): a Flash account whose Bridge customer has passed KYC has a verified identity, and that is enough for Level 2. This adds a scheduled job that applies the policy through the existing reviewer path. Nothing goes around it.

Every 15 minutes, while the new switch is on, for each Flash account that is linked to a Bridge customer, where that customer's live Bridge status is active, the customer is an individual, and the account is active and at Level 0 or 1:

  1. File an Account Upgrade Request (Level 2) using Bridge's verified legal name and the account's phone and email.
  2. Create an ID Verification with identity_source = bridge_kyc. It keeps Bridge's id, status, updated_at and endorsements only, the same slice flash keeps.
  3. Approve it with the existing approve_upgrade_request(..., reason_code="APPROVE_BRIDGE_KYC"). That creates or reuses the ERP Customer by mobile number (flash's required erpParty for Level 2), stamps the decision, mirrors it to the ID Verification and writes the upgrade_approved ledger event. Flash posts the level change to the ops feed.

The request has no address. The request doctype's address block is no longer reqd; it's required for Level 3 in the desk form only (mandatory_depends_on). Frappe enforces reqd on every save, not just the insert, so a required address made every later save of these requests fail:

  • the approval's own save, after flash had already moved;
  • reviewer Reject;
  • the Account Hub phone sync;
  • flash closing a user's pending requests.

lnflash/flash#517 now requires a complete address on every request a customer submits. _create_erp_records already treats address and bank as optional, and with no address_title the Customer is created as an Individual. Why each request exists goes on the ID Verification's internal reviewer_note; the reviewer pages show support_note as "Rejection Reason".

Switch

  • New Auto-Upgrade Bridge KYC to Level 2 check in ID Verification Settings. It defaults to off, so the deploy is inert.
  • It can't be saved on while Bridge KYC Satisfies Identity is off.
  • Changes are ledgered (idv_settings_changed) like the other settings.
  • The first run after switching it on is the backfill, at most 25 upgrades per run.

Skipped (with a reason), never upgraded

  • A pending request (a reviewer owns it).
  • A business customer (KYB is the L3 conversation).
  • One Bridge customer linked to several accounts.
  • An inactive account.
  • A customer id that's missing at Bridge.
  • No username, phone or legal name.
  • Already upgraded (per the live re-check).
  • An existing erpParty that approval wouldn't find by mobile number. Approving would repoint erpParty at a new Customer and strand the old one's bank accounts, so that case goes to a human.
  • An upgrade that failed less than 24h ago (retried after that).
  • Nothing is ever downgraded.

Failures

Nothing is committed until the approval commits. If an approval fails:

  • Flash is still below L2 (the common case): everything is rolled back. That leaves no request the user never filed (the app would show it as pending and it couldn't be dismissed) and no orphan Customer. The account is retried after 24h.
  • Flash moved, or can't be read: the records are kept, because flash's erpParty may name the new Customer. The request stays Pending for a reviewer, who can now approve or reject it.

Each failure writes an Error Log row, after the rollback and committed on its own. A run that upgraded or failed anything prints a summary line to stdout, using an explicit setLevel because frappe.logger() drops INFO on the cluster.

preview_bridge_kyc_upgrades() (whitelisted, admin-only, read-only) lists candidates and skips. See docs/bridge-kyc-auto-upgrade.md.

Rollout

  1. Deploy fix: prepare for Bridge KYC Level 2 upgrades; proxy-addr 2.0.8 flash#517 first. The app's LatestAccountUpgradeRequest selects Address's non-null fields, so without it an address-less request errors on the upgrade screen. It also takes over requiring a complete address on customer requests, which this PR stops ERP doing.
  2. Release this (v-tag, GHCR, bump, make erp). Migrate adds the setting and the scheduled job.
  3. Preview: bench --site erp.flashapp.me execute admin_panel.api.bridge_kyc_upgrade.preview_bridge_kyc_upgrades
  4. Turn the switch on. Needs the scheduler worker running, the same one the daily ledger anchor uses.

Companion PRs (ship before switching this on)

Tests

  • test_bridge_kyc_upgrade.py (49 tests). The eligibility rules are tested pure. The job runs against FakeFrappe with the real approve_upgrade_request, and the fixture now enforces each doctype's reqd fields on insert and save, read from the doctype JSON. Restoring the old reqd flags fails 10 tests.
  • Covered:
    • the switches;
    • the end-to-end approval;
    • the live re-check;
    • the erpParty guard;
    • failure before flash moves: no trace, then the 24h backoff;
    • failure after flash moved, and flash unreadable: records kept;
    • the Error Log surviving a later rollback;
    • the per-run cap;
    • logging with an ERROR-level logger;
    • the preview not writing anything.
  • Settings and contract tests: the new guard, the toggle being ledgered, the field default, the exact scheduler entries, and the request address being required only for L3 in the form.
  • Mutation-checked: each safeguard has a test that goes red without it (rollback vs commit on both failure paths, the Error Log commit, the backoff, unreadable flash, setLevel, the reason code, the erpParty guard).
  • Full suite: 862 passed. ruff 0.8.1 (the pre-commit pin) is clean.

🤖 Generated with Claude Code

Dread and others added 2 commits October 6, 2026 14:23
Policy (2026-10-06): a Flash account whose Bridge customer has passed KYC
has a verified identity, and that is enough for Level 2.

A cron job (every 15 min) finds Bridge-linked accounts whose live Bridge
status is `active`, individual customers only, at Level 0/1 and active,
and for each files an Account Upgrade Request plus an ID Verification
(identity_source = bridge_kyc) and approves it through the existing
approve_upgrade_request with APPROVE_BRIDGE_KYC. That approval creates or
reuses the ERP Customer (flash's required erpParty), stamps the decision,
mirrors it to the ID Verification and writes the ledger event, so nothing
bypasses the reviewer path. The request has no address, so it is saved
with ignore_mandatory; _create_erp_records already treats address and
bank as optional.

- New switch "Auto-Upgrade Bridge KYC to Level 2" in ID Verification
  Settings, default off; refused unless Bridge KYC Satisfies Identity is
  on; changes are ledgered like the other settings.
- Skips with reasons: pending request (reviewer owns it), business
  customer, one Bridge customer on several accounts, inactive account,
  missing at Bridge, no username/phone/legal name, already upgraded, and
  an existing erpParty the approval would not find by mobile number
  (approving would repoint erpParty and strand its bank accounts).
- At most 25 upgrades per run; the first run is the backfill.
- A failed approval stays Pending for a reviewer and is not retried;
  failures write an Error Log row. Summary to stdout when anything changed.
- preview_bridge_kyc_upgrades() shows candidates and skips, read-only.

Needs the flash fix for address-less upgrade requests deployed first: the
app's LatestAccountUpgradeRequest selects Address's non-null fields.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Frappe enforces static reqd fields on every save, not just the insert, and
only a flags.ignore_mandatory on that exact doc object skips it. The job set
the flag on its insert, but approve_upgrade_request reloads the request and
saves it after flash has already moved to Level 2, so every auto-upgrade
would have failed there and then rolled back the Customer flash's erpParty
now names. The same check broke reviewer Reject, request_resubmission, the
Account Hub phone sync (it re-saves every request for the user), and flash
closing a user's pending requests (frappe.client.bulk_update).

- Account Upgrade Request: the address block is no longer reqd. It is
  required for Level 3 in the desk form only (mandatory_depends_on; frappe
  does not enforce that on the server). Flash's API still requires an
  address on every customer-submitted request. ignore_mandatory is gone.
- Nothing is committed before the approval commits. On failure, if flash is
  still below Level 2 everything is rolled back (no request the user never
  filed, which the app would show as pending, and no orphan Customer) and
  the account is retried after 24h. If flash moved or can't be read, the
  records are kept and the request stays Pending for a reviewer.
- The Error Log row is written after that rollback and committed on its own,
  so the next account's rollback can't remove it.
- The "why" note moves to the ID Verification's reviewer_note; the reviewer
  pages render the request's support_note as "Rejection Reason".

Tests: the job fixture now enforces each doctype's reqd fields on insert AND
save, read from the doctype JSON; restoring the old reqd flags fails 10
tests. New failure-path tests (no trace + backoff, flash moved, flash
unreadable, Error Log survives a later rollback). Each new safeguard was
mutation-checked. 862 passed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@islandbitcoin
islandbitcoin merged commit 28bac20 into main Oct 6, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant