@cratis/pi 7.143.0 → 7.144.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.
Files changed (19) hide show
  1. package/package/corpus/harnesses/copilot/instructions/secrets.instructions.md +8 -8
  2. package/package/corpus/hooks/README.md +39 -1
  3. package/package/corpus/hooks/scripts/cratis-guard-secrets.sh +150 -18
  4. package/package/corpus/hooks/scripts/cratis-guard-shell-writes.sh +39 -7
  5. package/package/corpus/rules/secrets.md +8 -8
  6. package/package/corpus/skills/cratis-screenplay-reverse-engineering/SKILL.md +17 -8
  7. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/behavior-catalog.md +122 -0
  8. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/documentation-intake-and-screens.md +2 -0
  9. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/evidence-format.md +1 -1
  10. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/executable-intake.md +48 -2
  11. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/fidelity-check.md +12 -5
  12. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/ground-truth-benchmark.md +28 -10
  13. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/probing-and-naming.md +51 -18
  14. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/synthesis-rules.md +19 -2
  15. package/package/corpus/skills/cratis-screenplay-reverse-engineering/references/target-and-first-validation.md +6 -0
  16. package/package/corpus/skills/cratis-screenplay-reverse-engineering/scripts/score-fidelity-self-test.mjs +76 -0
  17. package/package/corpus/skills/cratis-screenplay-reverse-engineering/scripts/score-fidelity.mjs +177 -0
  18. package/package/corpus/skills/cratis-screenplay-reverse-engineering/verification.json +6 -1
  19. package/package.json +1 -1
@@ -0,0 +1,177 @@
1
+ // Copyright (c) Cratis. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ import { readFileSync, realpathSync } from 'node:fs';
5
+ import { fileURLToPath } from 'node:url';
6
+
7
+ export const DEFAULT_WEIGHTS = { rule: 0.25, rejection: 0.25, flow: 0.25, time: 0.1, authorization: 0.1, 'query-shape': 0.05 };
8
+ export const CLASS_ITEM_WEIGHTS = { rule: 1, rejection: 1, flow: 1, time: 1.5, authorization: 1, 'query-shape': 1 };
9
+ export const NAMING_WEIGHTS = { commands: 0.2, events: 0.15, readModels: 0.15, screens: 0.15, constraints: 0.15, concepts: 0.1, specifications: 0.03, handWrittenRules: 0.07 };
10
+ const STRUCTURE = ['commands', 'events', 'readModels', 'screens', 'concepts'];
11
+ const RULES = ['constraints', 'handWrittenRules', 'specifications'];
12
+ const MODEL_SCORE = { expressed: 1, partial: 0.5, missing: 0 };
13
+ const EXECUTED_SCORE = { pass: 1, fail: 0 };
14
+ // Legacy structure and rules score a renamed item as found; naming scores it as not matching.
15
+ const LEGACY_RESULT = { exact: 1, renamed: 1, partial: 0.5, missing: 0 };
16
+ const NAMING_RESULT = { exact: 1, renamed: 0, partial: 0.5, missing: 0 };
17
+ const TOLERANCE = 1e-6;
18
+
19
+ export class CatalogError extends Error {}
20
+
21
+ const round = value => Math.round(value * 10000) / 10000;
22
+
23
+ function weightedMean(parts) {
24
+ const total = parts.reduce((sum, part) => sum + part.weight, 0);
25
+ return total === 0 ? null : parts.reduce((sum, part) => sum + part.weight * part.score, 0) / total;
26
+ }
27
+
28
+ const fail = message => { throw new CatalogError(message); };
29
+ const knownExecuted = value => value === undefined || value === 'not-run' || value in EXECUTED_SCORE;
30
+
31
+ function validateItem(item, weights) {
32
+ if (!(item.class in weights)) fail(`${item.id}: class "${item.class}" has no weight`);
33
+ if (!(item.status in MODEL_SCORE)) fail(`${item.id}: unknown status "${item.status}"`);
34
+ if (!knownExecuted(item.executed)) fail(`${item.id}: unknown executed "${item.executed}"`);
35
+ if (item.weight !== undefined && !(item.weight > 0)) fail(`${item.id}: weight must be positive`);
36
+ }
37
+
38
+ function validateCatalog(catalog) {
39
+ if (!Array.isArray(catalog?.items) || catalog.items.length === 0) fail('catalog needs a non-empty "items" array');
40
+ const weights = catalog.weights ?? DEFAULT_WEIGHTS;
41
+ const sum = Object.values(weights).reduce((total, value) => total + value, 0);
42
+ if (Math.abs(sum - 1) > TOLERANCE) fail(`class weights sum to ${sum}, not 1`);
43
+ const ids = catalog.items.map(item => item.id);
44
+ if (ids.some(id => !id) || new Set(ids).size !== ids.length) fail('item ids must be present and unique');
45
+ catalog.items.forEach(item => validateItem(item, weights));
46
+ const empty = Object.entries(weights).find(([name, weight]) => weight > 0 && !catalog.items.some(item => item.class === name));
47
+ if (empty) fail(`class "${empty[0]}" has weight ${empty[1]} but no items`);
48
+ return weights;
49
+ }
50
+
51
+ function behaviorScores(catalog, weights) {
52
+ const byClass = {};
53
+ for (const name of Object.keys(weights)) {
54
+ const items = catalog.items.filter(item => item.class === name);
55
+ if (items.length === 0) continue;
56
+ const parts = executed => items.map(item => ({
57
+ weight: item.weight ?? CLASS_ITEM_WEIGHTS[name] ?? 1,
58
+ score: executed && item.executed in EXECUTED_SCORE ? EXECUTED_SCORE[item.executed] : MODEL_SCORE[item.status]
59
+ }));
60
+ byClass[name] = { items: items.length, model: weightedMean(parts(false)), executed: weightedMean(parts(true)) };
61
+ }
62
+ const total = key => Object.entries(byClass).reduce((sum, [name, value]) => sum + weights[name] * value[key], 0);
63
+ const activeWeight = Object.keys(byClass).reduce((sum, name) => sum + weights[name], 0);
64
+ const notRun = catalog.items.filter(item => !(item.executed in EXECUTED_SCORE)).length;
65
+ return {
66
+ byClass,
67
+ behaviorModel: total('model') / activeWeight,
68
+ behaviorExecuted: total('executed') / activeWeight,
69
+ items: catalog.items.length,
70
+ notRun,
71
+ executedFailedButExpressed: catalog.items.filter(item => item.status === 'expressed' && item.executed === 'fail').map(item => item.id),
72
+ assemblySourced: catalog.items.filter(item => item.source === 'assembly').length
73
+ };
74
+ }
75
+
76
+ function categoryScores(items, table, weights, categories) {
77
+ const parts = [];
78
+ for (const category of categories) {
79
+ const own = items.filter(item => item.category === category);
80
+ if (own.length === 0) continue;
81
+ parts.push({ weight: weights[category], score: own.reduce((sum, item) => sum + table[item.result], 0) / own.length });
82
+ }
83
+ return weightedMean(parts);
84
+ }
85
+
86
+ function namingScores(naming) {
87
+ const weights = naming.weights ?? NAMING_WEIGHTS;
88
+ for (const item of naming.items ?? []) {
89
+ if (!(item.category in weights)) throw new CatalogError(`naming: unknown category "${item.category}"`);
90
+ if (!(item.result in LEGACY_RESULT)) throw new CatalogError(`naming: unknown result "${item.result}" for ${item.name}`);
91
+ }
92
+ const all = naming.items ?? [];
93
+ const nonLeaked = all.filter(item => !item.leaked);
94
+ const categories = Object.keys(weights);
95
+ return {
96
+ legacyStructure: categoryScores(all, LEGACY_RESULT, weights, STRUCTURE),
97
+ legacyRules: categoryScores(all, LEGACY_RESULT, weights, RULES),
98
+ namingAll: categoryScores(all, NAMING_RESULT, weights, categories),
99
+ namingNonLeaked: categoryScores(nonLeaked, NAMING_RESULT, weights, categories),
100
+ leakShare: all.length === 0 ? null : all.filter(item => item.leaked).length / all.length,
101
+ namingItems: all.length,
102
+ namingNonLeakedItems: nonLeaked.length
103
+ };
104
+ }
105
+
106
+ export function score(catalog, naming) {
107
+ const weights = validateCatalog(catalog);
108
+ const result = { behavior: behaviorScores(catalog, weights) };
109
+ if (naming) result.naming = namingScores(naming);
110
+ return result;
111
+ }
112
+
113
+ function format(result) {
114
+ const fixed = value => (value === null || value === undefined ? 'n/a' : value.toFixed(2));
115
+ const { behavior, naming } = result;
116
+ const lines = [
117
+ `Behavior (model read): ${fixed(behavior.behaviorModel)}`,
118
+ `Behavior (executed where run): ${fixed(behavior.behaviorExecuted)}; not-run items: ${behavior.notRun}/${behavior.items}`,
119
+ ...Object.entries(behavior.byClass).map(([name, value]) => ` ${name}: ${value.items} items, model ${fixed(value.model)}, executed ${fixed(value.executed)}`)
120
+ ];
121
+ if (behavior.executedFailedButExpressed.length > 0) lines.push(`Expressed but failed when executed: ${behavior.executedFailedButExpressed.join(', ')}`);
122
+ if (behavior.assemblySourced > 0) lines.push(`Items sourced from assembly: ${behavior.assemblySourced}`);
123
+ if (naming) {
124
+ lines.push(
125
+ `Structure (legacy): ${fixed(naming.legacyStructure)}`,
126
+ `Rules (legacy): ${fixed(naming.legacyRules)}`,
127
+ `Naming (all): ${fixed(naming.namingAll)}`,
128
+ `Naming (non-leaked): ${fixed(naming.namingNonLeaked)} over ${naming.namingNonLeakedItems} of ${naming.namingItems} items`,
129
+ `Leak share: ${fixed(naming.leakShare)}`);
130
+ }
131
+ return lines.join('\n');
132
+ }
133
+
134
+ function roundDeep(value) {
135
+ if (typeof value === 'number') return round(value);
136
+ if (Array.isArray(value)) return value.map(roundDeep);
137
+ if (value && typeof value === 'object') return Object.fromEntries(Object.entries(value).map(([key, inner]) => [key, roundDeep(inner)]));
138
+ return value;
139
+ }
140
+
141
+ function parseArguments(args) {
142
+ const options = {};
143
+ for (let index = 0; index < args.length; index++) {
144
+ if (args[index] === '--json') options.json = true;
145
+ else if (args[index] === '--self-test') options.selfTest = true;
146
+ else if (args[index] === '--naming' && args[index + 1]) options.naming = args[++index];
147
+ else if (!args[index].startsWith('--') && !options.catalog) options.catalog = args[index];
148
+ else throw new CatalogError('Usage: node score-fidelity.mjs <catalog.json> [--naming <naming.json>] [--json] | --self-test');
149
+ }
150
+ return options;
151
+ }
152
+
153
+ async function main() {
154
+ try {
155
+ const options = parseArguments(process.argv.slice(2));
156
+ if (options.selfTest) {
157
+ const { selfTest } = await import('./score-fidelity-self-test.mjs');
158
+ selfTest(score, CatalogError);
159
+ return;
160
+ }
161
+ if (!options.catalog) throw new CatalogError('Usage: node score-fidelity.mjs <catalog.json> [--naming <naming.json>] [--json] | --self-test');
162
+ const read = path => {
163
+ try {
164
+ return JSON.parse(readFileSync(path, 'utf8'));
165
+ } catch (error) {
166
+ throw new CatalogError(`cannot read ${path}: ${error.message}`);
167
+ }
168
+ };
169
+ const result = score(read(options.catalog), options.naming ? read(options.naming) : undefined);
170
+ console.log(options.json ? JSON.stringify(roundDeep(result)) : format(result));
171
+ } catch (error) {
172
+ console.error(`score-fidelity: ${error.message}`);
173
+ process.exitCode = error instanceof CatalogError ? 2 : 1;
174
+ }
175
+ }
176
+
177
+ if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url))) await main();
@@ -24,6 +24,11 @@
24
24
  { "kind": "skill-contains", "value": "plausibility report", "file": "references/documentation-intake-and-screens.md" },
25
25
  { "kind": "skill-contains", "value": "morluto/rea", "file": "references/provenance.md" },
26
26
  { "kind": "skill-contains", "value": "limit + 1", "file": "references/probing-and-naming.md" },
27
- { "kind": "skill-contains", "value": "Without the Screenplay MCP server", "file": "references/target-and-first-validation.md" }
27
+ { "kind": "skill-contains", "value": "Without the Screenplay MCP server", "file": "references/target-and-first-validation.md" },
28
+ { "kind": "skill-contains", "value": "probe coverage" },
29
+ { "kind": "skill-contains", "value": "Managed assembly intake", "file": "references/executable-intake.md" },
30
+ { "kind": "skill-contains", "value": "Naming (non-leaked)", "file": "references/ground-truth-benchmark.md" },
31
+ { "kind": "skill-contains", "value": "behavior catalog" },
32
+ { "kind": "skill-contains", "value": "score-fidelity.mjs" }
28
33
  ]
29
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "7.143.0",
3
+ "version": "7.144.0",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [