@allansantos-dev/smart-tool 0.9.57 → 0.9.65

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.
@@ -88,6 +88,10 @@ REGISTRY_PATH = os.path.join(RUN_DIR, "daemon.json")
88
88
  DEFAULT_PORT = int(os.environ.get("SMART_TOOL_PORT", "8765"))
89
89
  PROTOCOL_VERSION = "2025-06-18"
90
90
  MCP_MAX_REQUEST_BYTES = 10 * 1024 * 1024
91
+ # Claude Code aborts an MCP POST whose response has not started within 60 s; a tool call still running after
92
+ # MCP_STREAM_AFTER_S is answered as an SSE stream instead, with a comment every MCP_KEEPALIVE_S.
93
+ MCP_STREAM_AFTER_S = 10
94
+ MCP_KEEPALIVE_S = 15
91
95
  _BOUND_PORT = DEFAULT_PORT
92
96
 
93
97
  DDG_SEARCH_SCRIPT = web_search_adapters.DDG_SEARCH_SCRIPT
@@ -362,10 +366,10 @@ SERVER_INSTRUCTIONS = (
362
366
  TOOLS = [
363
367
  {
364
368
  "name": "project_manage",
365
- "description": "Manages folders and indexing. Every action takes project_root (the folder) or project_id. list gives a short line per project; status shows storage, scope and jobs. graph with symbol (function, Class.method, class name, or path::name when the name repeats) answers what an edit touches: definition, callers with line, calls, tests that reach it through static calls (depth hops, default 3) and files importing it; use it before changing a function to know what to update. affected_tests answers which tests to run after editing: every test file importing a changed file (git diff against base, default HEAD, untracked included) plus changed tests, likeliest failures first, with the command to run them and run_all when config or unanalyzed code changed. graph without symbol reads the indexed view: coverage, references and static calls in Java, Angular, Python and JS/TS/JSX/TSX; includes HTML/CSS/Markdown references and TXT/DOCX coverage; with file_path, narrows to that file's symbols, imports and calls (lists capped by limit). inspect lists the files of a view_id; with file_path, returns only that file's chunks. usage shows consumption and reuse; docs lists public functions without a docstring (Python docstring, JSDoc, Javadoc; private, nested and override functions excluded) with coverage percent, excluding tests unless include_tests=true, for documenting a project that started without it; duplicates lists duplicated functions (identical, near-identical and semantic bodies, min_similarity default 0.90; below that most pairs measured were false positives), excluding tests unless include_tests=true; fix real duplicates; only dismiss with duplicates_dismiss (finding_id, reason=false_positive|intentional, note) what is not a duplicate or is a copy kept on purpose: the finding stays hidden until the code changes, and duplicates_restore reopens it; integration shows the MCP client, hook and tool usage metrics; install_hook_preview shows the change that would install the hook redirecting Grep/Read to smart_search and WebSearch/WebFetch to web_search/web_fetch in this session's client and returns a plan_id; install_hook writes it with that plan_id and confirm=true, with a backup, only after the user approves the change; search_limits saves the project's default top_k per block ({code, test, doc}, each 1-20, total at most 20); storage shows disk usage and views; pin_view protects a view; cleanup_preview and cleanup_commit remove selected views after confirmation; compare compares two stored views, without checkout or embeddings. register adds a folder; preview checks the scope; index/rebuild update; pause/cancel keep checkpoints; resume continues; policy picks on_search/eager; scope adjusts the scope; remove deletes only the local index. setup_audit (project_root) reports what Claude Code and Codex actually load for that folder (settings layers, plugins, hooks, MCP servers, skills, CLAUDE.md/AGENTS.md) and where they collide: hooks deciding on the same tool in parallel, missing hook scripts, silenced hooks, MCP servers down or offering duplicate tools, skill name and budget problems, AGENTS.md that only Codex reads, broken @imports; what could not be checked is listed as not verified; each finding carries its fix (the file to edit or the command to run), applied by the agent when the user agrees, never by Smart Tool; mode=last returns the stored report instead of auditing again. redirect_proposals (no project_root) lists the redirect patterns proposed from allowed searches, most matched first, with a guide; redirect_decide decides them by ids: accept, instruct (tip without blocking), reject, or remove an accepted one. Jobs via smart_search_result.",
369
+ "description": "Manages folders and indexing. Every action takes project_root (the folder) or project_id. list gives a short line per project; status shows storage, scope and jobs. graph with symbol (function, Class.method, class name, or path::name when the name repeats) answers what an edit touches: definition, callers with line, calls, tests that reach it through static calls (depth hops, default 3) and files importing it; use it before changing a function to know what to update. affected_tests answers which tests to run after editing: every test file importing a changed file (git diff against base, default HEAD, untracked included) plus changed tests, likeliest failures first, with the command to run them and run_all when config or unanalyzed code changed. pr_impact (base, default HEAD; main or origin/main for a branch) returns a Markdown report of the change for a pull request: the functions the diff touches with their callers, the documentation entries it leaves out of date and the tests to run with their command; it publishes nothing. graph without symbol reads the indexed view: coverage, references and static calls in Java, Angular, Python, Go and JS/TS/JSX/TSX; includes HTML/CSS/Markdown references and TXT/DOCX coverage; with file_path, narrows to that file's symbols, imports and calls (lists capped by limit). inspect lists the files of a view_id; with file_path, returns only that file's chunks. usage shows consumption and reuse; docs lists public functions without a docstring (Python docstring, JSDoc, Javadoc, Go doc comment starting with the name; private, nested and override functions excluded) with coverage percent, excluding tests unless include_tests=true, for documenting a project that started without it; docs generate=true writes the documentation scope to docs/scope in the project in three levels tied to the code: technical pages by folder (an entry per public function anchored on path::name with signature, first doc line, layer, callers, calls and tests, plus a Mermaid flow by layer), functional.md (each entry point and the functions its flow reaches, by layer) and simple.md; the prose between the prose markers is kept by regeneration; docs write_prose=true writes the empty prose blocks with the Docs model of the setup (each paragraph checked against the code first) within about 40 s and says how many are left, call it again until none is; docs check=true reports anchors whose function changed signature or is gone and broken links; docs validate=true checks every claim of docs/scope against the code (anchors, links, line numbers, callers and tests, coverage, entry points, command-line options, prose names, counts, glossary, navigation, versions) and writes docs/scope/validation.md with pass/fail per check, the evidence, a score and the verdict (generate runs it too); docs with symbol lists where that function is documented; layers declares the project's layers (ordered highest first, each with path globs and annotations): without layers it returns the saved rule or a suggestion read from folder names and annotations, with files, symbols and calls between layers (direction down or up); pass layers to preview a rule and the same layers with confirm=true to save it in the project (.smart-tool/layers.json); duplicates lists duplicated functions (identical, near-identical and semantic bodies, min_similarity default 0.90; below that most pairs measured were false positives), excluding tests unless include_tests=true; fix real duplicates; only dismiss with duplicates_dismiss (finding_id, reason=false_positive|intentional, note) what is not a duplicate or is a copy kept on purpose: the finding stays hidden until the code changes, and duplicates_restore reopens it; integration shows the MCP client, hook and tool usage metrics; install_hook_preview shows the change that would install the hook redirecting Grep/Read to smart_search and WebSearch/WebFetch to web_search/web_fetch in this session's client and returns a plan_id; install_hook writes it with that plan_id and confirm=true, with a backup, only after the user approves the change; search_limits saves the project's default top_k per block ({code, test, doc}, each 1-20, total at most 20); storage shows disk usage and views; pin_view protects a view; cleanup_preview and cleanup_commit remove selected views after confirmation; compare compares two stored views, without checkout or embeddings. register adds a folder; preview checks the scope; index/rebuild update; pause/cancel keep checkpoints; resume continues; policy picks on_search/eager; scope adjusts the scope; remove deletes only the local index. setup_audit (project_root) reports what Claude Code and Codex actually load for that folder (settings layers, plugins, hooks, MCP servers, skills, CLAUDE.md/AGENTS.md) and where they collide: hooks deciding on the same tool in parallel, missing hook scripts, silenced hooks, MCP servers down or offering duplicate tools, skill name and budget problems, AGENTS.md that only Codex reads, broken @imports; what could not be checked is listed as not verified; each finding carries its fix (the file to edit or the command to run), applied by the agent when the user agrees, never by Smart Tool; mode=last returns the stored report instead of auditing again. redirect_proposals (no project_root) lists the redirect patterns proposed from allowed searches, most matched first, with a guide; redirect_decide decides them by ids: accept, instruct (tip without blocking), reject, or remove an accepted one. Jobs via smart_search_result.",
366
370
  "inputSchema": {
367
371
  "type": "object", "properties": {
368
- "action": {"type": "string", "enum": ["list", "status", "register", "preview", "index", "rebuild", "pause", "cancel", "resume", "watch", "scope", "remove", "relocate", "probe", "policy", "inspect", "graph", "usage", "storage", "pin_view", "cleanup_preview", "cleanup_commit", "compare", "search_limits", "integration", "install_hook_preview", "install_hook", "duplicates", "duplicates_dismiss", "duplicates_restore", "docs", "affected_tests", "setup_audit", "redirect_proposals", "redirect_decide"]},
372
+ "action": {"type": "string", "enum": ["list", "status", "register", "preview", "index", "rebuild", "pause", "cancel", "resume", "watch", "scope", "remove", "relocate", "probe", "policy", "inspect", "graph", "usage", "storage", "pin_view", "cleanup_preview", "cleanup_commit", "compare", "search_limits", "integration", "install_hook_preview", "install_hook", "duplicates", "duplicates_dismiss", "duplicates_restore", "docs", "layers", "affected_tests", "pr_impact", "setup_audit", "redirect_proposals", "redirect_decide"]},
369
373
  "ids": {"type": "array", "items": {"type": "string"}, "description": "redirect_decide: proposal ids from redirect_proposals (accepted ids for remove)"},
370
374
  "decision": {"type": "string", "enum": ["accept", "instruct", "reject", "remove"], "description": "redirect_decide: accept redirects matching calls to smart_search; instruct lets them run with a tip naming smart_search; reject discards; remove deletes an accepted pattern"},
371
375
  "finding_id": {"type": "string"},
@@ -375,6 +379,15 @@ TOOLS = [
375
379
  "min_similarity": {"type": "number", "minimum": 0.5, "maximum": 1, "default": 0.9,
376
380
  "description": "Semantic cutoff for duplicates; keep 0.90: below it most pairs were false positives in measurement"},
377
381
  "include_tests": {"type": "boolean", "default": False},
382
+ "generate": {"type": "boolean", "default": False, "description": "docs: write the documentation scope to docs/scope, keeping the prose already written"},
383
+ "check": {"type": "boolean", "default": False, "description": "docs: report documentation anchors out of date with the code"},
384
+ "validate": {"type": "boolean", "default": False, "description": "docs: check every claim of docs/scope against the code and write the pass/fail report docs/scope/validation.md"},
385
+ "write_prose": {"type": "boolean", "default": False, "description": "docs: write the empty prose blocks of docs/scope with the Docs model, then regenerate and validate"},
386
+ "layers": {"type": "array", "description": "layers: the rule, highest layer first", "items": {
387
+ "type": "object", "additionalProperties": False, "required": ["name"], "properties": {
388
+ "name": {"type": "string"},
389
+ "paths": {"type": "array", "items": {"type": "string"}, "description": "globs relative to the project root, e.g. internal/handler/**"},
390
+ "annotations": {"type": "array", "items": {"type": "string"}, "description": "e.g. @RestController, @app.route"}}}},
378
391
  "limit": {"type": "integer", "minimum": 1, "maximum": 200, "default": 30},
379
392
  "days": {"type":"integer","minimum":1,"maximum":366},
380
393
  "top_k": {"type": "object", "additionalProperties": False, "properties": {
@@ -389,7 +402,7 @@ TOOLS = [
389
402
  "mode": {"type": "string", "enum": ["run", "last"], "default": "run", "description": "setup_audit: run audits now; last reads the stored report (the service audits registered projects every 30 minutes) without running anything, and fails when none is stored"},
390
403
  "probe": {"type": "boolean", "default": True, "description": "setup_audit: start/call each MCP server (initialize + tools/list); false reads configs only"},
391
404
  "base": {"type": "string", "default": "HEAD", "description": "affected_tests: git revision the changes are compared with (HEAD = uncommitted work; main or origin/main for a branch)"},
392
- "symbol": {"type": "string", "description": "graph: function, Class.method, class name or path::name to get its callers, calls, tests and importers"},
405
+ "symbol": {"type": "string", "description": "graph: function, Class.method, class name or path::name to get its callers, calls, tests and importers; docs: where that function is documented"},
393
406
  "depth": {"type": "integer", "minimum": 1, "maximum": 4, "default": 3, "description": "graph with symbol: call hops searched for tests"},
394
407
  "project_root": {"type": "string"}, "project_id": {"type": "string"},
395
408
  "job_id": {"type": "string"}, "watch": {"type": "boolean"},
@@ -2122,7 +2135,7 @@ def _project_action(arguments):
2122
2135
  action = arguments.get("action", "list")
2123
2136
  if action == "probe":
2124
2137
  return _probe_index_models()
2125
- if action in ('graph','compare','duplicates','duplicates_dismiss','duplicates_restore','docs','affected_tests'):
2138
+ if action in ('graph','compare','duplicates','duplicates_dismiss','duplicates_restore','docs','layers','affected_tests','pr_impact'):
2126
2139
  # Análise somente leitura pode demorar; não prende o lock global de ações.
2127
2140
  key=arguments.get('project_id')
2128
2141
  if not isinstance(key,str) or not re.fullmatch(r'[a-f0-9]{16}',key):
@@ -2134,11 +2147,39 @@ def _project_action(arguments):
2134
2147
  return _find_duplicates(project['root'], arguments, project.get('dismissed_duplicates') or {})
2135
2148
  if action=='docs':
2136
2149
  import doc_check
2150
+ import doc_scope
2137
2151
  include_tests=arguments.get('include_tests',False)
2138
- if type(include_tests) is not bool:
2139
- raise ValueError('include_tests must be true or false.')
2152
+ generate,check,symbol=arguments.get('generate',False),arguments.get('check',False),arguments.get('symbol')
2153
+ validate,write_prose=arguments.get('validate',False),arguments.get('write_prose',False)
2154
+ for name,value in (('include_tests',include_tests),('generate',generate),('check',check),('validate',validate),('write_prose',write_prose)):
2155
+ if type(value) is not bool:
2156
+ raise ValueError(f'{name} must be true or false.')
2157
+ if generate+check+validate+write_prose+(symbol is not None)+include_tests>1:
2158
+ raise ValueError('docs takes one of generate=true, check=true, validate=true, write_prose=true, symbol or include_tests=true.')
2140
2159
  limit=_parse_bounded_int(arguments.get('limit'),default=30,min_v=1,max_v=200,field_name='limit')
2160
+ if generate:
2161
+ return doc_scope.generate(project['root'],arguments.get('view_id'),limit)
2162
+ if check:
2163
+ return doc_scope.check(project['root'],arguments.get('view_id'),limit)
2164
+ if validate:
2165
+ import doc_validate
2166
+ return doc_validate.validate(project['root'],arguments.get('view_id'),limit)
2167
+ if write_prose:
2168
+ problem=_gateway_problem()
2169
+ if problem:
2170
+ raise RuntimeError(f'{problem} The prose is written by the Docs model through the gateway; no block was written.')
2171
+ import doc_prose
2172
+ return doc_prose.write(project['root'],arguments.get('view_id'),limit)
2173
+ if symbol is not None:
2174
+ return doc_scope.anchors(project['root'],symbol,arguments.get('view_id'))
2141
2175
  return doc_check.coverage(project['root'],include_tests,limit,arguments.get('view_id'))
2176
+ if action=='layers':
2177
+ import layers
2178
+ return layers.report(project['root'],arguments.get('layers'),arguments.get('confirm',False),arguments.get('view_id'))
2179
+ if action=='pr_impact':
2180
+ import pr_impact
2181
+ limit=_parse_bounded_int(arguments.get('limit'),default=30,min_v=1,max_v=200,field_name='limit')
2182
+ return pr_impact.report(project['root'],arguments.get('base','HEAD'),limit,arguments.get('view_id'))
2142
2183
  if action=='affected_tests':
2143
2184
  import affected_tests
2144
2185
  limit=_parse_bounded_int(arguments.get('limit'),default=30,min_v=1,max_v=200,field_name='limit')
@@ -4211,6 +4252,16 @@ def _dispatch(method, params, session_id):
4211
4252
  raise KeyError(method)
4212
4253
 
4213
4254
 
4255
+ def _rpc_reply(method, params, session_id, req_id):
4256
+ """JSON-RPC response to one request: its result, or the error the client should see."""
4257
+ try:
4258
+ return {"jsonrpc": "2.0", "id": req_id, "result": _dispatch(method, params, session_id)}
4259
+ except KeyError:
4260
+ return {"jsonrpc": "2.0", "id": req_id, "error": {"code": -32601, "message": f"method not supported: {method}"}}
4261
+ except Exception as exc:
4262
+ return {"jsonrpc": "2.0", "id": req_id, "error": {"code": -32000, "message": _sanitize_text(str(exc))}}
4263
+
4264
+
4214
4265
  _LOOPBACK_HOSTNAMES = frozenset({"127.0.0.1", "localhost", "::1"})
4215
4266
 
4216
4267
 
@@ -4350,9 +4401,31 @@ class MCPHandler(BaseHTTPRequestHandler):
4350
4401
  raise RequestTooLarge(f"Request body over {max_bytes} bytes")
4351
4402
  return self.rfile.read(length) if length else b""
4352
4403
 
4404
+ def _mcp_method_not_allowed(self):
4405
+ """405 for GET and DELETE on /mcp: no server-initiated stream and no client-ended sessions. The spec's answer for
4406
+ both; the MCP SDK client treats any other status, 404 included, as a broken connection."""
4407
+ self.send_response(405)
4408
+ self.send_header("Allow", "POST")
4409
+ self.send_header("Content-Length", "0")
4410
+ self.end_headers()
4411
+
4412
+ def do_DELETE(self):
4413
+ """Routes a DELETE: 405 on /mcp, 404 elsewhere."""
4414
+ if self._reject_foreign_origin():
4415
+ return
4416
+ if self.path == "/mcp":
4417
+ self._mcp_method_not_allowed()
4418
+ return
4419
+ self._write_json(404, {"error": "not found"})
4420
+
4353
4421
  def do_GET(self):
4422
+ """Routes a GET: 405 on /mcp, the health and status probes, the setup and project pages and their data; 404
4423
+ elsewhere."""
4354
4424
  if self._reject_foreign_origin():
4355
4425
  return
4426
+ if self.path == "/mcp":
4427
+ self._mcp_method_not_allowed()
4428
+ return
4356
4429
  if self.path == "/health":
4357
4430
  self._write_json(200, {"status": "ok", "version": version.VERSION})
4358
4431
  return
@@ -4508,17 +4581,42 @@ class MCPHandler(BaseHTTPRequestHandler):
4508
4581
  self.end_headers()
4509
4582
  return
4510
4583
 
4511
- try:
4512
- result = _dispatch(method, params, session_id)
4513
- except KeyError:
4514
- self._write_json(200, {"jsonrpc": "2.0", "id": req_id, "error": {"code": -32601, "message": f"method not supported: {method}"}})
4584
+ if method == "tools/call" and "text/event-stream" in (self.headers.get("Accept") or ""):
4585
+ self._answer_tool_call(params, session_id, req_id)
4515
4586
  return
4516
- except Exception as exc:
4517
- self._write_json(200, {"jsonrpc": "2.0", "id": req_id, "error": {"code": -32000, "message": _sanitize_text(str(exc))}})
4587
+ reply = _rpc_reply(method, params, session_id, req_id)
4588
+ headers = {"Mcp-Session-Id": session_id} if method == "initialize" and "result" in reply else {}
4589
+ self._write_json(200, reply, headers)
4590
+
4591
+ def _answer_tool_call(self, params, session_id, req_id):
4592
+ """Answers a tools/call from a client that accepts SSE: one JSON body when the call ends within
4593
+ MCP_STREAM_AFTER_S, else an SSE stream started at once, a comment every MCP_KEEPALIVE_S and the reply as its
4594
+ only event. A client gone before the end leaves the call to finish unanswered, as a closed JSON request does."""
4595
+ box = {}
4596
+ worker = threading.Thread(target=lambda: box.update(reply=_rpc_reply("tools/call", params, session_id, req_id)),
4597
+ daemon=True)
4598
+ worker.start()
4599
+ worker.join(MCP_STREAM_AFTER_S)
4600
+ if not worker.is_alive():
4601
+ self._write_json(200, box["reply"])
4518
4602
  return
4519
-
4520
- headers = {"Mcp-Session-Id": session_id} if method == "initialize" else {}
4521
- self._write_json(200, {"jsonrpc": "2.0", "id": req_id, "result": result}, headers)
4603
+ try:
4604
+ self.send_response(200)
4605
+ self.send_header("Content-Type", "text/event-stream")
4606
+ self.send_header("Cache-Control", "no-store")
4607
+ self.send_header("X-Content-Type-Options", "nosniff")
4608
+ self.end_headers()
4609
+ self.wfile.flush()
4610
+ worker.join(MCP_KEEPALIVE_S)
4611
+ while worker.is_alive():
4612
+ self.wfile.write(b": keepalive\n\n")
4613
+ self.wfile.flush()
4614
+ worker.join(MCP_KEEPALIVE_S)
4615
+ event = json.dumps(box["reply"], ensure_ascii=False)
4616
+ self.wfile.write(f"event: message\ndata: {event}\n\n".encode("utf-8"))
4617
+ self.wfile.flush()
4618
+ except (ConnectionError, BrokenPipeError):
4619
+ pass
4522
4620
 
4523
4621
 
4524
4622
  def _write_registry(port):
@@ -4551,9 +4649,11 @@ def _smart_tool_answers(port):
4551
4649
 
4552
4650
 
4553
4651
  def _bind_server():
4554
- """Default port, then the port clients already know, then any free port (announced in daemon.json and synced to
4555
- the clients by endpoint_sync). A port held by another live Smart Tool stops this start instead of duplicating it."""
4556
- for port in dict.fromkeys(p for p in (DEFAULT_PORT, endpoint_sync.known_port(), 0) if p is not None):
4652
+ """The port clients already know, then the default port, then any free port (announced in daemon.json and synced
4653
+ to the clients by endpoint_sync). A connected session keeps the URL it started with, so a restart that moved
4654
+ back to the default would cut every session on the known port. A port held by another live Smart Tool stops this
4655
+ start instead of duplicating it."""
4656
+ for port in dict.fromkeys(p for p in (endpoint_sync.known_port(), DEFAULT_PORT, 0) if p is not None):
4557
4657
  try:
4558
4658
  return _ExclusiveThreadingHTTPServer(("127.0.0.1", port), MCPHandler)
4559
4659
  except OSError:
package/usage_meter.py CHANGED
@@ -11,7 +11,7 @@ import project_store
11
11
 
12
12
  _CONTEXT=contextvars.ContextVar('smart_usage',default=None)
13
13
  _PHASES={'scope':'scope','embedding_query':'query_embedding','model_probe':'embedding_probe',
14
- 'indexing':'index_embedding','reranking':'rerank','scout':'scout'}
14
+ 'indexing':'index_embedding','reranking':'rerank','scout':'scout','docs':'docs'}
15
15
 
16
16
 
17
17
  @contextlib.contextmanager
@@ -23,6 +23,11 @@ def job(root,view,job_id):
23
23
  finally:_CONTEXT.reset(token)
24
24
 
25
25
 
26
+ def purpose():
27
+ """Purpose of the model calls made now ('scope', 'docs'...), None outside a metered job."""
28
+ return (_CONTEXT.get() or {}).get('purpose')
29
+
30
+
26
31
  def phase(name):
27
32
  current=_CONTEXT.get()
28
33
  if current and name in _PHASES:_CONTEXT.set({**current,'purpose':_PHASES[name]})
package/version.py CHANGED
@@ -1,7 +1,7 @@
1
1
  """Single source of the Smart Tool version and of the User-Agent sent to public APIs."""
2
2
  import re
3
3
 
4
- VERSION = "0.9.57"
4
+ VERSION = "0.9.65"
5
5
  _CONTACT_RE = re.compile(r"^(?:[^\s@()<>;]+@[^\s@()<>;]+\.[^\s@()<>;]+|https?://[^\s()<>;]+)$")
6
6
 
7
7
 
@@ -0,0 +1,66 @@
1
+ """Removes the Camoufox builds older than the active one after a verified update. camoufox 0.5.6 installs each build in
2
+ its own folder under `browsers/<repo>/` and never deletes the previous ones, so the cache grows by about 2.5 GB per
3
+ build. Nothing is removed while the user pinned a build (`camoufox set` writes `pinned` in config.json), and only the
4
+ active build's own repository is pruned: the active build and any newer one stay. Each folder is first renamed to a
5
+ hidden name, which Windows refuses while any file inside is open, so a build in use is skipped whole instead of left
6
+ half removed; a hidden folder left by an interrupted deletion is removed on the next run."""
7
+ import os
8
+ import shutil
9
+ import time
10
+
11
+ REMOVING_PREFIX = ".removing-"
12
+
13
+
14
+ def folder_size(path):
15
+ """Bytes of the files under path."""
16
+ return sum(os.path.getsize(os.path.join(root, name)) for root, _dirs, names in os.walk(path) for name in names)
17
+
18
+
19
+ def _delete(build, browsers_dir):
20
+ """Renames the build's folder to a hidden one in browsers_dir and deletes it; the size freed, or None when the
21
+ rename was refused because a file inside is in use."""
22
+ name = f"{build.repo_name}/{build.path.name}"
23
+ size = folder_size(build.path)
24
+ hidden = browsers_dir / f"{REMOVING_PREFIX}{build.repo_name}-{build.path.name}-{time.time_ns()}"
25
+ try:
26
+ os.rename(build.path, hidden)
27
+ except PermissionError as exc:
28
+ print(f"Kept Camoufox build {name}: in use ({exc.strerror})", flush=True)
29
+ return None
30
+ shutil.rmtree(hidden)
31
+ print(f"Removed old Camoufox build {name} ({size / 2**20:.0f} MB)", flush=True)
32
+ return size
33
+
34
+
35
+ def prune(installed, config, browsers_dir):
36
+ """Deletes the builds in installed (camoufox `InstalledVersion`s) of the active build's repository whose version is
37
+ older than the active one, unless config pins a build, and returns the bytes freed."""
38
+ if config.get("pinned"):
39
+ print(f"Camoufox build pinned to {config['pinned']}: old builds kept", flush=True)
40
+ return 0
41
+ freed = 0
42
+ for leftover in browsers_dir.glob(f"{REMOVING_PREFIX}*"):
43
+ size = folder_size(leftover)
44
+ shutil.rmtree(leftover)
45
+ freed += size
46
+ print(f"Removed an interrupted deletion: {leftover.name} ({size / 2**20:.0f} MB)", flush=True)
47
+ active = next((build for build in installed if build.is_active), None)
48
+ if active is None:
49
+ print("No active Camoufox build: old builds kept", flush=True)
50
+ return freed
51
+ for build in installed:
52
+ if build.repo_name == active.repo_name and build.version < active.version:
53
+ freed += _delete(build, browsers_dir) or 0
54
+ print(f"Old Camoufox builds pruned: {freed / 2**30:.2f} GB freed; active build "
55
+ f"{active.repo_name}/{active.path.name} kept", flush=True)
56
+ return freed
57
+
58
+
59
+ def main():
60
+ """Prunes the user's real Camoufox cache, read through camoufox's own multiversion manager."""
61
+ from camoufox import multiversion
62
+ prune(multiversion.list_installed(), multiversion.load_config(), multiversion.BROWSERS_DIR)
63
+
64
+
65
+ if __name__ == "__main__":
66
+ main()
package/web_fetch.py CHANGED
@@ -71,7 +71,7 @@ def _ip_allowed(ip):
71
71
 
72
72
 
73
73
  def _public_address(host, port):
74
- """Resolves once; every address must be publicly routable (no private, loopback, link-local, CGNAT/Tailscale)."""
74
+ """Resolves once; every address must be publicly routable (no private, loopback, link-local, CGNAT)."""
75
75
  try:
76
76
  infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
77
77
  except (socket.gaierror, UnicodeError):