amom 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- amom/__init__.py +3 -0
- amom/__main__.py +3 -0
- amom/cli.py +379 -0
- amom/config.py +105 -0
- amom/database.py +367 -0
- amom/indexer.py +474 -0
- amom/locks.py +62 -0
- amom/mcp_server.py +386 -0
- amom/projects.py +219 -0
- amom/retrieval.py +347 -0
- amom/watcher.py +88 -0
- amom-0.1.0.dist-info/METADATA +171 -0
- amom-0.1.0.dist-info/RECORD +16 -0
- amom-0.1.0.dist-info/WHEEL +4 -0
- amom-0.1.0.dist-info/entry_points.txt +2 -0
- amom-0.1.0.dist-info/licenses/LICENSE +21 -0
amom/__init__.py
ADDED
amom/__main__.py
ADDED
amom/cli.py
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
"""Command-line interface for the local Amom engine."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sqlite3
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from . import __version__
|
|
12
|
+
from .config import SHARED_WORKSPACE_DIR, database_path
|
|
13
|
+
from .database import Database
|
|
14
|
+
from .indexer import index_project
|
|
15
|
+
from .retrieval import (
|
|
16
|
+
forget_memory,
|
|
17
|
+
get_context,
|
|
18
|
+
remember,
|
|
19
|
+
search_code,
|
|
20
|
+
search_memories,
|
|
21
|
+
update_memory,
|
|
22
|
+
)
|
|
23
|
+
from .watcher import watch_project
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _json(value) -> None:
|
|
27
|
+
print(json.dumps(value, ensure_ascii=False, indent=2, default=str))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _database(args) -> Database:
|
|
31
|
+
return Database(database_path(args.db))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _root(args) -> Path:
|
|
35
|
+
return Path(getattr(args, "root", None) or Path.cwd()).expanduser().resolve()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _handle_init(args) -> int:
|
|
39
|
+
project = _database(args).register_project(_root(args), args.name)
|
|
40
|
+
_json({"registered": True, "project": project, "next": "amom index"})
|
|
41
|
+
return 0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _handle_index(args) -> int:
|
|
45
|
+
database = _database(args)
|
|
46
|
+
root = _root(args)
|
|
47
|
+
database.register_project(root, args.name)
|
|
48
|
+
result = index_project(
|
|
49
|
+
database,
|
|
50
|
+
root,
|
|
51
|
+
max_file_bytes=args.max_file_bytes,
|
|
52
|
+
max_files=args.max_files,
|
|
53
|
+
)
|
|
54
|
+
_json(result.as_dict())
|
|
55
|
+
return 0
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _handle_search(args) -> int:
|
|
59
|
+
database = _database(args)
|
|
60
|
+
root = _root(args)
|
|
61
|
+
result = search_code(database, root, args.query, args.limit)
|
|
62
|
+
memories = search_memories(database, args.query, root, args.limit)
|
|
63
|
+
_json({**result, "memories": memories})
|
|
64
|
+
return 0
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _handle_context(args) -> int:
|
|
68
|
+
_json(
|
|
69
|
+
get_context(
|
|
70
|
+
_database(args),
|
|
71
|
+
_root(args),
|
|
72
|
+
args.query,
|
|
73
|
+
max_tokens=args.max_tokens,
|
|
74
|
+
limit=args.limit,
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
return 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _handle_remember(args) -> int:
|
|
81
|
+
root = _root(args) if args.scope == "project" else None
|
|
82
|
+
_json(
|
|
83
|
+
remember(
|
|
84
|
+
_database(args),
|
|
85
|
+
args.content,
|
|
86
|
+
root=root,
|
|
87
|
+
category=args.category,
|
|
88
|
+
source="cli",
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
return 0
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _handle_update(args) -> int:
|
|
95
|
+
_json(
|
|
96
|
+
update_memory(
|
|
97
|
+
_database(args),
|
|
98
|
+
args.memory_id,
|
|
99
|
+
args.content,
|
|
100
|
+
root=None if args.global_scope else _root(args),
|
|
101
|
+
category=args.category,
|
|
102
|
+
global_scope=args.global_scope,
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
return 0
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _handle_forget(args) -> int:
|
|
109
|
+
result = forget_memory(
|
|
110
|
+
_database(args),
|
|
111
|
+
args.memory_id,
|
|
112
|
+
root=None if args.global_scope else _root(args),
|
|
113
|
+
global_only=args.global_scope,
|
|
114
|
+
)
|
|
115
|
+
_json(result)
|
|
116
|
+
return 0
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _handle_recall(args) -> int:
|
|
120
|
+
_json(
|
|
121
|
+
search_memories(
|
|
122
|
+
_database(args),
|
|
123
|
+
args.query,
|
|
124
|
+
_root(args),
|
|
125
|
+
args.limit,
|
|
126
|
+
)
|
|
127
|
+
)
|
|
128
|
+
return 0
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _handle_projects(args) -> int:
|
|
132
|
+
projects = _database(args).list_projects()
|
|
133
|
+
_json({"projects": projects, "count": len(projects)})
|
|
134
|
+
return 0
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def _handle_status(args) -> int:
|
|
138
|
+
database = _database(args)
|
|
139
|
+
root = _root(args)
|
|
140
|
+
project = database.get_project(root)
|
|
141
|
+
if not project:
|
|
142
|
+
_json({"registered": False, "root": str(root), "database": str(database.path)})
|
|
143
|
+
return 1
|
|
144
|
+
counts = database.project_counts(project["id"])
|
|
145
|
+
_json(
|
|
146
|
+
{
|
|
147
|
+
"registered": True,
|
|
148
|
+
"project": project,
|
|
149
|
+
"counts": counts,
|
|
150
|
+
"database": str(database.path),
|
|
151
|
+
}
|
|
152
|
+
)
|
|
153
|
+
return 0
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _handle_watch(args) -> int:
|
|
157
|
+
database = _database(args)
|
|
158
|
+
root = _root(args)
|
|
159
|
+
database.register_project(root, args.name)
|
|
160
|
+
|
|
161
|
+
def report(result: dict) -> None:
|
|
162
|
+
_json({"event": "index_updated", **result})
|
|
163
|
+
|
|
164
|
+
def report_error(exc: Exception) -> None:
|
|
165
|
+
print(f"warning: index refresh failed: {exc}", file=sys.stderr)
|
|
166
|
+
|
|
167
|
+
print(f"Watching {root} (interval: {args.interval}s). Press Ctrl+C to stop.", file=sys.stderr)
|
|
168
|
+
try:
|
|
169
|
+
watch_project(
|
|
170
|
+
database,
|
|
171
|
+
root,
|
|
172
|
+
interval=args.interval,
|
|
173
|
+
on_index=report,
|
|
174
|
+
on_error=report_error,
|
|
175
|
+
)
|
|
176
|
+
except KeyboardInterrupt:
|
|
177
|
+
print("Stopped watching.", file=sys.stderr)
|
|
178
|
+
return 0
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _handle_mcp(args) -> int:
|
|
182
|
+
try:
|
|
183
|
+
from .mcp_server import run
|
|
184
|
+
except ModuleNotFoundError as exc: # pragma: no cover - depends on installation
|
|
185
|
+
if exc.name != "mcp":
|
|
186
|
+
raise
|
|
187
|
+
print(
|
|
188
|
+
"MCP support is optional. Install it with: pip install 'amom[mcp]'",
|
|
189
|
+
file=sys.stderr,
|
|
190
|
+
)
|
|
191
|
+
return 2
|
|
192
|
+
if args.root:
|
|
193
|
+
root = Path(args.root).expanduser().resolve()
|
|
194
|
+
else:
|
|
195
|
+
# No explicit root means "follow the client workspace". The shared
|
|
196
|
+
# workspace is only a last-resort identity and is never indexed
|
|
197
|
+
# automatically.
|
|
198
|
+
root = SHARED_WORKSPACE_DIR
|
|
199
|
+
root.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
200
|
+
try:
|
|
201
|
+
run(root=root, db_path=database_path(args.db))
|
|
202
|
+
except (OSError, RuntimeError, ValueError) as exc:
|
|
203
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
204
|
+
return 2
|
|
205
|
+
return 0
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
209
|
+
parser = argparse.ArgumentParser(
|
|
210
|
+
prog="amom",
|
|
211
|
+
description="Local-first project memory and code intelligence shared by AI agents.",
|
|
212
|
+
)
|
|
213
|
+
parser.add_argument(
|
|
214
|
+
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
215
|
+
)
|
|
216
|
+
parser.add_argument(
|
|
217
|
+
"--db",
|
|
218
|
+
help="Override the shared SQLite database path (or set AMOM_DB).",
|
|
219
|
+
)
|
|
220
|
+
common = argparse.ArgumentParser(add_help=False)
|
|
221
|
+
common.add_argument(
|
|
222
|
+
"--db",
|
|
223
|
+
default=argparse.SUPPRESS,
|
|
224
|
+
help="Override the shared SQLite database path (also accepted after the command).",
|
|
225
|
+
)
|
|
226
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
227
|
+
|
|
228
|
+
def add_project_options(command):
|
|
229
|
+
command.add_argument(
|
|
230
|
+
"--root", default=".", help="Project directory (default: current directory)."
|
|
231
|
+
)
|
|
232
|
+
command.add_argument("--name", help="Optional display name for the project.")
|
|
233
|
+
|
|
234
|
+
init_parser = subparsers.add_parser(
|
|
235
|
+
"init",
|
|
236
|
+
help="Register the current project in the shared local database.",
|
|
237
|
+
parents=[common],
|
|
238
|
+
)
|
|
239
|
+
add_project_options(init_parser)
|
|
240
|
+
init_parser.set_defaults(handler=_handle_init)
|
|
241
|
+
|
|
242
|
+
index_parser = subparsers.add_parser(
|
|
243
|
+
"index",
|
|
244
|
+
help="Index project files and update only changed files.",
|
|
245
|
+
parents=[common],
|
|
246
|
+
)
|
|
247
|
+
add_project_options(index_parser)
|
|
248
|
+
index_parser.add_argument("--max-file-bytes", type=int, default=1_000_000)
|
|
249
|
+
index_parser.add_argument("--max-files", type=int, default=50_000)
|
|
250
|
+
index_parser.set_defaults(handler=_handle_index)
|
|
251
|
+
|
|
252
|
+
search_parser = subparsers.add_parser(
|
|
253
|
+
"search",
|
|
254
|
+
help="Search indexed code, symbols, and memories.",
|
|
255
|
+
parents=[common],
|
|
256
|
+
)
|
|
257
|
+
search_parser.add_argument("query")
|
|
258
|
+
search_parser.add_argument("--root", default=".")
|
|
259
|
+
search_parser.add_argument("--limit", type=int, default=8)
|
|
260
|
+
search_parser.set_defaults(handler=_handle_search)
|
|
261
|
+
|
|
262
|
+
context_parser = subparsers.add_parser(
|
|
263
|
+
"context",
|
|
264
|
+
help="Build a compact context pack for an agent task.",
|
|
265
|
+
parents=[common],
|
|
266
|
+
)
|
|
267
|
+
context_parser.add_argument("query")
|
|
268
|
+
context_parser.add_argument("--root", default=".")
|
|
269
|
+
context_parser.add_argument("--max-tokens", type=int, default=2_000)
|
|
270
|
+
context_parser.add_argument("--limit", type=int, default=8)
|
|
271
|
+
context_parser.set_defaults(handler=_handle_context)
|
|
272
|
+
|
|
273
|
+
memory_parser = subparsers.add_parser(
|
|
274
|
+
"remember",
|
|
275
|
+
help="Store a project-scoped or global memory.",
|
|
276
|
+
parents=[common],
|
|
277
|
+
)
|
|
278
|
+
memory_parser.add_argument("content")
|
|
279
|
+
memory_parser.add_argument("--category", default="note")
|
|
280
|
+
memory_parser.add_argument("--scope", choices=("project", "global"), default="project")
|
|
281
|
+
memory_parser.add_argument("--root", default=".")
|
|
282
|
+
memory_parser.set_defaults(handler=_handle_remember)
|
|
283
|
+
|
|
284
|
+
update_parser = subparsers.add_parser(
|
|
285
|
+
"update",
|
|
286
|
+
help="Correct an existing memory in place.",
|
|
287
|
+
parents=[common],
|
|
288
|
+
)
|
|
289
|
+
update_parser.add_argument("memory_id", type=int)
|
|
290
|
+
update_parser.add_argument("content")
|
|
291
|
+
update_parser.add_argument("--category", default="note")
|
|
292
|
+
update_parser.add_argument(
|
|
293
|
+
"--global",
|
|
294
|
+
dest="global_scope",
|
|
295
|
+
action="store_true",
|
|
296
|
+
help="Update a global memory instead of a project memory.",
|
|
297
|
+
)
|
|
298
|
+
update_parser.add_argument("--root", default=".")
|
|
299
|
+
update_parser.set_defaults(handler=_handle_update)
|
|
300
|
+
|
|
301
|
+
forget_parser = subparsers.add_parser(
|
|
302
|
+
"forget",
|
|
303
|
+
help="Delete a wrong or obsolete memory.",
|
|
304
|
+
parents=[common],
|
|
305
|
+
)
|
|
306
|
+
forget_parser.add_argument("memory_id", type=int)
|
|
307
|
+
forget_parser.add_argument(
|
|
308
|
+
"--global",
|
|
309
|
+
dest="global_scope",
|
|
310
|
+
action="store_true",
|
|
311
|
+
help="Delete a global memory instead of a project memory.",
|
|
312
|
+
)
|
|
313
|
+
forget_parser.add_argument("--root", default=".")
|
|
314
|
+
forget_parser.set_defaults(handler=_handle_forget)
|
|
315
|
+
|
|
316
|
+
recall_parser = subparsers.add_parser(
|
|
317
|
+
"recall", help="Search saved memories.", parents=[common]
|
|
318
|
+
)
|
|
319
|
+
recall_parser.add_argument("query")
|
|
320
|
+
recall_parser.add_argument("--root", default=".")
|
|
321
|
+
recall_parser.add_argument("--limit", type=int, default=8)
|
|
322
|
+
recall_parser.set_defaults(handler=_handle_recall)
|
|
323
|
+
|
|
324
|
+
projects_parser = subparsers.add_parser(
|
|
325
|
+
"projects",
|
|
326
|
+
help="List registered projects and their index counts.",
|
|
327
|
+
parents=[common],
|
|
328
|
+
)
|
|
329
|
+
projects_parser.set_defaults(handler=_handle_projects)
|
|
330
|
+
|
|
331
|
+
status_parser = subparsers.add_parser(
|
|
332
|
+
"status",
|
|
333
|
+
help="Show index and memory status for this project.",
|
|
334
|
+
parents=[common],
|
|
335
|
+
)
|
|
336
|
+
status_parser.add_argument("--root", default=".")
|
|
337
|
+
status_parser.set_defaults(handler=_handle_status)
|
|
338
|
+
|
|
339
|
+
watch_parser = subparsers.add_parser(
|
|
340
|
+
"watch",
|
|
341
|
+
help="Watch project files and refresh the index when they change.",
|
|
342
|
+
parents=[common],
|
|
343
|
+
)
|
|
344
|
+
add_project_options(watch_parser)
|
|
345
|
+
watch_parser.add_argument(
|
|
346
|
+
"--interval",
|
|
347
|
+
type=float,
|
|
348
|
+
default=1.0,
|
|
349
|
+
help="Polling interval in seconds (minimum 0.2).",
|
|
350
|
+
)
|
|
351
|
+
watch_parser.set_defaults(handler=_handle_watch)
|
|
352
|
+
|
|
353
|
+
mcp_parser = subparsers.add_parser(
|
|
354
|
+
"mcp", help="Run the MCP server over stdio.", parents=[common]
|
|
355
|
+
)
|
|
356
|
+
mcp_parser.add_argument(
|
|
357
|
+
"--root",
|
|
358
|
+
default=None,
|
|
359
|
+
help=(
|
|
360
|
+
"Project checkout to serve. Omit to detect the client's workspace "
|
|
361
|
+
"roots or process directory automatically."
|
|
362
|
+
),
|
|
363
|
+
)
|
|
364
|
+
mcp_parser.set_defaults(handler=_handle_mcp)
|
|
365
|
+
return parser
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def main(argv: list[str] | None = None) -> int:
|
|
369
|
+
parser = build_parser()
|
|
370
|
+
args = parser.parse_args(argv)
|
|
371
|
+
try:
|
|
372
|
+
return args.handler(args)
|
|
373
|
+
except (OSError, ValueError, sqlite3.Error) as exc:
|
|
374
|
+
print(f"error: {exc}", file=sys.stderr)
|
|
375
|
+
return 1
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
if __name__ == "__main__":
|
|
379
|
+
raise SystemExit(main())
|
amom/config.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""Application paths, safety limits, and safe defaults."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from platformdirs import user_data_path
|
|
9
|
+
|
|
10
|
+
APP_NAME = "amom"
|
|
11
|
+
DEFAULT_DATA_DIR = Path(user_data_path(APP_NAME, appauthor=False))
|
|
12
|
+
DEFAULT_DATABASE_PATH = DEFAULT_DATA_DIR / "database" / "amom.sqlite3"
|
|
13
|
+
|
|
14
|
+
# The installer may create a single shared MCP server rooted here. It is not a
|
|
15
|
+
# real project; project-scoped requests must use the client's workspace roots or
|
|
16
|
+
# an explicit project path instead.
|
|
17
|
+
SHARED_WORKSPACE_DIR = DEFAULT_DATA_DIR / "workspace"
|
|
18
|
+
|
|
19
|
+
# Generated output, dependencies, caches, and agent state are not useful source
|
|
20
|
+
# context. Hidden agent state is also excluded because it can contain tokens,
|
|
21
|
+
# credentials, and private prompts.
|
|
22
|
+
IGNORED_DIRECTORIES = {
|
|
23
|
+
".git", ".hg", ".svn", ".venv", "venv", "env", "__pycache__",
|
|
24
|
+
"node_modules", "vendor", "build", "dist", "target", ".dart_tool",
|
|
25
|
+
".next", ".nuxt", ".turbo", ".expo", ".firebase", "coverage", "htmlcov",
|
|
26
|
+
"Pods", "out", ".gradle", ".idea", ".pytest_cache", ".mypy_cache", ".ruff_cache",
|
|
27
|
+
# Local tool state and credential stores.
|
|
28
|
+
".cache", ".config", ".local", ".ssh", ".gnupg", ".aws", ".azure",
|
|
29
|
+
".kube", ".docker", ".npm", ".pnpm-store", ".yarn", ".rustup", ".cargo",
|
|
30
|
+
".conda", ".jupyter", ".vscode-server", ".cursor-server",
|
|
31
|
+
# Build systems and generated artifacts that are frequently huge.
|
|
32
|
+
".build", ".swiftpm", "DerivedData", ".terraform", ".serverless",
|
|
33
|
+
".angular", ".svelte-kit", ".parcel-cache", ".webpack",
|
|
34
|
+
}
|
|
35
|
+
IGNORED_FILE_NAMES = {
|
|
36
|
+
".env", ".env.local", ".env.production", ".env.development",
|
|
37
|
+
".git-credentials", ".netrc", ".pgpass", "id_rsa", "id_ed25519",
|
|
38
|
+
"credentials.json", "secrets.json", "service-account.json",
|
|
39
|
+
"auth.json", "token.json",
|
|
40
|
+
}
|
|
41
|
+
IGNORED_SUFFIXES = {
|
|
42
|
+
".pem", ".key", ".p12", ".pfx", ".keystore", ".jks",
|
|
43
|
+
# Local databases and lock-protected state can contain private records.
|
|
44
|
+
".sqlite", ".sqlite3", ".db", ".db3",
|
|
45
|
+
}
|
|
46
|
+
DEFAULT_MAX_FILE_BYTES = 1_000_000
|
|
47
|
+
DEFAULT_MAX_FILES = 50_000
|
|
48
|
+
DEFAULT_CHUNK_LINES = 80
|
|
49
|
+
DEFAULT_CHUNK_OVERLAP = 12
|
|
50
|
+
|
|
51
|
+
# Escape hatch for intentionally indexing a home directory or filesystem root.
|
|
52
|
+
ALLOW_UNSAFE_ROOT_ENV = "AMOM_ALLOW_UNSAFE_ROOT"
|
|
53
|
+
PROJECT_ROOT_ENV = "AMOM_PROJECT_ROOT"
|
|
54
|
+
LEGACY_ALLOW_UNSAFE_ROOT_ENV = "AGENT_MEMORY_ALLOW_UNSAFE_ROOT"
|
|
55
|
+
LEGACY_PROJECT_ROOT_ENV = "AGENT_MEMORY_PROJECT_ROOT"
|
|
56
|
+
LEGACY_DATABASE_ENV = "AGENT_MEMORY_DB"
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def database_path(override: str | Path | None = None) -> Path:
|
|
60
|
+
"""Return the shared local database path, honoring an explicit override."""
|
|
61
|
+
raw = override or os.environ.get("AMOM_DB") or os.environ.get(LEGACY_DATABASE_ENV)
|
|
62
|
+
return Path(raw).expanduser() if raw else DEFAULT_DATABASE_PATH
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def env_project_root() -> Path | None:
|
|
66
|
+
"""Return the explicit project root supplied through the environment."""
|
|
67
|
+
raw = os.environ.get(PROJECT_ROOT_ENV) or os.environ.get(LEGACY_PROJECT_ROOT_ENV)
|
|
68
|
+
if not raw:
|
|
69
|
+
return None
|
|
70
|
+
path = Path(raw).expanduser()
|
|
71
|
+
return path.resolve() if path.exists() else path
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def is_shared_workspace(root: str | Path) -> bool:
|
|
75
|
+
"""True when ``root`` is the installer's catch-all workspace directory."""
|
|
76
|
+
try:
|
|
77
|
+
return Path(root).expanduser().resolve() == SHARED_WORKSPACE_DIR.resolve()
|
|
78
|
+
except OSError:
|
|
79
|
+
return False
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def is_unsafe_project_root(root: str | Path) -> bool:
|
|
83
|
+
"""Reject filesystem roots and home directories as project roots.
|
|
84
|
+
|
|
85
|
+
Indexing either would crawl enormous unrelated trees and could ingest
|
|
86
|
+
private files outside the project the developer intended to share.
|
|
87
|
+
"""
|
|
88
|
+
try:
|
|
89
|
+
path = Path(root).expanduser().resolve()
|
|
90
|
+
except OSError:
|
|
91
|
+
return True
|
|
92
|
+
return path == Path(path.anchor) or path == Path.home().resolve()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def allow_unsafe_root() -> bool:
|
|
96
|
+
"""Whether the developer explicitly opted into indexing an unsafe root."""
|
|
97
|
+
value = os.environ.get(ALLOW_UNSAFE_ROOT_ENV) or os.environ.get(
|
|
98
|
+
LEGACY_ALLOW_UNSAFE_ROOT_ENV
|
|
99
|
+
)
|
|
100
|
+
return (value or "").strip().lower() in {
|
|
101
|
+
"1",
|
|
102
|
+
"true",
|
|
103
|
+
"yes",
|
|
104
|
+
"on",
|
|
105
|
+
}
|