@krisada-m/ksd-intelligence 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.antigravity-plugin/plugin.json +20 -0
- package/LICENSE +21 -0
- package/README.md +195 -2
- package/agy-skills/erosiano-review/SKILL.md +17 -0
- package/dist/adapters/agy/skills.js +252 -0
- package/dist/adapters/cli/check-changed.js +3 -0
- package/dist/adapters/cli/main.js +1013 -0
- package/dist/adapters/cli/policy-bin.js +10 -0
- package/dist/adapters/cli/review-verdict.js +3 -0
- package/dist/adapters/omp/extension.js +884 -0
- package/dist/adapters/omp/skills.js +130 -0
- package/dist/adapters/ui/hud.js +389 -0
- package/dist/adapters/ui/tree.js +471 -0
- package/dist/core/audit/index.js +2 -0
- package/dist/core/audit/log.js +505 -0
- package/dist/core/audit/summary.js +188 -0
- package/dist/core/changes/changes.js +591 -0
- package/dist/core/changes/git.js +266 -0
- package/dist/core/changes/plan.js +109 -0
- package/dist/core/changes/render.js +356 -0
- package/dist/core/engine/build.js +178 -0
- package/dist/core/engine/child.js +389 -0
- package/dist/core/engine/host.js +206 -0
- package/dist/core/engine/lock.js +133 -0
- package/dist/core/engine/protocol.js +43 -0
- package/dist/core/engine/runtime.js +44 -0
- package/dist/core/engine/status.js +452 -0
- package/dist/core/engine/typescript.js +194 -0
- package/dist/core/engine/workspace.js +72 -0
- package/dist/core/graph/graph.js +127 -0
- package/dist/core/graph/limits.js +16 -0
- package/dist/core/impact/query.js +1292 -0
- package/dist/core/impact/render.js +590 -0
- package/dist/core/impact/session.js +694 -0
- package/dist/core/indexing/declarations.js +455 -0
- package/dist/core/indexing/differential.js +345 -0
- package/dist/core/indexing/doctor.js +120 -0
- package/dist/core/indexing/indexer.js +811 -0
- package/dist/core/indexing/keys.js +451 -0
- package/dist/core/indexing/lexical.js +212 -0
- package/dist/core/indexing/packages.js +57 -0
- package/dist/core/indexing/program.js +171 -0
- package/dist/core/indexing/resolve.js +1004 -0
- package/dist/core/indexing/stamp.js +52 -0
- package/dist/core/indexing/surface.js +335 -0
- package/dist/core/indexing/ui-progress.js +37 -0
- package/dist/core/indexing/verify.js +76 -0
- package/dist/core/kernel/config.js +383 -0
- package/dist/core/kernel/glob.js +105 -0
- package/dist/core/kernel/guards.js +4 -0
- package/dist/core/kernel/types.js +3 -0
- package/dist/core/kernel/verdict.js +142 -0
- package/dist/core/knowledge/anchors.js +139 -0
- package/dist/core/knowledge/knowledge.js +95 -0
- package/dist/core/knowledge/learning.js +102 -0
- package/dist/core/knowledge/render.js +76 -0
- package/dist/core/knowledge/store.js +109 -0
- package/dist/core/knowledge/yaml.js +87 -0
- package/dist/core/search/lexical.js +382 -0
- package/dist/core/search/render.js +98 -0
- package/dist/core/search/segments.js +118 -0
- package/dist/core/verify/parse.js +127 -0
- package/dist/core/verify/render.js +67 -0
- package/dist/core/verify/report.js +40 -0
- package/dist/core/verify/revision.js +79 -0
- package/dist/core/verify/run.js +117 -0
- package/dist/core/verify/store.js +66 -0
- package/dist/core/verify/verify.js +173 -0
- package/dist/core/workspace/init.js +267 -0
- package/dist/core/workspace/probe.js +56 -0
- package/dist/core/workspace/scan.js +420 -0
- package/dist/engineering/baseline.js +337 -0
- package/dist/engineering/change-scope.js +500 -0
- package/dist/engineering/check-changed.js +40 -0
- package/dist/engineering/config.js +129 -0
- package/dist/engineering/dist/policy.d.ts +17 -0
- package/dist/engineering/dist/policy.js +150 -0
- package/dist/engineering/dist/runtime.d.ts +40 -0
- package/dist/engineering/dist/runtime.js +77 -0
- package/dist/engineering/dist/sdlc.d.ts +7 -0
- package/dist/engineering/dist/sdlc.js +27 -0
- package/dist/engineering/eslint-rules/index.js +107 -0
- package/dist/engineering/index.d.ts +7 -0
- package/dist/engineering/index.js +319 -0
- package/dist/engineering/intelligence.js +44 -0
- package/dist/engineering/package.json +36 -0
- package/dist/engineering/policy.js +2 -0
- package/dist/engineering/practice-tool.js +72 -0
- package/dist/engineering/practices.js +283 -0
- package/dist/engineering/practices.json +2453 -0
- package/dist/engineering/review-verdict.js +262 -0
- package/dist/engineering/sdlc.js +2 -0
- package/dist/index.js +20 -0
- package/dist/storage/records.js +245 -0
- package/dist/storage/sqlite.js +314 -0
- package/dist/workflow/catalog.js +69 -0
- package/dist/workflow/classify.js +55 -0
- package/dist/workflow/loader.js +140 -0
- package/dist/workflow/packs.js +469 -0
- package/dist/workflow/registry.js +359 -0
- package/dist/workflow/render.js +61 -0
- package/dist/workflow/router.js +141 -0
- package/dist/workflow/sources.js +229 -0
- package/dist/workflow/types.js +44 -0
- package/dist/workflow/updates.js +532 -0
- package/engineering.lock.json +94 -0
- package/package.json +81 -4
- package/rules/engineering.md +20 -0
- package/scripts/agy-mcp.mjs +27 -0
- package/scripts/mcp/engineering.mjs +91 -0
- package/scripts/mcp/stdio.mjs +304 -0
- package/scripts/mcp/tools.mjs +491 -0
- package/skills/ksd-intelligence/SKILL.md +54 -0
- package/skills/matt-sdlc/SKILL.md +42 -0
- package/skills/osmani-quality/SKILL.md +38 -0
- package/skills/poteto-mode/SKILL.md +40 -0
- package/skills/pstack-analysis/SKILL.md +36 -0
- package/skills.lock.json +71 -0
- package/skills.registry.json +181 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { closeSync, constants, fstatSync, openSync, readSync, realpathSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { collectBaselineEvidence } from "./baseline.js";
|
|
6
|
+
|
|
7
|
+
const CATALOG_URL = new URL("./practices.json", import.meta.url);
|
|
8
|
+
const MAX_CONTEXT_BYTES = 256 * 1024;
|
|
9
|
+
|
|
10
|
+
function isRecord(value) {
|
|
11
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function readJson(path, limit) {
|
|
15
|
+
const fd = openSync(path, constants.O_RDONLY | (constants.O_NONBLOCK ?? 0));
|
|
16
|
+
try {
|
|
17
|
+
const info = fstatSync(fd);
|
|
18
|
+
if (!info.isFile() || info.size > limit) throw new Error("Expected a bounded JSON file");
|
|
19
|
+
const data = Buffer.alloc(limit + 1);
|
|
20
|
+
let length = 0;
|
|
21
|
+
while (length < data.length) {
|
|
22
|
+
const size = readSync(fd, data, length, data.length - length, null);
|
|
23
|
+
if (!size) break;
|
|
24
|
+
length += size;
|
|
25
|
+
}
|
|
26
|
+
if (length > limit) throw new Error("JSON input exceeds size limit");
|
|
27
|
+
|
|
28
|
+
return JSON.parse(data.subarray(0, length).toString("utf8"));
|
|
29
|
+
} finally {
|
|
30
|
+
closeSync(fd);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function readPracticeCatalog() {
|
|
35
|
+
const catalog = readJson(CATALOG_URL, MAX_CONTEXT_BYTES);
|
|
36
|
+
if (catalog.schemaVersion !== 1 || !Array.isArray(catalog.practices) || !Array.isArray(catalog.facets)) throw new Error("Unsupported practice catalog");
|
|
37
|
+
const ids = new Set();
|
|
38
|
+
const facets = new Set(catalog.facets);
|
|
39
|
+
for (const rule of catalog.practices) {
|
|
40
|
+
if (!isRecord(rule) || !/^(?:BL|S|C|T|A|D|R|SEC|F|B|Q|AI)\d{2}$/.test(rule.id) || ids.has(rule.id) ||
|
|
41
|
+
!Number.isInteger(rule.version) || rule.version < 1 || typeof rule.title !== "string" || typeof rule.intent !== "string" ||
|
|
42
|
+
!Array.isArray(rule.allOf) || !Array.isArray(rule.anyOf) || ![...rule.allOf, ...rule.anyOf].every(facet => facets.has(facet)) ||
|
|
43
|
+
!Array.isArray(rule.mechanisms) || !["REQUIRED", "PREFERRED", "ADVISORY"].includes(rule.defaultLevel)) {
|
|
44
|
+
throw new Error("Invalid or duplicate practice catalog entry");
|
|
45
|
+
}
|
|
46
|
+
ids.add(rule.id);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return catalog;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function applicability(rule, facts) {
|
|
53
|
+
const get = name => Object.hasOwn(facts, name) ? facts[name] : undefined;
|
|
54
|
+
if (rule.allOf.some(name => get(name) === false)) return "NOT_APPLICABLE";
|
|
55
|
+
if (rule.anyOf.length && rule.anyOf.every(name => get(name) === false)) return "NOT_APPLICABLE";
|
|
56
|
+
if (rule.allOf.some(name => get(name) === undefined)) return "UNKNOWN";
|
|
57
|
+
if (rule.anyOf.length && !rule.anyOf.some(name => get(name) === true)) return "UNKNOWN";
|
|
58
|
+
|
|
59
|
+
return "APPLICABLE";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function validateContext(input, catalog) {
|
|
63
|
+
if (!isRecord(input) || !isRecord(input.facets)) throw new Error("Context requires a facets object with true/false observations");
|
|
64
|
+
const validFacets = new Set(catalog.facets);
|
|
65
|
+
const ids = new Set(catalog.practices.map(rule => rule.id));
|
|
66
|
+
for (const [key, value] of Object.entries(input.facets)) {
|
|
67
|
+
if (!validFacets.has(key) || typeof value !== "boolean") throw new Error("Unknown facet or non-boolean observation: " + key);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const overrides = input.projectOverrides ?? {};
|
|
71
|
+
if (!isRecord(overrides)) throw new Error("projectOverrides must be an object");
|
|
72
|
+
for (const [id, decision] of Object.entries(overrides)) {
|
|
73
|
+
if (!ids.has(id) || !isRecord(decision) || !["required", "prefer-project", "exception"].includes(decision.disposition) ||
|
|
74
|
+
typeof decision.reason !== "string" || !decision.reason.trim() ||
|
|
75
|
+
typeof decision.evidence !== "string" || !decision.evidence.trim()) {
|
|
76
|
+
throw new Error("Invalid or unsupported project override: " + id);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const gates = input.enabledGates ?? [];
|
|
81
|
+
const optionalGates = new Set(catalog.practices.filter(rule => rule.mechanisms.includes("optional-native-gate")).map(rule => rule.id));
|
|
82
|
+
if (!Array.isArray(gates) || gates.some(id => !optionalGates.has(id))) throw new Error("enabledGates must contain known optional-native-gate practice IDs");
|
|
83
|
+
if (input.mode !== undefined && !["balanced", "adaptive"].includes(input.mode)) throw new Error("Unsupported enforcement mode");
|
|
84
|
+
|
|
85
|
+
return { overrides, gates: new Set(gates) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Decisions describe supplied evidence, not an authenticated repository scan or executed gate. */
|
|
89
|
+
export function resolvePractices(input, catalog = readPracticeCatalog()) {
|
|
90
|
+
const { overrides, gates } = validateContext(input, catalog);
|
|
91
|
+
const mode = input.mode ?? "balanced";
|
|
92
|
+
const decisions = catalog.practices.map(rule => {
|
|
93
|
+
const status = applicability(rule, input.facets);
|
|
94
|
+
const override = Object.hasOwn(overrides, rule.id) ? overrides[rule.id] : undefined;
|
|
95
|
+
const nativeGate = gates.has(rule.id) && rule.mechanisms.includes("optional-native-gate");
|
|
96
|
+
const decision = { id: rule.id, version: rule.version, title: rule.title, group: rule.group, intent: rule.intent, status, level: rule.defaultLevel,
|
|
97
|
+
mechanisms: rule.mechanisms, safetyInvariant: rule.safetyInvariant === true, reason: "Applicable to supplied task facets" };
|
|
98
|
+
if (status !== "APPLICABLE") {
|
|
99
|
+
if (override?.disposition === "required" || nativeGate) {
|
|
100
|
+
return { ...decision, status: "CONFLICT", level: "REQUIRED", reason: "Explicit project gate conflicts with missing or negative applicability; resolve with evidence", project: override };
|
|
101
|
+
}
|
|
102
|
+
return { ...decision, level: status === "NOT_APPLICABLE" ? "NOT_APPLICABLE" : "ADVISORY",
|
|
103
|
+
reason: status === "UNKNOWN" ? "Missing task/baseline evidence; do not claim pass" : "Supplied facets exclude practice" };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (override && override.disposition !== "required") {
|
|
107
|
+
if (rule.safetyInvariant || rule.defaultLevel === "REQUIRED" || nativeGate) {
|
|
108
|
+
return { ...decision, status: "CONFLICT", level: "REQUIRED", reason: "Project exception conflicts with required invariant or native gate; resolve explicitly", project: override };
|
|
109
|
+
}
|
|
110
|
+
return { ...decision, status: "SUPPRESSED", level: "ADVISORY", reason: "Scoped project convention or exception takes precedence", project: override };
|
|
111
|
+
}
|
|
112
|
+
if (override?.disposition === "required" || nativeGate) {
|
|
113
|
+
return { ...decision, level: "REQUIRED", reason: "Explicitly supplied project rule or opted-in native gate", project: override };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (rule.id === "S01") {
|
|
117
|
+
return { ...decision, level: mode === "balanced" ? "ADVISORY" : "PREFERRED",
|
|
118
|
+
reviewDisposition: mode === "adaptive" ? "FIX_REQUIRED_IF_BEHAVIOR_PRESERVING" : "ADVISORY" };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return decision;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
return { schemaVersion: 1, mode, evidence: "SUPPLIED_NOT_AUTHENTICATED", decisions };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// A fresh hash binds the supplied interpretation to the sampled bytes, not to the truth of the interpretation.
|
|
128
|
+
function checkProvenance(provenance, paths) {
|
|
129
|
+
if (!isRecord(provenance) || !Array.isArray(provenance.paths) || provenance.paths.length < 1 || provenance.paths.length > 24 ||
|
|
130
|
+
!provenance.paths.every(path => typeof path === "string" && paths.has(path)) ||
|
|
131
|
+
!["CURRENT", "LEGACY", "TRANSITIONAL", "LOCAL", "EXCEPTION", "UNKNOWN"].includes(provenance.classification) ||
|
|
132
|
+
!["HIGH", "MEDIUM", "LOW", "UNKNOWN"].includes(provenance.confidence)) {
|
|
133
|
+
throw new Error("Invalid or unsampled observation provenance");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return ["HIGH", "MEDIUM"].includes(provenance.confidence) && ["CURRENT", "LOCAL", "EXCEPTION"].includes(provenance.classification);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Explicit opt-in; root is the operator/host-selected root, never an untrusted context field. */
|
|
140
|
+
export function resolveFreshPractices(input, { root, useGit = false }, catalog = readPracticeCatalog()) {
|
|
141
|
+
validateContext(input, catalog);
|
|
142
|
+
const baseline = input.baseline;
|
|
143
|
+
if (!isRecord(baseline) || typeof baseline.scope !== "string" || typeof baseline.fingerprint !== "string" ||
|
|
144
|
+
!/^[a-f0-9]{64}$/.test(baseline.fingerprint) || Object.hasOwn(baseline, "root") || Object.hasOwn(baseline, "useGit")) {
|
|
145
|
+
throw new Error("Expected a scoped baseline fingerprint; select the root and native Git option outside the context");
|
|
146
|
+
}
|
|
147
|
+
const snapshot = collectBaselineEvidence({ root, scope: baseline.scope, additionalPaths: baseline.additionalPaths ?? [], useGit });
|
|
148
|
+
if (!snapshot.cacheable || snapshot.fingerprint !== baseline.fingerprint) throw new Error("STALE_OR_INCOMPLETE_BASELINE: recapture and review scoped evidence");
|
|
149
|
+
const paths = new Set(snapshot.evidence.map(item => item.path));
|
|
150
|
+
if (!isRecord(input.observations)) throw new Error("Fresh selection requires observation provenance");
|
|
151
|
+
|
|
152
|
+
const facets = {};
|
|
153
|
+
const unresolved = [];
|
|
154
|
+
for (const [name, value] of Object.entries(input.facets)) {
|
|
155
|
+
if (!Object.hasOwn(input.observations, name)) throw new Error("Missing facet provenance: " + name);
|
|
156
|
+
if (checkProvenance(input.observations[name], paths)) facets[name] = value;
|
|
157
|
+
else unresolved.push(name);
|
|
158
|
+
}
|
|
159
|
+
const overrides = input.projectOverrides ?? {};
|
|
160
|
+
for (const [id, decision] of Object.entries(overrides)) {
|
|
161
|
+
if (!checkProvenance(decision.provenance, paths)) throw new Error("Uncertain project override: " + id);
|
|
162
|
+
}
|
|
163
|
+
const gates = input.enabledGates ?? [];
|
|
164
|
+
if (gates.length && !isRecord(input.gateEvidence)) throw new Error("Enabled native gates require provenance");
|
|
165
|
+
for (const id of gates) {
|
|
166
|
+
if (!Object.hasOwn(input.gateEvidence, id) || !checkProvenance(input.gateEvidence[id], paths)) throw new Error("Uncertain native gate: " + id);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const selection = resolvePractices({ ...input, facets }, catalog);
|
|
170
|
+
|
|
171
|
+
return { ...selection, evidence: "FRESH_SAMPLED_BYTES_SUPPLIED_SEMANTICS", baseline: { scope: snapshot.scope, fingerprint: snapshot.fingerprint, revision: snapshot.revision, coverage: snapshot.coverage },
|
|
172
|
+
unresolvedFacets: unresolved.sort(), limitation: "Fresh sampled bytes only; supplied classifications, task semantics and native gate claims are not independently authenticated or executed." };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function renderPracticeSelection(result) {
|
|
176
|
+
const selected = result.decisions.filter(rule => rule.status === "APPLICABLE" || rule.status === "CONFLICT");
|
|
177
|
+
const lines = selected.map(rule => `${rule.id} [${rule.status}/${rule.level}] ${rule.title}: ${rule.intent}${rule.status === "CONFLICT" ? " — " + rule.reason : ""}`);
|
|
178
|
+
const unknown = result.decisions.filter(rule => rule.status === "UNKNOWN").map(rule => rule.id);
|
|
179
|
+
|
|
180
|
+
return [`Practices: ${selected.length} selected/conflicting; ${unknown.length} unresolved. Evidence: ${result.evidence}; semantics not authenticated.`, ...lines, ...(unknown.length ? ["Unknown applicability: " + unknown.join(", ")] : [])].join("\n");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Compose only from a fresh resolution, not arbitrary user-authored instructions.
|
|
184
|
+
// Never include raw observation reasons or file contents in the pack.
|
|
185
|
+
export function composePracticePack(selection, { group, maxChars = 6000 } = {}) {
|
|
186
|
+
if (selection?.evidence !== "FRESH_SAMPLED_BYTES_SUPPLIED_SEMANTICS" || !Array.isArray(selection.decisions)) {
|
|
187
|
+
throw new Error("A policy pack requires a fresh scoped practice selection");
|
|
188
|
+
}
|
|
189
|
+
if (!Number.isInteger(maxChars) || maxChars < 1000 || maxChars > 12000) throw new Error("Invalid policy pack limit");
|
|
190
|
+
if (group !== undefined && !selection.decisions.some(rule => rule.group === group)) throw new Error("Unknown practice group");
|
|
191
|
+
const all = selection.decisions;
|
|
192
|
+
const relevant = all.filter(rule => group === undefined || rule.group === group);
|
|
193
|
+
const conflicts = all.filter(rule => rule.status === "CONFLICT").map(rule => rule.id);
|
|
194
|
+
const unknownRequired = all.filter(rule => rule.status === "UNKNOWN" && rule.safetyInvariant).map(rule => rule.id);
|
|
195
|
+
const suppressed = relevant.filter(rule => rule.status === "SUPPRESSED").map(rule => rule.id);
|
|
196
|
+
const selected = relevant.filter(rule => rule.status === "APPLICABLE");
|
|
197
|
+
const priority = { REQUIRED: 0, PREFERRED: 1, ADVISORY: 2 };
|
|
198
|
+
selected.sort((a, b) => priority[a.level] - priority[b.level] || a.id.localeCompare(b.id, "en"));
|
|
199
|
+
const prefix = [
|
|
200
|
+
"Scoped erosiano-dev practice pack (not an authoritative project rule or verification).",
|
|
201
|
+
"Fresh sampled bytes only; interpretations are supplied. Existing contracts, project instructions, security and native gates remain authoritative.",
|
|
202
|
+
"Review current evidence before changes. Refresh this selection if relevant files, scope or task change.",
|
|
203
|
+
"Mode: " + selection.mode + "; group: " + (group ?? "all") + "; sampled fingerprint: " + selection.baseline.fingerprint,
|
|
204
|
+
"Conflicts across all groups: " + (conflicts.join(", ") || "none"),
|
|
205
|
+
"Unknown safety/required-risk applicability across all groups: " + (unknownRequired.join(", ") || "none"),
|
|
206
|
+
"Suppressed project preferences in this group: " + (suppressed.join(", ") || "none"),
|
|
207
|
+
"Resolve conflicts and applicable unknown risks before claiming verification. Do not treat omitted or suppressed preferences as waived native gates.",
|
|
208
|
+
].join("\n");
|
|
209
|
+
const included = [];
|
|
210
|
+
const omitted = [];
|
|
211
|
+
const lines = [];
|
|
212
|
+
// Reserve room for a complete list of omitted IDs even in a dense all-group selection.
|
|
213
|
+
const budget = maxChars - prefix.length - 1100;
|
|
214
|
+
let used = 0;
|
|
215
|
+
for (const rule of selected) {
|
|
216
|
+
const line = rule.id + " v" + rule.version + " [" + rule.level + "] " + rule.title + ": " + rule.intent;
|
|
217
|
+
if (used + line.length + 1 > budget) {
|
|
218
|
+
omitted.push(rule.id);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
lines.push(line);
|
|
222
|
+
included.push(rule.id);
|
|
223
|
+
used += line.length + 1;
|
|
224
|
+
}
|
|
225
|
+
const outside = group !== undefined ? all.filter(rule => rule.group !== group && rule.status === "APPLICABLE").length : 0;
|
|
226
|
+
const footer = "Omitted applicable IDs due to output bound: " + (omitted.join(", ") || "none") +
|
|
227
|
+
". Applicable outside requested group: " + outside + ". Request a narrower group or inspect omitted IDs; no omission is a pass.";
|
|
228
|
+
const text = [prefix, ...lines, footer].join("\n");
|
|
229
|
+
if (text.length > maxChars) {
|
|
230
|
+
return { status: "NARROW_GROUP_REQUIRED", text: "Policy output exceeds bound. Request a group; no selected, conflicting or unknown rule is waived.", included: [], omitted: selected.map(rule => rule.id), conflicts, unknownRequired };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return { status: conflicts.length ? "CONFLICT" : omitted.length || outside ? "PARTIAL_PACK" : "PACK", text, included, omitted, conflicts, unknownRequired };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function main(args) {
|
|
237
|
+
if (args.length === 2 && args[0] === "--group") {
|
|
238
|
+
const catalog = readPracticeCatalog();
|
|
239
|
+
const rules = catalog.practices.filter(rule => rule.group === args[1]);
|
|
240
|
+
if (!rules.length) throw new Error("Unknown practice group");
|
|
241
|
+
console.log(JSON.stringify(rules, null, 2));
|
|
242
|
+
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const usage = "Usage: erosiano-practices --context context.json [--root project [--git]] [--policy [--group group]] | --group group";
|
|
246
|
+
if (args[0] !== "--context" || !args[1] || args[1].startsWith("--")) throw new Error(usage);
|
|
247
|
+
const options = {};
|
|
248
|
+
for (let i = 2; i < args.length; i++) {
|
|
249
|
+
const flag = args[i];
|
|
250
|
+
if (flag === "--git" || flag === "--policy") {
|
|
251
|
+
if (Object.hasOwn(options, flag)) throw new Error(usage);
|
|
252
|
+
options[flag] = true;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
if (!["--root", "--group"].includes(flag) || Object.hasOwn(options, flag) || !args[i + 1] || args[i + 1].startsWith("--")) throw new Error(usage);
|
|
256
|
+
options[flag] = args[++i];
|
|
257
|
+
}
|
|
258
|
+
if ((options["--git"] || options["--policy"]) && !options["--root"]) throw new Error(usage);
|
|
259
|
+
if (options["--group"] && !options["--policy"]) throw new Error(usage);
|
|
260
|
+
const input = readJson(resolve(args[1]), MAX_CONTEXT_BYTES);
|
|
261
|
+
if (!options["--root"] && Object.hasOwn(input ?? {}, "baseline")) throw new Error("Fresh context requires an operator-selected --root");
|
|
262
|
+
const result = options["--root"] ? resolveFreshPractices(input, { root: options["--root"], useGit: options["--git"] === true }) : resolvePractices(input);
|
|
263
|
+
console.log(JSON.stringify(options["--policy"] ? composePracticePack(result, { group: options["--group"] }) : result, null, 2));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function isMainModule() {
|
|
267
|
+
try {
|
|
268
|
+
if (!process.argv[1]) return false;
|
|
269
|
+
|
|
270
|
+
return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
|
|
271
|
+
} catch {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (isMainModule()) {
|
|
277
|
+
try {
|
|
278
|
+
main(process.argv.slice(2));
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.error("Practice selection NOT_ASSESSED: " + error.message);
|
|
281
|
+
process.exitCode = 2;
|
|
282
|
+
}
|
|
283
|
+
}
|