@eldrforge/kodrdriv 1.2.125 → 1.2.127

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.
@@ -5,7 +5,7 @@ import { DEFAULT_TO_COMMIT_ALIAS, DEFAULT_MAX_DIFF_BYTES, DEFAULT_EXCLUDED_PATTE
5
5
  import { getCurrentBranch, getDefaultFromRef, safeJsonParse } from '@eldrforge/git-tools';
6
6
  import { create } from '../content/log.js';
7
7
  import { create as create$1, truncateDiffByFiles } from '../content/diff.js';
8
- import { createReleasePrompt, createCompletionWithRetry, requireTTY, getUserChoice, STANDARD_CHOICES, getLLMFeedbackInEditor, editContentInEditor } from '@eldrforge/ai-service';
8
+ import { runAgenticRelease, requireTTY, createReleasePrompt, createCompletionWithRetry, getUserChoice, STANDARD_CHOICES, getLLMFeedbackInEditor, editContentInEditor } from '@eldrforge/ai-service';
9
9
  import { improveContentWithLLM } from '../util/interactive.js';
10
10
  import { toAIConfig } from '../util/aiAdapter.js';
11
11
  import { createStorageAdapter } from '../util/storageAdapter.js';
@@ -88,6 +88,189 @@ Please revise the release notes according to the user's feedback while maintaini
88
88
  };
89
89
  return await improveContentWithLLM(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, improvementConfig);
90
90
  }
91
+ // Helper function to generate self-reflection output for release notes
92
+ async function generateSelfReflection(agenticResult, outputDirectory, storage, logger) {
93
+ try {
94
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('.')[0];
95
+ const reflectionPath = getOutputPath(outputDirectory, `agentic-reflection-release-${timestamp}.md`);
96
+ // Calculate tool effectiveness metrics
97
+ const toolMetrics = agenticResult.toolMetrics || [];
98
+ const toolStats = new Map();
99
+ for (const metric of toolMetrics){
100
+ if (!toolStats.has(metric.name)) {
101
+ toolStats.set(metric.name, {
102
+ total: 0,
103
+ success: 0,
104
+ failures: 0,
105
+ totalDuration: 0
106
+ });
107
+ }
108
+ const stats = toolStats.get(metric.name);
109
+ stats.total++;
110
+ stats.totalDuration += metric.duration;
111
+ if (metric.success) {
112
+ stats.success++;
113
+ } else {
114
+ stats.failures++;
115
+ }
116
+ }
117
+ // Build reflection document
118
+ const sections = [];
119
+ sections.push('# Agentic Release Notes - Self-Reflection Report');
120
+ sections.push('');
121
+ sections.push(`Generated: ${new Date().toISOString()}`);
122
+ sections.push('');
123
+ sections.push('## Execution Summary');
124
+ sections.push('');
125
+ sections.push(`- **Iterations**: ${agenticResult.iterations}`);
126
+ sections.push(`- **Tool Calls**: ${agenticResult.toolCallsExecuted}`);
127
+ sections.push(`- **Unique Tools Used**: ${toolStats.size}`);
128
+ sections.push('');
129
+ sections.push('## Tool Effectiveness Analysis');
130
+ sections.push('');
131
+ if (toolStats.size === 0) {
132
+ sections.push('*No tools were called during execution.*');
133
+ sections.push('');
134
+ } else {
135
+ sections.push('| Tool | Calls | Success Rate | Avg Duration | Total Time |');
136
+ sections.push('|------|-------|--------------|--------------|------------|');
137
+ const sortedTools = Array.from(toolStats.entries()).sort((a, b)=>b[1].total - a[1].total);
138
+ for (const [toolName, stats] of sortedTools){
139
+ const successRate = (stats.success / stats.total * 100).toFixed(1);
140
+ const avgDuration = (stats.totalDuration / stats.total).toFixed(0);
141
+ const totalTime = stats.totalDuration.toFixed(0);
142
+ sections.push(`| ${toolName} | ${stats.total} | ${successRate}% | ${avgDuration}ms | ${totalTime}ms |`);
143
+ }
144
+ sections.push('');
145
+ }
146
+ // Tool usage insights
147
+ sections.push('## Tool Usage Insights');
148
+ sections.push('');
149
+ if (toolStats.size > 0) {
150
+ const mostUsedTool = Array.from(toolStats.entries()).sort((a, b)=>b[1].total - a[1].total)[0];
151
+ sections.push(`- **Most Used Tool**: \`${mostUsedTool[0]}\` (${mostUsedTool[1].total} calls)`);
152
+ const slowestTool = Array.from(toolStats.entries()).sort((a, b)=>b[1].totalDuration / b[1].total - a[1].totalDuration / a[1].total)[0];
153
+ const slowestAvg = (slowestTool[1].totalDuration / slowestTool[1].total).toFixed(0);
154
+ sections.push(`- **Slowest Tool**: \`${slowestTool[0]}\` (${slowestAvg}ms average)`);
155
+ const failedTools = Array.from(toolStats.entries()).filter(([_, stats])=>stats.failures > 0);
156
+ if (failedTools.length > 0) {
157
+ sections.push(`- **Tools with Failures**: ${failedTools.length} tool(s) had at least one failure`);
158
+ for (const [toolName, stats] of failedTools){
159
+ sections.push(` - \`${toolName}\`: ${stats.failures}/${stats.total} calls failed`);
160
+ }
161
+ } else {
162
+ sections.push('- **Reliability**: All tool calls succeeded ✓');
163
+ }
164
+ }
165
+ sections.push('');
166
+ // Execution patterns
167
+ sections.push('## Execution Patterns');
168
+ sections.push('');
169
+ const iterationsPerToolCall = agenticResult.toolCallsExecuted > 0 ? (agenticResult.iterations / agenticResult.toolCallsExecuted).toFixed(2) : 'N/A';
170
+ sections.push(`- **Iterations per Tool Call**: ${iterationsPerToolCall}`);
171
+ const totalExecutionTime = Array.from(toolStats.values()).reduce((sum, stats)=>sum + stats.totalDuration, 0);
172
+ sections.push(`- **Total Tool Execution Time**: ${totalExecutionTime.toFixed(0)}ms`);
173
+ if (agenticResult.toolCallsExecuted > 0) {
174
+ const avgTimePerCall = (totalExecutionTime / agenticResult.toolCallsExecuted).toFixed(0);
175
+ sections.push(`- **Average Time per Tool Call**: ${avgTimePerCall}ms`);
176
+ }
177
+ sections.push('');
178
+ // Recommendations
179
+ sections.push('## Recommendations');
180
+ sections.push('');
181
+ const recommendations = [];
182
+ const failedTools = Array.from(toolStats.entries()).filter(([_, stats])=>stats.failures > 0);
183
+ if (failedTools.length > 0) {
184
+ recommendations.push('- **Tool Reliability**: Some tools failed during execution. Review error messages and consider improving error handling or tool implementation.');
185
+ }
186
+ const slowTools = Array.from(toolStats.entries()).filter(([_, stats])=>stats.totalDuration / stats.total > 1000);
187
+ if (slowTools.length > 0) {
188
+ recommendations.push('- **Performance**: Consider optimizing slow tools or caching results to improve execution speed.');
189
+ }
190
+ if (agenticResult.iterations >= (agenticResult.maxIterations || 30)) {
191
+ recommendations.push('- **Max Iterations Reached**: The agent reached maximum iterations. Consider increasing the limit or improving tool efficiency to allow the agent to complete naturally.');
192
+ }
193
+ const underutilizedTools = Array.from(toolStats.entries()).filter(([_, stats])=>stats.total === 1);
194
+ if (underutilizedTools.length > 3) {
195
+ recommendations.push('- **Underutilized Tools**: Many tools were called only once. Consider whether all tools are necessary or if the agent needs better guidance on when to use them.');
196
+ }
197
+ if (agenticResult.toolCallsExecuted === 0) {
198
+ recommendations.push('- **No Tools Used**: The agent completed without calling any tools. This might indicate the initial prompt provided sufficient information, or the agent may benefit from more explicit guidance to use tools.');
199
+ }
200
+ if (recommendations.length === 0) {
201
+ sections.push('*No specific recommendations at this time. Execution appears optimal.*');
202
+ } else {
203
+ for (const rec of recommendations){
204
+ sections.push(rec);
205
+ }
206
+ }
207
+ sections.push('');
208
+ // Add detailed execution timeline
209
+ sections.push('## Detailed Execution Timeline');
210
+ sections.push('');
211
+ if (toolMetrics.length === 0) {
212
+ sections.push('*No tool execution timeline available.*');
213
+ } else {
214
+ sections.push('| Time | Iteration | Tool | Result | Duration |');
215
+ sections.push('|------|-----------|------|--------|----------|');
216
+ for (const metric of toolMetrics){
217
+ const time = new Date(metric.timestamp).toLocaleTimeString();
218
+ const result = metric.success ? '✅ Success' : `❌ ${metric.error || 'Failed'}`;
219
+ sections.push(`| ${time} | ${metric.iteration} | ${metric.name} | ${result} | ${metric.duration}ms |`);
220
+ }
221
+ sections.push('');
222
+ }
223
+ // Add conversation history
224
+ sections.push('## Conversation History');
225
+ sections.push('');
226
+ sections.push('<details>');
227
+ sections.push('<summary>Click to expand full agentic interaction</summary>');
228
+ sections.push('');
229
+ sections.push('```json');
230
+ sections.push(JSON.stringify(agenticResult.conversationHistory, null, 2));
231
+ sections.push('```');
232
+ sections.push('');
233
+ sections.push('</details>');
234
+ sections.push('');
235
+ // Add generated release notes
236
+ sections.push('## Generated Release Notes');
237
+ sections.push('');
238
+ sections.push('### Title');
239
+ sections.push('```');
240
+ sections.push(agenticResult.releaseNotes.title);
241
+ sections.push('```');
242
+ sections.push('');
243
+ sections.push('### Body');
244
+ sections.push('```markdown');
245
+ sections.push(agenticResult.releaseNotes.body);
246
+ sections.push('```');
247
+ sections.push('');
248
+ // Write the reflection file
249
+ const reflectionContent = sections.join('\n');
250
+ await storage.writeFile(reflectionPath, reflectionContent, 'utf-8');
251
+ logger.info('');
252
+ logger.info('═'.repeat(80));
253
+ logger.info('📊 SELF-REFLECTION REPORT GENERATED');
254
+ logger.info('═'.repeat(80));
255
+ logger.info('');
256
+ logger.info('📁 Location: %s', reflectionPath);
257
+ logger.info('');
258
+ logger.info('📈 Report Summary:');
259
+ logger.info(' • %d iterations completed', agenticResult.iterations);
260
+ logger.info(' • %d tool calls executed', agenticResult.toolCallsExecuted);
261
+ logger.info(' • %d unique tools used', toolStats.size);
262
+ logger.info('');
263
+ logger.info('💡 Use this report to:');
264
+ logger.info(' • Understand which tools were most effective');
265
+ logger.info(' • Identify performance bottlenecks');
266
+ logger.info(' • Optimize tool selection and usage patterns');
267
+ logger.info(' • Improve agentic release notes generation');
268
+ logger.info('');
269
+ logger.info('═'.repeat(80));
270
+ } catch (error) {
271
+ logger.warn('Failed to generate self-reflection report: %s', error.message);
272
+ }
273
+ }
91
274
  // Interactive feedback loop for release notes
92
275
  async function handleInteractiveReleaseFeedback(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, storage, logContent, diffContent) {
93
276
  const logger = getDryRunLogger(false);
@@ -143,7 +326,7 @@ async function handleInteractiveReleaseFeedback(releaseSummary, runConfig, promp
143
326
  }
144
327
  }
145
328
  const execute = async (runConfig)=>{
146
- var _runConfig_release, _runConfig_release1, _runConfig_release2, _runConfig_release3, _runConfig_release4, _runConfig_release5, _runConfig_release6, _runConfig_release7, _runConfig_release8, _aiConfig_commands_release, _aiConfig_commands, _aiConfig_commands_release1, _aiConfig_commands1, _runConfig_release9;
329
+ var _runConfig_release, _runConfig_release1, _runConfig_release2, _runConfig_release3, _runConfig_release4, _runConfig_release5, _runConfig_release6, _runConfig_release7, _runConfig_release8, _runConfig_release9, _aiConfig_commands_release, _aiConfig_commands, _aiConfig_commands_release1, _aiConfig_commands1, _runConfig_release10;
147
330
  const isDryRun = runConfig.dryRun || false;
148
331
  const logger = getDryRunLogger(isDryRun);
149
332
  // Get current branch to help determine best tag comparison
@@ -234,14 +417,87 @@ const execute = async (runConfig)=>{
234
417
  const aiConfig = toAIConfig(runConfig);
235
418
  const aiStorageAdapter = createStorageAdapter();
236
419
  const aiLogger = createLoggerAdapter(isDryRun);
420
+ // Always ensure output directory exists for request/response files
421
+ const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
422
+ const storage = createStorage();
423
+ await storage.ensureDirectory(outputDirectory);
424
+ // Check if agentic mode is enabled
425
+ if ((_runConfig_release7 = runConfig.release) === null || _runConfig_release7 === void 0 ? void 0 : _runConfig_release7.agentic) {
426
+ var _runConfig_release11, _runConfig_release12, _aiConfig_commands_release2, _aiConfig_commands2, _runConfig_release13, _aiConfig_commands_release3, _aiConfig_commands3, _runConfig_release14, _runConfig_release15;
427
+ logger.info('🤖 Using agentic mode for release notes generation');
428
+ // Run agentic release notes generation
429
+ const agenticResult = await runAgenticRelease({
430
+ fromRef,
431
+ toRef,
432
+ logContent,
433
+ diffContent,
434
+ milestoneIssues: milestoneIssuesContent,
435
+ releaseFocus: (_runConfig_release11 = runConfig.release) === null || _runConfig_release11 === void 0 ? void 0 : _runConfig_release11.focus,
436
+ userContext: (_runConfig_release12 = runConfig.release) === null || _runConfig_release12 === void 0 ? void 0 : _runConfig_release12.context,
437
+ model: ((_aiConfig_commands2 = aiConfig.commands) === null || _aiConfig_commands2 === void 0 ? void 0 : (_aiConfig_commands_release2 = _aiConfig_commands2.release) === null || _aiConfig_commands_release2 === void 0 ? void 0 : _aiConfig_commands_release2.model) || aiConfig.model || 'gpt-4o',
438
+ maxIterations: ((_runConfig_release13 = runConfig.release) === null || _runConfig_release13 === void 0 ? void 0 : _runConfig_release13.maxAgenticIterations) || 30,
439
+ debug: runConfig.debug,
440
+ debugRequestFile: getOutputPath(outputDirectory, getTimestampedRequestFilename('release-agentic')),
441
+ debugResponseFile: getOutputPath(outputDirectory, getTimestampedResponseFilename('release-agentic')),
442
+ storage: aiStorageAdapter,
443
+ logger: aiLogger,
444
+ openaiReasoning: ((_aiConfig_commands3 = aiConfig.commands) === null || _aiConfig_commands3 === void 0 ? void 0 : (_aiConfig_commands_release3 = _aiConfig_commands3.release) === null || _aiConfig_commands_release3 === void 0 ? void 0 : _aiConfig_commands_release3.reasoning) || aiConfig.reasoning
445
+ });
446
+ logger.info('🔍 Agentic analysis complete: %d iterations, %d tool calls', agenticResult.iterations, agenticResult.toolCallsExecuted);
447
+ // Generate self-reflection output if enabled
448
+ if ((_runConfig_release14 = runConfig.release) === null || _runConfig_release14 === void 0 ? void 0 : _runConfig_release14.selfReflection) {
449
+ await generateSelfReflection(agenticResult, outputDirectory, storage, logger);
450
+ }
451
+ // Apply stop-context filtering to release notes
452
+ const titleFilterResult = filterContent(agenticResult.releaseNotes.title, runConfig.stopContext);
453
+ const bodyFilterResult = filterContent(agenticResult.releaseNotes.body, runConfig.stopContext);
454
+ let releaseSummary = {
455
+ title: titleFilterResult.filtered,
456
+ body: bodyFilterResult.filtered
457
+ };
458
+ // Handle interactive mode
459
+ if (((_runConfig_release15 = runConfig.release) === null || _runConfig_release15 === void 0 ? void 0 : _runConfig_release15.interactive) && !isDryRun) {
460
+ var _runConfig_release16;
461
+ requireTTY('Interactive mode requires a terminal. Use --dry-run instead.');
462
+ const interactivePromptContext = {
463
+ context: (_runConfig_release16 = runConfig.release) === null || _runConfig_release16 === void 0 ? void 0 : _runConfig_release16.context,
464
+ directories: runConfig.contextDirectories
465
+ };
466
+ const interactiveResult = await handleInteractiveReleaseFeedback(releaseSummary, runConfig, promptConfig, interactivePromptContext, outputDirectory, storage, logContent, diffContent);
467
+ if (interactiveResult.action === 'skip') {
468
+ logger.info('RELEASE_ABORTED: Release notes generation aborted by user | Reason: User choice | Status: cancelled');
469
+ } else {
470
+ logger.info('RELEASE_FINALIZED: Release notes finalized and accepted | Status: ready | Next: Create release or save');
471
+ }
472
+ releaseSummary = interactiveResult.finalSummary;
473
+ }
474
+ // Save timestamped copy of release notes to output directory
475
+ try {
476
+ const timestampedFilename = getTimestampedReleaseNotesFilename();
477
+ const outputPath = getOutputPath(outputDirectory, timestampedFilename);
478
+ // Format the release notes as markdown
479
+ const releaseNotesContent = `# ${releaseSummary.title}\n\n${releaseSummary.body}`;
480
+ await storage.writeFile(outputPath, releaseNotesContent, 'utf-8');
481
+ logger.debug('Saved timestamped release notes: %s', outputPath);
482
+ } catch (error) {
483
+ logger.warn('RELEASE_SAVE_FAILED: Failed to save timestamped release notes | Error: %s | Impact: Notes not persisted to file', error.message);
484
+ }
485
+ if (isDryRun) {
486
+ logger.info('RELEASE_SUMMARY_COMPLETE: Generated release summary successfully | Status: completed');
487
+ logger.info('RELEASE_SUMMARY_TITLE: %s', releaseSummary.title);
488
+ logger.info('RELEASE_SUMMARY_BODY: %s', releaseSummary.body);
489
+ }
490
+ return releaseSummary;
491
+ }
492
+ // Non-agentic mode: use traditional prompt-based approach
237
493
  const promptContent = {
238
494
  logContent,
239
495
  diffContent,
240
- releaseFocus: (_runConfig_release7 = runConfig.release) === null || _runConfig_release7 === void 0 ? void 0 : _runConfig_release7.focus,
496
+ releaseFocus: (_runConfig_release8 = runConfig.release) === null || _runConfig_release8 === void 0 ? void 0 : _runConfig_release8.focus,
241
497
  milestoneIssues: milestoneIssuesContent
242
498
  };
243
499
  const promptContext = {
244
- context: (_runConfig_release8 = runConfig.release) === null || _runConfig_release8 === void 0 ? void 0 : _runConfig_release8.context,
500
+ context: (_runConfig_release9 = runConfig.release) === null || _runConfig_release9 === void 0 ? void 0 : _runConfig_release9.context,
245
501
  directories: runConfig.contextDirectories
246
502
  };
247
503
  const promptResult = await createReleasePrompt(promptConfig, promptContent, promptContext);
@@ -249,10 +505,6 @@ const execute = async (runConfig)=>{
249
505
  const request = Formatter.create({
250
506
  logger
251
507
  }).formatPrompt(modelToUse, promptResult.prompt);
252
- // Always ensure output directory exists for request/response files
253
- const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
254
- const storage = createStorage();
255
- await storage.ensureDirectory(outputDirectory);
256
508
  logger.debug('Release analysis: isLargeRelease=%s, maxTokens=%d', promptResult.isLargeRelease, promptResult.maxTokens);
257
509
  // Create retry callback that reduces diff size on token limit errors
258
510
  const createRetryCallback = (originalDiffContent, originalLogContent)=>async (attempt)=>{
@@ -304,7 +556,7 @@ const execute = async (runConfig)=>{
304
556
  body: bodyFilterResult.filtered
305
557
  };
306
558
  // Handle interactive mode
307
- if (((_runConfig_release9 = runConfig.release) === null || _runConfig_release9 === void 0 ? void 0 : _runConfig_release9.interactive) && !isDryRun) {
559
+ if (((_runConfig_release10 = runConfig.release) === null || _runConfig_release10 === void 0 ? void 0 : _runConfig_release10.interactive) && !isDryRun) {
308
560
  requireTTY('Interactive mode requires a terminal. Use --dry-run instead.');
309
561
  const interactiveResult = await handleInteractiveReleaseFeedback(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, storage, logContent, diffContent);
310
562
  if (interactiveResult.action === 'skip') {