Skip to content

fix(main): avoid historical scans during startup sequence repair - #1203

Merged
khaira777 merged 2 commits into
mainfrom
fix/1182-startup-sequence-repair-scan
Oct 17, 2026
Merged

khaira777 merged 2 commits into
mainfrom
fix/1182-startup-sequence-repair-scan

Conversation

@khaira777

Copy link
Copy Markdown
Contributor

Intent

Fix issue #1182: Startup sequence repair scans all historical orders and bills (ARCH-012b)

What Changed

  • Bypassed historical order and bill table scans on ordinary startup boots by tracking repair status with sequence_repair_pending and sequence_repair_completed_at settings flags.
  • Replaced in-memory JavaScript row iteration and regex parsing with in-database SQLite aggregation queries (executeSequenceRepair) that update sequence counters directly, wired into migrations and backup restores.
  • Added performance test suite tests/sequence-repair-performance.test.ts to benchmark sequence repair and startup duration up to 365,000 records, and updated architecture documentation in docs/architecture/data-and-migrations.md.

Risk Assessment

✅ Low: The change cleanly replaces JavaScript heap materialization with atomic SQLite aggregations and introduces constant-time startup guards while preserving sequence monotonicity, data safety, and offline invariants.

Testing

Validated Issue #1182 (ARCH-012b) against the real running product in an isolated environment. Executed all 6 live scenarios (ordinary boot fast-path, set-based atomic SQL repair, adversarial gap/higher counter integrity, malformed suffix filters, self-healing recovery, and end-to-end HTTP API order creation on a running Express server) and scale benchmarks up to 365k rows (10 years of data). Ordinary boot took ~0.05 ms without scanning historical records, heap delta remained strictly bounded (< 11 KB), and all 6 scenarios passed. No UI surface was modified by this backend database change, so visual artifacts were not applicable.

  • Live validation: ✅ go - 6 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Ordinary startup with historical orders skips scanning historical tables (Fast-path) ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Pending startup repair executes SQL aggregation without JS heap bloat ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Gapped order history and higher existing sequence counter integrity ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Non-canonical prefixes and malformed suffixes are ignored ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Self-healing triggers when sequences table is lost but historical orders exist ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Live running Express server and HTTP API order creation end-to-end ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/api-order-creation-response.json
Evidence: Live Verification Log

[2026-10-17T11:50:51.276Z] FloCafe Issue #1182 (ARCH-012b) Live Product Verification Driver >>> Scenario 1: Ordinary startup with historical orders (Fast-path) Seeding 10,000 historical orders and bills across 100 days... Ordinary boot check completed in: 0.066 ms Result: repaired=false, ordersUpdated=0, billsUpdated=0 >>> Scenario 2: Pending startup repair executes SQL aggregation Pending repair completed in: 7.77 ms, heap delta: 9.7 KB >>> Scenario 3: Gapped order history & higher counter integrity (Adversarial) Gapped date (20261017) counter: 5 (expected 5) Higher counter date (20261018) counter: 25 (expected 25) >>> Scenario 4: Non-canonical prefixes & malformed suffixes (Adversarial) Malformed date (20261020) sequence record: undefined Valid date (20261021) sequence current_value: 3 >>> Scenario 5: Self-healing fallback when sequences table is missing Self-healing boot result: repaired=true >>> Scenario 6: Live Express server & HTTP API order creation end-to-end Express server listening on live port: http://127.0.0.1:49662&#10;Sending live HTTP POST http://127.0.0.1:49662/api/orders ... Response status: 201 Created order: id=10017, order_number=ORD-20261017-0011 ✓ All 6 live scenarios executed and passed successfully!

[2026-10-17T11:50:51.273Z] ================================================================
[2026-10-17T11:50:51.276Z] FloCafe Issue #1182 (ARCH-012b) Live Product Verification Driver
[2026-10-17T11:50:51.276Z] ================================================================
[2026-10-17T11:50:51.356Z] 
>>> Scenario 1: Ordinary startup with historical orders (Fast-path)
[2026-10-17T11:50:51.356Z]   Seeding 10,000 historical orders and bills across 100 days...
[2026-10-17T11:50:51.467Z]   Ordinary boot check completed in: 0.066 ms
[2026-10-17T11:50:51.467Z]   Result: repaired=false, ordersUpdated=0, billsUpdated=0
[2026-10-17T11:50:51.467Z] 
>>> Scenario 2: Pending startup repair executes SQL aggregation
[2026-10-17T11:50:51.475Z]   Pending repair completed in: 7.77 ms, heap delta: 9.7 KB
[2026-10-17T11:50:51.475Z] 
>>> Scenario 3: Gapped order history & higher counter integrity (Adversarial)
[2026-10-17T11:50:51.516Z]   Gapped date (20261017) counter: 5 (expected 5)
[2026-10-17T11:50:51.517Z]   Higher counter date (20261018) counter: 25 (expected 25)
[2026-10-17T11:50:51.517Z] 
>>> Scenario 4: Non-canonical prefixes & malformed suffixes (Adversarial)
[2026-10-17T11:50:51.517Z]   Malformed date (20261020) sequence record: undefined
[2026-10-17T11:50:51.517Z]   Valid date (20261021) sequence current_value: 3
[2026-10-17T11:50:51.517Z] 
>>> Scenario 5: Self-healing fallback when sequences table is missing
[2026-10-17T11:50:51.518Z]   Self-healing boot result: repaired=true
[2026-10-17T11:50:51.518Z] 
>>> Scenario 6: Live Express server & HTTP API order creation end-to-end
[2026-10-17T11:50:51.603Z]   Express server listening on live port: http://127.0.0.1:49662
[2026-10-17T11:50:51.611Z]   Pre-seeded today's order sequence: current_value = 10
[2026-10-17T11:50:51.611Z]   Sending live HTTP POST http://127.0.0.1:49662/api/orders ...
[2026-10-17T11:50:51.670Z]   Response status: 201
[2026-10-17T11:50:51.670Z]   Created order: id=10017, order_number=ORD-20261017-0011
[2026-10-17T11:50:51.670Z]   Express server closed
[2026-10-17T11:50:51.671Z] 
================================================================
[2026-10-17T11:50:51.671Z] ✓ All 6 live scenarios executed and passed successfully!
[2026-10-17T11:50:51.671Z] ================================================================
Evidence: Scale Benchmark Output

100k Rows (1,000 dates): - Repair duration: 88.00 ms - Heap delta: 9.9 KB - Ordinary boot duration: 0.055 ms (< 15 ms required) 365k Rows (3,650 dates / 10 years): - Repair duration: 354.62 ms (< 1,500 ms required) - Heap delta: 8.4 KB (< 2 MB required) - Ordinary boot duration: 0.051 ms (< 15 ms required)

============================================================
Sequence Repair Performance & Invariants Tests (#1182 / ARCH-012b)
============================================================
[DB] Opening database at: /var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/flo-seq-perf-n8Bkib/flo.db
[DB] Schema: v0 → v114
[DB] Triggering auto-backup before migrating v0 → v114...
[DB] Auto-backup before migrating v0 → v114 created at /var/folders/y_/1ltcxtwj0zd_w1dg9jv4jl580000gn/T/flo-seq-perf-n8Bkib/backups/flo-backup-2026-10-17T11-50-55-400Z-pre-v0-to-v114.db
[DB] Install defaults loaded; first-run setup pending
[MIGRATION v23] normalized: 0, unparseable: 0
[MIGRATION v23] merged 0 duplicate customer(s)
[MIGRATION v23] verification: 0 customers, 0 still non-E.164
[MIGRATION v24] normalized: 0, unparseable: 0
[MIGRATION v25] backfilled addons for 0 order items (0 unparseable, skipped)
[MIGRATION v30] backfilled 0 order_item(s) still missing a normalized addons snapshot
[MIGRATION v30] Dropped order_items.addons — order_item_addons is now the only place selected addons live.
[MIGRATION v73] normalized 0 merchant template payload(s); 0 already canonical or left untouched
[DB] integrity_check: ok
[DB] foreign_key_check: clean

--- 1. Invariants, Gaps, and No-Reuse Contract ---
  ✓ Repair executed on force
  ✓ Stored counter advanced from 1 to greatest suffix 5 across gap
  ✓ Existing higher counter 25 is never lowered
  ✓ Absent sequence row seeded to greatest suffix 8
  ✓ Generated next order number: ORD-20261017-0026

--- 2. Malformed Suffixes & Coverage Boundaries ---
  ✓ Malformed suffixes, non-canonical prefixes, and zero suffixes ignored
  ✓ Valid order sequence on 20261021 parsed correctly

--- 3. Recovery Triggering & Ordinary Boot Fast Path ---
  ✓ sequence_repair_completed_at timestamp recorded
  ✓ sequence_repair_pending cleared to 0
  ✓ Ordinary boot skips historical scan when sequences are in sync
  ✓ sequence_repair_pending = 1 successfully triggers repair
  ✓ sequence_repair_pending reset to 0 after repair
  ✓ Orders exist
  ✓ Self-healing triggered when sequences table is empty but orders exist
  ✓ Sequences restored by self-healing

--- 4. Scale Benchmark: 100,000 Orders & 100,000 Bills ---
  Seeding 100,000 orders across 1,000 calendar dates (100 orders/day)...
  Seeded 100k orders + 100k bills in 1017.0 ms
  100k Repair duration: 82.99 ms (order updates: 1000, bill updates: 1000)
  100k Heap delta: 10.2 KB
  ✓ All 1,000 order date buckets seeded
  ✓ All 1,000 bill date buckets seeded
  ✓ Heap delta must be < 2 MB, was 10.2 KB
  ✓ Sample date 20100500 has high-water 100
  100k Ordinary boot duration: 0.047 ms
  ✓ Ordinary boot skips scanning 100k rows
  ✓ Ordinary boot must take < 15 ms, took 0.05 ms

--- 5. Scale Benchmark: 365,000 Orders & 365,000 Bills ---
  Expanding dataset to 365,000 orders across 3,650 dates (10 years)...
  Expanded to 365k in 3147.0 ms
  ✓ Orders count is exactly 365,000
  365k Repair duration: 378.03 ms
  365k Heap delta: 8.4 KB
  ✓ Repair duration must be < 1500 ms, took 378.03 ms
  ✓ Heap delta must be < 2 MB, was 8.4 KB
  ✓ Sample 365k order sequence has high-water 100
  365k Ordinary boot duration: 0.047 ms
  ✓ Ordinary boot skips scanning 365k rows
  ✓ Ordinary boot must take < 15 ms, took 0.05 ms

============================================================
✓ All sequence repair performance and correctness tests passed!
============================================================
[DB] Database closed
Evidence: HTTP API Order Creation Response
{
  "endpoint": "http://127.0.0.1:49662/api/orders",
  "statusCode": 201,
  "responseBody": {
    "order": {
      "id": 10017,
      "order_number": "ORD-20261017-0011",
      "table_id": null,
      "customer_id": null,
      "user_id": "owner-test-001",
      "type": "dine_in",
      "delivery_address": null,
      "expected_payment_method": null,
      "delivery_note": null,
      "guest_count": null,
      "special_instructions": null,
      "packaging_charge": 0,
      "delivery_charge": 0,
      "service_charge": 0,
      "charges_breakdown": null,
      "status": "pending",
      "subtotal": 900,
      "tax_amount": 0,
      "tax_breakdown": [],
      "tax_snapshot": null,
      "packaging_tax_category_id": null,
      "delivery_tax_category_id": null,
      "service_charge_tax_category_id": null,
      "discount_amount": 0,
      "discount_type": null,
      "discount_value": null,
      "discount_reason": null,
      "round_off": 0,
      "total": 900,
      "cooking_started_at": null,
      "ready_at": null,
      "served_at": null,
      "completed_at": null,
      "cancelled_at": null,
      "cancellation_reason": null,
      "created_at": "2026-10-17 11:50:51",
      "updated_at": "2026-10-17 11:50:51",
      "online_platform": null,
      "external_order_id": null,
      "expected_payment_method_id": null,
      "items": [
        {
          "id": 1,
          "order_id": 10017,
          "product_id": "prod-latte",
          "product_name": "Caffe Latte",
          "product_sku": null,
          "unit_price": 450,
          "quantity": 2,
          "inventory_deducted_quantity": 0,
          "inventory_product_id": null,
          "subtotal": 900,
          "tax_amount": 0,
          "tax_breakdown": [],
          "tax_snapshot": null,
          "tax_type": "none",
          "discount_amount": 0,
          "total": 900,
          "variant_selection": null,
          "modifier_selection": null,
          "special_instructions": null,
          "status": "pending",
          "created_at": "2026-10-17 11:50:51",
          "updated_at": "2026-10-17 11:50:51",
          "voided_at": null,
          "recipe_snapshot": null,
          "variant_id": null,
          "inventory_variant_id": null,
          "addons": [],
          "edit_version": 0
        }
      ]
    }
  },
  "verifiedSequence": {
    "current_value": 11
  }
}
Evidence: Live Scenario Execution Results
[
  {
    "scenario": "Ordinary startup with historical orders skips scanning historical tables (Fast-path)",
    "status": "pass",
    "duration_ms": 0.06637500000010732,
    "details": "10k orders present; ordinary boot completed in 0.066 ms without re-scanning"
  },
  {
    "scenario": "Pending startup repair executes SQL aggregation without JS heap bloat",
    "status": "pass",
    "duration_ms": 7.765583000000106,
    "heap_delta_kb": 9.7265625,
    "details": "10k records repaired in 7.77 ms with 9.7 KB heap delta"
  },
  {
    "scenario": "Gapped order history and higher existing sequence counter integrity",
    "status": "pass",
    "details": "Gapped history advanced counter to 5; higher existing counter 25 preserved"
  },
  {
    "scenario": "Non-canonical prefixes and malformed suffixes are ignored",
    "status": "pass",
    "details": "Malformed suffixes and custom prefixes filtered by SQL GLOB patterns without corruption"
  },
  {
    "scenario": "Self-healing triggers when sequences table is lost but historical orders exist",
    "status": "pass",
    "details": "Empty sequences table with existing orders triggered self-healing and restored high-water mark"
  },
  {
    "scenario": "Live running Express server and HTTP API order creation end-to-end",
    "status": "pass",
    "details": "POST /api/orders returned 201 with ORD-20261017-0011; DB sequence updated to 11"
  }
]

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 6 of 6 scenarios driven live against the product
Scenario Result Live Evidence
Ordinary startup with historical orders skips scanning historical tables (Fast-path) ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Pending startup repair executes SQL aggregation without JS heap bloat ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Gapped order history and higher existing sequence counter integrity ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Non-canonical prefixes and malformed suffixes are ignored ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Self-healing triggers when sequences table is lost but historical orders exist ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/scenario-results.json
Live running Express server and HTTP API order creation end-to-end ✅ pass live ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/api-order-creation-response.json
  • node tests/run-electron-node-test.cjs tests/sequence-generation.test.ts
  • node tests/run-electron-node-test.cjs tests/sequence-repair-performance.test.ts
  • node tests/run-electron-node-test.cjs tests/order-numbering-settings.test.ts
  • npm run test:backup
  • node tests/run-electron-node-test.cjs ~/.no-mistakes/evidence/01M54TQQCNZZ1SGG7AZ30EPD9K/live-driver.js
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

@coderabbitai

coderabbitai Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository: FreeOpenSourcePOS/FloCafe/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f844cb82-59b8-44b8-a4e5-be0415358c8f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@khaira777
khaira777 merged commit 27beb29 into main Oct 17, 2026
23 checks passed
@khaira777
khaira777 deleted the fix/1182-startup-sequence-repair-scan branch October 17, 2026 12:04
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