# Stock Bot — Studio Agent Guide ## Runtime & Tooling - Use **Bun** (`bun`, `bun test`, `bun run`) and **Turbo** (`turbo`) for builds, scripts, and tests. - Build: `bun run build` - Typecheck: `bun run typecheck` - Tests: `bun test` (PostgreSQL safety tests require `PIPELINE_TEST_DATABASE_URL` pointing to a dedicated `stock_bot_safety*` database, configured via `/home/boki/.studio/test-env/stock-bot.env`). ### Check Gates & Sandbox Split - **Why `bun run check` (`scripts/check.ts`) cannot pass end-to-end inside the agent sandbox**: worktree `.git` metadata lives outside the sandbox (breaking `audit:secrets` and the worktree-git tooling tests in `scripts/test/audit-secrets-repo.test.ts`, `strategy-packaging.test.ts`, `benchmark-research.test.ts`, `benchmark-replay.test.ts`, and `benchmark-sma.test.ts`), and sandbox network isolation blocks host PostgreSQL `127.0.0.1:5432` for `PIPELINE_TEST_DATABASE_URL` suites. - **Sandbox-runnable gates (run locally before handoff)**: `bun run typecheck`, `bun run test:contracts`, `bun run lint:check`, `CARGO_BUILD_JOBS=2 bun run test:native`, `bun run test:utils`, `bun run test:calculations`, and targeted non-git/non-Postgres tests (e.g. `bun test scripts/test/security.test.ts`). - **Studio unsandboxed verification (`bun run build` and `bun --env-file=/home/boki/.studio/test-env/stock-bot.env test`)**: authoritatively covers `build`, `audit:secrets` (via `scripts/test/audit-secrets-repo.test.ts`), `test:tooling` (including all worktree-git tests), `test:integration`, and PostgreSQL-backed tests. Sandbox-only `.git`/PostgreSQL failures are not a review deadlock when Studio's unsandboxed run is green, and agents must never skip or mock out those checks to make them pass inside the sandbox. - **Full host verification outside a sandbox**: run `CARGO_BUILD_JOBS=2 bun run check`. ## Environments (`dev` / `qa` / `prod`) | Environment | `NODE_ENV` | Compose File | PostgreSQL | MongoDB (Write) | MongoDB (Read / Pipeline) | Dragonfly | QuestDB | | --- | --- | --- | --- | --- | --- | --- | --- | | **dev** | `development` | `docker-compose.dev.yml` | `localhost:5433` (`trading_bot_dev`) | `localhost:27018` (`stock_dev`) | `localhost:27017` (`stock`, **read-only** via `MONGODB_READ_URI`) | `localhost:6380` | `localhost:9100` | | **qa** | `qa` | `docker-compose.yml` | `localhost:5432` (`trading_bot`) | `localhost:27017` (`stock`) | `localhost:27017` (`stock`) | `localhost:6379` | `localhost:9000` | | **prod** | `production` | `docker-compose.prod.yml` | Separate host (env-driven) | Separate host (env-driven) | Separate host (`MONGODB_READ_URI` optional) | Env-driven | Env-driven | ## Data Flow & Strict Safety Rules 1. **QA MongoDB (`stock-bot_mongodb_data` on `localhost:27017`, database `stock`) holds the full ingested multi-billion-document dataset. NEVER drop, reset, or write to QA MongoDB from `dev` or automated tests.** - Never run `docker compose down -v` against `docker-compose.yml`. - `./scripts/docker.sh reset` is hard-blocked for `qa` and `prod`. 2. **`data-pipeline` reads MongoDB and writes master records to PostgreSQL.** - `apps/stock/data-pipeline` initializes its container with `mongoReadOnly: true`, which uses `database.mongodb.readUri` (`MONGODB_READ_URI`) when set and rejects all MongoDB write/index/collection-mutation operations at the client layer. - In `dev`, `data-pipeline` reads QA MongoDB read-only (`studio_stock_bot_read@stock`) and writes master records into isolated dev PostgreSQL (`localhost:5433/trading_bot_dev`). 3. **`data-ingestion` writes raw provider data to MongoDB.** - In `dev`, `data-ingestion` must target isolated dev MongoDB (`localhost:27018/stock_dev`). - `assertEnvironmentSafety()` in `apps/stock/config/src/env-safety.ts` blocks `development` services from opening writable connections to QA MongoDB (`27017/stock`) or QA PostgreSQL (`5432/trading_bot`). 4. **Tests** must never mutate QA Postgres (`trading_bot`) or QA MongoDB (`stock`). Use isolated schemas in `stock_bot_safety*` or in-memory/testcontainer databases. ## Key Modules - `apps/stock/data-ingestion`: Provider crawlers/ingesters writing raw documents into MongoDB. - `apps/stock/data-pipeline`: Processes raw MongoDB documents into normalized master records in PostgreSQL (`exchanges`, `symbols`, `provider_exchange_mappings`, `provider_mappings`, `sync_status`). - `apps/stock/config` & `libs/core/config`: Environment config schemas (`development`, `qa`, `test`, `production`) and startup safety guards. - `apps/stock/web-api` & `apps/stock/web-app`: API and dashboard UI. - `apps/stock/orchestrator` & `apps/stock/engine`: Backtest/paper/live orchestration and Rust execution engine. ## Documentation - `docs/README.md`: Documentation index and navigation map. - `docs/PLATFORM-PLAN.md`: Target platform architecture and locked decisions (`D1`–`D12`). - `docs/MILESTONES.md`: Ordered `M0`–`M5` build plan, file targets, and exit criteria. - `docs/DATA-CONTRACTS.md`: Empirical MongoDB provider rules, PostgreSQL security master, DuckDB/Parquet, and QuestDB `10.2.0` schemas. - `docs/VALIDATION-AND-RIGOR.md`: Canonical metrics, `trial_ledger`, DSR/PBO/CPCV math, 6-month vault + post-2023 recency gate, and IBKR Margin (`USD`/`CAD`) execution rules. - `docs/servers.md`: Hardware and container topology (`quant` `10.0.0.51`, `9900K`, `TrueNAS`, local dev).