Files
autofirmer-expanded/lib/version.ts
T
Brandon LiandClaude Opus 5.5 b932452e98 Self-healing broker clients, per-firm scheduler isolation, eager boot
Why the autotrader sometimes broke after a weekend until `pm2 restart`:
`next start` only logs uncaught errors, so nothing ever crashed, and a few
paths could leave a client dead for the life of the process.

Tradovate: a closed socket scheduled exactly one re-login, with no catch —
one failure during weekend maintenance (or a rate-limit penalty returned as
a 200 without a token) and the client never came back. connectAndAuth is now
a single-flight loop that never throws: jittered backoff capped at 5 min,
penalty tickets waited out, captcha demands backed off. A 15s watchdog
abandons silent, never-authorized or stuck-closing sockets and stale syncs;
renewal falls back to a fresh login; orders fail fast (OrderNotPlacedError)
on a dead socket and time out instead of hanging; a bad sync answer no
longer wipes accounts and positions.

TopstepX: hub start/stop/invoke and every REST call are bounded; a wedged
hub is discarded and rebuilt; a stuck sync is superseded after 3 min;
re-login no longer tears down the sync loop; a watchdog re-logs when syncs
go stale; a no-fill timeout cancels the order.

Scheduler: one unready client used to hold every firm forever. It now holds
the tick for a 10-minute grace, then trades the other firms without it.
Per-firm try/catch in batch collection, one batch at a time, accounts marked
in flight, legs un-counted only when the entry never reached the broker, and
the scheduler starts before the first batch fires.

Boot and supervision: instrumentation.ts boots the process at server start
(it used to wait for the first dashboard request); a supervisor rebuilds a
client that has been unhealthy for 20 min (not a TopstepX one holding a
position); broker HTTP calls default to a 60s timeout; GET /api/health shows
each client's liveness and the scheduler's last decision. Shared state the
eager boot would otherwise split across bundles lives on globalThis. Logs no
longer carry access tokens or whole AxiosErrors (which include the encoded
Tradovate password).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 20:18:33 -05:00

48 lines
1.9 KiB
TypeScript

/**
* The version of the running build.
*
* Updates arrive as `git pull` (scripts/update-check.mjs), so the checkout's
* HEAD commit *is* the version — there is no release number to bump and
* nothing for anyone to forget. The commit time rides along because hashes
* don't order: it is what lets the master say which of two different
* versions is the stale one.
*
* Read once and cached: the update task restarts the process after every
* pull, so HEAD cannot move under a running build — and when it briefly does
* (pull landed, restart pending), the cached value still describes the code
* that is actually executing.
*/
import { execFileSync } from 'node:child_process';
export interface BuildVersion {
/** Short commit hash of HEAD, e.g. "c493796". */
commit: string;
/** ISO commit time of HEAD — orders versions, which hashes cannot. */
time: string;
}
// Per process, not per bundle: instrumentation.ts and the route handlers load
// their own copies of this module, and each would otherwise read HEAD at its own
// first call — after an update's pull, a late first call reports code that isn't
// running yet.
const _g = globalThis as typeof globalThis & { __buildVersion?: BuildVersion | null };
/** null when this isn't a git checkout or git isn't installed — both mean
* "no version to report", and the master shows the machine as an old build. */
export function getBuildVersion(): BuildVersion | null {
if (_g.__buildVersion !== undefined) return _g.__buildVersion;
try {
const out = execFileSync('git', ['log', '-1', '--format=%h %cI'], {
cwd: process.cwd(),
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
}).trim();
const [commit, time] = out.split(' ');
_g.__buildVersion = commit && time ? { commit, time } : null;
} catch {
_g.__buildVersion = null;
}
return _g.__buildVersion;
}