Saltar a contenido

✂️ Split & Promote

Split and batch promote both change the shape of composite transactions, but they are not implemented as delete-and-recreate inverses:

Operation Direction Batch behavior
Split Composite pair → two standalone rows Mutates both persisted rows in place and removes their reciprocal links
Promote Two standalone rows → composite pair Mutates the resolved rows in place and creates reciprocal links

The durable pair is always stored with reciprocal related_transaction_id values. link_uuid is only a request-scoped correlation value for creates; it is not a column on Transaction.


🔢 Pipeline Position

The relevant execute_batch() stages run in this order:

delete → split → update → updated-pair validation → create → promote → create-link resolution
       → WAC and FX issues

Split deliberately precedes update. A batch can therefore split a pair and then apply updates whose type swaps are legal only for the resulting standalone types. Promote follows create so its references can resolve either persisted rows or rows that were flushed earlier in the same batch.


✂️ Split

⚙️ In-place behavior

For each TXSplitBatchItem, apply_splits():

  1. resolves both required database IDs;
  2. verifies that each row's related_transaction_id points to the other;
  3. determines source/destination by negative/positive quantity for TRANSFER, or by negative/positive amount for cash pairs;
  4. changes both row types using SPLIT_TYPE_MAP;
  5. clears both related_transaction_id values;
  6. clears asset_id for the cash split outcomes;
  7. for a TRANSFER pair, clears cost_basis_override and cost_basis_currency on the giving leg, an ADJUSTMENT without a value (see The giving leg of a TRANSFER); and
  8. records both brokers' affected dates and a two-ID split result.

The rows keep their IDs. No create or delete is involved.

Source composite type Negative/source leg Positive/destination leg
CASH_TRANSFER WITHDRAWAL DEPOSIT
TRANSFER ADJUSTMENT ADJUSTMENT
FX_CONVERSION WITHDRAWAL DEPOSIT

Deleting a broker with its transactions converts the surviving half of each pair with another broker by the same rules, through the same helpers of batch_edits.py: pair_sides() (step 3) and make_standalone() (steps 4–6), and leaves a giving TRANSFER leg without a cost basis, as a split does (below). One difference: a received TRANSFER without a cost basis first gets its sender's Auto cost, which a split does not write. See Transaction Service — Broker Deletion.

📤 The giving leg of a TRANSFER

A split TRANSFER pair leaves two ADJUSTMENTs. The receiving one keeps its cost basis. The giving one takes units out, and an ADJUSTMENT taking units out has no value (decision 47 of release 1.3, as the developer changed it on 21/10/2026, N78-1; see An ADJUSTMENT_OUT has no value): its units only leave the quantity, at 0 in the FIFO lots, and their cost is realized as a loss.

  • apply_splits() clears the leg's cost_basis_override and cost_basis_currency (step 7). No cost is computed, so the split needs no rate and is never refused for that leg: until N78-1 it left at the Auto cost of its own broker, and a missing conversion (wacFxUnavailable) or an unreadable pool (autoCostUnreadable) refused the split.
  • An update of the same batch cannot give the leg a cost: rule 12 refuses a cost_basis_mode on an ADJUSTMENT taking units out (costBasisModeIncompatible), and rule 12b a cost_basis_override set without one (costBasisNotAllowed, W-B72; see Per-type rules).

Tests: ./dev.py test services transaction-cost-basis-rule (TestDecision47TheGivingLegOfATransfer).

📦 Request shape

Split is a first-class item in the unified batch:

POST /transactions/commit
{
  "splits": [
    {"id_a": 101, "id_b": 102}
  ]
}

id_a and id_b must be different positive IDs and must identify the two reciprocally linked rows. Their input order does not determine source and destination; the signed quantity or amount does.

The Transactions page sends this item on its own, outside the bulk workspace: Split pair in a row's actions asks for confirmation, then calls commitTransactions({splits: [{id_a, id_b}]}). Inside the workspace a saved pair is queued in pendingSplits and travels with Save All instead (see Transaction Staging State).


🔗 Batch Promote

⚙️ In-place behavior

apply_promotes() accepts:

  • two saved rows (id_a + id_b);
  • two creates from this batch (link_uuid_a + link_uuid_b); or
  • one saved and one same-batch row.

Each side must provide exactly one reference form: id_* or link_uuid_*, never both. After resolving the references, the stage:

  1. rejects a row that already has related_transaction_id;
  2. calls _find_promote_rule_match() to infer the target type from TX_TYPE_METADATA.promote_from and its field constraints;
  3. validates both rows as they would be once promoted: _promoted_state_issues() runs validate_transaction_business_rules() on each row with the target type, the per-type rules that creates and updates go through too. Each error becomes an issue on operation promote, at the promote's index, with ref_id set only for a saved row. _resolved_cost_issues() checks the merged cost basis beside them: a non-null resolved_fields.cost_basis_override where neither row takes a cost basis under the target type — a cash pair, CASH_TRANSFER or FX_CONVERSION — gets one costBasisNotAllowed, field cost_basis_override, params {type, qty}, no ref_id (W-B72, night decision N78). On any issue the promote is then skipped and neither row changes;
  4. assigns the inferred type to both existing ORM rows;
  5. writes reciprocal related_transaction_id values;
  6. clears cost_basis_override and cost_basis_currency on a side whose new type takes no cost basis (cost_basis_allowed() is false: the giving side of a TRANSFER), such as an ADJUSTMENT taking units out that an older version left with a cost; the receiving side keeps its own (decision 47);
  7. applies the merged values of resolved_fields, the keys the request names (see Request shape);
  8. marks same-batch correlation keys as consumed so the later create-link stage does not process them again; and
  9. emits a promote result containing the same two row IDs.

No original row is deleted and no replacement row is created.

Step 3 exists for a row linked to an asset event, such as an ADJUSTMENT linked to a SPLIT. A promote never clears asset_event_id, and the business rules accept that link only on the types TX_TYPE_METADATA marks event_compatible (DIVIDEND, INTEREST, ADJUSTMENT; see Per-type rules): every promote target has event_compatible: false, so such a row is refused with eventTypeIncompatible. Without the check it would become a TRANSFER still linked to the split, and the average cost, which treats every row linked to a SPLIT event as a split whatever its type, would read it as one.

🧭 Inferred target rules

The batch payload does not contain new_type. The current metadata permits:

Standalone input types Inferred target Required constraints
ADJUSTMENT + ADJUSTMENT TRANSFER Same asset_id, different brokers, opposite quantities
WITHDRAWAL + DEPOSIT FX_CONVERSION Same broker, different cash currencies
WITHDRAWAL + DEPOSIT CASH_TRANSFER Different brokers, same cash currency, opposite cash amounts

The match is symmetric: either row may be supplied as side A.

📦 Request shape

A saved-row promote can carry merged values, resolved_fields:

POST /transactions/commit
{
  "promotes": [
    {
      "id_a": 201,
      "id_b": 202,
      "resolved_fields": {
        "description": "Broker transfer",
        "tags": ["internal"],
        "date": "2026-09-11",
        "cost_basis_override": {"code": "EUR", "amount": "42.50"}
      }
    }
  ]
}

resolved_fields is a TXPromoteResolvedFields (backend/app/schemas/transactions/batch.py; H-41, within R13-SEC7-3): a StrictModel typed like a create's fields, so an unknown key is refused.

Key Type Applied to
description Optional[str], at most 500 characters Both rows
tags Optional[List[str]], through the create's tag validator (validate_tags_list(): a list, or a comma-separated string) Both rows
date Optional[date]; an explicit null is refused Both rows
cost_basis_override Optional[Currency] For an asset transfer, the positive-quantity receiver only; the sender's is cleared (decision 47). A cash pair takes none: a value is refused (costBasisNotAllowed, step 3), where before W-B72 it was written on both rows; a null clears both

_apply_promote_resolved_fields() (batch_creates.py) applies the keys the request names (model_fields_set): a key left out leaves both rows alone, and an explicit null clears the description, the tags or the cost basis. Once parsed, the resolved cost basis is checked against its column (check_decimal_limits(), see Decimal limits) and the resolved date against the range (check_dates(), see Transaction dates), before any stage applies them.

A value the model refuses becomes an issue of the promote at the batch's parse (_parse_lenient()): operation promote, pydantic's error type as code, as params pydantic's context when it is plain data — {max_length: 500} for a description past 500 characters, {error: <pydantic's text>} for a malformed date, {class_name} for a cost basis that is no object — and null otherwise, for a value_error or an error without context (W-B70, the rule of Per-type rules; before, params was always null here), and a field named by the merged field itself, without the resolved_fields. prefix (loc_to_field() of backend/app/utils/pydantic_errors.py): description, tags, date, cost_basis_override, cost_basis_override.amount, or the unknown key. The issue thus carries that field's label. As with any parse refusal of this lenient batch, the other operations are still checked, then nothing is saved (committed=False). Before H-41 the dict was applied as sent: a malformed date, tags or cost basis raised out of the batch, a 500; a description that was no string reached the Text column; an unknown key was ignored.

The Transactions page sends a saved-row promote on its own, outside the workspace, when exactly two selected rows are unpaired, sit on brokers the user can edit, and match a rule (findPromoteMatch() on the promote_from metadata of GET /transactions/types, which finds none when a row is linked to an asset event and the target type refuses the link). 🔗 Promote pair then opens a plain confirmation, or PromoteMergeModal when the two rows differ in description, tags, date or cost basis; only the merge dialog adds resolved_fields, with description and tags alone, as Merge all does in the bulk workspace (PromoteAllModal): date and cost_basis_override come from API clients only. The merged description joins the two with a line break (mergeStrings() in frontend/src/lib/features/transactions/bulk/promoteHelpers.ts), so two long descriptions can pass 500 characters, which the backend refuses with string_too_long, as it refuses a create's (TXCreateItem.description takes 500 too). The frontend stops before it (W-B70; the backend limit is unchanged): PromoteMergeModal counts the merged description in code points, as Python's len() does (textLength() against TX_DESCRIPTION_MAX_LENGTH), and keeps Confirm promotion disabled past 500; Merge all with Combine leaves such a pair unpromoted, for the merge dialog, and a warning counts the pairs left (see Promote suggestions).

A mixed saved/new request uses a create correlation value:

POST /transactions/commit
{
  "creates": [
    {
      "broker_id": 8,
      "type": "DEPOSIT",
      "date": "2026-09-11",
      "cash": {"code": "EUR", "amount": "300"},
      "link_uuid": "draft-deposit-8"
    }
  ],
  "promotes": [
    {"id_a": 201, "link_uuid_b": "draft-deposit-8"}
  ]
}

Here link_uuid_b resolves the just-flushed create. Once consumed by promote, that correlation group is skipped by resolve_create_links(). The persisted rows contain only reciprocal related_transaction_id values.

🧮 Cost basis boundary

A promote item has no cost_basis_mode, and apply_promotes() does not calculate WAC. The later _compute_wac_for_auto_items() worklist is built only from parsed create and update items. Promote-specific cost-basis resolution therefore comes from resolved_fields.cost_basis_override, which only an API client sends (the merge dialogs send description and tags); without that key, the receiver keeps the cost basis it already has, and the sender is left without one (step 6).

Because apply_promotes() writes related_transaction_id before the WAC step, a create or update in Auto that the promote makes the receiving side averages its partner's broker, as of the partner's date and without the partner's outgoing leg.

validate_cost_basis() then checks the receiving side like any other row: a new row as a create and a saved row also in updates[] as that update (the promote changes its type, so even an update of its notes alone is checked), both unless in Auto; any other saved row without a cost basis gets costBasisRequired on operation promote, with the promote's index and ref_id set to that row. See WAC & Cost Basis.


🧯 Errors and Rollback

Split/promote row failures are accumulated as TXValidationIssue entries while their stage loops continue. Common stable codes are:

Code Cause
txNotFound A split ID does not resolve
splitIdsMismatch Split IDs are not reciprocal partners
typeCannotSplit The current pair type has no split mapping
promoteRefNotFound A saved or same-batch promote reference cannot be resolved
alreadyPaired A promote candidate already has a persistent partner
noPromoteRule No metadata rule and constraint set matches the two rows
eventTypeIncompatible A promoted row is linked to an asset event, which the target type refuses (step 3)
costBasisNotAllowed On operation promote: a cash pair's resolved cost_basis_override, which neither leg takes (step 3, W-B72)

The service does not roll the session back itself. /transactions/validate always rolls back; /transactions/commit commits only when TXBatchResponse.committed is true and rolls back otherwise. Access denial, a value past its column's digits before the decimal point or a date out of range may return before either mutation stage, and balance replay stops after its first balance exception, so the overall pipeline is not an unconditional collect-every-error pass.