Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(signals): reject scratchpad text searches the trigram index cannot serve (#127953)

posthog[bot]63d4e88d2091
diff --git a/products/signals/backend/scout_harness/serializers.py b/products/signals/backend/scout_harness/serializers.py
index 8b9c5e15b616..342c4bd5aa73 100644
--- a/products/signals/backend/scout_harness/serializers.py
+++ b/products/signals/backend/scout_harness/serializers.py
@@ -1023,13 +1023,20 @@ class ScratchpadEntrySerializer(serializers.Serializer):
)
+# pg_trgm pads a word where a literal separator bounds it, so `ai_` still yields the trigram "AI ".
+_TRIGRAM_INDEXABLE_TEXT = re.compile(r"[^\W_]{3}|(?<=[\W_])[^\W_]{2}|[^\W_]{2}(?=[\W_])")
+
+
class SearchMemoryQuerySerializer(serializers.Serializer):
"""Query parameters for `search-memory`."""
text = serializers.CharField(
required=False,
allow_blank=True,
- help_text="ILIKE substring match against `content`. Omit to return the most recent entries.",
+ help_text=(
+ "ILIKE substring match against `content` and `key`. Must contain 3 letters or digits in a row, "
+ "or 2 next to a symbol such as `ai_` or `pr:`. Omit to return the most recent entries."
+ ),
)
key = serializers.CharField(
required=False,
@@ -1090,6 +1097,16 @@ class SearchMemoryQuerySerializer(serializers.Serializer):
help_text="Max rows to return (default 20, hard cap 1000).",
)
+ def validate_text(self, value: str) -> str:
+ # The trigram indexes extract nothing from a term without an indexable word, so Postgres
+ # reads and upper-cases every body the team holds to answer it.
+ if value and not _TRIGRAM_INDEXABLE_TEXT.search(value):
+ raise serializers.ValidationError(
+ "Use 3 letters or digits in a row, or 2 next to a symbol such as `ai_`, for example a whole word or identifier. "
+ "To read one known entry, pass `key` instead."
+ )
+ return value
+
@extend_schema_field(OpenApiTypes.STR)
class _BestEffortDateTimeField(serializers.DateTimeField):
diff --git a/products/signals/backend/test/test_scout_harness_api.py b/products/signals/backend/test/test_scout_harness_api.py
index 28d68d0cdd71..7c123019c336 100644
--- a/products/signals/backend/test/test_scout_harness_api.py
+++ b/products/signals/backend/test/test_scout_harness_api.py
@@ -77,6 +77,7 @@ from products.signals.backend.scout_harness.limits import (
from products.signals.backend.scout_harness.note_targets import PIPELINE_AUDIENCE_REPORT_RESEARCH as PIPELINE_AUDIENCE
from products.signals.backend.scout_harness.prompt import FOLLOWUP_KEY_PREFIX
from products.signals.backend.scout_harness.serializers import (
+ SearchMemoryQuerySerializer,
SignalScoutConfigUpdateSerializer,
SignalScoutRunSummarySerializer,
SignalScoutSlackDestinationSerializer,
@@ -2390,6 +2391,11 @@ class TestScoutHarnessScratchpadAPI(APIBaseTest):
keys = [row["key"] for row in response.json()]
assert keys == ["match"]
+ def test_search_rejects_text_the_trigram_indexes_cannot_serve(self) -> None:
+ response = self.client.get(self._list_url(), {"text": "ai"})
+ assert response.status_code == status.HTTP_400_BAD_REQUEST
+ assert response.json()["attr"] == "text"
+
def test_search_keys_only_blanks_content(self) -> None:
SignalScratchpad.objects.create(team=self.team, key="k1", content="a long body")
response = self.client.get(f"{self._list_url()}?keys_only=true")
@@ -2628,6 +2634,25 @@ class TestScoutHarnessScratchpadAPI(APIBaseTest):
assert NOT_IN_USE_WRITES_KEY not in (sibling_run.metadata or {})
+class TestSearchMemoryQuerySerializer(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("blank", "", True),
+ ("word", "checkout", True),
+ ("key_prefix", "pattern:impl:posthog/posthog", True),
+ ("three_digits", "404", True),
+ ("non_ascii_word", "café", True),
+ ("two_chars_before_separator", "ai_", True),
+ ("two_chars_after_separator", ":pr", True),
+ ("two_chars", "ai", False),
+ ("short_words_split_by_separators", "a-b_c d", False),
+ ("punctuation_only", "%%%", False),
+ ]
+ )
+ def test_text_needs_a_trigram(self, _name: str, text: str, valid: bool) -> None:
+ assert SearchMemoryQuerySerializer(data={"text": text}).is_valid() is valid
+
+
class TestScoutHarnessNotesAPI(APIBaseTest):
# Session auth throughout — unlike the scratchpad, the notes surface is deliberately on the
# user-grantable `signal_scout` scopes, so any team member's session/PAK can write.
diff --git a/products/signals/frontend/generated/api.schemas.ts b/products/signals/frontend/generated/api.schemas.ts
index ab59a9efca86..e8a10296931c 100644
--- a/products/signals/frontend/generated/api.schemas.ts
+++ b/products/signals/frontend/generated/api.schemas.ts
@@ -11746,7 +11746,7 @@ export type SignalsScoutScratchpadSearchParams = {
*/
skill_name?: string
/**
- * ILIKE substring match against `content`. Omit to return the most recent entries.
+ * ILIKE substring match against `content` and `key`. Must contain 3 letters or digits in a row, or 2 next to a symbol such as `ai_` or `pr:`. Omit to return the most recent entries.
*/
text?: string
}
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 530f6af08723..a10c3bf4c6e4 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -132216,7 +132216,7 @@ export namespace Schemas {
*/
skill_name?: string;
/**
- * ILIKE substring match against `content`. Omit to return the most recent entries.
+ * ILIKE substring match against `content` and `key`. Must contain 3 letters or digits in a row, or 2 next to a symbol such as `ai_` or `pr:`. Omit to return the most recent entries.
*/
text?: string;
};
diff --git a/services/mcp/src/generated/signals/api.ts b/services/mcp/src/generated/signals/api.ts
index c133f3c386e8..ca27742606c5 100644
--- a/services/mcp/src/generated/signals/api.ts
+++ b/services/mcp/src/generated/signals/api.ts
@@ -4668,7 +4668,9 @@ export const SignalsScoutScratchpadSearchQueryParams = () => zod.object({
text: zod
.string()
.optional()
- .describe('ILIKE substring match against `content`. Omit to return the most recent entries.'),
+ .describe(
+ 'ILIKE substring match against `content` and `key`. Must contain 3 letters or digits in a row, or 2 next to a symbol such as `ai_` or `pr:`. Omit to return the most recent entries.'
+ ),
})
/**
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/signals-scout-scratchpad-search.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/signals-scout-scratchpad-search.json
index 7f558d76c7ed..2fc6c6c94e25 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/common/signals-scout-scratchpad-search.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/common/signals-scout-scratchpad-search.json
@@ -8,7 +8,7 @@
"type": "number"
},
"text": {
- "description": "ILIKE substring match against `content`. Omit to return the most recent entries.",
+ "description": "ILIKE substring match against `content` and `key`. Must contain 3 letters or digits in a row, or 2 next to a symbol such as `ai_` or `pr:`. Omit to return the most recent entries.",
"type": "string"
}
},
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/scout-scratchpad-search.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/scout-scratchpad-search.json
index 9a3e178ed206..485388e941a2 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/scout-scratchpad-search.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/scout-scratchpad-search.json
@@ -43,7 +43,7 @@
"type": "string"
},
"text": {
- "description": "ILIKE substring match against `content`. Omit to return the most recent entries.",
+ "description": "ILIKE substring match against `content` and `key`. Must contain 3 letters or digits in a row, or 2 next to a symbol such as `ai_` or `pr:`. Omit to return the most recent entries.",
"type": "string"
}
},
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/signals-scout-scratchpad-search.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/signals-scout-scratchpad-search.json
index 9a3e178ed206..485388e941a2 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/signals-scout-scratchpad-search.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/signals-scout-scratchpad-search.json
@@ -43,7 +43,7 @@
"type": "string"
},
"text": {
- "description": "ILIKE substring match against `content`. Omit to return the most recent entries.",
+ "description": "ILIKE substring match against `content` and `key`. Must contain 3 letters or digits in a row, or 2 next to a symbol such as `ai_` or `pr:`. Omit to return the most recent entries.",
"type": "string"
}
},