Expand description
Topic segmentation for session conversation threads.
Detects topic changes within a session so the context builder can prioritize messages from the current topic over stale threads. This prevents the model from confusing concurrent conversation topics (e.g. workspace cleanup vs. security monitoring vs. Go migration) in long-running sessions.
§Architecture
- Detection: Compares the user’s new message against the most recent
assistant response via cosine similarity on embeddings. A drop below
the
TOPIC_CONTINUITY_THRESHOLDsignals a topic shift. - Tagging: Each message gets a
topic_tag(e.g."topic-3") stored in the database. Sequential messages on the same topic share a tag. - Context assembly:
build_context_with_budgetuses topic tags to ensure the current topic’s messages are prioritized within the token budget, with older topics getting a compressed summary instead of full message history.
Structs§
- Topic
Segment - Represents a detected topic boundary in a conversation.
Constants§
- TOPIC_
CONTINUITY_ THRESHOLD - Cosine similarity threshold below which a new message is considered a topic change from the previous conversation thread.
Functions§
- detect_
topic_ shift - Detect whether a new user message is a continuation of the current topic or a shift to a new one.
- partition_
by_ topic - Given a list of messages with topic tags, partition them into current-topic messages and off-topic messages.
- summarize_
topic_ block - Generate a compressed summary line for an off-topic message block. Used in context assembly to represent trimmed topics as a single system note rather than including all their messages verbatim.