- Remove ~44k LOC of dead code, legacy v1 TS simulator/strategy stack, event-bus, analytics FastAPI prototype, legacy calculation archives, and unused deps (data-ingestion provider API handlers untouched) - Extract @stock-bot/di/http-security and isolate @stock-bot/browser to data-ingestion via ServiceContainerBuilder.withRegistrations() - Harden @stock-bot/mongodb read-only proxies, flatten @stock-bot/postgres with unified master+research migrations, and fix @stock-bot/config EnvLoader, ServiceApplication worker lifecycle, NamespacedCache.clear(), and BatchProcessor error propagation - Flatten workspace root (remove apps/stock/package.json) and collapse orchestrator re-export shims - Pin all Docker image versions, add shm_size/nofile/RAM caps and docker-compose.quant.yml, fix 05-add-visible-column.sql view recreation, and decouple dev/QA Compose project names for side-by-side execution - Update PLATFORM-PLAN, DATA-CONTRACTS (bars_1m PARTITION BY MONTH, Rust bulk ETL, DuckDB limits), MILESTONES, and servers docs
145 lines
5.1 KiB
TypeScript
145 lines
5.1 KiB
TypeScript
import { z } from 'zod';
|
|
import type { PaperDeploymentStatus } from '@stock-bot/stock-config/paper-deployments';
|
|
import type { PaperReadiness, PaperRuntime } from '@stock-bot/stock-config/paper-runtime';
|
|
import type { CanonicalExecutionRepository, CanonicalPostgres } from '../execution/canonical-paper';
|
|
import {
|
|
authorizedPaperPackage,
|
|
PaperDeploymentError,
|
|
paperHash,
|
|
readPaperDeployment,
|
|
verifyPaperEvidenceCustody,
|
|
} from '@stock-bot/trading-runtime/paper-deployment-custody';
|
|
import type { createPromotedPaperRuntime } from './paper-host';
|
|
import { paperHostUrl, readPaperHostIdentity } from '@stock-bot/trading-runtime/paper-host-identity';
|
|
|
|
export const unboundPaperReadiness = (reason = 'deployment-not-bound'): PaperReadiness => ({
|
|
configured: false,
|
|
accountId: null,
|
|
sourceId: null,
|
|
active: false,
|
|
ready: false,
|
|
operatorHalted: true,
|
|
connected: false,
|
|
reasons: [reason],
|
|
lastReceivedAt: null,
|
|
lastAvailableAt: null,
|
|
requiresHistoricalRecovery: false,
|
|
pendingInput: null,
|
|
pendingBatch: null,
|
|
pendingDecisionSequence: null,
|
|
nativeStatus: null,
|
|
risk: null,
|
|
});
|
|
|
|
/** One explicit paper host. No default account, scheduler, automatic activation or strategy hot swap. */
|
|
export class PaperDeploymentHost {
|
|
private bound?: { id: string; runtime: PaperRuntime };
|
|
private tail: Promise<unknown> = Promise.resolve();
|
|
private readonly startupIdentity = readPaperHostIdentity().catch(() => null);
|
|
constructor(
|
|
private readonly options: {
|
|
database: CanonicalPostgres;
|
|
repository: CanonicalExecutionRepository;
|
|
now?: () => number;
|
|
}
|
|
) {}
|
|
private serial<T>(work: () => Promise<T>): Promise<T> {
|
|
const next = this.tail.then(work, work);
|
|
this.tail = next.catch(() => undefined);
|
|
return next;
|
|
}
|
|
private async authorized(id: string) {
|
|
return this.options.database.transaction(async tx => {
|
|
const deployment = await readPaperDeployment(tx, id);
|
|
const { pkg } = await authorizedPaperPackage(tx, deployment, true);
|
|
await verifyPaperEvidenceCustody(tx, deployment);
|
|
const startup = await this.startupIdentity;
|
|
if (
|
|
!startup ||
|
|
paperHash(startup) !== paperHash(deployment.preview.host) ||
|
|
paperHash(await readPaperHostIdentity()) !== paperHash(deployment.preview.host)
|
|
) {
|
|
throw new PaperDeploymentError(
|
|
409,
|
|
'Installed paper host/native/runtime differs from the reviewed promotion'
|
|
);
|
|
}
|
|
return { deployment, pkg };
|
|
});
|
|
}
|
|
async bind(id: string): Promise<PaperDeploymentStatus> {
|
|
id = z.string().uuid().parse(id).toLowerCase();
|
|
return this.serial(async () => {
|
|
if (this.bound) {
|
|
if (this.bound.id !== id) {
|
|
throw new PaperDeploymentError(
|
|
409,
|
|
'This paper host is already bound; restart with the explicit deployment configuration to change it'
|
|
);
|
|
}
|
|
return this.status(id);
|
|
}
|
|
const { deployment, pkg } = await this.authorized(id);
|
|
const url = new URL(paperHostUrl());
|
|
url.searchParams.set('sha256', deployment.preview.host.hostSha256);
|
|
const module = (await import(url.href)) as {
|
|
createPromotedPaperRuntime: typeof createPromotedPaperRuntime;
|
|
};
|
|
const runtime = await module.createPromotedPaperRuntime({
|
|
deployment,
|
|
pkg,
|
|
repository: this.options.repository,
|
|
now: this.options.now,
|
|
});
|
|
this.bound = { id, runtime };
|
|
return this.status(id);
|
|
});
|
|
}
|
|
async activate(id: string, recoverPending = false): Promise<PaperDeploymentStatus> {
|
|
id = z.string().uuid().parse(id).toLowerCase();
|
|
return this.serial(async () => {
|
|
await this.authorized(id);
|
|
const runtime = this.runtime(id);
|
|
await runtime.activate({ recoverPending });
|
|
return this.status(id);
|
|
});
|
|
}
|
|
async halt(id: string, reason: string): Promise<PaperDeploymentStatus> {
|
|
id = z.string().uuid().parse(id).toLowerCase();
|
|
// Emergency halt remains available even if evidence/build/review later becomes unavailable.
|
|
return this.serial(async () => {
|
|
await this.runtime(id).halt(reason);
|
|
return this.status(id);
|
|
});
|
|
}
|
|
async readiness(): Promise<PaperReadiness> {
|
|
return this.bound ? this.bound.runtime.status() : unboundPaperReadiness();
|
|
}
|
|
|
|
async status(id: string): Promise<PaperDeploymentStatus> {
|
|
id = z.string().uuid().parse(id).toLowerCase();
|
|
await this.options.database.transaction(tx => readPaperDeployment(tx, id));
|
|
return {
|
|
deploymentId: id,
|
|
boundDeploymentId: this.bound?.id ?? null,
|
|
readiness:
|
|
this.bound?.id === id
|
|
? await this.bound.runtime.status()
|
|
: unboundPaperReadiness(
|
|
this.bound ? 'host-bound-to-another-deployment' : 'deployment-not-bound'
|
|
),
|
|
};
|
|
}
|
|
runtime(id?: string): PaperRuntime {
|
|
if (!this.bound || (id !== undefined && this.bound.id !== id)) {
|
|
throw new PaperDeploymentError(409, 'Deployment is not bound to this paper host');
|
|
}
|
|
return this.bound.runtime;
|
|
}
|
|
async shutdown() {
|
|
return this.serial(async () => {
|
|
await this.bound?.runtime.shutdown();
|
|
this.bound = undefined;
|
|
});
|
|
}
|
|
}
|