codechart 0.2.0 → 0.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codechart",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Agent skill + CLI: maps and flowcharts of code changes for AI coding agents, in terminal, chat and HTML",
5
5
  "keywords": [
6
6
  "agent-skills",
@@ -52,7 +52,11 @@
52
52
  "lint": "eslint .",
53
53
  "measure": "tsx scripts/measure.ts",
54
54
  "bench:tokens": "tsx scripts/token-bench.ts",
55
- "gen:config-schema": "tsx scripts/gen-config-schema.ts"
55
+ "gen:config-schema": "tsx scripts/gen-config-schema.ts",
56
+ "complexity": "node scripts/complexity.mjs",
57
+ "dup": "jscpd src --threshold 1 && jscpd tests --threshold 3",
58
+ "knip": "knip",
59
+ "gen:client": "tsx scripts/gen-client.ts"
56
60
  },
57
61
  "dependencies": {
58
62
  "commander": "15.0.0",
@@ -67,10 +71,15 @@
67
71
  "eslint": "^10.13.0",
68
72
  "fast-check": "4.10.2",
69
73
  "happy-dom": "21.0.4",
74
+ "jscpd": "5.4.2",
75
+ "knip": "6.44.1",
70
76
  "tsup": "^9.0.0",
71
77
  "tsx": "4.23.15",
72
78
  "typescript": "^6.0.3",
73
79
  "typescript-eslint": "^8.72.1",
74
80
  "vitest": "^5.0.5"
81
+ },
82
+ "overrides": {
83
+ "minimatch": "^11.0.1"
75
84
  }
76
85
  }
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: codechart
3
- description: 'Use whenever you are about to describe code changes or a plan for them, whether you made the changes or not. Triggers include "summarize the changes", "summarize the staged/uncommitted changes", "what changed", "what did you change", "recap", "walk me through the diff", "explain this diff/commit/branch", "write the PR description", "review what we did", "wrap up", and finishing a task with a summary; also "make a plan", "how would you implement", "plan this feature/migration/refactor", "design the schema change", any request for a map, diagram or overview of how files, tables, models, services or config relate; and any request to see how code behaves: "show me the flow", "how does this work", "walk me through the logic", "what are the edge cases", "what happens when…", "draw the decision flow". Not needed when the change is a single file with only text, comments, formatting, a typo or a version bump: if there is a diff, look at it first and skip this skill then.'
3
+ description: 'Use this skill before you write any summary, wrap-up, PR description or plan of a code change, including your own final report after implementing something. Rule: finished a task that touched 3+ files or added branches, or about to plan one, or asked "what changed", "summarize the changes", "recap", "explain this diff/commit/branch", "write the PR description", "make a plan", "how would you implement", "show me the flow", "how does this work", "what are the edge cases"? Then run this skill FIRST and put its verified map or flow in your reply instead of a list of changed files. Skip only for a single-file text, comment, formatting, typo or version-bump change (look at the diff first).'
4
4
  ---
5
5
 
6
6
  # codechart
@@ -9,7 +9,7 @@ Renders maps and flows from JSON specs you write, for a **plan** or **change sum
9
9
 
10
10
  A flow the user asked for ("show me the flow"): skip step 1, write a flow spec without `rules` (`summary`, unchanged code `existing`), render as in step 2 with `--output both`.
11
11
 
12
- Skip it for a trivial change (see description), a question not about code changes or behaviour, or if the user said no diagram. If `codechart` is "not found", say once "codechart is not installed, so no map" and go on; never install it.
12
+ Skip it for a trivial change (see description), a question not about code changes or behaviour, or if the user said no diagram. If `codechart` is "not found", say once "the codechart CLI is not installed (`npm i -g codechart`), so no map" and go on; never install it.
13
13
 
14
14
  ## 1. Check
15
15
 
@@ -40,7 +40,7 @@ codechart render --md - <<'EOF'
40
40
  EOF
41
41
  ```
42
42
 
43
- For a change summary, add the same `--git`/`--since`/`--commit`/`--head` you gave `check`. Commit or branch not checked out: add `--at '<sha or branch>'` so anchors are read from it. On errors, fix the spec and rerun. A `✗` line is a claim that doesn't match the code: if your spec was wrong (status, path), fix it and rerender; if the change really differs from what you said, keep the `✗` and say so. Stderr `no map drawn`/`no flow drawn`: skip it silently. Rare cases: [spec-guide.md](spec-guide.md), [flow-guide.md](flow-guide.md), `codechart schema [--flow]`.
43
+ For a change summary, add the same `--git`/`--since`/`--commit`/`--head` you gave `check`. Commit or branch not checked out: add `--at '<sha or branch>'` so anchors are read from it. On errors, fix the spec and rerun. A `✗` line is a claim that doesn't match the code: if your spec was wrong (status, path), fix it and rerender; if the change really differs from what you said, keep the `✗` and say so. `✗ … not on map`: add a node or name the file in `omitted`. A summary footer saying `diff not checked`: you left out the flag, rerender with it. Stderr `no map drawn`/`no flow drawn`: skip it silently. Stderr `codechart <version> is available`: tell the user in one line at the end of your reply, once. Rare cases: [spec-guide.md](spec-guide.md), [flow-guide.md](flow-guide.md), `codechart schema [--flow]`.
44
44
 
45
45
  ### Flow (one behaviour each)
46
46
 
@@ -81,11 +81,12 @@ Draw only branches you saw in the code or (plan) will build; unverified ones go
81
81
  "gaps": ["users: columns not read"] }
82
82
  ```
83
83
 
84
- - One node per table, model, file, service or endpoint, never merged: `new`, `changed`, `removed`, `config` (config/env), `context` (untouched neighbour the reader needs). Under 15; tests and docs go in `omitted`.
84
+ - One node per table, model, file, service or endpoint, never merged: `new`, `changed`, `removed`, `config` (config/env), `context` (untouched neighbour the reader needs). Under 15; tests and docs go in `omitted`, naming each changed file by path, file name, `folder/` or glob (`"tests: test_teams.py"`); lockfiles and generated files need neither.
85
+ - Infrastructure: one node per resource, module or Kubernetes object, labelled as the code names it (`aws_s3_bucket.logs`, `module.vpc`, `Deployment api`), `kind` `other`, `path` the file that declares it; variables and values `config`; `terraform plan not run` goes in `gaps`. See [spec-guide.md](spec-guide.md#infrastructure).
85
86
  - `layer` required, from a small set. `kind`: `table` `enum` `model` `service` `endpoint` `component` `file` (default with `path`) `config` `other`. Migration label: `migration: <what it does>`.
86
87
  - `fields`: all you read, `change`: `added`/`removed`/`modified`; `fieldsComplete: true` only if you read the whole definition.
87
88
  - Optional on a `new`/`changed` node with `path`: `match`, a distinctive piece of one line from the diff, as proof (shown in the HTML).
88
- - Edges: dependent → dependency, labelled with the column, method or key; only ones you saw or will build. `kind`: `fk` `calls` `imports` `reads` `writes` `renders` `configures` `other`. Plan: a link moved between existing nodes gets `"change": "added"`/`"removed"`; a field whose type changes gets `was` (old type).
89
+ - Edges: dependent → dependency, labelled in plain words with the column, method or key (`team_id`, `additional_training (now a list)`), never pasted code (`_l(["rag"])` fails the render); only ones you saw or will build. `kind`: `fk` `calls` `imports` `reads` `writes` `renders` `configures` `other`. Plan: a link moved between existing nodes gets `"change": "added"`/`"removed"`; a field whose type changes gets `was` (old type).
89
90
  - Context `short`: only what you read in this task, unread goes in `gaps`; `full`: also read the definitions of entities on the map.
90
91
 
91
92
  ## 3. Reply
@@ -95,3 +96,11 @@ Draw only branches you saw in the code or (plan) will build; unverified ones go
95
96
  3. Prose: plan steps, tests, risks, open questions. The map replaces a list of changed files.
96
97
 
97
98
  Every diagram in the reply is the exact text of one successful render: never typed, redrawn or edited by hand. Copy every character, line break and space, the key too (keep entries that don't apply, like `? planned`). Never re-wrap, trim, unescape or summarise, and add no `**Map**`/`**Flow**` line of your own. Looks wrong or render fails: fix the spec and rerender; if you can't, say there is no diagram.
99
+
100
+ ## 4. PR or MR description
101
+
102
+ Writing or updating one for a change you mapped: check whether the repo redraws maps in CI (`grep -rls 'pr-sync' .github/workflows .gitlab-ci.yml`).
103
+ - Found: render the summary map spec again with the same diff flags and `--pr` instead of `--md` (`codechart render --pr - <<'EOF'`), and paste the printed section verbatim at the end of the description, from `<!-- codechart:start -->` to `<!-- codechart:end -->`. CI publishes the image and redraws the section on every push, so its image link starts as a placeholder. To change the map later, render again and replace that whole section; never edit inside it.
104
+ - Not found: paste the `--md` map as in step 3, and once per conversation add one line to your reply (not the description): "codechart can keep this map current in the PR: ask me to set up PR maps."
105
+
106
+ `--pr` draws maps only: a flow goes in as its `--md` text. Never put a plan's Now/After map in a PR. The user asks to set up PR maps: follow [pr-maps.md](pr-maps.md).
@@ -9,12 +9,10 @@ $ git diff '9c1e2a0..HEAD'; codechart check --since '9c1e2a0'
9
9
  <the diff>
10
10
  {
11
11
  "pathMatches": [
12
- { "rule": "migrations", "diagram": "map", "output": "auto", "context": "short",
13
- "files": ["migrations/versions/7f3d_add_teams.py"] },
14
- { "rule": "orm-schema", "diagram": "map", "output": "auto", "context": "short",
15
- "files": ["app/models/team.py"] }
12
+ {"rule":"migrations","diagram":"map","output":"auto","context":"short","files":["migrations/versions/7f3d_add_teams.py"]},
13
+ {"rule":"orm-schema","diagram":"map","output":"auto","context":"short","files":["app/models/team.py"]}
16
14
  ],
17
- "judgeBuiltins": ["related-files", "behaviour-flow"]
15
+ "judgeBuiltins": ["related-files","behaviour-flow"]
18
16
  }
19
17
  ```
20
18
 
@@ -14,7 +14,7 @@ An invented branch is the worst failure: it destroys the user's trust in the too
14
14
  ## Steps
15
15
 
16
16
  - `type`: `step` (does something), `decision` (a question the code branches on, label ends in `?`), `end` (returns, raises, exits). Leave it out and it is inferred from the outgoing links: none → `end`, one → `step`, two or more → `decision`. A label ending in `?` with fewer than 2 links fails the render. `diagram: "flow"` is inferred from `steps` too.
17
- - `label`: verb + object, aim for 30 characters or fewer (`look up invite by token`, `expired?`), not a sentence or a function name. Longer labels are cut at the terminal width; details belong in your prose or the anchored code. One action per step: ` / ` or ` + ` fails validation.
17
+ - `label`: verb + object, aim for 30 characters or fewer (`look up invite by token`, `expired?`), not a sentence or a function name. Longer labels are cut at the terminal width; details belong in your prose or the anchored code. One action per step: ` / ` or ` + ` fails validation. Step and link labels are plain words, never code: quotes, a call with arguments, `[...]` with something inside, `=` or `...` fails validation (`reads as code`); rename it and render again.
18
18
  - **No hidden decisions:** a label with "if", "else", "or" or `;` hides a branch. Make it a decision with labelled branches, or put it in `gaps` if you didn't verify it.
19
19
  - Decision labels are unique: one decision per question; if two really differ, say how (`map spec?`, `flow anchors valid?`).
20
20
  - `start`: the id of the first step.
@@ -0,0 +1,13 @@
1
+ # Setting up PR maps
2
+
3
+ Only when the user asks for it ("set up codechart PR maps", "keep the map in my PRs current"). Never set this up on your own: it adds a CI file to their repo.
4
+
5
+ 1. From the repo root, run `codechart setup-pr-maps` (no `--yes`). It changes nothing: it prints the file it would add (or the job it would append to `.gitlab-ci.yml`), the token it uses and what runs on each push.
6
+ 2. Show the user that plan in full and ask whether to go ahead, and how:
7
+ - write the file into their work tree, uncommitted (`codechart setup-pr-maps --yes`), for them to review and commit;
8
+ - or open a pull/merge request with it (`codechart setup-pr-maps --yes --pr`, needs `gh` or `glab` signed in), which they merge to turn it on or close to say no.
9
+ 3. Run what they chose. Never commit or push the file yourself unless they ask.
10
+ 4. GitLab: the job needs a masked CI/CD variable `CODECHART_GITLAB_TOKEN`. Give the user the two links the command printed (create an access token with `api` scope, then add it under Settings > CI/CD > Variables). Never ask for the token or set it yourself.
11
+ 5. Say what happens next: from then on, PR/MR descriptions you write for a mapped change get the map section (SKILL.md step 4), and CI redraws it on every push.
12
+
13
+ Output `PR maps are already set up`: tell the user, nothing to do. `can't tell whether … is GitHub or GitLab`: ask which, then add `--host github` or `--host gitlab`. `… already exists and is not codechart's`: show the message; the user sets it up by hand: https://github.com/freeleex/codechart/blob/main/docs/pr-maps.md#by-hand
@@ -23,7 +23,7 @@ Rules:
23
23
  { "id": "legacy-roles", "label": "legacy_roles", "kind": "table", "status": "removed", "layer": "database" }
24
24
  ```
25
25
  with `users` as `changed` (its `legacy_role_id` field `"change": "removed"`) and the old edge `users → legacy-roles` kept.
26
- - Leave out tests, docs and lock files; list them in `omitted` instead (`"tests: test_teams.py"`).
26
+ - Leave out tests, docs and lock files; list them in `omitted` instead (`"tests: test_teams.py"`). With a diff flag, `render` flags every changed file that no node's `path` covers (a folder path covers the files under it) and no `omitted` entry names by repo path, file name, `folder/` or glob. Lockfiles, generated files and files a map rule sends to output `none` are never flagged.
27
27
  - Don't add a node you can't describe from what you read. Put it in `gaps`.
28
28
  - In a plan, name files the repo's way (look at neighbouring files for naming and numbering); they are shown as `new`.
29
29
  - Aim for under 15 nodes. More than that: drop the least connected `context` nodes and mention them in `omitted`.
@@ -53,6 +53,39 @@ Rules:
53
53
  - `false`: you list only what you saw, such as the changed columns in a migration. The map then says only some fields are shown.
54
54
  - In `full` context, open the definition so you can list every field and set `true`.
55
55
 
56
+ ## Infrastructure
57
+
58
+ Terraform, OpenTofu, Terragrunt, Helm, Kustomize and Kubernetes files match the built-in `infrastructure` rule. No special kinds:
59
+
60
+ | Thing | Node |
61
+ |---|---|
62
+ | Resource or data source | `kind: other`, label as the code names it (`aws_s3_bucket.logs`), `path` its `.tf` file, `match` its block line (`resource "aws_s3_bucket" "logs"`) |
63
+ | Module call | `kind: other`, label `module.vpc`, `path` the file with the `module` block, `match` its line (`module "vpc"`) |
64
+ | Local module's code, when the change edits it | `kind: file`, label `modules/vpc`, `path` its folder |
65
+ | Kubernetes object (manifest or Helm template) | `kind: other`, label `Deployment api`, `path` its file |
66
+ | Chart | `kind: file`, label `chart: api`, `path` its folder |
67
+ | Variable, output, tfvars or values key, ConfigMap or Secret | `kind: config`, status `config` |
68
+
69
+ - `layer` by role: `network`, `compute`, `storage`, `iam`, `dns`, `config`; not by file or environment.
70
+ - Edges: dependent → dependency, labelled with the attribute that refers (`vpc_id`, `role`, `serviceName`). `reads` for a reference or a variable, `configures` for values or inputs fed into a module or template, `other` for `depends_on`.
71
+ - `fields`: the attributes you changed or that matter, with `was` on a changed value (`"name": "instance_type", "type": "t3.small", "change": "modified", "was": "t3.micro"`); `fieldsComplete: false` unless you listed the whole block.
72
+ - `gaps`: resources and remote modules you didn't read, and `terraform plan not run` (you can't tell an in-place update from a replacement without it). A rename or a replacement you did spot is a risk for your prose.
73
+
74
+ ```json
75
+ { "kind": "summary", "title": "Access logs bucket for the load balancer", "rules": ["infrastructure"],
76
+ "nodes": [
77
+ { "id": "logs", "label": "aws_s3_bucket.logs", "kind": "other", "status": "new", "layer": "storage", "path": "infra/logs.tf", "match": "resource \"aws_s3_bucket\" \"logs\"" },
78
+ { "id": "alb", "label": "aws_lb.api", "kind": "other", "status": "changed", "layer": "network", "path": "infra/alb.tf", "fieldsComplete": false,
79
+ "fields": [{ "name": "access_logs.enabled", "type": "true", "change": "modified", "was": "false" }] },
80
+ { "id": "retention", "label": "var.log_retention_days", "kind": "config", "status": "config", "layer": "config", "path": "infra/variables.tf" }
81
+ ],
82
+ "edges": [
83
+ { "from": "alb", "to": "logs", "label": "access_logs.bucket", "kind": "reads" },
84
+ { "from": "logs", "to": "retention", "label": "expiration.days", "kind": "reads" }
85
+ ],
86
+ "gaps": ["terraform plan not run"] }
87
+ ```
88
+
56
89
  ## What `render` checks
57
90
 
58
91
  Warnings, not failures (`--strict` makes them exit 1). Each mismatch is named in a `✗` line at the top of the output.
@@ -68,7 +101,7 @@ Warnings, not failures (`--strict` makes them exit 1). Each mismatch is named in
68
101
  ```
69
102
 
70
103
  - Direction: from the thing that depends to the thing it depends on (FK holder → referenced table; caller → callee; reader → config).
71
- - `label`: the column, method or key that makes the link (`team_id`, `list_projects()`, `reads DEFAULT_CURRENCY`). Short.
104
+ - `label`: the column, method or key that makes the link, in plain words (`team_id`, `list_projects()`, `reads DEFAULT_CURRENCY`, `additional_training (now a list)`). Short. Never a code expression from the diff: a node or edge label with quotes, a call with arguments, `[...]` with something inside, `=` or `...` fails validation (`reads as code`); rename it and render again.
72
105
  - `kind`: fk, calls, imports, reads, writes, renders, configures, other. Use `writes` for a migration that creates or alters a table, and `other` for a model's `maps` edge to its table.
73
106
  - Only edges you saw in code, or (in a plan) ones you are committing to build.
74
107
  - In a plan, `change` marks a link that moves between nodes existing before and after: `"added"` for one the plan builds, `"removed"` for one it drops. Leave it out on edges to a `new` or `removed` node; their status already says it. The HTML then shows the plan as Now and After side by side.
@@ -13,6 +13,7 @@
13
13
  # minNodes: 8 # auto output: write HTML from this many nodes on, or when the map cuts text
14
14
  # Only in ~/.config/codechart/config.yaml (ignored here): web: outDir: ~/.cache/codechart (relative to home), keep: 20, maxAgeDays: 7 (HTML cleanup),
15
15
  # open: auto (auto | always | never; auto opens new HTML in your browser inside VS Code-based editors, whose links show the source)
16
+ # Also global only: updateCheck: true (look up the newest codechart on npm at most once a day; false turns it off)
16
17
 
17
18
  # useBuiltinRules: true # common migration and model paths, "more than 2 changed files depend on each other", and behaviour-flow
18
19
 
@@ -0,0 +1,29 @@
1
+ # codechart PR maps (written by `codechart setup-pr-maps`, or copy to .github/workflows/codechart.yml).
2
+ # On every push to a PR, redraws the codechart map in its description against the PR's latest commits.
3
+ # Does nothing for PRs without a codechart section.
4
+ name: codechart map
5
+ on:
6
+ pull_request:
7
+ types: [opened, synchronize, reopened]
8
+ # A newer push cancels the run still drawing the older one; pr-sync also skips saving if the PR moved on.
9
+ concurrency:
10
+ group: codechart-map-${{ github.event.pull_request.number }}
11
+ cancel-in-progress: true
12
+ permissions:
13
+ contents: write # pushes the map image to refs/codechart/pr-<n>, not to a branch
14
+ pull-requests: write
15
+ jobs:
16
+ map:
17
+ # A fork's token is read-only: it can neither push the image nor edit the PR.
18
+ if: github.event.pull_request.head.repo.full_name == github.repository
19
+ runs-on: ubuntu-latest
20
+ steps:
21
+ - uses: actions/checkout@v5
22
+ with:
23
+ fetch-depth: 0
24
+ - uses: actions/setup-node@v5
25
+ with:
26
+ node-version: 22
27
+ - run: npx --yes codechart@latest pr-sync
28
+ env:
29
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,15 @@
1
+ # codechart PR maps (added by `codechart setup-pr-maps`, or add it to .gitlab-ci.yml yourself).
2
+ # On every push to an MR, redraws the codechart map in its description against the MR's latest commits.
3
+ # Does nothing for MRs without a codechart section.
4
+ # Needs a masked CI/CD variable CODECHART_GITLAB_TOKEN: an access token with api scope and at least
5
+ # Developer role (CI_JOB_TOKEN can't edit MRs or upload files).
6
+ codechart-map:
7
+ image: node:22
8
+ # A newer pipeline cancels this one (with "Auto-cancel redundant pipelines", on by default); pr-sync also skips saving if the MR moved on.
9
+ interruptible: true
10
+ variables:
11
+ GIT_DEPTH: 0
12
+ rules:
13
+ - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CODECHART_GITLAB_TOKEN
14
+ script:
15
+ - npx --yes codechart@latest pr-sync