Skip to content

fix(tally): the reconciler re-queues a voucher missing in Tally - #632

Open
privatejoel wants to merge 1 commit into
mainfrom
claude/reconciler-requeue-fix
Open

privatejoel wants to merge 1 commit into
mainfrom
claude/reconciler-requeue-fix

Conversation

@privatejoel

Copy link
Copy Markdown
Collaborator

Summary

The drift check did not re-queue a voucher that is missing in Tally.

applyAutoFix("missing_in_tally") in src/api/_lib/tally-reconciler.js wrote a tally_retry_queue row with three columns that do not exist (tally_voucher_record_id, attempt, reason). The row also had no voucher_type, payload_xml or payload_hash. These three columns are NOT NULL (016_tally_v2.sql). Thus the insert always failed. The code did not read the error and returned re_pushed. The finding showed "re_pushed", but the queue stayed empty.

This PR makes these changes:

  1. The reconciler now queues the row through enqueueTallyVoucher (src/api/_lib/tally-enqueue.js). This is the helper that the copilot uses. It reads the order in the finding's tenant. It refuses an order that is not approved. It builds the XML with buildSalesVoucherXml. It sets payload_hash = order.payload_hash || approval.payloadHash. It checks the insert error.
  2. enqueueTallyVoucher has two new optional inputs: voucherRecordId and voucherNo. The reconciler sets voucher_record_id to the voucher record, so the retry drain updates that record after the push. The XML uses the voucher number that Anvil pushed before.
  3. enqueueTallyVoucher now refuses a cancelled order (ORDER_CANCELLED). After fix(so): close five sales order audit defects #631, the retry drain skips a cancelled order. Without this check, the row is queued but never pushed.
  4. When the reconciler cannot queue the row, the finding shows re_push_failed. The run does not count it in auto_fixes_applied. The reason goes to the log.
  5. The reconciler queues one automatic re-push for each voucher record. The voucher XML has ACTION="Create", so each push can add one more voucher to the ledger. The check runs every 30 minutes. If the mirror does not show the voucher (for example, Tally gave it a different number), the old logic (if it had worked) would push the voucher again on each run. Now a later run finds the earlier row and does nothing (auto_fix_applied stays empty). If the reconciler cannot read the queue, it does not queue (fail closed).
  6. The drift check now reads company_id from tally_voucher_records, so the row goes to the company that the voucher was pushed to.

Deploy notes

  • No migration. All columns are in 016_tally_v2.sql.
  • This path runs only when tenant_settings.tally_recon_auto_fix_enabled is true. The cron also needs tally_drift_addon_enabled.
  • After deploy, a tenant with auto-fix on can get new pending rows in tally_retry_queue for vouchers that the mirror does not show. Before, these rows never got written. Check the queue for each such tenant after the first cron run. Make sure that tally/sync fills tally_voucher_state for that tenant. If not, every exported voucher is "missing" and gets one re-push.
  • Copilot post_tally_voucher on a cancelled order now fails with ORDER_CANCELLED. Before, it queued a row.
  • Merge fix(so): close five sales order audit defects #631 too. Without fix(so): close five sales order audit defects #631, the retry drain does not check the approval hash or the order status when it pushes.

Tests

  • New: src/v3-app/tally-reconciler.requeue.test.js (9 tests). Each test runs the real driftCheck, the real enqueueTallyVoucher and the real buildSalesVoucherXml against an in-memory database. The fake database refuses a tally_retry_queue row with an unknown column or a missing NOT NULL value, as PostgREST and Postgres do. It returns only the selected columns. The tests show:
    • the row has the real columns, the voucher record id, the pushed voucher number, the company of the voucher record, and the order's payload hash (or the approval hash when the order has none);
    • a failed insert shows re_push_failed, not re_pushed, and does not count as a fix;
    • an order that is not approved, a cancelled order, an order in another tenant, and an order with an unmapped line are not queued;
    • a failed read of the queue does not queue;
    • a second run does not queue a second row.
  • Mutation check: 15 mutations, all killed. They include the original broken insert, no insert-error check, no tenant filter on the order read, no cancelled check, no approval check, hash order swapped, no voucher record id, no voucher number, no company id (passed and selected), no dedupe, dedupe read error ignored, builder error not caught, a failure counted as a fix, and an enqueue failure reported as re_pushed.
  • Gates: npm run check, lint, build, verify, eval:golden and NODE_OPTIONS=--no-experimental-webstorage npx vitest run pass.

Not covered

  • After the one automatic re-push, the reconciler does not push again, also when the retry drain gave up. The operator must push by hand. A later finding shows no auto-fix.
  • The dedupe uses voucher_record_id. A row that the copilot queued for the same order has no voucher record id, so the reconciler does not see it.
  • voucher_cancelled_in_tally writes in applyAutoFix also do not check errors. This PR does not change them.
  • The drift check compares tally_voucher_state by voucher_no only. If Tally renumbers a voucher, the finding is wrong. This PR limits the damage to one re-push, but does not fix the match.
  • No test against a real Postgres.

🤖 Generated with Claude Code

applyAutoFix("missing_in_tally") wrote a tally_retry_queue row with
columns that do not exist and without the NOT NULL voucher_type,
payload_xml and payload_hash. The insert always failed, but the finding
said "re_pushed", so nothing was re-pushed.

The reconciler now queues the row through enqueueTallyVoucher: the order
is read in the finding's tenant, it must be approved, the XML comes from
buildSalesVoucherXml, and the insert error is checked. The row carries
the voucher record id, the pushed voucher number and the voucher's
company. A row that is not in the queue shows "re_push_failed" and does
not count as a fix. One automatic re-push per voucher record, because
each push creates a voucher in Tally. enqueueTallyVoucher also refuses a
cancelled order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
anvil Ready Ready Preview Oct 21, 2026 2:46am UTC

This branch was successfully deployed

1 active deployment
Preview — 21d97f20 Deployed Oct 21, 2026 by vercel[bot]
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