Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
593 lines (513 loc) · 22.3 KB
/
Copy pathconfig.py
File metadata and controls
593 lines (513 loc) · 22.3 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
"""Runtime configuration: settings, model providers, and environment loading."""
from __future__ import annotations
import os
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import TypedDict, Unpack
from dotenv import load_dotenv
from loguru import logger
from pydantic import Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from . import constants as cs
from . import exceptions as ex
from . import logs
from .graph_dialects import DIALECT_MEMGRAPH, available_dialects
from .types_defs import CgrignorePatterns, ModelConfigKwargs
# Load only the configuration file in the invocation directory. The default
# python-dotenv discovery walks parent directories, which can silently import
# credentials from an unrelated workspace (and makes tests depend on the
# caller's directory layout).
load_dotenv(dotenv_path=Path.cwd() / ".env")
class ApiKeyInfoEntry(TypedDict):
env_var: str
url: str
name: str
API_KEY_INFO: dict[str, ApiKeyInfoEntry] = {
cs.Provider.OPENAI: {
"env_var": "OPENAI_API_KEY",
"url": "https://platform.openai.com/api-keys",
"name": "OpenAI",
},
cs.Provider.ANTHROPIC: {
"env_var": "ANTHROPIC_API_KEY",
"url": "https://console.anthropic.com/settings/keys",
"name": "Anthropic",
},
cs.Provider.GOOGLE: {
"env_var": "GOOGLE_API_KEY",
"url": "https://console.cloud.google.com/apis/credentials",
"name": "Google AI",
},
cs.Provider.AZURE: {
"env_var": "AZURE_API_KEY",
"url": "https://portal.azure.com/",
"name": "Azure OpenAI",
},
cs.Provider.MINIMAX: {
"env_var": "MINIMAX_API_KEY",
"url": "https://platform.minimax.io/user-center/basic-information/interface-key",
"name": "MiniMax",
},
}
def format_missing_api_key_errors(
provider: str, role: str = cs.DEFAULT_MODEL_ROLE
) -> str:
provider_lower = provider.lower()
if provider_lower in API_KEY_INFO:
info = API_KEY_INFO[provider_lower]
env_var = info["env_var"]
url = info["url"]
name = info["name"]
else:
env_var = f"{provider.upper()}_API_KEY"
url = f"your {provider} provider's website"
name = provider.capitalize()
role_msg = f" for {role}" if role != cs.DEFAULT_MODEL_ROLE else ""
error_msg = f"""
─── API Key Missing ───────────────────────────────────────────────
Error: {env_var} environment variable is not set.
This is required to use {name}{role_msg}.
To fix this:
1. Get your API key from:
{url}
2. Set it in your environment:
export {env_var}='your-key-here'
Or add it to your .env file in the project root:
{env_var}=your-key-here
3. Alternatively, you can use a local model with Ollama:
(No API key required)
───────────────────────────────────────────────────────────────────
""".strip() # noqa: W293
return error_msg
LOCAL_PROVIDERS = frozenset({cs.Provider.OLLAMA})
# The provider-owned variable `validate_api_key` accepts INSTEAD of the role's
# own `<ROLE>_API_KEY`. Module level so `cgr doctor` can name the same variable
# the gate reads rather than restating the rule and drifting from it (#1910).
#
# DERIVED from API_KEY_INFO rather than hand-kept, because a hand-kept subset is
# what #1913 was: it left out OpenAI and Google, so the gate refused a
# configuration naming the variable `format_missing_api_key_errors` had just
# told the user to export, and that the provider itself reads
# (`_resolve_api_key(api_key, cs.ENV_OPENAI_API_KEY)` at providers/base.py:181,
# and ENV_GOOGLE_API_KEY at :116). One table means the gate, the error message
# and `cgr doctor` cannot disagree about which variable counts.
PROVIDER_ENV_KEYS = {
provider: info["env_var"] for provider, info in API_KEY_INFO.items()
}
@dataclass
class ModelConfig:
provider: str
model_id: str
api_key: str | None = None
endpoint: str | None = None
project_id: str | None = None
region: str | None = None
provider_type: str | None = None
thinking_budget: int | None = None
service_account_file: str | None = None
def to_update_kwargs(self) -> ModelConfigKwargs:
result = asdict(self)
del result[cs.FIELD_PROVIDER]
del result[cs.FIELD_MODEL_ID]
return ModelConfigKwargs(**result)
def validate_api_key(self, role: str = cs.DEFAULT_MODEL_ROLE) -> None:
provider_lower = self.provider.lower()
env_key = PROVIDER_ENV_KEYS.get(provider_lower)
if (
provider_lower in LOCAL_PROVIDERS
or (
provider_lower == cs.Provider.GOOGLE
and self.provider_type == cs.GoogleProviderType.VERTEX
)
or (env_key and os.environ.get(env_key))
):
return
if (
not self.api_key
or not self.api_key.strip()
or self.api_key == cs.DEFAULT_API_KEY
):
error_msg = format_missing_api_key_errors(self.provider, role)
raise ValueError(error_msg)
class AppConfig(BaseSettings):
"""
All settings are loaded from environment variables or a .env file.
"""
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False,
)
# Which graph engine the ingestor talks to. Memgraph stays the default,
# so an existing install keeps its behaviour without touching config;
# see `graph_dialects` for what actually differs between engines.
GRAPH_BACKEND: str = DIALECT_MEMGRAPH
MEMGRAPH_HOST: str = "localhost"
MEMGRAPH_PORT: int = 7687
MEMGRAPH_HTTP_PORT: int = 7444
MEMGRAPH_USERNAME: str | None = None
MEMGRAPH_PASSWORD: str | None = None
# Neo4j connects by URI rather than host/port: the scheme carries the
# routing mode (`neo4j://` for a cluster, `bolt://` for one instance)
# and TLS (`+s`/`+ssc`), none of which a host/port pair can express.
NEO4J_URI: str = "bolt://localhost:7687"
NEO4J_USERNAME: str | None = None
NEO4J_PASSWORD: str | None = None
NEO4J_DATABASE: str = "neo4j"
LAB_PORT: int = 3000
MEMGRAPH_BATCH_SIZE: int = 1000
AGENT_RETRIES: int = 3
ORCHESTRATOR_OUTPUT_RETRIES: int = 100
ORCHESTRATOR_PROVIDER: str = ""
ORCHESTRATOR_MODEL: str = ""
ORCHESTRATOR_API_KEY: str | None = None
ORCHESTRATOR_ENDPOINT: str | None = None
ORCHESTRATOR_PROJECT_ID: str | None = None
ORCHESTRATOR_REGION: str = cs.DEFAULT_REGION
ORCHESTRATOR_PROVIDER_TYPE: cs.GoogleProviderType | None = None
ORCHESTRATOR_THINKING_BUDGET: int | None = None
ORCHESTRATOR_SERVICE_ACCOUNT_FILE: str | None = None
CYPHER_PROVIDER: str = ""
CYPHER_MODEL: str = ""
CYPHER_API_KEY: str | None = None
CYPHER_ENDPOINT: str | None = None
CYPHER_PROJECT_ID: str | None = None
CYPHER_REGION: str = cs.DEFAULT_REGION
CYPHER_PROVIDER_TYPE: cs.GoogleProviderType | None = None
CYPHER_THINKING_BUDGET: int | None = None
CYPHER_SERVICE_ACCOUNT_FILE: str | None = None
OLLAMA_BASE_URL: str = "http://localhost:11434"
@property
def ollama_endpoint(self) -> str:
return f"{self.OLLAMA_BASE_URL.rstrip('/')}/v1"
TARGET_REPO_PATH: str = "."
# HYBRID degrades to pure tree-sitter when libclang or compile_commands.json
# is missing, so it is a safe default and strictly better (macros, includes,
# expansion calls) with one.
CPP_FRONTEND: cs.CppFrontend = cs.CppFrontend.HYBRID
# Opt-in Roslyn semantic layer for C#. Defaults to pure tree-sitter because
# HYBRID needs a dotnet SDK + a restorable .csproj/.sln and degrades without
# them. HYBRID augments (base-vs-interface, overload and extension binding,
# partial-class identity); tree-sitter stays the standalone-correct backbone.
# Default to tree-sitter so indexing an untrusted repository never auto-invokes
# MSBuild/Roslyn; opt into AUTO/HYBRID/ROSLYN explicitly (security, #1231).
CSHARP_FRONTEND: cs.CSharpFrontend = cs.CSharpFrontend.TREESITTER
# Opt-in go/packages semantic layer for Go (issue #1179). AUTO uses it where a
# go toolchain is on PATH and degrades to pure tree-sitter otherwise. GOTYPES
# augments exact first-party call binding and external-site suppression;
# tree-sitter stays the standalone-correct backbone.
GO_FRONTEND: cs.GoFrontend = cs.GoFrontend.AUTO
PYTHON_FRONTEND: cs.PythonFrontend = cs.PythonFrontend.HEURISTIC
JAVA_FRONTEND: cs.JavaFrontend = cs.JavaFrontend.HEURISTIC
LOMBOK_JAR: str | None = None
CAPTURE_FUNCTION_LOCAL_DEFINITIONS: bool = Field(
True, validation_alias="CGR_CAPTURE_LOCAL_DEFINITIONS"
)
CGR_HOME: Path = Field(default_factory=lambda: Path.home() / ".cgr")
# Editor integration for clickable report locations (OSC 8 hyperlinks)
# and `cgr duplicates --open`. AUTO sniffs the hosting app (Cursor,
# Windsurf, Zed, VS Code's terminal) and falls back to VS Code; the
# templates override any editor choice. See EDITOR_URL_TEMPLATES for
# the named editors and the {path}/{line} and {left}/{right} slots.
CGR_EDITOR: str = cs.EDITOR_AUTO
CGR_EDITOR_URL_TEMPLATE: str | None = None
CGR_DIFF_COMMAND: str | None = None
SHELL_COMMAND_TIMEOUT: int = 30
SHELL_COMMAND_ALLOWLIST: frozenset[str] = frozenset(
{
"ls",
"rg",
"cat",
"git",
"echo",
"pwd",
"pytest",
"mypy",
"ruff",
"uv",
"find",
"pre-commit",
"rm",
"cp",
"mv",
"mkdir",
"rmdir",
"wc",
"head",
"tail",
"sort",
"uniq",
"cut",
"tr",
"xargs",
"awk",
"sed",
"tee",
}
)
# Only commands that cannot accept filesystem paths are approval-free.
# Filesystem and Git reads require approval because their path syntaxes can
# escape the project root (absolute paths, traversal, symlinks, git -C, and
# --git-dir). Project-confined read/search tools should be preferred instead.
SHELL_READ_ONLY_COMMANDS: frozenset[str] = frozenset(
{
"pwd",
"echo",
"tr",
}
)
SHELL_SAFE_GIT_SUBCOMMANDS: frozenset[str] = frozenset()
# Read-only, path-taking commands a NON-INTERACTIVE session (benchmark
# harnesses, batch jobs) may run without an operator. Kept a subset of
# SHELL_COMMAND_ALLOWLIST; the non-interactive wrapper additionally
# rejects redirects, find's mutating actions, and absolute or
# parent-traversal path arguments, so these reads stay inside the
# project root.
SHELL_NONINTERACTIVE_READ_COMMANDS: frozenset[str] = frozenset(
{
"ls",
"rg",
"cat",
"find",
"wc",
"head",
"tail",
"sort",
"uniq",
"cut",
}
)
QDRANT_DB_PATH: str = "./.qdrant_code_embeddings"
QDRANT_URL: str | None = None
QDRANT_COLLECTION_NAME: str = "code_embeddings"
QDRANT_VECTOR_DIM: int = 768
QDRANT_TOP_K: int = 5
QDRANT_UPSERT_RETRIES: int = Field(default=3, gt=0)
QDRANT_RETRY_BASE_DELAY: float = Field(default=0.5, gt=0)
QDRANT_BATCH_SIZE: int = Field(default=50, gt=0)
VECTOR_STORE_BACKEND: cs.VectorStoreBackend = Field(
cs.VectorStoreBackend.QDRANT, validation_alias="CGR_VECTOR_STORE_BACKEND"
)
MILVUS_URI: str = "./.milvus_code_embeddings.db"
MILVUS_TOKEN: str | None = None
MILVUS_DB_NAME: str | None = None
MILVUS_COLLECTION_NAME: str = "code_embeddings"
MILVUS_VECTOR_DIM: int = 768
MILVUS_TOP_K: int = 5
MILVUS_CONSISTENCY_LEVEL: str = "Strong"
EMBEDDING_PROVIDER: cs.EmbeddingProvider = Field(
cs.EmbeddingProvider.UNIXCODER, validation_alias="CGR_EMBEDDING_PROVIDER"
)
OPENAI_EMBEDDING_BASE_URL: str = cs.OPENAI_DEFAULT_ENDPOINT
OPENAI_EMBEDDING_MODEL: str = cs.OPENAI_EMBEDDING_DEFAULT_MODEL
OPENAI_EMBEDDING_API_KEY: str | None = None
OPENAI_EMBEDDING_DIMENSIONS: int | None = Field(default=None, gt=0)
OPENAI_EMBEDDING_BATCH_SIZE: int = Field(default=128, gt=0)
OPENAI_EMBEDDING_TIMEOUT: float = Field(default=60.0, gt=0)
EMBEDDING_MAX_LENGTH: int = 512
EMBEDDING_PROGRESS_INTERVAL: int = 10
SKIP_EMBEDDINGS: bool = Field(False, validation_alias="CGR_SKIP_EMBEDDINGS")
EMBEDDING_DEVICE: cs.EmbeddingDevice | None = Field(
None, validation_alias="CGR_EMBEDDING_DEVICE"
)
FLUSH_THREAD_POOL_SIZE: int = Field(default=4, gt=0)
FILE_FLUSH_INTERVAL: int = Field(default=500, gt=0)
CACHE_MAX_ENTRIES: int = 1000
CACHE_MAX_MEMORY_MB: int = 500
CACHE_EVICTION_DIVISOR: int = 10
CACHE_MEMORY_THRESHOLD_RATIO: float = 0.8
QUERY_RESULT_MAX_TOKENS: int = Field(default=16000, gt=0)
# The model's OUTPUT budget per request. Without it pydantic-ai falls back
# to the provider default, which on Anthropic is far below what current
# models support -- a long answer then fails with "Model token limit
# (provider default) exceeded before any response was generated" and the
# model never replies at all (issue #1498).
#
# Distinct from QUERY_RESULT_MAX_TOKENS above, which trims what goes IN.
# This bounds what comes back.
# 8192 rather than something larger: pydantic-ai forwards this value
# unchanged and exposes no per-model output cap to clamp against, so the
# default must fit the SMALLEST catalogued model or it breaks working
# configurations. `gemini-2.0-flash` caps output at 8192 and is
# selectable today; 16000 was rejected outright by it. This still fixes
# the reported crash, whose remedy is any value meaningfully above
# Anthropic's 4096 provider default.
#
# Raise it per-deployment when the chosen model allows more. A general
# clamping table over every model was rejected: it would need
# hand-maintaining and would silently go stale on every new release,
# capping a launch below what it supports. Retired snapshots are the
# exception -- their maxima are frozen -- so `LEGACY_MAX_OUTPUT_TOKENS`
# lowers this budget for those ids alone, leaving everything else at the
# configured value.
MODEL_MAX_TOKENS: int = Field(default=8192, gt=0)
QUERY_RESULT_ROW_CAP: int = Field(default=500, gt=0)
QUERY_MEMORY_LIMIT_MB: int = Field(default=4096, gt=0)
QUERY_TIMEOUT_S: float = Field(default=60.0, gt=0)
@field_validator("GRAPH_BACKEND")
@classmethod
def _known_backend(cls, value: str) -> str:
"""Reject an unknown engine name at startup.
Defaulting an unrecognised value to Memgraph would send Memgraph
DDL to whatever server is actually configured, and
`MemgraphIngestor.ensure_constraints` swallows DDL failures -- so
a typo would surface much later as a graph built with no
constraints rather than as a startup error.
"""
normalised = value.strip().lower()
if normalised not in available_dialects():
raise ValueError(
f"GRAPH_BACKEND must be one of {', '.join(available_dialects())}; "
f"got {value!r}"
)
return normalised
OLLAMA_HEALTH_TIMEOUT: float = 5.0
LITELLM_HEALTH_TIMEOUT: float = 5.0
_active_orchestrator: ModelConfig | None = None
_active_cypher: ModelConfig | None = None
QUIET: bool = Field(False, validation_alias="CGR_QUIET")
CGR_CAPTURE: str = Field("", validation_alias="CGR_CAPTURE")
# Loopback by default: the StreamableHTTP endpoint has no built-in
# auth, so exposing it beyond the host must be an explicit operator
# choice via MCP_HTTP_HOST (issue #808).
MCP_HTTP_HOST: str = "127.0.0.1"
MCP_HTTP_PORT: int = 8080
MCP_HTTP_ENDPOINT_PATH: str = "/mcp"
# Bearer token for the HTTP MCP endpoint; unset means loopback-only
# (serve_http refuses a non-loopback bind without it).
MCP_HTTP_AUTH_TOKEN: str | None = None
def _get_default_config(self, role: str) -> ModelConfig:
role_upper = role.upper()
provider = getattr(self, f"{role_upper}_PROVIDER", None)
model = getattr(self, f"{role_upper}_MODEL", None)
if provider and model:
return ModelConfig(
provider=provider.lower(),
model_id=model,
api_key=getattr(self, f"{role_upper}_API_KEY", None),
endpoint=getattr(self, f"{role_upper}_ENDPOINT", None),
project_id=getattr(self, f"{role_upper}_PROJECT_ID", None),
region=getattr(self, f"{role_upper}_REGION", cs.DEFAULT_REGION),
provider_type=getattr(self, f"{role_upper}_PROVIDER_TYPE", None),
thinking_budget=getattr(self, f"{role_upper}_THINKING_BUDGET", None),
service_account_file=getattr(
self, f"{role_upper}_SERVICE_ACCOUNT_FILE", None
),
)
return ModelConfig(
provider=cs.Provider.OLLAMA,
model_id=cs.DEFAULT_MODEL,
endpoint=self.ollama_endpoint,
api_key=cs.DEFAULT_API_KEY,
)
def _get_default_orchestrator_config(self) -> ModelConfig:
return self._get_default_config(cs.ModelRole.ORCHESTRATOR)
def _get_default_cypher_config(self) -> ModelConfig:
return self._get_default_config(cs.ModelRole.CYPHER)
@property
def active_orchestrator_config(self) -> ModelConfig:
return self._active_orchestrator or self._get_default_orchestrator_config()
@property
def active_cypher_config(self) -> ModelConfig:
return self._active_cypher or self._get_default_cypher_config()
def set_orchestrator(
self, provider: str, model: str, **kwargs: Unpack[ModelConfigKwargs]
) -> None:
config = ModelConfig(provider=provider.lower(), model_id=model, **kwargs)
self._active_orchestrator = config
def set_cypher(
self, provider: str, model: str, **kwargs: Unpack[ModelConfigKwargs]
) -> None:
config = ModelConfig(provider=provider.lower(), model_id=model, **kwargs)
self._active_cypher = config
def parse_model_string(self, model_string: str) -> tuple[str, str]:
if ":" not in model_string:
return cs.Provider.OLLAMA, model_string
provider, model = model_string.split(":", 1)
if not provider:
raise ValueError(ex.PROVIDER_EMPTY)
return provider.lower(), model
def resolve_batch_size(self, batch_size: int | None) -> int:
resolved = self.MEMGRAPH_BATCH_SIZE if batch_size is None else batch_size
if resolved < 1:
raise ValueError(ex.BATCH_SIZE_POSITIVE)
return resolved
settings = AppConfig()
CGRIGNORE_FILENAME = ".cgrignore"
GITIGNORE_FILENAME = ".gitignore"
EMPTY_CGRIGNORE = CgrignorePatterns(exclude=frozenset(), unignore=frozenset())
def _load_ignore_file(ignore_file: Path) -> CgrignorePatterns:
if not ignore_file.is_file():
return EMPTY_CGRIGNORE
exclude: set[str] = set()
unignore: set[str] = set()
try:
with ignore_file.open(encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if line.startswith("!"):
unignore.add(line[1:].strip())
else:
exclude.add(line)
if exclude or unignore:
logger.info(
logs.CGRIGNORE_LOADED.format(
exclude_count=len(exclude),
unignore_count=len(unignore),
path=ignore_file,
)
)
return CgrignorePatterns(
exclude=frozenset(exclude),
unignore=frozenset(unignore),
)
except (OSError, ValueError) as e:
logger.warning(logs.CGRIGNORE_READ_FAILED.format(path=ignore_file, error=e))
return EMPTY_CGRIGNORE
def load_cgrignore_patterns(repo_path: Path) -> CgrignorePatterns:
return _load_ignore_file(repo_path / CGRIGNORE_FILENAME)
def load_ignore_patterns(repo_path: Path) -> CgrignorePatterns:
# Merged exclude/unignore set for indexing: root .gitignore (gitignored
# paths are build artifacts and generated output that pollute the graph and
# dead-code report) plus .cgrignore, the authoritative cgr channel. The skip
# check gives excludes precedence, so a negation overrides a .gitignore
# exclude only by CANCELLING the exact pattern (`!generated/` drops
# `generated/`); .cgrignore excludes are never cancelled.
# ponytail: root .gitignore only, exact-string cancellation only; a
# finer-grained negation (`!dist/keep.py` under excluded `dist/`) still
# cannot rescue -- an ordered PathSpec soft layer in should_skip_path is
# the upgrade path if real repos need it.
cgr = _load_ignore_file(repo_path / CGRIGNORE_FILENAME)
git = _load_ignore_file(repo_path / GITIGNORE_FILENAME)
negations = cgr.unignore | git.unignore
return CgrignorePatterns(
exclude=cgr.exclude | (git.exclude - negations),
unignore=negations,
)
CGR_INSTRUCTIONS_FILENAME = ".cgr.md"
GLOBAL_CGR_INSTRUCTIONS_PATH = Path.home() / CGR_INSTRUCTIONS_FILENAME
def _read_cgr_instructions_file(path: Path) -> str | None:
if not path.is_file():
return None
try:
with path.open(encoding="utf-8") as f:
body = f.read().strip()
except OSError as e:
logger.warning(logs.CGR_INSTRUCTIONS_READ_FAILED.format(path=path, error=e))
return None
if not body:
return None
logger.info(logs.CGR_INSTRUCTIONS_LOADED.format(path=path, chars=len(body)))
return body
def load_cgr_instructions(repo_path: Path | None) -> str | None:
global_body = _read_cgr_instructions_file(GLOBAL_CGR_INSTRUCTIONS_PATH)
repo_body = (
_read_cgr_instructions_file(repo_path / CGR_INSTRUCTIONS_FILENAME)
if repo_path is not None
else None
)
if global_body and repo_body:
return f"{global_body}\n\n---\n\n{repo_body}"
return global_body or repo_body