Repository: humanlayer/humanlayer
File: hld/store/sqlite.go (Go)

Code before:
}

// SearchSessionsByTitle searches for sessions by title using SQL LIKE
func (s *SQLiteStore) SearchSessionsByTitle(ctx context.Context, query string, limit int) ([]*Session, error) {
	if limit <= 0 {
		limit = 10
	}
	if limit > 50 {
		limit = 50
	}

	// Build the SQL query with LIKE pattern
	// Use % wildcards for substring matching anywhere in title
	sqlQuery := `
		SELECT id, run_id, claude_session_id, parent_session_id,
			query, summary, title, model, model_id, working_dir, max_turns, system_prompt, append_system_prompt, custom_instructions,
			permission_prompt_tool, allowed_tools, disallowed_tools,
			status, created_at, last_activity_at, completed_at,
			cost_usd, input_tokens, output_tokens, cache_creation_input_tokens, cache_read_input_tokens, effective_context_tokens,
			duration_ms, num_turns, result_content, error_message, auto_accept_edits, archived,
			dangerously_skip_permissions, dangerously_skip_permissions_expires_at, dangerously_skip_permissions_timeout_ms,
			proxy_enabled, proxy_base_url, proxy_model_override, proxy_api_key, additional_directories, editor_state
		FROM sessions
		WHERE 1=1`

	var args []interface{}

	// Only add WHERE clause if query is not empty
	if query != "" {
		sqlQuery += " AND title LIKE ?"
		args = append(args, "%"+query+"%")
	}

	// Order by last activity and limit
	// Look at 20 most recent sessions for performance
	sqlQuery += `
		ORDER BY last_activity_at DESC
		LIMIT 20`

	// Then apply the user's limit in Go after fetching
	rows, err := s.db.QueryContext(ctx, sqlQuery, args...)
	if err != nil {
		return nil, fmt.Errorf("search sessions query: %w", err)
	}
	defer func() { _ = rows.Close() }()

	var sessions []*Session
	for rows.Next() {
		var session Session
		var claudeSessionID, parentSessionID, summary, title, model, modelID, workingDir, systemPrompt, appendSystemPrompt, customInstructions sql.NullString
		var permissionPromptTool, allowedTools, disallowedTools sql.NullString

Review comment (refactor):
I don't think the `WHERE 1=1` is necessary here, curious why it's here - couldn't you just do 

`WHERE status != 'draft' AND status != 'discarded' AND (archived IS NULL OR archived = 0) AND NOT EXISTS (...)` ?

Also can't wait to refactor the data structure here so that we don't have the subquery

Code after:
}

// SearchSessionsByTitle searches for sessions by title using SQL LIKE
func (s *SQLiteStore) SearchSessionsByTitle(ctx context.Context, query string, limit int) ([]*Session, error) {
	if limit <= 0 {
		limit = 10
	}
	if limit > 50 {
		limit = 50
	}

	// Build the SQL query with LIKE pattern
	// Use % wildcards for substring matching anywhere in title
	// Filter to only leaf sessions (sessions with no children)
	// Filter to only "normal" sessions (not archived, not draft, not discarded)
	sqlQuery := `
		SELECT id, run_id, claude_session_id, parent_session_id,
			query, summary, title, model, model_id, working_dir, max_turns, system_prompt, append_system_prompt, custom_instructions,
			permission_prompt_tool, allowed_tools, disallowed_tools,
			status, created_at, last_activity_at, completed_at,
			cost_usd, input_tokens, output_tokens, cache_creation_input_tokens, cache_read_input_tokens, effective_context_tokens,
			duration_ms, num_turns, result_content, error_message, auto_accept_edits, archived,
			dangerously_skip_permissions, dangerously_skip_permissions_expires_at, dangerously_skip_permissions_timeout_ms,
			proxy_enabled, proxy_base_url, proxy_model_override, proxy_api_key, additional_directories, editor_state
		FROM sessions
		WHERE 1=1
		AND NOT EXISTS (
			SELECT 1 FROM sessions children
			WHERE children.parent_session_id = sessions.id
		)
		AND (archived IS NULL OR archived = 0)
		AND status != 'draft'
		AND status != 'discarded'`

	var args []interface{}

	// Search across title, summary, and query fields to match UI display logic
	// UI shows: title || summary || query, so search should match this behavior
	if query != "" {
		sqlQuery += " AND (title LIKE ? OR summary LIKE ? OR query LIKE ?)"
		args = append(args, "%"+query+"%", "%"+query+"%", "%"+query+"%")
	}

	// Order by last activity and limit
	// Look at 20 most recent sessions for performance
	sqlQuery += `
		ORDER BY last_activity_at DESC
		LIMIT 20`

	// Then apply the user's limit in Go after fetching
	rows, err := s.db.QueryContext(ctx, sqlQuery, args...)