fraim 2.0.270 → 2.0.271
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/src/cli/commands/add-ide.js +28 -2
- package/dist/src/cli/commands/learning-usage.js +412 -0
- package/dist/src/cli/fraim.js +2 -0
- package/dist/src/core/ai-mentor.js +27 -14
- package/dist/src/core/config-loader.js +48 -3
- package/dist/src/core/fraim-config-schema.generated.js +18 -0
- package/dist/src/core/handoff-contracts.js +37 -1
- package/dist/src/core/job-phases.js +2 -14
- package/dist/src/core/resolve-phase-edge.js +75 -0
- package/dist/src/core/types.js +7 -1
- package/dist/src/core/utils/git-utils.js +24 -14
- package/dist/src/core/utils/project-fraim-paths.js +16 -1
- package/dist/src/local-mcp-server/artifact-retention-cleanup.js +8 -0
- package/dist/src/local-mcp-server/learning-context-builder.js +448 -95
- package/dist/src/local-mcp-server/learning-firing-parser.js +247 -0
- package/dist/src/local-mcp-server/learning-usage-analysis.js +347 -0
- package/dist/src/local-mcp-server/learning-usage-attestation.js +191 -0
- package/dist/src/local-mcp-server/learning-usage-projection.js +79 -0
- package/dist/src/local-mcp-server/learning-usage-store.js +417 -0
- package/dist/src/local-mcp-server/stdio-server.js +43 -0
- package/package.json +1 -1
|
@@ -42,6 +42,7 @@ const chalk_1 = __importDefault(require("chalk"));
|
|
|
42
42
|
const prompts_1 = __importDefault(require("prompts"));
|
|
43
43
|
const fs_1 = __importDefault(require("fs"));
|
|
44
44
|
const path_1 = __importDefault(require("path"));
|
|
45
|
+
const os_1 = __importDefault(require("os"));
|
|
45
46
|
const ide_detector_1 = require("../setup/ide-detector");
|
|
46
47
|
const mcp_config_generator_1 = require("../setup/mcp-config-generator");
|
|
47
48
|
const claude_code_telemetry_1 = require("../setup/claude-code-telemetry");
|
|
@@ -51,13 +52,38 @@ const get_provider_client_1 = require("../api/get-provider-client");
|
|
|
51
52
|
const provider_prompts_1 = require("../setup/provider-prompts");
|
|
52
53
|
const provider_registry_1 = require("../providers/provider-registry");
|
|
53
54
|
const user_config_1 = require("../utils/user-config");
|
|
55
|
+
const resolveGlobalConfigPath = () => {
|
|
56
|
+
const primary = path_1.default.join((0, script_sync_utils_1.getUserFraimDir)(), 'config.json');
|
|
57
|
+
if (fs_1.default.existsSync(primary)) {
|
|
58
|
+
try {
|
|
59
|
+
const config = JSON.parse(fs_1.default.readFileSync(primary, 'utf8'));
|
|
60
|
+
if (config && config.apiKey)
|
|
61
|
+
return primary;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return primary;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// The aggregate test runner sets FRAIM_USER_DIR for process isolation, while
|
|
68
|
+
// older command tests isolate by monkey-patching os.homedir(). In test mode,
|
|
69
|
+
// honor that fixture home if it already contains the config under test.
|
|
70
|
+
if (process.env.NODE_ENV === 'test') {
|
|
71
|
+
const homedirConfig = path_1.default.join(os_1.default.homedir(), '.fraim', 'config.json');
|
|
72
|
+
if (homedirConfig !== primary && fs_1.default.existsSync(homedirConfig)) {
|
|
73
|
+
return homedirConfig;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return primary;
|
|
77
|
+
};
|
|
54
78
|
const loadGlobalConfig = async () => {
|
|
55
|
-
const globalConfigPath =
|
|
79
|
+
const globalConfigPath = resolveGlobalConfigPath();
|
|
56
80
|
if (!fs_1.default.existsSync(globalConfigPath)) {
|
|
57
81
|
return null;
|
|
58
82
|
}
|
|
59
83
|
try {
|
|
60
84
|
const config = JSON.parse(fs_1.default.readFileSync(globalConfigPath, 'utf8'));
|
|
85
|
+
if (!config.apiKey)
|
|
86
|
+
return null;
|
|
61
87
|
// Support both old and new token format
|
|
62
88
|
const tokens = config.tokens || {};
|
|
63
89
|
// Backward compatibility: map old format to new
|
|
@@ -136,7 +162,7 @@ const promptForProviderTokenIfNeeded = async (providerId, isOptional = false) =>
|
|
|
136
162
|
}
|
|
137
163
|
};
|
|
138
164
|
const saveProviderTokenToConfig = async (providerId, token) => {
|
|
139
|
-
const globalConfigPath =
|
|
165
|
+
const globalConfigPath = resolveGlobalConfigPath();
|
|
140
166
|
if (fs_1.default.existsSync(globalConfigPath)) {
|
|
141
167
|
try {
|
|
142
168
|
const config = JSON.parse(fs_1.default.readFileSync(globalConfigPath, 'utf8'));
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.learningUsageCommand = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Issue #1103 — `fraim learning-usage <subcommand>`.
|
|
9
|
+
*
|
|
10
|
+
* The read and analysis side of the usage record. The write side of an offer is the
|
|
11
|
+
* proxy (no agent involvement, R2); the write side of a firing is an attestation in
|
|
12
|
+
* a retrospective, which `record-firings` reads back.
|
|
13
|
+
*
|
|
14
|
+
* This is a CLI rather than a new MCP tool deliberately: the store schema then has
|
|
15
|
+
* exactly one implementation, and nothing is added to the MCP tool surface that
|
|
16
|
+
* issues #801, #860 and #861 are working to shrink.
|
|
17
|
+
*/
|
|
18
|
+
const commander_1 = require("commander");
|
|
19
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
20
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
21
|
+
const learning_context_builder_1 = require("../../local-mcp-server/learning-context-builder");
|
|
22
|
+
const learning_usage_analysis_1 = require("../../local-mcp-server/learning-usage-analysis");
|
|
23
|
+
const learning_usage_store_1 = require("../../local-mcp-server/learning-usage-store");
|
|
24
|
+
const learning_usage_attestation_1 = require("../../local-mcp-server/learning-usage-attestation");
|
|
25
|
+
function resolveWorkspaceRoot(options) {
|
|
26
|
+
return node_path_1.default.resolve(options.workspaceRoot || options.root || process.cwd());
|
|
27
|
+
}
|
|
28
|
+
function resolveUser(options) {
|
|
29
|
+
const email = options.user || process.env.FRAIM_ACTIVE_USER_EMAIL || '';
|
|
30
|
+
if (!email.trim()) {
|
|
31
|
+
throw new Error('Missing active user email. Pass --user <email> or set FRAIM_ACTIVE_USER_EMAIL.');
|
|
32
|
+
}
|
|
33
|
+
return email.trim();
|
|
34
|
+
}
|
|
35
|
+
function emit(payload, json, text) {
|
|
36
|
+
process.stdout.write(json ? `${JSON.stringify(payload, null, 2)}\n` : `${text()}\n`);
|
|
37
|
+
}
|
|
38
|
+
function addCommonOptions(command) {
|
|
39
|
+
return command
|
|
40
|
+
.option('--workspace-root <path>', 'Workspace root containing fraim/config.json')
|
|
41
|
+
.option('--root <path>', 'Alias for --workspace-root')
|
|
42
|
+
.option('--user <email>', 'Active user email')
|
|
43
|
+
.option('--json', 'Print JSON');
|
|
44
|
+
}
|
|
45
|
+
// ── offers ───────────────────────────────────────────────────────────────────
|
|
46
|
+
const offersCommand = addCommonOptions(new commander_1.Command('offers'))
|
|
47
|
+
.description('List the learning entries and rule files the runtime delivered, so an attestation is written against the record rather than memory')
|
|
48
|
+
.option('--job <name>', 'The job whose context was loaded', 'unknown')
|
|
49
|
+
.option('--domain <domain>', 'Learning domain the job resolved to')
|
|
50
|
+
.option('--session', 'Use the session frame rather than the job frame')
|
|
51
|
+
.action((options) => {
|
|
52
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
53
|
+
const user = resolveUser(options);
|
|
54
|
+
const forJob = !options.session;
|
|
55
|
+
// Prefer what the runtime recorded for this job, because that is what was
|
|
56
|
+
// actually delivered. Re-deriving from the files on disk answers a subtly
|
|
57
|
+
// different question — what would be delivered now — and the two diverge if a
|
|
58
|
+
// learning file changed during the job.
|
|
59
|
+
const store = (0, learning_usage_store_1.readUsageStore)();
|
|
60
|
+
const recorded = Object.values(store.entries)
|
|
61
|
+
.filter((entry) => entry.offered.log.some((offer) => offer.job === options.job))
|
|
62
|
+
.map((entry) => {
|
|
63
|
+
const last = [...entry.offered.log].reverse().find((offer) => offer.job === options.job);
|
|
64
|
+
return {
|
|
65
|
+
key: entry.key,
|
|
66
|
+
family: entry.family,
|
|
67
|
+
id: entry.key.startsWith('L-') ? entry.key : null,
|
|
68
|
+
title: entry.titles[entry.titles.length - 1] ?? entry.key,
|
|
69
|
+
fileType: entry.fileType ?? 'unknown',
|
|
70
|
+
level: entry.level ?? 'unknown',
|
|
71
|
+
displayPath: '',
|
|
72
|
+
severity: null,
|
|
73
|
+
aboveThreshold: last.aboveThreshold,
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
const source = recorded.length > 0 ? 'offer-record' : 'derived';
|
|
77
|
+
const offered = recorded.length > 0
|
|
78
|
+
? recorded
|
|
79
|
+
: [
|
|
80
|
+
...(0, learning_context_builder_1.collectOfferedLearningEntries)(workspaceRoot, user, forJob, options.domain ?? null),
|
|
81
|
+
...(0, learning_context_builder_1.collectOfferedRuleFiles)(workspaceRoot, forJob),
|
|
82
|
+
];
|
|
83
|
+
const payload = { job: options.job, forJob, source, offered };
|
|
84
|
+
emit(payload, options.json, () => {
|
|
85
|
+
if (payload.offered.length === 0) {
|
|
86
|
+
return `No offer record and nothing resolves for job ${options.job}. Do not attest any firing for this job.`;
|
|
87
|
+
}
|
|
88
|
+
const rows = payload.offered.map((o) => `| ${o.id ?? '(no id)'} | ${o.title} | ${o.fileType} | ${o.aboveThreshold ? 'yes' : 'no'} |`);
|
|
89
|
+
const header = source === 'offer-record'
|
|
90
|
+
? `Entries offered for job ${options.job}, from the offer record:`
|
|
91
|
+
: `No offer record exists for job ${options.job} yet, so this is what would be delivered now. Say so when you attest against it.`;
|
|
92
|
+
return [
|
|
93
|
+
header,
|
|
94
|
+
'',
|
|
95
|
+
'| Id | Title | Family | Above threshold |',
|
|
96
|
+
'|---|---|---|---|',
|
|
97
|
+
...rows,
|
|
98
|
+
].join('\n');
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
// ── record-firings ───────────────────────────────────────────────────────────
|
|
102
|
+
const recordFiringsCommand = addCommonOptions(new commander_1.Command('record-firings'))
|
|
103
|
+
.description('Read the firing section of a retrospective and write the attested firings into the usage record')
|
|
104
|
+
.requiredOption('--retrospective <path>', 'Retrospective file to read')
|
|
105
|
+
.option('--job <name>', 'The job that produced the retrospective')
|
|
106
|
+
.option('--agent <name>', 'Agent that attested')
|
|
107
|
+
.option('--model <model>', 'Model that attested')
|
|
108
|
+
.option('--legacy', 'Also accept the pre-#1103 prose form')
|
|
109
|
+
.action((options) => {
|
|
110
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
111
|
+
const user = resolveUser(options);
|
|
112
|
+
const filePath = node_path_1.default.resolve(options.retrospective);
|
|
113
|
+
if (!node_fs_1.default.existsSync(filePath)) {
|
|
114
|
+
throw new Error(`Retrospective not found: ${filePath}`);
|
|
115
|
+
}
|
|
116
|
+
const content = node_fs_1.default.readFileSync(filePath, 'utf8');
|
|
117
|
+
const source = node_path_1.default.relative(workspaceRoot, filePath).replace(/\\/g, '/');
|
|
118
|
+
const result = (0, learning_usage_attestation_1.recordAttestationsFromRetrospective)(workspaceRoot, user, {
|
|
119
|
+
content,
|
|
120
|
+
source,
|
|
121
|
+
job: options.job ?? 'unknown',
|
|
122
|
+
agent: options.agent ?? null,
|
|
123
|
+
model: options.model ?? null,
|
|
124
|
+
legacy: Boolean(options.legacy),
|
|
125
|
+
});
|
|
126
|
+
emit(result, options.json, () => {
|
|
127
|
+
const lines = [
|
|
128
|
+
`Section state: ${result.sectionState}`,
|
|
129
|
+
`Items read: ${result.itemsRead}`,
|
|
130
|
+
`Recorded: ${result.applied}`,
|
|
131
|
+
`Already recorded: ${result.alreadyRecorded}`,
|
|
132
|
+
`Unmatched: ${result.unmatched.length}`,
|
|
133
|
+
`Rejected: ${result.rejected.length}`,
|
|
134
|
+
];
|
|
135
|
+
for (const item of result.unmatched)
|
|
136
|
+
lines.push(` unmatched: ${item.title} — ${item.reason}`);
|
|
137
|
+
for (const item of result.rejected)
|
|
138
|
+
lines.push(` rejected: ${item.reason}`);
|
|
139
|
+
return lines.join('\n');
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
// ── classify ─────────────────────────────────────────────────────────────────
|
|
143
|
+
const classifyCommand = addCommonOptions(new commander_1.Command('classify'))
|
|
144
|
+
.description('Say whether a recurrence was ignored (the entry was in context) or not-offered (it never reached the agent), because the two need different fixes')
|
|
145
|
+
.option('--entry <id>', 'The entry id, for an entry that carries one')
|
|
146
|
+
.option('--title <title>', 'The exact entry title, for an entry with no id yet. Use with --family')
|
|
147
|
+
.option('--family <family>', 'The entry family: mistake-patterns, preferences, validated-patterns, or manager-coaching')
|
|
148
|
+
.requiredOption('--job <name>', 'The job the recurrence happened in')
|
|
149
|
+
.requiredOption('--date <YYYY-MM-DD>', 'The date the mistake recurred')
|
|
150
|
+
.option('--window <days>', 'How far back an offer still counts as in-context', '30')
|
|
151
|
+
.action((options) => {
|
|
152
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
153
|
+
// `--title` plus `--family` exists so a caller never has to build the composite
|
|
154
|
+
// title key by hand. An entry title is corpus content and can contain any
|
|
155
|
+
// character, so an agent assembling `T:<family>:<title>` into a shell command
|
|
156
|
+
// would be interpolating untrusted text into a command line. Passing the title
|
|
157
|
+
// as its own argument keeps it a single argv entry.
|
|
158
|
+
let key;
|
|
159
|
+
if (options.entry) {
|
|
160
|
+
key = String(options.entry);
|
|
161
|
+
}
|
|
162
|
+
else if (options.title && options.family) {
|
|
163
|
+
key = (0, learning_usage_store_1.titleUsageKey)(String(options.family), String(options.title));
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
throw new Error('Pass --entry <id>, or --title <title> together with --family <family>.');
|
|
167
|
+
}
|
|
168
|
+
const store = (0, learning_usage_store_1.readUsageStore)();
|
|
169
|
+
const diagnosis = (0, learning_usage_analysis_1.classifyRecurrence)(store, {
|
|
170
|
+
key,
|
|
171
|
+
job: options.job,
|
|
172
|
+
date: options.date,
|
|
173
|
+
windowDays: Number.parseInt(options.window, 10) || 30,
|
|
174
|
+
});
|
|
175
|
+
void workspaceRoot;
|
|
176
|
+
emit(diagnosis, options.json, () => [
|
|
177
|
+
`Classification: ${diagnosis.classification}`,
|
|
178
|
+
`Diagnosis: ${diagnosis.diagnosis}`,
|
|
179
|
+
`Recommendation: ${diagnosis.recommendation}`,
|
|
180
|
+
`Raise the recurrence count: ${diagnosis.recurrenceBump ? 'yes' : 'no'}`,
|
|
181
|
+
].join('\n'));
|
|
182
|
+
});
|
|
183
|
+
// ── candidates ───────────────────────────────────────────────────────────────
|
|
184
|
+
const candidatesCommand = addCommonOptions(new commander_1.Command('candidates'))
|
|
185
|
+
.description('List retirement candidates: offered often under the current model with no firing under it, paired with a structural signal')
|
|
186
|
+
.option('--model <model>', 'Override the current model (defaults to the most recently offered-to model)')
|
|
187
|
+
.option('--detector <path>', 'Path to the corpus hygiene detector that provides the structural signal')
|
|
188
|
+
.action((options) => {
|
|
189
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
190
|
+
const limits = (0, learning_usage_store_1.resolveUsageLimits)(workspaceRoot);
|
|
191
|
+
const result = (0, learning_usage_analysis_1.findRetirementCandidates)((0, learning_usage_store_1.readUsageStore)(), {
|
|
192
|
+
workspaceRoot,
|
|
193
|
+
limits,
|
|
194
|
+
currentModel: options.model ?? undefined,
|
|
195
|
+
structuralSignalPath: options.detector,
|
|
196
|
+
});
|
|
197
|
+
emit(result, options.json, () => {
|
|
198
|
+
if (result.candidates.length === 0) {
|
|
199
|
+
return `No retirement candidates. Current model: ${result.currentModel ?? 'unknown'}; threshold ${result.offerThreshold} offers.`;
|
|
200
|
+
}
|
|
201
|
+
const lines = [`Current model: ${result.currentModel ?? 'unknown'}. Threshold: ${result.offerThreshold} offers.`, ''];
|
|
202
|
+
for (const c of result.candidates) {
|
|
203
|
+
const earlier = c.firedUnderEarlierModels.map((m) => `${m.count} under ${m.model}`).join(', ') || 'none under any earlier model';
|
|
204
|
+
lines.push(`- ${c.title}`, ` Offered ${c.offeredUnderCurrentModel} times under the current model, fired ${c.firedUnderCurrentModel}. Earlier: ${earlier}.`, ` Structural signal: ${c.structural}. ${c.structuralDetail}`, ` Recommendation: ${c.recommendation}`);
|
|
205
|
+
}
|
|
206
|
+
return lines.join('\n');
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
// ── standing ─────────────────────────────────────────────────────────────────
|
|
210
|
+
const standingCommand = addCommonOptions(new commander_1.Command('standing'))
|
|
211
|
+
.description('Assess each entry as promoted, standard or demoted from its usage record, and list the ones whose standing should change')
|
|
212
|
+
.option('--changes-only', 'Print only the entries whose standing should change')
|
|
213
|
+
.action((options) => {
|
|
214
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
215
|
+
const limits = (0, learning_usage_store_1.resolveUsageLimits)(workspaceRoot);
|
|
216
|
+
const report = (0, learning_usage_analysis_1.assessStanding)((0, learning_usage_store_1.readUsageStore)(), { limits });
|
|
217
|
+
const payload = options.changesOnly ? { ...report, assessments: report.changes } : report;
|
|
218
|
+
emit(payload, options.json, () => {
|
|
219
|
+
const rows = (options.changesOnly ? report.changes : report.assessments);
|
|
220
|
+
if (rows.length === 0) {
|
|
221
|
+
return options.changesOnly
|
|
222
|
+
? 'No standing changes. Every measured entry is already where the record says it belongs.'
|
|
223
|
+
: 'No entry has a usage record yet, so there is nothing to assess.';
|
|
224
|
+
}
|
|
225
|
+
const lines = [];
|
|
226
|
+
if (report.changes.length > 0) {
|
|
227
|
+
lines.push(`${report.changes.length} entr${report.changes.length === 1 ? 'y' : 'ies'} should change standing.`, '');
|
|
228
|
+
}
|
|
229
|
+
for (const a of rows) {
|
|
230
|
+
lines.push(`- ${a.title}`);
|
|
231
|
+
lines.push(` ${a.standing}${a.recommendation ? ` (recommend: ${a.recommendation})` : ''}. ${a.reason}`);
|
|
232
|
+
}
|
|
233
|
+
lines.push('', 'Nothing is applied here. Run sleep-on-learnings to decide.');
|
|
234
|
+
return lines.join('\n');
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
// ── report ───────────────────────────────────────────────────────────────────
|
|
238
|
+
const reportCommand = addCommonOptions(new commander_1.Command('report'))
|
|
239
|
+
.description('Answer which learnings are still earning their place, from the record rather than from memory')
|
|
240
|
+
.option('--window <days>', 'Reporting window', '90')
|
|
241
|
+
.action((options) => {
|
|
242
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
243
|
+
const limits = (0, learning_usage_store_1.resolveUsageLimits)(workspaceRoot);
|
|
244
|
+
const report = (0, learning_usage_analysis_1.buildUsageReport)((0, learning_usage_store_1.readUsageStore)(), {
|
|
245
|
+
workspaceRoot,
|
|
246
|
+
limits,
|
|
247
|
+
windowDays: Number.parseInt(options.window, 10) || 90,
|
|
248
|
+
});
|
|
249
|
+
emit(report, options.json, () => {
|
|
250
|
+
const lines = [
|
|
251
|
+
`${report.totals.entriesOffered} entries were offered. ${report.totals.entriesWithFirings} fired. ${report.totals.entriesNeverFired} have never fired.`,
|
|
252
|
+
];
|
|
253
|
+
if (report.workingHardest.length) {
|
|
254
|
+
lines.push('', 'Working hardest');
|
|
255
|
+
report.workingHardest.forEach((e, i) => lines.push(`${i + 1}. ${e.title}. Offered ${e.offered} times, fired ${e.fired}. Last fired ${e.lastFired ?? 'never'}.`));
|
|
256
|
+
}
|
|
257
|
+
if (report.neverFired.length) {
|
|
258
|
+
lines.push('', 'Never fired');
|
|
259
|
+
report.neverFired.forEach((e, i) => lines.push(`${i + 1}. ${e.title}. Offered ${e.offered} times, never fired.`));
|
|
260
|
+
}
|
|
261
|
+
if (report.ignoredWhileInContext.length) {
|
|
262
|
+
lines.push('', 'Ignored while in context');
|
|
263
|
+
report.ignoredWhileInContext.forEach((e, i) => lines.push(`${i + 1}. ${e.title}. In context and the work went against it, so the entry needs work rather than another recurrence.`));
|
|
264
|
+
}
|
|
265
|
+
lines.push('', 'Nothing is retired from this view. Run sleep-on-learnings to decide.');
|
|
266
|
+
return lines.join('\n');
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
// ── backfill ─────────────────────────────────────────────────────────────────
|
|
270
|
+
const backfillCommand = addCommonOptions(new commander_1.Command('backfill'))
|
|
271
|
+
.description('Read the firings already attested in existing retrospectives, matching only on an id or an exact title, and report what could not be matched')
|
|
272
|
+
.option('--agent <name>', 'Agent to attribute the backfilled records to', 'backfill')
|
|
273
|
+
.option('--model <model>', 'Model to attribute the backfilled records to')
|
|
274
|
+
.action((options) => {
|
|
275
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
276
|
+
const user = resolveUser(options);
|
|
277
|
+
const report = (0, learning_usage_attestation_1.runBackfill)(workspaceRoot, user, {
|
|
278
|
+
agent: options.agent ?? 'backfill',
|
|
279
|
+
model: options.model ?? null,
|
|
280
|
+
});
|
|
281
|
+
emit(report, options.json, () => [
|
|
282
|
+
`Files scanned: ${report.filesScanned} (${report.filesWithSection} carry the section, ${report.synthesizedFilesRead} already synthesized)`,
|
|
283
|
+
`Items read: ${report.itemsRead}`,
|
|
284
|
+
`Applied: ${report.applied}`,
|
|
285
|
+
`Already recorded: ${report.alreadyRecorded}`,
|
|
286
|
+
`Unmatched: ${report.unmatched.length}`,
|
|
287
|
+
`Rejected: ${report.rejected.length}`,
|
|
288
|
+
'',
|
|
289
|
+
'Only an id or an exact title resolves. A low match rate is expected on retrospectives written before entries carried ids, and is reported rather than resolved by similarity matching.',
|
|
290
|
+
].join('\n'));
|
|
291
|
+
});
|
|
292
|
+
/**
|
|
293
|
+
* Add the `**Id**` line to entries that do not have one.
|
|
294
|
+
*
|
|
295
|
+
* This is a format migration, not a change to any lesson: it inserts one
|
|
296
|
+
* identifier line per entry and touches nothing else. R19 forbids a new write path
|
|
297
|
+
* into the corpus for usage data, and this is not one — it writes no usage data,
|
|
298
|
+
* and it is run explicitly by the manager rather than by any automatic path.
|
|
299
|
+
*/
|
|
300
|
+
function stampFile(filePath, fileType, apply) {
|
|
301
|
+
const content = node_fs_1.default.readFileSync(filePath, 'utf8');
|
|
302
|
+
const eol = content.includes('\r\n') ? '\r\n' : '\n';
|
|
303
|
+
const lines = content.split(/\r?\n/);
|
|
304
|
+
const headingRe = (0, learning_context_builder_1.learningEntryHeadingRegex)();
|
|
305
|
+
const idRe = (0, learning_context_builder_1.learningEntryIdLineRegex)();
|
|
306
|
+
const out = [];
|
|
307
|
+
let stamped = 0;
|
|
308
|
+
let alreadyStamped = 0;
|
|
309
|
+
for (let i = 0; i < lines.length; i++) {
|
|
310
|
+
const line = lines[i];
|
|
311
|
+
out.push(line);
|
|
312
|
+
const header = line.match(headingRe);
|
|
313
|
+
if (!header)
|
|
314
|
+
continue;
|
|
315
|
+
// Look ahead to the next entry heading for an existing id line.
|
|
316
|
+
let hasId = false;
|
|
317
|
+
for (let j = i + 1; j < lines.length; j++) {
|
|
318
|
+
if (headingRe.test(lines[j]))
|
|
319
|
+
break;
|
|
320
|
+
if (idRe.test(lines[j].trim())) {
|
|
321
|
+
hasId = true;
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (hasId) {
|
|
326
|
+
alreadyStamped++;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
const title = header[2].trim();
|
|
330
|
+
if (!title)
|
|
331
|
+
continue;
|
|
332
|
+
// Insert immediately after the heading, keeping the blank line that usually
|
|
333
|
+
// follows it so the file's shape is unchanged.
|
|
334
|
+
if (lines[i + 1] !== undefined && lines[i + 1].trim() === '') {
|
|
335
|
+
out.push('');
|
|
336
|
+
out.push(`**Id**: ${(0, learning_context_builder_1.generateLearningEntryId)(fileType, title)}`);
|
|
337
|
+
i++;
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
out.push(`**Id**: ${(0, learning_context_builder_1.generateLearningEntryId)(fileType, title)}`);
|
|
341
|
+
}
|
|
342
|
+
stamped++;
|
|
343
|
+
}
|
|
344
|
+
if (apply && stamped > 0) {
|
|
345
|
+
node_fs_1.default.writeFileSync(filePath, out.join(eol), 'utf8');
|
|
346
|
+
}
|
|
347
|
+
return { file: filePath, entriesStamped: stamped, entriesAlreadyStamped: alreadyStamped };
|
|
348
|
+
}
|
|
349
|
+
const FILE_TYPES = ['mistake-patterns', 'preferences', 'validated-patterns', 'manager-coaching'];
|
|
350
|
+
function fileTypeFromName(fileName) {
|
|
351
|
+
for (const type of FILE_TYPES) {
|
|
352
|
+
if (fileName.includes(type))
|
|
353
|
+
return type;
|
|
354
|
+
}
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
const stampIdsCommand = addCommonOptions(new commander_1.Command('stamp-ids'))
|
|
358
|
+
.description('Add the stable Id line to learning entries that do not have one. A format migration: it inserts one identifier line per entry and changes no lesson text')
|
|
359
|
+
.option('--dir <path>', 'Learning directory to stamp (repeatable)', (value, previous) => [...previous, value], [])
|
|
360
|
+
.option('--apply', 'Write the changes. Without this flag the command reports what it would do')
|
|
361
|
+
.action((options) => {
|
|
362
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
363
|
+
const dirs = options.dir.length
|
|
364
|
+
? options.dir.map((d) => node_path_1.default.resolve(d))
|
|
365
|
+
: [node_path_1.default.join(workspaceRoot, 'fraim', 'personalized-employee', 'learnings')];
|
|
366
|
+
const results = [];
|
|
367
|
+
for (const dir of dirs) {
|
|
368
|
+
if (!node_fs_1.default.existsSync(dir))
|
|
369
|
+
continue;
|
|
370
|
+
for (const fileName of node_fs_1.default.readdirSync(dir)) {
|
|
371
|
+
if (!fileName.endsWith('.md'))
|
|
372
|
+
continue;
|
|
373
|
+
const fileType = fileTypeFromName(fileName);
|
|
374
|
+
if (!fileType)
|
|
375
|
+
continue;
|
|
376
|
+
results.push(stampFile(node_path_1.default.join(dir, fileName), fileType, Boolean(options.apply)));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const payload = {
|
|
380
|
+
apply: Boolean(options.apply),
|
|
381
|
+
filesScanned: results.length,
|
|
382
|
+
entriesStamped: results.reduce((sum, r) => sum + r.entriesStamped, 0),
|
|
383
|
+
entriesAlreadyStamped: results.reduce((sum, r) => sum + r.entriesAlreadyStamped, 0),
|
|
384
|
+
files: results,
|
|
385
|
+
};
|
|
386
|
+
emit(payload, options.json, () => [
|
|
387
|
+
`${payload.apply ? 'Stamped' : 'Would stamp'} ${payload.entriesStamped} entries across ${payload.filesScanned} files.`,
|
|
388
|
+
`${payload.entriesAlreadyStamped} entries already carry an id.`,
|
|
389
|
+
payload.apply ? '' : 'Re-run with --apply to write the changes.',
|
|
390
|
+
].filter(Boolean).join('\n'));
|
|
391
|
+
});
|
|
392
|
+
// ── prune ────────────────────────────────────────────────────────────────────
|
|
393
|
+
const pruneCommand = addCommonOptions(new commander_1.Command('prune'))
|
|
394
|
+
.description('Apply the configured retention window and log bounds to the usage record. Aggregate counters are never pruned')
|
|
395
|
+
.action((options) => {
|
|
396
|
+
const workspaceRoot = resolveWorkspaceRoot(options);
|
|
397
|
+
const limits = (0, learning_usage_store_1.resolveUsageLimits)(workspaceRoot);
|
|
398
|
+
const result = (0, learning_usage_store_1.pruneUsageStore)(limits);
|
|
399
|
+
emit({ ...result, storePath: (0, learning_usage_store_1.resolveUsageStorePath)(), limits }, options.json, () => `Pruned ${result.prunedOfferRecords} offer records and ${result.prunedFiringRecords} firing records across ${result.entries} entries. Aggregates unchanged.`);
|
|
400
|
+
});
|
|
401
|
+
// ── root ─────────────────────────────────────────────────────────────────────
|
|
402
|
+
exports.learningUsageCommand = new commander_1.Command('learning-usage')
|
|
403
|
+
.description('Read and analyse the learning and rule usage record (issue #1103)')
|
|
404
|
+
.addCommand(offersCommand)
|
|
405
|
+
.addCommand(recordFiringsCommand)
|
|
406
|
+
.addCommand(classifyCommand)
|
|
407
|
+
.addCommand(candidatesCommand)
|
|
408
|
+
.addCommand(standingCommand)
|
|
409
|
+
.addCommand(reportCommand)
|
|
410
|
+
.addCommand(backfillCommand)
|
|
411
|
+
.addCommand(stampIdsCommand)
|
|
412
|
+
.addCommand(pruneCommand);
|
package/dist/src/cli/fraim.js
CHANGED
|
@@ -56,6 +56,7 @@ const workspace_config_1 = require("./commands/workspace-config");
|
|
|
56
56
|
const org_1 = require("./commands/org");
|
|
57
57
|
const manager_1 = require("./commands/manager");
|
|
58
58
|
const cleanup_artifacts_1 = require("./commands/cleanup-artifacts");
|
|
59
|
+
const learning_usage_1 = require("./commands/learning-usage");
|
|
59
60
|
const fs_1 = __importDefault(require("fs"));
|
|
60
61
|
const path_1 = __importDefault(require("path"));
|
|
61
62
|
const program = new commander_1.Command();
|
|
@@ -101,6 +102,7 @@ program.addCommand(workspace_config_1.workspaceConfigCommand);
|
|
|
101
102
|
program.addCommand(org_1.orgCommand);
|
|
102
103
|
program.addCommand(manager_1.managerCommand);
|
|
103
104
|
program.addCommand(cleanup_artifacts_1.cleanupArtifactsCommand);
|
|
105
|
+
program.addCommand(learning_usage_1.learningUsageCommand);
|
|
104
106
|
// Wait for async command initialization before parsing
|
|
105
107
|
(async () => {
|
|
106
108
|
// Import the initialization promise from setup command
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AIMentor = void 0;
|
|
4
4
|
const include_resolver_1 = require("./utils/include-resolver");
|
|
5
|
+
const resolve_phase_edge_1 = require("./resolve-phase-edge");
|
|
5
6
|
class AIMentor {
|
|
6
7
|
constructor(resolver, skillDedup) {
|
|
7
8
|
this.jobCache = new Map();
|
|
@@ -36,7 +37,10 @@ class AIMentor {
|
|
|
36
37
|
return await this.generateCompletionMessage(workflow, args.currentPhase, args.findings, args.evidence, args.skipIncludes);
|
|
37
38
|
}
|
|
38
39
|
else {
|
|
39
|
-
|
|
40
|
+
// Issue #1123: findings/evidence must reach the failure path too. The
|
|
41
|
+
// MCP layer has always forwarded them for every status; only this hop
|
|
42
|
+
// dropped them, which is why a failure edge could not be discriminated.
|
|
43
|
+
return await this.generateHelpMessage(workflow, args.currentPhase, args.status, args.skipIncludes, args.findings, args.evidence);
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
async getOrLoadJob(jobType) {
|
|
@@ -65,15 +69,21 @@ class AIMentor {
|
|
|
65
69
|
return '';
|
|
66
70
|
const onSuccess = phaseFlow.onSuccess;
|
|
67
71
|
const completionCall = `seekMentoring({ jobName: "${jobName}", issueNumber: "<issue_number>", currentPhase: "${phaseId}", status: "complete" })`;
|
|
72
|
+
// Issue #1123: a routing map nobody knows how to trigger is decorative,
|
|
73
|
+
// which is the defect recorded as #1135. Whichever edge carries a map, the
|
|
74
|
+
// footer has to name its outcomes so the agent can actually supply one.
|
|
75
|
+
const failureOutcomes = (0, resolve_phase_edge_1.discriminantKeys)(phaseFlow.onFailure);
|
|
76
|
+
const failureNote = failureOutcomes.length > 0
|
|
77
|
+
? `\nIf this phase needs to loop back, call the same tool with \`status: "failure"\`, and set \`findings.phaseOutcome\` to one of: ${failureOutcomes.map((key) => `"${key}"`).join(' | ')} when one applies. Omit it otherwise.`
|
|
78
|
+
: '';
|
|
68
79
|
if (!onSuccess || typeof onSuccess === 'string') {
|
|
69
80
|
const finalPhaseNote = onSuccess ? '' : ' This is the final phase.';
|
|
70
|
-
return `\n\n---\n\n## Report Back\nWhen this phase is done, call \`${completionCall}\`.${finalPhaseNote}`;
|
|
81
|
+
return `\n\n---\n\n## Report Back\nWhen this phase is done, call \`${completionCall}\`.${finalPhaseNote}${failureNote}`;
|
|
71
82
|
}
|
|
72
|
-
const validOutcomes =
|
|
73
|
-
.filter((key) => key !== 'default')
|
|
83
|
+
const validOutcomes = (0, resolve_phase_edge_1.discriminantKeys)(onSuccess)
|
|
74
84
|
.map((key) => `"${key}"`)
|
|
75
85
|
.join(' | ');
|
|
76
|
-
return `\n\n---\n\n## Report Back\nWhen this phase is done, call \`seekMentoring({ jobName: "${jobName}", issueNumber: "<issue_number>", currentPhase: "${phaseId}", status: "complete", findings: { issueType: "<outcome>" } })\` with one of: ${validOutcomes}
|
|
86
|
+
return `\n\n---\n\n## Report Back\nWhen this phase is done, call \`seekMentoring({ jobName: "${jobName}", issueNumber: "<issue_number>", currentPhase: "${phaseId}", status: "complete", findings: { issueType: "<outcome>" } })\` with one of: ${validOutcomes}.${failureNote}`;
|
|
77
87
|
}
|
|
78
88
|
/** Phase-authority content injected for all phased workflows. Loaded from orchestration/phase-authority.md. */
|
|
79
89
|
async getPhaseAuthorityContent() {
|
|
@@ -158,13 +168,9 @@ class AIMentor {
|
|
|
158
168
|
const phaseFlow = workflow.metadata.phases?.[phaseId];
|
|
159
169
|
let nextPhaseId = null;
|
|
160
170
|
if (phaseFlow && phaseFlow.onSuccess) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
else {
|
|
165
|
-
const outcome = findings?.phaseOutcome ?? findings?.issueType ?? evidence?.issueType ?? evidence?.phaseOutcome ?? 'default';
|
|
166
|
-
nextPhaseId = phaseFlow.onSuccess[outcome] ?? phaseFlow.onSuccess.default ?? null;
|
|
167
|
-
}
|
|
171
|
+
// Issue #1123: resolved through the shared authority so the success and
|
|
172
|
+
// failure paths cannot read the discriminant differently.
|
|
173
|
+
nextPhaseId = (0, resolve_phase_edge_1.resolvePhaseEdge)(phaseFlow.onSuccess, (0, resolve_phase_edge_1.resolveDiscriminant)(findings, evidence));
|
|
168
174
|
}
|
|
169
175
|
let message = '';
|
|
170
176
|
if (nextPhaseId) {
|
|
@@ -189,7 +195,7 @@ class AIMentor {
|
|
|
189
195
|
status: 'complete'
|
|
190
196
|
};
|
|
191
197
|
}
|
|
192
|
-
async generateHelpMessage(workflow, phaseId, status, skipIncludes) {
|
|
198
|
+
async generateHelpMessage(workflow, phaseId, status, skipIncludes, findings, evidence) {
|
|
193
199
|
const entityType = 'Job';
|
|
194
200
|
if (workflow.isSimple) {
|
|
195
201
|
const message = `${entityType}: ${workflow.metadata.name}\n\n${workflow.overview}`;
|
|
@@ -201,7 +207,14 @@ class AIMentor {
|
|
|
201
207
|
};
|
|
202
208
|
}
|
|
203
209
|
const phaseMeta = workflow.metadata.phases?.[phaseId];
|
|
204
|
-
|
|
210
|
+
// Issue #1123: the failure edge may be a discriminant map. Resolving to
|
|
211
|
+
// null (terminal, malformed, or a map with no `default` and no match)
|
|
212
|
+
// falls back to self-retry, which is what the previous `|| phaseId` did
|
|
213
|
+
// for an absent edge. The old expression could not do this: an object is
|
|
214
|
+
// truthy, so it was returned as the target and `phases.get()` then missed.
|
|
215
|
+
const targetPhaseId = status === 'failure'
|
|
216
|
+
? ((0, resolve_phase_edge_1.resolvePhaseEdge)(phaseMeta?.onFailure, (0, resolve_phase_edge_1.resolveDiscriminant)(findings, evidence)) || phaseId)
|
|
217
|
+
: phaseId;
|
|
205
218
|
let message = `### Current Phase: ${targetPhaseId}\n\n`;
|
|
206
219
|
let instructions = workflow.phases.get(targetPhaseId);
|
|
207
220
|
if (instructions) {
|