Skip to main content

Module topic

Module topic 

Source
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_THRESHOLD signals 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_budget uses 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§

TopicSegment
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.