ctxmedic 0.10.0 → 0.11.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/dist/index.js +106 -28
- package/package.json +1 -1
- package/schema.json +57 -0
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var RuleId = z.enum([
|
|
|
21
21
|
"ERR_INVALID_SKILL",
|
|
22
22
|
"CRIT_SECRET_LEAK",
|
|
23
23
|
"INFO_UNSUPPORTED_SYNTAX",
|
|
24
|
+
"INFO_UNUSED_DISABLE",
|
|
24
25
|
"FIN_BLOAT_IGNORE",
|
|
25
26
|
"FIN_UNUSED_MCP",
|
|
26
27
|
"FIN_MODEL_OVERKILL",
|
|
@@ -56,6 +57,8 @@ var Finding = z.object({
|
|
|
56
57
|
severity: Severity,
|
|
57
58
|
message: z.string(),
|
|
58
59
|
files: z.array(z.string()).default([]),
|
|
60
|
+
/** 1-based line in `files[0]`, when the finding points at one line (line-level disable comments use it). */
|
|
61
|
+
line: z.number().int().positive().optional(),
|
|
59
62
|
/** Raw lab values used by the clinical layer (token counts, ratios, $). */
|
|
60
63
|
data: z.record(z.string(), z.unknown()).default({}),
|
|
61
64
|
monthlySavingsUsd: z.number().nonnegative().optional(),
|
|
@@ -787,6 +790,17 @@ var finopsMeta = {
|
|
|
787
790
|
|
|
788
791
|
// ../core/src/rules/index.ts
|
|
789
792
|
var rules = [brokenLink, contextBloat, duplicates, skills, secretLeak, syntax, bloatIgnore];
|
|
793
|
+
var unusedDisableMeta = {
|
|
794
|
+
description: "Remove ctxmedic-disable comments that silence nothing",
|
|
795
|
+
type: "suggestion",
|
|
796
|
+
docs: docsUrl("INFO_UNUSED_DISABLE"),
|
|
797
|
+
fixable: false
|
|
798
|
+
};
|
|
799
|
+
var allRuleMeta = () => [
|
|
800
|
+
...rules.map((r) => [r.id, r.meta, "config"]),
|
|
801
|
+
["INFO_UNUSED_DISABLE", unusedDisableMeta, "config"],
|
|
802
|
+
...Object.entries(finopsMeta).map(([id, meta2]) => [id, meta2, "session-logs"])
|
|
803
|
+
];
|
|
790
804
|
async function runRules(ctx2, list = rules, options = {}, off = /* @__PURE__ */ new Set()) {
|
|
791
805
|
const out = [];
|
|
792
806
|
for (const r of list) {
|
|
@@ -906,11 +920,36 @@ import fs6 from "fs";
|
|
|
906
920
|
import path9 from "path";
|
|
907
921
|
import picomatch2 from "picomatch";
|
|
908
922
|
import { z as z5 } from "zod";
|
|
923
|
+
|
|
924
|
+
// ../core/src/presets.ts
|
|
925
|
+
var PRESET_NAMES = ["ctxmedic:recommended", "ctxmedic:strict"];
|
|
926
|
+
var PRESETS = {
|
|
927
|
+
// Today's defaults, spelled out so a project can say it chose them.
|
|
928
|
+
"ctxmedic:recommended": { rules: {} },
|
|
929
|
+
// Stricter thresholds, and info-only rules raised to warnings. Rules that grade their own findings
|
|
930
|
+
// (secrets, broken links, bloat) keep "on": forcing a severity would demote their critical findings.
|
|
931
|
+
"ctxmedic:strict": {
|
|
932
|
+
rules: {
|
|
933
|
+
WARN_CONTEXT_BLOAT: ["on", { tokens: 2e3 }],
|
|
934
|
+
WARN_DUPLICATE_RULES: ["on", { similarity: 0.6 }],
|
|
935
|
+
INFO_UNSUPPORTED_SYNTAX: "warning",
|
|
936
|
+
INFO_UNUSED_DISABLE: "warning",
|
|
937
|
+
FIN_CONTEXT_SNOWBALL: ["on", { steps: 25 }],
|
|
938
|
+
FIN_CACHE_MISS: ["on", { minHitRatio: 0.5 }],
|
|
939
|
+
FIN_MODEL_OVERKILL: ["on", { minSessions: 2 }]
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
|
|
944
|
+
// ../core/src/config.ts
|
|
909
945
|
var CONFIG_FILE = "ctxmedic.config.json";
|
|
910
946
|
var RuleLevel = z5.union([z5.literal("off"), z5.literal("on"), Severity]);
|
|
911
947
|
var RuleEntry = z5.union([RuleLevel, z5.tuple([RuleLevel]), z5.tuple([RuleLevel, z5.record(z5.string(), z5.unknown())])]);
|
|
948
|
+
var Preset = z5.enum(PRESET_NAMES);
|
|
912
949
|
var CtxmedicConfig = z5.object({
|
|
913
950
|
$schema: z5.string().optional(),
|
|
951
|
+
/** Built-in presets applied in order before this file's own `rules` and `ignores`. */
|
|
952
|
+
extends: z5.union([Preset, z5.array(Preset)]).optional(),
|
|
914
953
|
rules: z5.partialRecord(RuleId, RuleEntry).default({}),
|
|
915
954
|
/** Globs over finding paths (project-relative, or `~/` for global files). */
|
|
916
955
|
ignores: z5.array(z5.string()).default([])
|
|
@@ -919,6 +958,19 @@ var ConfigError = class extends Error {
|
|
|
919
958
|
};
|
|
920
959
|
var levelOf = (e) => e === void 0 ? "on" : typeof e === "string" ? e : e[0];
|
|
921
960
|
var optionsOf = (e) => Array.isArray(e) && e[1] || {};
|
|
961
|
+
function resolveConfig(c) {
|
|
962
|
+
const names = c.extends === void 0 ? [] : typeof c.extends === "string" ? [c.extends] : c.extends;
|
|
963
|
+
const rules3 = {};
|
|
964
|
+
const ignores = [];
|
|
965
|
+
for (const layer of [...names.map((n2) => PRESETS[n2]), { rules: c.rules, ignores: c.ignores }]) {
|
|
966
|
+
for (const [id, entry] of Object.entries(layer.rules)) {
|
|
967
|
+
const before = optionsOf(rules3[id]);
|
|
968
|
+
rules3[id] = !Object.keys(optionsOf(entry)).length && Object.keys(before).length ? [levelOf(entry), before] : entry;
|
|
969
|
+
}
|
|
970
|
+
ignores.push(...layer.ignores ?? []);
|
|
971
|
+
}
|
|
972
|
+
return { ...c, rules: rules3, ignores };
|
|
973
|
+
}
|
|
922
974
|
var RuleInfo = z5.object({
|
|
923
975
|
id: RuleId,
|
|
924
976
|
/** "config": reads config files; "session-logs": a FIN_* detector that reads session logs. */
|
|
@@ -934,11 +986,7 @@ var RuleInfo = z5.object({
|
|
|
934
986
|
changed: z5.record(z5.string(), z5.unknown())
|
|
935
987
|
});
|
|
936
988
|
function describeRules(config) {
|
|
937
|
-
|
|
938
|
-
...rules.map((r) => [r.id, r.meta, "config"]),
|
|
939
|
-
...Object.entries(finopsMeta).map(([id, meta2]) => [id, meta2, "session-logs"])
|
|
940
|
-
];
|
|
941
|
-
return all.map(([id, meta2, source]) => {
|
|
989
|
+
return allRuleMeta().map(([id, meta2, source]) => {
|
|
942
990
|
const entry = config.rules[id];
|
|
943
991
|
const options = meta2.options?.parse(optionsOf(entry)) ?? {};
|
|
944
992
|
const defaults = meta2.options?.parse({}) ?? {};
|
|
@@ -957,41 +1005,65 @@ function loadConfig(root) {
|
|
|
957
1005
|
}
|
|
958
1006
|
const r = CtxmedicConfig.safeParse(raw);
|
|
959
1007
|
if (!r.success) throw new ConfigError(`${CONFIG_FILE}: ${z5.prettifyError(r.error)}`);
|
|
960
|
-
|
|
1008
|
+
const config = resolveConfig(r.data);
|
|
1009
|
+
for (const [id, entry] of Object.entries(config.rules)) {
|
|
961
1010
|
const opts = optionsOf(entry);
|
|
962
1011
|
if (!Object.keys(opts).length) continue;
|
|
963
|
-
const schema = (
|
|
1012
|
+
const schema = allRuleMeta().find(([x]) => x === id)?.[1].options;
|
|
964
1013
|
if (!schema) throw new ConfigError(`${CONFIG_FILE}: rule ${id} takes no options`);
|
|
965
1014
|
const o2 = schema.safeParse(opts);
|
|
966
1015
|
if (!o2.success) throw new ConfigError(`${CONFIG_FILE}: rule ${id}: ${z5.prettifyError(o2.error)}`);
|
|
967
1016
|
}
|
|
968
|
-
return
|
|
1017
|
+
return config;
|
|
969
1018
|
}
|
|
970
1019
|
function configHash(c) {
|
|
971
1020
|
if (!Object.keys(c.rules).length && !c.ignores.length) return "";
|
|
972
1021
|
const rulesSorted = Object.fromEntries(Object.entries(c.rules).sort(([a2], [b]) => a2.localeCompare(b)));
|
|
973
1022
|
return crypto2.createHash("sha256").update(JSON.stringify([rulesSorted, c.ignores])).digest("hex").slice(0, 12);
|
|
974
1023
|
}
|
|
975
|
-
var DIRECTIVE = /<!--\s*ctxmedic-disable
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
const
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
1024
|
+
var DIRECTIVE = /<!--\s*ctxmedic-disable(-next-line|-line)?(?!\w|-(?!->))([\s\S]*?)-->/g;
|
|
1025
|
+
var lineAt = (text2, i) => text2.slice(0, i).split("\n").length;
|
|
1026
|
+
function directivesIn(text2) {
|
|
1027
|
+
return [...text2.matchAll(DIRECTIVE)].map((m) => {
|
|
1028
|
+
const kind = m[1] === "-next-line" ? "next-line" : m[1] === "-line" ? "line" : "file";
|
|
1029
|
+
const line = lineAt(text2, m.index);
|
|
1030
|
+
const ids = new Set(m[2].split("--")[0].split(/[\s,]+/).filter(Boolean));
|
|
1031
|
+
const target = kind === "line" ? line : kind === "next-line" ? lineAt(text2, m.index + m[0].length) + 1 : void 0;
|
|
1032
|
+
return { kind, ids, line, target };
|
|
1033
|
+
});
|
|
984
1034
|
}
|
|
985
|
-
function applyConfig(findings, files, config) {
|
|
1035
|
+
function applyConfig(findings, files, config, opts = {}) {
|
|
986
1036
|
const ignored = config.ignores.length ? picomatch2(config.ignores, { dot: true }) : () => false;
|
|
987
|
-
const
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
if (f.files.length && f.files.every((p3) => ignored(p3))) return
|
|
992
|
-
|
|
993
|
-
|
|
1037
|
+
const comments = new Map(files.filter((f) => f.text?.includes("ctxmedic-disable")).map((f) => [f.rel, directivesIn(f.text)]));
|
|
1038
|
+
const used = /* @__PURE__ */ new Set();
|
|
1039
|
+
const level = (f) => {
|
|
1040
|
+
const l = levelOf(config.rules[f.ruleId]);
|
|
1041
|
+
if (l === "off" || f.files.length && f.files.every((p3) => ignored(p3))) return null;
|
|
1042
|
+
return l === "on" ? f : { ...f, severity: l };
|
|
1043
|
+
};
|
|
1044
|
+
const out = findings.flatMap((f) => {
|
|
1045
|
+
const kept = level(f);
|
|
1046
|
+
if (!kept) return [];
|
|
1047
|
+
const hits = f.files.flatMap(
|
|
1048
|
+
(p3, i) => (comments.get(p3) ?? []).filter(
|
|
1049
|
+
(d2) => (!d2.ids.size || d2.ids.has(f.ruleId)) && (d2.kind === "file" || i === 0 && f.line !== void 0 && d2.target === f.line)
|
|
1050
|
+
)
|
|
1051
|
+
);
|
|
1052
|
+
for (const d2 of hits) used.add(d2);
|
|
1053
|
+
return hits.length ? [] : [kept];
|
|
994
1054
|
});
|
|
1055
|
+
if (!opts.reportUnused) return out;
|
|
1056
|
+
const unused = [...comments].flatMap(
|
|
1057
|
+
([rel, ds]) => ds.filter((d2) => !used.has(d2)).map((d2) => ({
|
|
1058
|
+
ruleId: "INFO_UNUSED_DISABLE",
|
|
1059
|
+
severity: "info",
|
|
1060
|
+
message: `${rel}:${d2.line}: ctxmedic-disable${d2.kind === "file" ? "" : `-${d2.kind}`} silences nothing`,
|
|
1061
|
+
files: [rel],
|
|
1062
|
+
line: d2.line,
|
|
1063
|
+
data: { ids: [...d2.ids], kind: d2.kind }
|
|
1064
|
+
}))
|
|
1065
|
+
);
|
|
1066
|
+
return [...out, ...unused.flatMap((f) => level(f) ?? [])];
|
|
995
1067
|
}
|
|
996
1068
|
|
|
997
1069
|
// ../core/src/examine.ts
|
|
@@ -1003,7 +1075,7 @@ async function examine(root, profiles, opts = {}) {
|
|
|
1003
1075
|
const entries = Object.entries(config.rules);
|
|
1004
1076
|
const off = new Set(entries.filter(([, e]) => levelOf(e) === "off").map(([id]) => id));
|
|
1005
1077
|
const options = Object.fromEntries(entries.map(([id, e]) => [id, optionsOf(e)]));
|
|
1006
|
-
const findings = applyConfig(await runRules({ root, files, profiles, startup }, void 0, options, off), files, config);
|
|
1078
|
+
const findings = applyConfig(await runRules({ root, files, profiles, startup }, void 0, options, off), files, config, { reportUnused: true });
|
|
1007
1079
|
const vitals2 = PatientVitals.parse({
|
|
1008
1080
|
root,
|
|
1009
1081
|
scannedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
@@ -1505,6 +1577,11 @@ var en = {
|
|
|
1505
1577
|
prognosis: (f) => `${d(f, "harness")} reads ${(d(f, "directives") ?? []).join(", ")} as plain text: noise at best, wrong instructions at worst.`,
|
|
1506
1578
|
action: () => "Move tool-specific directives into that tool's own file, or keep shared files directive-free."
|
|
1507
1579
|
},
|
|
1580
|
+
INFO_UNUSED_DISABLE: {
|
|
1581
|
+
name: "Phantom Prescription",
|
|
1582
|
+
prognosis: (f) => `${f.files[0]}:${f.line} silences ${(d(f, "ids") ?? []).join(", ") || "every rule"}, but nothing there needed it. A stale disable hides the next real problem.`,
|
|
1583
|
+
action: (f) => `Delete the ctxmedic-disable comment on line ${f.line} of ${f.files[0]}.`
|
|
1584
|
+
},
|
|
1508
1585
|
FIN_BLOAT_IGNORE: {
|
|
1509
1586
|
name: "Indexing Bloat",
|
|
1510
1587
|
prognosis: (f) => `${d(f, "harness")} indexes build output and lockfiles. Search results fill with junk and retrieval burns tokens.`,
|
|
@@ -1726,13 +1803,14 @@ var harnesses = [
|
|
|
1726
1803
|
];
|
|
1727
1804
|
|
|
1728
1805
|
// ../clinical/src/engine.ts
|
|
1729
|
-
var VERSION = true ? "0.
|
|
1806
|
+
var VERSION = true ? "0.11.0" : "0.0.0-dev";
|
|
1730
1807
|
var TRIAGE = { critical: "resuscitation", warning: "therapy", info: "preventive" };
|
|
1731
1808
|
var ORDER = ["resuscitation", "therapy", "preventive"];
|
|
1732
1809
|
var human = (k) => k.replace(/([A-Z])/g, " $1").replace(/^./, (c) => c.toUpperCase());
|
|
1733
1810
|
function vitals(f) {
|
|
1734
1811
|
const out = [];
|
|
1735
|
-
|
|
1812
|
+
const shown = f.files.slice(0, 3).map((p3, i) => i === 0 && f.line ? `${p3}:${f.line}` : p3);
|
|
1813
|
+
if (f.files.length) out.push({ label: "Files", value: shown.join(", ") + (f.files.length > 3 ? ` +${f.files.length - 3}` : "") });
|
|
1736
1814
|
for (const [k, v] of Object.entries(f.data)) {
|
|
1737
1815
|
if (typeof v === "number")
|
|
1738
1816
|
out.push({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ctxmedic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "The context medic for your AI coding setup: checkup, treat, savings for Claude Code, Cursor, Windsurf, Codex CLI, OpenCode, Cline and Copilot.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "FSL-1.1-MIT",
|
package/schema.json
CHANGED
|
@@ -5,6 +5,28 @@
|
|
|
5
5
|
"$schema": {
|
|
6
6
|
"type": "string"
|
|
7
7
|
},
|
|
8
|
+
"extends": {
|
|
9
|
+
"anyOf": [
|
|
10
|
+
{
|
|
11
|
+
"type": "string",
|
|
12
|
+
"enum": [
|
|
13
|
+
"ctxmedic:recommended",
|
|
14
|
+
"ctxmedic:strict"
|
|
15
|
+
]
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"type": "array",
|
|
19
|
+
"items": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"enum": [
|
|
22
|
+
"ctxmedic:recommended",
|
|
23
|
+
"ctxmedic:strict"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"description": "Built-in presets applied before this file's rules: ctxmedic:recommended, ctxmedic:strict."
|
|
29
|
+
},
|
|
8
30
|
"rules": {
|
|
9
31
|
"type": "object",
|
|
10
32
|
"properties": {
|
|
@@ -265,6 +287,41 @@
|
|
|
265
287
|
],
|
|
266
288
|
"description": "Keep build output and lockfiles out of the AI index with an ignore file. https://github.com/ponomarenko/ctxmedic/blob/main/docs/rules/FIN_BLOAT_IGNORE.md"
|
|
267
289
|
},
|
|
290
|
+
"INFO_UNUSED_DISABLE": {
|
|
291
|
+
"anyOf": [
|
|
292
|
+
{
|
|
293
|
+
"$ref": "#/definitions/RuleLevel"
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
"type": "array",
|
|
297
|
+
"items": [
|
|
298
|
+
{
|
|
299
|
+
"$ref": "#/definitions/RuleLevel"
|
|
300
|
+
}
|
|
301
|
+
],
|
|
302
|
+
"additionalItems": false,
|
|
303
|
+
"minItems": 1,
|
|
304
|
+
"maxItems": 1
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
"type": "array",
|
|
308
|
+
"items": [
|
|
309
|
+
{
|
|
310
|
+
"$ref": "#/definitions/RuleLevel"
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
"type": "object",
|
|
314
|
+
"properties": {},
|
|
315
|
+
"additionalProperties": false
|
|
316
|
+
}
|
|
317
|
+
],
|
|
318
|
+
"additionalItems": false,
|
|
319
|
+
"minItems": 2,
|
|
320
|
+
"maxItems": 2
|
|
321
|
+
}
|
|
322
|
+
],
|
|
323
|
+
"description": "Remove ctxmedic-disable comments that silence nothing. https://github.com/ponomarenko/ctxmedic/blob/main/docs/rules/INFO_UNUSED_DISABLE.md"
|
|
324
|
+
},
|
|
268
325
|
"FIN_UNUSED_MCP": {
|
|
269
326
|
"anyOf": [
|
|
270
327
|
{
|