imprint-mcp 0.6.3 → 0.6.5
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/README.md +13 -1
- package/package.json +1 -1
- package/src/cli.ts +64 -12
- package/src/imprint/audit.ts +43 -22
- package/src/imprint/claude-cli-compile.ts +43 -7
- package/src/imprint/codex-cli-compile.ts +205 -129
- package/src/imprint/inject-listener.ts +31 -3
- package/src/imprint/llm.ts +3 -3
- package/src/imprint/react-flight-limits.ts +31 -0
- package/src/imprint/record.ts +657 -46
- package/src/imprint/redact.ts +240 -30
- package/src/imprint/response-body-intent.ts +73 -0
- package/src/imprint/response-body-lifecycle.ts +32 -0
- package/src/imprint/response-body-stream.ts +704 -0
- package/src/imprint/teach-state.ts +156 -14
- package/src/imprint/teach.ts +297 -116
- package/src/imprint/tool-candidates.ts +121 -0
- package/src/imprint/tracing.ts +60 -238
package/src/imprint/teach.ts
CHANGED
|
@@ -23,6 +23,7 @@ import { authWorkflowHash } from './auth-compile-tools.ts';
|
|
|
23
23
|
import {
|
|
24
24
|
type SharedModuleManifestEntry,
|
|
25
25
|
buildPlanSidecarPath,
|
|
26
|
+
deriveTokenContractHints,
|
|
26
27
|
readBuildPlanFile,
|
|
27
28
|
topoLevelsForTools,
|
|
28
29
|
} from './build-plan.ts';
|
|
@@ -95,26 +96,34 @@ import {
|
|
|
95
96
|
type WorkflowState,
|
|
96
97
|
analysisBlockRunsForWindow,
|
|
97
98
|
buildTeachStateFromSession,
|
|
99
|
+
candidateSelectionChanged,
|
|
98
100
|
detectCandidatesCompletedSteps,
|
|
99
101
|
discoverCompletedWorkflows,
|
|
100
102
|
discoverOrphanSession,
|
|
101
103
|
friendlySessionTimestamp,
|
|
104
|
+
isCandidateBackedResume,
|
|
102
105
|
isExistingTeachFile as isExistingFile,
|
|
103
106
|
loadTeachState,
|
|
104
107
|
nextTeachStep as nextStep,
|
|
105
108
|
pruneStalePendingTeachWorkflows,
|
|
109
|
+
pruneUnselectedCandidateWorkflows,
|
|
106
110
|
resolveStepStartTarget,
|
|
107
111
|
resolveTeachStatePath,
|
|
108
112
|
resolveWorkflowTriagedPath,
|
|
113
|
+
resolvedTeachRunPolicy,
|
|
109
114
|
saveTeachState,
|
|
110
115
|
selectMultiToolResumePlans,
|
|
116
|
+
staleCompletedToolsForRun,
|
|
111
117
|
toRelativeTeachStatePath as toRelative,
|
|
112
118
|
} from './teach-state.ts';
|
|
113
119
|
import {
|
|
114
120
|
type SharedCompileContext,
|
|
115
121
|
type ToolCandidate,
|
|
116
122
|
buildSharedCompileContext as buildCandidateSharedCompileContext,
|
|
123
|
+
closeCandidateSelection,
|
|
124
|
+
deriveStructuralCandidateDependencies,
|
|
117
125
|
detectToolCandidates,
|
|
126
|
+
mergeCandidateDependencies,
|
|
118
127
|
primaryToolCandidate,
|
|
119
128
|
sharedContextHasAuth,
|
|
120
129
|
} from './tool-candidates.ts';
|
|
@@ -178,6 +187,40 @@ function fileSha256(path: string): string {
|
|
|
178
187
|
return createHash('sha256').update(readFileSync(path)).digest('hex');
|
|
179
188
|
}
|
|
180
189
|
|
|
190
|
+
/** Read replay classifications only when the sidecar belongs to the exact
|
|
191
|
+
* recording being compiled. Request seq numbers are recording-local, so using
|
|
192
|
+
* a site-global cache from another recording could fabricate dependency edges. */
|
|
193
|
+
export function loadCachedClassificationsForSession(
|
|
194
|
+
classPath: string,
|
|
195
|
+
sessionPath: string,
|
|
196
|
+
): ClassifiedValue[] | undefined {
|
|
197
|
+
if (!existsSync(classPath) || !existsSync(sessionPath)) return undefined;
|
|
198
|
+
try {
|
|
199
|
+
const cached = JSON.parse(readFileSync(classPath, 'utf8')) as {
|
|
200
|
+
sourceSessionHash?: string;
|
|
201
|
+
classifications?: ClassifiedValue[];
|
|
202
|
+
};
|
|
203
|
+
if (
|
|
204
|
+
!cached.sourceSessionHash ||
|
|
205
|
+
cached.sourceSessionHash !== fileSha256(sessionPath) ||
|
|
206
|
+
!Array.isArray(cached.classifications)
|
|
207
|
+
) {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
return cached.classifications;
|
|
211
|
+
} catch {
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function classificationsPlanningHash(
|
|
217
|
+
classifications: ClassifiedValue[] | undefined,
|
|
218
|
+
): string | undefined {
|
|
219
|
+
return classifications
|
|
220
|
+
? createHash('sha256').update(JSON.stringify(classifications)).digest('hex')
|
|
221
|
+
: undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
181
224
|
export {
|
|
182
225
|
buildTeachStateFromSession,
|
|
183
226
|
resolveTeachStatePath,
|
|
@@ -227,8 +270,8 @@ interface TeachOptions {
|
|
|
227
270
|
fromSession?: string;
|
|
228
271
|
/** Retain parser.test.ts after successful compile-agent verification. */
|
|
229
272
|
keepTest?: boolean;
|
|
230
|
-
/**
|
|
231
|
-
|
|
273
|
+
/** Compile only the primary detected candidate and its upstream dependencies. */
|
|
274
|
+
primaryTool?: boolean;
|
|
232
275
|
/** Bounded resume: compile exactly one persisted candidate from the retained recording. */
|
|
233
276
|
tool?: string;
|
|
234
277
|
/** Skip the replay-and-diff stage entirely. */
|
|
@@ -518,6 +561,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
518
561
|
let redactedPath: string | null = null;
|
|
519
562
|
let resumedTriagedPath: string | null = null;
|
|
520
563
|
let usingFromSession = false;
|
|
564
|
+
let resumesExistingRun = false;
|
|
521
565
|
|
|
522
566
|
const hasExisting = completedWorkflows.length > 0 || incompleteWorkflows.length > 0;
|
|
523
567
|
|
|
@@ -534,6 +578,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
534
578
|
// run's persisted outputs. resolveStepStartTarget picks the most-recent
|
|
535
579
|
// workflow and THROWS if it didn't reach far enough (the dependency guard).
|
|
536
580
|
const target = resolveStepStartTarget(site, state, opts.fromStep, opts.tool);
|
|
581
|
+
resumesExistingRun = true;
|
|
537
582
|
workflowKey = target.workflowKey;
|
|
538
583
|
startFrom = opts.fromStep;
|
|
539
584
|
sessionPath = resolveTeachStatePath(site, target.ws.sessionPath);
|
|
@@ -571,6 +616,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
571
616
|
if (choice.action === 'new') {
|
|
572
617
|
startFrom = 'record';
|
|
573
618
|
} else if (choice.action === 'continue') {
|
|
619
|
+
resumesExistingRun = true;
|
|
574
620
|
workflowKey = choice.workflowKey;
|
|
575
621
|
const ws = state.workflows[workflowKey];
|
|
576
622
|
if (!ws) {
|
|
@@ -584,6 +630,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
584
630
|
} else if (choice.action === 'redo') {
|
|
585
631
|
workflowKey = choice.workflowKey;
|
|
586
632
|
startFrom = choice.fromStep;
|
|
633
|
+
resumesExistingRun = startFrom !== 'record';
|
|
587
634
|
const ws = state.workflows[workflowKey];
|
|
588
635
|
if (ws) {
|
|
589
636
|
sessionPath = resolveTeachStatePath(site, ws.sessionPath);
|
|
@@ -614,6 +661,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
614
661
|
}
|
|
615
662
|
|
|
616
663
|
const startIdx = STEPS.indexOf(startFrom);
|
|
664
|
+
const runPolicy = resolvedTeachRunPolicy(startFrom, resumesExistingRun);
|
|
617
665
|
// Upper bound of the phase window: `--to-step`/`--only` stop the chain after a
|
|
618
666
|
// given step (default = the last step, i.e. run to the end). A phase runs only
|
|
619
667
|
// when it falls within [startFrom, toStep].
|
|
@@ -912,6 +960,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
912
960
|
if (stopIdx < STEPS.indexOf('replay-and-diff')) await finishEarly();
|
|
913
961
|
|
|
914
962
|
let plans: CandidateCompilePlan[];
|
|
963
|
+
let candidateBackedResume = false;
|
|
915
964
|
|
|
916
965
|
// The replay→triage→detect-candidates analysis is one atomic block (the sub-
|
|
917
966
|
// steps share a parallel run + the triaged session), so the window can START
|
|
@@ -959,24 +1008,30 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
959
1008
|
const replayPromise = (async () => {
|
|
960
1009
|
if (!needsReplay) {
|
|
961
1010
|
const classPath = pathJoin(localSiteDir(site), '.classifications.json');
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
/* proceed without */
|
|
968
|
-
}
|
|
1011
|
+
const cached = loadCachedClassificationsForSession(classPath, replaySessionPath);
|
|
1012
|
+
if (!cached && existsSync(classPath)) {
|
|
1013
|
+
p.log.warn(
|
|
1014
|
+
'Ignoring cached replay classifications because they are untagged or belong to a different recording.',
|
|
1015
|
+
);
|
|
969
1016
|
}
|
|
970
|
-
return
|
|
1017
|
+
return cached;
|
|
971
1018
|
}
|
|
972
1019
|
return siteReplayAndDiff(site, replaySessionPath, mp);
|
|
973
1020
|
})();
|
|
974
1021
|
|
|
975
1022
|
// Branch B: triage → detect-candidates → user selection (fast, ~30s)
|
|
976
|
-
type CandidateChainResult =
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
1023
|
+
type CandidateChainResult =
|
|
1024
|
+
| {
|
|
1025
|
+
triageResult?: TriageResult;
|
|
1026
|
+
plans: CandidateCompilePlan[];
|
|
1027
|
+
}
|
|
1028
|
+
| {
|
|
1029
|
+
triageResult?: TriageResult;
|
|
1030
|
+
detection: Awaited<ReturnType<typeof detectTeachCandidates>>;
|
|
1031
|
+
preliminarySelected: ToolCandidate[];
|
|
1032
|
+
baseState: WorkflowState;
|
|
1033
|
+
pendingKey: string | null;
|
|
1034
|
+
};
|
|
980
1035
|
const candidatePromise = (async (): Promise<CandidateChainResult> => {
|
|
981
1036
|
if (!needsCandidates) {
|
|
982
1037
|
const ws = state.workflows[workflowKey];
|
|
@@ -1065,12 +1120,16 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1065
1120
|
mp.pause();
|
|
1066
1121
|
mp.clear();
|
|
1067
1122
|
spinner.start('Detecting candidate tools');
|
|
1068
|
-
const
|
|
1123
|
+
const detected = await detectTeachCandidates({
|
|
1069
1124
|
sessionPath: compileSessionPath,
|
|
1070
1125
|
providerName,
|
|
1071
1126
|
model,
|
|
1072
1127
|
trustSessionScope: !!localTriagedPath,
|
|
1073
1128
|
});
|
|
1129
|
+
const detection = {
|
|
1130
|
+
...detected,
|
|
1131
|
+
candidates: deriveStructuralCandidateDependencies(detected.candidates),
|
|
1132
|
+
};
|
|
1074
1133
|
spinner.stop(
|
|
1075
1134
|
`Detected ${detection.candidates.length} candidate tool${detection.candidates.length === 1 ? '' : 's'}.`,
|
|
1076
1135
|
);
|
|
@@ -1079,8 +1138,6 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1079
1138
|
const selected = await selectTeachCandidates(detection, opts);
|
|
1080
1139
|
mp.resume();
|
|
1081
1140
|
|
|
1082
|
-
const sharedContext = buildCandidateSharedCompileContext(detection, selected);
|
|
1083
|
-
|
|
1084
1141
|
// ── Credential prompt (deferred until here so the LLM decides which login succeeded) ──
|
|
1085
1142
|
const llmLoginSeqs = new Set(detection.sharedContext?.loginRequestSeqs ?? []);
|
|
1086
1143
|
if (credentialFindings.length > 0 && llmLoginSeqs.size > 0) {
|
|
@@ -1147,48 +1204,17 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1147
1204
|
if (localTriagedPath) {
|
|
1148
1205
|
baseState.triagedPath = toRelative(site, localTriagedPath);
|
|
1149
1206
|
}
|
|
1150
|
-
const candidatePlans = selected.map((candidate) => {
|
|
1151
|
-
checkpoint(site, state, candidate.toolName, {
|
|
1152
|
-
...baseState,
|
|
1153
|
-
// Preserve prior progress only when re-detecting the SAME recording, so
|
|
1154
|
-
// a re-run of the analysis block (`--from-step`/`--only detect-candidates`,
|
|
1155
|
-
// or interactive redo) doesn't regress a tool that already reached
|
|
1156
|
-
// generate…register (which would break a later `--from-step register`).
|
|
1157
|
-
// A fresh / different recording producing the same toolName must NOT
|
|
1158
|
-
// inherit the old `plan-prereqs` marker, or the alreadyPlanned shortcut
|
|
1159
|
-
// below would skip re-planning and compile against the previous
|
|
1160
|
-
// recording's `_shared/` modules — detectCandidatesCompletedSteps gates
|
|
1161
|
-
// on the recording's sessionPath.
|
|
1162
|
-
completedSteps: detectCandidatesCompletedSteps(
|
|
1163
|
-
state.workflows[candidate.toolName],
|
|
1164
|
-
baseState.sessionPath,
|
|
1165
|
-
),
|
|
1166
|
-
candidate,
|
|
1167
|
-
sharedContext,
|
|
1168
|
-
});
|
|
1169
|
-
return {
|
|
1170
|
-
workflowKey: candidate.toolName,
|
|
1171
|
-
startFrom: 'generate' as Step,
|
|
1172
|
-
candidate,
|
|
1173
|
-
sharedContext,
|
|
1174
|
-
};
|
|
1175
|
-
});
|
|
1176
|
-
|
|
1177
|
-
if (pendingKey && state.workflows[pendingKey]) {
|
|
1178
|
-
delete state.workflows[pendingKey];
|
|
1179
|
-
saveTeachState(site, state);
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
1207
|
return {
|
|
1183
1208
|
triageResult: localTriageResult,
|
|
1184
|
-
|
|
1209
|
+
detection,
|
|
1210
|
+
preliminarySelected: selected,
|
|
1211
|
+
baseState,
|
|
1212
|
+
pendingKey,
|
|
1185
1213
|
};
|
|
1186
1214
|
})();
|
|
1187
1215
|
|
|
1188
1216
|
// Wait for candidate chain (includes user interaction)
|
|
1189
1217
|
const candidateResult = await candidatePromise;
|
|
1190
|
-
plans = candidateResult.plans;
|
|
1191
|
-
|
|
1192
1218
|
triageResult = candidateResult.triageResult;
|
|
1193
1219
|
|
|
1194
1220
|
// Wait for replay — may already be done, or show progress while waiting
|
|
@@ -1213,6 +1239,70 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1213
1239
|
|
|
1214
1240
|
mp.clear();
|
|
1215
1241
|
|
|
1242
|
+
if ('plans' in candidateResult) {
|
|
1243
|
+
plans = candidateResult.plans;
|
|
1244
|
+
} else {
|
|
1245
|
+
const finalizedSelection = finalizeTeachCandidateSelection(
|
|
1246
|
+
candidateResult.detection.candidates,
|
|
1247
|
+
candidateResult.preliminarySelected.map((candidate) => candidate.toolName),
|
|
1248
|
+
siteClassifications,
|
|
1249
|
+
);
|
|
1250
|
+
const candidates = finalizedSelection.candidates;
|
|
1251
|
+
const finalDetection = { ...candidateResult.detection, candidates };
|
|
1252
|
+
const notice = formatTeachCandidateAutoAddNotice(finalizedSelection.autoAdded, 'replay');
|
|
1253
|
+
if (notice) p.note(notice.body, notice.title);
|
|
1254
|
+
for (const cycle of finalizedSelection.cycles) {
|
|
1255
|
+
p.log.warn(`Tool dependency cycle detected: ${cycle.join(' → ')}`);
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
const sharedContext = buildCandidateSharedCompileContext(
|
|
1259
|
+
finalDetection,
|
|
1260
|
+
finalizedSelection.selected,
|
|
1261
|
+
);
|
|
1262
|
+
const classificationsHash = classificationsPlanningHash(siteClassifications);
|
|
1263
|
+
const selectedNames = new Set(
|
|
1264
|
+
finalizedSelection.selected.map((candidate) => candidate.toolName),
|
|
1265
|
+
);
|
|
1266
|
+
const selectionChanged = candidateSelectionChanged(
|
|
1267
|
+
state,
|
|
1268
|
+
candidateResult.baseState.sessionPath,
|
|
1269
|
+
finalizedSelection.selected,
|
|
1270
|
+
sharedContext,
|
|
1271
|
+
classificationsHash,
|
|
1272
|
+
);
|
|
1273
|
+
pruneUnselectedCandidateWorkflows(
|
|
1274
|
+
state,
|
|
1275
|
+
candidateResult.baseState.sessionPath,
|
|
1276
|
+
selectedNames,
|
|
1277
|
+
);
|
|
1278
|
+
plans = finalizedSelection.selected.map((candidate) => {
|
|
1279
|
+
checkpoint(site, state, candidate.toolName, {
|
|
1280
|
+
...candidateResult.baseState,
|
|
1281
|
+
// Preserve prior progress only when re-detecting the SAME recording, so
|
|
1282
|
+
// a re-run of the analysis block doesn't regress a tool that already
|
|
1283
|
+
// reached generate…register. A different recording must not inherit the
|
|
1284
|
+
// old plan-prereqs marker or shared-module build.
|
|
1285
|
+
completedSteps: detectCandidatesCompletedSteps(
|
|
1286
|
+
selectionChanged ? undefined : state.workflows[candidate.toolName],
|
|
1287
|
+
candidateResult.baseState.sessionPath,
|
|
1288
|
+
),
|
|
1289
|
+
candidate,
|
|
1290
|
+
sharedContext,
|
|
1291
|
+
classificationsHash,
|
|
1292
|
+
});
|
|
1293
|
+
return {
|
|
1294
|
+
workflowKey: candidate.toolName,
|
|
1295
|
+
startFrom: 'generate' as Step,
|
|
1296
|
+
candidate,
|
|
1297
|
+
sharedContext,
|
|
1298
|
+
};
|
|
1299
|
+
});
|
|
1300
|
+
if (candidateResult.pendingKey && state.workflows[candidateResult.pendingKey]) {
|
|
1301
|
+
delete state.workflows[candidateResult.pendingKey];
|
|
1302
|
+
saveTeachState(site, state);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1216
1306
|
// Checkpoints — write sequentially after both complete
|
|
1217
1307
|
updateCandidateStageCheckpoints({
|
|
1218
1308
|
site,
|
|
@@ -1233,12 +1323,8 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1233
1323
|
} else {
|
|
1234
1324
|
// Resuming from generate or later — load cached data
|
|
1235
1325
|
const classPath = pathJoin(localSiteDir(site), '.classifications.json');
|
|
1236
|
-
if (
|
|
1237
|
-
|
|
1238
|
-
siteClassifications = JSON.parse(readFileSync(classPath, 'utf8')).classifications;
|
|
1239
|
-
} catch {
|
|
1240
|
-
/* proceed without */
|
|
1241
|
-
}
|
|
1326
|
+
if (redactedPath) {
|
|
1327
|
+
siteClassifications = loadCachedClassificationsForSession(classPath, redactedPath);
|
|
1242
1328
|
}
|
|
1243
1329
|
const ws = state.workflows[workflowKey];
|
|
1244
1330
|
const resolvedTriagedPath = resolveWorkflowTriagedPath(site, ws);
|
|
@@ -1252,17 +1338,18 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1252
1338
|
triagedPath: resolvedArtifactCheckpointPath(site, ws.triagedPath, resolvedTriagedPath),
|
|
1253
1339
|
});
|
|
1254
1340
|
}
|
|
1255
|
-
// A
|
|
1256
|
-
//
|
|
1257
|
-
//
|
|
1258
|
-
//
|
|
1341
|
+
// A candidate-backed resume reconstructs the prior run's tools from persisted
|
|
1342
|
+
// state (shared-module planning may need ≥2 tools). This applies both to an
|
|
1343
|
+
// explicit `--from-step` and to the interactive Continue/Redo paths.
|
|
1344
|
+
// Resume defaults to the full persisted set. --primary-tool narrows to the
|
|
1345
|
+
// primary candidate plus its upstream dependency closure.
|
|
1259
1346
|
// selectMultiToolResumePlans scopes reconstruction to the same recording as the
|
|
1260
1347
|
// resume target and to tools that actually reached `startFrom`'s prerequisites,
|
|
1261
1348
|
// so a sibling from a different run can't be compiled against the wrong session
|
|
1262
|
-
// and one that failed earlier can't crash loading a missing artifact.
|
|
1263
|
-
|
|
1349
|
+
// and one that failed earlier can't crash loading a missing artifact.
|
|
1350
|
+
candidateBackedResume = isCandidateBackedResume(ws);
|
|
1264
1351
|
const allCandidatePlans: CandidateCompilePlan[] = [];
|
|
1265
|
-
if (
|
|
1352
|
+
if (candidateBackedResume) {
|
|
1266
1353
|
let plannedToolNames: Set<string> | undefined;
|
|
1267
1354
|
const persistedBuildPlanPath = resolveTeachStatePath(site, ws?.buildPlanPath);
|
|
1268
1355
|
if (persistedBuildPlanPath && existsSync(persistedBuildPlanPath)) {
|
|
@@ -1272,20 +1359,22 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1272
1359
|
}
|
|
1273
1360
|
}
|
|
1274
1361
|
const selection = selectMultiToolResumePlans(state, workflowKey, startFrom, {
|
|
1275
|
-
|
|
1276
|
-
allTools: opts.allTools,
|
|
1362
|
+
primaryTool: opts.primaryTool,
|
|
1277
1363
|
toolName: opts.tool,
|
|
1278
1364
|
plannedToolNames,
|
|
1279
1365
|
});
|
|
1280
1366
|
for (const { workflowKey: skippedKey, reason } of selection.skipped) {
|
|
1281
1367
|
p.log.warn(
|
|
1282
1368
|
reason === 'different-recording'
|
|
1283
|
-
? `Skipping tool "${skippedKey}"
|
|
1369
|
+
? `Skipping tool "${skippedKey}" while resuming from ${startFrom}: it belongs to a different recording than the resume target.`
|
|
1284
1370
|
: reason === 'not-in-build-plan'
|
|
1285
|
-
? `Skipping tool "${skippedKey}"
|
|
1286
|
-
: `Skipping tool "${skippedKey}"
|
|
1371
|
+
? `Skipping tool "${skippedKey}" while resuming from ${startFrom}: it is not in the current build plan.`
|
|
1372
|
+
: `Skipping tool "${skippedKey}" while resuming from ${startFrom}: its prior run didn't reach "${startFrom}" — resume an earlier step or re-run it.`,
|
|
1287
1373
|
);
|
|
1288
1374
|
}
|
|
1375
|
+
for (const cycle of selection.cycles) {
|
|
1376
|
+
p.log.warn(`Tool dependency cycle detected: ${cycle.join(' → ')}`);
|
|
1377
|
+
}
|
|
1289
1378
|
for (const sel of selection.plans) {
|
|
1290
1379
|
allCandidatePlans.push({
|
|
1291
1380
|
workflowKey: sel.workflowKey,
|
|
@@ -1295,10 +1384,14 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1295
1384
|
});
|
|
1296
1385
|
}
|
|
1297
1386
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1387
|
+
if (candidateBackedResume && allCandidatePlans.length === 0) {
|
|
1388
|
+
throw new Error(
|
|
1389
|
+
`Cannot resume from "${startFrom}": the selected recording has no resumable candidates. Re-run from "detect-candidates".`,
|
|
1390
|
+
);
|
|
1391
|
+
}
|
|
1392
|
+
plans = candidateBackedResume
|
|
1393
|
+
? allCandidatePlans
|
|
1394
|
+
: [{ workflowKey, startFrom, candidate: ws?.candidate, sharedContext: ws?.sharedContext }];
|
|
1302
1395
|
}
|
|
1303
1396
|
|
|
1304
1397
|
// Early stop: `--to-step replay-and-diff|triage|detect-candidates` finishes
|
|
@@ -1367,16 +1460,18 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1367
1460
|
});
|
|
1368
1461
|
|
|
1369
1462
|
// ── Clean up stale tools from previous teach runs ──
|
|
1370
|
-
// Skipped
|
|
1463
|
+
// Skipped on every resume, including interactive Continue/Redo: a resume is scoped to a subset of
|
|
1371
1464
|
// the site's tools (selectMultiToolResumePlans intentionally leaves other
|
|
1372
1465
|
// recordings' tools — and same-recording tools that didn't reach the step —
|
|
1373
1466
|
// alone), so "not in the resume set" does NOT mean "stale". Treating it as stale
|
|
1374
1467
|
// here would silently rmSync a tool the resume just promised to preserve. Cleanup
|
|
1375
1468
|
// only applies to a fresh run that produces a superseding tool set.
|
|
1376
1469
|
const incomingToolNames = new Set(plans.map((pl) => pl.candidate?.toolName ?? pl.workflowKey));
|
|
1377
|
-
const staleTools =
|
|
1378
|
-
|
|
1379
|
-
|
|
1470
|
+
const staleTools = staleCompletedToolsForRun(
|
|
1471
|
+
discoverCompletedWorkflows(site),
|
|
1472
|
+
incomingToolNames,
|
|
1473
|
+
runPolicy.cleanupStaleTools,
|
|
1474
|
+
);
|
|
1380
1475
|
if (staleTools.length > 0) {
|
|
1381
1476
|
let shouldReplace = true;
|
|
1382
1477
|
if (!opts.noInteractive) {
|
|
@@ -1417,7 +1512,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1417
1512
|
// one tool; otherwise its assigned shared modules and producer-token contract
|
|
1418
1513
|
// silently disappear from the revision agent's context.
|
|
1419
1514
|
const alreadyPlanned =
|
|
1420
|
-
|
|
1515
|
+
runPolicy.reuseExistingBuildPlan &&
|
|
1421
1516
|
plans.every((pl) => state.workflows[pl.workflowKey]?.completedSteps.includes('plan-prereqs')) &&
|
|
1422
1517
|
existsSync(sidecar);
|
|
1423
1518
|
if (willGenerate && compileModel && alreadyPlanned && firstWs) {
|
|
@@ -1721,10 +1816,9 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1721
1816
|
sharedTriageResult: triageResult,
|
|
1722
1817
|
siteClassifications,
|
|
1723
1818
|
teachCredentials,
|
|
1724
|
-
allTools: opts.allTools,
|
|
1725
1819
|
buildPlanPath: buildPlanPath || undefined,
|
|
1726
1820
|
sharedModules: sharedModulesManifest.length > 0 ? sharedModulesManifest : undefined,
|
|
1727
|
-
resumeExistingArtifacts:
|
|
1821
|
+
resumeExistingArtifacts: runPolicy.reviseExistingArtifacts,
|
|
1728
1822
|
});
|
|
1729
1823
|
} finally {
|
|
1730
1824
|
unmuteLog();
|
|
@@ -1754,7 +1848,7 @@ export async function teach(opts: TeachOptions): Promise<TeachResult> {
|
|
|
1754
1848
|
}
|
|
1755
1849
|
}
|
|
1756
1850
|
|
|
1757
|
-
const primaryResult = results
|
|
1851
|
+
const primaryResult = selectPrimaryNamedResult(results, plans) as TeachToolResult;
|
|
1758
1852
|
|
|
1759
1853
|
// ── 6. Platform integration ────────────────────────────────────────
|
|
1760
1854
|
if (inWindow('register')) {
|
|
@@ -1913,47 +2007,133 @@ async function detectTeachCandidates(opts: {
|
|
|
1913
2007
|
);
|
|
1914
2008
|
}
|
|
1915
2009
|
|
|
1916
|
-
async function selectTeachCandidates(
|
|
2010
|
+
export async function selectTeachCandidates(
|
|
1917
2011
|
detection: Awaited<ReturnType<typeof detectToolCandidates>>,
|
|
1918
2012
|
opts: TeachOptions,
|
|
1919
2013
|
): Promise<ToolCandidate[]> {
|
|
1920
2014
|
if (detection.candidates.length === 1) return [detection.candidates[0] as ToolCandidate];
|
|
1921
2015
|
|
|
1922
|
-
if (opts.
|
|
1923
|
-
if (opts.allTools) return detection.candidates;
|
|
2016
|
+
if (opts.primaryTool) {
|
|
1924
2017
|
const primary = primaryToolCandidate(detection);
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
2018
|
+
const closure = finalizeTeachCandidateSelection(detection.candidates, [primary.toolName]);
|
|
2019
|
+
for (const cycle of closure.cycles) {
|
|
2020
|
+
p.log.warn(`Tool dependency cycle detected: ${cycle.join(' → ')}`);
|
|
2021
|
+
}
|
|
2022
|
+
return closure.selected;
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
if (opts.noInteractive) {
|
|
2026
|
+
return detection.candidates;
|
|
1929
2027
|
}
|
|
1930
2028
|
|
|
2029
|
+
const picker = buildTeachCandidatePicker(detection.candidates);
|
|
1931
2030
|
const answer = await p.multiselect({
|
|
1932
2031
|
message:
|
|
1933
2032
|
'Which tools should Imprint compile from this recording?\n (press [space] to toggle, [enter] to submit)',
|
|
1934
2033
|
required: true,
|
|
1935
|
-
initialValues:
|
|
1936
|
-
|
|
1937
|
-
.map((c) => c.toolName),
|
|
1938
|
-
options: detection.candidates.map((candidate) => ({
|
|
1939
|
-
value: candidate.toolName,
|
|
1940
|
-
label: `${candidate.toolName}${candidate.primary ? ' (primary)' : ''}`,
|
|
1941
|
-
hint: `${Math.round(candidate.confidence * 100)}% — ${candidate.description}`,
|
|
1942
|
-
})),
|
|
2034
|
+
initialValues: picker.initialValues,
|
|
2035
|
+
options: picker.options,
|
|
1943
2036
|
});
|
|
1944
2037
|
if (p.isCancel(answer)) {
|
|
1945
2038
|
p.outro('Cancelled.');
|
|
1946
2039
|
process.exit(0);
|
|
1947
2040
|
}
|
|
1948
2041
|
|
|
1949
|
-
const
|
|
1950
|
-
|
|
1951
|
-
selectedNames.has(candidate.toolName),
|
|
1952
|
-
);
|
|
1953
|
-
if (selected.length === 0) {
|
|
2042
|
+
const closure = finalizeTeachCandidateSelection(detection.candidates, answer as string[]);
|
|
2043
|
+
if (closure.selected.length === 0) {
|
|
1954
2044
|
throw new Error('At least one tool candidate must be selected.');
|
|
1955
2045
|
}
|
|
1956
|
-
|
|
2046
|
+
const notice = formatTeachCandidateAutoAddNotice(closure.autoAdded, 'structural');
|
|
2047
|
+
if (notice) p.note(notice.body, notice.title);
|
|
2048
|
+
for (const cycle of closure.cycles) {
|
|
2049
|
+
p.log.warn(`Tool dependency cycle detected: ${cycle.join(' → ')}`);
|
|
2050
|
+
}
|
|
2051
|
+
return closure.selected;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
export function buildTeachCandidatePicker(candidates: ToolCandidate[]): {
|
|
2055
|
+
initialValues: string[];
|
|
2056
|
+
options: Array<{ value: string; label: string; hint: string }>;
|
|
2057
|
+
} {
|
|
2058
|
+
return {
|
|
2059
|
+
initialValues: candidates.map((candidate) => candidate.toolName),
|
|
2060
|
+
options: candidates.map((candidate) => ({
|
|
2061
|
+
value: candidate.toolName,
|
|
2062
|
+
label: `${candidate.toolName}${candidate.primary ? ' (primary)' : ''}`,
|
|
2063
|
+
hint: [
|
|
2064
|
+
`${Math.round(candidate.confidence * 100)}% — ${candidate.description}`,
|
|
2065
|
+
candidate.dependsOnTools.length > 0
|
|
2066
|
+
? `requires ${candidate.dependsOnTools.join(', ')}`
|
|
2067
|
+
: undefined,
|
|
2068
|
+
]
|
|
2069
|
+
.filter(Boolean)
|
|
2070
|
+
.join(' — '),
|
|
2071
|
+
})),
|
|
2072
|
+
};
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
/** Merge replay-grounded producer edges using the persisted ClassifiedValue
|
|
2076
|
+
* shape. The token deriver deliberately accepts a value-only projection so raw
|
|
2077
|
+
* recorded values stay out of planner payloads; adapt value1 at this boundary. */
|
|
2078
|
+
export function mergeReplayCandidateDependencies(
|
|
2079
|
+
candidates: ToolCandidate[],
|
|
2080
|
+
siteClassifications?: ClassifiedValue[],
|
|
2081
|
+
): ToolCandidate[] {
|
|
2082
|
+
const tokenHints = siteClassifications
|
|
2083
|
+
? deriveTokenContractHints({
|
|
2084
|
+
selectedTools: candidates,
|
|
2085
|
+
ephemeralValues: siteClassifications.map((classification) => ({
|
|
2086
|
+
classification: classification.classification,
|
|
2087
|
+
originalSeq: classification.originalSeq,
|
|
2088
|
+
location: classification.location,
|
|
2089
|
+
producerSeq: classification.producerSeq,
|
|
2090
|
+
producerPath: classification.producerPath,
|
|
2091
|
+
value: classification.value1,
|
|
2092
|
+
})),
|
|
2093
|
+
})
|
|
2094
|
+
: [];
|
|
2095
|
+
return mergeCandidateDependencies(
|
|
2096
|
+
candidates,
|
|
2097
|
+
tokenHints.map((hint) => ({
|
|
2098
|
+
consumerTool: hint.consumerTool,
|
|
2099
|
+
producerTool: hint.producerTool,
|
|
2100
|
+
})),
|
|
2101
|
+
);
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
/** Pure boundary between detected candidates, optional replay evidence, and
|
|
2105
|
+
* the exact dependency-closed set handed to checkpointing/build planning. */
|
|
2106
|
+
export function finalizeTeachCandidateSelection(
|
|
2107
|
+
candidates: ToolCandidate[],
|
|
2108
|
+
selectedToolNames: Iterable<string>,
|
|
2109
|
+
siteClassifications?: ClassifiedValue[],
|
|
2110
|
+
): ReturnType<typeof closeCandidateSelection> & { candidates: ToolCandidate[] } {
|
|
2111
|
+
const mergedCandidates = mergeReplayCandidateDependencies(candidates, siteClassifications);
|
|
2112
|
+
return {
|
|
2113
|
+
candidates: mergedCandidates,
|
|
2114
|
+
...closeCandidateSelection(mergedCandidates, selectedToolNames),
|
|
2115
|
+
};
|
|
2116
|
+
}
|
|
2117
|
+
|
|
2118
|
+
export function formatTeachCandidateAutoAddNotice(
|
|
2119
|
+
autoAdded: ToolCandidate[],
|
|
2120
|
+
source: 'structural' | 'replay',
|
|
2121
|
+
): { body: string; title: string } | undefined {
|
|
2122
|
+
if (autoAdded.length === 0) return undefined;
|
|
2123
|
+
return {
|
|
2124
|
+
body: autoAdded.map((candidate) => ` + ${candidate.toolName}`).join('\n'),
|
|
2125
|
+
title: source === 'replay' ? 'Added recorded prerequisites' : 'Added required upstream tools',
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
/** Select the compiled result corresponding to the detected primary candidate.
|
|
2130
|
+
* Dependency closure preserves detection order, which need not put primary first. */
|
|
2131
|
+
export function selectPrimaryNamedResult<T extends { workflow: { toolName: string } }>(
|
|
2132
|
+
results: readonly T[],
|
|
2133
|
+
plans: ReadonlyArray<{ candidate?: { toolName: string; primary: boolean } }>,
|
|
2134
|
+
): T | undefined {
|
|
2135
|
+
const primaryToolName = plans.find((plan) => plan.candidate?.primary)?.candidate?.toolName;
|
|
2136
|
+
return results.find((result) => result.workflow.toolName === primaryToolName) ?? results[0];
|
|
1957
2137
|
}
|
|
1958
2138
|
|
|
1959
2139
|
async function compileCandidatePlans(opts: {
|
|
@@ -1969,10 +2149,6 @@ async function compileCandidatePlans(opts: {
|
|
|
1969
2149
|
sharedTriageResult?: TriageResult;
|
|
1970
2150
|
siteClassifications?: ClassifiedValue[];
|
|
1971
2151
|
teachCredentials?: { site: string; values: Record<string, string> };
|
|
1972
|
-
/** Mirror of TeachOptions.allTools — when true, partial failures abort
|
|
1973
|
-
* the run with a non-zero exit so the user notices missing tools instead
|
|
1974
|
-
* of getting a silent warning. */
|
|
1975
|
-
allTools?: boolean;
|
|
1976
2152
|
/** Absolute path to the multi-tool build plan sidecar (.build-plan.json). */
|
|
1977
2153
|
buildPlanPath?: string;
|
|
1978
2154
|
/** Shared-module build manifest (verified flags) for this site. */
|
|
@@ -2142,18 +2318,20 @@ async function compileCandidatePlans(opts: {
|
|
|
2142
2318
|
if (first) p.log.warn(`${first.name}: ${first.firstLineError}`);
|
|
2143
2319
|
}
|
|
2144
2320
|
|
|
2145
|
-
|
|
2146
|
-
// partial compiles ship MCP servers with missing tools; the user only
|
|
2147
|
-
// notices later when an LLM tries to call one that doesn't exist.
|
|
2148
|
-
if (opts.allTools && summary.failures.length > 0) {
|
|
2149
|
-
throw new Error(
|
|
2150
|
-
`--all-tools requested but ${summary.failures.length} of ${opts.plans.length} tools failed to compile. See the summary above; re-run \`imprint teach\` after addressing the failures (or omit --all-tools to ship only what compiled).`,
|
|
2151
|
-
);
|
|
2152
|
-
}
|
|
2321
|
+
assertCompleteSelectedToolSet(summary.failures.length, opts.plans.length);
|
|
2153
2322
|
|
|
2154
2323
|
return summary.successes;
|
|
2155
2324
|
}
|
|
2156
2325
|
|
|
2326
|
+
/** Enforce that compilation never ships a partial selected set. A missing
|
|
2327
|
+
* producer can make every otherwise-successful consumer unusable. */
|
|
2328
|
+
export function assertCompleteSelectedToolSet(failureCount: number, selectedCount: number): void {
|
|
2329
|
+
if (failureCount === 0) return;
|
|
2330
|
+
throw new Error(
|
|
2331
|
+
`Selected tool set is incomplete: ${failureCount} of ${selectedCount} tools failed to compile. See the summary above and re-run \`imprint teach\` after addressing the failures.`,
|
|
2332
|
+
);
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2157
2335
|
/** Pure summarizer — extracted so unit tests can drive arbitrary outcome
|
|
2158
2336
|
* shapes without spinning up real compile pipelines. */
|
|
2159
2337
|
interface CompileOutcomeSummary {
|
|
@@ -2488,7 +2666,10 @@ async function siteReplayAndDiff(
|
|
|
2488
2666
|
};
|
|
2489
2667
|
|
|
2490
2668
|
const classPath = pathJoin(localSiteDir(site), '.classifications.json');
|
|
2491
|
-
writeFileSync(
|
|
2669
|
+
writeFileSync(
|
|
2670
|
+
classPath,
|
|
2671
|
+
JSON.stringify({ ...diffResult, sourceSessionHash: fileSha256(sessionPath) }, null, 2),
|
|
2672
|
+
);
|
|
2492
2673
|
|
|
2493
2674
|
mp.clear();
|
|
2494
2675
|
mp.remove('replay');
|