File size: 7,301 Bytes
5e5d6c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | from __future__ import annotations
import hashlib
import json
import pytest
from ctx.runtime.query_vocabulary import (
QUERY_VOCABULARY_SCHEMA,
AuthenticatedQueryVocabulary,
)
from ctx.runtime.query_work import (
MAX_QUERY_LANGUAGE_CODEPOINTS,
MAX_QUERY_TASK_BYTES,
normalize_query_work,
)
NAMESPACE_DIGEST = hashlib.sha256(b"catalog:local-user").hexdigest()
GRAPH_DIGEST = hashlib.sha256(b"graph:v1").hexdigest()
def _vocabulary(*signals: str) -> AuthenticatedQueryVocabulary:
ordered = tuple(sorted(signals))
payload = {
"catalog_namespace_digest": NAMESPACE_DIGEST,
"graph_artifact_sha256": GRAPH_DIGEST,
"schema": QUERY_VOCABULARY_SCHEMA,
"signals": list(ordered),
}
digest = hashlib.sha256(
json.dumps(
payload,
ensure_ascii=True,
separators=(",", ":"),
sort_keys=True,
).encode("utf-8")
).hexdigest()
return AuthenticatedQueryVocabulary(
signals=ordered,
catalog_namespace_digest=NAMESPACE_DIGEST,
graph_artifact_sha256=GRAPH_DIGEST,
vocabulary_digest=digest,
)
def _normalize(
task: str,
*,
language: str = "python",
vocabulary: AuthenticatedQueryVocabulary,
):
return normalize_query_work(
task=task,
language=language,
vocabulary=vocabulary,
expected_catalog_namespace_digest=NAMESPACE_DIGEST,
expected_graph_artifact_sha256=GRAPH_DIGEST,
)
def test_keeps_only_authenticated_vocabulary_and_declared_language() -> None:
work = _normalize(
(
"PRIVATE_PATCH_SENTINEL implement `Response.json_or` in "
"/Users/alice/secret-repository with Python testing"
),
vocabulary=_vocabulary("json", "response", "testing"),
)
assert work.languages == ("python",)
assert work.signals == ("json", "response", "testing")
assert work.requested_limit == 5
assert "private" not in repr(work).casefold()
assert "secret-repository" not in repr(work)
assert "users" not in repr(work)
def test_paths_and_secret_values_are_removed_even_if_vocabulary_contains_them() -> None:
secret_value = "sk-ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"
work = _normalize(
(
f"OPENAI_API_KEY={secret_value} password: hunter2 "
"repair /Users/alice/secret-repository parser"
),
language="TypeScript",
vocabulary=_vocabulary(
"alice",
"hunter2",
"openai-api-key",
"parser",
"password",
"repair",
"secret-repository",
"users",
),
)
serialized = repr(work)
assert secret_value not in serialized
assert work.languages == ("typescript",)
assert work.signals == ("parser", "repair")
def test_relative_quoted_paths_and_flag_or_prose_secrets_are_removed() -> None:
work = _normalize(
(
"inspect customer-mercury/src/parser.py and 'C:\\Program Files\\repo' "
"--password hunter2; password is swordfish; then validate"
),
vocabulary=_vocabulary(
"customer-mercury",
"files",
"hunter2",
"parser",
"repo",
"src",
"swordfish",
"validate",
),
)
assert work.signals == ("validate",)
def test_wrapped_paths_and_formatted_or_authorization_secrets_are_removed() -> None:
work = _normalize(
(
"`src/customer-mercury/parser.py` <src/customer-mercury/parser.py> "
"Authorization: Bearer hunter2; password is: swordfish; "
"**credential** is privatevalue; validate"
),
vocabulary=_vocabulary(
"credential",
"customer-mercury",
"hunter2",
"parser",
"privatevalue",
"src",
"swordfish",
"validate",
),
)
assert work.signals == ("validate",)
def test_mainstream_camelcase_technologies_match_compact_graph_tags() -> None:
work = _normalize(
"Add a FastAPI GraphQL OpenAPI PostgreSQL PyTorch endpoint",
vocabulary=_vocabulary(
"endpoint",
"fastapi",
"graphql",
"openapi",
"postgresql",
"pytorch",
),
)
assert work.signals == (
"endpoint",
"fastapi",
"graphql",
"openapi",
"postgresql",
"pytorch",
)
def test_language_aliases_cannot_be_inferred_from_ordinary_prose() -> None:
with pytest.raises(ValueError, match="language aliases"):
_vocabulary("go", "testing")
work = _normalize(
"please go add testing",
language="python",
vocabulary=_vocabulary("testing"),
)
assert work.languages == ("python",)
assert work.signals == ("testing",)
def test_generic_or_unmatched_work_abstains() -> None:
work = _normalize(
"please implement the requested change",
language="C++",
vocabulary=_vocabulary("cmake", "serialization"),
)
assert work.languages == ("cpp",)
assert work.signals == ()
assert work.requested_limit == 0
def test_rejects_lexically_colliding_authenticated_signals() -> None:
with pytest.raises(ValueError, match="lexical collision"):
_vocabulary("mcp-server", "mcp.server", "validation")
with pytest.raises(ValueError, match="lexical collision"):
_vocabulary("mcp-server", "mcpserver")
def test_catalog_bindings_must_match_the_query_composition() -> None:
vocabulary = _vocabulary("testing")
with pytest.raises(ValueError, match="catalog binding"):
normalize_query_work(
task="python testing",
language="python",
vocabulary=vocabulary,
expected_catalog_namespace_digest="f" * 64,
expected_graph_artifact_sha256=GRAPH_DIGEST,
)
@pytest.mark.parametrize(
("field", "value", "message"),
[
("task", 3, "task must be a string"),
("language", None, "language must be a string"),
("vocabulary", ("python",), "AuthenticatedQueryVocabulary"),
],
)
def test_rejects_untyped_inputs(field: str, value: object, message: str) -> None:
values: dict[str, object] = {
"task": "python testing",
"language": "python",
"vocabulary": _vocabulary("testing"),
"expected_catalog_namespace_digest": NAMESPACE_DIGEST,
"expected_graph_artifact_sha256": GRAPH_DIGEST,
}
values[field] = value
with pytest.raises((TypeError, ValueError), match=message):
normalize_query_work(**values) # type: ignore[arg-type]
def test_task_input_is_bounded_before_vocabulary_matching() -> None:
with pytest.raises(ValueError, match="task exceeds"):
_normalize(
"x" * (MAX_QUERY_TASK_BYTES + 1),
vocabulary=_vocabulary("testing"),
)
def test_declared_language_input_is_bounded() -> None:
with pytest.raises(ValueError, match="language exceeds"):
_normalize(
"testing",
language="x" * (MAX_QUERY_LANGUAGE_CODEPOINTS + 1),
vocabulary=_vocabulary("testing"),
)
|