diff --git a/internal/application/service/session.go b/internal/application/service/session.go index 80d8ff3e..f0c6d5ab 100644 --- a/internal/application/service/session.go +++ b/internal/application/service/session.go @@ -707,6 +707,26 @@ func (s *sessionService) destroyBoundSandbox(ctx context.Context, sessionID stri } } +// maxSessionTitleRunes bounds the auto-generated session title. sessions.title +// is VARCHAR(255) in every shipped migration, so an over-long model response +// would be rejected by the database; 100 runes stays well clear of that limit +// while still being a reasonable title length in the UI. +const maxSessionTitleRunes = 100 + +// sanitizeGeneratedTitle turns a raw title completion into something safe to +// persist: the reasoning prefix some models emit is dropped, surrounding +// whitespace is trimmed, and the result is truncated by rune (not byte) so a +// multi-byte character is never cut in half. It reports whether truncation +// happened so the caller can log it. +func sanitizeGeneratedTitle(raw string) (string, bool) { + title := strings.TrimSpace(strings.TrimPrefix(raw, "\n\n")) + runes := []rune(title) + if len(runes) <= maxSessionTitleRunes { + return title, false + } + return strings.TrimSpace(string(runes[:maxSessionTitleRunes])), true +} + // GenerateTitle generates a title for the current conversation content // modelID: optional model ID to use for title generation (if empty, uses first available KnowledgeQA model) func (s *sessionService) GenerateTitle(ctx context.Context, @@ -804,7 +824,14 @@ func (s *sessionService) GenerateTitle(ctx context.Context, } // Process and store the generated title - session.Title = strings.TrimPrefix(response.Content, "\n\n") + title, truncated := sanitizeGeneratedTitle(response.Content) + if truncated { + logger.Warnf(ctx, + "Generated session title exceeded %d runes and was truncated, session=%s, model=%s", + maxSessionTitleRunes, session.ID, modelID, + ) + } + session.Title = title // Update session with new title _, err = s.sessionRepo.Update(ctx, session, session.UserID) diff --git a/internal/application/service/session_title_test.go b/internal/application/service/session_title_test.go new file mode 100644 index 00000000..45d57ed8 --- /dev/null +++ b/internal/application/service/session_title_test.go @@ -0,0 +1,78 @@ +package service + +import ( + "strings" + "testing" +) + +func TestSanitizeGeneratedTitle(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + raw string + want string + wantTruncated bool + }{ + { + name: "plain title is kept as is", + raw: "保单理赔流程咨询", + want: "保单理赔流程咨询", + }, + { + name: "thinking prefix and surrounding whitespace are dropped", + raw: "\n\n Claim filing steps \n", + want: "Claim filing steps", + }, + { + name: "over-long ascii title is truncated", + raw: strings.Repeat("a", 300), + want: strings.Repeat("a", maxSessionTitleRunes), + wantTruncated: true, + }, + { + name: "over-long cjk title is truncated by rune, not byte", + raw: strings.Repeat("保", 300), + want: strings.Repeat("保", maxSessionTitleRunes), + wantTruncated: true, + }, + { + name: "title exactly at the limit is not truncated", + raw: strings.Repeat("保", maxSessionTitleRunes), + want: strings.Repeat("保", maxSessionTitleRunes), + }, + { + name: "empty completion stays empty", + raw: " ", + want: "", + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + got, truncated := sanitizeGeneratedTitle(tt.raw) + if got != tt.want { + t.Fatalf("title = %q, want %q", got, tt.want) + } + if truncated != tt.wantTruncated { + t.Fatalf("truncated = %v, want %v", truncated, tt.wantTruncated) + } + if len([]rune(got)) > maxSessionTitleRunes { + t.Fatalf("title still exceeds %d runes: %d", maxSessionTitleRunes, len([]rune(got))) + } + }) + } +} + +// The database column is VARCHAR(255) in every shipped migration; guard the +// constant so nobody raises it past what the column can hold. +func TestMaxSessionTitleRunesFitsColumn(t *testing.T) { + t.Parallel() + // Worst case for UTF-8 is 4 bytes per rune, but the column counts characters + // in PostgreSQL and bytes in some engines, so keep a conservative bound. + if maxSessionTitleRunes > 255 { + t.Fatalf("maxSessionTitleRunes=%d exceeds the sessions.title column limit", maxSessionTitleRunes) + } +}