T-89: M1.1n — Security-master run CLI: read-only Mongo sources + bar spans → composeSecurityMasterRun → match report files, with opt-in persist to dev PostgreSQL (no new dependencies)
This commit is contained in:
parent
d691d64260
commit
d1057c41f3
4 changed files with 210 additions and 17 deletions
|
|
@ -154,22 +154,34 @@ MONGODB_READ_URI="mongodb://studio_stock_bot_read:<password>@localhost:27017/sto
|
|||
--out-dir ./tmp/security-master-report
|
||||
```
|
||||
|
||||
- **Scope of `--as-of <YYYY-MM-DD>`:** `--as-of` sets the calendar reference date for Pass 6 delisting classification (active grace window vs. delisted outcome). It does **not** take a historical point-in-time snapshot of MongoDB — source symbol records and bar spans are read from the current collections, so the report can incorporate provider observations recorded after `--as-of`.
|
||||
|
||||
### Persist to isolated dev PostgreSQL (`localhost:5433/trading_bot_dev`)
|
||||
Ensure the `master` track migrations have been applied to dev PostgreSQL first (`bun run db:migrate --track master`), then run with `--persist`:
|
||||
Apply the `master` track migrations to isolated dev PostgreSQL first (passing the dev target variables explicitly so a QA-configured shell cannot migrate QA):
|
||||
|
||||
```bash
|
||||
NODE_ENV=development \
|
||||
POSTGRES_HOST=localhost \
|
||||
POSTGRES_PORT=5433 \
|
||||
POSTGRES_DATABASE=trading_bot_dev \
|
||||
bun run db:migrate --track master
|
||||
```
|
||||
|
||||
Then run the CLI with `--persist` against the same isolated dev PostgreSQL target:
|
||||
|
||||
```bash
|
||||
NODE_ENV=development \
|
||||
MONGODB_READ_URI="mongodb://studio_stock_bot_read:<password>@localhost:27017/stock?authSource=stock" \
|
||||
POSTGRES_HOST=localhost \
|
||||
POSTGRES_PORT=5433 \
|
||||
POSTGRES_DB=trading_bot_dev \
|
||||
POSTGRES_DATABASE=trading_bot_dev \
|
||||
bun run apps/stock/data-pipeline/src/security-master/run-security-master.ts \
|
||||
--as-of 2026-01-15 \
|
||||
--out-dir ./tmp/security-master-report \
|
||||
--persist
|
||||
```
|
||||
|
||||
- **Safety guards:** MongoDB is always opened with `mongoReadOnly: true` via `MONGODB_READ_URI`. Without `--persist`, no PostgreSQL client is constructed. With `--persist`, `assertEnvironmentSafety()` runs before opening PostgreSQL (with `STOCK_BOT_ALLOW_DEV_ON_QA_DB` stripped) so `development` runs cannot write to QA PostgreSQL (`localhost:5432/trading_bot`).
|
||||
- **Safety guards:** MongoDB is always opened with `mongoReadOnly: true` via `MONGODB_READ_URI`. Without `--persist`, no PostgreSQL client is constructed. With `--persist`, `assertSecurityMasterPersistSafety()` runs `assertEnvironmentSafety()` (with `STOCK_BOT_ALLOW_DEV_ON_QA_DB` stripped) and enforces `NODE_ENV=development` against an isolated dev PostgreSQL database (`*_dev` / `stock_bot_safety*`), blocking `NODE_ENV=qa` and QA PostgreSQL (`localhost:5432/trading_bot`) before any PostgreSQL client is created.
|
||||
- **Pass 4 status:** Pass 4 cross-sectional price cross-check input (`sampleCloses` / `fxRates`) is **not yet wired** into this CLI; `composeSecurityMasterRun` is invoked without sample closes and Pass 4 is explicitly reported as `skipped` (`pass4: null`).
|
||||
|
||||
### Output files (`--out-dir`)
|
||||
|
|
|
|||
|
|
@ -216,6 +216,7 @@ export {
|
|||
} from './run';
|
||||
|
||||
export {
|
||||
assertSecurityMasterPersistSafety,
|
||||
formatRunSecurityMasterSummary,
|
||||
parseRunSecurityMasterArgs,
|
||||
RUN_SECURITY_MASTER_USAGE,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,11 @@ import { dirname, join } from 'node:path';
|
|||
import { getLogger } from '@stock-bot/logger';
|
||||
import { MongoDBClient } from '@stock-bot/mongodb';
|
||||
import { PostgreSQLClient } from '@stock-bot/postgres';
|
||||
import { assertEnvironmentSafety, initializeStockConfig } from '@stock-bot/stock-config';
|
||||
import {
|
||||
assertEnvironmentSafety,
|
||||
initializeStockConfig,
|
||||
type StockAppConfig,
|
||||
} from '@stock-bot/stock-config';
|
||||
import {
|
||||
computeBarSpans,
|
||||
normalizeBarDate,
|
||||
|
|
@ -55,7 +59,7 @@ export const RUN_SECURITY_MASTER_USAGE = [
|
|||
'Options:',
|
||||
' --as-of <YYYY-MM-DD> Required. Calendar reference date for Pass 6 delisting classification.',
|
||||
' --out-dir <dir> Required. Output directory for match-report.md, conflicts.csv, override-template.sql, and run-stats.json.',
|
||||
' --persist Optional. Persist security-master rows to PostgreSQL after running assertEnvironmentSafety() (default: off).',
|
||||
' --persist Optional. Persist security-master rows to isolated dev PostgreSQL after running safety guards (default: off).',
|
||||
' --help Show this help message and exit.',
|
||||
].join('\n');
|
||||
|
||||
|
|
@ -220,6 +224,40 @@ export function parseRunSecurityMasterArgs(argv: readonly string[]): ParsedRunSe
|
|||
};
|
||||
}
|
||||
|
||||
export function assertSecurityMasterPersistSafety(
|
||||
config: StockAppConfig,
|
||||
env: Record<string, string | undefined> = process.env
|
||||
): void {
|
||||
const pg = config.database.postgres;
|
||||
if (!pg.enabled) {
|
||||
throw new Error('PostgreSQL is disabled in configuration; cannot run with --persist');
|
||||
}
|
||||
|
||||
const { STOCK_BOT_ALLOW_DEV_ON_QA_DB: _stripped, ...safeEnv } = env;
|
||||
assertEnvironmentSafety(config, 'dataPipeline', safeEnv);
|
||||
|
||||
if (config.environment !== 'development') {
|
||||
throw new Error(
|
||||
`Security master --persist is restricted to NODE_ENV=development targeting isolated dev PostgreSQL ` +
|
||||
`(got environment "${config.environment}", target ${pg.host}:${pg.port}/${pg.database}).`
|
||||
);
|
||||
}
|
||||
|
||||
if (pg.database === 'trading_bot' || pg.port === 5432) {
|
||||
throw new Error(
|
||||
`Security master --persist cannot target QA PostgreSQL (${pg.host}:${pg.port}/${pg.database}); ` +
|
||||
'configure isolated dev PostgreSQL (localhost:5433/trading_bot_dev).'
|
||||
);
|
||||
}
|
||||
|
||||
if (!pg.database.endsWith('_dev') && !pg.database.startsWith('stock_bot_safety')) {
|
||||
throw new Error(
|
||||
`Security master --persist requires an isolated dev PostgreSQL database ` +
|
||||
`(ending in "_dev" or "stock_bot_safety*"), got "${pg.database}".`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function formatRunSecurityMasterSummary(
|
||||
params: Pick<
|
||||
RunSecurityMasterOutput,
|
||||
|
|
@ -471,7 +509,7 @@ export async function runSecurityMaster(
|
|||
}
|
||||
}
|
||||
|
||||
// Step 8: Emit concise one-screen summary after all steps succeed.
|
||||
// Step 8: Build concise one-screen summary (emitted only after cleanup succeeds).
|
||||
const summary = formatRunSecurityMasterSummary({
|
||||
asOfDate: options.asOfDate,
|
||||
outDir: options.outDir,
|
||||
|
|
@ -479,12 +517,6 @@ export async function runSecurityMaster(
|
|||
filesWritten,
|
||||
composed,
|
||||
});
|
||||
const writeStdout =
|
||||
deps.writeStdout ??
|
||||
((text: string) => {
|
||||
process.stdout.write(text);
|
||||
});
|
||||
writeStdout(summary);
|
||||
|
||||
output = {
|
||||
asOfDate: options.asOfDate,
|
||||
|
|
@ -514,6 +546,13 @@ export async function runSecurityMaster(
|
|||
throw wrapStepError('close-mongo', mongoCloseError);
|
||||
}
|
||||
|
||||
const writeStdout =
|
||||
deps.writeStdout ??
|
||||
((text: string) => {
|
||||
process.stdout.write(text);
|
||||
});
|
||||
writeStdout(output.summary);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
|
@ -532,11 +571,7 @@ export async function runCli(argv: readonly string[] = process.argv.slice(2)): P
|
|||
await mongo.connect();
|
||||
|
||||
const assertSafety = (): void => {
|
||||
if (!config.database.postgres.enabled) {
|
||||
throw new Error('PostgreSQL is disabled in configuration; cannot run with --persist');
|
||||
}
|
||||
const { STOCK_BOT_ALLOW_DEV_ON_QA_DB: _stripped, ...safeEnv } = process.env;
|
||||
assertEnvironmentSafety(config, 'dataPipeline', safeEnv);
|
||||
assertSecurityMasterPersistSafety(config, process.env);
|
||||
};
|
||||
|
||||
const createPostgresClient = async (): Promise<SecurityMasterPostgresHandle> => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue