Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

20.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

/**
 * v5 reconciler decision core (spec:
 * .ai/specs/2026-08-12-accounting-sync-reconciler-unification.md, D1).
 *
 * ONE pure, state-shaped answer to "what should happen for this entity right
 * now?" — subsuming the decision logic that previously lived in five places
 * (the event path's per-table branches, the cooldown/transition routing, and
 * the sweep's document/payment diffs and re-drive rules). The event handler,
 * the outbound sweep, and any future caller are just callers: they load
 * state, ask, and apply.
 *
 * Import-light on purpose (no Inngest, no @carbon/auth): the golden decision
 * matrix (reconcile-golden.test.ts) exercises this exhaustively without
 * booting anything.
 *
 * Level-triggered semantics: the decision never looks at what CHANGED (no
 * event deltas, no old/new rows) — only at what IS. Idempotence follows: the
 * same state always produces the same decision, and a decision whose work is
 * already recorded produces "nothing". That is what makes lost events, burst
 * duplicates, and cooldown races unrepresentable rather than specially
 * handled.
 */
import type { ChargePolicyInput } from "@carbon/ee/accounting";
import type { EffectivePostingSyncSettings } from "@carbon/ee/sync";
import {
  MAX_REDRIVE_ATTEMPTS,
  planJournalPostingFromState,
  SWEPT_BILL_STATUSES,
  SWEPT_CHARGE_STATUSES,
  SWEPT_INVOICE_STATUSES,
  SWEPT_PAYMENT_STATUSES,
  SWEPT_REIMBURSEMENT_STATUSES,
  type SyncOperationRequest,
  shouldEnqueueMissingDocument,
  type TerminalSyncOperationRequest
} from "./accounting-sync-operations";

/** Entity types the outbound reconciler covers. */
export type ReconcileEntityType =
  | "journalEntry"
  | "bill"
  | "invoice"
  | "charge"
  | "reimbursement"
  | "payment"
  | "customer"
  | "vendor"
  | "item"
  | "purchaseOrder"
  | "salesOrder"
  | "creditMemo"
  | "supplierCredit";

export type ReconcileRef = {
  entityType: ReconcileEntityType;
  entityId: string;
};

/** The newest ledger operation for the entity's push tuple. */
export type ReconcileLatestOperation = {
  id: string;
  status: string;
  errorCode: string | null;
  attemptCount: number;
  createdAt: string;
};

export type ReconcileEntityInput = {
  entityType: ReconcileEntityType;
  entityId: string;
  /**
   * Current row state, per type:
   * - journalEntry: { status, sourceType, reversalOfId }
   * - bill/invoice/payment: { status, updatedAt? }
   * - master data: { updatedAt? }
   * Null = the row does not exist (deleted; DELETE sync is unimplemented).
   */
  snapshot: {
    status?: string | null;
    sourceType?: string | null;
    reversalOfId?: string | null;
    updatedAt?: string | null;
  } | null;
  /** externalIntegrationMapping state for the entity (push identity). */
  hasMappingWithExternalId: boolean;
  /** A native push mapping remains remotely active (including payment fan-out). */
  hasUnvoidedPushMapping?: boolean;
  /**
   * `externalIntegrationMapping.lastSyncedAt`. Typed to admit a `Date`:
   * the executor loads it through Kysely, where node-postgres decodes
   * timestamptz as a Date regardless of what the generated types say.
   */
  lastSyncedAt: string | Date | null;
  /** A Pending/In Flight push op exists for the tuple. */
  hasLiveOperation: boolean;
  /** Newest push op for the tuple (by createdAt), live or terminal. */
  latestOperation: ReconcileLatestOperation | null;
  /**
   * journalEntry only: whether ANY op row exists for the plain id and for
   * the `${id}:reversal` id (spec I1 coverage — one disposition each).
   */
  journalCoverage?: { normalCovered: boolean; reversalCovered: boolean };
  /**
   * journalEntry only, "Charge" source: the backing charge
   * (`type` + whether it has a supplier). A Charge with a supplier is
   * DOC_BACKED by the synced charge object when the charge entity is enabled;
   * everything else keeps pushing as a journal entry.
   */
  charge?: ChargePolicyInput | null;
  /**
   * bill only: a posted "Purchase Invoice" journal exists for this bill —
   * the input the account-costed replay needs (the re-drive condition).
   */
  hasPostedBackingJournal?: boolean;
  /**
   * payment only: the document the parked payment settles
   * (latest op's metadata.targetDocumentId) NOW has a provider mapping —
   * the UNSYNCED_DOCUMENT re-drive condition.
   */
  settledDocumentMapped?: boolean;
  context: ReconcileContext;
};

export type ReconcileContext = {
  /** journalEntry posting enabled for this integration (always-on when an
   *  integration is connected; resolved by isJournalEntryPostingEnabled). */
  journalEntryPushEnabled: boolean;
  /** THIS entity type's resolved config: enabled and not pull-only. */
  entityPushEnabled: boolean;
  /** Rillet true — the only provider with outbound payment push. */
  providerSupportsPaymentPush: boolean;
  /** Rillet supports native document/payment deletion; Xero/QBO support charge deletion. */
  providerSupportsNativeVoid?: boolean;
  /** Inputs the journal policy core needs (planJournalPostingFromState). */
  /**
   * Settings with ledger delegation applied. Branded so the pure decision core
   * cannot be reached with raw settings — see `applyLedgerDelegation`.
   */
  settings: EffectivePostingSyncSettings;
  docSync: {
    invoiceEnabled: boolean;
    billEnabled: boolean;
    chargeEnabled: boolean;
    chargeCreditEnabled: boolean;
    creditMemoEnabled?: boolean;
    supplierCreditEnabled?: boolean;
    reimbursementEnabled?: boolean;
  };
  inventoryAdjustmentEnabled: boolean;
  /** Resolved by the executor only for Payment-source journals when the
   * AR/AP family modes diverge (otherwise the side cannot matter). */
  paymentFamily: "ar" | "ap" | null;
  /** Memo source types only: the backing memo's party (decides the family). */
  memoParty?: "customer" | "supplier" | null;
};

export type ReconcileAction =
  | { kind: "enqueue"; request: SyncOperationRequest }
  | { kind: "record-terminal"; request: TerminalSyncOperationRequest }
  | { kind: "re-drive"; operationId: string }
  | { kind: "nothing"; reason: string };

export type ReconcileDecision = { actions: ReconcileAction[] };

const nothing = (reason: string): ReconcileDecision => ({
  actions: [{ kind: "nothing", reason }]
});

const MASTER_ENTITY_TYPES: ReadonlySet<string> = new Set([
  "customer",
  "vendor",
  "item",
  "purchaseOrder",
  "salesOrder"
]);

export function computeReconcileDecision(
  input: ReconcileEntityInput
): ReconcileDecision {
  if (!input.snapshot) {
    return nothing("entity not found (deleted rows are not synced)");
  }

  switch (input.entityType) {
    case "journalEntry":
      return reconcileJournal(input);
    case "bill":
    case "invoice":
    case "charge":
    // Every reimbursement is document-shaped (POSTING_POLICY.Reimbursement is
    // `representation: "document"`), so there is no per-row charge-style
    // analogue to resolve first — it always reconciles as a document.
    case "reimbursement":
    // Credit memos and supplier credits push as native provider documents too.
    // They were wired into the entity union, the snapshot tables, the table map,
    // the subscriptions and the outbound sweep — but not here, so every ref fell
    // to `default` and returned "not reconciled": the document reached neither
    // the provider's GL nor its subledger, while its journal was excluded as
    // DOC_BACKED. Plumbed end to end except at the one place that decides.
    case "creditMemo":
    case "supplierCredit":
      return reconcileDocument(input);
    case "payment":
      return reconcilePayment(input);
    default:
      if (MASTER_ENTITY_TYPES.has(input.entityType)) {
        return reconcileMasterData(input);
      }
      return nothing(`entity type '${input.entityType}' is not reconciled`);
  }
}

/**
 * Journals (spec I1): every Posted/Reversed journal carries exactly one
 * recorded disposition per push identity (the plain id, and `:reversal` for
 * Reversed journals). Missing dispositions route through the SAME policy
 * core the event path and backfill use (planJournalPostingFromState) — push
 * ops enqueue, policy exclusions record terminally.
 *
 * A journal row that IS a reversal entry (reversalOfId set) is never its own
 * push identity — the original journal's `:reversal` disposition carries it.
 */
function reconcileJournal(input: ReconcileEntityInput): ReconcileDecision {
  const snapshot = input.snapshot;
  if (!snapshot) return nothing("entity not found");

  if (snapshot.reversalOfId != null) {
    return nothing(
      "reversal entry — represented by the original journal's :reversal disposition"
    );
  }
  if (snapshot.status !== "Posted" && snapshot.status !== "Reversed") {
    return nothing(
      `journal status '${snapshot.status ?? "unknown"}' is not posted`
    );
  }
  if (!input.context.journalEntryPushEnabled) {
    return nothing("posting sync (journalEntry) is disabled");
  }

  const coverage = input.journalCoverage ?? {
    normalCovered: false,
    reversalCovered: false
  };

  const actions: ReconcileAction[] = [];

  const plan = (reversal: boolean): void => {
    const planned = planJournalPostingFromState({
      journalId: input.entityId,
      sourceType: snapshot.sourceType ?? null,
      reversal,
      settings: input.context.settings,
      docSync: input.context.docSync,
      paymentFamily: input.context.paymentFamily,
      memoParty: input.context.memoParty ?? null,
      inventoryAdjustmentEntitySyncEnabled:
        input.context.inventoryAdjustmentEnabled,
      charge: input.charge ?? null
    });
    if (planned.action === "push") {
      actions.push({ kind: "enqueue", request: planned.request });
    } else if (planned.action === "terminal") {
      actions.push({ kind: "record-terminal", request: planned.request });
    }
  };

  if (!coverage.normalCovered) plan(false);
  if (snapshot.status === "Reversed" && !coverage.reversalCovered) plan(true);

  if (actions.length === 0) {
    return nothing("journal disposition already recorded");
  }
  return { actions };
}

/**
 * Documents (bills, invoices). Posted-set statuses only — the transient
 * mid-posting "Pending" is deliberately not reconciled (the posting server
 * function's own status flip re-raises the hint seconds later, after the
 * posting journal exists; this removes the F5 race at the source instead of
 * parking a premature Warning).
 *
 * Order of rules:
 * 1. Re-drive: a bill parked Warning UNMAPPED_ACCOUNTS whose posted
 *    "Purchase Invoice" journal NOW exists flips back to Pending (capped).
 * 2. Missing remotely: shouldEnqueueMissingDocument (no mapping + no live op
 *    + latest null/Completed — the lost-event and phantom-success classes).
 * 3. Changed since a Failed/Warning attempt: the document was edited after
 *    the failure recorded — a genuinely new attempt is warranted (fix-the-
 *    data-and-save retries without waiting for a human or the re-drive).
 * 4. Otherwise nothing — parked dispositions belong to humans/policy.
 */
function reconcileNativeVoid(input: ReconcileEntityInput): ReconcileDecision {
  if (!input.context.providerSupportsNativeVoid) {
    return nothing("no active native push mapping to void");
  }
  if (input.hasLiveOperation)
    return nothing("a live operation covers the void");
  const latest = input.latestOperation;
  if (!input.hasUnvoidedPushMapping) {
    // A create may have reached the provider before its response or mapping
    // persisted. Do not silently lose that remote GL effect when Carbon voids
    // before a retry can recover the create identity. Never create just to void.
    if (
      input.entityType === "charge" &&
      !input.hasMappingWithExternalId &&
      latest &&
      ["Failed", "Warning", "Completed"].includes(latest.status) &&
      latest.errorCode !== "UNCONFIRMED_REMOTE_VOID"
    ) {
      return {
        actions: [
          {
            kind: "record-terminal",
            request: {
              entityType: input.entityType,
              entityId: input.entityId,
              direction: "push-to-accounting",
              status: "Warning",
              errorCode: "UNCONFIRMED_REMOTE_VOID",
              errorMessage:
                "A card charge was voided after a sync attempt without a durable remote identity. Verify and remove any provider transaction before resolving this warning."
            }
          }
        ]
      };
    }
    return nothing("no active native push mapping to void");
  }
  if (latest?.status === "Failed" || latest?.status === "Warning") {
    // A document can reach the provider before its payment void drains. Retry
    // that transient ordering failure through the bounded ledger lifecycle.
    return latest.attemptCount < MAX_REDRIVE_ATTEMPTS
      ? { actions: [{ kind: "re-drive", operationId: latest.id }] }
      : nothing("native void exhausted automatic retries");
  }
  return {
    actions: [
      {
        kind: "enqueue",
        request: {
          entityType: input.entityType,
          entityId: input.entityId,
          direction: "push-to-accounting"
        }
      }
    ]
  };
}

/**
 * Which statuses count as "posted" per document entity type.
 *
 * A lookup rather than a ternary chain: the chain's fallthrough was the bug
 * surface — a type nobody added an arm for silently took the sales-invoice set,
 * which never contains "Posted". Anything absent here is an invoice.
 *
 * `SWEPT_MEMO_STATUSES` is declared locally rather than beside its siblings in
 * `accounting-sync-operations.ts` only to keep this change off a file another
 * change is editing; fold it in there when convenient. It must NOT reuse
 * `SWEPT_CHARGE_STATUSES` — the values coincide today and would silently follow
 * any future change to charge statuses.
 */
const SWEPT_MEMO_STATUSES = ["Posted", "Voided"] as const;

const DOCUMENT_POSTED_STATUSES: Partial<
  Record<ReconcileEntityType, readonly string[]>
> = {
  bill: SWEPT_BILL_STATUSES,
  charge: SWEPT_CHARGE_STATUSES,
  reimbursement: SWEPT_REIMBURSEMENT_STATUSES,
  creditMemo: SWEPT_MEMO_STATUSES,
  supplierCredit: SWEPT_MEMO_STATUSES,
  invoice: SWEPT_INVOICE_STATUSES
};

function reconcileDocument(input: ReconcileEntityInput): ReconcileDecision {
  const snapshot = input.snapshot;
  if (!snapshot) return nothing("entity not found");

  if (!input.context.entityPushEnabled) {
    return nothing(`${input.entityType} push is disabled in the sync config`);
  }

  if (snapshot.status === "Voided") return reconcileNativeVoid(input);

  const postedStatuses: readonly string[] =
    DOCUMENT_POSTED_STATUSES[input.entityType] ?? SWEPT_INVOICE_STATUSES;
  if (!snapshot.status || !postedStatuses.includes(snapshot.status)) {
    return nothing(
      `${input.entityType} status '${snapshot.status ?? "unknown"}' is not posted`
    );
  }

  const latest = input.latestOperation;

  if (
    input.entityType === "bill" &&
    !input.hasLiveOperation &&
    latest?.status === "Warning" &&
    latest.errorCode === "UNMAPPED_ACCOUNTS" &&
    input.hasPostedBackingJournal === true &&
    latest.attemptCount < MAX_REDRIVE_ATTEMPTS
  ) {
    return { actions: [{ kind: "re-drive", operationId: latest.id }] };
  }

  if (
    shouldEnqueueMissingDocument({
      hasMapping: input.hasMappingWithExternalId,
      hasLiveOperation: input.hasLiveOperation,
      latestOperationStatus: latest?.status ?? null
    })
  ) {
    return {
      actions: [
        {
          kind: "enqueue",
          request: {
            entityType: input.entityType,
            entityId: input.entityId,
            direction: "push-to-accounting"
          }
        }
      ]
    };
  }

  if (
    !input.hasLiveOperation &&
    latest !== null &&
    (latest?.status === "Failed" || latest?.status === "Warning") &&
    snapshot.updatedAt != null &&
    snapshot.updatedAt > latest.createdAt
  ) {
    return {
      actions: [
        {
          kind: "enqueue",
          request: {
            entityType: input.entityType,
            entityId: input.entityId,
            direction: "push-to-accounting"
          }
        }
      ]
    };
  }

  return nothing(
    input.hasMappingWithExternalId
      ? "document already represented remotely"
      : "document disposition is parked (human retry or re-drive owns it)"
  );
}

/**
 * Payments (Rillet only). Posted payments enqueue once. Voided payments with
 * active Carbon-originated mappings enqueue again, independently of the prior
 * posting operation, until native deletion is durably recorded.
 */
function reconcilePayment(input: ReconcileEntityInput): ReconcileDecision {
  const snapshot = input.snapshot;
  if (!snapshot) return nothing("entity not found");

  if (!input.context.providerSupportsPaymentPush) {
    return nothing("provider has no outbound payment push");
  }
  if (
    snapshot.status === "Voided" &&
    input.context.providerSupportsNativeVoid
  ) {
    return reconcileNativeVoid(input);
  }
  const posted: readonly string[] = SWEPT_PAYMENT_STATUSES;
  if (!snapshot.status || !posted.includes(snapshot.status)) {
    return nothing(
      `payment status '${snapshot.status ?? "unknown"}' is not posted`
    );
  }

  // Re-drive (mirror of the bill UNMAPPED_ACCOUNTS rule): a payment parked
  // because its settled document had not synced flips back to Pending once
  // that document has a provider mapping — the fix-arrives-later path the
  // dependency chain creates (invoice fails → payment parks → invoice
  // heals → payment must follow without a human).
  const latest = input.latestOperation;
  if (
    !input.hasLiveOperation &&
    latest?.status === "Warning" &&
    latest.errorCode === "UNSYNCED_DOCUMENT" &&
    input.settledDocumentMapped === true &&
    latest.attemptCount < MAX_REDRIVE_ATTEMPTS
  ) {
    return { actions: [{ kind: "re-drive", operationId: latest.id }] };
  }

  if (input.hasLiveOperation || input.latestOperation !== null) {
    return nothing("payment already has a recorded push operation");
  }

  return {
    actions: [
      {
        kind: "enqueue",
        request: {
          entityType: "payment",
          entityId: input.entityId,
          direction: "push-to-accounting"
        }
      }
    ]
  };
}

/**
 * Epoch ms for a timestamp that reached us as either an ISO string
 * (supabase-js / PostgREST) or a `Date` (Kysely — node-postgres hands back
 * a Date for timestamptz while the generated types say `string`). Null when
 * it is neither, or unparseable.
 *
 * Comparing the two forms DIRECTLY is the trap: `string <= Date` coerces
 * both toward numbers, the string becomes NaN, and every such comparison is
 * false. That silently disabled the change check below, so every master-data
 * row edited inside the sweep window re-enqueued a no-op push twice an hour
 * for the whole 7-day window.
 */
function instantMs(value: string | Date | null | undefined): number | null {
  if (value == null) return null;
  const ms = value instanceof Date ? value.getTime() : Date.parse(value);
  return Number.isNaN(ms) ? null : ms;
}

/**
 * Master data (customer/vendor/item/PO/SO). State-shaped change detection:
 * enqueue when nothing exists remotely, or when the row changed since the
 * last successful sync (updatedAt > mapping.lastSyncedAt). This REPLACES
 * the 60s completed-row cooldown with something strictly better: an
 * unchanged just-synced entity reconciles to nothing (no window to expire),
 * and a changed one enqueues immediately (no window to swallow it). The
 * drain's fast-bailout remains the backstop for false positives.
 *
 * The two timestamps are compared as INSTANTS, never as strings: they are
 * loaded by different clients (see `instantMs`), and even two ISO strings
 * disagree on fractional-digit count and `Z` vs `+00:00`, so lexicographic
 * order is only accidentally right.
 */
function reconcileMasterData(input: ReconcileEntityInput): ReconcileDecision {
  const snapshot = input.snapshot;
  if (!snapshot) return nothing("entity not found");

  if (!input.context.entityPushEnabled) {
    return nothing(`${input.entityType} push is disabled in the sync config`);
  }
  if (input.hasLiveOperation) {
    return nothing("a live operation already covers this entity");
  }
  const lastSyncedMs = instantMs(input.lastSyncedAt);
  const updatedMs = instantMs(snapshot.updatedAt);
  if (
    input.hasMappingWithExternalId &&
    lastSyncedMs != null &&
    updatedMs != null &&
    updatedMs <= lastSyncedMs
  ) {
    return nothing("unchanged since the last successful sync");
  }

  return {
    actions: [
      {
        kind: "enqueue",
        request: {
          entityType: input.entityType,
          entityId: input.entityId,
          direction: "push-to-accounting"
        }
      }
    ]
  };
}