@cbartens/bkit 5.5.0 → 6.0.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/.bkit/README.md +30 -20
- package/.bkit/core/.agents/docs.md +1 -0
- package/.bkit/core/.agents/rules.md +51 -26
- package/.bkit/core/.bkit/README.md +29 -19
- package/.bkit/core/.bkit/authoring.md +117 -0
- package/.bkit/core/.bkit/cloud.md +11 -11
- package/.bkit/core/.bkit/comparison.md +5 -5
- package/.bkit/core/.bkit/install.md +20 -20
- package/.bkit/core/.bkit/jsonl.md +202 -122
- package/.bkit/core/.bkit/lite.md +1 -1
- package/.bkit/core/.bkit/pipeline.md +49 -52
- package/.bkit/core/.bkit/prompts.md +106 -167
- package/.bkit/core/.bkit/providers.md +29 -32
- package/.bkit/core/.bkit/templates.md +44 -18
- package/.bkit/core/.bkit/trackers.md +60 -28
- package/.bkit/core/.bkit/workflows.md +399 -359
- package/.bkit/core/AGENTS.md +16 -22
- package/.bkit/core/docs/templates/bkit-audit-accessibility.md +14 -38
- package/.bkit/core/docs/templates/bkit-audit-adoption.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-agents.md +19 -43
- package/.bkit/core/docs/templates/bkit-audit-aio-seo.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-analytics.md +14 -38
- package/.bkit/core/docs/templates/bkit-audit-annotations.md +11 -35
- package/.bkit/core/docs/templates/bkit-audit-app-performance.md +15 -39
- package/.bkit/core/docs/templates/bkit-audit-app-store.md +12 -36
- package/.bkit/core/docs/templates/bkit-audit-architecture.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-ci-performance.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-config.md +18 -42
- package/.bkit/core/docs/templates/bkit-audit-cost.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-coverage.md +15 -39
- package/.bkit/core/docs/templates/bkit-audit-database.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-dead-code.md +15 -39
- package/.bkit/core/docs/templates/bkit-audit-deferrals.md +15 -38
- package/.bkit/core/docs/templates/bkit-audit-dependencies.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-documentation.md +11 -35
- package/.bkit/core/docs/templates/bkit-audit-duplication.md +18 -42
- package/.bkit/core/docs/templates/bkit-audit-failures.md +13 -37
- package/.bkit/core/docs/templates/bkit-audit-flakes.md +12 -36
- package/.bkit/core/docs/templates/bkit-audit-gates.md +14 -38
- package/.bkit/core/docs/templates/bkit-audit-issues.md +15 -39
- package/.bkit/core/docs/templates/bkit-audit-llm.md +16 -40
- package/.bkit/core/docs/templates/bkit-audit-logs.md +14 -38
- package/.bkit/core/docs/templates/bkit-audit-models.md +12 -36
- package/.bkit/core/docs/templates/bkit-audit-privacy.md +12 -36
- package/.bkit/core/docs/templates/bkit-audit-prompts.md +15 -39
- package/.bkit/core/docs/templates/bkit-audit-red-team.md +15 -39
- package/.bkit/core/docs/templates/bkit-audit-reliability.md +15 -39
- package/.bkit/core/docs/templates/bkit-audit-repo.md +12 -36
- package/.bkit/core/docs/templates/bkit-audit-security.md +13 -37
- package/.bkit/core/docs/templates/bkit-audit-slop.md +14 -38
- package/.bkit/core/docs/templates/bkit-audit-testing.md +12 -36
- package/.bkit/core/docs/templates/bkit-audit-usability.md +16 -40
- package/.bkit/core/docs/templates/bkit-plan.md +97 -226
- package/.bkit/core/engine/checks/after/ci-yml.ts +39 -30
- package/.bkit/core/engine/checks/after/github.ts +101 -72
- package/.bkit/core/engine/checks/after/index.ts +10 -12
- package/.bkit/core/engine/checks/after/suppression.ts +8 -5
- package/.bkit/core/engine/checks/after/tools.ts +47 -92
- package/.bkit/core/engine/checks/before/env.ts +2 -2
- package/.bkit/core/engine/checks/before/files.ts +5 -4
- package/.bkit/core/engine/checks/before/index.ts +18 -1
- package/.bkit/core/engine/checks/before/repo.ts +168 -2
- package/.bkit/core/engine/checks/tips/catalogue.ts +16 -5
- package/.bkit/core/engine/checks/tips/detect.ts +34 -0
- package/.bkit/core/engine/checks/tips/index.ts +10 -7
- package/.bkit/core/engine/cli.ts +20 -53
- package/.bkit/core/engine/commands/analysis.ts +171 -140
- package/.bkit/core/engine/commands/ci/ci.ts +6 -6
- package/.bkit/core/engine/commands/comply.ts +77 -103
- package/.bkit/core/engine/commands/install/doctor.ts +2 -2
- package/.bkit/core/engine/commands/install/install.ts +57 -18
- package/.bkit/core/engine/commands/install/uninstall.ts +65 -28
- package/.bkit/core/engine/commands/land.ts +18 -12
- package/.bkit/core/engine/commands/lib.ts +79 -17
- package/.bkit/core/engine/commands/registry.ts +4 -0
- package/.bkit/core/engine/commands/setup/setup.ts +7 -7
- package/.bkit/core/engine/commands/stats.ts +204 -0
- package/.bkit/core/engine/commands/tips.ts +10 -5
- package/.bkit/core/engine/commands/workflow/branches.ts +741 -62
- package/.bkit/core/engine/commands/workflow/challenge.ts +673 -0
- package/.bkit/core/engine/commands/workflow/context.ts +853 -29
- package/.bkit/core/engine/commands/workflow/counters.ts +267 -84
- package/.bkit/core/engine/commands/workflow/decisions.ts +541 -0
- package/.bkit/core/engine/commands/workflow/disk.ts +2 -2
- package/.bkit/core/engine/commands/workflow/fids.ts +793 -33
- package/.bkit/core/engine/commands/workflow/issues.ts +427 -105
- package/.bkit/core/engine/commands/workflow/ledgers.ts +884 -27
- package/.bkit/core/engine/commands/workflow/locks.ts +26 -2
- package/.bkit/core/engine/commands/workflow/plan-blocks.ts +582 -0
- package/.bkit/core/engine/commands/workflow/plan-versions.ts +21 -0
- package/.bkit/core/engine/commands/workflow/prs.ts +166 -30
- package/.bkit/core/engine/commands/workflow/repo.ts +59 -7
- package/.bkit/core/engine/commands/workflow/roadmap.ts +16 -7
- package/.bkit/core/engine/commands/workflow/run-facts.ts +501 -0
- package/.bkit/core/engine/commands/workflow/runs.ts +179 -49
- package/.bkit/core/engine/commands/workflow/runtime.ts +22 -1
- package/.bkit/core/engine/commands/workflow/status.ts +142 -29
- package/.bkit/core/engine/commands/workflow/step-table.ts +339 -0
- package/.bkit/core/engine/commands/workflow/steps.ts +213 -88
- package/.bkit/core/engine/commands/workflow/summary.ts +398 -0
- package/.bkit/core/engine/commands/workflow/tests.ts +1013 -148
- package/.bkit/core/engine/commands/workflow/tracker.ts +1195 -0
- package/.bkit/core/engine/commands/workflow/workflows.ts +984 -207
- package/.bkit/core/engine/commands/workflow/workstreams.ts +11 -5
- package/.bkit/core/engine/commands/workflow/worktrees.ts +276 -22
- package/.bkit/core/engine/install/manifest.ts +11 -9
- package/.bkit/core/engine/install/modules/cloud.ts +21 -13
- package/.bkit/core/engine/install/modules/core.ts +325 -7
- package/.bkit/core/engine/install/modules/opencode.ts +2 -2
- package/.bkit/core/engine/install/version-file.ts +6 -2
- package/.bkit/core/engine/ledgers/migrate.ts +92 -0
- package/.bkit/core/engine/ledgers/status.ts +92 -15
- package/.bkit/core/engine/ledgers/telemetry.ts +1261 -254
- package/.bkit/core/engine/render/hosts.ts +4 -0
- package/.bkit/core/engine/render/models.ts +12 -0
- package/.bkit/core/engine/shared/branches.ts +4 -1
- package/.bkit/core/engine/shared/egress.ts +7 -0
- package/.bkit/core/engine/shared/json.ts +25 -7
- package/.bkit/core/engine/shared/settings.ts +202 -33
- package/.bkit/core/engine/shared/text.ts +361 -0
- package/.bkit/core/engine/tools/grammar-density.ts +215 -0
- package/.bkit/core/engine/tools/instructions-sources.ts +834 -0
- package/.bkit/core/engine/tools/instructions.ts +1751 -0
- package/.bkit/core/hooks/gateguard-engine.js +24 -23
- package/.bkit/core/hooks/guardrails-shell.js +2 -3
- package/.bkit/core/hooks/guardrails-tiers.js +7 -1
- package/.bkit/core/hooks/readguard-engine.js +2 -2
- package/.bkit/core/prompts/bkit-audit-generate.md +62 -25
- package/.bkit/core/prompts/bkit-build-challenge-1.md +40 -17
- package/.bkit/core/prompts/bkit-build-challenge-2.md +44 -17
- package/.bkit/core/prompts/bkit-build-install-skills.md +26 -13
- package/.bkit/core/prompts/bkit-build-plan-convert.md +74 -0
- package/.bkit/core/prompts/bkit-build-plan-current.md +26 -13
- package/.bkit/core/prompts/bkit-build-plan-execute.md +74 -26
- package/.bkit/core/prompts/bkit-build-plan-gaps-remediate.md +64 -23
- package/.bkit/core/prompts/bkit-build-plan-verify.md +57 -33
- package/.bkit/core/prompts/bkit-debug-abort-handover.md +30 -14
- package/.bkit/core/prompts/bkit-debug-failure-diagnose.md +34 -14
- package/.bkit/core/prompts/bkit-debug-failure-remediate.md +34 -17
- package/.bkit/core/prompts/bkit-debug-failure-reproduce.md +26 -13
- package/.bkit/core/prompts/bkit-debug-failure-soak.md +31 -16
- package/.bkit/core/prompts/bkit-design-brainstorm-ideate-1.md +21 -11
- package/.bkit/core/prompts/bkit-design-brainstorm-ideate-2.md +20 -11
- package/.bkit/core/prompts/bkit-design-brainstorm-ideate-3.md +20 -11
- package/.bkit/core/prompts/bkit-design-generate.md +33 -13
- package/.bkit/core/prompts/bkit-design-organise.md +25 -12
- package/.bkit/core/prompts/bkit-design-review-1.md +31 -15
- package/.bkit/core/prompts/bkit-design-review-2.md +28 -13
- package/.bkit/core/prompts/bkit-design-spike.md +30 -13
- package/.bkit/core/prompts/bkit-design-update.md +33 -19
- package/.bkit/core/prompts/bkit-plan-generate.md +75 -19
- package/.bkit/core/prompts/bkit-plan-review-1.md +50 -27
- package/.bkit/core/prompts/bkit-plan-review-2.md +59 -28
- package/.bkit/core/prompts/bkit-plan-scope-check.md +109 -0
- package/.bkit/core/prompts/bkit-plan-update.md +61 -24
- package/.bkit/core/prompts/bkit-ship-conflict-resolve.md +27 -15
- package/.bkit/core/prompts/bkit-ship-deferred-sweep.md +31 -16
- package/.bkit/core/prompts/bkit-ship-update-docs.md +30 -15
- package/.bkit/core/prompts/bkit-utility-compress-markdown.md +24 -14
- package/.bkit/core/prompts/bkit-utility-update-decisions.md +32 -14
- package/.bkit/core/prompts/bkit-utility-update-deferrals.md +29 -14
- package/.bkit/core/prompts/bkit-utility-update-documentation.md +28 -13
- package/.bkit/core/prompts/bkit-utility-update-failures.md +31 -14
- package/.bkit/core/prompts/bkit-utility-update-knowledge.md +33 -14
- package/.bkit/core/prompts/bkit-utility-update-roadmap.md +38 -17
- package/.bkit/core/prompts/bkit-utility-update-tracker.md +43 -20
- package/.bkit/core/workflows/bkit-auto.md +54 -46
- package/.bkit/core/workflows/bkit-build.md +577 -458
- package/.bkit/core/workflows/bkit-debug.md +348 -333
- package/.bkit/core/workflows/bkit-design.md +201 -237
- package/.bkit/core/workflows/bkit-plan.md +210 -321
- package/.bkit/core/workflows/bkit-ship.md +328 -278
- package/.bkit/shared/.bkit/.env.example +2 -2
- package/.bkit/shared/.claude/settings.json +11 -0
- package/.bkit/shared/.cursor/mcp.json +1 -1
- package/.bkit/shared/.cursor/rules/bkit-rules.mdc +1 -1
- package/.bkit/shared/.gitattributes +1 -1
- package/.bkit/shared/.mcp.json +1 -1
- package/.bkit/shared/.opencode/opencode.json +11 -0
- package/.bkit/shared/CLAUDE.md +2 -2
- package/.bkit/shared/GEMINI.md +2 -2
- package/.bkit/shared/docs/architecture.md +1 -1
- package/.bkit/shared/docs/data-schema.md +1 -1
- package/.bkit/shared/docs/deployments.md +16 -28
- package/.bkit/shared/docs/design.md +1 -1
- package/.bkit/shared/docs/noise.md +1 -1
- package/.bkit/shared/docs/observability.md +2 -2
- package/.bkit/shared/docs/security.md +1 -1
- package/.bkit/shared/docs/setup.md +1 -1
- package/.bkit/shared/docs/technology.md +1 -1
- package/.bkit/shared/docs/testing.md +27 -10
- package/.bkit/templates/.claude/hooks/bkit-cloud-bootstrap.sh +1 -1
- package/.bkit/templates/.github/ISSUE_TEMPLATE/bkit-change.yml +26 -5
- package/.bkit/templates/.github/workflows/bkit-ci.yml +22 -84
- package/.bkit/templates/.github/workflows/bkit-cloud.yml +21 -19
- package/.bkit/templates/.husky/pre-push +52 -13
- package/.bkit/templates/.prettierignore +7 -0
- package/.bkit/templates/scripts/ci/bkit-local-stack-lock.sh +194 -0
- package/.bkit/templates/scripts/ci/check-capstone.mjs +2 -4
- package/.bkit/templates/scripts/ci/check-capstone.test.mjs +4 -23
- package/.bkit/templates/scripts/ci/lanes/resolve-push-base.mjs +104 -23
- package/.bkit/templates/scripts/ci/lanes/resolve-push-base.test.mjs +103 -6
- package/.bkit/templates/scripts/cleanup/bkit-disk-prune.mjs +1 -1
- package/.bkit/templates/scripts/graphify/bkit-graph-pre-push.sh +3 -1
- package/.bkit/templates/scripts/prettier/bkit-insert-ignore.mjs +49 -29
- package/CHANGELOG.md +142 -0
- package/README.md +58 -34
- package/dist/checks/after/ci-yml.js +39 -29
- package/dist/checks/after/github.js +99 -72
- package/dist/checks/after/index.js +11 -13
- package/dist/checks/after/suppression.js +8 -5
- package/dist/checks/after/tools.js +40 -83
- package/dist/checks/before/env.js +2 -2
- package/dist/checks/before/files.js +5 -3
- package/dist/checks/before/index.js +15 -2
- package/dist/checks/before/repo.js +163 -2
- package/dist/checks/tips/catalogue.js +14 -5
- package/dist/checks/tips/detect.js +32 -0
- package/dist/checks/tips/index.js +7 -5
- package/dist/cli.js +19 -47
- package/dist/commands/analysis.js +109 -100
- package/dist/commands/ci/ci.js +6 -6
- package/dist/commands/comply.js +54 -91
- package/dist/commands/install/doctor.js +2 -2
- package/dist/commands/install/install.js +50 -19
- package/dist/commands/install/uninstall.js +59 -28
- package/dist/commands/land.js +16 -12
- package/dist/commands/lib.js +51 -16
- package/dist/commands/registry.js +4 -0
- package/dist/commands/setup/setup.js +7 -7
- package/dist/commands/stats.js +136 -0
- package/dist/commands/tips.js +7 -3
- package/dist/commands/workflow/branches.js +656 -56
- package/dist/commands/workflow/challenge.js +556 -0
- package/dist/commands/workflow/context.js +709 -30
- package/dist/commands/workflow/counters.js +208 -75
- package/dist/commands/workflow/decisions.js +441 -0
- package/dist/commands/workflow/disk.js +2 -2
- package/dist/commands/workflow/fids.js +689 -33
- package/dist/commands/workflow/issues.js +362 -99
- package/dist/commands/workflow/ledgers.js +762 -27
- package/dist/commands/workflow/locks.js +24 -2
- package/dist/commands/workflow/plan-blocks.js +437 -0
- package/dist/commands/workflow/plan-versions.js +22 -0
- package/dist/commands/workflow/prs.js +142 -32
- package/dist/commands/workflow/repo.js +52 -7
- package/dist/commands/workflow/roadmap.js +9 -7
- package/dist/commands/workflow/run-facts.js +426 -0
- package/dist/commands/workflow/runs.js +144 -43
- package/dist/commands/workflow/runtime.js +12 -1
- package/dist/commands/workflow/status.js +130 -29
- package/dist/commands/workflow/step-table.js +276 -0
- package/dist/commands/workflow/steps.js +171 -80
- package/dist/commands/workflow/summary.js +275 -0
- package/dist/commands/workflow/tests.js +834 -136
- package/dist/commands/workflow/tracker.js +983 -0
- package/dist/commands/workflow/workflows.js +834 -195
- package/dist/commands/workflow/workstreams.js +11 -5
- package/dist/commands/workflow/worktrees.js +221 -22
- package/dist/install/manifest.js +11 -9
- package/dist/install/modules/cloud.js +22 -13
- package/dist/install/modules/core.js +326 -7
- package/dist/install/modules/opencode.js +2 -2
- package/dist/install/version-file.js +6 -2
- package/dist/ledgers/migrate.js +81 -0
- package/dist/ledgers/status.js +61 -8
- package/dist/ledgers/telemetry.js +905 -124
- package/dist/render/hosts.js +4 -0
- package/dist/render/models.js +12 -0
- package/dist/shared/branches.js +3 -1
- package/dist/shared/egress.js +7 -0
- package/dist/shared/json.js +20 -7
- package/dist/shared/settings.js +147 -26
- package/dist/shared/text.js +333 -0
- package/dist/tools/grammar-density.js +183 -0
- package/dist/tools/instructions-sources.js +721 -0
- package/dist/tools/instructions.js +1579 -0
- package/package.json +114 -23
- package/.bkit/core/engine/commands/harvest.ts +0 -459
- package/.bkit/core/engine/tools/wording.ts +0 -748
- package/.bkit/core/prompts/bkit-build-challenge-reconcile.md +0 -69
- package/.bkit/core/prompts/bkit-build-challenge-remediate.md +0 -71
- package/.bkit/core/prompts/bkit-build-challenge-tests.md +0 -74
- package/.bkit/core/prompts/bkit-build-summary.md +0 -94
- package/.bkit/core/prompts/bkit-debug-summary.md +0 -96
- package/.bkit/core/prompts/bkit-design-summary.md +0 -107
- package/.bkit/core/prompts/bkit-plan-complexity-check.md +0 -67
- package/.bkit/core/prompts/bkit-plan-complexity-split.md +0 -88
- package/.bkit/core/prompts/bkit-plan-review-3.md +0 -93
- package/.bkit/core/prompts/bkit-plan-scope-check-1.md +0 -85
- package/.bkit/core/prompts/bkit-plan-scope-check-2.md +0 -85
- package/.bkit/core/prompts/bkit-plan-summary.md +0 -107
- package/.bkit/core/prompts/bkit-ship-summary.md +0 -78
- package/.bkit/templates/scripts/ci/lanes/check-tree-cache.mjs +0 -138
- package/.bkit/templates/scripts/ci/lanes/check-tree-cache.test.mjs +0 -181
- package/dist/commands/harvest.js +0 -353
- package/dist/tools/wording.js +0 -680
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
// shared/text.ts — engine output as plain text, and markdown tables that provably keep their data.
|
|
2
|
+
//
|
|
3
|
+
// `renderText` prints a command's output object as `key: value` lines: arrays of flat
|
|
4
|
+
// objects as markdown tables, nested values as indented `key:` blocks. `parseText` reads
|
|
5
|
+
// that text back to the value `--json` prints for the same object. Scalars keep their
|
|
6
|
+
// type: a string that would read as `null`, a boolean, a number, `[]`, `{}` or a quoted
|
|
7
|
+
// string prints inside one pair of double quotes. `markdownTable` escapes `|`,
|
|
8
|
+
// backslashes and line breaks, and prints a missing cell as `MISSING`; a literal
|
|
9
|
+
// `MISSING` value is escaped, so the two stay distinct. `checkedTable` renders an array
|
|
10
|
+
// of flat objects and parses the table back, refusing anything that would not read back
|
|
11
|
+
// as the same typed rows.
|
|
12
|
+
|
|
13
|
+
import { parseJson } from "./json.js";
|
|
14
|
+
|
|
15
|
+
/** A missing cell. A value equal to it prints escaped as `\—`. */
|
|
16
|
+
export const MISSING = "—";
|
|
17
|
+
|
|
18
|
+
export type Cell = string | number | boolean | null | undefined;
|
|
19
|
+
|
|
20
|
+
type Scalar = string | number | boolean | null;
|
|
21
|
+
|
|
22
|
+
const isScalar = (v: unknown): v is Scalar =>
|
|
23
|
+
v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean";
|
|
24
|
+
|
|
25
|
+
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
|
26
|
+
typeof v === "object" && v !== null && !Array.isArray(v);
|
|
27
|
+
|
|
28
|
+
/** Escape backslashes and line breaks so a value stays on one line. */
|
|
29
|
+
const escapeLine = (s: string): string =>
|
|
30
|
+
s.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/\r/g, "\\r");
|
|
31
|
+
|
|
32
|
+
const scalarText = (v: Scalar): string => (v === null ? "null" : String(v));
|
|
33
|
+
|
|
34
|
+
const NUMBER = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
|
|
35
|
+
const LITERALS = new Set(["null", "true", "false", "[]", "{}"]);
|
|
36
|
+
|
|
37
|
+
const quoted = (s: string): boolean => s.length >= 2 && s.startsWith('"') && s.endsWith('"');
|
|
38
|
+
|
|
39
|
+
/** A scalar as typed text: JSON's spelling for non-strings, a string quoted when it would read as one. */
|
|
40
|
+
function scalarCode(v: Scalar): string {
|
|
41
|
+
if (typeof v === "number") return Number.isFinite(v) ? String(v) : "null";
|
|
42
|
+
if (typeof v !== "string") return scalarText(v);
|
|
43
|
+
return v === "" || LITERALS.has(v) || NUMBER.test(v) || quoted(v) ? `"${v}"` : v;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Typed text back to its scalar. */
|
|
47
|
+
function readScalar(text: string): Scalar {
|
|
48
|
+
if (quoted(text)) return text.slice(1, -1);
|
|
49
|
+
if (text === "null") return null;
|
|
50
|
+
if (text === "true" || text === "false") return text === "true";
|
|
51
|
+
return NUMBER.test(text) ? Number(text) : text;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function escapeCell(cell: Cell): string {
|
|
55
|
+
if (cell === undefined) return MISSING;
|
|
56
|
+
const text = escapeLine(scalarText(cell)).replace(/\|/g, "\\|");
|
|
57
|
+
return text === MISSING ? `\\${MISSING}` : text;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const tableLine = (cells: string[]): string => `| ${cells.join(" | ")} |`;
|
|
61
|
+
|
|
62
|
+
/** A markdown table as lines, or nothing when there are no rows. Unpadded. */
|
|
63
|
+
export function markdownTable(head: string[], rows: Cell[][]): string[] {
|
|
64
|
+
if (rows.length === 0) return [];
|
|
65
|
+
return [
|
|
66
|
+
tableLine(head.map(escapeCell)),
|
|
67
|
+
tableLine(head.map(() => "---")),
|
|
68
|
+
...rows.map((r) => tableLine(head.map((_, i) => escapeCell(r[i])))),
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Split one table line into its raw cells; null when it is not `| … |`. */
|
|
73
|
+
function splitLine(line: string): string[] | null {
|
|
74
|
+
if (!line.startsWith("| ") || !line.endsWith(" |")) return null;
|
|
75
|
+
const body = line.slice(2, -2);
|
|
76
|
+
const cells: string[] = [];
|
|
77
|
+
let cell = "";
|
|
78
|
+
for (let i = 0; i < body.length; i++) {
|
|
79
|
+
const ch = body[i]!;
|
|
80
|
+
if (ch === "\\" && i + 1 < body.length) {
|
|
81
|
+
cell += ch + body[i + 1];
|
|
82
|
+
i++;
|
|
83
|
+
} else if (ch === "|" && body[i - 1] === " " && body[i + 1] === " ") {
|
|
84
|
+
cells.push(cell.slice(0, -1));
|
|
85
|
+
cell = "";
|
|
86
|
+
i++;
|
|
87
|
+
} else {
|
|
88
|
+
cell += ch;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
cells.push(cell);
|
|
92
|
+
return cells;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const UNESCAPE: Record<string, string> = {
|
|
96
|
+
n: "\n",
|
|
97
|
+
r: "\r",
|
|
98
|
+
"\\": "\\",
|
|
99
|
+
"|": "|",
|
|
100
|
+
[MISSING]: MISSING,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/** Undo `escapeLine` (plus `\|` and `\—` in a cell); null for a bad escape. */
|
|
104
|
+
function unescape(raw: string, escapes: Record<string, string>): string | null {
|
|
105
|
+
let out = "";
|
|
106
|
+
for (let i = 0; i < raw.length; i++) {
|
|
107
|
+
const ch = raw[i]!;
|
|
108
|
+
if (ch !== "\\") {
|
|
109
|
+
out += ch;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const next = escapes[raw[i + 1] ?? ""];
|
|
113
|
+
if (next === undefined) return null;
|
|
114
|
+
out += next;
|
|
115
|
+
i++;
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** One raw cell back to its value; undefined for a missing cell, null for a bad escape. */
|
|
121
|
+
const unescapeCell = (raw: string): string | undefined | null =>
|
|
122
|
+
raw === MISSING ? undefined : unescape(raw, UNESCAPE);
|
|
123
|
+
|
|
124
|
+
const LINE_ESCAPES: Record<string, string> = { n: "\n", r: "\r", "\\": "\\" };
|
|
125
|
+
|
|
126
|
+
export interface ParsedTable {
|
|
127
|
+
head: string[];
|
|
128
|
+
/** One record per row; missing cells are left out. */
|
|
129
|
+
rows: Record<string, string>[];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Parse a table `markdownTable` printed; null when the text is not such a table. */
|
|
133
|
+
export function parseMarkdownTable(text: string): ParsedTable | null {
|
|
134
|
+
const lines = text.split("\n");
|
|
135
|
+
if (lines.length < 3) return null;
|
|
136
|
+
const headCells = splitLine(lines[0]!);
|
|
137
|
+
if (!headCells || lines[1] !== tableLine(headCells.map(() => "---"))) return null;
|
|
138
|
+
const head: string[] = [];
|
|
139
|
+
for (const raw of headCells) {
|
|
140
|
+
const name = unescapeCell(raw);
|
|
141
|
+
if (typeof name !== "string") return null;
|
|
142
|
+
head.push(name);
|
|
143
|
+
}
|
|
144
|
+
const rows: Record<string, string>[] = [];
|
|
145
|
+
for (const line of lines.slice(2)) {
|
|
146
|
+
const cells = splitLine(line);
|
|
147
|
+
if (!cells || cells.length !== head.length) return null;
|
|
148
|
+
const row: Record<string, string> = {};
|
|
149
|
+
for (const [i, raw] of cells.entries()) {
|
|
150
|
+
const value = unescapeCell(raw);
|
|
151
|
+
if (value === null) return null;
|
|
152
|
+
if (value !== undefined) row[head[i]!] = value;
|
|
153
|
+
}
|
|
154
|
+
rows.push(row);
|
|
155
|
+
}
|
|
156
|
+
return { head, rows };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** A `checkedTable` table back to its typed rows; null when it is not such a table. */
|
|
160
|
+
function readTable(text: string): Record<string, Scalar>[] | null {
|
|
161
|
+
const parsed = parseMarkdownTable(text);
|
|
162
|
+
if (!parsed) return null;
|
|
163
|
+
return parsed.rows.map((r) =>
|
|
164
|
+
Object.fromEntries(Object.entries(r).map(([k, v]) => [k, readScalar(v)])),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export type TableResult = { ok: true; text: string } | { ok: false; reason: string };
|
|
169
|
+
|
|
170
|
+
/** Why `value` cannot be a table, or the flat rows it holds. */
|
|
171
|
+
function flatRows(value: unknown): { rows: Record<string, Scalar>[] } | { reason: string } {
|
|
172
|
+
if (!Array.isArray(value)) return { reason: "not an array" };
|
|
173
|
+
if (value.length === 0) return { reason: "empty array" };
|
|
174
|
+
for (const [i, item] of value.entries()) {
|
|
175
|
+
if (!isRecord(item)) return { reason: `item ${i} is not an object` };
|
|
176
|
+
if (Object.keys(item).length === 0) return { reason: `item ${i} has no keys` };
|
|
177
|
+
for (const [key, v] of Object.entries(item)) {
|
|
178
|
+
if (!isScalar(v)) return { reason: `item ${i} key ${key} is nested` };
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return { rows: value as Record<string, Scalar>[] };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* An array of flat objects as a markdown table whose typed parse equals the input rows.
|
|
186
|
+
* Columns are the union of keys in first-seen order. Anything nested, mixed or empty,
|
|
187
|
+
* or a table that would not read back as the same values of the same types, is refused.
|
|
188
|
+
*/
|
|
189
|
+
export function checkedTable(value: unknown): TableResult {
|
|
190
|
+
const flat = flatRows(value);
|
|
191
|
+
if ("reason" in flat) return { ok: false, reason: flat.reason };
|
|
192
|
+
const head = [...new Set(flat.rows.flatMap((r) => Object.keys(r)))];
|
|
193
|
+
const text = markdownTable(
|
|
194
|
+
head,
|
|
195
|
+
flat.rows.map((r) => head.map((k) => (Object.hasOwn(r, k) ? scalarCode(r[k]!) : undefined))),
|
|
196
|
+
).join("\n");
|
|
197
|
+
const parsed = parseMarkdownTable(text);
|
|
198
|
+
const rows = readTable(text);
|
|
199
|
+
if (!parsed || !rows) return { ok: false, reason: "table does not parse back" };
|
|
200
|
+
const same =
|
|
201
|
+
parsed.head.length === head.length &&
|
|
202
|
+
parsed.head.every((h, i) => h === head[i]) &&
|
|
203
|
+
rows.length === flat.rows.length &&
|
|
204
|
+
rows.every((row, i) => {
|
|
205
|
+
const want = flat.rows[i]!;
|
|
206
|
+
const keys = Object.keys(row);
|
|
207
|
+
return keys.length === Object.keys(want).length && keys.every((k) => row[k] === want[k]);
|
|
208
|
+
});
|
|
209
|
+
return same ? { ok: true, text } : { ok: false, reason: "table parses back to different rows" };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ---------------------------------------------------------------------------
|
|
213
|
+
// renderText
|
|
214
|
+
// ---------------------------------------------------------------------------
|
|
215
|
+
|
|
216
|
+
/** A key as printed: JSON-quoted when it would not read back as a plain key. */
|
|
217
|
+
const keyText = (key: string): string =>
|
|
218
|
+
key === "" || /[:\n\r\\]/.test(key) || /^[-|"\s]/.test(key) || /\s$/.test(key)
|
|
219
|
+
? JSON.stringify(key)
|
|
220
|
+
: key;
|
|
221
|
+
|
|
222
|
+
const scalarLine = (v: Scalar): string => escapeLine(scalarCode(v));
|
|
223
|
+
|
|
224
|
+
function valueLines(key: string, value: unknown, pad: string): string[] {
|
|
225
|
+
const head = `${pad}${keyText(key)}:`;
|
|
226
|
+
if (isScalar(value)) return [`${head} ${scalarLine(value)}`];
|
|
227
|
+
if (Array.isArray(value)) {
|
|
228
|
+
if (value.length === 0) return [`${head} []`];
|
|
229
|
+
const table = checkedTable(value);
|
|
230
|
+
if (table.ok) return [head, ...table.text.split("\n").map((l) => pad + l)];
|
|
231
|
+
return [head, ...itemLines(value, `${pad} `)];
|
|
232
|
+
}
|
|
233
|
+
if (isRecord(value)) {
|
|
234
|
+
const inner = objectLines(value, `${pad} `);
|
|
235
|
+
return inner.length === 0 ? [`${head} {}`] : [head, ...inner];
|
|
236
|
+
}
|
|
237
|
+
return [`${head} ${String(value)}`];
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function itemLines(items: unknown[], pad: string): string[] {
|
|
241
|
+
return items.flatMap((item) => {
|
|
242
|
+
if (item === undefined) return [`${pad}- null`];
|
|
243
|
+
if (isScalar(item)) return [`${pad}- ${scalarLine(item)}`];
|
|
244
|
+
if (Array.isArray(item))
|
|
245
|
+
return item.length === 0 ? [`${pad}- []`] : [`${pad}-`, ...itemLines(item, `${pad} `)];
|
|
246
|
+
if (isRecord(item)) {
|
|
247
|
+
const inner = objectLines(item, `${pad} `);
|
|
248
|
+
return inner.length === 0 ? [`${pad}- {}`] : [`${pad}-`, ...inner];
|
|
249
|
+
}
|
|
250
|
+
return [`${pad}- ${String(item)}`];
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function objectLines(obj: Record<string, unknown>, pad: string): string[] {
|
|
255
|
+
return Object.entries(obj)
|
|
256
|
+
.filter(([, v]) => v !== undefined)
|
|
257
|
+
.flatMap(([k, v]) => valueLines(k, v, pad));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** A command's output object as text: one `key: value` line per scalar, blocks for the rest. */
|
|
261
|
+
export function renderText(payload: Record<string, unknown>): string {
|
|
262
|
+
const lines = objectLines(payload, "");
|
|
263
|
+
return lines.length === 0 ? "" : `${lines.join("\n")}\n`;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// ---------------------------------------------------------------------------
|
|
267
|
+
// parseText
|
|
268
|
+
// ---------------------------------------------------------------------------
|
|
269
|
+
|
|
270
|
+
interface Cursor {
|
|
271
|
+
lines: string[];
|
|
272
|
+
i: number;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** No value: the text is not something `renderText` printed. */
|
|
276
|
+
const BAD = Symbol("bad");
|
|
277
|
+
type Read<T> = T | typeof BAD;
|
|
278
|
+
|
|
279
|
+
/** The value after `key: `: an empty container or a typed scalar. */
|
|
280
|
+
function inlineValue(raw: string): Read<unknown> {
|
|
281
|
+
if (raw === "[]") return [];
|
|
282
|
+
if (raw === "{}") return {};
|
|
283
|
+
const text = unescape(raw, LINE_ESCAPES);
|
|
284
|
+
return text === null ? BAD : readScalar(text);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** `key:` / `key: value` split into the key and what follows the colon. */
|
|
288
|
+
function keyLine(line: string): { key: string; rest: string } | null {
|
|
289
|
+
if (line.startsWith('"')) {
|
|
290
|
+
const m = /^"(?:[^"\\]|\\.)*"/.exec(line);
|
|
291
|
+
const key = m ? parseJson(m[0]) : { ok: false as const };
|
|
292
|
+
if (!m || !key.ok || typeof key.value !== "string") return null;
|
|
293
|
+
return { key: key.value, rest: line.slice(m[0].length) };
|
|
294
|
+
}
|
|
295
|
+
const colon = line.indexOf(":");
|
|
296
|
+
return colon <= 0 ? null : { key: line.slice(0, colon), rest: line.slice(colon) };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const at = (line: string | undefined, pad: string): boolean =>
|
|
300
|
+
line !== undefined && line.startsWith(pad) && line[pad.length] !== " ";
|
|
301
|
+
|
|
302
|
+
/** The block under a `key:` line whose key sits at `pad`. */
|
|
303
|
+
function readBlock(cur: Cursor, pad: string): Read<unknown> {
|
|
304
|
+
const next = cur.lines[cur.i];
|
|
305
|
+
if (at(next, pad) && next!.startsWith(`${pad}| `)) {
|
|
306
|
+
const table: string[] = [];
|
|
307
|
+
while (at(cur.lines[cur.i], pad) && cur.lines[cur.i]!.startsWith(`${pad}| `))
|
|
308
|
+
table.push(cur.lines[cur.i++]!.slice(pad.length));
|
|
309
|
+
return readTable(table.join("\n")) ?? BAD;
|
|
310
|
+
}
|
|
311
|
+
const inner = `${pad} `;
|
|
312
|
+
if (!at(next, inner)) return BAD;
|
|
313
|
+
return next!.slice(inner.length).startsWith("-") ? readList(cur, inner) : readObject(cur, inner);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function readObject(cur: Cursor, pad: string): Read<Record<string, unknown>> {
|
|
317
|
+
const obj: Record<string, unknown> = {};
|
|
318
|
+
while (at(cur.lines[cur.i], pad) && !/^[-|]/.test(cur.lines[cur.i]!.slice(pad.length))) {
|
|
319
|
+
const parsed = keyLine(cur.lines[cur.i++]!.slice(pad.length));
|
|
320
|
+
if (!parsed) return BAD;
|
|
321
|
+
let value: Read<unknown>;
|
|
322
|
+
if (parsed.rest === ":") value = readBlock(cur, pad);
|
|
323
|
+
else if (parsed.rest.startsWith(": ")) value = inlineValue(parsed.rest.slice(2));
|
|
324
|
+
else return BAD;
|
|
325
|
+
if (value === BAD) return BAD;
|
|
326
|
+
obj[parsed.key] = value;
|
|
327
|
+
}
|
|
328
|
+
return obj;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function readList(cur: Cursor, pad: string): Read<unknown[]> {
|
|
332
|
+
const items: unknown[] = [];
|
|
333
|
+
while (at(cur.lines[cur.i], pad) && cur.lines[cur.i]!.slice(pad.length).startsWith("-")) {
|
|
334
|
+
const rest = cur.lines[cur.i++]!.slice(pad.length + 1);
|
|
335
|
+
let item: Read<unknown>;
|
|
336
|
+
if (rest.startsWith(" ")) item = inlineValue(rest.slice(1));
|
|
337
|
+
else if (rest !== "") return BAD;
|
|
338
|
+
else {
|
|
339
|
+
const inner = `${pad} `;
|
|
340
|
+
const next = cur.lines[cur.i];
|
|
341
|
+
if (!at(next, inner)) return BAD;
|
|
342
|
+
item = next!.slice(inner.length).startsWith("-")
|
|
343
|
+
? readList(cur, inner)
|
|
344
|
+
: readObject(cur, inner);
|
|
345
|
+
}
|
|
346
|
+
if (item === BAD) return BAD;
|
|
347
|
+
items.push(item);
|
|
348
|
+
}
|
|
349
|
+
return items;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Text `renderText` printed, read back to the value `--json` prints for the same
|
|
354
|
+
* object; null when the text is not such output.
|
|
355
|
+
*/
|
|
356
|
+
export function parseText(text: string): Record<string, unknown> | null {
|
|
357
|
+
const body = text.endsWith("\n") ? text.slice(0, -1) : text;
|
|
358
|
+
const cur: Cursor = { lines: body === "" ? [] : body.split("\n"), i: 0 };
|
|
359
|
+
const obj = readObject(cur, "");
|
|
360
|
+
return obj === BAD || cur.i !== cur.lines.length ? null : obj;
|
|
361
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// tools/grammar-density.ts — articles (`a`, `an`, `the`) per 1,000 words of agent text.
|
|
2
|
+
//
|
|
3
|
+
// The measure behind `.bkit/core/.bkit/authoring.md` § Notation, Grammar: clipped text
|
|
4
|
+
// drops articles, plain English keeps them. `pnpm grammar:density` prints one row per
|
|
5
|
+
// group for the working tree, `--rev=<rev>` for a past commit, and `--diff=<base>..<head>`
|
|
6
|
+
// for only the lines `<head>` added. Repo-only tooling, like `tools/instructions.ts`.
|
|
7
|
+
//
|
|
8
|
+
// Counting: fence marker lines (a line starting with three backticks) are dropped; the
|
|
9
|
+
// rest is lowercased, every character but `a`-`z` becomes a word break.
|
|
10
|
+
|
|
11
|
+
import { spawnSync } from "node:child_process";
|
|
12
|
+
import * as fs from "node:fs";
|
|
13
|
+
import * as path from "node:path";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
|
|
16
|
+
export interface Count {
|
|
17
|
+
words: number;
|
|
18
|
+
articles: number;
|
|
19
|
+
per1k: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface GroupCount extends Count {
|
|
23
|
+
name: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Where files come from: the working tree or one git revision. */
|
|
27
|
+
export interface Source {
|
|
28
|
+
/** Repo-relative paths of every file under `dir`, sorted. */
|
|
29
|
+
list(dir: string): string[];
|
|
30
|
+
/** File content, or undefined when the file is absent. */
|
|
31
|
+
read(file: string): string | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface GroupDef {
|
|
35
|
+
name: string;
|
|
36
|
+
files(src: Source): string[];
|
|
37
|
+
/** Indexes of the counted lines; undefined = every line. */
|
|
38
|
+
lines?(content: string, file: string): number[];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Minimum per-1k values for the canonical shared text (`grammar-density.test.ts`). */
|
|
42
|
+
export const FLOORS: Record<string, number> = { clauses: 20, rules: 16 };
|
|
43
|
+
|
|
44
|
+
const FENCE = /^```/;
|
|
45
|
+
const ARTICLE = new Set(["a", "an", "the"]);
|
|
46
|
+
|
|
47
|
+
export function articlesPer1k(lines: string[]): Count {
|
|
48
|
+
let words = 0;
|
|
49
|
+
let articles = 0;
|
|
50
|
+
for (const line of lines) {
|
|
51
|
+
if (FENCE.test(line)) continue;
|
|
52
|
+
for (const w of line.toLowerCase().split(/[^a-z]+/)) {
|
|
53
|
+
if (w === "") continue;
|
|
54
|
+
words++;
|
|
55
|
+
if (ARTICLE.has(w)) articles++;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { words, articles, per1k: words === 0 ? 0 : (articles * 1000) / words };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Line indexes inside ` ```text ` blocks; with `section`, only under that `## ` heading. */
|
|
62
|
+
export function textBlockLines(md: string, section?: string): number[] {
|
|
63
|
+
const lines = md.split("\n");
|
|
64
|
+
const out: number[] = [];
|
|
65
|
+
let inSection = section === undefined;
|
|
66
|
+
let inBlock = false;
|
|
67
|
+
for (let i = 0; i < lines.length; i++) {
|
|
68
|
+
const line = lines[i]!;
|
|
69
|
+
if (!inBlock && line.startsWith("## ")) {
|
|
70
|
+
if (section !== undefined) inSection = line === `## ${section}`;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!inSection) continue;
|
|
74
|
+
if (inBlock) {
|
|
75
|
+
if (line === "```") inBlock = false;
|
|
76
|
+
else out.push(i);
|
|
77
|
+
} else if (line === "```text") {
|
|
78
|
+
inBlock = true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** New-side 0-based line indexes a `git diff -U0` output adds. */
|
|
85
|
+
export function addedLines(diff: string): Set<number> {
|
|
86
|
+
const out = new Set<number>();
|
|
87
|
+
for (const m of diff.matchAll(/^@@ -\S+ \+(\d+)(?:,(\d+))? @@/gm)) {
|
|
88
|
+
const start = Number(m[1]);
|
|
89
|
+
const count = m[2] === undefined ? 1 : Number(m[2]);
|
|
90
|
+
for (let n = 0; n < count; n++) out.add(start - 1 + n);
|
|
91
|
+
}
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const STORE = ".bkit/instructions.md";
|
|
96
|
+
|
|
97
|
+
const one = (file: string) => (src: Source) => (src.read(file) === undefined ? [] : [file]);
|
|
98
|
+
|
|
99
|
+
export const GROUPS: readonly GroupDef[] = [
|
|
100
|
+
{ name: "rules", files: one(".bkit/core/.agents/rules.md") },
|
|
101
|
+
{ name: "prompts", files: (src) => src.list(".bkit/core/prompts") },
|
|
102
|
+
{ name: "workflows", files: (src) => src.list(".bkit/core/workflows") },
|
|
103
|
+
{
|
|
104
|
+
// Revisions before the store kept the clauses in `.bkit/wording.md`.
|
|
105
|
+
name: "clauses",
|
|
106
|
+
files: (src) => (src.read(STORE) !== undefined ? [STORE] : one(".bkit/wording.md")(src)),
|
|
107
|
+
lines: (md, file) => (file === STORE ? textBlockLines(md, "Clauses") : textBlockLines(md)),
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "store-rules",
|
|
111
|
+
files: one(STORE),
|
|
112
|
+
lines: (md) => textBlockLines(md, "Rules"),
|
|
113
|
+
},
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* One count per group. `added` limits each file to the line indexes it names; a file
|
|
118
|
+
* missing from `added` counts nothing.
|
|
119
|
+
*/
|
|
120
|
+
export function measure(src: Source, added?: Map<string, Set<number>>): GroupCount[] {
|
|
121
|
+
return GROUPS.map((g) => {
|
|
122
|
+
const counted: string[] = [];
|
|
123
|
+
for (const file of g.files(src)) {
|
|
124
|
+
const content = src.read(file);
|
|
125
|
+
if (content === undefined) continue;
|
|
126
|
+
const lines = content.split("\n");
|
|
127
|
+
let idx = g.lines ? g.lines(content, file) : lines.map((_, i) => i);
|
|
128
|
+
if (added) {
|
|
129
|
+
const keep = added.get(file) ?? new Set<number>();
|
|
130
|
+
idx = idx.filter((i) => keep.has(i));
|
|
131
|
+
}
|
|
132
|
+
for (const i of idx) counted.push(lines[i]!);
|
|
133
|
+
}
|
|
134
|
+
return { name: g.name, ...articlesPer1k(counted) };
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function workingTree(root: string): Source {
|
|
139
|
+
const walk = (rel: string): string[] => {
|
|
140
|
+
const abs = path.join(root, rel);
|
|
141
|
+
if (!fs.existsSync(abs)) return [];
|
|
142
|
+
return fs
|
|
143
|
+
.readdirSync(abs, { withFileTypes: true })
|
|
144
|
+
.flatMap((e) => (e.isDirectory() ? walk(path.join(rel, e.name)) : [path.join(rel, e.name)]));
|
|
145
|
+
};
|
|
146
|
+
return {
|
|
147
|
+
list: (dir) => walk(dir).sort(),
|
|
148
|
+
read: (file) => {
|
|
149
|
+
const abs = path.join(root, file);
|
|
150
|
+
return fs.existsSync(abs) ? fs.readFileSync(abs, "utf-8") : undefined;
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function git(root: string, args: string[]): { ok: boolean; out: string } {
|
|
156
|
+
const res = spawnSync("git", args, { cwd: root, encoding: "utf-8", maxBuffer: 64 * 1024 * 1024 });
|
|
157
|
+
return { ok: res.status === 0, out: res.stdout ?? "" };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function gitRev(root: string, rev: string): Source {
|
|
161
|
+
return {
|
|
162
|
+
list: (dir) =>
|
|
163
|
+
git(root, ["ls-tree", "-r", "--name-only", rev, "--", dir])
|
|
164
|
+
.out.split("\n")
|
|
165
|
+
.filter(Boolean)
|
|
166
|
+
.sort(),
|
|
167
|
+
read: (file) => {
|
|
168
|
+
const res = git(root, ["show", `${rev}:${file}`]);
|
|
169
|
+
return res.ok ? res.out : undefined;
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
// CLI: `pnpm grammar:density [--rev=<rev>] [--diff=<base>..<head>]`
|
|
176
|
+
// ---------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
function main(argv: string[]): number {
|
|
179
|
+
const root = process.cwd();
|
|
180
|
+
const arg = (name: string) =>
|
|
181
|
+
argv.find((a) => a.startsWith(`--${name}=`))?.slice(name.length + 3);
|
|
182
|
+
const rev = arg("rev");
|
|
183
|
+
const diff = arg("diff");
|
|
184
|
+
let groups: GroupCount[];
|
|
185
|
+
if (diff !== undefined) {
|
|
186
|
+
const [base, head] = diff.split("..");
|
|
187
|
+
if (!base || !head) {
|
|
188
|
+
console.error("[bkit-grammar] --diff takes <base>..<head>");
|
|
189
|
+
return 2;
|
|
190
|
+
}
|
|
191
|
+
const src = gitRev(root, head);
|
|
192
|
+
const added = new Map<string, Set<number>>();
|
|
193
|
+
for (const g of GROUPS) {
|
|
194
|
+
for (const file of g.files(src)) {
|
|
195
|
+
added.set(file, addedLines(git(root, ["diff", "-U0", base, head, "--", file]).out));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
groups = measure(src, added);
|
|
199
|
+
} else {
|
|
200
|
+
groups = measure(rev === undefined ? workingTree(root) : gitRev(root, rev));
|
|
201
|
+
}
|
|
202
|
+
console.log(`${"group".padEnd(12)} ${"words".padStart(8)} ${"art/1k".padStart(7)}`);
|
|
203
|
+
for (const g of groups) {
|
|
204
|
+
console.log(
|
|
205
|
+
`${g.name.padEnd(12)} ${String(g.words).padStart(8)} ${g.per1k.toFixed(1).padStart(7)}`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return 0;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (process.argv[1] && fs.existsSync(process.argv[1])) {
|
|
212
|
+
if (fs.realpathSync(fileURLToPath(import.meta.url)) === fs.realpathSync(process.argv[1])) {
|
|
213
|
+
process.exitCode = main(process.argv.slice(2));
|
|
214
|
+
}
|
|
215
|
+
}
|