Old rules for older accounts, a rules date per account, and a firm cap

When the master changes an account type with "New accounts only", it now
serves the type's old rules as kept rule sets (firm.keptAccountRules), each
for accounts dated inside [openedAt, closedAt). This build applies them:

- getAccountConfig, the one rules lookup trading reads, puts a kept set's
  rule fields over the live type when the account's rules date falls in
  its window. Id, label, prefix, size and the projection fields stay the
  live type's, so every report still names the live type.
- account_assignments gains rules_date and rules_date_manual. A new row is
  dated when the machine first sees the account, on the master's clock
  when this one runs behind and never before the catalog's last change.
  Auto dates come down to the broker's evidence: the end of the first fund
  day in Chicago, or a DXtrade registration instant. Evidence is applied
  before an account's first trade. Rows from before the migration get its
  time; unpinning keeps the row and its date. Backups carry both columns.
- An account on a kept set stays on its type while its prefix and cached
  size still match, so a tie-break flip can't move it off its old rules.
- Kept entries parse through the same normalizer as live types (now
  normalizeAccountType in lib/kept-rules.ts); a malformed one rejects the
  sync like a malformed type. The cached catalog records the parser that
  stored it, and a cache from an older parser is fetched in full rather
  than trusted with ?since= (this also brings in maxConcurrentAccounts).
- Reports add keptRuleIds per firm and live counts per kept set per type.
- The dashboard row and the account page show the rules date next to the
  account name, with an "old rules" chip saying what differs. Clicking
  the date edits it inline (PUT /api/accounts/:id/rules-date), previewing
  which rules the date lands on.

Also: the master's per-firm cap on accounts traded at once
(maxConcurrentAccounts), applied on top of the machine's own max
concurrent when a batch is drawn and in copy trading.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-10-18 22:21:55 -05:00
co-authored by Claude Opus 5.5
parent 98016b3eb3
commit c0899620d4
24 changed files with 2127 additions and 153 deletions
+29 -1
View File
@@ -1,4 +1,10 @@
import type { FirmPlatform } from './lib/master-protocol';
import type { KeptRulesWindow } from './lib/kept-rules';
/** When one of an account type's old rule sets applies: accounts whose rules
* date is inside [openedAt, closedAt) keep those rules (openedAt null = no
* lower bound). `id` is the kept set's own, never an account type id. */
export type { KeptRulesWindow };
export interface AccountConfig {
/** Master's account type id — what a manual assignment pins to. */
@@ -36,6 +42,14 @@ export interface AccountConfig {
tradesEveryDay?: boolean;
}
/** An account type as the firm listing (/api/firms) carries it: the current
* rules, plus the windows of the old rule sets it keeps, so the dashboard can
* preview which rules a rules date lands on. */
export interface FirmAccountType extends AccountConfig {
/** Sorted by closedAt; empty when every account is on the current rules. */
kept: KeptRulesWindow[];
}
export interface FirmConfig {
/** Master's firm id (uuid). */
id: string;
@@ -57,7 +71,7 @@ export interface FirmConfig {
instrumentsSource: 'master' | 'legacy';
/** The firm's account types, straight from the catalog. Doubles as the
* option list for a manual account-type override. */
accounts: AccountConfig[];
accounts: FirmAccountType[];
catalogUpdatedAt: string;
}
@@ -138,6 +152,20 @@ export interface AccountState {
inferredAccountSize: number | null;
/** True when more than one account type matched and updatedAt decided it. */
accountTypeAmbiguous: boolean;
/** ISO. Decides which of the type's rule sets the account trades on: one
* dated before a firm's rule change keeps the old rules. Defaults to when
* the account started (broker's first deposit / registration), else when
* this machine first saw it. null = no assignment row yet. */
rulesDate: string | null;
/** True when the rules date was set by hand — broker evidence never moves it. */
rulesDateManual: boolean;
/** The old rule set the account trades on; null = the type's current
* rules. accountType above already carries these rules. */
keptRules: (KeptRulesWindow & {
/** Rule fields where the kept set differs from the type's current
* rules — raw values; the dashboard formats them. */
differs: { field: string; kept: unknown; now: unknown }[];
}) | null;
}
export interface FirmState {