textoneitor 0.0.0-stage → 1.2.2
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/LICENSE +101 -0
- package/LICENSE-AGPL-3.0 +661 -0
- package/README.md +69 -2
- package/dist/api/index.d.ts +34 -0
- package/dist/api/index.js +98 -0
- package/dist/baseline/index.d.ts +20 -0
- package/dist/baseline/index.js +81 -0
- package/dist/bundle/dist/cli.mjs +29893 -0
- package/dist/bundle/package.json +6 -0
- package/dist/bundle/rulepack/rulepack.json +1496 -0
- package/dist/bundle/rules/situaciones.yml +27 -0
- package/dist/cli/benchmark.d.ts +89 -0
- package/dist/cli/benchmark.js +185 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +377 -0
- package/dist/config/core.d.ts +55 -0
- package/dist/config/core.js +244 -0
- package/dist/config/glob.d.ts +2 -0
- package/dist/config/glob.js +72 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.js +38 -0
- package/dist/config/situaciones.d.ts +17 -0
- package/dist/config/situaciones.js +43 -0
- package/dist/contracts/index.d.ts +238 -0
- package/dist/contracts/index.js +4 -0
- package/dist/document/index.d.ts +10 -0
- package/dist/document/index.js +70 -0
- package/dist/document/text.d.ts +22 -0
- package/dist/document/text.js +91 -0
- package/dist/document/tokenize.d.ts +4 -0
- package/dist/document/tokenize.js +59 -0
- package/dist/markdown/index.d.ts +7 -0
- package/dist/markdown/index.js +178 -0
- package/dist/paths.d.ts +4 -0
- package/dist/paths.js +14 -0
- package/dist/reporters/index.d.ts +29 -0
- package/dist/reporters/index.js +182 -0
- package/dist/rules/compiler/build.d.ts +1 -0
- package/dist/rules/compiler/build.js +46 -0
- package/dist/rules/compiler/index.d.ts +40 -0
- package/dist/rules/compiler/index.js +343 -0
- package/dist/rules/compiler/safety.d.ts +16 -0
- package/dist/rules/compiler/safety.js +124 -0
- package/dist/rules/detectors/index.d.ts +4 -0
- package/dist/rules/detectors/index.js +651 -0
- package/dist/rules/detectors/shared.d.ts +30 -0
- package/dist/rules/detectors/shared.js +87 -0
- package/dist/rules/runtime/index.d.ts +17 -0
- package/dist/rules/runtime/index.js +74 -0
- package/dist/runner/index.d.ts +18 -0
- package/dist/runner/index.js +176 -0
- package/dist/runner/lint-text.d.ts +19 -0
- package/dist/runner/lint-text.js +20 -0
- package/dist/scoring/index.d.ts +16 -0
- package/dist/scoring/index.js +41 -0
- package/dist/suppressions/index.d.ts +23 -0
- package/dist/suppressions/index.js +86 -0
- package/dist/web/engine.meta.json +1 -0
- package/dist/web/engine.mjs +11029 -0
- package/dist/web/index.d.ts +39 -0
- package/dist/web/index.js +37 -0
- package/dist/web/node-crypto-shim.d.ts +9 -0
- package/dist/web/node-crypto-shim.js +24 -0
- package/dist/web/sha256.d.ts +11 -0
- package/dist/web/sha256.js +89 -0
- package/package.json +88 -4
- package/rulepack/rulepack.json +1496 -0
- package/rules/profiles/academico.yml +4 -0
- package/rules/profiles/chat.yml +4 -0
- package/rules/profiles/correo.yml +3 -0
- package/rules/profiles/general.yml +2 -0
- package/rules/profiles/marketing.yml +4 -0
- package/rules/profiles/readme.yml +5 -0
- package/rules/profiles/redes.yml +6 -0
- package/rules/profiles/tecnico.yml +4 -0
- package/rules/situaciones.yml +27 -0
- package/schemas/rule.schema.json +90 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Situaciones: la capa de contexto de la humanización (docs/decisions.md, 2026-09-18).
|
|
2
|
+
#
|
|
3
|
+
# Cada situación junta tres cosas:
|
|
4
|
+
# perfil el perfil de reglas con el que se revisa (rules/profiles/<perfil>.yml)
|
|
5
|
+
# guia el principio de su viñeta en la sección «Según la situación» de la guía
|
|
6
|
+
# (integrations/claude-code/guia/humano.md); el gate `situaciones` comprueba que existe
|
|
7
|
+
# archivos los tipos de archivo de texto que le corresponden. Con `--profile auto`, la ruta decide la situación:
|
|
8
|
+
# gana la primera que coincide. Un archivo que no coincide con ninguna no se revisa con `auto`.
|
|
9
|
+
#
|
|
10
|
+
# Los overrides de textoneitor.yml siguen mandando sobre esto.
|
|
11
|
+
schema_version: 1
|
|
12
|
+
situaciones:
|
|
13
|
+
- id: chat
|
|
14
|
+
perfil: chat
|
|
15
|
+
guia: "Conversación"
|
|
16
|
+
- id: correo
|
|
17
|
+
perfil: correo
|
|
18
|
+
guia: "Correo"
|
|
19
|
+
archivos: ["**/correo*.{md,txt}", "**/*.correo.md", "**/correos/**/*.{md,txt}"]
|
|
20
|
+
- id: readme
|
|
21
|
+
perfil: readme
|
|
22
|
+
guia: "README y textos de un proyecto"
|
|
23
|
+
archivos: ["**/README*.md", "**/CHANGELOG*.md", "**/CONTRIBUTING*.md", "**/docs/**/*.md"]
|
|
24
|
+
- id: redes
|
|
25
|
+
perfil: redes
|
|
26
|
+
guia: "Redes"
|
|
27
|
+
archivos: ["**/*.post.md", "**/redes/**/*.{md,txt}"]
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { type RunnerContext } from "../runner/index.js";
|
|
2
|
+
export interface CorpusSample {
|
|
3
|
+
id: string;
|
|
4
|
+
class: "human" | "ai";
|
|
5
|
+
file: string;
|
|
6
|
+
register: string;
|
|
7
|
+
sha256: string;
|
|
8
|
+
[k: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
export interface BenchmarkReport {
|
|
11
|
+
schema_version: 1;
|
|
12
|
+
partition: string;
|
|
13
|
+
tool: {
|
|
14
|
+
version: string;
|
|
15
|
+
rulepack: string;
|
|
16
|
+
};
|
|
17
|
+
config: {
|
|
18
|
+
threshold: number;
|
|
19
|
+
min_words_for_index: number;
|
|
20
|
+
profile: string;
|
|
21
|
+
register?: string;
|
|
22
|
+
annotations?: string;
|
|
23
|
+
};
|
|
24
|
+
corpus: {
|
|
25
|
+
samples: number;
|
|
26
|
+
human: number;
|
|
27
|
+
ai: number;
|
|
28
|
+
human_words: number;
|
|
29
|
+
ai_words: number;
|
|
30
|
+
manifest_sha256: string;
|
|
31
|
+
};
|
|
32
|
+
per_rule: Record<string, {
|
|
33
|
+
status: string;
|
|
34
|
+
findings_human: number;
|
|
35
|
+
findings_ai: number;
|
|
36
|
+
docs_human: number;
|
|
37
|
+
docs_ai: number;
|
|
38
|
+
fp_per_1000_human_words: number;
|
|
39
|
+
per_1000_ai_words: number;
|
|
40
|
+
adjudicated: {
|
|
41
|
+
correct: number;
|
|
42
|
+
incorrect: number;
|
|
43
|
+
precision: number | null;
|
|
44
|
+
};
|
|
45
|
+
by_register: Record<string, {
|
|
46
|
+
human: number;
|
|
47
|
+
ai: number;
|
|
48
|
+
}>;
|
|
49
|
+
ms: number;
|
|
50
|
+
}>;
|
|
51
|
+
aggregate: {
|
|
52
|
+
threshold: number;
|
|
53
|
+
confusion: {
|
|
54
|
+
tp: number;
|
|
55
|
+
fp: number;
|
|
56
|
+
tn: number;
|
|
57
|
+
fn: number;
|
|
58
|
+
skipped_short: number;
|
|
59
|
+
};
|
|
60
|
+
tpr: number | null;
|
|
61
|
+
fpr: number | null;
|
|
62
|
+
precision: number | null;
|
|
63
|
+
balanced_accuracy: number | null;
|
|
64
|
+
ci95: {
|
|
65
|
+
tpr: [number, number] | null;
|
|
66
|
+
fpr: [number, number] | null;
|
|
67
|
+
};
|
|
68
|
+
index_median_human: number;
|
|
69
|
+
index_median_ai: number;
|
|
70
|
+
by_register: Record<string, {
|
|
71
|
+
n_human: number;
|
|
72
|
+
n_ai: number;
|
|
73
|
+
median_human: number | null;
|
|
74
|
+
median_ai: number | null;
|
|
75
|
+
}>;
|
|
76
|
+
};
|
|
77
|
+
notes: string[];
|
|
78
|
+
}
|
|
79
|
+
/** Contexto con el que se evalúa un corpus: reglas a su nivel por defecto en el perfil pedido, sin overrides ni caché. Lo usa también dump-findings. */
|
|
80
|
+
export declare function benchmarkContext(ctx: RunnerContext): RunnerContext;
|
|
81
|
+
/**
|
|
82
|
+
* `annotations`: archivo de adjudicaciones. Sin él se busca `benchmark/annotations/<partición>.yml` junto al corpus, que es lo que
|
|
83
|
+
* reproducen los informes publicados. Ese nombre no lleva versión y lo comparten corpus distintos con la misma partición, así que
|
|
84
|
+
* para adjudicar un corpus concreto hay que pasar el archivo.
|
|
85
|
+
*/
|
|
86
|
+
export declare function runBenchmark(ctx: RunnerContext, corpusDir: string, partition: string, opts?: {
|
|
87
|
+
register?: string;
|
|
88
|
+
annotations?: string;
|
|
89
|
+
}): BenchmarkReport;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { parse as parseYaml } from "yaml";
|
|
5
|
+
import { lintDocumentText } from "../runner/index.js";
|
|
6
|
+
import { buildDocument } from "../document/index.js";
|
|
7
|
+
import { runRules } from "../rules/runtime/index.js";
|
|
8
|
+
function wilson(k, n) {
|
|
9
|
+
if (n === 0)
|
|
10
|
+
return null;
|
|
11
|
+
const z = 1.96;
|
|
12
|
+
const p = k / n;
|
|
13
|
+
const d = 1 + (z * z) / n;
|
|
14
|
+
const c = p + (z * z) / (2 * n);
|
|
15
|
+
const m = z * Math.sqrt((p * (1 - p)) / n + (z * z) / (4 * n * n));
|
|
16
|
+
return [round3((c - m) / d), round3((c + m) / d)];
|
|
17
|
+
}
|
|
18
|
+
const round3 = (x) => Math.round(x * 1000) / 1000;
|
|
19
|
+
const median = (a) => {
|
|
20
|
+
if (!a.length)
|
|
21
|
+
return 0;
|
|
22
|
+
const s = [...a].sort((x, y) => x - y);
|
|
23
|
+
const m = s.length >> 1;
|
|
24
|
+
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
25
|
+
};
|
|
26
|
+
/** Contexto con el que se evalúa un corpus: reglas a su nivel por defecto en el perfil pedido, sin overrides ni caché. Lo usa también dump-findings. */
|
|
27
|
+
export function benchmarkContext(ctx) {
|
|
28
|
+
return { ...ctx, config: { ...ctx.config, rules: {}, overrides: [], cache: { ...ctx.config.cache, enabled: false } } };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* `annotations`: archivo de adjudicaciones. Sin él se busca `benchmark/annotations/<partición>.yml` junto al corpus, que es lo que
|
|
32
|
+
* reproducen los informes publicados. Ese nombre no lleva versión y lo comparten corpus distintos con la misma partición, así que
|
|
33
|
+
* para adjudicar un corpus concreto hay que pasar el archivo.
|
|
34
|
+
*/
|
|
35
|
+
export function runBenchmark(ctx, corpusDir, partition, opts = {}) {
|
|
36
|
+
const manifestPath = path.join(corpusDir, "manifests", `${partition}.yml`);
|
|
37
|
+
if (!fs.existsSync(manifestPath))
|
|
38
|
+
throw new Error(`no existe el manifiesto ${manifestPath}`);
|
|
39
|
+
const manifestRaw = fs.readFileSync(manifestPath, "utf8");
|
|
40
|
+
const manifest = parseYaml(manifestRaw);
|
|
41
|
+
// Filtro por registro: un corpus con varios registros se evalúa con el perfil de cada uno por separado.
|
|
42
|
+
const selected = opts.register ? manifest.samples.filter((s) => s.register === opts.register) : manifest.samples;
|
|
43
|
+
if (opts.register && !selected.length)
|
|
44
|
+
throw new Error(`el manifiesto ${partition} no tiene muestras del registro ${opts.register}`);
|
|
45
|
+
const benchRoot = path.resolve(corpusDir, "..", "benchmark");
|
|
46
|
+
const thrFile = path.join(benchRoot, "configs", "threshold.yml");
|
|
47
|
+
const threshold = fs.existsSync(thrFile) ? Number(parseYaml(fs.readFileSync(thrFile, "utf8")).index_threshold) : 20;
|
|
48
|
+
if (opts.annotations && !fs.existsSync(opts.annotations))
|
|
49
|
+
throw new Error(`no existe el archivo de adjudicaciones ${opts.annotations}`);
|
|
50
|
+
const annFile = opts.annotations ?? path.join(benchRoot, "annotations", `${partition}.yml`);
|
|
51
|
+
const ann = fs.existsSync(annFile) ? parseYaml(fs.readFileSync(annFile, "utf8")) ?? {} : {};
|
|
52
|
+
const usedKeys = new Set();
|
|
53
|
+
// El benchmark siempre se ejecuta con todas las reglas activas a su nivel por defecto y sin caché.
|
|
54
|
+
const bctx = benchmarkContext(ctx);
|
|
55
|
+
const perRule = {};
|
|
56
|
+
for (const r of ctx.pack.rules) {
|
|
57
|
+
perRule[r.id] = {
|
|
58
|
+
status: r.status, findings_human: 0, findings_ai: 0, docs_human: 0, docs_ai: 0, fp_per_1000_human_words: 0,
|
|
59
|
+
per_1000_ai_words: 0, adjudicated: { correct: 0, incorrect: 0, precision: null }, by_register: {}, ms: 0,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
let humanWords = 0;
|
|
63
|
+
let aiWords = 0;
|
|
64
|
+
const idx = [];
|
|
65
|
+
const notes = [];
|
|
66
|
+
for (const s of selected) {
|
|
67
|
+
const abs = path.join(corpusDir, s.file);
|
|
68
|
+
const text = fs.readFileSync(abs, "utf8");
|
|
69
|
+
const sha = createHash("sha256").update(text).digest("hex");
|
|
70
|
+
if (sha !== s.sha256)
|
|
71
|
+
throw new Error(`hash distinto para ${s.id}: el corpus no coincide con el manifiesto`);
|
|
72
|
+
const res = lintDocumentText(text, bctx, { relPath: s.file });
|
|
73
|
+
const tdoc = buildDocument(text, { path: s.file });
|
|
74
|
+
for (const r of ctx.pack.rules) {
|
|
75
|
+
const t0 = performance.now();
|
|
76
|
+
runRules(tdoc, [r]);
|
|
77
|
+
perRule[r.id].ms += performance.now() - t0;
|
|
78
|
+
}
|
|
79
|
+
if (s.class === "human")
|
|
80
|
+
humanWords += res.score.eligibleWords;
|
|
81
|
+
else
|
|
82
|
+
aiWords += res.score.eligibleWords;
|
|
83
|
+
idx.push({ cls: s.class, register: s.register, index: res.score.index });
|
|
84
|
+
const seen = new Set();
|
|
85
|
+
for (const f of res.findings) {
|
|
86
|
+
const pr = perRule[f.rule];
|
|
87
|
+
if (!pr)
|
|
88
|
+
continue;
|
|
89
|
+
if (s.class === "human")
|
|
90
|
+
pr.findings_human++;
|
|
91
|
+
else
|
|
92
|
+
pr.findings_ai++;
|
|
93
|
+
const br = (pr.by_register[s.register] ??= { human: 0, ai: 0 });
|
|
94
|
+
br[s.class]++;
|
|
95
|
+
if (!seen.has(f.rule)) {
|
|
96
|
+
seen.add(f.rule);
|
|
97
|
+
if (s.class === "human")
|
|
98
|
+
pr.docs_human++;
|
|
99
|
+
else
|
|
100
|
+
pr.docs_ai++;
|
|
101
|
+
}
|
|
102
|
+
const key = `${s.id}|${f.rule}|${f.fingerprint}`;
|
|
103
|
+
const a = ann.adjudications?.[key];
|
|
104
|
+
if (a)
|
|
105
|
+
usedKeys.add(key);
|
|
106
|
+
if (a === "correct")
|
|
107
|
+
pr.adjudicated.correct++;
|
|
108
|
+
else if (a === "incorrect")
|
|
109
|
+
pr.adjudicated.incorrect++;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (const pr of Object.values(perRule)) {
|
|
113
|
+
pr.ms = Math.round(pr.ms * 10) / 10;
|
|
114
|
+
pr.fp_per_1000_human_words = humanWords ? round3((pr.findings_human * 1000) / humanWords) : 0;
|
|
115
|
+
pr.per_1000_ai_words = aiWords ? round3((pr.findings_ai * 1000) / aiWords) : 0;
|
|
116
|
+
const n = pr.adjudicated.correct + pr.adjudicated.incorrect;
|
|
117
|
+
pr.adjudicated.precision = n ? round3(pr.adjudicated.correct / n) : null;
|
|
118
|
+
}
|
|
119
|
+
let tp = 0, fp = 0, tn = 0, fn = 0, skipped = 0;
|
|
120
|
+
for (const d of idx) {
|
|
121
|
+
if (d.index === null) {
|
|
122
|
+
skipped++;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const flagged = d.index >= threshold;
|
|
126
|
+
if (d.cls === "ai")
|
|
127
|
+
flagged ? tp++ : fn++;
|
|
128
|
+
else
|
|
129
|
+
flagged ? fp++ : tn++;
|
|
130
|
+
}
|
|
131
|
+
const tpr = tp + fn ? round3(tp / (tp + fn)) : null;
|
|
132
|
+
const fpr = fp + tn ? round3(fp / (fp + tn)) : null;
|
|
133
|
+
const registers = [...new Set(idx.map((d) => d.register))].sort();
|
|
134
|
+
const byReg = {};
|
|
135
|
+
for (const r of registers) {
|
|
136
|
+
const h = idx.filter((d) => d.register === r && d.cls === "human" && d.index !== null).map((d) => d.index);
|
|
137
|
+
const a = idx.filter((d) => d.register === r && d.cls === "ai" && d.index !== null).map((d) => d.index);
|
|
138
|
+
byReg[r] = { n_human: h.length, n_ai: a.length, median_human: h.length ? median(h) : null, median_ai: a.length ? median(a) : null };
|
|
139
|
+
}
|
|
140
|
+
if (!ann.adjudications)
|
|
141
|
+
notes.push("Sin adjudicaciones para esta partición: la precisión adjudicada por regla es null.");
|
|
142
|
+
else if (opts.annotations) {
|
|
143
|
+
// Una adjudicación que no casa con ningún hallazgo suele ser una huella vieja: el texto o la regla cambiaron después de adjudicar.
|
|
144
|
+
const huerfanas = Object.keys(ann.adjudications).filter((k) => !usedKeys.has(k)).length;
|
|
145
|
+
if (huerfanas)
|
|
146
|
+
notes.push(`${huerfanas} adjudicaciones no corresponden a ningún hallazgo de esta ejecución.`);
|
|
147
|
+
}
|
|
148
|
+
notes.push("No se publica recall por regla: el corpus no está anotado exhaustivamente.");
|
|
149
|
+
notes.push("El índice mide densidad de patrones editoriales; el umbral solo se usa para evaluar separación, no como veredicto de autoría.");
|
|
150
|
+
return {
|
|
151
|
+
schema_version: 1,
|
|
152
|
+
partition,
|
|
153
|
+
tool: { version: ctx.toolVersion, rulepack: ctx.pack.version },
|
|
154
|
+
config: {
|
|
155
|
+
threshold,
|
|
156
|
+
min_words_for_index: bctx.config.min_words_for_index,
|
|
157
|
+
profile: bctx.config.profile,
|
|
158
|
+
...(opts.register ? { register: opts.register } : {}),
|
|
159
|
+
// Relativa a la carpeta que contiene el corpus, para que el informe no lleve rutas del equipo.
|
|
160
|
+
...(opts.annotations ? { annotations: path.relative(path.resolve(corpusDir, ".."), path.resolve(opts.annotations)).replace(/\\/g, "/") } : {}),
|
|
161
|
+
},
|
|
162
|
+
corpus: {
|
|
163
|
+
samples: selected.length,
|
|
164
|
+
human: selected.filter((s) => s.class === "human").length,
|
|
165
|
+
ai: selected.filter((s) => s.class === "ai").length,
|
|
166
|
+
human_words: humanWords,
|
|
167
|
+
ai_words: aiWords,
|
|
168
|
+
manifest_sha256: createHash("sha256").update(manifestRaw).digest("hex"),
|
|
169
|
+
},
|
|
170
|
+
per_rule: perRule,
|
|
171
|
+
aggregate: {
|
|
172
|
+
threshold,
|
|
173
|
+
confusion: { tp, fp, tn, fn, skipped_short: skipped },
|
|
174
|
+
tpr,
|
|
175
|
+
fpr,
|
|
176
|
+
precision: tp + fp ? round3(tp / (tp + fp)) : null,
|
|
177
|
+
balanced_accuracy: tpr !== null && fpr !== null ? round3((tpr + (1 - fpr)) / 2) : null,
|
|
178
|
+
ci95: { tpr: wilson(tp, tp + fn), fpr: wilson(fp, fp + tn) },
|
|
179
|
+
index_median_human: median(idx.filter((d) => d.cls === "human" && d.index !== null).map((d) => d.index)),
|
|
180
|
+
index_median_ai: median(idx.filter((d) => d.cls === "ai" && d.index !== null).map((d) => d.index)),
|
|
181
|
+
by_register: byReg,
|
|
182
|
+
},
|
|
183
|
+
notes,
|
|
184
|
+
};
|
|
185
|
+
}
|