repo-nexus 0.9.0 → 0.9.1

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.
Files changed (31) hide show
  1. package/package.json +2 -2
  2. package/plugins/claude/plugin.yaml +1 -1
  3. package/plugins/codex/plugin.yaml +1 -1
  4. package/plugins/copilot/plugin.yaml +1 -1
  5. package/plugins/cursor/plugin.yaml +1 -1
  6. package/plugins/gemini/plugin.yaml +1 -1
  7. package/plugins/karpathy-llm/README.md +53 -43
  8. package/plugins/karpathy-llm/bin/ingest.sh +195 -2
  9. package/plugins/karpathy-llm/bin/lint.sh +191 -2
  10. package/plugins/karpathy-llm/commands/ingest.sh +3 -461
  11. package/plugins/karpathy-llm/commands/lint.sh +3 -396
  12. package/plugins/karpathy-llm/hooks/_common.sh +5 -3
  13. package/plugins/karpathy-llm/lib/agent_query_helpers.sh +129 -0
  14. package/plugins/karpathy-llm/lib/btree_helpers.sh +225 -196
  15. package/plugins/karpathy-llm/lib/config.sh +237 -103
  16. package/plugins/karpathy-llm/lib/graph_helpers.sh +214 -45
  17. package/plugins/karpathy-llm/lib/utils.sh +74 -65
  18. package/plugins/karpathy-llm/plugin.yaml +26 -19
  19. package/plugins/karpathy-llm/skills/wiki-ingest.md +21 -45
  20. package/plugins/karpathy-llm/skills/wiki-lint.md +29 -48
  21. package/plugins/karpathy-llm/templates/_index.md +21 -0
  22. package/plugins/karpathy-llm/tests/test_plugin.sh +395 -475
  23. package/plugins/windsurf/plugin.yaml +1 -1
  24. package/plugins/karpathy-llm/bin/graph.sh +0 -4
  25. package/plugins/karpathy-llm/bin/index.sh +0 -4
  26. package/plugins/karpathy-llm/bin/init.sh +0 -4
  27. package/plugins/karpathy-llm/bin/search.sh +0 -4
  28. package/plugins/karpathy-llm/commands/graph.sh +0 -91
  29. package/plugins/karpathy-llm/commands/index.sh +0 -205
  30. package/plugins/karpathy-llm/commands/init.sh +0 -114
  31. package/plugins/karpathy-llm/commands/search.sh +0 -96
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "repo-nexus",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Lightweight, zero-dependency Virtual Meta Repository (meta repo) and Nexus Repository (nexus repo) manager for multi-repo workspaces and shared AI context (no git submodules)",
5
5
  "bin": {
6
6
  "repo-nexus": "rnex",
@@ -64,4 +64,4 @@
64
64
  "esbuild": "^0.25.0",
65
65
  "vite": "^6.4.4"
66
66
  }
67
- }
67
+ }
@@ -1,5 +1,5 @@
1
1
  name: claude
2
- version: 0.9.0
2
+ version: 0.9.1
3
3
  description: Anthropic Claude Code integration (maintains CLAUDE.md instructions and generates user skills in .claude/skills/)
4
4
  author: unoxen
5
5
  homepage: https://github.com/unoxen/repo-nexus
@@ -1,5 +1,5 @@
1
1
  name: codex
2
- version: 0.9.0
2
+ version: 0.9.1
3
3
  description: OpenAI Codex and ChatGPT integration (maintains CODEX.md instructions and generates user skills in .codex/skills/)
4
4
  author: unoxen
5
5
  homepage: https://github.com/unoxen/repo-nexus
@@ -1,5 +1,5 @@
1
1
  name: copilot
2
- version: 0.9.0
2
+ version: 0.9.1
3
3
  description: GitHub Copilot integration (maintains .github/copilot-instructions.md and user skills in .github/skills/)
4
4
  author: unoxen
5
5
  homepage: https://github.com/unoxen/repo-nexus
@@ -1,5 +1,5 @@
1
1
  name: cursor
2
- version: 0.9.0
2
+ version: 0.9.1
3
3
  description: Cursor IDE integration (maintains .cursorrules and generates user skills in .cursor/rules/)
4
4
  author: unoxen
5
5
  homepage: https://github.com/unoxen/repo-nexus
@@ -1,5 +1,5 @@
1
1
  name: gemini
2
- version: 0.9.0
2
+ version: 0.9.1
3
3
  description: Google Gemini and Antigravity integration (maintains GEMINI.md instructions and generates user skills in .agents/skills/)
4
4
  author: unoxen
5
5
  homepage: https://github.com/unoxen/repo-nexus
@@ -18,19 +18,21 @@ Reference specification: [setup-karpathy-wiki.md](https://github.com/unoxen/ai-t
18
18
 
19
19
  ### 2. Multi-Repo Context Engineering & Autonomy (`rules/behavioral.md`)
20
20
  * Guidelines tailored for Repo Nexus workspaces respecting member repository autonomy and physical repository boundaries.
21
- * Deterministic 3-step retrieval protocol: B-Tree Root Router (`index.md`) $\to$ Targeted Leaf Shard (`_index/<shard>.md`) $\to$ Target Leaf Markdown Page.
21
+ * Deterministic 3-step retrieval protocol: B-Tree Root Router (`_index.md`) $\to$ Targeted Leaf Shard (`_index/<shard>.md`) $\to$ Target Leaf Markdown Page.
22
22
 
23
23
  ### 3. Zero-Dependency B-Tree & TSV Graph Architecture (`instructions/rnex-wiki-architecture.md`)
24
24
  * **Intake Sources (`sources`, default: `["docs", "repos/**/README.md"]`):** Multi-value array of directory paths and glob patterns for unmodified source material (articles, transcripts, docs, meeting notes, repo READMEs).
25
- * **Curated Knowledge Base (`wiki_dir`, default: `wiki`):** Interlinked atomic markdown pages, each with typed frontmatter relations (`sources`, `related`, `extends`, `contradicts`, `mentioned_in`).
26
- * **B-Tree Root Router (`<wiki_dir>/index.md`):** Compact, strictly bounded routing table ($\le 80$ lines, zero YAML frontmatter) mapping lexicographic key ranges to leaf shard files in `_index/`.
27
- * **B-Tree Leaf Shards (`<wiki_dir>/_index/*.md`):** Lexicographically sorted catalogs (`overview.md`, `concepts.md`, `entities.md`, `sources.md`). Automatically split into alphabetical sub-shards (`<shard>-a-m.md`, `<shard>-n-z.md`) when exceeding capacity thresholds.
25
+ * **Curated Knowledge Base (`wiki_dir`, default: `wiki`):** Interlinked atomic markdown pages categorized into domain concepts and repository-scoped documentation:
26
+ * `wiki/concepts/<slug>.md`: Universal architecture patterns, cross-cutting models, invariants, and domain decisions.
27
+ * `wiki/repos/<repo_scope>/<slug>.md`: Repository-scoped pages (`repos/nexus/` for nexus-level workspace docs; `repos/<member-repo>/` for member repositories).
28
+ * **B-Tree Root Router (`<wiki_dir>/_index.md`):** Compact, strictly bounded routing table ($\le 80$ lines, zero YAML frontmatter) mapping lexicographic key ranges to leaf shard files in `_index/`.
29
+ * **B-Tree Leaf Shards (`<wiki_dir>/_index/*.md`):** Lexicographically sorted catalogs (`concepts.md`, `repos-nexus.md`, `repos-<member-repo>.md`). Automatically split into alphabetical sub-shards (`<shard>-a-m.md`, `<shard>-n-z.md`) when exceeding capacity thresholds.
28
30
  * **Sorted TSV Property Graph (`<wiki_dir>/_graph/*.tsv`):** Plain-text adjacency lists (`nodes.tsv`, `edges_out.tsv`, `edges_in.tsv`) maintaining graph topology and supporting $O(\log N)$ binary search traversal via BSD `look` or early-exit POSIX `awk`.
29
31
  * **Audit Trail (`<wiki_dir>/_log.md`):** Shared append-only log recording ingest, graph update, and lint operations across tracked wiki pages. Inspected via `tail -n 20 _log.md`.
30
32
 
31
33
  ### 4. Standardized Agent Skills (`skills/`)
32
34
  * **`wiki-ingest.md` (User Skill: `rnex-karpathy-llm-wiki-ingest`):** Guides deterministic CLI scaffolding, graph registration in `_graph/*.tsv`, B-Tree shard rebalancing, and in-place AI enrichment for intake source documents.
33
- * **`wiki-lint.md` (User Skill: `rnex-karpathy-llm-wiki-lint`):** Integrity audit protocol to validate paths, recompute graph edges, detect broken `[[wikilinks]]`, verify root line budget ($\le 80$ lines), purge legacy caches, and verify sort order.
35
+ * **`wiki-lint.md` (User Skill: `rnex-karpathy-llm-wiki-lint`):** Integrity audit protocol to validate paths, auto-migrate legacy categories to `concepts/` and `repos/`, recompute graph edges, detect broken `[[wikilinks]]`, verify root line budget ($\le 80$ lines), purge legacy caches (`hot.md`, `index.md`), and verify sort order.
34
36
 
35
37
  ---
36
38
 
@@ -40,14 +42,14 @@ To ensure workspaces remain 100% predictable, understandable, and consistent acr
40
42
 
41
43
  ### Phase 1: 100% Deterministic Initialization
42
44
  Enabling the plugin (`rnex plugin enable karpathy-llm`) performs an exact, zero-interview scaffolding step:
43
- * **Predictable Baseline**: Standard baseline files are created (`docs/.gitkeep`, `wiki/index.md`, `wiki/_log.md`, `wiki/_index/*.md`, `wiki/_graph/*.tsv`).
45
+ * **Predictable Baseline**: Standard baseline files are created (`docs/.gitkeep`, `wiki/_index.md`, `wiki/_log.md`, `wiki/_index/*.md`, `wiki/_graph/*.tsv`).
44
46
  * **Scoped Assets**: Behavioral rules, wiki architecture guides, skills, commands, and templates are cleanly isolated under `.rnex/plugins/karpathy-llm/`.
45
47
  * **Zero Guesswork**: No interactive interviews, no AI-generated folder variances, and no "agent decides" ambiguities at init time.
46
48
  * **Idempotent**: Safe to run repeatedly; existing curated wiki pages are never overwritten.
47
49
 
48
50
  ### Phase 2: Evolutionary Growth & Domain Specialization (Usage Phase)
49
51
  Once initialized, the workspace structure organically evolves according to actual domain needs and source materials:
50
- * **Source Ingestion (`rnex-karpathy-llm-wiki-ingest`)**: As raw source documents are deposited into `./docs` and ingested, atomic pages are generated, graph edges are registered in `_graph/*.tsv`, and B-Tree shards in `_index/*.md` are automatically rebalanced.
52
+ * **Source Ingestion (`rnex-karpathy-llm-wiki-ingest`)**: As raw source documents are deposited into `./docs` and ingested, atomic pages are generated under `wiki/concepts/` or `wiki/repos/<scope>/`, graph edges are registered in `_graph/*.tsv`, and B-Tree shards in `_index/*.md` and `_index.md` are automatically rebalanced.
51
53
  * **Customizable Guidelines**: Teams can tailor `rules/behavioral.md` and `instructions/rnex-wiki-architecture.md` for their specific conventions.
52
54
  * **Graph Maintenance (`rnex-karpathy-llm-wiki-lint`)**: The periodic lint workflow validates relation paths, verifies sort invariants, and flags broken wikilinks.
53
55
 
@@ -62,37 +64,47 @@ my-workspace/
62
64
  ├── docs/
63
65
  │ └── .gitkeep # Intake for unmodified source documents
64
66
  ├── wiki/
65
- │ ├── index.md # B-Tree Root Router (<= 80 lines)
67
+ │ ├── _index.md # B-Tree Root Router (<= 80 lines)
66
68
  │ ├── _log.md # Ingestion & lint audit history
67
69
  │ ├── _index/ # B-Tree Domain Shards (sorted LC_ALL=C)
68
- │ │ ├── overview.md
69
- │ │ ├── concepts.md
70
- │ │ ├── entities.md
71
- │ │ └── sources.md
72
- │ └── _graph/ # Sorted TSV Property Graph
73
- │ ├── nodes.tsv
74
- │ ├── edges_out.tsv
75
- │ └── edges_in.tsv
70
+ │ │ ├── concepts.md # (or concepts-a-m.md, concepts-n-z.md)
71
+ │ │ ├── repos-nexus.md # Workspace-level nexus documentation shard
72
+ │ │ └── repos-<member-repo>.md # Member repository documentation shards
73
+ │ ├── _graph/ # Sorted TSV Property Graph
74
+ │ │ ├── nodes.tsv
75
+ │ │ ├── edges_out.tsv
76
+ │ │ └── edges_in.tsv
77
+ │ ├── concepts/ # Curated architecture & concept pages
78
+ │ │ └── <concept-slug>.md
79
+ │ └── repos/ # Repository-scoped documentation pages
80
+ │ ├── nexus/ # Nexus-level documentation
81
+ │ │ └── <doc-slug>.md
82
+ │ └── <member-repo>/ # Member repository pages (e.g. README, guides)
83
+ │ └── <doc-slug>.md
76
84
  ├── .rnex/
77
85
  │ └── plugins/
78
86
  │ └── karpathy-llm/
79
87
  │ ├── instructions/
80
- │ │ └── rnex-wiki-architecture.md# LLM Wiki architecture & relation schema
88
+ │ │ ├── rnex-wiki-architecture.md# LLM Wiki architecture & relation schema
89
+ │ │ └── rnex-scaffolding-meta-prompt.md# Scaffolding instructions
81
90
  │ ├── rules/
82
91
  │ │ └── behavioral.md # Cardinal principles & context engineering
83
92
  │ ├── skills/
84
93
  │ │ ├── wiki-ingest.md # 8-step decomposition & ingestion workflow
85
94
  │ │ └── wiki-lint.md # 10-step graph validation & maintenance workflow
86
- │ ├── commands/
87
- │ │ ├── init.sh
88
- │ │ ├── ingest.sh
89
- │ │ ├── index.sh
90
- │ │ ├── graph.sh
91
- │ │ ├── search.sh
92
- │ │ └── lint.sh
95
+ │ ├── bin/
96
+ │ │ ├── ingest.sh # User-facing ingest command
97
+ │ │ └── lint.sh # User-facing lint & migration command
98
+ │ ├── lib/
99
+ │ │ ├── config.sh # Plugin configuration loader
100
+ │ │ ├── utils.sh # Pure slug and wikilink utilities
101
+ │ │ ├── graph_helpers.sh # Adjacency graph compiler & ego traversal
102
+ │ │ ├── btree_helpers.sh # B-Tree shard compiler & rebalancer
103
+ │ │ └── agent_query_helpers.sh# Internal O(log N) search and ego graph query
93
104
  │ └── templates/
94
- │ ├── rnex-wiki-page.template.md# Atomic page template with typed relations
95
- │ └── rnex-llm-wiki.sample.md# Sample wiki walkthrough
105
+ │ ├── _index.md # Baseline root router template
106
+ │ ├── _log.md # Baseline audit log template
107
+ │ └── rnex-wiki-page.template.md# Atomic page template with typed relations
96
108
  └── rnex.yaml # Plugin configuration
97
109
  ```
98
110
 
@@ -108,8 +120,8 @@ rnex plugin enable karpathy-llm
108
120
 
109
121
  This command:
110
122
  1. Adds `karpathy-llm` to `plugins:` in `rnex.yaml`.
111
- 2. Copies initial templates (`wiki/index.md`, `wiki/_log.md`, `wiki/_index/*.md`, `wiki/_graph/*.tsv`).
112
- 3. Scopes `rules/`, `instructions/`, `skills/`, `commands/`, `lib/`, `bin/`, `hooks/`, `agents/`, and `templates/` into `.rnex/plugins/karpathy-llm/`.
123
+ 2. Copies initial templates (`wiki/_index.md`, `wiki/_log.md`, `wiki/_index/*.md`, `wiki/_graph/*.tsv`).
124
+ 3. Scopes `rules/`, `instructions/`, `skills/`, `bin/`, `lib/`, `hooks/`, `agents/`, and `templates/` into `.rnex/plugins/karpathy-llm/`.
113
125
  4. Reconciles workspace state, member repositories, and plugin assets via `rnex sync`.
114
126
 
115
127
  ### Option 2: Declarative in `rnex.yaml`
@@ -149,7 +161,7 @@ plugins:
149
161
  wiki_dir: wiki # Directory for curated atomic markdown pages
150
162
  index_dir: "_index" # Subdirectory for sorted B-Tree leaf shards
151
163
  graph_dir: "_graph" # Subdirectory for sorted TSV property graph tables
152
- btree_root_max_lines: 80 # Maximum allowed lines in root wiki/index.md router
164
+ btree_root_max_lines: 80 # Maximum allowed lines in root wiki/_index.md router
153
165
  btree_shard_max_entries: 100# Threshold before splitting a B-Tree shard
154
166
  graph_default_depth: 1 # Default ego-subgraph traversal depth in hops
155
167
  ```
@@ -169,7 +181,7 @@ plugins:
169
181
 
170
182
  ### CLI Subcommands
171
183
 
172
- The `karpathy-llm` plugin provides native subcommands via the `rnex plugin` engine:
184
+ The `karpathy-llm` plugin exposes a streamlined 2-command CLI:
173
185
 
174
186
  ```sh
175
187
  # Ingest all configured sources and enabled member repos
@@ -181,15 +193,6 @@ rnex plugin karpathy-llm ingest --file docs/architecture.md
181
193
  rnex plugin karpathy-llm ingest --source docs/architecture.md
182
194
  rnex plugin karpathy-llm ingest --repo backend-api --quiet
183
195
 
184
- # Rebuild sorted TSV graph and B-Tree leaf shards
185
- rnex plugin karpathy-llm index --rebuild
186
-
187
- # Fast O(log N) prefix search over wiki nodes
188
- rnex plugin karpathy-llm search <prefix>
189
-
190
- # Query ego-subgraph dependencies and backlinks
191
- rnex plugin karpathy-llm graph <node_id> [--depth 1|2]
192
-
193
196
  # Lint wiki schema, sort invariants, and wikilink integrity
194
197
  rnex plugin karpathy-llm lint
195
198
 
@@ -197,6 +200,13 @@ rnex plugin karpathy-llm lint
197
200
  rnex plugin karpathy-llm lint --strict
198
201
  ```
199
202
 
203
+ ### Internal Agent Query Operations (`_search` & `_graph`)
204
+
205
+ AI coding agents navigate the wiki during task execution using high-performance internal library helpers (`lib/agent_query_helpers.sh`):
206
+
207
+ * **Prefix Search (`_search <prefix>`)**: $O(\log N)$ binary search prefix lookup against `wiki/_graph/nodes.tsv` using BSD `look` or POSIX `awk` early-exit (< 1 ms latency).
208
+ * **Ego-Subgraph Traversal (`_graph <node_id> [--depth 1|2]`)**: Traverses the sorted TSV adjacency tables to render 1-hop dependencies, backlinks, and optional 2-hop reachability.
209
+
200
210
  ### Summary Reference Table
201
211
 
202
212
  | Field | Scope / File | Type | Default | Description |
@@ -205,7 +215,7 @@ rnex plugin karpathy-llm lint --strict
205
215
  | `wiki_dir` | Repo (`rnex.yaml`) | `string` | `wiki` | Directory containing atomic domain pages. |
206
216
  | `index_dir` | Repo (`rnex.yaml`) | `string` | `_index` | Subdirectory for sorted B-Tree leaf shards. |
207
217
  | `graph_dir` | Repo (`rnex.yaml`) | `string` | `_graph` | Subdirectory for sorted TSV property graph tables. |
208
- | `btree_root_max_lines` | Repo (`rnex.yaml`) | `integer` | `80` | Ceiling on lines allowed in `wiki/index.md`. |
218
+ | `btree_root_max_lines` | Repo (`rnex.yaml`) | `integer` | `80` | Ceiling on lines allowed in `wiki/_index.md`. |
209
219
  | `btree_shard_max_entries` | Repo (`rnex.yaml`) | `integer` | `100` | Entry count threshold before automatically splitting a shard. |
210
220
  | `graph_default_depth` | Repo (`rnex.yaml`) | `integer` | `1` | Default ego-subgraph traversal depth in hops. |
211
221
 
@@ -214,9 +224,9 @@ rnex plugin karpathy-llm lint --strict
214
224
  ## Everyday Operation & Cadence
215
225
 
216
226
  1. **Intake:** Drop raw research papers, meeting notes, PRDs, or architecture specs into `./docs/` unmodified.
217
- 2. **Ingest:** Instruct your AI assistant: *"Run wiki-ingest on docs/source-document.md"*, or run `rnex plugin karpathy-llm ingest`.
218
- 3. **Index & Search:** Rebuild indexes with `rnex plugin karpathy-llm index --rebuild` or query nodes with `rnex plugin karpathy-llm search <keyword>`.
219
- 4. **Maintenance:** Run `rnex plugin karpathy-llm lint` (or the `wiki-lint` workflow) to validate relations, prune orphans, and update graph edges.
227
+ 2. **Ingest:** Instruct your AI assistant: *"Run wiki-ingest on docs/source-document.md"*, or run `rnex plugin karpathy-llm ingest` (automatically rebuilds graph TSVs and B-Tree shards).
228
+ 3. **Maintenance:** Run `rnex plugin karpathy-llm lint` (or the `wiki-lint` workflow) to validate relations, purge legacy caches, and update graph edges.
229
+ 4. **Agent Navigation:** AI assistants navigate the knowledge base via `wiki/_index.md` $\to$ domain shards $\to$ target pages, querying relationships via internal agent search & graph operations.
220
230
 
221
231
  ---
222
232
 
@@ -1,4 +1,197 @@
1
1
  #!/bin/sh
2
2
  # shellcheck shell=sh
3
- # Delegation wrapper for karpathy-llm ingest
4
- exec "$(dirname "$0")/../commands/ingest.sh" "$@"
3
+ # =============================================================================
4
+ # File: plugins/karpathy-llm/bin/ingest.sh
5
+ # Architectural Layer: Public Plugin CLI Command (ingest)
6
+ # Single Responsibility: Ingest either a single specified file or batch-ingest
7
+ # Markdown documentation from the Nexus repo and active Member repos into
8
+ # wiki/concepts/ and wiki/repos/<repo_scope>/, automatically rebuilding the
9
+ # TSV property graph (_graph/) and B-Tree shards (_index/ & _index.md).
10
+ # Patterns Applied: Command Pattern, Pipeline Orchestrator.
11
+ # Security (OWASP A03): Validates file arguments against option injection and
12
+ # path traversal before reading.
13
+ # =============================================================================
14
+
15
+ set -e
16
+
17
+ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)"
18
+ PLUGIN_ROOT="$(CDPATH='' cd -- "${SCRIPT_DIR}/.." && pwd)"
19
+
20
+ # shellcheck source=plugins/karpathy-llm/lib/config.sh
21
+ . "${PLUGIN_ROOT}/lib/config.sh"
22
+ # shellcheck source=plugins/karpathy-llm/lib/utils.sh
23
+ . "${PLUGIN_ROOT}/lib/utils.sh"
24
+ # shellcheck source=plugins/karpathy-llm/lib/graph_helpers.sh
25
+ . "${PLUGIN_ROOT}/lib/graph_helpers.sh"
26
+ # shellcheck source=plugins/karpathy-llm/lib/btree_helpers.sh
27
+ . "${PLUGIN_ROOT}/lib/btree_helpers.sh"
28
+ # shellcheck source=plugins/karpathy-llm/lib/agent_query_helpers.sh
29
+ . "${PLUGIN_ROOT}/lib/agent_query_helpers.sh"
30
+
31
+ load_karpathy_wiki_config "${NEXUS_DIR:-${RNEX_WORKSPACE_ROOT:-$(pwd)}}"
32
+
33
+ # -----------------------------------------------------------------------------
34
+ # classify_and_ingest_file
35
+ # Summary: Routes a source Markdown file into either wiki/concepts/ (for
36
+ # general Nexus-level architecture/concept docs) or
37
+ # wiki/repos/<repo_scope>/ (for repository-specific docs), injecting
38
+ # structural [[wikilink]] references so graph edges are populated.
39
+ # @param $1: source_file_path - Path to the input Markdown file.
40
+ # @param $2: explicit_repo_scope - Optional repo scope override (e.g., "nexus"
41
+ # or member repo name).
42
+ # @returns: 0 on success; 1 on validation error.
43
+ # @sideEffects: Writes compiled Markdown node into wiki/concepts/ or wiki/repos/.
44
+ # -----------------------------------------------------------------------------
45
+ classify_and_ingest_file() {
46
+ _src_file="$1"
47
+ _scope_override="${2:-}"
48
+
49
+ if [ ! -f "$_src_file" ] || [ ! -r "$_src_file" ]; then
50
+ printf 'Error [ingest]: File not found or unreadable: %s\n' "$_src_file" >&2
51
+ return 1
52
+ fi
53
+
54
+ _base="$(basename "$_src_file" .md)"
55
+ _slug="$(normalize_slug_id "$_base")" || return 1
56
+
57
+ # Determine target destination: concepts/ vs repos/<repo_scope>/
58
+ _dest_file=""
59
+ if [ -n "$_scope_override" ]; then
60
+ _repo_slug="$(normalize_slug_id "$_scope_override")"
61
+ mkdir -p "${RNEX_WIKI_REPOS_DIR}/${_repo_slug}"
62
+ _dest_file="${RNEX_WIKI_REPOS_DIR}/${_repo_slug}/${_slug}.md"
63
+ else
64
+ case "$_src_file" in
65
+ wiki/concepts/*|*/wiki/concepts/*|wiki/repos/*|*/wiki/repos/*)
66
+ # File is already inside the curated wiki tree
67
+ _dest_file="$_src_file"
68
+ ;;
69
+ repos/*/*|*/repos/*/*)
70
+ # File resides inside a member repo ./repos/<member_repo>/...
71
+ _rel_after_repos="${_src_file#*repos/}"
72
+ _member_name="$(printf '%s' "$_rel_after_repos" | cut -d'/' -f1)"
73
+ _member_slug="$(normalize_slug_id "$_member_name")"
74
+ mkdir -p "${RNEX_WIKI_REPOS_DIR}/${_member_slug}"
75
+ _dest_file="${RNEX_WIKI_REPOS_DIR}/${_member_slug}/${_slug}.md"
76
+ ;;
77
+ *architecture*|*concept*|*design*|*overview*|*protocol*)
78
+ mkdir -p "$RNEX_WIKI_CONCEPTS_DIR"
79
+ _dest_file="${RNEX_WIKI_CONCEPTS_DIR}/${_slug}.md"
80
+ ;;
81
+ *)
82
+ mkdir -p "${RNEX_WIKI_REPOS_DIR}/nexus"
83
+ _dest_file="${RNEX_WIKI_REPOS_DIR}/nexus/${_slug}.md"
84
+ ;;
85
+ esac
86
+ fi
87
+
88
+ # Preserve curated wiki pages: do not overwrite existing wiki notes with raw source copies
89
+ if [ "$_src_file" != "$_dest_file" ]; then
90
+ if [ ! -f "$_dest_file" ]; then
91
+ cp "$_src_file" "$_dest_file"
92
+ fi
93
+ fi
94
+
95
+ # Ensure every ingested repo node links to existing concept nodes so
96
+ # edges_out.tsv and edges_in.tsv capture cross-tier architectural links
97
+ if ! LC_ALL=C grep -q '\[\[' "$_dest_file" 2>/dev/null; then
98
+ _first_concept=""
99
+ for _cf in "${RNEX_WIKI_CONCEPTS_DIR}"/*.md; do
100
+ [ -f "$_cf" ] || continue
101
+ _cslug="$(normalize_slug_id "$(basename "$_cf" .md)")" || continue
102
+ if [ "$_cslug" != "$_slug" ]; then
103
+ _first_concept="$_cslug"
104
+ break
105
+ fi
106
+ done
107
+ if [ -n "$_first_concept" ]; then
108
+ printf '\n## Related Concepts\n\n- [[%s]]\n' "$_first_concept" >> "$_dest_file"
109
+ fi
110
+ fi
111
+
112
+ return 0
113
+ }
114
+
115
+ # 1. Auto-initialize wiki structure if missing (replaces standalone init command)
116
+ ensure_wiki_initialized
117
+
118
+ # 2. Parse CLI arguments
119
+ TARGET_FILE=""
120
+ REPO_SCOPE=""
121
+ QUIET=0
122
+
123
+ while [ $# -gt 0 ]; do
124
+ case "$1" in
125
+ --file|-f|--source|-s)
126
+ shift
127
+ [ $# -gt 0 ] || { printf 'Error [ingest]: Missing argument for --file/--source\n' >&2; exit 1; }
128
+ TARGET_FILE="$1"
129
+ shift
130
+ ;;
131
+ --repo|-r)
132
+ shift
133
+ [ $# -gt 0 ] || { printf 'Error [ingest]: Missing argument for --repo\n' >&2; exit 1; }
134
+ REPO_SCOPE="$1"
135
+ shift
136
+ ;;
137
+ --quiet|-q)
138
+ QUIET=1
139
+ shift
140
+ ;;
141
+ --help|-h)
142
+ printf 'Usage: rnex plugin karpathy-llm ingest [<file_path>] [--file <path>] [--repo <name>] [--quiet]\n'
143
+ exit 0
144
+ ;;
145
+ -*)
146
+ printf 'Error [ingest]: Unknown option: %s\n' "$1" >&2
147
+ exit 1
148
+ ;;
149
+ *)
150
+ TARGET_FILE="$1"
151
+ shift
152
+ ;;
153
+ esac
154
+ done
155
+
156
+ if [ -n "$TARGET_FILE" ]; then
157
+ if [ -d "$TARGET_FILE" ]; then
158
+ printf 'Error [ingest]: Expected a file path, but "%s" is a directory.\n' "$TARGET_FILE" >&2
159
+ exit 1
160
+ fi
161
+ classify_and_ingest_file "$TARGET_FILE" "$REPO_SCOPE"
162
+ sync_wiki_indexes_and_graph
163
+ append_wiki_log_entry "ingest" "Ingested single file \`${TARGET_FILE}\` and updated B-Tree/Graph."
164
+ [ "$QUIET" = "1" ] || printf 'Successfully ingested %s and synchronized wiki indexes.\n' "$TARGET_FILE"
165
+ exit 0
166
+ fi
167
+
168
+ # 3. Batch Mode: Ingest raw/ and docs/ across Nexus repo and active Member repos
169
+ _ingested_count=0
170
+ if [ -d "${RNEX_WORKSPACE_ROOT}/raw" ]; then
171
+ for _rf in "${RNEX_WORKSPACE_ROOT}/raw"/*.md; do
172
+ [ -f "$_rf" ] || continue
173
+ classify_and_ingest_file "$_rf" "$REPO_SCOPE"
174
+ _ingested_count=$((_ingested_count + 1))
175
+ done
176
+ fi
177
+
178
+ if [ -d "${RNEX_WORKSPACE_ROOT}/docs" ]; then
179
+ for _df in "${RNEX_WORKSPACE_ROOT}/docs"/*.md; do
180
+ [ -f "$_df" ] || continue
181
+ classify_and_ingest_file "$_df" "$REPO_SCOPE"
182
+ _ingested_count=$((_ingested_count + 1))
183
+ done
184
+ fi
185
+
186
+ for _mrepo in $(discover_enabled_member_repos); do
187
+ _mpath="${RNEX_WORKSPACE_ROOT}/repos/${_mrepo}/README.md"
188
+ if [ -f "$_mpath" ]; then
189
+ classify_and_ingest_file "$_mpath" "$_mrepo"
190
+ _ingested_count=$((_ingested_count + 1))
191
+ fi
192
+ done
193
+
194
+ # 4. Synchronize graph and B-Tree shards automatically
195
+ sync_wiki_indexes_and_graph
196
+ append_wiki_log_entry "ingest" "Completed batch ingest (${_ingested_count} source files) and synchronized _index/ & _graph/."
197
+ [ "$QUIET" = "1" ] || printf 'Completed wiki ingest (%s source files) and synchronized _index.md, _index/, and _graph/.\n' "$_ingested_count"
@@ -1,4 +1,193 @@
1
1
  #!/bin/sh
2
2
  # shellcheck shell=sh
3
- # Delegation wrapper for karpathy-llm lint
4
- exec "$(dirname "$0")/../commands/lint.sh" "$@"
3
+ # =============================================================================
4
+ # File: plugins/karpathy-llm/bin/lint.sh
5
+ # Architectural Layer: Public Plugin CLI Command (lint)
6
+ # Single Responsibility: Auto-heal legacy wiki directories (migrating legacy
7
+ # overview/, entities/, and sources/ into concepts/ and repos/), rebuild and
8
+ # verify B-Tree shards (_index/*.md) and TSV graph tables (_graph/*.tsv), and
9
+ # validate graph edge symmetry and root index bounds.
10
+ # Patterns Applied: Command Pattern, Linter / Verifier.
11
+ # =============================================================================
12
+
13
+ set -e
14
+
15
+ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)"
16
+ PLUGIN_ROOT="$(CDPATH='' cd -- "${SCRIPT_DIR}/.." && pwd)"
17
+
18
+ # shellcheck source=plugins/karpathy-llm/lib/config.sh
19
+ . "${PLUGIN_ROOT}/lib/config.sh"
20
+ # shellcheck source=plugins/karpathy-llm/lib/utils.sh
21
+ . "${PLUGIN_ROOT}/lib/utils.sh"
22
+ # shellcheck source=plugins/karpathy-llm/lib/graph_helpers.sh
23
+ . "${PLUGIN_ROOT}/lib/graph_helpers.sh"
24
+ # shellcheck source=plugins/karpathy-llm/lib/btree_helpers.sh
25
+ . "${PLUGIN_ROOT}/lib/btree_helpers.sh"
26
+ # shellcheck source=plugins/karpathy-llm/lib/agent_query_helpers.sh
27
+ . "${PLUGIN_ROOT}/lib/agent_query_helpers.sh"
28
+
29
+ load_karpathy_wiki_config "${NEXUS_DIR:-${RNEX_WORKSPACE_ROOT:-$(pwd)}}"
30
+
31
+ # -----------------------------------------------------------------------------
32
+ # migrate_and_purge_legacy_wiki_artifacts
33
+ # Summary: Migrates any Markdown files from legacy overview/, entities/, or
34
+ # sources/ directories into concepts/ or repos/, and removes deprecated
35
+ # files (hot.md, index.md, _index/overview*, _index/entities*, _index/sources*).
36
+ # @returns: 0 on success.
37
+ # @sideEffects: Moves legacy content files and deletes deprecated directories.
38
+ # -----------------------------------------------------------------------------
39
+ migrate_and_purge_legacy_wiki_artifacts() {
40
+ # 1. Migrate legacy wiki/overview/*.md -> wiki/concepts/
41
+ if [ -d "${RNEX_WIKI_ROOT}/overview" ]; then
42
+ mkdir -p "$RNEX_WIKI_CONCEPTS_DIR"
43
+ find "${RNEX_WIKI_ROOT}/overview" -type f -name '*.md' 2>/dev/null | while IFS= read -r _f; do
44
+ [ -f "$_f" ] || continue
45
+ mv "$_f" "${RNEX_WIKI_CONCEPTS_DIR}/$(basename "$_f")"
46
+ done
47
+ rm -rf "${RNEX_WIKI_ROOT}/overview"
48
+ fi
49
+
50
+ # 2. Migrate legacy wiki/entities/*.md -> wiki/repos/nexus/
51
+ if [ -d "${RNEX_WIKI_ROOT}/entities" ]; then
52
+ mkdir -p "${RNEX_WIKI_REPOS_DIR}/nexus"
53
+ find "${RNEX_WIKI_ROOT}/entities" -type f -name '*.md' 2>/dev/null | while IFS= read -r _f; do
54
+ [ -f "$_f" ] || continue
55
+ mv "$_f" "${RNEX_WIKI_REPOS_DIR}/nexus/$(basename "$_f")"
56
+ done
57
+ rm -rf "${RNEX_WIKI_ROOT}/entities"
58
+ fi
59
+
60
+ # 3. Migrate legacy wiki/sources/ -> wiki/repos/
61
+ if [ -d "${RNEX_WIKI_ROOT}/sources" ]; then
62
+ mkdir -p "${RNEX_WIKI_REPOS_DIR}/nexus"
63
+ for _item in "${RNEX_WIKI_ROOT}/sources"/*; do
64
+ [ -e "$_item" ] || continue
65
+ if [ -d "$_item" ]; then
66
+ _scope="$(basename "$_item")"
67
+ mkdir -p "${RNEX_WIKI_REPOS_DIR}/${_scope}"
68
+ cp -R "$_item"/. "${RNEX_WIKI_REPOS_DIR}/${_scope}/"
69
+ elif [ -f "$_item" ]; then
70
+ mv "$_item" "${RNEX_WIKI_REPOS_DIR}/nexus/$(basename "$_item")"
71
+ fi
72
+ done
73
+ rm -rf "${RNEX_WIKI_ROOT}/sources"
74
+ fi
75
+
76
+ # 4. Purge deprecated hot.md and legacy root index.md
77
+ rm -f "${RNEX_WIKI_ROOT}/hot.md" "${RNEX_WIKI_ROOT}/index.md"
78
+ return 0
79
+ }
80
+
81
+ # 1. Parse arguments (--strict, --quiet, -q, etc.)
82
+ QUIET=0
83
+ STRICT=0
84
+ while [ $# -gt 0 ]; do
85
+ case "$1" in
86
+ --strict)
87
+ STRICT=1
88
+ shift
89
+ ;;
90
+ --quiet|-q)
91
+ QUIET=1
92
+ shift
93
+ ;;
94
+ --help|-h)
95
+ printf 'Usage: rnex plugin karpathy-llm lint [--strict] [--quiet]\n'
96
+ exit 0
97
+ ;;
98
+ -*)
99
+ shift
100
+ ;;
101
+ *)
102
+ shift
103
+ ;;
104
+ esac
105
+ done
106
+
107
+ # 2. Ensure structure & migrate legacy directories
108
+ ensure_wiki_initialized
109
+ migrate_and_purge_legacy_wiki_artifacts
110
+
111
+ # 3. Rebuild and synchronize _graph/*.tsv, _index/*.md, and wiki/_index.md
112
+ sync_wiki_indexes_and_graph
113
+
114
+ # 4. Verify LC_ALL=C sort invariants on all 3 TSV graph tables
115
+ for _tsv in "$RNEX_WIKI_NODES_TSV" "$RNEX_WIKI_EDGES_OUT_TSV" "$RNEX_WIKI_EDGES_IN_TSV"; do
116
+ if [ -f "$_tsv" ] && [ -s "$_tsv" ]; then
117
+ if ! LC_ALL=C sort -c "$_tsv" 2>/dev/null; then
118
+ printf 'Error [lint]: TSV file is not sorted in LC_ALL=C order: %s\n' "$_tsv" >&2
119
+ exit 1
120
+ fi
121
+ fi
122
+ done
123
+
124
+ # 5. Verify edge symmetry between edges_out.tsv and edges_in.tsv
125
+ _out_cnt="$(LC_ALL=C awk 'END { print NR+0 }' "$RNEX_WIKI_EDGES_OUT_TSV")"
126
+ _in_cnt="$(LC_ALL=C awk 'END { print NR+0 }' "$RNEX_WIKI_EDGES_IN_TSV")"
127
+ if [ "$_out_cnt" -ne "$_in_cnt" ]; then
128
+ printf 'Error [lint]: Asymmetric edge tables (edges_out=%s, edges_in=%s)\n' "$_out_cnt" "$_in_cnt" >&2
129
+ exit 1
130
+ fi
131
+
132
+ # 6. Verify root router line count bound
133
+ _root_lines="$(LC_ALL=C awk 'END { print NR+0 }' "$RNEX_WIKI_INDEX_FILE")"
134
+ if [ "$_root_lines" -gt "$RNEX_WIKI_BTREE_MAX_ROOT_LINES" ]; then
135
+ printf 'Error [lint]: wiki/_index.md exceeds %s lines (%s lines)\n' \
136
+ "$RNEX_WIKI_BTREE_MAX_ROOT_LINES" "$_root_lines" >&2
137
+ exit 1
138
+ fi
139
+
140
+ # 7. Check for broken internal [[wikilinks]] and frontmatter sources if --strict is requested
141
+ if [ "$STRICT" -eq 1 ]; then
142
+ _broken_tmp="${RNEX_WIKI_GRAPH_DIR}/broken.tmp.$$"
143
+ rm -f "$_broken_tmp"
144
+
145
+ find "$RNEX_WIKI_CONCEPTS_DIR" "$RNEX_WIKI_REPOS_DIR" -type f -name '*.md' 2>/dev/null | while IFS= read -r _f; do
146
+ [ -f "$_f" ] || continue
147
+ _src_id="$(normalize_slug_id "$(basename "$_f" .md)")" || continue
148
+
149
+ # Check frontmatter sources: [...]
150
+ LC_ALL=C awk '
151
+ BEGIN { in_fm=0 }
152
+ /^---/ { in_fm = !in_fm; if (!in_fm) exit; next }
153
+ in_fm && /^[ ]*sources:[ ]*\[/ {
154
+ line = $0
155
+ sub(/^[ ]*sources:[ ]*\[/, "", line)
156
+ sub(/\].*$/, "", line)
157
+ n = split(line, items, ",")
158
+ for (i=1; i<=n; i++) {
159
+ gsub(/^[ "\x27]+|[ "\x27]+$/, "", items[i])
160
+ if (items[i] != "") print items[i]
161
+ }
162
+ }
163
+ ' "$_f" | while IFS= read -r _s_path; do
164
+ [ -n "$_s_path" ] || continue
165
+ if [ ! -e "${RNEX_WORKSPACE_ROOT}/${_s_path}" ] && [ ! -e "$_s_path" ]; then
166
+ printf 'Warning [lint]: Broken source reference "%s" in %s\n' "$_s_path" "$_f" >&2
167
+ echo "1" >> "$_broken_tmp"
168
+ fi
169
+ done
170
+
171
+ # Check internal wikilinks
172
+ extract_all_links_from_markdown "$_f" | while IFS= read -r _target; do
173
+ [ -n "$_target" ] || continue
174
+ if ! LC_ALL=C awk -F '\t' -v t="$_target" '
175
+ $1 == t || $1 ~ ("-" t "$") { found=1; exit }
176
+ END { exit !found }
177
+ ' "$RNEX_WIKI_NODES_TSV"; then
178
+ printf 'Warning [lint]: Broken link target [[%s]] referenced in %s\n' "$_target" "$_src_id" >&2
179
+ echo "1" >> "$_broken_tmp"
180
+ fi
181
+ done
182
+ done
183
+
184
+ if [ -f "$_broken_tmp" ]; then
185
+ _broken_cnt="$(LC_ALL=C awk 'END { print NR+0 }' "$_broken_tmp")"
186
+ rm -f "$_broken_tmp"
187
+ printf 'Error [lint]: Found %s unresolved reference(s).\n' "$_broken_cnt" >&2
188
+ exit 1
189
+ fi
190
+ fi
191
+
192
+ append_wiki_log_entry "lint" "Verified B-Tree shards (_index/) and symmetric graph tables (${_out_cnt} edges)."
193
+ [ "$QUIET" = "1" ] || printf 'Wiki lint passed: %s edges verified, root _index.md is %s lines.\n' "$_out_cnt" "$_root_lines"