Internal Code Contracts
Purpose
This page defines how internal classes and modules are documented in VibeCheck. It complements the class diagram and is intended for maintainers implementing or changing runtime logic.
Implemented Contracts
Class: PromptTracker
Purpose: Tracks per-workspace prompt usage metrics and response counts in MongoDB.
Fields
| Field | Type | Visibility | Purpose | Invariants |
|---|---|---|---|---|
| _col | Mongo collection handle | private | Stores prompt statistics documents | Bound to vibecheck.prompt_stats |
| _active_prompt | dict[(team_id, channel_id), prompt_id] | private | Maps channel/workspace to active prompt for response counting | Keys are tuple(team_id, channel), values are string prompt ids |
Public Methods
init(mongo_uri: str) -> None
- Purpose: Initialize MongoDB connection and runtime index map.
- Preconditions: mongo_uri is valid and reachable by runtime.
- Postconditions: collection handle and active prompt map are initialized.
- Throws: PyMongo connection exceptions when connection cannot be created.
- Side-effects: opens DB client connection.
- Retry and idempotency: constructor call is not idempotent at process level.
record_prompt_sent(prompt_id: str, prompt_text: str, tags: str, channel: str, team_id: str) -> None
- Purpose: Upsert prompt stats and increment times_asked.
- Preconditions:
- team_id and channel identify a workspace context.
- prompt_id and prompt_text are non-empty for meaningful metrics.
- Postconditions:
- Prompt document exists for (team_id, prompt_id).
- times_asked increased by 1.
- last_asked_at updated to current UTC time.
- active prompt map updated for (team_id, channel).
- Throws:
- Database exceptions on write failure.
- Side-effects: writes to MongoDB and mutates in-memory map.
- Retry and idempotency: not idempotent; retries will increment times_asked again.
record_response(channel: str, team_id: str) -> None
- Purpose: Increment times_responded for active prompt in channel/workspace.
- Preconditions: active prompt exists for (team_id, channel) or operation no-ops.
- Postconditions:
- If active prompt exists, times_responded is incremented by 1.
- If none exists, no DB mutation occurs.
- Throws:
- Database exceptions on write failure.
- Side-effects: writes to MongoDB.
- Retry and idempotency: not idempotent; retries increment counter repeatedly.
get_all_stats(team_id: str) -> list[dict]
- Purpose: Return workspace prompt stats sorted by times_asked descending.
- Preconditions: team_id is provided.
- Postconditions: returns list of documents without Mongo _id field.
- Throws:
- Database exceptions on query failure.
- Side-effects: none outside DB read.
- Retry and idempotency: idempotent read; safe to retry.
Example Usage
from services.mongo_service import PromptTracker
tracker = PromptTracker(mongo_uri)
tracker.record_prompt_sent("12", "Share your current vibe", "social", "C123", "T123")
stats = tracker.get_all_stats("T123")
Class: BotState
Purpose
Stores mutable, per-workspace runtime configuration for scheduling, channels, tags, and one-time prompt overrides.
Fields
| Field | Type | Visibility | Purpose | Invariants |
|---|---|---|---|---|
| _lock | threading.Lock | private | Guards all state reads/writes | Must be held when reading/writing private fields |
| _daily_target_time | Optional[str] | private | Current selected prompt time | Time format should be HH:MM:SS AM/PM when set |
| _active_channel | Optional[str] | private | Channel used for posting/listening | Slack channel identifier semantics |
| _active_token | Optional[str] | private | OAuth bot token for the workspace | Set at channel configuration time |
| _selected_preset | Optional[str] | private | Currently selected preset option key | Values of form time_N |
| _selected_mode | Optional[str] | private | Scheduling mode | Expected values include mode_random, mode_preset, mode_static |
| _random_start_time | Optional[str] | private | Start of random scheduling window | Time format HH:MM:SS AM/PM when set |
| _random_end_time | Optional[str] | private | End of random scheduling window | Time format HH:MM:SS AM/PM when set |
| _static_time | Optional[str] | private | Fixed time used in static mode | Time format HH:MM:SS AM/PM when set |
| _active_days | Set[str] | private | Days when posting is enabled | Members must be valid weekday names |
| _pending_topic | Optional[str] | private | One-time topic override | Cleared after retrieval |
| _active_tags | Set[str] | private | Allowed prompt tag filter | Empty set means all tags allowed |
| _last_prompt_ts | Optional[str] | private | Slack message timestamp of last posted prompt | Used to match response reactions for tracking |
| _reminder_sent | bool | private | Whether reminder DM has been sent for current prompt | Reset to False when a new prompt is posted |
| _reminder_delay_minutes | int | private | Minutes after prompt post before sending reminders | Defaults to 10 |
| _pending_custom_prompt | Optional[str] | private | User-authored prompt text pending posting | Cleared after retrieval |
| _user_prompt_creator_used_today | bool | private | Whether a user has been invited to create today's prompt | Ensures only one invite per day |
| _social_connector_used_today | bool | private | Whether the social connector has run today | Ensures it runs at most once per day |