Add files using upload-large-folder tool
Browse files- scripts/ci_classifier.py +1 -0
- scripts/sync_huggingface.py +36 -34
- src/ctx_init.py +161 -26
- src/ctx_monitor.py +1 -0
scripts/ci_classifier.py
CHANGED
|
@@ -32,6 +32,7 @@ DOCS_PATTERNS = (
|
|
| 32 |
GRAPH_ARTIFACT_PATTERNS = (
|
| 33 |
"graph/communities.json",
|
| 34 |
"graph/skills-sh-catalog.json.gz",
|
|
|
|
| 35 |
"graph/wiki-graph.tar.gz",
|
| 36 |
"graph/*.html",
|
| 37 |
)
|
|
|
|
| 32 |
GRAPH_ARTIFACT_PATTERNS = (
|
| 33 |
"graph/communities.json",
|
| 34 |
"graph/skills-sh-catalog.json.gz",
|
| 35 |
+
"graph/wiki-graph-runtime.tar.gz",
|
| 36 |
"graph/wiki-graph.tar.gz",
|
| 37 |
"graph/*.html",
|
| 38 |
)
|
scripts/sync_huggingface.py
CHANGED
|
@@ -3,13 +3,14 @@
|
|
| 3 |
|
| 4 |
from __future__ import annotations
|
| 5 |
|
| 6 |
-
import argparse
|
| 7 |
-
import os
|
| 8 |
-
import shutil
|
| 9 |
-
import subprocess
|
| 10 |
-
import sys
|
| 11 |
-
import tempfile
|
| 12 |
-
from pathlib import Path
|
|
|
|
| 13 |
|
| 14 |
DEFAULT_REPO_ID = "Stevesolun/ctx"
|
| 15 |
DEFAULT_REPO_TYPE = "dataset"
|
|
@@ -34,6 +35,7 @@ tags:
|
|
| 34 |
LFS_POINTER_PREFIX = b"version https://git-lfs.github.com/spec/v1"
|
| 35 |
HYDRATED_ARTIFACT_MIN_BYTES = {
|
| 36 |
Path("graph/wiki-graph.tar.gz"): 100_000_000,
|
|
|
|
| 37 |
Path("graph/skills-sh-catalog.json.gz"): 1_000_000,
|
| 38 |
}
|
| 39 |
|
|
@@ -73,11 +75,11 @@ def _iter_tracked_files(repo: Path) -> list[Path]:
|
|
| 73 |
return files
|
| 74 |
|
| 75 |
|
| 76 |
-
def _assert_hydrated_artifacts(repo: Path) -> None:
|
| 77 |
-
for rel, min_bytes in HYDRATED_ARTIFACT_MIN_BYTES.items():
|
| 78 |
-
artifact = repo / rel
|
| 79 |
-
if not artifact.is_file():
|
| 80 |
-
raise FileNotFoundError(
|
| 81 |
f"{rel.as_posix()} is required before Hugging Face sync"
|
| 82 |
)
|
| 83 |
size = artifact.stat().st_size
|
|
@@ -88,26 +90,26 @@ def _assert_hydrated_artifacts(repo: Path) -> None:
|
|
| 88 |
)
|
| 89 |
with artifact.open("rb") as fh:
|
| 90 |
prefix = fh.read(len(LFS_POINTER_PREFIX))
|
| 91 |
-
if prefix == LFS_POINTER_PREFIX:
|
| 92 |
-
raise RuntimeError(
|
| 93 |
-
f"{rel.as_posix()} is a Git LFS pointer, not the hydrated artifact"
|
| 94 |
-
)
|
| 95 |
-
_validate_graph_artifact_integrity(repo)
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def _validate_graph_artifact_integrity(repo: Path) -> None:
|
| 99 |
-
src_dir = repo / "src"
|
| 100 |
-
if str(src_dir) not in sys.path:
|
| 101 |
-
sys.path.insert(0, str(src_dir))
|
| 102 |
-
try:
|
| 103 |
-
from validate_graph_artifacts import validate_graph_artifacts
|
| 104 |
-
|
| 105 |
-
validate_graph_artifacts(repo / "graph")
|
| 106 |
-
except Exception as exc: # noqa: BLE001
|
| 107 |
-
raise RuntimeError(
|
| 108 |
-
"graph artifact integrity validation failed before Hugging Face sync: "
|
| 109 |
-
f"{exc}"
|
| 110 |
-
) from exc
|
| 111 |
|
| 112 |
|
| 113 |
def _export_tracked_tree(repo: Path, export_dir: Path) -> None:
|
|
@@ -162,7 +164,7 @@ def _repo_commit_url(*, repo_id: str, repo_type: str, sha: str) -> str:
|
|
| 162 |
|
| 163 |
def _remote_has_no_stale_paths(
|
| 164 |
*,
|
| 165 |
-
api:
|
| 166 |
export_dir: Path,
|
| 167 |
repo_id: str,
|
| 168 |
repo_type: str,
|
|
@@ -176,7 +178,7 @@ def _remote_has_no_stale_paths(
|
|
| 176 |
|
| 177 |
def _upload_export(
|
| 178 |
*,
|
| 179 |
-
api:
|
| 180 |
export_dir: Path,
|
| 181 |
repo_id: str,
|
| 182 |
repo_type: str,
|
|
|
|
| 3 |
|
| 4 |
from __future__ import annotations
|
| 5 |
|
| 6 |
+
import argparse
|
| 7 |
+
import os
|
| 8 |
+
import shutil
|
| 9 |
+
import subprocess
|
| 10 |
+
import sys
|
| 11 |
+
import tempfile
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
from typing import Any
|
| 14 |
|
| 15 |
DEFAULT_REPO_ID = "Stevesolun/ctx"
|
| 16 |
DEFAULT_REPO_TYPE = "dataset"
|
|
|
|
| 35 |
LFS_POINTER_PREFIX = b"version https://git-lfs.github.com/spec/v1"
|
| 36 |
HYDRATED_ARTIFACT_MIN_BYTES = {
|
| 37 |
Path("graph/wiki-graph.tar.gz"): 100_000_000,
|
| 38 |
+
Path("graph/wiki-graph-runtime.tar.gz"): 10_000_000,
|
| 39 |
Path("graph/skills-sh-catalog.json.gz"): 1_000_000,
|
| 40 |
}
|
| 41 |
|
|
|
|
| 75 |
return files
|
| 76 |
|
| 77 |
|
| 78 |
+
def _assert_hydrated_artifacts(repo: Path) -> None:
|
| 79 |
+
for rel, min_bytes in HYDRATED_ARTIFACT_MIN_BYTES.items():
|
| 80 |
+
artifact = repo / rel
|
| 81 |
+
if not artifact.is_file():
|
| 82 |
+
raise FileNotFoundError(
|
| 83 |
f"{rel.as_posix()} is required before Hugging Face sync"
|
| 84 |
)
|
| 85 |
size = artifact.stat().st_size
|
|
|
|
| 90 |
)
|
| 91 |
with artifact.open("rb") as fh:
|
| 92 |
prefix = fh.read(len(LFS_POINTER_PREFIX))
|
| 93 |
+
if prefix == LFS_POINTER_PREFIX:
|
| 94 |
+
raise RuntimeError(
|
| 95 |
+
f"{rel.as_posix()} is a Git LFS pointer, not the hydrated artifact"
|
| 96 |
+
)
|
| 97 |
+
_validate_graph_artifact_integrity(repo)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def _validate_graph_artifact_integrity(repo: Path) -> None:
|
| 101 |
+
src_dir = repo / "src"
|
| 102 |
+
if str(src_dir) not in sys.path:
|
| 103 |
+
sys.path.insert(0, str(src_dir))
|
| 104 |
+
try:
|
| 105 |
+
from validate_graph_artifacts import validate_graph_artifacts
|
| 106 |
+
|
| 107 |
+
validate_graph_artifacts(repo / "graph")
|
| 108 |
+
except Exception as exc: # noqa: BLE001
|
| 109 |
+
raise RuntimeError(
|
| 110 |
+
"graph artifact integrity validation failed before Hugging Face sync: "
|
| 111 |
+
f"{exc}"
|
| 112 |
+
) from exc
|
| 113 |
|
| 114 |
|
| 115 |
def _export_tracked_tree(repo: Path, export_dir: Path) -> None:
|
|
|
|
| 164 |
|
| 165 |
def _remote_has_no_stale_paths(
|
| 166 |
*,
|
| 167 |
+
api: Any,
|
| 168 |
export_dir: Path,
|
| 169 |
repo_id: str,
|
| 170 |
repo_type: str,
|
|
|
|
| 178 |
|
| 179 |
def _upload_export(
|
| 180 |
*,
|
| 181 |
+
api: Any,
|
| 182 |
export_dir: Path,
|
| 183 |
repo_id: str,
|
| 184 |
repo_type: str,
|
src/ctx_init.py
CHANGED
|
@@ -212,9 +212,14 @@ def _resolve_ctx_src_dir() -> Path:
|
|
| 212 |
|
| 213 |
|
| 214 |
_GRAPH_ARCHIVE_NAME = "wiki-graph.tar.gz"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
_GRAPH_RELEASE_URL = (
|
| 216 |
"https://github.com/stevesolun/ctx/releases/download/"
|
| 217 |
-
"v{version}/
|
| 218 |
)
|
| 219 |
_GRAPH_REQUIRED_FILES = frozenset({
|
| 220 |
"index.md",
|
|
@@ -232,7 +237,24 @@ _GRAPH_MANAGED_PATHS = (
|
|
| 232 |
"concepts",
|
| 233 |
"external-catalogs",
|
| 234 |
"index.md",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
|
| 238 |
def build_graph(
|
|
@@ -240,27 +262,35 @@ def build_graph(
|
|
| 240 |
*,
|
| 241 |
force: bool = False,
|
| 242 |
graph_url: str | None = None,
|
|
|
|
| 243 |
) -> int:
|
| 244 |
"""Install the pre-built knowledge graph into ``~/.claude/skill-wiki``."""
|
|
|
|
|
|
|
| 245 |
claude_dir = claude or _claude_dir()
|
| 246 |
wiki_dir = claude_dir / "skill-wiki"
|
| 247 |
graph_json = wiki_dir / "graphify-out" / "graph.json"
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
print(f"Graph already installed at {graph_json}; use --force to refresh.")
|
| 250 |
return 0
|
| 251 |
|
| 252 |
temp_dir: tempfile.TemporaryDirectory[str] | None = None
|
| 253 |
-
archive = _find_local_graph_archive()
|
| 254 |
try:
|
| 255 |
if archive is None:
|
| 256 |
temp_dir = tempfile.TemporaryDirectory(prefix="ctx-graph-download-")
|
| 257 |
-
archive = Path(temp_dir.name) /
|
| 258 |
-
url = graph_url or _release_graph_url()
|
| 259 |
print(f"Downloading pre-built graph from {url}")
|
| 260 |
_download_graph_archive(archive, url=url)
|
| 261 |
else:
|
| 262 |
print(f"Installing pre-built graph from {archive}")
|
| 263 |
-
_extract_graph_archive(archive, wiki_dir)
|
| 264 |
except Exception as exc:
|
| 265 |
print(
|
| 266 |
f" [error] graph install failed: {type(exc).__name__}: {exc}",
|
|
@@ -279,20 +309,32 @@ def build_graph(
|
|
| 279 |
return 0
|
| 280 |
|
| 281 |
|
| 282 |
-
def _find_local_graph_archive() -> Path | None:
|
| 283 |
module_path = Path(__file__).resolve()
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
for candidate in candidates:
|
| 289 |
if candidate.is_file():
|
| 290 |
return candidate
|
| 291 |
return None
|
| 292 |
|
| 293 |
|
| 294 |
-
def _release_graph_url() -> str:
|
| 295 |
-
return _GRAPH_RELEASE_URL.format(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
|
| 298 |
def _package_version() -> str:
|
|
@@ -313,25 +355,46 @@ def _download_graph_archive(destination: Path, *, url: str) -> None:
|
|
| 313 |
shutil.copyfileobj(response, fh)
|
| 314 |
|
| 315 |
|
| 316 |
-
def _extract_graph_archive(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
target_dir.parent.mkdir(parents=True, exist_ok=True)
|
| 318 |
with tempfile.TemporaryDirectory(
|
| 319 |
prefix=f".{target_dir.name}-stage-",
|
| 320 |
dir=target_dir.parent,
|
| 321 |
) as staging_name:
|
| 322 |
staging_dir = Path(staging_name)
|
| 323 |
-
_extract_graph_archive_to_dir(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
_validate_graph_install_tree(staging_dir)
|
| 325 |
_promote_graph_tree(staging_dir, target_dir)
|
| 326 |
|
| 327 |
|
| 328 |
-
def _extract_graph_archive_to_dir(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
target_dir.mkdir(parents=True, exist_ok=True)
|
| 330 |
target_root = target_dir.resolve()
|
|
|
|
| 331 |
with tarfile.open(archive, "r:gz") as tf:
|
| 332 |
-
|
| 333 |
-
for member in members:
|
| 334 |
_validate_graph_tar_member(member)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
destination = _graph_member_destination(target_dir, target_root, member)
|
| 336 |
if member.isdir():
|
| 337 |
destination.mkdir(parents=True, exist_ok=True)
|
|
@@ -343,6 +406,13 @@ def _extract_graph_archive_to_dir(archive: Path, target_dir: Path) -> None:
|
|
| 343 |
_ensure_path_under_root(destination.parent, target_root)
|
| 344 |
with source, destination.open("wb") as fh:
|
| 345 |
shutil.copyfileobj(source, fh)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
|
| 348 |
def _graph_install_complete(wiki_dir: Path) -> bool:
|
|
@@ -353,6 +423,13 @@ def _graph_install_complete(wiki_dir: Path) -> bool:
|
|
| 353 |
return True
|
| 354 |
|
| 355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
def _validate_graph_install_tree(wiki_dir: Path) -> None:
|
| 357 |
missing = [
|
| 358 |
name
|
|
@@ -362,13 +439,7 @@ def _validate_graph_install_tree(wiki_dir: Path) -> None:
|
|
| 362 |
if missing:
|
| 363 |
raise ValueError(f"graph archive is missing required files: {missing}")
|
| 364 |
|
| 365 |
-
|
| 366 |
-
if not isinstance(graph, dict):
|
| 367 |
-
raise ValueError("graphify-out/graph.json must contain a JSON object")
|
| 368 |
-
if not isinstance(graph.get("nodes"), list):
|
| 369 |
-
raise ValueError("graphify-out/graph.json is missing a nodes list")
|
| 370 |
-
if not isinstance(graph.get("edges", graph.get("links")), list):
|
| 371 |
-
raise ValueError("graphify-out/graph.json is missing an edges/links list")
|
| 372 |
|
| 373 |
manifest = _read_json_file(wiki_dir / "graphify-out" / "graph-export-manifest.json")
|
| 374 |
if not isinstance(manifest, dict):
|
|
@@ -389,6 +460,29 @@ def _validate_graph_install_tree(wiki_dir: Path) -> None:
|
|
| 389 |
raise ValueError("graph export manifest artifacts map is incomplete")
|
| 390 |
|
| 391 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
def _read_json_file(path: Path) -> Any:
|
| 393 |
with path.open("r", encoding="utf-8") as f:
|
| 394 |
return json.load(f)
|
|
@@ -425,6 +519,28 @@ def _graph_member_destination(
|
|
| 425 |
return destination
|
| 426 |
|
| 427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
def _ensure_path_under_root(path: Path, root: Path) -> None:
|
| 429 |
resolved = path.resolve(strict=False)
|
| 430 |
if resolved != root and root not in resolved.parents:
|
|
@@ -1423,6 +1539,15 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 1423 |
"--graph-url",
|
| 1424 |
help="Override the pre-built wiki-graph.tar.gz download URL",
|
| 1425 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1426 |
parser.add_argument(
|
| 1427 |
"--knowledge-mode",
|
| 1428 |
choices=("shipped", "local", "enriched", "skip"),
|
|
@@ -1552,9 +1677,19 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 1552 |
print(" [skip] hook injection (pass --hooks to enable)")
|
| 1553 |
|
| 1554 |
if args.graph:
|
| 1555 |
-
rc = build_graph(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1556 |
if rc == 0:
|
| 1557 |
print(" [ok] knowledge graph installed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1558 |
else:
|
| 1559 |
print(f" [warn] graph install returned {rc}", file=sys.stderr)
|
| 1560 |
if final_rc == 0:
|
|
|
|
| 212 |
|
| 213 |
|
| 214 |
_GRAPH_ARCHIVE_NAME = "wiki-graph.tar.gz"
|
| 215 |
+
_GRAPH_RUNTIME_ARCHIVE_NAME = "wiki-graph-runtime.tar.gz"
|
| 216 |
+
_GRAPH_ARCHIVE_NAMES = {
|
| 217 |
+
"runtime": _GRAPH_RUNTIME_ARCHIVE_NAME,
|
| 218 |
+
"full": _GRAPH_ARCHIVE_NAME,
|
| 219 |
+
}
|
| 220 |
_GRAPH_RELEASE_URL = (
|
| 221 |
"https://github.com/stevesolun/ctx/releases/download/"
|
| 222 |
+
"v{version}/{archive_name}"
|
| 223 |
)
|
| 224 |
_GRAPH_REQUIRED_FILES = frozenset({
|
| 225 |
"index.md",
|
|
|
|
| 237 |
"concepts",
|
| 238 |
"external-catalogs",
|
| 239 |
"index.md",
|
| 240 |
+
"catalog.md",
|
| 241 |
+
"converted-index.md",
|
| 242 |
+
"log.md",
|
| 243 |
+
"SCHEMA.md",
|
| 244 |
+
"versions-catalog.md",
|
| 245 |
+
".obsidian",
|
| 246 |
)
|
| 247 |
+
_GRAPH_JSON_OUTLINE_BYTES = 1024 * 1024
|
| 248 |
+
_GRAPH_INSTALL_MODES = ("runtime", "full")
|
| 249 |
+
_GRAPH_RUNTIME_PREFIXES = ("graphify-out/", "external-catalogs/", "entities/harnesses/")
|
| 250 |
+
_GRAPH_RUNTIME_ROOT_FILES = frozenset({
|
| 251 |
+
"catalog.md",
|
| 252 |
+
"converted-index.md",
|
| 253 |
+
"index.md",
|
| 254 |
+
"log.md",
|
| 255 |
+
"SCHEMA.md",
|
| 256 |
+
"versions-catalog.md",
|
| 257 |
+
})
|
| 258 |
|
| 259 |
|
| 260 |
def build_graph(
|
|
|
|
| 262 |
*,
|
| 263 |
force: bool = False,
|
| 264 |
graph_url: str | None = None,
|
| 265 |
+
install_mode: str = "runtime",
|
| 266 |
) -> int:
|
| 267 |
"""Install the pre-built knowledge graph into ``~/.claude/skill-wiki``."""
|
| 268 |
+
if install_mode not in _GRAPH_INSTALL_MODES:
|
| 269 |
+
raise ValueError(f"unknown graph install mode: {install_mode}")
|
| 270 |
claude_dir = claude or _claude_dir()
|
| 271 |
wiki_dir = claude_dir / "skill-wiki"
|
| 272 |
graph_json = wiki_dir / "graphify-out" / "graph.json"
|
| 273 |
+
install_complete = (
|
| 274 |
+
_graph_full_install_complete(wiki_dir)
|
| 275 |
+
if install_mode == "full"
|
| 276 |
+
else _graph_install_complete(wiki_dir)
|
| 277 |
+
)
|
| 278 |
+
if not force and install_complete:
|
| 279 |
print(f"Graph already installed at {graph_json}; use --force to refresh.")
|
| 280 |
return 0
|
| 281 |
|
| 282 |
temp_dir: tempfile.TemporaryDirectory[str] | None = None
|
| 283 |
+
archive = _find_local_graph_archive(install_mode)
|
| 284 |
try:
|
| 285 |
if archive is None:
|
| 286 |
temp_dir = tempfile.TemporaryDirectory(prefix="ctx-graph-download-")
|
| 287 |
+
archive = Path(temp_dir.name) / _graph_archive_name(install_mode)
|
| 288 |
+
url = graph_url or _release_graph_url(install_mode)
|
| 289 |
print(f"Downloading pre-built graph from {url}")
|
| 290 |
_download_graph_archive(archive, url=url)
|
| 291 |
else:
|
| 292 |
print(f"Installing pre-built graph from {archive}")
|
| 293 |
+
_extract_graph_archive(archive, wiki_dir, install_mode=install_mode)
|
| 294 |
except Exception as exc:
|
| 295 |
print(
|
| 296 |
f" [error] graph install failed: {type(exc).__name__}: {exc}",
|
|
|
|
| 309 |
return 0
|
| 310 |
|
| 311 |
|
| 312 |
+
def _find_local_graph_archive(install_mode: str = "runtime") -> Path | None:
|
| 313 |
module_path = Path(__file__).resolve()
|
| 314 |
+
archive_names = [_graph_archive_name(install_mode)]
|
| 315 |
+
if install_mode == "runtime":
|
| 316 |
+
archive_names.append(_GRAPH_ARCHIVE_NAME)
|
| 317 |
+
graph_dirs = (module_path.parent.parent / "graph", Path.cwd() / "graph")
|
| 318 |
+
candidates = [
|
| 319 |
+
graph_dir / archive_name
|
| 320 |
+
for archive_name in archive_names
|
| 321 |
+
for graph_dir in graph_dirs
|
| 322 |
+
]
|
| 323 |
for candidate in candidates:
|
| 324 |
if candidate.is_file():
|
| 325 |
return candidate
|
| 326 |
return None
|
| 327 |
|
| 328 |
|
| 329 |
+
def _release_graph_url(install_mode: str = "runtime") -> str:
|
| 330 |
+
return _GRAPH_RELEASE_URL.format(
|
| 331 |
+
version=_package_version(),
|
| 332 |
+
archive_name=_graph_archive_name(install_mode),
|
| 333 |
+
)
|
| 334 |
+
|
| 335 |
+
|
| 336 |
+
def _graph_archive_name(install_mode: str) -> str:
|
| 337 |
+
return _GRAPH_ARCHIVE_NAMES.get(install_mode, _GRAPH_RUNTIME_ARCHIVE_NAME)
|
| 338 |
|
| 339 |
|
| 340 |
def _package_version() -> str:
|
|
|
|
| 355 |
shutil.copyfileobj(response, fh)
|
| 356 |
|
| 357 |
|
| 358 |
+
def _extract_graph_archive(
|
| 359 |
+
archive: Path,
|
| 360 |
+
target_dir: Path,
|
| 361 |
+
*,
|
| 362 |
+
install_mode: str,
|
| 363 |
+
) -> None:
|
| 364 |
target_dir.parent.mkdir(parents=True, exist_ok=True)
|
| 365 |
with tempfile.TemporaryDirectory(
|
| 366 |
prefix=f".{target_dir.name}-stage-",
|
| 367 |
dir=target_dir.parent,
|
| 368 |
) as staging_name:
|
| 369 |
staging_dir = Path(staging_name)
|
| 370 |
+
_extract_graph_archive_to_dir(
|
| 371 |
+
archive,
|
| 372 |
+
staging_dir,
|
| 373 |
+
install_mode=install_mode,
|
| 374 |
+
)
|
| 375 |
_validate_graph_install_tree(staging_dir)
|
| 376 |
_promote_graph_tree(staging_dir, target_dir)
|
| 377 |
|
| 378 |
|
| 379 |
+
def _extract_graph_archive_to_dir(
|
| 380 |
+
archive: Path,
|
| 381 |
+
target_dir: Path,
|
| 382 |
+
*,
|
| 383 |
+
install_mode: str,
|
| 384 |
+
) -> None:
|
| 385 |
target_dir.mkdir(parents=True, exist_ok=True)
|
| 386 |
target_root = target_dir.resolve()
|
| 387 |
+
extracted_required: set[str] = set()
|
| 388 |
with tarfile.open(archive, "r:gz") as tf:
|
| 389 |
+
for member in tf:
|
|
|
|
| 390 |
_validate_graph_tar_member(member)
|
| 391 |
+
safe_name = _safe_graph_member_name(member.name)
|
| 392 |
+
if not _should_extract_graph_member(
|
| 393 |
+
safe_name,
|
| 394 |
+
member,
|
| 395 |
+
install_mode=install_mode,
|
| 396 |
+
):
|
| 397 |
+
continue
|
| 398 |
destination = _graph_member_destination(target_dir, target_root, member)
|
| 399 |
if member.isdir():
|
| 400 |
destination.mkdir(parents=True, exist_ok=True)
|
|
|
|
| 406 |
_ensure_path_under_root(destination.parent, target_root)
|
| 407 |
with source, destination.open("wb") as fh:
|
| 408 |
shutil.copyfileobj(source, fh)
|
| 409 |
+
if safe_name in _GRAPH_REQUIRED_FILES:
|
| 410 |
+
extracted_required.add(safe_name)
|
| 411 |
+
if (
|
| 412 |
+
install_mode == "runtime"
|
| 413 |
+
and _GRAPH_REQUIRED_FILES <= extracted_required
|
| 414 |
+
):
|
| 415 |
+
break
|
| 416 |
|
| 417 |
|
| 418 |
def _graph_install_complete(wiki_dir: Path) -> bool:
|
|
|
|
| 423 |
return True
|
| 424 |
|
| 425 |
|
| 426 |
+
def _graph_full_install_complete(wiki_dir: Path) -> bool:
|
| 427 |
+
if not _graph_install_complete(wiki_dir):
|
| 428 |
+
return False
|
| 429 |
+
entities = wiki_dir / "entities"
|
| 430 |
+
return entities.is_dir() and any(entities.iterdir())
|
| 431 |
+
|
| 432 |
+
|
| 433 |
def _validate_graph_install_tree(wiki_dir: Path) -> None:
|
| 434 |
missing = [
|
| 435 |
name
|
|
|
|
| 439 |
if missing:
|
| 440 |
raise ValueError(f"graph archive is missing required files: {missing}")
|
| 441 |
|
| 442 |
+
_validate_graph_json_outline(wiki_dir / "graphify-out" / "graph.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
|
| 444 |
manifest = _read_json_file(wiki_dir / "graphify-out" / "graph-export-manifest.json")
|
| 445 |
if not isinstance(manifest, dict):
|
|
|
|
| 460 |
raise ValueError("graph export manifest artifacts map is incomplete")
|
| 461 |
|
| 462 |
|
| 463 |
+
def _validate_graph_json_outline(path: Path) -> None:
|
| 464 |
+
size = path.stat().st_size
|
| 465 |
+
read_size = min(size, _GRAPH_JSON_OUTLINE_BYTES)
|
| 466 |
+
with path.open("rb") as f:
|
| 467 |
+
head = f.read(read_size)
|
| 468 |
+
if size > read_size:
|
| 469 |
+
f.seek(max(0, size - read_size))
|
| 470 |
+
tail = f.read(read_size)
|
| 471 |
+
else:
|
| 472 |
+
tail = b""
|
| 473 |
+
head_text = head.decode("utf-8", errors="ignore")
|
| 474 |
+
tail_text = tail.decode("utf-8", errors="ignore")
|
| 475 |
+
if not head_text.lstrip().startswith("{"):
|
| 476 |
+
raise ValueError("graphify-out/graph.json must contain a JSON object")
|
| 477 |
+
if tail_text and not tail_text.rstrip().endswith("}"):
|
| 478 |
+
raise ValueError("graphify-out/graph.json appears truncated")
|
| 479 |
+
outline = f"{head_text}\n{tail_text}"
|
| 480 |
+
if '"nodes"' not in outline:
|
| 481 |
+
raise ValueError("graphify-out/graph.json is missing a nodes list")
|
| 482 |
+
if '"edges"' not in outline and '"links"' not in outline:
|
| 483 |
+
raise ValueError("graphify-out/graph.json is missing an edges/links list")
|
| 484 |
+
|
| 485 |
+
|
| 486 |
def _read_json_file(path: Path) -> Any:
|
| 487 |
with path.open("r", encoding="utf-8") as f:
|
| 488 |
return json.load(f)
|
|
|
|
| 519 |
return destination
|
| 520 |
|
| 521 |
|
| 522 |
+
def _safe_graph_member_name(name: str) -> str:
|
| 523 |
+
path = PurePosixPath(name.replace("\\", "/"))
|
| 524 |
+
return path.as_posix()
|
| 525 |
+
|
| 526 |
+
|
| 527 |
+
def _should_extract_graph_member(
|
| 528 |
+
safe_name: str,
|
| 529 |
+
member: tarfile.TarInfo,
|
| 530 |
+
*,
|
| 531 |
+
install_mode: str,
|
| 532 |
+
) -> bool:
|
| 533 |
+
if install_mode == "full":
|
| 534 |
+
return True
|
| 535 |
+
if member.isdir():
|
| 536 |
+
return False
|
| 537 |
+
return (
|
| 538 |
+
safe_name in _GRAPH_RUNTIME_ROOT_FILES
|
| 539 |
+
or safe_name in _GRAPH_REQUIRED_FILES
|
| 540 |
+
or any(safe_name.startswith(prefix) for prefix in _GRAPH_RUNTIME_PREFIXES)
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
+
|
| 544 |
def _ensure_path_under_root(path: Path, root: Path) -> None:
|
| 545 |
resolved = path.resolve(strict=False)
|
| 546 |
if resolved != root and root not in resolved.parents:
|
|
|
|
| 1539 |
"--graph-url",
|
| 1540 |
help="Override the pre-built wiki-graph.tar.gz download URL",
|
| 1541 |
)
|
| 1542 |
+
parser.add_argument(
|
| 1543 |
+
"--graph-install-mode",
|
| 1544 |
+
choices=_GRAPH_INSTALL_MODES,
|
| 1545 |
+
default="runtime",
|
| 1546 |
+
help=(
|
| 1547 |
+
"Graph install shape: runtime extracts graph/catalog artifacts "
|
| 1548 |
+
"needed for recommendations; full expands every wiki markdown file."
|
| 1549 |
+
),
|
| 1550 |
+
)
|
| 1551 |
parser.add_argument(
|
| 1552 |
"--knowledge-mode",
|
| 1553 |
choices=("shipped", "local", "enriched", "skip"),
|
|
|
|
| 1677 |
print(" [skip] hook injection (pass --hooks to enable)")
|
| 1678 |
|
| 1679 |
if args.graph:
|
| 1680 |
+
rc = build_graph(
|
| 1681 |
+
claude,
|
| 1682 |
+
force=args.force,
|
| 1683 |
+
graph_url=args.graph_url,
|
| 1684 |
+
install_mode=args.graph_install_mode,
|
| 1685 |
+
)
|
| 1686 |
if rc == 0:
|
| 1687 |
print(" [ok] knowledge graph installed")
|
| 1688 |
+
if args.graph_install_mode == "runtime":
|
| 1689 |
+
print(
|
| 1690 |
+
" [info] runtime graph install only; pass "
|
| 1691 |
+
"--graph-install-mode full to expand the full wiki"
|
| 1692 |
+
)
|
| 1693 |
else:
|
| 1694 |
print(f" [warn] graph install returned {rc}", file=sys.stderr)
|
| 1695 |
if final_rc == 0:
|
src/ctx_monitor.py
CHANGED
|
@@ -582,6 +582,7 @@ def _artifact_status() -> dict[str, Any]:
|
|
| 582 |
repo_graph_dir / "wiki-graph.tar.gz",
|
| 583 |
),
|
| 584 |
"skills_sh_catalog": _first_existing_file_status(
|
|
|
|
| 585 |
claude_graph_dir / "skills-sh-catalog.json.gz",
|
| 586 |
repo_graph_dir / "skills-sh-catalog.json.gz",
|
| 587 |
),
|
|
|
|
| 582 |
repo_graph_dir / "wiki-graph.tar.gz",
|
| 583 |
),
|
| 584 |
"skills_sh_catalog": _first_existing_file_status(
|
| 585 |
+
wiki / "external-catalogs" / "skills-sh" / "catalog.json",
|
| 586 |
claude_graph_dir / "skills-sh-catalog.json.gz",
|
| 587 |
repo_graph_dir / "skills-sh-catalog.json.gz",
|
| 588 |
),
|