fix(monitoring): report observed research and paper readiness (O02)

This commit is contained in:
Boki 2026-10-28 12:58:38 -04:00
parent 70d173a908
commit 8c858b9d5c
23 changed files with 1340 additions and 319 deletions

View file

@ -5,6 +5,7 @@
* mutable fields never represent a shared paper account or several simultaneous runs.
*/
import { EventEmitter } from 'node:events';
import { redactLogValue } from '@stock-bot/logger';
import type { Logger } from '@stock-bot/services';
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
import {
@ -323,6 +324,10 @@ export class BacktestRunner extends EventEmitter {
} catch (error) {
await this.progressWrites;
let failure = this.leaseFailure ?? deadlineFailure ?? error;
const publicFailure = () =>
new Error(
`Backtest failed during ${this.phase}: ${redactLogValue(failure instanceof Error ? failure.message : String(failure))}`
);
// After lease loss, another worker may own the run. Even a failure/cancellation
// write would be an unauthorized state change, so leave recovery to the store.
if (this.active && !(failure instanceof BacktestLeaseLostError)) {
@ -332,7 +337,7 @@ export class BacktestRunner extends EventEmitter {
} else {
await this.dependencies.repository.fail(
this.active.runId,
failure instanceof Error ? failure.message : String(failure),
publicFailure().message,
this.active.attemptId
);
}
@ -351,7 +356,7 @@ export class BacktestRunner extends EventEmitter {
status: 'cancelled',
});
} else if (this.listenerCount('error')) {
this.publish('error', failure);
this.publish('error', publicFailure());
}
throw failure;
} finally {
@ -453,6 +458,16 @@ export class BacktestRunner extends EventEmitter {
this.requireActive(runId);
throw new Error('Native backtests run at maximum speed');
}
/** A local attempt snapshot; PostgreSQL remains authoritative for leases. */
getAttemptDiagnostics() {
return {
runId: this.active?.runId ?? null,
backtestId: this.active?.backtestId ?? null,
attemptId: this.active?.attemptId ?? null,
strategyVersionId: this.active?.config.strategy.versionId ?? null,
phase: this.busy ? this.phase : ('idle' as const),
};
}
/** Local worker projection; persisted run status remains authoritative after restart. */
getStatus() {
return {