codecartographer-pi 0.1.0

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 (75) hide show
  1. package/.codecarto/BACKLOG.md +192 -0
  2. package/.codecarto/CHANGELOG-2026-05-02-feedback-pass.md +118 -0
  3. package/.codecarto/CONTRIBUTING.md +56 -0
  4. package/.codecarto/GUIDE.md +298 -0
  5. package/.codecarto/LICENSE +21 -0
  6. package/.codecarto/NEW_THREAD_BLURB.md +47 -0
  7. package/.codecarto/README.md +39 -0
  8. package/.codecarto/THREAD_LOG.md +39 -0
  9. package/.codecarto/closeouts/2026-05-02-framework-feedback-pass.md +111 -0
  10. package/.codecarto/findings/architecture/README.md +3 -0
  11. package/.codecarto/findings/architecture/SKILL.md +102 -0
  12. package/.codecarto/findings/build-and-deploy/.gitkeep +0 -0
  13. package/.codecarto/findings/config-model/.gitkeep +0 -0
  14. package/.codecarto/findings/contracts/README.md +3 -0
  15. package/.codecarto/findings/contracts/SKILL.md +89 -0
  16. package/.codecarto/findings/defect-scan/README.md +18 -0
  17. package/.codecarto/findings/defect-scan/SKILL.md +87 -0
  18. package/.codecarto/findings/defect-scan/passes/01-logic-and-correctness.md +50 -0
  19. package/.codecarto/findings/defect-scan/passes/02-error-handling.md +55 -0
  20. package/.codecarto/findings/defect-scan/passes/03-concurrency-and-resources.md +54 -0
  21. package/.codecarto/findings/defect-scan/passes/04-security-and-trust.md +62 -0
  22. package/.codecarto/findings/defect-scan/passes/05-api-contract-violations.md +57 -0
  23. package/.codecarto/findings/defect-scan/passes/06-config-and-environment.md +58 -0
  24. package/.codecarto/findings/defect-scan-mechanical/README.md +17 -0
  25. package/.codecarto/findings/defect-scan-mechanical/SKILL.md +60 -0
  26. package/.codecarto/findings/defect-scan-semantic/README.md +17 -0
  27. package/.codecarto/findings/defect-scan-semantic/SKILL.md +54 -0
  28. package/.codecarto/findings/porting/README.md +3 -0
  29. package/.codecarto/findings/porting/SKILL.md +52 -0
  30. package/.codecarto/findings/protocols/README.md +3 -0
  31. package/.codecarto/findings/protocols/SKILL.md +87 -0
  32. package/.codecarto/findings/public-surfaces/README.md +3 -0
  33. package/.codecarto/findings/reimplementation-spec/README.md +3 -0
  34. package/.codecarto/findings/reimplementation-spec/SKILL.md +66 -0
  35. package/.codecarto/findings/runtime-lifecycle/README.md +3 -0
  36. package/.codecarto/findings/state-and-storage/README.md +3 -0
  37. package/.codecarto/scratch/.gitkeep +0 -0
  38. package/.codecarto/skills/spec-delta-application/SKILL.md +102 -0
  39. package/.codecarto/templates/architecture-map.md +143 -0
  40. package/.codecarto/templates/behavioral-contracts.md +134 -0
  41. package/.codecarto/templates/closeout-template.md +85 -0
  42. package/.codecarto/templates/conventions-template.md +65 -0
  43. package/.codecarto/templates/decisions-template.md +82 -0
  44. package/.codecarto/templates/defect-fix-tracker.md +77 -0
  45. package/.codecarto/templates/defect-report.md +116 -0
  46. package/.codecarto/templates/deltas-applied.md +71 -0
  47. package/.codecarto/templates/mechanical-defects.md +104 -0
  48. package/.codecarto/templates/protocols-and-state.md +126 -0
  49. package/.codecarto/templates/reimplementation-spec-opinionated.md +183 -0
  50. package/.codecarto/templates/reimplementation-spec.md +148 -0
  51. package/.codecarto/templates/reverse-engineering-bundle.md +141 -0
  52. package/.codecarto/templates/semantic-defects.md +109 -0
  53. package/.codecarto/templates/thread-log-entry-template.md +27 -0
  54. package/.codecarto/workflow/VALIDATE.md +81 -0
  55. package/.codecarto/workflow/pipeline-architecture-only.yaml +38 -0
  56. package/.codecarto/workflow/pipeline-defect-scan.yaml +61 -0
  57. package/.codecarto/workflow/pipeline-full-with-audit.yaml +188 -0
  58. package/.codecarto/workflow/pipeline-full-with-deep-audit.yaml +227 -0
  59. package/.codecarto/workflow/pipeline-lite.yaml +100 -0
  60. package/.codecarto/workflow/pipeline.yaml +163 -0
  61. package/.codecarto/workflow/status.yaml +64 -0
  62. package/LICENSE +21 -0
  63. package/README.md +356 -0
  64. package/core/index.ts +11 -0
  65. package/core/pipeline.ts +175 -0
  66. package/core/prompts.ts +183 -0
  67. package/core/status.ts +155 -0
  68. package/core/types.ts +96 -0
  69. package/core/utils.ts +52 -0
  70. package/core/workspace.ts +81 -0
  71. package/core/yaml.ts +256 -0
  72. package/extensions/codecarto/index.ts +446 -0
  73. package/mcp-server/bin.mjs +7 -0
  74. package/mcp-server/server.ts +497 -0
  75. package/package.json +52 -0
package/README.md ADDED
@@ -0,0 +1,356 @@
1
+ # CodeCartographer
2
+
3
+ [![CI](https://github.com/HuginnIndustries/CodeCartographer/actions/workflows/ci.yml/badge.svg)](https://github.com/HuginnIndustries/CodeCartographer/actions/workflows/ci.yml)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
5
+ [![npm version](https://img.shields.io/npm/v/codecartographer-pi.svg)](https://www.npmjs.com/package/codecartographer-pi)
6
+
7
+ A structured reverse-engineering toolkit for understanding unfamiliar codebases using LLMs. Drop it into any repository, point an LLM at the guide, and get a comprehensive analysis: architecture map, behavioral contracts, protocol documentation, defect report, porting synthesis, and reimplementation spec.
8
+
9
+ ## What It Does
10
+
11
+ CodeCartographer guides an LLM through a phased analysis of your source code, producing structured documentation at each step. Instead of asking an LLM "explain this codebase" and getting a vague summary, you get a systematic evaluation with evidence-tagged findings, validated outputs, and cross-session continuity.
12
+
13
+ Each phase builds on the last. The architecture map feeds into behavioral contracts, which feed into protocol documentation, which feeds into a porting bundle, which feeds into a reimplementation spec. At the end, you have a complete evaluation bundle that a human or another LLM can use to understand, maintain, or rewrite the codebase.
14
+
15
+ ## Quick Start
16
+
17
+ **1. Copy `.codecarto/` into your repository:**
18
+
19
+ ```bash
20
+ cp -r /path/to/CodeCartographer/.codecarto /path/to/your-repo/
21
+ ```
22
+
23
+ **2. Choose a pipeline** (optional — defaults to the full 7-phase with split defect scan):
24
+
25
+ ```yaml
26
+ # Edit .codecarto/workflow/status.yaml and set the pipeline field:
27
+ pipeline: workflow/pipeline-full-with-deep-audit.yaml # 7-phase with split defect scan (default; depth-first)
28
+ pipeline: workflow/pipeline-full-with-audit.yaml # 6-phase with single early defect scan
29
+ pipeline: workflow/pipeline.yaml # 5-phase without defect scan — remove defect-scan phases
30
+ pipeline: workflow/pipeline-defect-scan.yaml # 2-phase defect audit — remove contracts through reimplementation-spec
31
+ pipeline: workflow/pipeline-lite.yaml # 3-phase understanding — remove defect-scan phases, porting, and reimplementation-spec
32
+ pipeline: workflow/pipeline-architecture-only.yaml # 1-phase quick overview — keep only architecture
33
+ ```
34
+
35
+ **3. Point an LLM at the guide:**
36
+
37
+ ```
38
+ Read .codecarto/GUIDE.md and begin the analysis.
39
+ ```
40
+
41
+ That's it. The LLM reads the guide, checks `workflow/status.yaml` for progress, and starts the next phase automatically. Each phase produces a validated output in `.codecarto/findings/`.
42
+
43
+ ## Pi Package
44
+
45
+ This branch also packages CodeCartographer for [Pi](https://github.com/badlogic/pi-mono/tree/main/packages/coding-agent) without changing `.codecarto/` itself. Pi is an **optional peer dependency** — if you only want the template or the MCP server, you don't need Pi installed.
46
+
47
+ Install from a local checkout or git URL:
48
+
49
+ ```bash
50
+ pi install /absolute/path/to/CodeCartographer
51
+ # or
52
+ pi install git:github.com/your-user/CodeCartographer
53
+ ```
54
+
55
+ For extension development, you can also point Pi directly at the extension entrypoint or place it in an auto-discovered extensions directory and use `/reload`:
56
+
57
+ ```bash
58
+ pi -e /absolute/path/to/CodeCartographer/extensions/codecarto/index.ts
59
+ ```
60
+
61
+ The extension is self-contained at runtime. It uses only Node built-ins plus Pi's peer dependencies, so direct loading and `/reload` do not require a separate `npm install`.
62
+
63
+ Then in the target repository:
64
+
65
+ ```text
66
+ /codecarto-init [full-with-deep-audit|full-with-audit|full|defect-scan|lite|architecture-only]
67
+ /codecarto-status
68
+ /codecarto-next
69
+ ```
70
+
71
+ If you install the whole repository as a Pi package, Pi may still run package installation steps for the package itself, but the CodeCartographer extension does not depend on any third-party runtime modules.
72
+
73
+ What the Pi extension adds:
74
+
75
+ - `/codecarto-init` to copy `.codecarto/` into the current repository
76
+ - `/codecarto-next` to queue the next eligible phase prompt
77
+ - `/codecarto-status` to show current phase progress
78
+ - `/codecarto-validate` and `/codecarto-complete` for validation-gated status updates
79
+ - a footer/widget showing the active CodeCartographer phase
80
+ - tool interception that blocks `edit` and `write` outside `.codecarto/`
81
+ - direct phase prompts that tell Pi exactly which `.codecarto/findings/<phase>/SKILL.md` file to read, without registering those internal files as global Pi skills
82
+
83
+ ## MCP Server
84
+
85
+ The same framework is also packaged as a [Model Context Protocol](https://modelcontextprotocol.io) server, so any MCP-compatible host (Claude Code, Claude Desktop, etc.) can drive a CodeCartographer workflow without the Pi runtime. The server imports the same `core/` primitives the Pi extension uses, so phase prompts and validation are byte-identical across both surfaces.
86
+
87
+ Install and wire it up:
88
+
89
+ ```bash
90
+ npm install --global codecartographer-pi
91
+ # or, in a project: npm install codecartographer-pi
92
+ ```
93
+
94
+ Add it to your MCP host config (Claude Code: `~/.config/claude-code/config.json`, Claude Desktop: `claude_desktop_config.json`):
95
+
96
+ ```json
97
+ {
98
+ "mcpServers": {
99
+ "codecartographer": {
100
+ "command": "codecarto-mcp"
101
+ }
102
+ }
103
+ }
104
+ ```
105
+
106
+ The server exposes seven tools, each accepting an absolute `cwd` for the target repository:
107
+
108
+ | Tool | Purpose | Pi equivalent |
109
+ |---|---|---|
110
+ | `codecarto_init` | Copy `.codecarto/` into the target repo and select a pipeline | `/codecarto-init` |
111
+ | `codecarto_status` | Current phase, active pipeline, progress, open questions | `/codecarto-status` |
112
+ | `codecarto_next` | Return the next eligible phase prompt as text | `/codecarto-next` |
113
+ | `codecarto_phase` | Return a specific phase's prompt (forced, even out of order) | `/codecarto-phase` |
114
+ | `codecarto_validate` | Validate a phase output, returning structured criteria rows | `/codecarto-validate` |
115
+ | `codecarto_complete` | Atomically mark a phase complete after validation passes | `/codecarto-complete` |
116
+ | `codecarto_skill` | Return a post-pipeline skill prompt | `/codecarto-skill` |
117
+
118
+ `codecarto_init` requires `force: true` to overwrite an existing `.codecarto/` (instead of Pi's interactive confirmation).
119
+
120
+ ## What It Produces
121
+
122
+ | Artifact | Description |
123
+ |---|---|
124
+ | Architecture map | Layers, dependency direction, public surfaces, runtime lifecycle, concurrency model |
125
+ | Defect report | Multi-pass scan for logic errors, security issues, concurrency bugs, API violations |
126
+ | Defect fix tracker | Remediation log mapping each fix, deferral, or acceptance back to the defect report |
127
+ | Behavioral contracts | Feature-by-feature behavior with defaults, error handling, and acceptance tests |
128
+ | Protocols and state | Event flows, state machines, persistence formats, compatibility hazards |
129
+ | Porting bundle | Everything synthesized into a porting-oriented view with priority rankings |
130
+ | Reimplementation spec | Language-agnostic build plan with modules, acceptance scenarios, and known unknowns |
131
+
132
+ Every finding is tagged with an evidence level: **observed fact**, **strong inference**, **portability hazard**, or **open question**. Every phase output is validated against explicit completion criteria before the pipeline advances.
133
+
134
+ ## Pipeline Variants
135
+
136
+ Not every project needs the full analysis. The default is the 7-phase **full-with-deep-audit** pipeline, which splits the defect scan into an early mechanical pass and a deep semantic pass so the reimplementation can design around defects with full contracts and protocols context. Scale back if you want less, or use the legacy single-scan pipeline if you don't need the deeper context-grounded defect findings:
137
+
138
+ | Variant | Phases | Use when |
139
+ |---|---|---|
140
+ | **Full with deep audit** (default) | 7 | Complete analysis with split defect scan; reimplementation grounded in contracts/protocols-aware defect findings |
141
+ | **Full with audit** | 6 | Single early defect scan; cheaper than the deep variant when the defects are mostly mechanical |
142
+ | **Full** | 5 | Porting or reimplementation without any defect scan |
143
+ | **Defect scan** | 2 | Maintenance audit to surface latent problems |
144
+ | **Lite** | 3 | You need to understand behavior without porting plans |
145
+ | **Architecture only** | 1 | Quick structural overview |
146
+
147
+ ## Compatible Environments
148
+
149
+ CodeCartographer works with any LLM that can read and write files:
150
+
151
+ | Environment | Notes |
152
+ |---|---|
153
+ | **Claude Code** | Point it at `.codecarto/GUIDE.md`. Works out of the box. |
154
+ | **OpenCode** | Same as Claude Code — file read/write is built in. |
155
+ | **Cursor / Windsurf / IDE copilots** | Open the repo. Point the LLM at `.codecarto/GUIDE.md` in chat. |
156
+ | **Aider** | Run from the repo root. |
157
+ | **Claude.ai / ChatGPT (web chat)** | Paste file contents manually. Tedious for multi-phase runs. |
158
+ | **API-based agents** | Load files programmatically, pass to the model, write outputs back. |
159
+
160
+ ## Token Usage and Cost
161
+
162
+ CodeCartographer is token-intensive. It reads your source code multiple times across phases and produces thousands of words of structured output. Here's what to expect:
163
+
164
+ ### Template Overhead (Fixed Cost)
165
+
166
+ Every session reads the guide, pipeline definition, status file, and validation protocol. On top of that, each phase reads its own SKILL.md and output template. This overhead is fixed regardless of codebase size:
167
+
168
+ | Component | Tokens (input) |
169
+ |---|---|
170
+ | Per-session base (GUIDE + pipeline + status + VALIDATE) | ~2,600 |
171
+ | Architecture phase instructions | ~1,500 |
172
+ | Defect scan phase instructions (includes 6 pass files) | ~5,000 |
173
+ | Contracts phase instructions | ~1,500 |
174
+ | Protocols phase instructions | ~1,200 |
175
+ | Porting phase instructions | ~1,200 |
176
+ | Reimplementation spec phase instructions | ~1,100 |
177
+ | **Total template overhead for a 6-phase run** | **~27,000** |
178
+
179
+ ### Source Code Reading (Variable Cost)
180
+
181
+ This is the dominant cost. Each phase reads some or all of your source code. The architecture phase reads the most (full structural scan); later phases are more targeted but also read prior findings.
182
+
183
+ Rough guide: **expect to read 1-3x your codebase size in tokens per phase**. A 50k-token codebase might consume 100-200k input tokens across a full pipeline run.
184
+
185
+ ### Output Generation
186
+
187
+ Each phase produces a structured findings document. From a real 6-phase run (CodeCartographer analyzing itself — a small ~14k-word template):
188
+
189
+ | Phase | Output size |
190
+ |---|---|
191
+ | Architecture map | ~3,100 tokens |
192
+ | Defect report | ~2,400 tokens |
193
+ | Behavioral contracts | ~4,500 tokens |
194
+ | Protocols and state | ~3,900 tokens |
195
+ | Porting bundle | ~3,400 tokens |
196
+ | Reimplementation spec | ~4,400 tokens |
197
+ | **Total output** | **~21,800 tokens** |
198
+
199
+ Larger codebases produce proportionally larger outputs.
200
+
201
+ ### Cost Estimates
202
+
203
+ For a medium-sized codebase (~100k tokens of source):
204
+
205
+ | Pipeline | Estimated Input | Estimated Output | Total |
206
+ |---|---|---|---|
207
+ | Architecture only | ~130k | ~5k | ~135k tokens |
208
+ | Defect scan (2-phase) | ~260k | ~10k | ~270k tokens |
209
+ | Lite (3-phase) | ~370k | ~15k | ~385k tokens |
210
+ | Full (5-phase) | ~570k | ~22k | ~592k tokens |
211
+ | Full with audit (6-phase) | ~700k | ~27k | ~727k tokens |
212
+
213
+ At current API pricing (~$3/M input, ~$15/M output for Claude Sonnet), a full 5-phase run on a 100k-token codebase costs roughly **$2-4**. Larger codebases scale linearly.
214
+
215
+ ### Tips to Reduce Token Usage
216
+
217
+ - **Start with architecture-only** to see if the output quality is useful before committing to a full run.
218
+ - **Use one session per phase** — each phase gets a fresh context window, so you're not paying to carry stale context.
219
+ - **For very large codebases** (500k+ tokens of source), the LLM can't read everything anyway. It will use the architecture map to prioritize and produce partial results. Check `open_questions` in status.yaml to see what it skipped.
220
+ - **The lite pipeline (3 phases) gives 80% of the value** for understanding a codebase without the porting-specific phases.
221
+
222
+ ## Model Compatibility
223
+
224
+ CodeCartographer is LLM-agnostic by design, but model choice affects both what you can analyze and how good the results are. There are two independent constraints: context window size and model capability.
225
+
226
+ ### Context Window
227
+
228
+ Each phase runs in its own session, so the context window limits how much source code can be read per phase — not across the whole pipeline. After subtracting template overhead, prior-phase findings, and output generation, here's how much room remains for reading source code:
229
+
230
+ | Phase | Available for Source Code (128k model) | Available (200k model) |
231
+ |---|---|---|
232
+ | Architecture | ~121k | ~193k |
233
+ | Defect scan | ~115k | ~187k |
234
+ | Contracts | ~114k | ~186k |
235
+ | Protocols | ~115k | ~187k |
236
+ | Porting | ~104k | ~176k |
237
+ | Reimplementation spec | ~103k | ~175k |
238
+
239
+ Since each phase reads 1–3x the codebase, practical limits by context window:
240
+
241
+ | Codebase Size | 128k Context | 200k Context |
242
+ |---|---|---|
243
+ | <30k tokens | All phases comfortable | All phases comfortable |
244
+ | 30–60k tokens | Feasible, some PARTIAL results | Comfortable |
245
+ | 60–100k tokens | Marginal — heavy PARTIAL use | Feasible with prioritization |
246
+ | >100k tokens | Not viable | Feasible, later phases may PARTIAL |
247
+
248
+ The pipeline handles context exhaustion gracefully: phases can write `PARTIAL` validation and log remaining work in `open_questions` in status.yaml.
249
+
250
+ ### Model Capability
251
+
252
+ Context window is the easier problem. The harder constraint is whether the model can handle the cognitive demands of each phase. The tasks that degrade fastest on weaker models:
253
+
254
+ 1. **Evidence classification** (high risk) — distinguishing `observed fact` from `strong inference` from `open question` requires calibrated self-awareness about certainty. Weaker models tend to over-classify inferences as facts and skip `open question` tagging.
255
+ 2. **Defect scan** (high risk) — the 6-pass scan demands domain-specific reasoning (concurrency, security, API contracts). Weaker models produce more false positives, miss subtle bugs, and over-report style issues as defects.
256
+ 3. **Architecture synthesis** (medium-high risk) — abstracting a coherent layer map from many files is high-order reasoning. Weaker models produce flatter, shallower descriptions with poor dependency direction analysis.
257
+ 4. **Structured output adherence** (medium risk) — filling templates correctly with all required sections and consistent formatting.
258
+ 5. **Cross-phase coherence** (medium risk) — later phases build on earlier findings. Weak architecture output compounds errors downstream.
259
+
260
+ ### Recommended Model Tiers
261
+
262
+ | Model Tier | Examples | Recommended Pipeline | Notes |
263
+ |---|---|---|---|
264
+ | Frontier | Claude Opus 4.6, Claude Sonnet 4.6 | Full-with-deep-audit (default) or full-with-audit | Full quality on codebases up to ~100k tokens; the deep audit's semantic pass benefits most from frontier reasoning |
265
+ | Strong mid-tier | Claude Haiku 4.5, GPT-4o | Lite (3-phase) | Architecture and contracts are solid. Skip defect scan — false positive rate too high. Evidence classification less reliable. |
266
+ | Smaller / faster | GPT-4o-mini, Gemini Flash, small open-weight models | Architecture only | Fair structural overview. Multi-phase pipelines produce significant quality loss. Defect scan not recommended. |
267
+
268
+ ### What to Expect Below Sonnet 4.6
269
+
270
+ - **Architecture phase**: Usually passable. The layer map and public surfaces will be present but may lack nuance in dependency direction and porting priorities.
271
+ - **Contracts and protocols**: Quality depends heavily on how well architecture was captured. Expect missing edge cases and less precise error-behavior documentation.
272
+ - **Defect scan**: Not recommended. The six specialized passes require strong domain reasoning. Weaker models produce noisy reports that cost more time to triage than they save.
273
+ - **Porting and reimplementation**: These synthesis phases amplify upstream quality. If earlier phases are weak, these will be too.
274
+
275
+ If you're testing a new model, start with `pipeline-architecture-only.yaml` on a codebase you already understand, and compare the output against your own knowledge. That gives you a fast signal on whether to trust the model with deeper phases.
276
+
277
+ ## How It Works
278
+
279
+ CodeCartographer is a pure template — no CLI, no runtime, no dependencies. The "code" is structured Markdown and YAML files that tell an LLM what to analyze, in what order, and how to format the results.
280
+
281
+ The workflow is driven by flat files inside `.codecarto/`:
282
+
283
+ - **`GUIDE.md`** — the LLM entry point. Every session starts here.
284
+ - **`workflow/pipeline.yaml`** — phase definitions, dependencies, and output paths.
285
+ - **`workflow/status.yaml`** — mutable per-project state. Single source of truth for progress.
286
+ - **`workflow/VALIDATE.md`** — validation protocol run after every phase.
287
+ - **`findings/<phase>/SKILL.md`** — detailed analysis instructions per phase.
288
+ - **`templates/`** — output templates that enforce consistent structure.
289
+
290
+ Phases form a DAG: `contracts` and `protocols` can run in parallel after `architecture`; `porting` waits for both; `reimplementation-spec` is last.
291
+
292
+ ### Multi-Session Workflows
293
+
294
+ Large codebases typically need one LLM session per phase. Start a new session and point it at `.codecarto/GUIDE.md` — it reads `status.yaml`, sees what's done, and picks up the next phase automatically. You don't need to explain what happened in previous sessions.
295
+
296
+ For follow-up sessions, you can also use `NEW_THREAD_BLURB.md` as a lighter entry point — it's a compact checklist that saves tokens by skipping the full guide.
297
+
298
+ ### The Defect Scan
299
+
300
+ The defect-scan phase runs six sequential analysis passes: logic and correctness, error handling, concurrency, security, API contract violations, and configuration hazards. Each finding gets a severity (critical/high/medium/low) and a recommended action (fix before porting / port differently / leave behind).
301
+
302
+ ## Design Principles
303
+
304
+ - **LLM-agnostic**: works with any model that can read/write files.
305
+ - **Phase-gated**: one phase per session, validated before advancing.
306
+ - **Single source of truth**: `status.yaml` tracks progress; no duplicated state.
307
+ - **Evidence-classified**: every finding is tagged as observed fact, strong inference, portability hazard, or open question.
308
+ - **Template-driven**: consistent output structure across projects and sessions.
309
+ - **Drop-in**: lives inside your repo as `.codecarto/`. No symlinking or copying source code.
310
+
311
+ ## Repository Structure
312
+
313
+ ```
314
+ .codecarto/ # The drop-in template (Markdown + YAML).
315
+ GUIDE.md # LLM entry point.
316
+ findings/
317
+ architecture/ # System structure, layers, dependency direction.
318
+ defect-scan/ # Multi-pass defect report with severity and actions.
319
+ passes/ # Per-category analysis instructions (6 pass files).
320
+ contracts/ # User-visible behavior, defaults, acceptance checks.
321
+ protocols/ # Event streams, state machines, persistence formats.
322
+ porting/ # Reverse-engineering synthesis bundle.
323
+ reimplementation-spec/ # Final language-agnostic build spec.
324
+ scratch/ # Disposable analysis notes.
325
+ templates/ # Output structure templates.
326
+ workflow/ # Pipeline definitions, status, validation.
327
+ THREAD_LOG.md # Cross-session summary log.
328
+ core/ # Pipeline state machine, validators, prompt assembly.
329
+ extensions/codecarto/ # Pi extension surface (slash commands, widget, tool gating).
330
+ mcp-server/ # MCP server surface (seven tools mirroring the Pi commands).
331
+ tests/ # Invariant tests catching cross-wrapper drift.
332
+ docs/ # Roadmap, design notes.
333
+ CONTRIBUTING.md # How to contribute to CodeCartographer itself.
334
+ SECURITY.md # Security policy and reporting.
335
+ CHANGELOG.md # Version history.
336
+ ```
337
+
338
+ ## Git
339
+
340
+ The `.codecarto/.gitignore` excludes generated findings and scratch files by default. The template files (workflow definitions, skills, templates) are safe to commit so other team members can run their own analysis.
341
+
342
+ ## For Automated Agents
343
+
344
+ 1. Load the active pipeline YAML and `workflow/status.yaml`.
345
+ 2. Select the first phase whose status is not `complete` and whose dependencies are all `complete`.
346
+ 3. Feed the phase's `skill_path` and `required_reads` to the agent.
347
+ 4. Write outputs to the declared paths. Run validation. Update status.
348
+ 5. Repeat until all phases are complete. Set `current_phase` to `complete` when done.
349
+
350
+ ## Contributing
351
+
352
+ Bug reports, feature requests, and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, branch model, and the maintainer release process. All participants are expected to follow the [Code of Conduct](CODE_OF_CONDUCT.md). For security issues, please follow [SECURITY.md](SECURITY.md) instead of filing a public issue.
353
+
354
+ ## License
355
+
356
+ MIT — see [LICENSE](LICENSE).
package/core/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ // Barrel re-export of the CodeCartographer framework core. Both wrappers
2
+ // (extensions/codecarto for Pi, mcp-server for MCP) consume the framework
3
+ // exclusively through this module.
4
+
5
+ export * from "./types.ts";
6
+ export * from "./utils.ts";
7
+ export * from "./yaml.ts";
8
+ export * from "./status.ts";
9
+ export * from "./pipeline.ts";
10
+ export * from "./prompts.ts";
11
+ export * from "./workspace.ts";
@@ -0,0 +1,175 @@
1
+ // Pipeline alias resolution, DAG walking, and phase-output validation.
2
+
3
+ import { readFile } from "node:fs/promises";
4
+ import { basename, join } from "node:path";
5
+ import type {
6
+ PipelineFile,
7
+ PipelinePhase,
8
+ ValidationResult,
9
+ WorkspaceState,
10
+ } from "./types.ts";
11
+ import { pathExists } from "./utils.ts";
12
+
13
+ export const PIPELINE_ALIASES: Record<string, string> = {
14
+ "full-with-audit": "workflow/pipeline-full-with-audit.yaml",
15
+ "full-with-deep-audit": "workflow/pipeline-full-with-deep-audit.yaml",
16
+ full: "workflow/pipeline.yaml",
17
+ "defect-scan": "workflow/pipeline-defect-scan.yaml",
18
+ lite: "workflow/pipeline-lite.yaml",
19
+ "architecture-only": "workflow/pipeline-architecture-only.yaml",
20
+ };
21
+
22
+ export const DEFAULT_PIPELINE_PATH = "workflow/pipeline-full-with-deep-audit.yaml";
23
+
24
+ export function getPhaseMap(pipeline: PipelineFile): Map<string, PipelinePhase> {
25
+ return new Map(pipeline.phases.map((phase) => [phase.id, phase]));
26
+ }
27
+
28
+ export function getPipelineLabel(pipelinePath: string): string {
29
+ const fileName = basename(pipelinePath, ".yaml");
30
+ if (fileName === "pipeline") return "full";
31
+ return fileName.replace(/^pipeline-/, "");
32
+ }
33
+
34
+ export function getNextEligiblePhase(state: WorkspaceState): PipelinePhase | null {
35
+ const phaseMap = getPhaseMap(state.pipeline);
36
+ for (const phaseId of state.pipeline.phase_order) {
37
+ const phaseStatus = state.status.phases[phaseId]?.status;
38
+ if (phaseStatus === "complete") continue;
39
+ const phase = phaseMap.get(phaseId);
40
+ if (!phase) continue;
41
+ const dependencies = phase.depends_on ?? [];
42
+ const ready = dependencies.every((dependencyId) => state.status.phases[dependencyId]?.status === "complete");
43
+ if (ready) return phase;
44
+ }
45
+ return null;
46
+ }
47
+
48
+ export function resolvePhase(state: WorkspaceState, phaseId?: string): PipelinePhase | null {
49
+ const trimmed = phaseId?.trim();
50
+ if (trimmed) {
51
+ return getPhaseMap(state.pipeline).get(trimmed) ?? null;
52
+ }
53
+ return getNextEligiblePhase(state);
54
+ }
55
+
56
+ export function resolvePipelineChoice(input: string): string | null {
57
+ const trimmed = input.trim();
58
+ if (!trimmed) return null;
59
+ if (PIPELINE_ALIASES[trimmed]) return PIPELINE_ALIASES[trimmed];
60
+ return trimmed.endsWith(".yaml") ? trimmed : null;
61
+ }
62
+
63
+ export async function validatePhaseOutput(state: WorkspaceState, phaseId?: string): Promise<ValidationResult> {
64
+ const phase = resolvePhase(state, phaseId);
65
+ if (!phase) {
66
+ throw new Error(phaseId ? `Unknown phase: ${phaseId}` : "No eligible phase found.");
67
+ }
68
+ if (!phase.primary_output) {
69
+ throw new Error(`Phase ${phase.id} has no primary_output in the active pipeline.`);
70
+ }
71
+
72
+ const outputPath = join(state.workspaceDir, phase.primary_output);
73
+ if (!(await pathExists(outputPath))) {
74
+ return {
75
+ phaseId: phase.id,
76
+ primaryOutput: phase.primary_output,
77
+ outputPath,
78
+ exists: false,
79
+ hasValidationBlock: false,
80
+ overall: "MISSING",
81
+ rows: [],
82
+ gaps: [],
83
+ errors: [`Missing primary output: .codecarto/${phase.primary_output}`],
84
+ };
85
+ }
86
+
87
+ const content = await readFile(outputPath, "utf8");
88
+ const validationHeadingIndex = content.lastIndexOf("## Validation");
89
+ if (validationHeadingIndex === -1) {
90
+ return {
91
+ phaseId: phase.id,
92
+ primaryOutput: phase.primary_output,
93
+ outputPath,
94
+ exists: true,
95
+ hasValidationBlock: false,
96
+ overall: "FAIL",
97
+ rows: [],
98
+ gaps: [],
99
+ errors: ["Primary output exists but is missing a ## Validation block."],
100
+ };
101
+ }
102
+
103
+ const validationContent = content.slice(validationHeadingIndex);
104
+ const rows: Array<{ criterion: string; result: string; evidence: string }> = [];
105
+ let overall: ValidationResult["overall"] = "FAIL";
106
+
107
+ for (const rawLine of validationContent.split(/\r?\n/)) {
108
+ const line = rawLine.trim();
109
+ if (line.startsWith("|")) {
110
+ const cells = line
111
+ .split("|")
112
+ .slice(1, -1)
113
+ .map((cell) => cell.trim());
114
+ if (cells.length >= 4 && cells[0] !== "#" && !/^[-:]+$/.test(cells[0])) {
115
+ rows.push({
116
+ criterion: cells[1] ?? "",
117
+ result: cells[2] ?? "",
118
+ evidence: cells[3] ?? "",
119
+ });
120
+ }
121
+ }
122
+
123
+ const overallMatch = line.match(/^\*\*Overall:\*\*\s*(.+)$/i);
124
+ if (overallMatch?.[1]) {
125
+ const normalizedOverall = overallMatch[1].trim().toUpperCase();
126
+ if (normalizedOverall === "PASS") overall = "PASS";
127
+ else if (normalizedOverall === "PASS WITH GAPS") overall = "PASS WITH GAPS";
128
+ else overall = "FAIL";
129
+ }
130
+ }
131
+
132
+ const errors: string[] = [];
133
+ const gaps = rows
134
+ .filter((row) => row.result.toUpperCase().includes("PARTIAL"))
135
+ .map((row) => `${row.criterion}: ${row.evidence}`);
136
+
137
+ if (rows.length === 0) {
138
+ errors.push("Validation block found, but no validation rows could be parsed.");
139
+ }
140
+ if (rows.some((row) => row.result.toUpperCase().includes("FAIL"))) {
141
+ errors.push("One or more validation criteria are marked FAIL.");
142
+ overall = "FAIL";
143
+ }
144
+ if (overall === "FAIL" && errors.length === 0) {
145
+ errors.push("Validation overall result is FAIL.");
146
+ }
147
+
148
+ return {
149
+ phaseId: phase.id,
150
+ primaryOutput: phase.primary_output,
151
+ outputPath,
152
+ exists: true,
153
+ hasValidationBlock: true,
154
+ overall,
155
+ rows,
156
+ gaps,
157
+ errors,
158
+ };
159
+ }
160
+
161
+ export function buildValidationSummary(validation: ValidationResult): string[] {
162
+ const lines = [`Validation: ${validation.overall}`];
163
+ if (!validation.exists) {
164
+ lines.push(...validation.errors);
165
+ return lines;
166
+ }
167
+ lines.push(`Output: .codecarto/${validation.primaryOutput}`);
168
+ if (validation.gaps.length > 0) {
169
+ lines.push(`Gaps: ${validation.gaps.length}`);
170
+ }
171
+ if (validation.errors.length > 0) {
172
+ lines.push(...validation.errors.slice(0, 3));
173
+ }
174
+ return lines;
175
+ }