Story Factory Development Workflows
Common multi-step workflows derived from commit history patterns. All file paths verified against the actual codebase.
Add New Setting
Settings always require changes across 4 files:
- •Add field to
src/settings/_settings.py(Settings class) - •Add validation in
src/settings/_validation.py - •Expose in UI in the relevant
src/ui/pages/settings/_*.pypage - •Add tests in
tests/unit/test_settings.py
Add New Entity Type
When adding a new entity type that participates in the quality loop (like locations, factions, items, concepts, events):
- •Create/update Pydantic models in
src/memory/story_state.pyorsrc/memory/entities.py - •Add quality scores model in
src/memory/world_quality/_entity_scores.py - •Create quality service module in
src/services/world_quality_service/_entityname.pyimplementing the create/judge/refine pattern (see existing_location.py,_faction.py,_item.py,_concept.py,_event.pyfor reference) - •Add entity to build pipeline in
src/services/world_service/_build.py— follows the same_create_fn/_judge_fn/_refine_fnpattern viaquality_refinement_loop() - •Add UI page in
src/ui/pages/world/for browsing/editing the new entity type - •Add comprehensive tests in
tests/unit/mirroring the source structure
Database Schema Migration
When modifying the WorldDatabase schema:
- •Update schema version and add migration in
src/memory/world_database/_schema.py - •Add database operations in
src/memory/world_database/— use a dedicated submodule if the operations are substantial (e.g.,_cycles.py,_events.py,_embeddings.py) - •Update
__init__.pyinsrc/memory/world_database/to expose new operations - •Update related services that consume the new data
- •Add migration tests and integration tests
Quality Loop Improvements
When modifying quality scoring or the refinement loop:
- •Update loop logic in
src/services/world_quality_service/_quality_loop.py - •Modify scoring models in
src/memory/world_quality/_models.pyorsrc/memory/world_quality/_entity_scores.py - •Update analytics in
src/services/world_quality_service/_analytics.py - •Update settings in
src/settings/_settings.pyand expose insrc/ui/pages/settings/ - •Add tests for new scoring/loop behaviors
World Building Pipeline Improvements
When improving entity generation, relationships, or pipeline reliability:
- •Update build pipeline in
src/services/world_service/_build.py - •Modify entity generation in the relevant
src/services/world_quality_service/_entityname.pymodule - •Update relationship generation in
src/services/world_quality_service/_relationship.py - •Update health metrics in
src/services/world_service/_health.py - •Add build tests covering the pipeline changes
Investigation Script Creation
When diagnosing model behavior, performance, or data issues:
- •Create investigation script as
scripts/investigate_*.py(see existing:investigate_vram_usage.py,investigate_build_performance.py,investigate_schema_compliance.py, etc.) - •Use shared helpers from
scripts/_ollama_helpers.pyfor Ollama interactions - •Respect the 80% GPU residency rule (
MIN_GPU_RESIDENCYinsrc/services/model_mode_service/_vram.py) when testing models - •Document findings and implement fixes in the main codebase
UI Component Enhancement
When adding or improving UI pages/components:
- •Update component in
src/ui/pages/(use package structure for complex pages) - •Modify related services for data fetching — services live in
src/services/, never import UI modules - •Update graph renderer in
src/ui/pages/world/_graph.pyif visualization changes are needed - •Add component tests in
tests/component/using NiceGUI User fixture
Modularization Pattern
When a file exceeds ~800 lines, split it into a package with underscore-prefixed private modules:
code
# Before: single large file src/services/world_quality_service.py # After: package with focused modules src/services/world_quality_service/ ├── __init__.py # Public API, re-exports ├── _quality_loop.py # Core loop logic ├── _character.py # Character-specific logic ├── _location.py # Location-specific logic ├── _batch.py # Shared batch helpers └── _analytics.py # Scoring analytics
Split by domain/responsibility, not by type. Pre-commit enforces a 1000-line limit.
Naming Conventions
| Element | Convention | Examples |
|---|---|---|
| Files/modules | snake_case | story_state.py, world_database.py |
| Private modules | underscore prefix | _schema.py, _quality_loop.py, _analytics.py |
| Classes | PascalCase | StoryState, WorldDatabase, BaseAgent |
| Functions/methods | snake_case | build_world(), generate_structured() |
| Constants | SCREAMING_SNAKE_CASE | MIN_GPU_RESIDENCY, RECOMMENDED_MODELS |
| Test files | test_ prefix | test_settings.py, test_world_service.py |
| Scripts | verb_noun pattern | investigate_vram_usage.py, evaluate_models.py |
Git Conventions
- •Conventional commits:
type: description— types:feat,fix,refactor,docs,test,chore,perf,ci - •PR references: Include
(#123)in commit messages when related to a PR/issue - •Never amend or force push — always create new commits