@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,1751 @@
|
|
|
1
|
+
// tools/instructions.ts — check and sync `.bkit/instructions.md`, the store of every
|
|
2
|
+
// instruction bkit gives an agent.
|
|
3
|
+
//
|
|
4
|
+
// Check mode runs in the test suite (`tools/instructions.test.ts`); write mode is
|
|
5
|
+
// `pnpm instructions:sync`, lookup is `pnpm instructions:show`. Same model as prettier:
|
|
6
|
+
// CI checks, the author writes.
|
|
7
|
+
//
|
|
8
|
+
// `## Clauses` holds the shared sentences.
|
|
9
|
+
// The unit is the sentence. A clause is matched as a substring of a line, so a
|
|
10
|
+
// list marker, step number, or trailing local text around it never matters.
|
|
11
|
+
// Clause instances carry no markers in the prompts: sync finds them by
|
|
12
|
+
// near-copy similarity and by the clause's `retired` wordings, which is how a
|
|
13
|
+
// wording change reaches every file — edit the clause, move the old text to
|
|
14
|
+
// `retired`, run the sync.
|
|
15
|
+
|
|
16
|
+
import * as fs from "node:fs";
|
|
17
|
+
import * as path from "node:path";
|
|
18
|
+
import { fileURLToPath } from "node:url";
|
|
19
|
+
|
|
20
|
+
import { findLineAnchoredMarkers, selectMarkers } from "../install/managed-block.js";
|
|
21
|
+
import {
|
|
22
|
+
checkSources,
|
|
23
|
+
parseDriftItem,
|
|
24
|
+
parseRuleEntries,
|
|
25
|
+
sectionOf,
|
|
26
|
+
syncSources,
|
|
27
|
+
type RuleEntry,
|
|
28
|
+
} from "./instructions-sources.js";
|
|
29
|
+
|
|
30
|
+
export { sectionOf };
|
|
31
|
+
|
|
32
|
+
export type Surface =
|
|
33
|
+
"prompt" | "workflow" | "rules" | "audit" | "repo-agent" | "plan-template" | "entry" | "doc";
|
|
34
|
+
export type Mode =
|
|
35
|
+
| "required"
|
|
36
|
+
| "where-used"
|
|
37
|
+
| "repeatable"
|
|
38
|
+
| "forbidden"
|
|
39
|
+
| "if-verdict"
|
|
40
|
+
| "if-bash"
|
|
41
|
+
| "if-search"
|
|
42
|
+
| "if-no-search";
|
|
43
|
+
export type Position = "first";
|
|
44
|
+
/** Header block a placed clause lives in: `**Protocol:**`, `**Contract:**`, `**Rules:**`, in that order. */
|
|
45
|
+
export type Block = "protocol" | "contract" | "rules";
|
|
46
|
+
|
|
47
|
+
export interface Clause {
|
|
48
|
+
name: string;
|
|
49
|
+
/** Canonical wordings; more than one = exactly one of them is used. */
|
|
50
|
+
texts: string[];
|
|
51
|
+
/** Earlier wordings: never valid, and sync rewrites them to `texts[0]`. */
|
|
52
|
+
retired: string[];
|
|
53
|
+
/** Every wording retired with no replacement: a record only, with no text and no surface, so no check, sync, or cut-off sees it. */
|
|
54
|
+
fullyRetired?: boolean;
|
|
55
|
+
modes: Partial<Record<Surface, Mode>>;
|
|
56
|
+
/** File basenames (no `.md`) that word this clause differently. */
|
|
57
|
+
except: Set<string>;
|
|
58
|
+
/** Header block the clause's line sits in (prompts, workflows, repo agents); placed clauses keep wording-file order inside their block. */
|
|
59
|
+
block?: Block;
|
|
60
|
+
/** `first` = the first bullet of its block. */
|
|
61
|
+
position?: Position;
|
|
62
|
+
/** Slotted clause only: a line holding the fixed text before the first slot must match one of the templates. */
|
|
63
|
+
anchored: boolean;
|
|
64
|
+
/** `R{nn}` of the `.agents/rules.md` rule the clause carries. */
|
|
65
|
+
rule?: string;
|
|
66
|
+
/** Rules the clause condenses, specialises, applies or cites (`- tags:`). */
|
|
67
|
+
tags?: string[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface CorpusFile {
|
|
71
|
+
surface: Surface;
|
|
72
|
+
/** Repo-relative path. */
|
|
73
|
+
id: string;
|
|
74
|
+
/** Basename without `.md`, matched against `except`. */
|
|
75
|
+
name: string;
|
|
76
|
+
content: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface Finding {
|
|
80
|
+
file: string;
|
|
81
|
+
line?: number;
|
|
82
|
+
clause: string;
|
|
83
|
+
problem: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Near-copy threshold: a sentence this similar to a clause but not equal to it is drift. */
|
|
87
|
+
export const NEAR_COPY = 0.8;
|
|
88
|
+
/** Cut-off per surface: a sentence shared by this many files of one surface belongs in the wording file. */
|
|
89
|
+
export const SHARED_MIN: Partial<Record<Surface, number>> = {
|
|
90
|
+
prompt: 2,
|
|
91
|
+
workflow: 2,
|
|
92
|
+
audit: 5,
|
|
93
|
+
doc: 2,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** Surfaces a clause can set a mode for. */
|
|
97
|
+
const SURFACES: readonly Surface[] = ["prompt", "workflow", "rules", "audit", "doc"];
|
|
98
|
+
/** Surfaces a clause can set a mode for: the shared-sentence surfaces plus the hand-authored repo agents. */
|
|
99
|
+
const MODE_SURFACES: readonly Surface[] = [...SURFACES, "repo-agent"];
|
|
100
|
+
/** Surfaces whose files open with the Protocol / Contract / Rules header blocks. */
|
|
101
|
+
const HEADED: readonly Surface[] = ["prompt", "workflow", "repo-agent"];
|
|
102
|
+
/** Surfaces checked inside the `bkit:start` / `bkit:end` block only. */
|
|
103
|
+
const MANAGED: readonly Surface[] = ["rules", "audit", "plan-template", "doc"];
|
|
104
|
+
/** Files a surface's mode and cut-off reach: `doc` also takes the plan template and AGENTS.md. */
|
|
105
|
+
export const POOL: Partial<Record<Surface, readonly Surface[]>> = {
|
|
106
|
+
doc: ["doc", "plan-template", "entry"],
|
|
107
|
+
};
|
|
108
|
+
/** The surface whose mode and cut-off apply to a file of `surface`. */
|
|
109
|
+
export const poolOf = (surface: Surface): Surface =>
|
|
110
|
+
(Object.keys(POOL) as Surface[]).find((p) => POOL[p]!.includes(surface)) ?? surface;
|
|
111
|
+
const NOUN: Partial<Record<Surface, string>> = {
|
|
112
|
+
prompt: "prompts",
|
|
113
|
+
workflow: "workflows",
|
|
114
|
+
audit: "audit templates",
|
|
115
|
+
doc: "docs",
|
|
116
|
+
};
|
|
117
|
+
const MODES: readonly Mode[] = [
|
|
118
|
+
"required",
|
|
119
|
+
"where-used",
|
|
120
|
+
"repeatable",
|
|
121
|
+
"forbidden",
|
|
122
|
+
"if-verdict",
|
|
123
|
+
"if-bash",
|
|
124
|
+
"if-search",
|
|
125
|
+
"if-no-search",
|
|
126
|
+
];
|
|
127
|
+
const POSITIONS: readonly Position[] = ["first"];
|
|
128
|
+
export const BLOCKS: readonly Block[] = ["protocol", "contract", "rules"];
|
|
129
|
+
/** Label line opening each header block, lead-in after it. */
|
|
130
|
+
export const BLOCK_LABEL: Record<Block, string> = {
|
|
131
|
+
protocol: "**Protocol:**",
|
|
132
|
+
contract: "**Contract:**",
|
|
133
|
+
rules: "**Rules:**",
|
|
134
|
+
};
|
|
135
|
+
const VERDICT_END = "<!-- verdict:end -->";
|
|
136
|
+
const SLOT = /\{\{([A-Za-z][\w-]*)(\?)?\}\}/g;
|
|
137
|
+
const LABEL = /^\*\*[^*]+:\*\* /;
|
|
138
|
+
/** Structure, not sentences: headings, HTML comments, table rows. Never counted toward the cut-off. */
|
|
139
|
+
const STRUCTURE = /^(?:#{1,6} |<!--|\|)/;
|
|
140
|
+
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
// Wording file
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
|
|
145
|
+
export interface Instructions {
|
|
146
|
+
clauses: Clause[];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Parse the store: each `## ` section by its own parser. */
|
|
150
|
+
export function parseInstructions(md: string): Instructions {
|
|
151
|
+
return { clauses: parseClauses(md) };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Parse the `## Clauses` section: one `### name` section per clause. */
|
|
155
|
+
export function parseClauses(md: string): Clause[] {
|
|
156
|
+
const body = sectionOf(md, "Clauses");
|
|
157
|
+
if (body === undefined) throw new Error("[bkit-instructions] no `## Clauses` section");
|
|
158
|
+
const clauses: Clause[] = [];
|
|
159
|
+
for (const section of body.split(/^### /m).slice(1)) {
|
|
160
|
+
const name = section.slice(0, section.indexOf("\n")).trim();
|
|
161
|
+
const clause: Clause = {
|
|
162
|
+
name,
|
|
163
|
+
texts: [],
|
|
164
|
+
retired: [],
|
|
165
|
+
modes: {},
|
|
166
|
+
except: new Set(),
|
|
167
|
+
anchored: false,
|
|
168
|
+
};
|
|
169
|
+
for (const m of section.matchAll(/^- ([a-z-]+): (.+)$/gm)) {
|
|
170
|
+
const [key, value] = [m[1]!, m[2]!.trim()];
|
|
171
|
+
if ((MODE_SURFACES as readonly string[]).includes(key)) {
|
|
172
|
+
if (!(MODES as readonly string[]).includes(value)) {
|
|
173
|
+
throw new Error(`[bkit-instructions] ${name}: unknown mode "${value}" for ${key}`);
|
|
174
|
+
}
|
|
175
|
+
clause.modes[key as Surface] = value as Mode;
|
|
176
|
+
} else if (key === "except") {
|
|
177
|
+
for (const f of value.split(",")) clause.except.add(f.trim());
|
|
178
|
+
} else if (key === "position") {
|
|
179
|
+
if (!(POSITIONS as readonly string[]).includes(value)) {
|
|
180
|
+
throw new Error(`[bkit-instructions] ${name}: position must be first`);
|
|
181
|
+
}
|
|
182
|
+
clause.position = value as Position;
|
|
183
|
+
} else if (key === "block") {
|
|
184
|
+
if (!(BLOCKS as readonly string[]).includes(value)) {
|
|
185
|
+
throw new Error(
|
|
186
|
+
`[bkit-instructions] ${name}: block must be protocol, contract, or rules`,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
clause.block = value as Block;
|
|
190
|
+
} else if (key === "anchored") {
|
|
191
|
+
clause.anchored = value === "yes";
|
|
192
|
+
} else if (key === "retired") {
|
|
193
|
+
clause.fullyRetired = value === "yes";
|
|
194
|
+
} else if (key === "rule") {
|
|
195
|
+
clause.rule = /^[RP]\d+/.exec(value)?.[0];
|
|
196
|
+
} else if (key === "tags") {
|
|
197
|
+
clause.tags = value.split(",").map((t) => t.trim());
|
|
198
|
+
} else if (key === "carriers" || key === "distinct-from") {
|
|
199
|
+
continue;
|
|
200
|
+
} else {
|
|
201
|
+
throw new Error(`[bkit-instructions] ${name}: unknown key "${key}"`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
for (const m of section.matchAll(/^```(text|retired)\n([\s\S]*?)^```$/gm)) {
|
|
205
|
+
const lines = m[2]!.split("\n").filter((l) => l.trim() !== "");
|
|
206
|
+
if (m[1] === "text") clause.texts.push(...lines);
|
|
207
|
+
else clause.retired.push(...lines);
|
|
208
|
+
}
|
|
209
|
+
if (clause.fullyRetired) {
|
|
210
|
+
if (clause.texts.length > 0) {
|
|
211
|
+
throw new Error(`[bkit-instructions] ${name}: a fully retired clause has no text block`);
|
|
212
|
+
}
|
|
213
|
+
if (Object.keys(clause.modes).length > 0) {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`[bkit-instructions] ${name}: a fully retired clause applies to no surface`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
clauses.push(clause);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (clause.texts.length === 0) throw new Error(`[bkit-instructions] ${name}: no text block`);
|
|
222
|
+
if (clause.position && !clause.block) {
|
|
223
|
+
throw new Error(`[bkit-instructions] ${name}: position needs a block`);
|
|
224
|
+
}
|
|
225
|
+
if (Object.keys(clause.modes).length === 0) {
|
|
226
|
+
throw new Error(`[bkit-instructions] ${name}: applies to no surface`);
|
|
227
|
+
}
|
|
228
|
+
clauses.push(clause);
|
|
229
|
+
}
|
|
230
|
+
return clauses;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ---------------------------------------------------------------------------
|
|
234
|
+
// Corpus
|
|
235
|
+
// ---------------------------------------------------------------------------
|
|
236
|
+
|
|
237
|
+
const RULES_FILES = [".bkit/core/.agents/rules.md", ".agents/rules.md"];
|
|
238
|
+
/** Audit templates, then the repo's adopted copies (same basenames, same managed block). */
|
|
239
|
+
const AUDIT_DIRS = [".bkit/core/docs/templates", "docs/templates"];
|
|
240
|
+
const AUDIT_FILE = /^bkit-audit-.+\.md$/;
|
|
241
|
+
/** Hand-authored repo-only agents. */
|
|
242
|
+
const REPO_AGENT_DIR = ".claude/agents";
|
|
243
|
+
const REPO_AGENT_FILE = /^utility-.+\.md$/;
|
|
244
|
+
const PLAN_TEMPLATE = ".bkit/core/docs/templates/bkit-plan.md";
|
|
245
|
+
const ENTRY_FILE = ".bkit/core/AGENTS.md";
|
|
246
|
+
/**
|
|
247
|
+
* Shipped docs with a `bkit:start` / `bkit:end` block, masters only: the installed
|
|
248
|
+
* copies at the repo root are rewritten from these by `bkit upgrade`.
|
|
249
|
+
*/
|
|
250
|
+
const DOC_DIRS = [
|
|
251
|
+
".bkit/core/.agents",
|
|
252
|
+
".bkit/core/.bkit",
|
|
253
|
+
".bkit/shared",
|
|
254
|
+
".bkit/shared/docs",
|
|
255
|
+
".bkit/shared/.cursor/rules",
|
|
256
|
+
];
|
|
257
|
+
const DOC_FILE = /\.mdc?$/;
|
|
258
|
+
const BLOCK_START = /^<!-- bkit:start -->$/m;
|
|
259
|
+
|
|
260
|
+
/** Every file the wording file governs, read from `root`. */
|
|
261
|
+
export function loadCorpus(root: string): CorpusFile[] {
|
|
262
|
+
const files: CorpusFile[] = [];
|
|
263
|
+
for (const [surface, dir] of [
|
|
264
|
+
["prompt", path.join(".bkit", "core", "prompts")],
|
|
265
|
+
["workflow", path.join(".bkit", "core", "workflows")],
|
|
266
|
+
] as const) {
|
|
267
|
+
for (const f of fs
|
|
268
|
+
.readdirSync(path.join(root, dir))
|
|
269
|
+
.filter((n) => n.endsWith(".md"))
|
|
270
|
+
.sort()) {
|
|
271
|
+
const id = path.join(dir, f);
|
|
272
|
+
files.push({
|
|
273
|
+
surface,
|
|
274
|
+
id,
|
|
275
|
+
name: f.slice(0, -3),
|
|
276
|
+
content: fs.readFileSync(path.join(root, id), "utf-8"),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
for (const dir of AUDIT_DIRS) {
|
|
281
|
+
if (!fs.existsSync(path.join(root, dir))) continue;
|
|
282
|
+
for (const f of fs
|
|
283
|
+
.readdirSync(path.join(root, dir))
|
|
284
|
+
.filter((n) => AUDIT_FILE.test(n))
|
|
285
|
+
.sort()) {
|
|
286
|
+
const id = path.join(dir, f);
|
|
287
|
+
files.push({
|
|
288
|
+
surface: "audit",
|
|
289
|
+
id,
|
|
290
|
+
name: f.slice(0, -3),
|
|
291
|
+
content: fs.readFileSync(path.join(root, id), "utf-8"),
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
for (const id of RULES_FILES) {
|
|
296
|
+
if (!fs.existsSync(path.join(root, id))) continue;
|
|
297
|
+
files.push({
|
|
298
|
+
surface: "rules",
|
|
299
|
+
id,
|
|
300
|
+
name: "rules",
|
|
301
|
+
content: fs.readFileSync(path.join(root, id), "utf-8"),
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
const taken = new Set([...RULES_FILES, PLAN_TEMPLATE, ENTRY_FILE]);
|
|
305
|
+
for (const dir of DOC_DIRS) {
|
|
306
|
+
if (!fs.existsSync(path.join(root, dir))) continue;
|
|
307
|
+
for (const f of fs
|
|
308
|
+
.readdirSync(path.join(root, dir))
|
|
309
|
+
.filter((n) => DOC_FILE.test(n))
|
|
310
|
+
.sort()) {
|
|
311
|
+
const id = path.join(dir, f);
|
|
312
|
+
if (taken.has(id)) continue;
|
|
313
|
+
const content = fs.readFileSync(path.join(root, id), "utf-8");
|
|
314
|
+
if (BLOCK_START.test(content))
|
|
315
|
+
files.push({ surface: "doc", id, name: f.replace(DOC_FILE, ""), content });
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
const agentDir = path.join(root, REPO_AGENT_DIR);
|
|
319
|
+
const agents = fs.existsSync(agentDir)
|
|
320
|
+
? fs
|
|
321
|
+
.readdirSync(agentDir)
|
|
322
|
+
.filter((n) => REPO_AGENT_FILE.test(n))
|
|
323
|
+
.sort()
|
|
324
|
+
: [];
|
|
325
|
+
const singles: [Surface, string][] = [
|
|
326
|
+
...agents.map((f): [Surface, string] => ["repo-agent", path.join(REPO_AGENT_DIR, f)]),
|
|
327
|
+
["plan-template", PLAN_TEMPLATE],
|
|
328
|
+
["entry", ENTRY_FILE],
|
|
329
|
+
];
|
|
330
|
+
for (const [surface, id] of singles) {
|
|
331
|
+
if (!fs.existsSync(path.join(root, id))) continue;
|
|
332
|
+
files.push({
|
|
333
|
+
surface,
|
|
334
|
+
id,
|
|
335
|
+
name: path.basename(id, ".md"),
|
|
336
|
+
content: fs.readFileSync(path.join(root, id), "utf-8"),
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
return files;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Indexes of the lines that are prose the clauses govern. Prompts and workflows:
|
|
344
|
+
* everything but front matter and fenced blocks (a fenced example is
|
|
345
|
+
* documentation, not instruction). Rules and audit templates: only the
|
|
346
|
+
* bkit-managed block — the consumer-owned part below `bkit:end` is never bkit's
|
|
347
|
+
* to check or rewrite.
|
|
348
|
+
*/
|
|
349
|
+
export function proseLineIndexes(file: CorpusFile): number[] {
|
|
350
|
+
const lines = file.content.split("\n");
|
|
351
|
+
let from = 0;
|
|
352
|
+
let to = lines.length;
|
|
353
|
+
if (MANAGED.includes(file.surface)) {
|
|
354
|
+
const [startMarker, endMarker] = selectMarkers("html-comment");
|
|
355
|
+
const starts = findLineAnchoredMarkers(file.content, startMarker);
|
|
356
|
+
const ends = findLineAnchoredMarkers(file.content, endMarker);
|
|
357
|
+
if (starts.length !== 1 || ends.length !== 1) return [];
|
|
358
|
+
from = file.content.slice(0, starts[0]).split("\n").length;
|
|
359
|
+
to = file.content.slice(0, ends[0]).split("\n").length - 1;
|
|
360
|
+
} else if (lines[0] === "---") {
|
|
361
|
+
const close = lines.indexOf("---", 1);
|
|
362
|
+
if (close !== -1) from = close + 1;
|
|
363
|
+
}
|
|
364
|
+
const out: number[] = [];
|
|
365
|
+
let fence: string | null = null;
|
|
366
|
+
for (let i = from; i < to; i++) {
|
|
367
|
+
const line = lines[i]!;
|
|
368
|
+
if (fence !== null) {
|
|
369
|
+
if (new RegExp(`^[ \\t]*${fence}[ \\t]*$`).test(line)) fence = null;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const open = /^[ \t]*(`{3,})[\w-]*[ \t]*$/.exec(line);
|
|
373
|
+
if (open) {
|
|
374
|
+
fence = open[1]!;
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
out.push(i);
|
|
378
|
+
}
|
|
379
|
+
return out;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Prose lines a clause can match: in a prompt, every prose line outside the
|
|
384
|
+
* `**Rules:**` block, whose lines are rules.md copies the `- carry:` check owns.
|
|
385
|
+
*/
|
|
386
|
+
export function clauseLineIndexes(file: CorpusFile): number[] {
|
|
387
|
+
const prose = proseLineIndexes(file);
|
|
388
|
+
const block = file.surface === "prompt" ? ruleBlock(file.content.split("\n")) : undefined;
|
|
389
|
+
return block ? prose.filter((i) => i < block.from || i >= block.to) : prose;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** Lines a clause is matched against: a `block: rules` clause also sees the prompt's `**Rules:**` block it sits in. */
|
|
393
|
+
function linesFor(clause: Clause, file: CorpusFile): number[] {
|
|
394
|
+
return clause.block === "rules" ? proseLineIndexes(file) : clauseLineIndexes(file);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** A column-0 `**Label:**` line, alone or with text after it. */
|
|
398
|
+
const LABEL_LINE = /^\*\*[^*]+:\*\*(?: |$)/;
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Line range of each header block: its label line up to the next column-0
|
|
402
|
+
* `**Label:**` line, `## ` heading or `---`. A block whose label is missing, or
|
|
403
|
+
* appears more than once, has no range.
|
|
404
|
+
*/
|
|
405
|
+
export function blockRanges(content: string): Partial<Record<Block, { from: number; to: number }>> {
|
|
406
|
+
const lines = content.split("\n");
|
|
407
|
+
const out: Partial<Record<Block, { from: number; to: number }>> = {};
|
|
408
|
+
for (const b of BLOCKS) {
|
|
409
|
+
const at = lines.flatMap((l, i) => (l.startsWith(BLOCK_LABEL[b]) ? [i] : []));
|
|
410
|
+
if (at.length !== 1) continue;
|
|
411
|
+
const from = at[0]!;
|
|
412
|
+
const end = lines.findIndex(
|
|
413
|
+
(l, i) => i > from && (LABEL_LINE.test(l) || l.startsWith("## ") || l === "---"),
|
|
414
|
+
);
|
|
415
|
+
out[b] = { from, to: end === -1 ? lines.length : end };
|
|
416
|
+
}
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Line range of a prompt's `**Rules:**` block. */
|
|
421
|
+
function ruleBlock(lines: string[]): { from: number; to: number } | undefined {
|
|
422
|
+
return blockRanges(lines.join("\n")).rules;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** Words that make a paragraph line an instruction. */
|
|
426
|
+
const STANDING_WORD = /\b(?:NEVER|ALWAYS|MUST|ONLY|NOT|STOP|CRITICAL|Do not)\b|→|⛔/;
|
|
427
|
+
const LIST_ITEM = /^[ \t]*(?:[-*+]|\d+\.)[ \t]+(?:\[[ xX]\][ \t]+)?/;
|
|
428
|
+
const QUOTE_BULLET = /^[ \t]*>[ \t]*[-*+][ \t]+/;
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The standing instructions of a file, 1-based line and text: list items and blockquote
|
|
432
|
+
* bullets (marker, step number and checkbox dropped), and paragraph lines carrying
|
|
433
|
+
* `NEVER` / `ALWAYS` / `MUST` / `ONLY` / `NOT` / `STOP` / `CRITICAL` / `Do not` / `→` /
|
|
434
|
+
* `⛔` or opening with a `**Label:**`. Headings, table rows, HTML comments, fenced
|
|
435
|
+
* blocks and front matter are dropped; managed surfaces: the `bkit:start` / `bkit:end`
|
|
436
|
+
* block only.
|
|
437
|
+
*/
|
|
438
|
+
export function standingLines(file: CorpusFile): { line: number; text: string }[] {
|
|
439
|
+
const lines = file.content.split("\n");
|
|
440
|
+
const out: { line: number; text: string }[] = [];
|
|
441
|
+
for (const i of proseLineIndexes(file)) {
|
|
442
|
+
const raw = lines[i]!;
|
|
443
|
+
if (raw.trim() === "" || /^[ \t]*(?:#{1,6} |\||<!--)/.test(raw)) continue;
|
|
444
|
+
const item = LIST_ITEM.exec(raw) ?? QUOTE_BULLET.exec(raw);
|
|
445
|
+
if (item) {
|
|
446
|
+
const text = raw.slice(item[0].length).trim();
|
|
447
|
+
if (text !== "" && !text.startsWith("<!--")) out.push({ line: i + 1, text });
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
const text = raw.replace(/^[ \t]*>[ \t]?/, "").trim();
|
|
451
|
+
if (text === "" || text.startsWith("[!")) continue;
|
|
452
|
+
if (STANDING_WORD.test(text) || LABEL.test(text)) out.push({ line: i + 1, text });
|
|
453
|
+
}
|
|
454
|
+
return out;
|
|
455
|
+
}
|
|
456
|
+
/** Tool names in the front matter `tools:` list, inline (`[a, b]`) or block (`- a`) form. */
|
|
457
|
+
export function toolsOf(content: string): string[] {
|
|
458
|
+
const lines = content.split("\n");
|
|
459
|
+
if (lines[0] !== "---") return [];
|
|
460
|
+
const close = lines.indexOf("---", 1);
|
|
461
|
+
const front = lines.slice(1, close === -1 ? 1 : close);
|
|
462
|
+
const at = front.findIndex((l) => /^tools:/.test(l));
|
|
463
|
+
if (at === -1) return [];
|
|
464
|
+
const inline = /^tools:[ \t]*\[(.*)\][ \t]*$/.exec(front[at]!);
|
|
465
|
+
if (inline) {
|
|
466
|
+
return inline[1]!
|
|
467
|
+
.split(",")
|
|
468
|
+
.map((t) => t.trim())
|
|
469
|
+
.filter(Boolean);
|
|
470
|
+
}
|
|
471
|
+
const tools: string[] = [];
|
|
472
|
+
for (const line of front.slice(at + 1)) {
|
|
473
|
+
if (/^\S/.test(line)) break;
|
|
474
|
+
const item = /^[ \t]+-[ \t]+(\S+)/.exec(line);
|
|
475
|
+
if (item) tools.push(item[1]!);
|
|
476
|
+
}
|
|
477
|
+
return tools;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// ---------------------------------------------------------------------------
|
|
481
|
+
// Matching
|
|
482
|
+
// ---------------------------------------------------------------------------
|
|
483
|
+
|
|
484
|
+
const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
485
|
+
|
|
486
|
+
interface Compiled {
|
|
487
|
+
re: RegExp;
|
|
488
|
+
slots: string[];
|
|
489
|
+
/** Fixed text before the first slot; the whole text when unslotted. */
|
|
490
|
+
prefix: string;
|
|
491
|
+
slotted: boolean;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const compiledCache = new Map<string, Compiled>();
|
|
495
|
+
|
|
496
|
+
export function compileText(text: string): Compiled {
|
|
497
|
+
const hit = compiledCache.get(text);
|
|
498
|
+
if (hit) return hit;
|
|
499
|
+
const slots: string[] = [];
|
|
500
|
+
let source = "";
|
|
501
|
+
let last = 0;
|
|
502
|
+
for (const m of text.matchAll(SLOT)) {
|
|
503
|
+
source += escapeRe(text.slice(last, m.index));
|
|
504
|
+
source += m[2] ? "(.*?)" : "(.+?)";
|
|
505
|
+
slots.push(m[1]!);
|
|
506
|
+
last = m.index! + m[0].length;
|
|
507
|
+
}
|
|
508
|
+
const tail = text.slice(last);
|
|
509
|
+
// A closing `.` also matches `;`: the line continues the sentence with local text.
|
|
510
|
+
source += tail.endsWith(".") ? `${escapeRe(tail.slice(0, -1))}[.;]` : escapeRe(tail);
|
|
511
|
+
const first = text.search(SLOT);
|
|
512
|
+
const compiled = {
|
|
513
|
+
re: new RegExp(source, "g"),
|
|
514
|
+
slots,
|
|
515
|
+
prefix: first === -1 ? text : text.slice(0, first),
|
|
516
|
+
slotted: slots.length > 0,
|
|
517
|
+
};
|
|
518
|
+
compiledCache.set(text, compiled);
|
|
519
|
+
return compiled;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const countIn = (line: string, c: Compiled) => [...line.matchAll(c.re)].length;
|
|
523
|
+
|
|
524
|
+
/** Drop the blockquote, list marker, step number, or checkbox in front of a line's text. */
|
|
525
|
+
export const stripLinePrefix = (line: string) =>
|
|
526
|
+
line.replace(/^[ \t]*(?:>[ \t]?)?(?:[ \t]*(?:[-*]|\d+\.)[ \t]+)?(?:\[[ xX]\][ \t]+)?/, "");
|
|
527
|
+
|
|
528
|
+
/** Split after `.` or `;` (bold close allowed between), never after `e.g.`; the punctuation stays on the sentence. */
|
|
529
|
+
export const splitSentences = (s: string) =>
|
|
530
|
+
s
|
|
531
|
+
.split(/(?<!\be\.g\.)(?<=[.;]\**)\s+(?=\S)/)
|
|
532
|
+
.map((x) => x.trim())
|
|
533
|
+
.filter(Boolean);
|
|
534
|
+
|
|
535
|
+
/** Word-level Levenshtein similarity in [0, 1]. */
|
|
536
|
+
export function similarity(a: string, b: string): number {
|
|
537
|
+
const x = a.split(/\s+/);
|
|
538
|
+
const y = b.split(/\s+/);
|
|
539
|
+
let prev = Array.from({ length: y.length + 1 }, (_, j) => j);
|
|
540
|
+
for (let i = 1; i <= x.length; i++) {
|
|
541
|
+
const cur = [i];
|
|
542
|
+
for (let j = 1; j <= y.length; j++) {
|
|
543
|
+
cur[j] = Math.min(
|
|
544
|
+
prev[j]! + 1,
|
|
545
|
+
cur[j - 1]! + 1,
|
|
546
|
+
prev[j - 1]! + (x[i - 1] === y[j - 1] ? 0 : 1),
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
prev = cur;
|
|
550
|
+
}
|
|
551
|
+
return 1 - prev[y.length]! / Math.max(x.length, y.length);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function modeFor(clause: Clause, file: CorpusFile): Mode | undefined {
|
|
555
|
+
if (clause.except.has(file.name)) return undefined;
|
|
556
|
+
const mode = clause.modes[poolOf(file.surface)];
|
|
557
|
+
if (mode === "if-verdict") return file.content.includes(VERDICT_END) ? "required" : "forbidden";
|
|
558
|
+
if (mode === "if-bash") return toolsOf(file.content).includes("Bash") ? "required" : "forbidden";
|
|
559
|
+
if (mode === "if-search" || mode === "if-no-search") {
|
|
560
|
+
const tools = toolsOf(file.content);
|
|
561
|
+
const searches = tools.includes("Grep") || tools.includes("Bash");
|
|
562
|
+
return searches === (mode === "if-search") ? "required" : "forbidden";
|
|
563
|
+
}
|
|
564
|
+
return mode;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
interface NearCopy {
|
|
568
|
+
line: number;
|
|
569
|
+
found: string;
|
|
570
|
+
canonical: string;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const siblingCache = new WeakMap<Set<string>, Map<string, string[]>>();
|
|
574
|
+
const lineCache = new Map<string, { sentences: string[]; words: Map<string, number> }>();
|
|
575
|
+
|
|
576
|
+
const wordCounts = (words: string[]) => {
|
|
577
|
+
const counts = new Map<string, number>();
|
|
578
|
+
for (const w of words) counts.set(w, (counts.get(w) ?? 0) + 1);
|
|
579
|
+
return counts;
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
const canonicalCache = new Map<
|
|
583
|
+
string,
|
|
584
|
+
{ sentences: number; words: string[]; counts: Map<string, number> }
|
|
585
|
+
>();
|
|
586
|
+
|
|
587
|
+
/** A clause wording's sentence count, words, and word counts; memoised like `lineParts`. */
|
|
588
|
+
function canonicalParts(canonical: string) {
|
|
589
|
+
let hit = canonicalCache.get(canonical);
|
|
590
|
+
if (!hit) {
|
|
591
|
+
const words = canonical.split(/\s+/);
|
|
592
|
+
hit = { sentences: splitSentences(canonical).length, words, counts: wordCounts(words) };
|
|
593
|
+
canonicalCache.set(canonical, hit);
|
|
594
|
+
}
|
|
595
|
+
return hit;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** A line's sentences and word counts, prefix dropped; memoised, since every clause scans every line. */
|
|
599
|
+
function lineParts(line: string) {
|
|
600
|
+
let hit = lineCache.get(line);
|
|
601
|
+
if (!hit) {
|
|
602
|
+
if (lineCache.size > 50_000) lineCache.clear();
|
|
603
|
+
const rest = stripLinePrefix(line);
|
|
604
|
+
hit = { sentences: splitSentences(rest), words: wordCounts(rest.split(/\s+/)) };
|
|
605
|
+
lineCache.set(line, hit);
|
|
606
|
+
}
|
|
607
|
+
return hit;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/** Other unslotted clause wordings near enough to `canonical` to read as its near-copy. */
|
|
611
|
+
function siblingsOf(canonical: string, allTexts: Set<string>): string[] {
|
|
612
|
+
let byText = siblingCache.get(allTexts);
|
|
613
|
+
if (!byText) {
|
|
614
|
+
byText = new Map();
|
|
615
|
+
siblingCache.set(allTexts, byText);
|
|
616
|
+
}
|
|
617
|
+
let hit = byText.get(canonical);
|
|
618
|
+
if (!hit) {
|
|
619
|
+
hit = [...allTexts].filter(
|
|
620
|
+
(t) => t !== canonical && !compileText(t).slotted && similarity(t, canonical) >= NEAR_COPY,
|
|
621
|
+
);
|
|
622
|
+
byText.set(canonical, hit);
|
|
623
|
+
}
|
|
624
|
+
return hit;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
/** Sentence windows in a line that are near, but not equal to, an unslotted clause wording. */
|
|
628
|
+
function nearCopies(
|
|
629
|
+
clause: Clause,
|
|
630
|
+
lineIdx: number,
|
|
631
|
+
line: string,
|
|
632
|
+
allTexts: Set<string>,
|
|
633
|
+
): NearCopy[] {
|
|
634
|
+
if (clause.texts.some((t) => countIn(line, compileText(t)) > 0)) return [];
|
|
635
|
+
const { sentences, words: lineWords } = lineParts(line);
|
|
636
|
+
const out: NearCopy[] = [];
|
|
637
|
+
for (const canonical of clause.texts) {
|
|
638
|
+
if (compileText(canonical).slotted) continue;
|
|
639
|
+
// A line carrying a sibling clause verbatim (`Gate 2` beside `Gate 3`) is that clause, not drift of this one.
|
|
640
|
+
if (siblingsOf(canonical, allTexts).some((t) => countIn(line, compileText(t)) > 0)) continue;
|
|
641
|
+
const { sentences: k, words: canonicalWords, counts } = canonicalParts(canonical);
|
|
642
|
+
const cw = canonicalWords.length;
|
|
643
|
+
// Every window is drawn from the line's words: too few of the clause's words anywhere → no near-copy.
|
|
644
|
+
let shared = 0;
|
|
645
|
+
for (const [w, n] of counts) shared += Math.min(n, lineWords.get(w) ?? 0);
|
|
646
|
+
if (shared < Math.ceil(NEAR_COPY * cw)) continue;
|
|
647
|
+
// A drift can add or drop a sentence break (`;` turned into `,`): try one sentence fewer and more.
|
|
648
|
+
for (let i = 0; i < sentences.length;) {
|
|
649
|
+
let best: { found: string; size: number; score: number } | undefined;
|
|
650
|
+
for (let size = Math.max(1, k - 1); size <= k + 1 && i + size <= sentences.length; size++) {
|
|
651
|
+
let window = sentences.slice(i, i + size).join(" ");
|
|
652
|
+
// An unlabelled clause can sit behind any file's own label: compare the text only.
|
|
653
|
+
if (!canonical.startsWith("**")) window = window.replace(LABEL, "");
|
|
654
|
+
// A clause can close a sentence that opens with local text (`**If** {cond} → {clause}`): also compare the tail.
|
|
655
|
+
const words = window.split(/\s+/);
|
|
656
|
+
const candidates = [window];
|
|
657
|
+
for (const n of [cw - 1, cw, cw + 1]) {
|
|
658
|
+
if (n > 0 && n < words.length) candidates.push(words.slice(-n).join(" "));
|
|
659
|
+
}
|
|
660
|
+
for (const found of candidates) {
|
|
661
|
+
if (allTexts.has(found) || !nearCandidate(found.split(/\s+/), canonicalWords)) continue;
|
|
662
|
+
const score = similarity(found, canonical);
|
|
663
|
+
if (score >= NEAR_COPY && (!best || score > best.score)) best = { found, size, score };
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (!best) {
|
|
667
|
+
i++;
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
out.push({ line: lineIdx, found: best.found, canonical });
|
|
671
|
+
i += best.size;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
return out;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Header shape of a prompt, workflow or repo agent: no `> [!CAUTION]` box, and the
|
|
679
|
+
* `**Protocol:**`, `**Contract:**` and `**Rules:**` blocks each exactly once, in that order.
|
|
680
|
+
*/
|
|
681
|
+
export function checkBlocks(files: CorpusFile[]): Finding[] {
|
|
682
|
+
const findings: Finding[] = [];
|
|
683
|
+
for (const file of files) {
|
|
684
|
+
if (!HEADED.includes(file.surface)) continue;
|
|
685
|
+
const lines = file.content.split("\n");
|
|
686
|
+
const caution = proseLineIndexes(file).find((i) => lines[i]!.startsWith("> [!CAUTION]"));
|
|
687
|
+
if (caution !== undefined) {
|
|
688
|
+
findings.push({
|
|
689
|
+
file: file.id,
|
|
690
|
+
line: caution + 1,
|
|
691
|
+
clause: "header-blocks",
|
|
692
|
+
problem: "CAUTION box: header lines go in the Protocol / Contract / Rules blocks",
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
const ranges = blockRanges(file.content);
|
|
696
|
+
let prev: Block | undefined;
|
|
697
|
+
for (const b of BLOCKS) {
|
|
698
|
+
const count = lines.filter((l) => l.startsWith(BLOCK_LABEL[b])).length;
|
|
699
|
+
if (count !== 1) {
|
|
700
|
+
findings.push({
|
|
701
|
+
file: file.id,
|
|
702
|
+
clause: "header-blocks",
|
|
703
|
+
problem: `${BLOCK_LABEL[b]} block appears ${count} times; expected once`,
|
|
704
|
+
});
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
if (prev && ranges[b]!.from < ranges[prev]!.from) {
|
|
708
|
+
findings.push({
|
|
709
|
+
file: file.id,
|
|
710
|
+
line: ranges[b]!.from + 1,
|
|
711
|
+
clause: "header-blocks",
|
|
712
|
+
problem: `${BLOCK_LABEL[b]} block must come after ${BLOCK_LABEL[prev]}`,
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
prev = b;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return findings;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// ---------------------------------------------------------------------------
|
|
722
|
+
// Check
|
|
723
|
+
// ---------------------------------------------------------------------------
|
|
724
|
+
|
|
725
|
+
export function checkClauses(clauses: Clause[], files: CorpusFile[]): Finding[] {
|
|
726
|
+
const findings: Finding[] = [];
|
|
727
|
+
const allTexts = new Set(clauses.flatMap((c) => c.texts));
|
|
728
|
+
const compiled = new Map(clauses.map((c) => [c, c.texts.map(compileText)]));
|
|
729
|
+
|
|
730
|
+
for (const file of files) {
|
|
731
|
+
const lines = file.content.split("\n");
|
|
732
|
+
const headed = HEADED.includes(file.surface);
|
|
733
|
+
const ranges = headed ? blockRanges(file.content) : {};
|
|
734
|
+
const placed: { clause: Clause; line: number }[] = [];
|
|
735
|
+
const at = (i: number | undefined) => (i === undefined ? undefined : i + 1);
|
|
736
|
+
for (const clause of clauses) {
|
|
737
|
+
const mode = modeFor(clause, file);
|
|
738
|
+
if (mode === undefined) continue;
|
|
739
|
+
const forms = compiled.get(clause)!;
|
|
740
|
+
// Forms can overlap (a slotted form may also fit another's line): a line counts once per instance.
|
|
741
|
+
const perLine = (i: number) => Math.max(...forms.map((c) => countIn(lines[i]!, c)));
|
|
742
|
+
const prose = linesFor(clause, file);
|
|
743
|
+
const hits = prose.filter((i) => perLine(i) > 0);
|
|
744
|
+
const count = hits.reduce((n, i) => n + perLine(i), 0);
|
|
745
|
+
if (mode === "forbidden" && count > 0) {
|
|
746
|
+
findings.push({
|
|
747
|
+
file: file.id,
|
|
748
|
+
line: at(hits[0]),
|
|
749
|
+
clause: clause.name,
|
|
750
|
+
problem: "present where it must not be",
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
if (mode === "required" && count === 0) {
|
|
754
|
+
findings.push({
|
|
755
|
+
file: file.id,
|
|
756
|
+
clause: clause.name,
|
|
757
|
+
problem: `missing; add: ${clause.texts[0]}`,
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
if ((mode === "required" || mode === "where-used") && count > 1) {
|
|
761
|
+
findings.push({
|
|
762
|
+
file: file.id,
|
|
763
|
+
line: at(hits[1]),
|
|
764
|
+
clause: clause.name,
|
|
765
|
+
problem: `appears ${count} times; expected once`,
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
if (mode === "forbidden") continue;
|
|
769
|
+
|
|
770
|
+
for (const i of prose) {
|
|
771
|
+
const line = lines[i]!;
|
|
772
|
+
for (const near of nearCopies(clause, i, line, allTexts)) {
|
|
773
|
+
findings.push({
|
|
774
|
+
file: file.id,
|
|
775
|
+
line: near.line + 1,
|
|
776
|
+
clause: clause.name,
|
|
777
|
+
problem: `near-copy (run pnpm instructions:sync): "${near.found}"`,
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
for (const old of clause.retired) {
|
|
781
|
+
if (countIn(line, compileText(old)) > 0) {
|
|
782
|
+
findings.push({
|
|
783
|
+
file: file.id,
|
|
784
|
+
line: i + 1,
|
|
785
|
+
clause: clause.name,
|
|
786
|
+
problem: "retired wording (run pnpm instructions:sync)",
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
if (
|
|
791
|
+
clause.anchored &&
|
|
792
|
+
forms.some((c) => c.slotted && line.includes(c.prefix)) &&
|
|
793
|
+
perLine(i) === 0
|
|
794
|
+
) {
|
|
795
|
+
const templates = clause.texts.filter((t) => compileText(t).slotted);
|
|
796
|
+
findings.push({
|
|
797
|
+
file: file.id,
|
|
798
|
+
line: i + 1,
|
|
799
|
+
clause: clause.name,
|
|
800
|
+
problem: `does not match the template${templates.length > 1 ? "s" : ""}: ${templates.join(" | ")}`,
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
if (clause.block && count > 0 && headed) placed.push({ clause, line: hits[0]! });
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// Placed clauses sit in their block, in wording-file order; `first` opens the block's bullets.
|
|
809
|
+
let prev: { clause: Clause; line: number } | undefined;
|
|
810
|
+
for (const p of placed) {
|
|
811
|
+
const block = p.clause.block!;
|
|
812
|
+
const range = ranges[block];
|
|
813
|
+
if (!range || p.line < range.from || p.line >= range.to) {
|
|
814
|
+
findings.push({
|
|
815
|
+
file: file.id,
|
|
816
|
+
line: at(p.line),
|
|
817
|
+
clause: p.clause.name,
|
|
818
|
+
problem: `must be in the ${BLOCK_LABEL[block]} block`,
|
|
819
|
+
});
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
const first = lines.findIndex((l, i) => i > range.from && i < range.to && l.startsWith("- "));
|
|
823
|
+
if (p.clause.position === "first" && p.line !== first) {
|
|
824
|
+
findings.push({
|
|
825
|
+
file: file.id,
|
|
826
|
+
line: at(p.line),
|
|
827
|
+
clause: p.clause.name,
|
|
828
|
+
problem: `must be the first bullet of the ${BLOCK_LABEL[block]} block`,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
if (prev && p.line < prev.line) {
|
|
832
|
+
findings.push({
|
|
833
|
+
file: file.id,
|
|
834
|
+
line: at(p.line),
|
|
835
|
+
clause: p.clause.name,
|
|
836
|
+
problem: `header order: must come after ${prev.clause.name}`,
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
prev = p;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
return findings.concat(unlistedShared(clauses, files));
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/** Could two sentences be NEAR_COPY-similar? Cheap word-count and shared-word bounds before `similarity`. */
|
|
846
|
+
function nearCandidate(a: string[], b: string[]): boolean {
|
|
847
|
+
const longer = Math.max(a.length, b.length);
|
|
848
|
+
const need = Math.ceil(NEAR_COPY * longer);
|
|
849
|
+
if (Math.min(a.length, b.length) < need) return false;
|
|
850
|
+
const pool = new Map<string, number>();
|
|
851
|
+
for (const w of b) pool.set(w, (pool.get(w) ?? 0) + 1);
|
|
852
|
+
let shared = 0;
|
|
853
|
+
for (const w of a) {
|
|
854
|
+
const n = pool.get(w) ?? 0;
|
|
855
|
+
if (n > 0) {
|
|
856
|
+
shared++;
|
|
857
|
+
pool.set(w, n - 1);
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
return shared >= need;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/**
|
|
864
|
+
* Sentences shared by a surface's SHARED_MIN or more files that no clause covers.
|
|
865
|
+
* The cut-off is the rule for what belongs in the wording file. Files count by
|
|
866
|
+
* basename, so an adopted copy never counts twice. Headings, HTML comments, and
|
|
867
|
+
* table rows are structure and never count. A sentence ending its line and
|
|
868
|
+
* the same sentence continued with `;` count as one. Near-copies count toward the
|
|
869
|
+
* cut-off, so one drifted copy cannot hide a sentence the other files share.
|
|
870
|
+
*/
|
|
871
|
+
export function unlistedShared(clauses: Clause[], files: CorpusFile[]): Finding[] {
|
|
872
|
+
const forms = clauses.flatMap((c) => c.texts.map(compileText));
|
|
873
|
+
const findings: Finding[] = [];
|
|
874
|
+
for (const surface of SURFACES) {
|
|
875
|
+
const min = SHARED_MIN[surface];
|
|
876
|
+
if (min === undefined) continue;
|
|
877
|
+
const seen = new Map<string, Map<string, string>>();
|
|
878
|
+
for (const file of files.filter((f) => poolOf(f.surface) === surface)) {
|
|
879
|
+
const lines = file.content.split("\n");
|
|
880
|
+
for (const i of clauseLineIndexes(file)) {
|
|
881
|
+
let rest = stripLinePrefix(lines[i]!);
|
|
882
|
+
if (STRUCTURE.test(rest)) continue;
|
|
883
|
+
for (const c of forms) rest = rest.replace(c.re, "\u0000");
|
|
884
|
+
for (const sentence of splitSentences(rest)) {
|
|
885
|
+
const s = sentence.replace(/[.;]$/, "");
|
|
886
|
+
if (s.includes("\u0000") || s.split(/\s+/).length < 3) continue;
|
|
887
|
+
if (!seen.has(s)) seen.set(s, new Map());
|
|
888
|
+
if (!seen.get(s)!.has(file.name)) seen.get(s)!.set(file.name, file.id);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
findings.push(...sharedGroups(seen, min, NOUN[surface]!));
|
|
893
|
+
}
|
|
894
|
+
return findings;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
/** Group each sentence with its near-copies; report every group reaching `min` files. */
|
|
898
|
+
function sharedGroups(
|
|
899
|
+
seen: Map<string, Map<string, string>>,
|
|
900
|
+
min: number,
|
|
901
|
+
noun: string,
|
|
902
|
+
): Finding[] {
|
|
903
|
+
const words = new Map([...seen.keys()].map((s) => [s, s.split(/\s+/)]));
|
|
904
|
+
const reported = new Set<string>();
|
|
905
|
+
const findings: Finding[] = [];
|
|
906
|
+
// Seeds: every sentence, widest first, so a group is reported once under its most-copied wording.
|
|
907
|
+
// A sentence in one file still seeds: with a cut-off of 2, its one near-copy elsewhere makes the pair.
|
|
908
|
+
const seeds = [...seen].sort((a, b) => b[1].size - a[1].size);
|
|
909
|
+
for (const [seed, ids] of seeds) {
|
|
910
|
+
if (reported.has(seed)) continue;
|
|
911
|
+
const near = [...seen.keys()].filter(
|
|
912
|
+
(s) =>
|
|
913
|
+
s !== seed &&
|
|
914
|
+
!reported.has(s) &&
|
|
915
|
+
nearCandidate(words.get(seed)!, words.get(s)!) &&
|
|
916
|
+
similarity(seed, s) >= NEAR_COPY,
|
|
917
|
+
);
|
|
918
|
+
const group = new Set([...ids.keys(), ...near.flatMap((s) => [...seen.get(s)!.keys()])]);
|
|
919
|
+
if (group.size < min) continue;
|
|
920
|
+
for (const s of [seed, ...near]) reported.add(s);
|
|
921
|
+
const drifted = near.length > 0 ? `, ${group.size - ids.size} as near-copies` : "";
|
|
922
|
+
findings.push({
|
|
923
|
+
file: [...ids.values()][0]!,
|
|
924
|
+
clause: "(none)",
|
|
925
|
+
problem: `shared by ${group.size} ${noun}${drifted} but not in .bkit/instructions.md: "${seed}"`,
|
|
926
|
+
});
|
|
927
|
+
}
|
|
928
|
+
return findings;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
// ---------------------------------------------------------------------------
|
|
932
|
+
// Sync
|
|
933
|
+
// ---------------------------------------------------------------------------
|
|
934
|
+
|
|
935
|
+
/** Rewrite near-copies and retired wordings in place. Returns only the files that changed. */
|
|
936
|
+
export function syncClauses(clauses: Clause[], files: CorpusFile[]): Map<string, string> {
|
|
937
|
+
const changed = new Map<string, string>();
|
|
938
|
+
const allTexts = new Set(clauses.flatMap((c) => c.texts));
|
|
939
|
+
for (const file of files) {
|
|
940
|
+
const lines = file.content.split("\n");
|
|
941
|
+
for (const i of clauseLineIndexes(file)) {
|
|
942
|
+
for (const clause of clauses) {
|
|
943
|
+
if (modeFor(clause, file) === undefined || modeFor(clause, file) === "forbidden") continue;
|
|
944
|
+
const target = compileText(clause.texts[0]!);
|
|
945
|
+
for (const old of clause.retired) {
|
|
946
|
+
const from = compileText(old);
|
|
947
|
+
lines[i] = lines[i]!.replace(from.re, (...groups) => {
|
|
948
|
+
const bySlot = new Map(from.slots.map((s, n) => [s, String(groups[n + 1])]));
|
|
949
|
+
const out = clause.texts[0]!.replace(SLOT, (_, slot: string) => bySlot.get(slot) ?? "");
|
|
950
|
+
return String(groups[0]).endsWith(";") && out.endsWith(".")
|
|
951
|
+
? `${out.slice(0, -1)};`
|
|
952
|
+
: out;
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
if (target.slotted) continue;
|
|
956
|
+
for (const near of nearCopies(clause, i, lines[i]!, allTexts)) {
|
|
957
|
+
const out =
|
|
958
|
+
near.found.endsWith(";") && near.canonical.endsWith(".")
|
|
959
|
+
? `${near.canonical.slice(0, -1)};`
|
|
960
|
+
: near.canonical;
|
|
961
|
+
lines[i] = lines[i]!.replace(near.found, out);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
const content = lines.join("\n");
|
|
966
|
+
if (content !== file.content) changed.set(file.id, content);
|
|
967
|
+
}
|
|
968
|
+
return changed;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
// ---------------------------------------------------------------------------
|
|
972
|
+
// Store carriers
|
|
973
|
+
// ---------------------------------------------------------------------------
|
|
974
|
+
|
|
975
|
+
/** Surfaces in the order a `- carriers:` line lists them. */
|
|
976
|
+
const CARRIER_ORDER: readonly Surface[] = [
|
|
977
|
+
"workflow",
|
|
978
|
+
"prompt",
|
|
979
|
+
"repo-agent",
|
|
980
|
+
"audit",
|
|
981
|
+
"plan-template",
|
|
982
|
+
"entry",
|
|
983
|
+
"doc",
|
|
984
|
+
"rules",
|
|
985
|
+
];
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
* Basenames as the store writes them: `prefix-*` for a family whose every member is
|
|
989
|
+
* listed (two or more files share `prefix-`), then single names, each sorted.
|
|
990
|
+
*/
|
|
991
|
+
export function formatNames(listed: string[], all: string[]): string {
|
|
992
|
+
const left = new Set(listed);
|
|
993
|
+
const families: string[] = [];
|
|
994
|
+
for (const name of [...listed].sort()) {
|
|
995
|
+
if (!left.has(name)) continue;
|
|
996
|
+
const parts = name.split("-");
|
|
997
|
+
for (let n = 2; n < parts.length; n++) {
|
|
998
|
+
const prefix = `${parts.slice(0, n).join("-")}-`;
|
|
999
|
+
const members = all.filter((a) => a.startsWith(prefix));
|
|
1000
|
+
if (members.length >= 2 && members.every((m) => left.has(m))) {
|
|
1001
|
+
families.push(`${prefix}*`);
|
|
1002
|
+
for (const m of members) left.delete(m);
|
|
1003
|
+
break;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
return [...families.sort(), ...[...left].sort()].map((n) => `\`${n}\``).join(", ");
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/** The `- carriers:` value for a clause: per surface, the files whose prose carries a text line today. */
|
|
1011
|
+
export function carriersLine(clause: Clause, files: CorpusFile[]): string {
|
|
1012
|
+
const forms = clause.texts.map(compileText);
|
|
1013
|
+
const parts: string[] = [];
|
|
1014
|
+
for (const surface of CARRIER_ORDER) {
|
|
1015
|
+
const ofSurface = files.filter((f) => f.surface === surface);
|
|
1016
|
+
const all = [...new Set(ofSurface.map((f) => f.name))];
|
|
1017
|
+
const carrying = new Set<string>();
|
|
1018
|
+
for (const file of ofSurface) {
|
|
1019
|
+
const lines = file.content.split("\n");
|
|
1020
|
+
if (linesFor(clause, file).some((i) => forms.some((c) => countIn(lines[i]!, c) > 0))) {
|
|
1021
|
+
carrying.add(file.name);
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
if (carrying.size === 0) continue;
|
|
1025
|
+
parts.push(surface === "rules" ? "rules" : `${surface} ${namesSpec(carrying, all)}`);
|
|
1026
|
+
}
|
|
1027
|
+
return parts.join("; ");
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
/** `all`, `all except …` (3 or fewer missing, fewer than carrying), or the names. */
|
|
1031
|
+
export function namesSpec(carrying: Set<string>, all: string[]): string {
|
|
1032
|
+
const missing = all.filter((n) => !carrying.has(n));
|
|
1033
|
+
if (missing.length === 0) return "all";
|
|
1034
|
+
if (missing.length <= 3 && missing.length < carrying.size) {
|
|
1035
|
+
return `all except ${formatNames(missing, all)}`;
|
|
1036
|
+
}
|
|
1037
|
+
return formatNames([...carrying], all);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/** The names a spec written by `namesSpec` (or by hand: `prefix-*` families) covers. */
|
|
1041
|
+
export function expandNames(spec: string, all: string[]): string[] {
|
|
1042
|
+
const listed = (s: string) => {
|
|
1043
|
+
const out = new Set<string>();
|
|
1044
|
+
for (const m of s.matchAll(/`([^`]+)`/g)) {
|
|
1045
|
+
const name = m[1]!;
|
|
1046
|
+
if (!name.endsWith("*")) {
|
|
1047
|
+
out.add(name);
|
|
1048
|
+
continue;
|
|
1049
|
+
}
|
|
1050
|
+
for (const a of all) if (a.startsWith(name.slice(0, -1))) out.add(a);
|
|
1051
|
+
}
|
|
1052
|
+
return out;
|
|
1053
|
+
};
|
|
1054
|
+
if (spec.trim() === "all") return [...all];
|
|
1055
|
+
if (spec.startsWith("all except ")) {
|
|
1056
|
+
const gone = listed(spec.slice("all except ".length));
|
|
1057
|
+
return all.filter((a) => !gone.has(a));
|
|
1058
|
+
}
|
|
1059
|
+
return [...listed(spec)];
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
/** The store with every clause's generated `- carriers:` line recomputed from the corpus. */
|
|
1063
|
+
export function syncStore(md: string, files: CorpusFile[]): string {
|
|
1064
|
+
const m = /(?:^|\n)## Clauses\n/.exec(md);
|
|
1065
|
+
if (!m) return md;
|
|
1066
|
+
const start = m.index + m[0].length;
|
|
1067
|
+
const rest = md.slice(start);
|
|
1068
|
+
const endRel = rest.search(/^## /m);
|
|
1069
|
+
const end = endRel === -1 ? md.length : start + endRel;
|
|
1070
|
+
const sections = md.slice(start, end).split(/^(?=### )/m);
|
|
1071
|
+
const out = sections.map((section) => {
|
|
1072
|
+
if (!section.startsWith("### ")) return section;
|
|
1073
|
+
const [clause] = parseClauses(`## Clauses\n${section}`);
|
|
1074
|
+
const carriers = carriersLine(clause!, files);
|
|
1075
|
+
const lines = section.split("\n").filter((l) => !l.startsWith("- carriers: "));
|
|
1076
|
+
let lastKey = -1;
|
|
1077
|
+
for (let i = 1; i < lines.length && !lines[i]!.startsWith("```"); i++) {
|
|
1078
|
+
if (/^- [a-z]+: /.test(lines[i]!)) lastKey = i;
|
|
1079
|
+
}
|
|
1080
|
+
if (carriers !== "" && lastKey !== -1) lines.splice(lastKey + 1, 0, `- carriers: ${carriers}`);
|
|
1081
|
+
return lines.join("\n");
|
|
1082
|
+
});
|
|
1083
|
+
return md.slice(0, start) + out.join("") + md.slice(end);
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
// ---------------------------------------------------------------------------
|
|
1087
|
+
// Coverage: every standing line has an entry, every entry is still carried
|
|
1088
|
+
// ---------------------------------------------------------------------------
|
|
1089
|
+
|
|
1090
|
+
/** Surfaces whose standing lines the store covers; the rules files are `## Rules` itself. */
|
|
1091
|
+
const COVERED: readonly Surface[] = [
|
|
1092
|
+
"workflow",
|
|
1093
|
+
"prompt",
|
|
1094
|
+
"repo-agent",
|
|
1095
|
+
"audit",
|
|
1096
|
+
"plan-template",
|
|
1097
|
+
"entry",
|
|
1098
|
+
];
|
|
1099
|
+
|
|
1100
|
+
/** `## Local Instructions` sub-section per surface. */
|
|
1101
|
+
export const LOCAL_SECTION: Partial<Record<Surface, string>> = {
|
|
1102
|
+
workflow: "Workflows",
|
|
1103
|
+
prompt: "Prompts",
|
|
1104
|
+
"repo-agent": "Repo agents",
|
|
1105
|
+
audit: "Audit templates",
|
|
1106
|
+
"plan-template": "Plan template",
|
|
1107
|
+
entry: "Entry file",
|
|
1108
|
+
};
|
|
1109
|
+
|
|
1110
|
+
export interface LocalEntry {
|
|
1111
|
+
/** Repo-relative path (per-file entries). */
|
|
1112
|
+
file?: string;
|
|
1113
|
+
/** `- carriers:` value (shared lines). */
|
|
1114
|
+
carriers?: string;
|
|
1115
|
+
/** `R{nn}` / `P{nn}` of the rule the line carries. */
|
|
1116
|
+
tags: string[];
|
|
1117
|
+
/** `fit `{target}` {score}`: the closest rule or clause the line could be reworded to. */
|
|
1118
|
+
fit?: string;
|
|
1119
|
+
/** Why the line stays worded as it is. */
|
|
1120
|
+
keep?: string;
|
|
1121
|
+
text: string;
|
|
1122
|
+
/** Store line index of the text. */
|
|
1123
|
+
at: number;
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
const LOCAL_TAG = /^(?:[RP]\d+|fit `[^`]+` [\d.]+)$/;
|
|
1127
|
+
|
|
1128
|
+
/** `[R01; fit `x` 0.8; keep: {reason}] {text}`; a bracket holding anything but tags stays text. */
|
|
1129
|
+
export function parseLocalLine(line: string): Pick<LocalEntry, "tags" | "fit" | "keep" | "text"> {
|
|
1130
|
+
const m = /^\[([^\]]+)\] (.+)$/.exec(line);
|
|
1131
|
+
if (m) {
|
|
1132
|
+
const parts = m[1]!.split("; ");
|
|
1133
|
+
const tags: string[] = [];
|
|
1134
|
+
let fit: string | undefined;
|
|
1135
|
+
let keep: string | undefined;
|
|
1136
|
+
let ok = true;
|
|
1137
|
+
for (let i = 0; i < parts.length; i++) {
|
|
1138
|
+
const p = parts[i]!;
|
|
1139
|
+
if (p.startsWith("keep: ")) {
|
|
1140
|
+
keep = parts.slice(i).join("; ").slice("keep: ".length);
|
|
1141
|
+
break;
|
|
1142
|
+
}
|
|
1143
|
+
if (!LOCAL_TAG.test(p)) {
|
|
1144
|
+
ok = false;
|
|
1145
|
+
break;
|
|
1146
|
+
}
|
|
1147
|
+
if (p.startsWith("fit ")) fit = p;
|
|
1148
|
+
else tags.push(p);
|
|
1149
|
+
}
|
|
1150
|
+
if (ok) {
|
|
1151
|
+
return {
|
|
1152
|
+
tags,
|
|
1153
|
+
...(fit === undefined ? {} : { fit }),
|
|
1154
|
+
...(keep === undefined ? {} : { keep }),
|
|
1155
|
+
text: m[2]!,
|
|
1156
|
+
};
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
return { tags: [], text: line };
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
/** Every entry of `## Local Instructions`: shared lines, then per-file lines. */
|
|
1163
|
+
export function parseLocal(md: string): LocalEntry[] {
|
|
1164
|
+
const lines = md.split("\n");
|
|
1165
|
+
const head = lines.indexOf("## Local Instructions");
|
|
1166
|
+
if (head === -1) return [];
|
|
1167
|
+
const out: LocalEntry[] = [];
|
|
1168
|
+
let sub = "";
|
|
1169
|
+
let file: string | undefined;
|
|
1170
|
+
let shared: LocalEntry | undefined;
|
|
1171
|
+
for (let i = head + 1; i < lines.length; i++) {
|
|
1172
|
+
const l = lines[i]!;
|
|
1173
|
+
if (l.startsWith("## ")) break;
|
|
1174
|
+
if (l.startsWith("### ")) {
|
|
1175
|
+
sub = l.slice(4).trim();
|
|
1176
|
+
file = undefined;
|
|
1177
|
+
continue;
|
|
1178
|
+
}
|
|
1179
|
+
if (sub === "Shared lines") {
|
|
1180
|
+
const c = /^- carriers: (.+)$/.exec(l);
|
|
1181
|
+
if (c) {
|
|
1182
|
+
shared = { carriers: c[1]!, tags: [], text: "", at: i };
|
|
1183
|
+
out.push(shared);
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
if (!shared) continue;
|
|
1187
|
+
const k = /^ {2}- ([a-z-]+): (.+)$/.exec(l);
|
|
1188
|
+
if (k?.[1] === "tags") {
|
|
1189
|
+
for (const t of k[2]!.split(", ")) {
|
|
1190
|
+
if (t.startsWith("fit ")) shared.fit = t;
|
|
1191
|
+
else shared.tags.push(t);
|
|
1192
|
+
}
|
|
1193
|
+
} else if (k?.[1] === "keep") {
|
|
1194
|
+
shared.keep = k[2]!;
|
|
1195
|
+
} else if (l === " ```text") {
|
|
1196
|
+
const close = lines.indexOf(" ```", i + 1);
|
|
1197
|
+
shared.text = lines
|
|
1198
|
+
.slice(i + 1, close)
|
|
1199
|
+
.map((x) => x.replace(/^ {2}/, ""))
|
|
1200
|
+
.join("\n");
|
|
1201
|
+
shared.at = i + 1;
|
|
1202
|
+
i = close;
|
|
1203
|
+
}
|
|
1204
|
+
continue;
|
|
1205
|
+
}
|
|
1206
|
+
const src = /^- source: `([^`]+)`$/.exec(l);
|
|
1207
|
+
if (src) {
|
|
1208
|
+
file = src[1]!;
|
|
1209
|
+
continue;
|
|
1210
|
+
}
|
|
1211
|
+
const open = /^(`{3,})text$/.exec(l);
|
|
1212
|
+
if (file && open) {
|
|
1213
|
+
const close = lines.indexOf(open[1]!, i + 1);
|
|
1214
|
+
for (let j = i + 1; j < close; j++) {
|
|
1215
|
+
if (lines[j]!.trim() === "") continue;
|
|
1216
|
+
out.push({ file, ...parseLocalLine(lines[j]!), at: j });
|
|
1217
|
+
}
|
|
1218
|
+
i = close;
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
return out;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
/** Surface → names of a shared line's `- carriers:` value. */
|
|
1225
|
+
function sharedCarriers(spec: string, namesOf: (s: Surface) => string[]): Map<Surface, string[]> {
|
|
1226
|
+
const out = new Map<Surface, string[]>();
|
|
1227
|
+
for (const part of spec.split("; ")) {
|
|
1228
|
+
const at = part.indexOf(" ");
|
|
1229
|
+
const surface = part.slice(0, at) as Surface;
|
|
1230
|
+
out.set(surface, expandNames(part.slice(at + 1), namesOf(surface)));
|
|
1231
|
+
}
|
|
1232
|
+
return out;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
const plain = (re: RegExp) => new RegExp(re.source, re.flags.replace("g", ""));
|
|
1236
|
+
|
|
1237
|
+
interface CoverageIndex {
|
|
1238
|
+
files: CorpusFile[];
|
|
1239
|
+
namesOf: (s: Surface) => string[];
|
|
1240
|
+
byName: (s: Surface, name: string) => CorpusFile | undefined;
|
|
1241
|
+
standing: Map<string, { line: number; text: string }[]>;
|
|
1242
|
+
prose: Map<string, string[]>;
|
|
1243
|
+
rules: RuleEntry[];
|
|
1244
|
+
clauses: Clause[];
|
|
1245
|
+
clauseForms: { clause: Clause; re: RegExp }[];
|
|
1246
|
+
local: LocalEntry[];
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function coverageIndex(store: string, corpus: CorpusFile[]): CoverageIndex {
|
|
1250
|
+
const files = corpus.filter(
|
|
1251
|
+
(f) => COVERED.includes(f.surface) && !f.id.startsWith("docs/templates/"),
|
|
1252
|
+
);
|
|
1253
|
+
const names = new Map<Surface, string[]>();
|
|
1254
|
+
for (const f of files) names.set(f.surface, [...(names.get(f.surface) ?? []), f.name]);
|
|
1255
|
+
const clauses = parseClauses(store);
|
|
1256
|
+
return {
|
|
1257
|
+
files,
|
|
1258
|
+
namesOf: (s) => names.get(s) ?? [],
|
|
1259
|
+
byName: (s, name) => files.find((f) => f.surface === s && f.name === name),
|
|
1260
|
+
standing: new Map(files.map((f) => [f.id, standingLines(f)])),
|
|
1261
|
+
prose: new Map(
|
|
1262
|
+
files.map((f) => {
|
|
1263
|
+
const lines = f.content.split("\n");
|
|
1264
|
+
return [f.id, proseLineIndexes(f).map((i) => lines[i]!)];
|
|
1265
|
+
}),
|
|
1266
|
+
),
|
|
1267
|
+
rules: [...parseRuleEntries(store, "Rules"), ...parseRuleEntries(store, "Project Rules")],
|
|
1268
|
+
clauses,
|
|
1269
|
+
clauseForms: clauses.flatMap((c) =>
|
|
1270
|
+
c.texts.map((t) => ({ clause: c, re: plain(compileText(t).re) })),
|
|
1271
|
+
),
|
|
1272
|
+
local: parseLocal(store),
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
/** Files of a rule's carriers with one of `relations`, on the covered surfaces. */
|
|
1277
|
+
function ruleCarriers(
|
|
1278
|
+
ix: CoverageIndex,
|
|
1279
|
+
r: RuleEntry,
|
|
1280
|
+
relations: string[],
|
|
1281
|
+
out?: string[],
|
|
1282
|
+
): { relation: string; file: CorpusFile }[] {
|
|
1283
|
+
const found: { relation: string; file: CorpusFile }[] = [];
|
|
1284
|
+
for (const c of r.carriers) {
|
|
1285
|
+
if (!relations.includes(c.relation) || !COVERED.includes(c.surface as Surface)) continue;
|
|
1286
|
+
const surface = c.surface as Surface;
|
|
1287
|
+
for (const n of expandNames(c.names, ix.namesOf(surface))) {
|
|
1288
|
+
const file = ix.byName(surface, n);
|
|
1289
|
+
if (file) found.push({ relation: c.relation, file });
|
|
1290
|
+
else out?.push(`${r.id} \`${r.name}\`: ${c.relation} carrier \`${n}\` is no ${surface} file`);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
return found;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
/** Files a drift cite can name, by basename, in surface order. */
|
|
1297
|
+
function citeFiles(ix: CoverageIndex, name: string): CorpusFile[] {
|
|
1298
|
+
return COVERED.map((s) => ix.byName(s, name)).filter((f): f is CorpusFile => f !== undefined);
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
const lineHolds = (file: CorpusFile, line: number, excerpt: string) =>
|
|
1302
|
+
(file.content.split("\n")[line - 1] ?? "").includes(excerpt);
|
|
1303
|
+
|
|
1304
|
+
/** Findings for the both-ways check of the store against the corpus. */
|
|
1305
|
+
export function checkCoverage(store: string, corpus: CorpusFile[]): string[] {
|
|
1306
|
+
const out: string[] = [];
|
|
1307
|
+
const ix = coverageIndex(store, corpus);
|
|
1308
|
+
const partialOf = new Map<string, string[]>();
|
|
1309
|
+
for (const r of ix.rules) {
|
|
1310
|
+
for (const { file } of ruleCarriers(ix, r, ["partial"])) {
|
|
1311
|
+
partialOf.set(file.id, [...(partialOf.get(file.id) ?? []), ...splitSentences(r.text)]);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
const localOf = new Map<string, LocalEntry[]>();
|
|
1315
|
+
for (const e of ix.local) {
|
|
1316
|
+
if (e.file !== undefined) localOf.set(e.file, [...(localOf.get(e.file) ?? []), e]);
|
|
1317
|
+
else {
|
|
1318
|
+
for (const [surface, names] of sharedCarriers(e.carriers ?? "", ix.namesOf)) {
|
|
1319
|
+
for (const n of names) {
|
|
1320
|
+
const f = ix.byName(surface, n);
|
|
1321
|
+
if (!f) {
|
|
1322
|
+
out.push(`shared line: carrier \`${n}\` is no ${surface} file: ${e.text}`);
|
|
1323
|
+
continue;
|
|
1324
|
+
}
|
|
1325
|
+
localOf.set(f.id, [...(localOf.get(f.id) ?? []), e]);
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// Nothing missing.
|
|
1332
|
+
for (const f of ix.files) {
|
|
1333
|
+
const own = new Set((localOf.get(f.id) ?? []).map((e) => e.text));
|
|
1334
|
+
const partial = partialOf.get(f.id) ?? [];
|
|
1335
|
+
for (const { line, text } of ix.standing.get(f.id)!) {
|
|
1336
|
+
if (own.has(text)) continue;
|
|
1337
|
+
if (ix.clauseForms.some((c) => c.re.test(text))) continue;
|
|
1338
|
+
if (ix.rules.some((r) => text.includes(r.text))) continue;
|
|
1339
|
+
if (partial.some((s) => text.includes(s))) continue;
|
|
1340
|
+
out.push(`${f.id}:${line}: no entry — add under #### ${f.name}: ${text}`);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
// Nothing extra: local entries.
|
|
1345
|
+
for (const e of ix.local) {
|
|
1346
|
+
if (e.file === undefined) {
|
|
1347
|
+
for (const [surface, names] of sharedCarriers(e.carriers ?? "", ix.namesOf)) {
|
|
1348
|
+
for (const n of names) {
|
|
1349
|
+
const f = ix.byName(surface, n);
|
|
1350
|
+
if (f && !ix.standing.get(f.id)!.some((s) => s.text === e.text)) {
|
|
1351
|
+
out.push(`${n}: shared line no longer in the file: ${e.text}`);
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
continue;
|
|
1356
|
+
}
|
|
1357
|
+
const f = ix.files.find((x) => x.id === e.file);
|
|
1358
|
+
if (!f) out.push(`${e.file}: local entry for a file outside the corpus: ${e.text}`);
|
|
1359
|
+
else if (!ix.standing.get(f.id)!.some((s) => s.text === e.text)) {
|
|
1360
|
+
out.push(`${f.name}: local entry no longer in the file: ${e.text}`);
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
// Nothing extra: rule carriers.
|
|
1365
|
+
for (const r of ix.rules) {
|
|
1366
|
+
const sentences = splitSentences(r.text);
|
|
1367
|
+
for (const { relation, file } of ruleCarriers(
|
|
1368
|
+
ix,
|
|
1369
|
+
r,
|
|
1370
|
+
["verbatim", "partial", "condensed", "specialised", "applies", "cites"],
|
|
1371
|
+
out,
|
|
1372
|
+
)) {
|
|
1373
|
+
const prose = ix.prose.get(file.id)!;
|
|
1374
|
+
const tag = `${r.id} \`${r.name}\``;
|
|
1375
|
+
const linked = ix.clauses.filter((c) => c.rule === r.id || c.name === r.clause);
|
|
1376
|
+
const viaClause = linked.some((c) =>
|
|
1377
|
+
c.texts.some((t) => prose.some((l) => plain(compileText(t).re).test(l))),
|
|
1378
|
+
);
|
|
1379
|
+
if (relation === "verbatim") {
|
|
1380
|
+
if (viaClause) continue;
|
|
1381
|
+
if (!prose.some((l) => l.includes(r.text)))
|
|
1382
|
+
out.push(`${tag}: verbatim carrier \`${file.name}\` lacks the text`);
|
|
1383
|
+
continue;
|
|
1384
|
+
}
|
|
1385
|
+
if (relation === "partial") {
|
|
1386
|
+
if (!viaClause && !prose.some((l) => sentences.some((s) => l.includes(s)))) {
|
|
1387
|
+
out.push(`${tag}: partial carrier \`${file.name}\` holds no sentence of the text`);
|
|
1388
|
+
}
|
|
1389
|
+
continue;
|
|
1390
|
+
}
|
|
1391
|
+
const tagged = (localOf.get(file.id) ?? []).some((e) => e.tags.includes(r.id));
|
|
1392
|
+
const viaTaggedClause = ix.clauses.some(
|
|
1393
|
+
(c) =>
|
|
1394
|
+
c.tags?.includes(r.id) &&
|
|
1395
|
+
c.texts.some((t) => prose.some((l) => plain(compileText(t).re).test(l))),
|
|
1396
|
+
);
|
|
1397
|
+
if (!tagged && !viaClause && !viaTaggedClause) {
|
|
1398
|
+
out.push(
|
|
1399
|
+
`${tag}: ${relation} carrier \`${file.name}\` has no local line or clause tagged ${r.id}`,
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
// Drift: each cite holds its excerpt.
|
|
1405
|
+
for (const d of r.drift) {
|
|
1406
|
+
const entry = /^(hook|permission) `([^`]+)`: /.exec(d.raw);
|
|
1407
|
+
if (entry) {
|
|
1408
|
+
const heading = entry[1] === "hook" ? "Hook Text" : "Permissions";
|
|
1409
|
+
if (
|
|
1410
|
+
!new RegExp(`^### ${escapeRe(entry[2]!)}$`, "m").test(sectionOf(store, heading) ?? "")
|
|
1411
|
+
) {
|
|
1412
|
+
out.push(`${r.id} \`${r.name}\`: drift names no \`## ${heading}\` entry \`${entry[2]}\``);
|
|
1413
|
+
}
|
|
1414
|
+
continue;
|
|
1415
|
+
}
|
|
1416
|
+
if (d.cites.length === 0 || d.excerpt === undefined) {
|
|
1417
|
+
out.push(
|
|
1418
|
+
`${r.id} \`${r.name}\`: drift item needs \`{file}:{line}\` cites and a "quoted excerpt": ${d.raw}`,
|
|
1419
|
+
);
|
|
1420
|
+
continue;
|
|
1421
|
+
}
|
|
1422
|
+
for (const c of d.cites) {
|
|
1423
|
+
const candidates = citeFiles(ix, c.name);
|
|
1424
|
+
if (candidates.length === 0)
|
|
1425
|
+
out.push(`${r.id} \`${r.name}\`: drift \`${c.name}:${c.line}\` names no file`);
|
|
1426
|
+
else if (!candidates.some((f) => lineHolds(f, c.line, d.excerpt!))) {
|
|
1427
|
+
out.push(
|
|
1428
|
+
`${r.id} \`${r.name}\`: drift \`${c.name}:${c.line}\` does not hold "${d.excerpt}"`,
|
|
1429
|
+
);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
return out;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
/** Local lines with a `fit` tag and no `keep` reason. */
|
|
1438
|
+
export function openFits(store: string): string[] {
|
|
1439
|
+
return parseLocal(store)
|
|
1440
|
+
.filter((e) => e.fit !== undefined && e.keep === undefined)
|
|
1441
|
+
.map((e) => `${e.file ?? `shared (${e.carriers})`}: ${e.fit}: ${e.text}`);
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
/** Drift items with no `kept` reason. */
|
|
1445
|
+
export function openDrift(store: string): string[] {
|
|
1446
|
+
return [...parseRuleEntries(store, "Rules"), ...parseRuleEntries(store, "Project Rules")].flatMap(
|
|
1447
|
+
(r) =>
|
|
1448
|
+
r.drift.filter((d) => d.kept === undefined).map((d) => `${r.id} \`${r.name}\`: ${d.raw}`),
|
|
1449
|
+
);
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
/** Label line opening a prompt's block of rule copies. */
|
|
1453
|
+
export const RULES_BLOCK = BLOCK_LABEL.rules;
|
|
1454
|
+
|
|
1455
|
+
/**
|
|
1456
|
+
* Rule copies outside the `**Rules:**` block: a `**Protocol:**` or `**Contract:**`
|
|
1457
|
+
* bullet opening with a bkit rule's first sentence, or `NEAR_COPY`-alike its text. A rule's copy
|
|
1458
|
+
* belongs in the Rules block, word for word.
|
|
1459
|
+
*/
|
|
1460
|
+
export function checkRuleCopies(store: string, corpus: CorpusFile[]): string[] {
|
|
1461
|
+
const rules = parseRuleEntries(store, "Rules");
|
|
1462
|
+
const label = (t: string) => /^\*\*([^*]+):\*\*/.exec(t)?.[1];
|
|
1463
|
+
const byLabel = new Map(rules.map((r) => [label(r.text), r]));
|
|
1464
|
+
const out: string[] = [];
|
|
1465
|
+
for (const f of corpus) {
|
|
1466
|
+
if (!HEADED.includes(f.surface)) continue;
|
|
1467
|
+
const lines = f.content.split("\n");
|
|
1468
|
+
const ranges = blockRanges(f.content);
|
|
1469
|
+
for (const b of ["protocol", "contract"] as const) {
|
|
1470
|
+
const range = ranges[b];
|
|
1471
|
+
if (!range) continue;
|
|
1472
|
+
for (let i = range.from + 1; i < range.to; i++) {
|
|
1473
|
+
const text = /^- (.+)$/.exec(lines[i]!)?.[1];
|
|
1474
|
+
if (text === undefined) continue;
|
|
1475
|
+
const own = label(text);
|
|
1476
|
+
const same = own !== undefined ? byLabel.get(own) : undefined;
|
|
1477
|
+
const rule =
|
|
1478
|
+
(same && splitSentences(same.text)[0] === splitSentences(text)[0] ? same : undefined) ??
|
|
1479
|
+
rules.find(
|
|
1480
|
+
(r) =>
|
|
1481
|
+
nearCandidate(text.split(/\s+/), r.text.split(/\s+/)) &&
|
|
1482
|
+
similarity(text, r.text) >= NEAR_COPY,
|
|
1483
|
+
);
|
|
1484
|
+
if (rule) {
|
|
1485
|
+
out.push(
|
|
1486
|
+
`${rule.id} \`${rule.name}\`: \`${f.id}:${i + 1}\` copies the rule in its ${BLOCK_LABEL[b]} block; it belongs in the ${RULES_BLOCK} block`,
|
|
1487
|
+
);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
return out;
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
/**
|
|
1496
|
+
* Rule copies in prompts against each rule's `- carry:` line: `missing` = a named prompt
|
|
1497
|
+
* without the whole bullet, or with its copies out of rules.md order; `undeclared` = a
|
|
1498
|
+
* prompt holding a bkit rule no carry line names; `empty` = a prompt that carries no rule.
|
|
1499
|
+
* Order is checked inside the `**Rules:**` block only.
|
|
1500
|
+
*/
|
|
1501
|
+
export function checkCarry(
|
|
1502
|
+
store: string,
|
|
1503
|
+
corpus: CorpusFile[],
|
|
1504
|
+
): { missing: string[]; undeclared: string[]; empty: string[] } {
|
|
1505
|
+
const prompts = corpus.filter((f) => f.surface === "prompt");
|
|
1506
|
+
const names = prompts.map((f) => f.name);
|
|
1507
|
+
const bkit = parseRuleEntries(store, "Rules");
|
|
1508
|
+
const rules = [...bkit, ...parseRuleEntries(store, "Project Rules")];
|
|
1509
|
+
const standing = new Map(prompts.map((f) => [f.name, standingLines(f)]));
|
|
1510
|
+
const missing: string[] = [];
|
|
1511
|
+
const undeclared: string[] = [];
|
|
1512
|
+
const carried = new Set<string>();
|
|
1513
|
+
for (const r of rules) {
|
|
1514
|
+
const named = new Set(r.carry === undefined ? [] : expandNames(r.carry, names));
|
|
1515
|
+
for (const n of named) {
|
|
1516
|
+
carried.add(n);
|
|
1517
|
+
if (!standing.get(n)?.some((l) => l.text === r.text)) {
|
|
1518
|
+
missing.push(`${r.id} \`${r.name}\`: carry \`${n}\` lacks the whole rule bullet`);
|
|
1519
|
+
}
|
|
1520
|
+
}
|
|
1521
|
+
if (!bkit.includes(r)) continue;
|
|
1522
|
+
for (const f of prompts) {
|
|
1523
|
+
if (!named.has(f.name) && standing.get(f.name)!.some((l) => l.text.includes(r.text))) {
|
|
1524
|
+
undeclared.push(
|
|
1525
|
+
`${r.id} \`${r.name}\`: \`${f.name}\` holds the rule but no carry line names it`,
|
|
1526
|
+
);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
for (const f of prompts) {
|
|
1531
|
+
const block = ruleBlock(f.content.split("\n"));
|
|
1532
|
+
const order = standing
|
|
1533
|
+
.get(f.name)!
|
|
1534
|
+
.filter((l) => block !== undefined && l.line > block.from && l.line <= block.to)
|
|
1535
|
+
.map((l) => rules.findIndex((r) => r.text === l.text))
|
|
1536
|
+
.filter((i) => i !== -1);
|
|
1537
|
+
if (order.some((v, i) => i > 0 && v < order[i - 1]!)) {
|
|
1538
|
+
missing.push(`\`${f.name}\`: rule copies are out of rules.md order`);
|
|
1539
|
+
}
|
|
1540
|
+
}
|
|
1541
|
+
return { missing, undeclared, empty: names.filter((n) => !carried.has(n)) };
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
/** Rewrite rule copies still in a retired wording, inside each carrying prompt's block. Returns only the files that changed. */
|
|
1545
|
+
export function syncCarry(store: string, corpus: CorpusFile[]): Map<string, string> {
|
|
1546
|
+
const prompts = corpus.filter((f) => f.surface === "prompt");
|
|
1547
|
+
const names = prompts.map((f) => f.name);
|
|
1548
|
+
const rules = [...parseRuleEntries(store, "Rules"), ...parseRuleEntries(store, "Project Rules")];
|
|
1549
|
+
const out = new Map<string, string>();
|
|
1550
|
+
for (const f of prompts) {
|
|
1551
|
+
const lines = f.content.split("\n");
|
|
1552
|
+
const block = ruleBlock(lines);
|
|
1553
|
+
if (!block) continue;
|
|
1554
|
+
for (const r of rules) {
|
|
1555
|
+
if (r.carry === undefined || !expandNames(r.carry, names).includes(f.name)) continue;
|
|
1556
|
+
for (let i = block.from + 1; i < block.to; i++) {
|
|
1557
|
+
if (r.retired.some((old) => lines[i] === `- ${old}`)) lines[i] = `- ${r.text}`;
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
const content = lines.join("\n");
|
|
1561
|
+
if (content !== f.content) out.set(f.id, content);
|
|
1562
|
+
}
|
|
1563
|
+
return out;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
/** Rules and clause texts at least `NEAR_COPY` alike, unless a `- distinct-from:` records the pair. */
|
|
1567
|
+
export function similarEntries(store: string): string[] {
|
|
1568
|
+
const entries: { name: string; text: string; distinct: Set<string> }[] = [];
|
|
1569
|
+
for (const heading of ["Rules", "Project Rules"] as const) {
|
|
1570
|
+
for (const r of parseRuleEntries(store, heading)) {
|
|
1571
|
+
entries.push({ name: r.name, text: r.text, distinct: new Set(r.distinctFrom) });
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
const body = sectionOf(store, "Clauses") ?? "";
|
|
1575
|
+
for (const section of body.split(/^### /m).slice(1)) {
|
|
1576
|
+
const name = section.slice(0, section.indexOf("\n")).trim();
|
|
1577
|
+
const distinct = new Set(
|
|
1578
|
+
[...section.matchAll(/^- distinct-from: (.+)$/gm)].flatMap((m) =>
|
|
1579
|
+
[...m[1]!.matchAll(/`([^`]+)`/g)].map((x) => x[1]!),
|
|
1580
|
+
),
|
|
1581
|
+
);
|
|
1582
|
+
for (const c of parseClauses(`## Clauses\n### ${section}`)) {
|
|
1583
|
+
for (const t of c.texts) entries.push({ name, text: t, distinct });
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
const out: string[] = [];
|
|
1587
|
+
const words = entries.map((e) => e.text.split(/\s+/));
|
|
1588
|
+
for (let i = 0; i < entries.length; i++) {
|
|
1589
|
+
for (let j = i + 1; j < entries.length; j++) {
|
|
1590
|
+
const [a, b] = [entries[i]!, entries[j]!];
|
|
1591
|
+
if (a.name === b.name || a.distinct.has(b.name) || b.distinct.has(a.name)) continue;
|
|
1592
|
+
if (!nearCandidate(words[i]!, words[j]!)) continue;
|
|
1593
|
+
const score = similarity(a.text, b.text);
|
|
1594
|
+
if (score >= NEAR_COPY) {
|
|
1595
|
+
out.push(
|
|
1596
|
+
`\`${a.name}\` and \`${b.name}\` are ${score.toFixed(2)} alike — reword one, or add \`- distinct-from: \`${a.name}\`\` to \`${b.name}\``,
|
|
1597
|
+
);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
return out;
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
/** The store with shared-line carriers and drift line numbers recomputed from the corpus. */
|
|
1605
|
+
export function syncCoverage(store: string, corpus: CorpusFile[]): string {
|
|
1606
|
+
const ix = coverageIndex(store, corpus);
|
|
1607
|
+
const lines = store.split("\n");
|
|
1608
|
+
for (const e of ix.local) {
|
|
1609
|
+
if (e.file !== undefined) continue;
|
|
1610
|
+
const parts: string[] = [];
|
|
1611
|
+
for (const surface of COVERED) {
|
|
1612
|
+
const holding = new Set(
|
|
1613
|
+
ix.files
|
|
1614
|
+
.filter(
|
|
1615
|
+
(f) => f.surface === surface && ix.standing.get(f.id)!.some((s) => s.text === e.text),
|
|
1616
|
+
)
|
|
1617
|
+
.map((f) => f.name),
|
|
1618
|
+
);
|
|
1619
|
+
if (holding.size > 0) parts.push(`${surface} ${namesSpec(holding, ix.namesOf(surface))}`);
|
|
1620
|
+
}
|
|
1621
|
+
const at = lines.lastIndexOf(`- carriers: ${e.carriers}`, e.at);
|
|
1622
|
+
if (at !== -1 && parts.length > 0) lines[at] = `- carriers: ${parts.join("; ")}`;
|
|
1623
|
+
}
|
|
1624
|
+
const rulesAt = lines.findIndex((l) => l === "## Rules");
|
|
1625
|
+
const clausesAt = lines.findIndex((l) => l === "## Clauses");
|
|
1626
|
+
for (let i = rulesAt; i !== -1 && i < clausesAt; i++) {
|
|
1627
|
+
const item = /^ {2}- (.+)$/.exec(lines[i]!);
|
|
1628
|
+
if (!item) continue;
|
|
1629
|
+
const d = parseDriftItem(item[1]!);
|
|
1630
|
+
if (d.excerpt === undefined) continue;
|
|
1631
|
+
let line = lines[i]!;
|
|
1632
|
+
for (const c of d.cites) {
|
|
1633
|
+
const candidates = citeFiles(ix, c.name);
|
|
1634
|
+
if (candidates.some((f) => lineHolds(f, c.line, d.excerpt!))) continue;
|
|
1635
|
+
const hits = candidates.flatMap((f) =>
|
|
1636
|
+
f.content
|
|
1637
|
+
.split("\n")
|
|
1638
|
+
.map((l, n) => (l.includes(d.excerpt!) ? n + 1 : 0))
|
|
1639
|
+
.filter((n) => n > 0),
|
|
1640
|
+
);
|
|
1641
|
+
if (hits.length === 1)
|
|
1642
|
+
line = line.replace(`\`${c.name}:${c.line}\``, `\`${c.name}:${hits[0]}\``);
|
|
1643
|
+
}
|
|
1644
|
+
lines[i] = line;
|
|
1645
|
+
}
|
|
1646
|
+
return lines.join("\n");
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
// ---------------------------------------------------------------------------
|
|
1650
|
+
// CLI: `pnpm instructions:sync` (write), `pnpm instructions:show {name}` /
|
|
1651
|
+
// `--file {basename}` (look up), `tsx .bkit/core/engine/tools/instructions.ts` (check)
|
|
1652
|
+
// ---------------------------------------------------------------------------
|
|
1653
|
+
|
|
1654
|
+
export const STORE_PATH = ".bkit/instructions.md";
|
|
1655
|
+
|
|
1656
|
+
/** Every `### ` entry of the store named `name`, or whose text names `` `basename` `` / whose clause the file carries. */
|
|
1657
|
+
export function showEntries(
|
|
1658
|
+
md: string,
|
|
1659
|
+
files: CorpusFile[],
|
|
1660
|
+
query: { name?: string; file?: string },
|
|
1661
|
+
): string[] {
|
|
1662
|
+
const entries = md.split(/^(?=## |### )/m).filter((e) => e.startsWith("### "));
|
|
1663
|
+
const carried = new Set<string>();
|
|
1664
|
+
if (query.file !== undefined) {
|
|
1665
|
+
const own = files.filter((f) => f.name === query.file);
|
|
1666
|
+
for (const c of parseClauses(md)) {
|
|
1667
|
+
if (carriersLine(c, own) !== "") carried.add(c.name);
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
return entries
|
|
1671
|
+
.filter((e) => {
|
|
1672
|
+
const name = e.slice(4, e.indexOf("\n")).trim();
|
|
1673
|
+
if (query.name !== undefined) return name === query.name;
|
|
1674
|
+
return carried.has(name) || e.includes(`\`${query.file}\``);
|
|
1675
|
+
})
|
|
1676
|
+
.map((e) => e.trimEnd());
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
function main(argv: string[]): number {
|
|
1680
|
+
const root = process.cwd();
|
|
1681
|
+
const storeFile = path.join(root, STORE_PATH);
|
|
1682
|
+
let store = fs.readFileSync(storeFile, "utf-8");
|
|
1683
|
+
const clauses = parseInstructions(store).clauses;
|
|
1684
|
+
let files = loadCorpus(root);
|
|
1685
|
+
if (argv.includes("--show")) {
|
|
1686
|
+
const at = argv.indexOf("--file");
|
|
1687
|
+
const query =
|
|
1688
|
+
at !== -1 ? { file: argv[at + 1] } : { name: argv.find((a) => !a.startsWith("--")) };
|
|
1689
|
+
if (query.file === undefined && query.name === undefined) {
|
|
1690
|
+
console.error("[bkit-instructions] --show takes {name} or --file {basename}");
|
|
1691
|
+
return 2;
|
|
1692
|
+
}
|
|
1693
|
+
const found = showEntries(store, files, query);
|
|
1694
|
+
console.log(found.join("\n\n"));
|
|
1695
|
+
return found.length === 0 ? 1 : 0;
|
|
1696
|
+
}
|
|
1697
|
+
if (argv.includes("--write")) {
|
|
1698
|
+
const copied = syncSources(root, store);
|
|
1699
|
+
if (copied !== store) {
|
|
1700
|
+
fs.writeFileSync(storeFile, copied, "utf-8");
|
|
1701
|
+
console.error(`[bkit-instructions] rewrote ${STORE_PATH} copies of their masters`);
|
|
1702
|
+
store = copied;
|
|
1703
|
+
}
|
|
1704
|
+
for (const [id, content] of syncCarry(store, files)) {
|
|
1705
|
+
fs.writeFileSync(path.join(root, id), content, "utf-8");
|
|
1706
|
+
console.error(`[bkit-instructions] rewrote ${id} rule copies`);
|
|
1707
|
+
}
|
|
1708
|
+
files = loadCorpus(root);
|
|
1709
|
+
const changed = syncClauses(clauses, files);
|
|
1710
|
+
for (const [id, content] of changed) {
|
|
1711
|
+
fs.writeFileSync(path.join(root, id), content, "utf-8");
|
|
1712
|
+
console.error(`[bkit-instructions] rewrote ${id}`);
|
|
1713
|
+
}
|
|
1714
|
+
files = loadCorpus(root);
|
|
1715
|
+
const synced = syncCoverage(syncStore(store, files), files);
|
|
1716
|
+
if (synced !== store) {
|
|
1717
|
+
fs.writeFileSync(storeFile, synced, "utf-8");
|
|
1718
|
+
console.error(`[bkit-instructions] rewrote ${STORE_PATH}`);
|
|
1719
|
+
store = synced;
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
const findings = [...checkClauses(clauses, files), ...checkBlocks(files)];
|
|
1723
|
+
for (const f of findings) {
|
|
1724
|
+
console.error(
|
|
1725
|
+
`[bkit-instructions] ${f.file}${f.line ? `:${f.line}` : ""} ${f.clause}: ${f.problem}`,
|
|
1726
|
+
);
|
|
1727
|
+
}
|
|
1728
|
+
const carry = checkCarry(store, files);
|
|
1729
|
+
const sourceFindings = [
|
|
1730
|
+
...checkSources(root, store),
|
|
1731
|
+
...checkCoverage(store, files),
|
|
1732
|
+
...similarEntries(store),
|
|
1733
|
+
...carry.missing,
|
|
1734
|
+
...carry.undeclared,
|
|
1735
|
+
...carry.empty.map((n) => `\`${n}\`: carries no rule`),
|
|
1736
|
+
...checkRuleCopies(store, files),
|
|
1737
|
+
];
|
|
1738
|
+
for (const f of sourceFindings) console.error(`[bkit-instructions] ${STORE_PATH} ${f}`);
|
|
1739
|
+
if (sourceFindings.length > 0) return 1;
|
|
1740
|
+
if (syncStore(store, files) !== store) {
|
|
1741
|
+
console.error(`[bkit-instructions] ${STORE_PATH}: carriers stale (run pnpm instructions:sync)`);
|
|
1742
|
+
return 1;
|
|
1743
|
+
}
|
|
1744
|
+
return findings.length === 0 ? 0 : 1;
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
if (process.argv[1] && fs.existsSync(process.argv[1])) {
|
|
1748
|
+
if (fs.realpathSync(fileURLToPath(import.meta.url)) === fs.realpathSync(process.argv[1])) {
|
|
1749
|
+
process.exitCode = main(process.argv.slice(2));
|
|
1750
|
+
}
|
|
1751
|
+
}
|