@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,834 @@
|
|
|
1
|
+
// tools/instructions-sources.ts — the sections of `.bkit/instructions.md` that copy a
|
|
2
|
+
// master elsewhere, read from that master and compared with the store.
|
|
3
|
+
//
|
|
4
|
+
// Masters: `## Rules` ← the `bkit:start` / `bkit:end` block of `.bkit/core/.agents/rules.md`;
|
|
5
|
+
// `## Project Rules` ← `## Project Rules` of `.agents/rules.md`; `## Hook Text` ← the
|
|
6
|
+
// messages built in `HOOK_SOURCES`; `## Permissions` ← the shipped Claude settings and
|
|
7
|
+
// opencode config; `## Special Behaviors` and `## Authoring Rules` ← `CATALOG_SOURCES`.
|
|
8
|
+
// Check mode runs in `tools/instructions-sources.test.ts`; `pnpm instructions:sync`
|
|
9
|
+
// rewrites the rule text and catalog copies. Hook text and permissions are fixed by hand.
|
|
10
|
+
|
|
11
|
+
import * as fs from "node:fs";
|
|
12
|
+
import * as path from "node:path";
|
|
13
|
+
|
|
14
|
+
/** One `## {heading}` section of a markdown file, heading line excluded, up to the next `## `. */
|
|
15
|
+
export function sectionOf(md: string, heading: string): string | undefined {
|
|
16
|
+
const m = new RegExp(`(?:^|\\n)## ${escapeRe(heading)}\\n`).exec(md);
|
|
17
|
+
if (!m) return undefined;
|
|
18
|
+
const rest = md.slice(m.index + m[0].length);
|
|
19
|
+
const end = rest.search(/^## /m);
|
|
20
|
+
return end === -1 ? rest : rest.slice(0, end);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
24
|
+
|
|
25
|
+
export interface RuleRef {
|
|
26
|
+
path: string;
|
|
27
|
+
section?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface Carrier {
|
|
31
|
+
/** `workflow`, `prompt`, `repo-agent`, `audit`, `plan-template`, `entry`, `hook` or `permission`. */
|
|
32
|
+
surface: string;
|
|
33
|
+
/** `verbatim`, `condensed`, `partial`, `specialised`, `applies` or `cites`. */
|
|
34
|
+
relation: string;
|
|
35
|
+
/** As written: `all`, `all except …`, or backticked names and `prefix-*` families. */
|
|
36
|
+
names: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface DriftItem {
|
|
40
|
+
/** The item line as written, ` - ` dropped. */
|
|
41
|
+
raw: string;
|
|
42
|
+
cites: { name: string; line: number }[];
|
|
43
|
+
/** The quoted excerpt the cited lines hold. */
|
|
44
|
+
excerpt?: string;
|
|
45
|
+
/** Why the drift stays, from a `- kept:` line under the item. */
|
|
46
|
+
kept?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RuleEntry {
|
|
50
|
+
name: string;
|
|
51
|
+
/** `R{nn}` (bkit block) or `P{nn}` (Project Rules). */
|
|
52
|
+
id: string;
|
|
53
|
+
text: string;
|
|
54
|
+
retired: string[];
|
|
55
|
+
refs: RuleRef[];
|
|
56
|
+
/** `## Clauses` entry carrying the rule's text. */
|
|
57
|
+
clause?: string;
|
|
58
|
+
carriers: Carrier[];
|
|
59
|
+
drift: DriftItem[];
|
|
60
|
+
/** Entries reviewed as distinct despite reading alike. */
|
|
61
|
+
distinctFrom: string[];
|
|
62
|
+
/** Prompts that copy the whole rule bullet, as written (`all`, `all except …`, names). */
|
|
63
|
+
carry?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const CARRIER_SURFACES = [
|
|
67
|
+
"workflow",
|
|
68
|
+
"prompt",
|
|
69
|
+
"repo-agent",
|
|
70
|
+
"audit",
|
|
71
|
+
"plan-template",
|
|
72
|
+
"entry",
|
|
73
|
+
"hook",
|
|
74
|
+
"permission",
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
const CITES = /^((?:`[^`:]+:\d+`(?:, )?)+): ?(.*)$/;
|
|
78
|
+
|
|
79
|
+
/** One drift item line (` - ` dropped) with its `kept` reason. */
|
|
80
|
+
export function parseDriftItem(raw: string, kept?: string): DriftItem {
|
|
81
|
+
const m = CITES.exec(raw);
|
|
82
|
+
const cites = m
|
|
83
|
+
? [...m[1]!.matchAll(/`([^`:]+):(\d+)`/g)].map((c) => ({ name: c[1]!, line: Number(c[2]) }))
|
|
84
|
+
: [];
|
|
85
|
+
const excerpt = m ? /"([^"]+)"/.exec(m[2]!)?.[1] : undefined;
|
|
86
|
+
return {
|
|
87
|
+
raw,
|
|
88
|
+
cites,
|
|
89
|
+
...(excerpt === undefined ? {} : { excerpt }),
|
|
90
|
+
...(kept === undefined ? {} : { kept }),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** `- {key}:` lines of an entry, each with the lines indented under it. */
|
|
95
|
+
export function entryKeys(section: string): { key: string; value: string; children: string[] }[] {
|
|
96
|
+
const out: { key: string; value: string; children: string[] }[] = [];
|
|
97
|
+
let inBlock = false;
|
|
98
|
+
for (const line of section.split("\n")) {
|
|
99
|
+
if (/^`{3,}/.test(line)) inBlock = !inBlock;
|
|
100
|
+
if (inBlock) continue;
|
|
101
|
+
const top = /^- ([a-z-]+):(?: (.*))?$/.exec(line);
|
|
102
|
+
if (top) out.push({ key: top[1]!, value: top[2] ?? "", children: [] });
|
|
103
|
+
else if (/^ {2,}- /.test(line) && out.length > 0) out[out.length - 1]!.children.push(line);
|
|
104
|
+
else if (line.trim() === "") continue;
|
|
105
|
+
else if (!/^ {2,}/.test(line)) out.push({ key: "", value: line, children: [] });
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The two rule masters: the bkit-block template and the repo's own rules file. */
|
|
111
|
+
export interface RuleMasters {
|
|
112
|
+
bkit: string;
|
|
113
|
+
project: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const RULE_SECTIONS = [
|
|
117
|
+
{ heading: "Rules", prefix: "R", region: "bkit" },
|
|
118
|
+
{ heading: "Project Rules", prefix: "P", region: "project" },
|
|
119
|
+
] as const;
|
|
120
|
+
|
|
121
|
+
/** Top-level `- ` bullets of the bkit block, or of `## Project Rules` below `bkit:end`, in file order. */
|
|
122
|
+
export function ruleBullets(rulesMd: string, region: "bkit" | "project"): string[] {
|
|
123
|
+
const start = rulesMd.indexOf("<!-- bkit:start -->");
|
|
124
|
+
const end = rulesMd.indexOf("<!-- bkit:end -->");
|
|
125
|
+
let body: string;
|
|
126
|
+
if (region === "bkit") {
|
|
127
|
+
body = start === -1 || end === -1 ? "" : rulesMd.slice(start, end);
|
|
128
|
+
} else {
|
|
129
|
+
body = sectionOf(end === -1 ? rulesMd : rulesMd.slice(end), "Project Rules") ?? "";
|
|
130
|
+
}
|
|
131
|
+
return body
|
|
132
|
+
.split("\n")
|
|
133
|
+
.filter((l) => l.startsWith("- "))
|
|
134
|
+
.map((l) => l.slice(2));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** One entry per `### {name}` of `## {heading}` in the store. */
|
|
138
|
+
export function parseRuleEntries(md: string, heading: "Rules" | "Project Rules"): RuleEntry[] {
|
|
139
|
+
const body = sectionOf(md, heading);
|
|
140
|
+
if (body === undefined) throw new Error(`[bkit-instructions] no \`## ${heading}\` section`);
|
|
141
|
+
return body
|
|
142
|
+
.split(/^### /m)
|
|
143
|
+
.slice(1)
|
|
144
|
+
.map((section) => {
|
|
145
|
+
const name = section.slice(0, section.indexOf("\n")).trim();
|
|
146
|
+
const id = /^- rule: ([RP]\d+)/m.exec(section)?.[1] ?? "";
|
|
147
|
+
const block = (kind: string) =>
|
|
148
|
+
[...section.matchAll(new RegExp(`^\`\`\`${kind}\\n([\\s\\S]*?)^\`\`\`$`, "gm"))].flatMap(
|
|
149
|
+
(m) => m[1]!.split("\n").filter((l) => l.trim() !== ""),
|
|
150
|
+
);
|
|
151
|
+
const refs: RuleRef[] = [];
|
|
152
|
+
const refsAt = section.search(/^- refs:$/m);
|
|
153
|
+
if (refsAt !== -1) {
|
|
154
|
+
for (const line of section.slice(refsAt).split("\n").slice(1)) {
|
|
155
|
+
const m = /^ {2}- `([^`]+)`(?: § (.+))?$/.exec(line);
|
|
156
|
+
if (!m) break;
|
|
157
|
+
refs.push(m[2] === undefined ? { path: m[1]! } : { path: m[1]!, section: m[2] });
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const carriers: Carrier[] = [];
|
|
161
|
+
const drift: DriftItem[] = [];
|
|
162
|
+
let clause: string | undefined;
|
|
163
|
+
let carry: string | undefined;
|
|
164
|
+
const distinctFrom: string[] = [];
|
|
165
|
+
for (const k of entryKeys(section)) {
|
|
166
|
+
if (CARRIER_SURFACES.includes(k.key)) {
|
|
167
|
+
for (const c of k.children) {
|
|
168
|
+
const m = /^ {2}- ([a-z]+): (.+)$/.exec(c);
|
|
169
|
+
if (m) carriers.push({ surface: k.key, relation: m[1]!, names: m[2]! });
|
|
170
|
+
}
|
|
171
|
+
} else if (k.key === "drift") {
|
|
172
|
+
for (let i = 0; i < k.children.length; i++) {
|
|
173
|
+
const item = /^ {2}- (.+)$/.exec(k.children[i]!);
|
|
174
|
+
if (!item) continue;
|
|
175
|
+
const kept = /^ {4}- kept: (.+)$/.exec(k.children[i + 1] ?? "")?.[1];
|
|
176
|
+
drift.push(parseDriftItem(item[1]!, kept));
|
|
177
|
+
}
|
|
178
|
+
} else if (k.key === "clause") {
|
|
179
|
+
clause = /`([^`]+)`/.exec(k.value)?.[1];
|
|
180
|
+
} else if (k.key === "carry") {
|
|
181
|
+
carry = k.value;
|
|
182
|
+
} else if (k.key === "distinct-from") {
|
|
183
|
+
distinctFrom.push(...[...k.value.matchAll(/`([^`]+)`/g)].map((m) => m[1]!));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
name,
|
|
188
|
+
id,
|
|
189
|
+
text: block("text").join("\n"),
|
|
190
|
+
retired: block("retired"),
|
|
191
|
+
refs,
|
|
192
|
+
...(clause === undefined ? {} : { clause }),
|
|
193
|
+
carriers,
|
|
194
|
+
drift,
|
|
195
|
+
distinctFrom,
|
|
196
|
+
...(carry === undefined ? {} : { carry }),
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const globRe = (base: string) =>
|
|
202
|
+
new RegExp(`^${base.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*")}$`);
|
|
203
|
+
|
|
204
|
+
/** Repo files a ref path names; a `*` in the basename matches any characters. */
|
|
205
|
+
function refFiles(root: string, ref: string): string[] {
|
|
206
|
+
const abs = path.join(root, ref);
|
|
207
|
+
if (!ref.includes("*")) return fs.existsSync(abs) ? [abs] : [];
|
|
208
|
+
const dir = path.dirname(abs);
|
|
209
|
+
if (!fs.existsSync(dir)) return [];
|
|
210
|
+
const re = globRe(path.basename(abs));
|
|
211
|
+
return fs
|
|
212
|
+
.readdirSync(dir)
|
|
213
|
+
.filter((f) => re.test(f))
|
|
214
|
+
.map((f) => path.join(dir, f));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** True when `ref` names a repo file (or glob) and, with `section`, one of its files has that heading. */
|
|
218
|
+
export function refExists(root: string, ref: string, section?: string): boolean {
|
|
219
|
+
const files = refFiles(root, ref);
|
|
220
|
+
if (files.length === 0) return false;
|
|
221
|
+
if (section === undefined) return true;
|
|
222
|
+
const heading = new RegExp(`^#{1,6} ${escapeRe(section)}$`, "m");
|
|
223
|
+
return files.some((f) => fs.statSync(f).isFile() && heading.test(fs.readFileSync(f, "utf-8")));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Findings for `## Rules` and `## Project Rules` against their masters. */
|
|
227
|
+
export function checkRules(
|
|
228
|
+
store: string,
|
|
229
|
+
masters: RuleMasters,
|
|
230
|
+
exists: (ref: string, section?: string) => boolean,
|
|
231
|
+
): string[] {
|
|
232
|
+
const out: string[] = [];
|
|
233
|
+
for (const s of RULE_SECTIONS) {
|
|
234
|
+
const entries = parseRuleEntries(store, s.heading);
|
|
235
|
+
const bullets = ruleBullets(masters[s.region], s.region);
|
|
236
|
+
const n = Math.max(entries.length, bullets.length);
|
|
237
|
+
for (let i = 0; i < n; i++) {
|
|
238
|
+
const want = `${s.prefix}${String(i + 1).padStart(2, "0")}`;
|
|
239
|
+
const e = entries[i];
|
|
240
|
+
const bullet = bullets[i];
|
|
241
|
+
if (!e) {
|
|
242
|
+
out.push(`${want}: rules.md bullet has no \`## ${s.heading}\` entry — ${bullet}`);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const tag = `${e.id || want} \`${e.name}\``;
|
|
246
|
+
if (e.id !== want) out.push(`${tag}: id should be ${want} (file order of rules.md)`);
|
|
247
|
+
if (bullet === undefined) {
|
|
248
|
+
out.push(`${tag}: no rules.md bullet at position ${i + 1}`);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (e.text !== bullet)
|
|
252
|
+
out.push(`${tag}: text differs from rules.md — run pnpm instructions:sync`);
|
|
253
|
+
for (const r of e.refs) {
|
|
254
|
+
if (!exists(r.path, r.section)) {
|
|
255
|
+
out.push(`${tag}: ref \`${r.path}\`${r.section ? ` § ${r.section}` : ""} does not exist`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Copy each changed rules.md bullet into its entry's `text` block and move the old text
|
|
265
|
+
* to the front of its `retired` block. Entries are matched by position; a deleted or
|
|
266
|
+
* added bullet is fixed by hand.
|
|
267
|
+
*/
|
|
268
|
+
export function syncRules(store: string, masters: RuleMasters): string {
|
|
269
|
+
const lines = store.split("\n");
|
|
270
|
+
for (const s of RULE_SECTIONS) {
|
|
271
|
+
const bullets = ruleBullets(masters[s.region], s.region);
|
|
272
|
+
const head = lines.indexOf(`## ${s.heading}`);
|
|
273
|
+
if (head === -1) continue;
|
|
274
|
+
let entry = -1;
|
|
275
|
+
for (let i = head + 1; i < lines.length && !lines[i]!.startsWith("## "); i++) {
|
|
276
|
+
if (lines[i]!.startsWith("### ")) entry++;
|
|
277
|
+
if (lines[i] !== "```text" || entry < 0) continue;
|
|
278
|
+
const close = lines.indexOf("```", i + 1);
|
|
279
|
+
const old = lines.slice(i + 1, close).join("\n");
|
|
280
|
+
const bullet = bullets[entry];
|
|
281
|
+
if (bullet === undefined || bullet === old) {
|
|
282
|
+
i = close;
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
lines.splice(i + 1, close - i - 1, bullet);
|
|
286
|
+
const after = i + 2;
|
|
287
|
+
let j = after + 1;
|
|
288
|
+
while (j < lines.length && lines[j] === "") j++;
|
|
289
|
+
if (lines[j] === "```retired") lines.splice(j + 1, 0, old);
|
|
290
|
+
else lines.splice(after + 1, 0, "", "```retired", old, "```");
|
|
291
|
+
i = after;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return lines.join("\n");
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ---------------------------------------------------------------------------
|
|
298
|
+
// Hook Text ← `.bkit/core/hooks/*.js` and the per-host guardrails hooks
|
|
299
|
+
// ---------------------------------------------------------------------------
|
|
300
|
+
|
|
301
|
+
/** Hook files whose messages `## Hook Text` copies. */
|
|
302
|
+
export const HOOK_SOURCES = [
|
|
303
|
+
".bkit/core/hooks/gateguard-engine.js",
|
|
304
|
+
".bkit/core/hooks/guardrails-read.js",
|
|
305
|
+
".bkit/core/hooks/guardrails-shell.js",
|
|
306
|
+
".bkit/core/hooks/guardrails-tiers.js",
|
|
307
|
+
".bkit/core/hooks/readguard-engine.js",
|
|
308
|
+
".bkit/core/.claude/hooks/bkit-guardrails.cjs",
|
|
309
|
+
".bkit/core/.codex/hooks/bkit-guardrails.cjs",
|
|
310
|
+
".bkit/core/.cursor/hooks/bkit-guardrails.cjs",
|
|
311
|
+
".bkit/core/.agents/plugins/bkit/hooks/bkit-guardrails.cjs",
|
|
312
|
+
];
|
|
313
|
+
|
|
314
|
+
export interface HookMessage {
|
|
315
|
+
file: string;
|
|
316
|
+
/** The enclosing `function {name}`. */
|
|
317
|
+
fn: string;
|
|
318
|
+
/** Cooked text; each interpolation kept as `${expr}`, its source spelling. */
|
|
319
|
+
text: string;
|
|
320
|
+
/** Literal parts, an interpolation between each pair. */
|
|
321
|
+
parts: string[];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
interface Literal {
|
|
325
|
+
start: number;
|
|
326
|
+
end: number;
|
|
327
|
+
parts: string[];
|
|
328
|
+
holes: string[];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const COOK: Record<string, string> = { n: "\n", t: "\t" };
|
|
332
|
+
const cook = (ch: string) => COOK[ch] ?? ch;
|
|
333
|
+
|
|
334
|
+
/** Index after the `${…}` expression opened at `i` (just past `${`), skipping nested strings. */
|
|
335
|
+
function skipExpr(src: string, i: number): number {
|
|
336
|
+
let depth = 1;
|
|
337
|
+
while (i < src.length) {
|
|
338
|
+
const c = src[i]!;
|
|
339
|
+
if (c === "'" || c === '"' || c === "`") {
|
|
340
|
+
i = readLiteral(src, i).end;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (c === "{") depth++;
|
|
344
|
+
if (c === "}" && --depth === 0) return i + 1;
|
|
345
|
+
i++;
|
|
346
|
+
}
|
|
347
|
+
return i;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function readLiteral(src: string, start: number): Literal {
|
|
351
|
+
const q = src[start]!;
|
|
352
|
+
const parts = [""];
|
|
353
|
+
const holes: string[] = [];
|
|
354
|
+
let i = start + 1;
|
|
355
|
+
while (i < src.length && src[i] !== q) {
|
|
356
|
+
const c = src[i]!;
|
|
357
|
+
if (c === "\\") {
|
|
358
|
+
parts[parts.length - 1] += cook(src[i + 1]!);
|
|
359
|
+
i += 2;
|
|
360
|
+
} else if (q === "`" && c === "$" && src[i + 1] === "{") {
|
|
361
|
+
const end = skipExpr(src, i + 2);
|
|
362
|
+
holes.push(src.slice(i + 2, end - 1));
|
|
363
|
+
parts.push("");
|
|
364
|
+
i = end;
|
|
365
|
+
} else {
|
|
366
|
+
parts[parts.length - 1] += c;
|
|
367
|
+
i++;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return { start, end: i + 1, parts, holes };
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Every string literal of a JS source, comments and regex literals skipped. */
|
|
374
|
+
function literals(src: string): Literal[] {
|
|
375
|
+
const out: Literal[] = [];
|
|
376
|
+
let prev = "";
|
|
377
|
+
let i = 0;
|
|
378
|
+
while (i < src.length) {
|
|
379
|
+
const c = src[i]!;
|
|
380
|
+
if (c === "/" && src[i + 1] === "/") {
|
|
381
|
+
i = src.indexOf("\n", i);
|
|
382
|
+
if (i === -1) break;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
if (c === "/" && src[i + 1] === "*") {
|
|
386
|
+
i = src.indexOf("*/", i + 2) + 2;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (c === "/" && (prev === "" || "(,=:[!&|?{};+".includes(prev))) {
|
|
390
|
+
i++;
|
|
391
|
+
let inClass = false;
|
|
392
|
+
while (i < src.length && (src[i] !== "/" || inClass)) {
|
|
393
|
+
if (src[i] === "\\") i++;
|
|
394
|
+
else if (src[i] === "[") inClass = true;
|
|
395
|
+
else if (src[i] === "]") inClass = false;
|
|
396
|
+
i++;
|
|
397
|
+
}
|
|
398
|
+
i++;
|
|
399
|
+
prev = "/";
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
if (c === "'" || c === '"' || c === "`") {
|
|
403
|
+
const lit = readLiteral(src, i);
|
|
404
|
+
out.push(lit);
|
|
405
|
+
i = lit.end;
|
|
406
|
+
prev = c;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (!/\s/.test(c)) prev = c;
|
|
410
|
+
i++;
|
|
411
|
+
}
|
|
412
|
+
return out;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const MESSAGE_START = /^(?:\[bkit [A-Za-z]+\]|bkit (?:[A-Za-z]+ )?guardrails:|Blocked)/;
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Agent-facing messages of a hook file: literals joined by `+` into one string, kept when
|
|
419
|
+
* the string opens with a bkit hook prefix (`[bkit GateGuard]`, `bkit … guardrails:`,
|
|
420
|
+
* `Blocked`), is a `fix:` value, or is the argument right after a kept message (Cursor's
|
|
421
|
+
* `agent_message`).
|
|
422
|
+
*/
|
|
423
|
+
export function hookMessages(src: string, file: string): HookMessage[] {
|
|
424
|
+
const lits = literals(src);
|
|
425
|
+
const fns = [...src.matchAll(/\bfunction\s+(\w+)\s*\(/g)].map((m) => ({
|
|
426
|
+
at: m.index!,
|
|
427
|
+
name: m[1]!,
|
|
428
|
+
}));
|
|
429
|
+
const out: HookMessage[] = [];
|
|
430
|
+
let lastEnd = -1;
|
|
431
|
+
for (let k = 0; k < lits.length; k++) {
|
|
432
|
+
const group = [lits[k]!];
|
|
433
|
+
while (k + 1 < lits.length && /^\s*\+\s*$/.test(src.slice(lits[k]!.end, lits[k + 1]!.start))) {
|
|
434
|
+
group.push(lits[++k]!);
|
|
435
|
+
}
|
|
436
|
+
const parts = [""];
|
|
437
|
+
const holes: string[] = [];
|
|
438
|
+
for (const lit of group) {
|
|
439
|
+
parts[parts.length - 1] += lit.parts[0];
|
|
440
|
+
for (let h = 0; h < lit.holes.length; h++) {
|
|
441
|
+
holes.push(lit.holes[h]!);
|
|
442
|
+
parts.push(lit.parts[h + 1]!);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
const start = group[0]!.start;
|
|
446
|
+
const isFix = /\bfix:\s*$/.test(src.slice(Math.max(0, start - 20), start));
|
|
447
|
+
const isNextArg = lastEnd !== -1 && /^\s*,\s*$/.test(src.slice(lastEnd, start));
|
|
448
|
+
if (!isFix && !isNextArg && !MESSAGE_START.test(parts[0]!)) continue;
|
|
449
|
+
lastEnd = group[group.length - 1]!.end;
|
|
450
|
+
const text = parts.map((p, h) => (h === 0 ? p : `\${${holes[h - 1]}}${p}`)).join("");
|
|
451
|
+
const fn = fns.filter((f) => f.at < group[0]!.start).pop()?.name ?? "";
|
|
452
|
+
out.push({ file, fn, text, parts });
|
|
453
|
+
}
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const escapeLiteral = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
458
|
+
|
|
459
|
+
/** One `### ` entry of `## Hook Text`; `texts` = its text block split at blank lines. */
|
|
460
|
+
interface HookEntry {
|
|
461
|
+
name: string;
|
|
462
|
+
file: string;
|
|
463
|
+
fn: string;
|
|
464
|
+
texts: string[];
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function parseHookEntries(md: string): HookEntry[] {
|
|
468
|
+
const body = sectionOf(md, "Hook Text") ?? "";
|
|
469
|
+
return body
|
|
470
|
+
.split(/^### /m)
|
|
471
|
+
.slice(1)
|
|
472
|
+
.map((section) => {
|
|
473
|
+
const name = section.slice(0, section.indexOf("\n")).trim();
|
|
474
|
+
const src = /^- source: `([^`]+)` `([^`]+)`$/m.exec(section);
|
|
475
|
+
const text = /^```text\n([\s\S]*?)\n```$/m.exec(section)?.[1] ?? "";
|
|
476
|
+
return { name, file: src?.[1] ?? "", fn: src?.[2] ?? "", texts: text.split("\n\n") };
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Findings for `## Hook Text` against the hook files (`file` → content), both ways: each
|
|
482
|
+
* message matches one text of an entry with its file and function (`${…}` on either side
|
|
483
|
+
* matches anything), and each entry text matches a message.
|
|
484
|
+
*/
|
|
485
|
+
export function checkHooks(store: string, sources: Map<string, string>): string[] {
|
|
486
|
+
const out: string[] = [];
|
|
487
|
+
const entries = parseHookEntries(store);
|
|
488
|
+
for (const e of entries) {
|
|
489
|
+
if (!e.file || !e.fn) out.push(`\`${e.name}\`: source must be \`{file}\` \`{function}\``);
|
|
490
|
+
}
|
|
491
|
+
const used = new Set<string>();
|
|
492
|
+
for (const [file, src] of sources) {
|
|
493
|
+
for (const m of hookMessages(src, file)) {
|
|
494
|
+
const re = new RegExp(`^${m.parts.map(escapeLiteral).join("[\\s\\S]*")}$`);
|
|
495
|
+
const hits = new Set<string>();
|
|
496
|
+
for (const e of entries) {
|
|
497
|
+
if (e.file !== file || e.fn !== m.fn) continue;
|
|
498
|
+
e.texts.forEach((t, i) => {
|
|
499
|
+
if (re.test(t)) {
|
|
500
|
+
hits.add(t);
|
|
501
|
+
used.add(`${e.name}\0${i}`);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
if (hits.size === 0) {
|
|
506
|
+
out.push(`${file} \`${m.fn}\`: no \`## Hook Text\` entry for ${JSON.stringify(m.text)}`);
|
|
507
|
+
} else if (hits.size > 1) {
|
|
508
|
+
out.push(
|
|
509
|
+
`${file} \`${m.fn}\`: ${hits.size} different entry texts match ${JSON.stringify(m.text)}`,
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
for (const e of entries) {
|
|
515
|
+
if (!e.file || !e.fn) continue;
|
|
516
|
+
e.texts.forEach((t, i) => {
|
|
517
|
+
if (!used.has(`${e.name}\0${i}`)) {
|
|
518
|
+
out.push(
|
|
519
|
+
`\`${e.name}\`: no longer emitted by \`${e.file}\` \`${e.fn}\` — ${JSON.stringify(t)}`,
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
return out;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// ---------------------------------------------------------------------------
|
|
528
|
+
// Permissions ← `.bkit/shared/.claude/settings.json` and `.bkit/shared/.opencode/opencode.json`
|
|
529
|
+
// ---------------------------------------------------------------------------
|
|
530
|
+
|
|
531
|
+
const TIERS = ["deny", "ask", "allow"] as const;
|
|
532
|
+
|
|
533
|
+
/** Findings for `## Permissions` against the shipped Claude settings and opencode config. */
|
|
534
|
+
export function checkPermissions(
|
|
535
|
+
store: string,
|
|
536
|
+
settings: { permissions?: Partial<Record<(typeof TIERS)[number], string[]>> },
|
|
537
|
+
opencode: { permission?: unknown },
|
|
538
|
+
): string[] {
|
|
539
|
+
const out: string[] = [];
|
|
540
|
+
const body = sectionOf(store, "Permissions") ?? "";
|
|
541
|
+
const groupsOf = new Map<string, string[]>();
|
|
542
|
+
const stored: Record<string, Map<string, string>> = {
|
|
543
|
+
deny: new Map(),
|
|
544
|
+
ask: new Map(),
|
|
545
|
+
allow: new Map(),
|
|
546
|
+
};
|
|
547
|
+
let opencodeSeen = false;
|
|
548
|
+
for (const section of body.split(/^### /m).slice(1)) {
|
|
549
|
+
const name = section.slice(0, section.indexOf("\n")).trim();
|
|
550
|
+
const tier = /^- tier: (.+)$/m.exec(section)?.[1] ?? "";
|
|
551
|
+
if (tier === "opencode `permission`") {
|
|
552
|
+
opencodeSeen = true;
|
|
553
|
+
const json = /^```json\n([\s\S]*?)\n```$/m.exec(section)?.[1] ?? "null";
|
|
554
|
+
if (JSON.stringify(JSON.parse(json)) !== JSON.stringify(opencode.permission ?? null)) {
|
|
555
|
+
out.push(
|
|
556
|
+
`\`${name}\`: differs from \`.bkit/shared/.opencode/opencode.json\` \`permission\``,
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
if (!(TIERS as readonly string[]).includes(tier)) {
|
|
562
|
+
out.push(`\`${name}\`: tier must be deny, ask, allow or opencode \`permission\``);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
const patterns = (/^```text\n([\s\S]*?)\n```$/m.exec(section)?.[1] ?? "")
|
|
566
|
+
.split("\n")
|
|
567
|
+
.filter((l) => l !== "");
|
|
568
|
+
const count = Number(/^- count: (\d+)$/m.exec(section)?.[1] ?? NaN);
|
|
569
|
+
if (count !== patterns.length)
|
|
570
|
+
out.push(`\`${name}\`: count ${count}, holds ${patterns.length}`);
|
|
571
|
+
for (const p of patterns) {
|
|
572
|
+
groupsOf.set(p, [...(groupsOf.get(p) ?? []), name]);
|
|
573
|
+
stored[tier]!.set(p, name);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
for (const [p, groups] of groupsOf) {
|
|
577
|
+
if (groups.length > 1) out.push(`\`${p}\` is in ${groups.length} groups: ${groups.join(", ")}`);
|
|
578
|
+
}
|
|
579
|
+
for (const tier of TIERS) {
|
|
580
|
+
const shipped = new Set(settings.permissions?.[tier] ?? []);
|
|
581
|
+
for (const p of shipped) {
|
|
582
|
+
if (!stored[tier]!.has(p)) out.push(`${tier}: \`${p}\` is shipped but in no group`);
|
|
583
|
+
}
|
|
584
|
+
for (const [p, group] of stored[tier]!) {
|
|
585
|
+
if (!shipped.has(p)) out.push(`${tier}: \`${p}\` is in \`${group}\` but not shipped`);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (!opencodeSeen) out.push("no `opencode-permission` group");
|
|
589
|
+
return out;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// ---------------------------------------------------------------------------
|
|
593
|
+
// Special Behaviors ← `.bkit/core/.bkit/prompts.md`; Authoring Rules ← `authoring.md`, `workflows.md`
|
|
594
|
+
// ---------------------------------------------------------------------------
|
|
595
|
+
|
|
596
|
+
/** Catalog files the two sections copy. */
|
|
597
|
+
export const CATALOG_SOURCES = [
|
|
598
|
+
".bkit/core/.bkit/prompts.md",
|
|
599
|
+
".bkit/core/.bkit/workflows.md",
|
|
600
|
+
".bkit/core/.bkit/authoring.md",
|
|
601
|
+
];
|
|
602
|
+
|
|
603
|
+
const FENCE_OPEN = /^(`{3,})/;
|
|
604
|
+
|
|
605
|
+
/** Per line: true when the line is a fence marker or inside a fenced block. */
|
|
606
|
+
function fencedLines(lines: string[]): boolean[] {
|
|
607
|
+
let fence = "";
|
|
608
|
+
return lines.map((l) => {
|
|
609
|
+
if (fence) {
|
|
610
|
+
if (l === fence) fence = "";
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
const m = FENCE_OPEN.exec(l);
|
|
614
|
+
if (m) fence = m[1]!;
|
|
615
|
+
return Boolean(m);
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Body of the heading named `heading` (any level), up to the next heading of the same or
|
|
621
|
+
* a higher level outside a fenced block; blank lines at both ends dropped.
|
|
622
|
+
*/
|
|
623
|
+
export function catalogSection(md: string, heading: string): string | undefined {
|
|
624
|
+
const lines = md.split("\n");
|
|
625
|
+
const fenced = fencedLines(lines);
|
|
626
|
+
const level = (l: string) => /^(#{1,6}) /.exec(l)?.[1]!.length ?? 0;
|
|
627
|
+
const at = lines.findIndex(
|
|
628
|
+
(l, i) => !fenced[i] && level(l) > 0 && l.replace(/^#+ /, "") === heading,
|
|
629
|
+
);
|
|
630
|
+
if (at === -1) return undefined;
|
|
631
|
+
const own = level(lines[at]!);
|
|
632
|
+
let end = at + 1;
|
|
633
|
+
while (
|
|
634
|
+
end < lines.length &&
|
|
635
|
+
!(level(lines[end]!) > 0 && !fenced[end] && level(lines[end]!) <= own)
|
|
636
|
+
)
|
|
637
|
+
end++;
|
|
638
|
+
const body = lines.slice(at + 1, end);
|
|
639
|
+
while (body.length > 0 && body[0]!.trim() === "") body.shift();
|
|
640
|
+
while (body.length > 0 && body[body.length - 1]!.trim() === "") body.pop();
|
|
641
|
+
return body.join("\n");
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** Cells of each `| … |` row of a section's table, header and separator dropped. */
|
|
645
|
+
function tableRows(body: string): string[][] {
|
|
646
|
+
return body
|
|
647
|
+
.split("\n")
|
|
648
|
+
.filter((l) => l.startsWith("| ") && l.endsWith(" |"))
|
|
649
|
+
.map((l) => l.slice(2, -2).split(" | "))
|
|
650
|
+
.slice(2);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
/** One `### ` entry of a store section, with the line indexes sync rewrites. */
|
|
654
|
+
interface CatalogEntry {
|
|
655
|
+
name: string;
|
|
656
|
+
keys: Map<string, { value: string; line: number }>;
|
|
657
|
+
file: string;
|
|
658
|
+
heading: string;
|
|
659
|
+
text: string;
|
|
660
|
+
/** Indexes of the text block's opening and closing fence lines. */
|
|
661
|
+
open: number;
|
|
662
|
+
close: number;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
function catalogEntries(lines: string[], section: string): CatalogEntry[] {
|
|
666
|
+
const fenced = fencedLines(lines);
|
|
667
|
+
const head = lines.findIndex((l, i) => !fenced[i] && l === `## ${section}`);
|
|
668
|
+
if (head === -1) return [];
|
|
669
|
+
const out: CatalogEntry[] = [];
|
|
670
|
+
let cur: CatalogEntry | undefined;
|
|
671
|
+
for (let i = head + 1; i < lines.length; i++) {
|
|
672
|
+
const l = lines[i]!;
|
|
673
|
+
if (!fenced[i] && l.startsWith("## ")) break;
|
|
674
|
+
if (!fenced[i] && l.startsWith("### ")) {
|
|
675
|
+
cur = {
|
|
676
|
+
name: l.slice(4).trim(),
|
|
677
|
+
keys: new Map(),
|
|
678
|
+
file: "",
|
|
679
|
+
heading: "",
|
|
680
|
+
text: "",
|
|
681
|
+
open: -1,
|
|
682
|
+
close: -1,
|
|
683
|
+
};
|
|
684
|
+
out.push(cur);
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
if (!cur) continue;
|
|
688
|
+
const key = /^- ([a-z]+): (.+)$/.exec(l);
|
|
689
|
+
if (key && !fenced[i]) {
|
|
690
|
+
cur.keys.set(key[1]!, { value: key[2]!, line: i });
|
|
691
|
+
const src = /^`([^`]+)` § (.+)$/.exec(key[2]!);
|
|
692
|
+
if (key[1] === "source" && src) [cur.file, cur.heading] = [src[1]!, src[2]!];
|
|
693
|
+
}
|
|
694
|
+
const open = /^(`{3,})text$/.exec(l);
|
|
695
|
+
if (open && cur.open === -1) {
|
|
696
|
+
cur.open = i;
|
|
697
|
+
cur.close = lines.indexOf(open[1]!, i + 1);
|
|
698
|
+
cur.text = lines.slice(i + 1, cur.close).join("\n");
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return out;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const BEHAVIORS = "Special Behaviors";
|
|
705
|
+
const AUTHORING = "Authoring Rules";
|
|
706
|
+
|
|
707
|
+
interface Want {
|
|
708
|
+
entry: CatalogEntry;
|
|
709
|
+
text?: string;
|
|
710
|
+
keys: Record<string, string>;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/** What each entry should hold, and findings for rows and sections without one. */
|
|
714
|
+
function catalogWants(
|
|
715
|
+
lines: string[],
|
|
716
|
+
catalogs: Map<string, string>,
|
|
717
|
+
): { wants: Want[]; out: string[] } {
|
|
718
|
+
const out: string[] = [];
|
|
719
|
+
const wants: Want[] = [];
|
|
720
|
+
const behaviors = catalogEntries(lines, BEHAVIORS);
|
|
721
|
+
const byTable = new Map<string, CatalogEntry[]>();
|
|
722
|
+
for (const e of behaviors) {
|
|
723
|
+
const k = `${e.file}\0${e.heading}`;
|
|
724
|
+
byTable.set(k, [...(byTable.get(k) ?? []), e]);
|
|
725
|
+
}
|
|
726
|
+
for (const [k, entries] of byTable) {
|
|
727
|
+
const [file, heading] = k.split("\0") as [string, string];
|
|
728
|
+
const body = catalogs.has(file) ? catalogSection(catalogs.get(file)!, heading) : undefined;
|
|
729
|
+
if (body === undefined) {
|
|
730
|
+
out.push(`\`${file}\` § ${heading}: not found`);
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
const rows = tableRows(body);
|
|
734
|
+
rows.forEach((row, i) => {
|
|
735
|
+
const e = entries[i];
|
|
736
|
+
if (!e) out.push(`${row[0]}: no \`## ${BEHAVIORS}\` entry`);
|
|
737
|
+
else
|
|
738
|
+
wants.push({
|
|
739
|
+
entry: e,
|
|
740
|
+
text: row[2] ?? "",
|
|
741
|
+
keys: { behavior: row[0] ?? "", agents: row[1] ?? "" },
|
|
742
|
+
});
|
|
743
|
+
});
|
|
744
|
+
for (const e of entries.slice(rows.length))
|
|
745
|
+
out.push(`\`${e.name}\`: no row in \`${file}\` § ${heading}`);
|
|
746
|
+
}
|
|
747
|
+
for (const e of catalogEntries(lines, AUTHORING)) {
|
|
748
|
+
const body = catalogs.has(e.file)
|
|
749
|
+
? catalogSection(catalogs.get(e.file)!, e.heading)
|
|
750
|
+
: undefined;
|
|
751
|
+
if (body === undefined) out.push(`\`${e.name}\`: \`${e.file}\` § ${e.heading} not found`);
|
|
752
|
+
else wants.push({ entry: e, text: body, keys: {} });
|
|
753
|
+
}
|
|
754
|
+
for (const e of behaviors) {
|
|
755
|
+
if (!e.file) out.push(`\`${e.name}\`: source must be \`{file}\` § {heading}`);
|
|
756
|
+
}
|
|
757
|
+
return { wants, out };
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** Findings for `## Special Behaviors` and `## Authoring Rules` against the catalogs (`file` → content). */
|
|
761
|
+
export function checkCatalogs(store: string, catalogs: Map<string, string>): string[] {
|
|
762
|
+
const { wants, out } = catalogWants(store.split("\n"), catalogs);
|
|
763
|
+
for (const w of wants) {
|
|
764
|
+
for (const [k, v] of Object.entries(w.keys)) {
|
|
765
|
+
if (w.entry.keys.get(k)?.value !== v) out.push(`\`${w.entry.name}\`: ${k} differs`);
|
|
766
|
+
}
|
|
767
|
+
if (w.entry.text !== w.text) {
|
|
768
|
+
out.push(
|
|
769
|
+
`\`${w.entry.name}\`: ${Object.keys(w.keys).length > 0 ? "notes" : "text"} differ${Object.keys(w.keys).length > 0 ? "" : "s"} — run pnpm instructions:sync`,
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
return out;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/** The store with each Special Behaviors and Authoring Rules copy refreshed from its catalog. */
|
|
777
|
+
export function syncCatalogs(store: string, catalogs: Map<string, string>): string {
|
|
778
|
+
const lines = store.split("\n");
|
|
779
|
+
const { wants } = catalogWants(lines, catalogs);
|
|
780
|
+
// Bottom-up, so earlier indexes stay valid.
|
|
781
|
+
wants.sort((a, b) => b.entry.open - a.entry.open);
|
|
782
|
+
for (const w of wants) {
|
|
783
|
+
const text = w.text ?? "";
|
|
784
|
+
if (w.entry.open !== -1 && w.entry.text !== text) {
|
|
785
|
+
const fence = text.includes("```") ? "````" : "```";
|
|
786
|
+
lines.splice(
|
|
787
|
+
w.entry.open,
|
|
788
|
+
w.entry.close - w.entry.open + 1,
|
|
789
|
+
`${fence}text`,
|
|
790
|
+
...text.split("\n"),
|
|
791
|
+
fence,
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
for (const [k, v] of Object.entries(w.keys)) {
|
|
795
|
+
const at = w.entry.keys.get(k);
|
|
796
|
+
if (at && at.value !== v) lines[at.line] = `- ${k}: ${v}`;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return lines.join("\n");
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// ---------------------------------------------------------------------------
|
|
803
|
+
// Every copied section, read from `root`
|
|
804
|
+
// ---------------------------------------------------------------------------
|
|
805
|
+
|
|
806
|
+
const readAt = (root: string, rel: string) => fs.readFileSync(path.join(root, rel), "utf-8");
|
|
807
|
+
|
|
808
|
+
export function ruleMasters(root: string): RuleMasters {
|
|
809
|
+
return {
|
|
810
|
+
bkit: readAt(root, ".bkit/core/.agents/rules.md"),
|
|
811
|
+
project: readAt(root, ".agents/rules.md"),
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
const catalogsAt = (root: string) => new Map(CATALOG_SOURCES.map((f) => [f, readAt(root, f)]));
|
|
816
|
+
|
|
817
|
+
/** Findings for every store section that copies a master, read from `root`. */
|
|
818
|
+
export function checkSources(root: string, store: string): string[] {
|
|
819
|
+
return [
|
|
820
|
+
...checkRules(store, ruleMasters(root), (ref, section) => refExists(root, ref, section)),
|
|
821
|
+
...checkHooks(store, new Map(HOOK_SOURCES.map((f) => [f, readAt(root, f)]))),
|
|
822
|
+
...checkPermissions(
|
|
823
|
+
store,
|
|
824
|
+
JSON.parse(readAt(root, ".bkit/shared/.claude/settings.json")),
|
|
825
|
+
JSON.parse(readAt(root, ".bkit/shared/.opencode/opencode.json")),
|
|
826
|
+
),
|
|
827
|
+
...checkCatalogs(store, catalogsAt(root)),
|
|
828
|
+
];
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/** The store with every generated copy refreshed from its master. */
|
|
832
|
+
export function syncSources(root: string, store: string): string {
|
|
833
|
+
return syncCatalogs(syncRules(store, ruleMasters(root)), catalogsAt(root));
|
|
834
|
+
}
|