Skip to main content

New Strategy Entity, Chat CRUD, and Schema Sync

Summary​

Blank bot creation now creates a real versioned strategy entity named New Strategy instead of leaving the bot with no active bot_strategies row. The AI chat can now create, inspect, update, and reset strategy entities, and every strategy code save path synchronizes code-used parameters into params and parameter_schema.properties.

This fixes the backtest failure for newly created blank bots:

Unsupported strategy: . Provide strategyId/strategySnapshotId or codeText, or select a different strategy.

Root Cause​

createBot only inserted a bot_strategies snapshot when bot.strategy.trim() was non-empty. A bot created with an empty strategy therefore had no strategy entity, no snapshot id, and no code/snapshot fallback for backtests.

The chat layer also detected code/schema drift but mostly reported it back to the model instead of repairing the stored defaults and schema. That allowed strategy code to reference parameters that did not exist in the saved schema or default parameter payload.

Changes​

Strategy lifecycle​

  • insertBotStrategyVersion now normalizes blank names to New Strategy at the repository boundary.
  • createBot always seeds an initial bot_strategies row, even when the requested strategy is empty.
  • Legacy blank bots are repaired lazily through the chat resolver and receive a New Strategy snapshot.
  • Bot creation greeting text now describes the blank New Strategy entity instead of implying there is no strategy.

AI chat strategy tools​

  • Added create_strategy for creating or replacing the active strategy entity.
  • Added delete_strategy as a non-destructive reset to a blank New Strategy snapshot.
  • Existing get_strategy, metadata, schema, and code-write tools now use normalized strategy display names.
  • Strategy mutation tools respect shared-bot permissions through the existing read/write tool filtering.

Parameter schema synchronization​

Code-used parameters are now inferred and merged into saved strategy state whenever code is persisted through:

  • commit_strategy_files
  • write_strategy_code
  • update_strategy_code file-scoped edits
  • update_strategy_code surgical edits
  • update_strategy_code full generated rewrites

The sync adds missing keys to both params and parameter_schema.properties. It preserves existing user-specified values and does not delete existing fields.

Backtest fallback repair​

  • Chat backtest tools now merge missing defaults from the active strategy snapshot before running.
  • Backtest requests carry the active strategy snapshot id and code when available.
  • Strategy labels are normalized so blank legacy labels do not flow into the runner.
  • The runner can now pair explicit strategyCodeText with a requested snapshot's saved params and parameter_schema, so explicit-code backtests still get the synced schema/default metadata.

Parameter mutation fix​

parseBotParameters now clones object and parsed JSON values before use. This prevents shared object mutation between tool calls and removed hidden state leakage in tests and runtime paths.

Files Changed​

  • services/backend/src/routes/bots/bot-strategies-repository.ts
  • services/backend/src/routes/bots/bots-service.ts
  • services/backend/src/routes/bots/bots-chat-handlers.ts
  • services/backend/src/lib/backtest-engine/runner.ts
  • services/backend/tests/bot-chat-tools.test.ts

Validation​

Executed successfully:

cd projects/quantago/services/backend && pnpm test:tools
cd projects/quantago/services/backend && pnpm typecheck
cd projects/quantago/services/backend && pnpm lint
cd projects/quantago && pnpm typecheck
cd projects/quantago && pnpm lint
cd /Users/adam/github/saas-stack && pnpm typecheck
cd /Users/adam/github/saas-stack && pnpm lint

Results:

  • Bot chat tool suite: 89/89 passing.
  • Backend typecheck: clean.
  • Backend lint: clean.
  • Quantago project typecheck: clean.
  • Quantago project lint: clean.
  • Root typecheck: clean.
  • Root lint: passed with one non-fatal Biome schema-version info for templates/base/biome.json.

Notes​

The delete_strategy chat tool intentionally resets the active strategy to a new blank snapshot instead of deleting historical rows. That keeps version history intact while giving the user the expected fresh New Strategy state.