@eldrforge/commands-publish 0.1.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.
package/dist/index.js ADDED
@@ -0,0 +1,2239 @@
1
+ import { getDryRunLogger, findDevelopmentBranch, KODRDRIV_DEFAULTS, DEFAULT_TO_COMMIT_ALIAS, Log, DEFAULT_MAX_DIFF_BYTES, Diff, DEFAULT_EXCLUDED_PATTERNS, DEFAULT_OUTPUT_DIRECTORY, toAIConfig, createStorageAdapter, createLoggerAdapter, getOutputPath, getTimestampedResponseFilename, getTimestampedRequestFilename, filterContent, getTimestampedReleaseNotesFilename, improveContentWithLLM, validateReleaseSummary, runGitWithLock, calculateBranchDependentVersion, checkIfTagExists, confirmVersionInteractively, getLogger } from '@eldrforge/core';
2
+ import { getCurrentBranch, run, localBranchExists, getDefaultFromRef, safeJsonParse, remoteBranchExists, runSecure, runWithDryRunSupport, validatePackageJson, validateGitRef, safeSyncBranchWithRemote, isBranchInSyncWithRemote } from '@eldrforge/git-tools';
3
+ import path from 'path';
4
+ import * as Commit from '@eldrforge/commands-git';
5
+ import { Formatter } from '@riotprompt/riotprompt';
6
+ import 'dotenv/config';
7
+ import { runAgenticRelease, requireTTY, generateReflectionReport, getUserChoice, STANDARD_CHOICES, getLLMFeedbackInEditor, editContentInEditor, createCompletionWithRetry, createReleasePrompt, runAgenticPublish, formatAgenticPublishResult } from '@eldrforge/ai-service';
8
+ import { createStorage, calculateTargetVersion } from '@eldrforge/shared';
9
+ import * as GitHub from '@eldrforge/github-tools';
10
+ import fs from 'fs/promises';
11
+
12
+ /**
13
+ * Create retroactive working branch tags for past releases
14
+ * Scans git history for X.X.X-dev.0 commits and tags them
15
+ */ async function createRetroactiveTags(workingBranch, isDryRun, logger, tagPrefix = 'working/') {
16
+ logger.info('');
17
+ logger.info('DEV_TAG_SCAN_STARTING: Scanning git history for past release points | Purpose: Create retroactive tags | Pattern: X.X.X-dev.0 version bumps');
18
+ logger.info('DEV_TAG_SCAN_PATTERN: Looking for development version bump commits | Version Format: X.X.X-dev.0 | Purpose: Identify release points');
19
+ logger.info('');
20
+ try {
21
+ // Get all commits on working branch with oneline format
22
+ const { stdout } = await run(`git log ${workingBranch} --oneline --all`);
23
+ const commits = stdout.trim().split('\n');
24
+ // Find commits that are version bumps to -dev.0 (these mark release points)
25
+ const devCommits = commits.filter((line)=>{
26
+ // Match patterns like: "4.4.52-dev.0" or "chore: bump version to 4.4.52-dev.0"
27
+ return /\b\d+\.\d+\.\d+-dev\.0\b/.test(line);
28
+ });
29
+ logger.info(`DEV_TAG_COMMITS_FOUND: Found potential development version commits | Count: ${devCommits.length} | Status: Analyzing for tag creation`);
30
+ const tagsCreated = [];
31
+ const tagsSkipped = [];
32
+ for (const commitLine of devCommits){
33
+ const [sha, ...messageParts] = commitLine.split(' ');
34
+ const message = messageParts.join(' ');
35
+ // Extract version from message (e.g., "4.4.52-dev.0" → "4.4.52")
36
+ const versionMatch = message.match(/(\d+\.\d+\.\d+)-dev\.0/);
37
+ if (!versionMatch) continue;
38
+ const releaseVersion = versionMatch[1]; // e.g., "4.4.52"
39
+ const workingTagName = `${tagPrefix}v${releaseVersion}`;
40
+ // Check if tag already exists
41
+ const tagExistsResult = await run(`git tag -l "${workingTagName}"`);
42
+ const tagExists = tagExistsResult.stdout.trim() !== '';
43
+ if (tagExists) {
44
+ tagsSkipped.push(workingTagName);
45
+ logger.verbose(` Skip: ${workingTagName} (already exists)`);
46
+ continue;
47
+ }
48
+ if (!isDryRun) {
49
+ // Tag the commit that represents the dev version bump
50
+ // This is the commit AFTER the release, which marks the starting point
51
+ logger.verbose(` Create: ${workingTagName} at ${sha.substring(0, 7)}`);
52
+ await run(`git tag ${workingTagName} ${sha}`);
53
+ tagsCreated.push(workingTagName);
54
+ } else {
55
+ logger.info(`DEV_TAG_DRY_RUN: Would create retroactive tag | Mode: dry-run | Tag: ${workingTagName} | Commit: ${sha.substring(0, 7)}`);
56
+ tagsCreated.push(workingTagName);
57
+ }
58
+ }
59
+ logger.info('');
60
+ if (tagsCreated.length > 0 && !isDryRun) {
61
+ logger.info(`DEV_TAG_PUSHING: Pushing retroactive tags to remote | Count: ${tagsCreated.length} | Remote: origin | Command: git push origin --tags`);
62
+ await run('git push origin --tags');
63
+ logger.info('');
64
+ logger.info(`DEV_TAG_PUSH_SUCCESS: Successfully created and pushed retroactive tags | Count: ${tagsCreated.length} | Remote: origin | Status: completed`);
65
+ tagsCreated.forEach((tag)=>logger.info(`DEV_TAG_CREATED: Retroactive tag created | Tag: ${tag} | Status: pushed`));
66
+ } else if (tagsCreated.length > 0 && isDryRun) {
67
+ logger.info(`DEV_TAG_DRY_RUN_SUMMARY: Would create and push retroactive tags | Mode: dry-run | Count: ${tagsCreated.length}`);
68
+ tagsCreated.forEach((tag)=>logger.info(`DEV_TAG_DRY_RUN_TAG: Would create tag | Tag: ${tag} | Mode: dry-run`));
69
+ }
70
+ if (tagsSkipped.length > 0) {
71
+ logger.verbose('');
72
+ logger.verbose(`Skipped ${tagsSkipped.length} existing tags:`);
73
+ tagsSkipped.forEach((tag)=>logger.verbose(` - ${tag}`));
74
+ }
75
+ if (tagsCreated.length === 0 && tagsSkipped.length === 0) {
76
+ logger.info('DEV_TAG_NO_COMMITS: No development version commits found in history | Pattern: X.X.X-dev.0 | Status: Nothing to tag | Action: No retroactive tags created');
77
+ }
78
+ logger.info('');
79
+ } catch (error) {
80
+ logger.warn(`DEV_TAG_CREATION_FAILED: Unable to create retroactive tags | Error: ${error.message} | Impact: Past releases not tagged | Alternative: Manual tagging available`);
81
+ logger.warn('DEV_TAG_MANUAL_OPTION: Manual tagging option available | Action: Use git tag manually for past releases | Purpose: Tag historical releases');
82
+ // Don't throw - retroactive tagging is optional
83
+ }
84
+ }
85
+ /**
86
+ * Execute the development command
87
+ */ const execute$2 = async (runConfig)=>{
88
+ const isDryRun = runConfig.dryRun || false;
89
+ const logger = getDryRunLogger(isDryRun);
90
+ logger.info('DEV_BRANCH_NAVIGATION: Navigating to working branch for development | Purpose: Start development cycle | Next: Version bump and sync');
91
+ try {
92
+ var _runConfig_development, _runConfig_development1, _runConfig_development2;
93
+ // Get current branch
94
+ const currentBranch = isDryRun ? 'mock-branch' : await getCurrentBranch();
95
+ logger.info(`DEV_CURRENT_BRANCH: Current branch identified | Branch: ${currentBranch} | Action: Determine working branch`);
96
+ // Find the working/development branch from configuration
97
+ let workingBranch = 'working'; // Default fallback
98
+ if (runConfig.branches) {
99
+ const configuredDevBranch = findDevelopmentBranch(runConfig.branches);
100
+ if (configuredDevBranch) {
101
+ workingBranch = configuredDevBranch;
102
+ logger.info(`DEV_WORKING_BRANCH_CONFIGURED: Using configured working branch | Branch: ${workingBranch} | Source: config | Current: ${currentBranch}`);
103
+ } else {
104
+ logger.info(`DEV_WORKING_BRANCH_DEFAULT: No working branch configured | Branch: ${workingBranch} | Source: default | Current: ${currentBranch}`);
105
+ }
106
+ } else {
107
+ logger.info(`DEV_WORKING_BRANCH_NO_CONFIG: No branch configuration found | Branch: ${workingBranch} | Source: default | Current: ${currentBranch}`);
108
+ }
109
+ // Track what actions are taken to determine the appropriate return message
110
+ let branchCreated = false;
111
+ let branchUpdated = false;
112
+ let alreadyOnBranch = false;
113
+ let mergedDevelopmentIntoWorking = false;
114
+ // Determine prerelease tag and increment level from configuration
115
+ const allBranchConfig = runConfig.branches || KODRDRIV_DEFAULTS.branches;
116
+ let prereleaseTag = 'dev'; // Default
117
+ let incrementLevel = 'patch'; // Default
118
+ // Check for development command specific targetVersion override
119
+ if ((_runConfig_development = runConfig.development) === null || _runConfig_development === void 0 ? void 0 : _runConfig_development.targetVersion) {
120
+ const targetVersion = runConfig.development.targetVersion;
121
+ // Validate targetVersion
122
+ if (![
123
+ 'patch',
124
+ 'minor',
125
+ 'major'
126
+ ].includes(targetVersion) && !/^\d+\.\d+\.\d+$/.test(targetVersion.replace(/^v/, ''))) {
127
+ throw new Error(`Invalid target version: ${targetVersion}. Expected "patch", "minor", "major", or a valid version string like "2.1.0"`);
128
+ }
129
+ incrementLevel = targetVersion;
130
+ } else if (allBranchConfig && allBranchConfig[workingBranch]) {
131
+ const workingBranchConfig = allBranchConfig[workingBranch];
132
+ if (workingBranchConfig.version) {
133
+ if (workingBranchConfig.version.tag) {
134
+ prereleaseTag = workingBranchConfig.version.tag;
135
+ }
136
+ if (workingBranchConfig.version.incrementLevel) {
137
+ incrementLevel = workingBranchConfig.version.incrementLevel;
138
+ }
139
+ }
140
+ }
141
+ logger.info(`DEV_VERSION_CONFIG: Development version configuration | Prerelease Tag: ${prereleaseTag} | Increment Level: ${incrementLevel}`);
142
+ logger.info(`DEV_VERSION_STRATEGY: Version increment strategy | Level: ${incrementLevel} | Tag: ${prereleaseTag} | Purpose: Development version management`);
143
+ // Step 1: Fetch latest remote information
144
+ if (!isDryRun) {
145
+ logger.info('DEV_GIT_FETCH: Fetching latest remote information | Remote: origin | Purpose: Ensure sync before branch operations');
146
+ try {
147
+ await run('git fetch origin');
148
+ logger.info('DEV_GIT_FETCH_SUCCESS: Successfully fetched remote information | Remote: origin | Status: up-to-date');
149
+ } catch (error) {
150
+ logger.warn(`DEV_GIT_FETCH_FAILED: Unable to fetch remote | Remote: origin | Error: ${error.message} | Impact: May have stale branch info`);
151
+ }
152
+ } else {
153
+ logger.info('DEV_GIT_FETCH_DRY_RUN: Would fetch latest remote information | Mode: dry-run | Remote: origin');
154
+ }
155
+ // Special case: If currently on development branch, merge development into working
156
+ if (currentBranch === 'development') {
157
+ if (!isDryRun) {
158
+ logger.info('DEV_MERGE_STARTING: Currently on development branch, merging into working | Source: development | Target: working | Purpose: Sync branches before development');
159
+ await run(`git checkout ${workingBranch}`);
160
+ await run(`git merge development --no-ff -m "Merge development into working for continued development"`);
161
+ await run('npm install');
162
+ // Check if npm install created any changes and commit them
163
+ const gitStatus = await run('git status --porcelain');
164
+ if (gitStatus.stdout.trim()) {
165
+ await run('git add -A');
166
+ await run('git commit -m "chore: update package-lock.json after merge"');
167
+ }
168
+ // Stay on working branch for development (removed checkout development)
169
+ mergedDevelopmentIntoWorking = true;
170
+ } else {
171
+ logger.info('DEV_MERGE_DRY_RUN: Would merge development into working | Mode: dry-run | Source: development | Target: working');
172
+ mergedDevelopmentIntoWorking = true;
173
+ }
174
+ }
175
+ // Step 2: Switch to working branch (create if needed) - skip if we handled development branch case
176
+ if (!isDryRun && !mergedDevelopmentIntoWorking) {
177
+ const workingBranchExists = await localBranchExists(workingBranch);
178
+ if (!workingBranchExists) {
179
+ logger.info(`DEV_BRANCH_CREATING: Working branch does not exist, creating now | Branch: ${workingBranch} | Action: Create and checkout | Source: current HEAD`);
180
+ await run(`git checkout -b ${workingBranch}`);
181
+ logger.info(`DEV_BRANCH_CREATED: Successfully created and switched to branch | Branch: ${workingBranch} | Status: checked-out`);
182
+ branchCreated = true;
183
+ } else if (currentBranch !== workingBranch) {
184
+ logger.info(`DEV_BRANCH_SWITCHING: Switching to working branch | Branch: ${workingBranch} | Action: checkout | Previous: ${currentBranch}`);
185
+ await run(`git checkout ${workingBranch}`);
186
+ logger.info(`DEV_BRANCH_SWITCHED: Successfully switched to branch | Branch: ${workingBranch} | Status: checked-out`);
187
+ branchUpdated = true;
188
+ } else {
189
+ logger.info(`DEV_BRANCH_CURRENT: Already on working branch | Branch: ${workingBranch} | Status: no-switch-needed`);
190
+ alreadyOnBranch = true;
191
+ }
192
+ } else if (!mergedDevelopmentIntoWorking) {
193
+ // For dry run, we need to mock the logic
194
+ const workingBranchExists = await localBranchExists(workingBranch);
195
+ if (!workingBranchExists) {
196
+ branchCreated = true;
197
+ } else if (currentBranch !== workingBranch) {
198
+ branchUpdated = true;
199
+ } else {
200
+ alreadyOnBranch = true;
201
+ }
202
+ logger.info(`DEV_BRANCH_DRY_RUN: Would switch to working branch | Mode: dry-run | Branch: ${workingBranch} | Action: Create if needed`);
203
+ logger.info(`DEV_SYNC_DRY_RUN: Would sync branch with remote | Mode: dry-run | Branch: ${workingBranch} | Purpose: Avoid conflicts`);
204
+ }
205
+ // Step 2.1: Sync with remote working branch to avoid conflicts
206
+ if (!isDryRun) {
207
+ try {
208
+ logger.info(`DEV_BRANCH_SYNCING: Synchronizing working branch with remote | Branch: ${workingBranch} | Remote: origin/${workingBranch} | Purpose: Avoid conflicts`);
209
+ const remoteExists = await run(`git ls-remote --exit-code --heads origin ${workingBranch}`).then(()=>true).catch(()=>false);
210
+ if (remoteExists) {
211
+ // Use explicit fetch+merge instead of pull to avoid git config conflicts
212
+ await run(`git fetch origin ${workingBranch}`);
213
+ await run(`git merge origin/${workingBranch} --no-edit`);
214
+ logger.info(`DEV_BRANCH_SYNCED: Successfully synchronized with remote | Branch: ${workingBranch} | Remote: origin/${workingBranch} | Status: in-sync`);
215
+ } else {
216
+ logger.info(`DEV_REMOTE_BRANCH_NOT_FOUND: No remote branch exists | Branch: ${workingBranch} | Remote: origin | Action: Will be created on first push`);
217
+ }
218
+ } catch (error) {
219
+ if (error.message && error.message.includes('CONFLICT')) {
220
+ logger.error(`DEV_MERGE_CONFLICTS: Merge conflicts detected when syncing with remote | Branch: ${workingBranch} | Remote: origin | Status: conflicts-detected`);
221
+ logger.error(`DEV_CONFLICT_RESOLUTION: Manual conflict resolution required:`);
222
+ logger.error(` Step 1: Resolve conflicts in the files`);
223
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
224
+ logger.error(` Step 3: Complete merge | Command: git commit`);
225
+ logger.error(` Step 4: Resume development | Command: kodrdriv development`);
226
+ throw new Error(`Merge conflicts detected when syncing ${workingBranch} with remote. Please resolve conflicts manually.`);
227
+ } else {
228
+ logger.warn(`DEV_SYNC_FAILED: Could not sync with remote | Branch: ${workingBranch} | Remote: origin | Error: ${error.message}`);
229
+ }
230
+ }
231
+ }
232
+ // Step 2.5: Sync with target branch (main) if it exists
233
+ // This is a safety net for when publish fails or user ends up on target branch
234
+ if (!isDryRun) {
235
+ var _allBranchConfig_workingBranch;
236
+ // Determine target branch from config
237
+ const targetBranch = allBranchConfig && ((_allBranchConfig_workingBranch = allBranchConfig[workingBranch]) === null || _allBranchConfig_workingBranch === void 0 ? void 0 : _allBranchConfig_workingBranch.targetBranch) || 'main';
238
+ const targetBranchExists = await localBranchExists(targetBranch);
239
+ if (targetBranchExists) {
240
+ logger.info(`DEV_TARGET_SYNC: Syncing working branch with target branch | Working: ${workingBranch} | Target: ${targetBranch} | Strategy: fast-forward`);
241
+ try {
242
+ await run(`git merge ${targetBranch} --ff-only`);
243
+ logger.info(`DEV_TARGET_MERGED_FF: Fast-forward merged target into working | Target: ${targetBranch} | Working: ${workingBranch} | Status: merged`);
244
+ } catch (error) {
245
+ // Fast-forward failed, might need regular merge
246
+ if (error.message && error.message.includes('Not possible to fast-forward')) {
247
+ logger.warn(`DEV_NO_FAST_FORWARD: Cannot fast-forward merge | Target: ${targetBranch} | Working: ${workingBranch} | Reason: Divergent history`);
248
+ logger.info(`DEV_REGULAR_MERGE_ATTEMPTING: Attempting regular merge | Strategy: fast-forward preferred | Purpose: Sync branches`);
249
+ try {
250
+ await run(`git merge ${targetBranch} -m "Merge ${targetBranch} into ${workingBranch} for sync"`);
251
+ logger.info(`DEV_TARGET_MERGED: Merged target into working | Target: ${targetBranch} | Working: ${workingBranch} | Status: merged`);
252
+ // Run npm install after merge
253
+ logger.info('DEV_POST_MERGE_INSTALL: Running npm install after merge | Command: npm install | Purpose: Update dependencies');
254
+ await run('npm install');
255
+ // Check if npm install created changes
256
+ const gitStatus = await run('git status --porcelain');
257
+ if (gitStatus.stdout.trim()) {
258
+ logger.info('DEV_POST_MERGE_COMMIT: Committing changes from npm install | Files: package-lock.json | Purpose: Finalize merge');
259
+ await run('git add -A');
260
+ await run('git commit -m "chore: update package-lock.json after merge"');
261
+ }
262
+ } catch (mergeError) {
263
+ if (mergeError.message && mergeError.message.includes('CONFLICT')) {
264
+ logger.error(`DEV_MERGE_CONFLICTS: Merge conflicts detected | Target: ${targetBranch} | Working: ${workingBranch} | Status: conflicts-detected`);
265
+ logger.error(`DEV_CONFLICT_RESOLUTION: Manual conflict resolution required:`);
266
+ logger.error(` Step 1: Resolve conflicts in the files`);
267
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
268
+ logger.error(` Step 3: Complete merge | Command: git commit`);
269
+ logger.error(` Step 4: Update dependencies | Command: npm install`);
270
+ logger.error(` Step 5: Resume development | Command: kodrdriv development`);
271
+ throw new Error(`Merge conflicts detected when merging ${targetBranch} into ${workingBranch}. Please resolve conflicts manually.`);
272
+ } else {
273
+ throw mergeError;
274
+ }
275
+ }
276
+ } else {
277
+ logger.warn(`DEV_TARGET_MERGE_FAILED: Could not merge target into working | Target: ${targetBranch} | Working: ${workingBranch} | Error: ${error.message}`);
278
+ }
279
+ }
280
+ } else {
281
+ logger.info(`DEV_TARGET_NOT_EXISTS: Target branch does not exist | Branch: ${targetBranch} | Action: Skipping target sync | Status: no-target-branch`);
282
+ }
283
+ } else {
284
+ logger.info('Would sync working branch with target branch (main) if it exists');
285
+ }
286
+ // Step 3: Merge latest changes from development branch if it exists
287
+ if (!isDryRun) {
288
+ const developmentBranchExists = await localBranchExists('development');
289
+ if (mergedDevelopmentIntoWorking) {
290
+ logger.info('DEV_ALREADY_MERGED: Already merged from development | Reason: Was on development branch | Action: Skipping');
291
+ } else if (developmentBranchExists) {
292
+ logger.info('DEV_DEVELOPMENT_MERGE: Merging latest changes from development branch | Source: development | Target: ' + workingBranch + ' | Purpose: Sync development changes');
293
+ try {
294
+ await run(`git merge development --no-ff -m "Merge latest development changes into ${workingBranch}"`);
295
+ logger.info('DEV_DEVELOPMENT_MERGED: Successfully merged development changes | Source: development | Target: ' + workingBranch + ' | Status: merged');
296
+ // Run npm install after merge to update dependencies
297
+ logger.info('DEV_DEVELOPMENT_INSTALL: Running npm install after merge | Command: npm install | Purpose: Update dependencies');
298
+ await run('npm install');
299
+ // Check if npm install created any changes (e.g., package-lock.json)
300
+ const gitStatus = await run('git status --porcelain');
301
+ if (gitStatus.stdout.trim()) {
302
+ logger.info('DEV_POST_MERGE_COMMIT: Committing changes from npm install | Files: package-lock.json | Purpose: Finalize merge');
303
+ await run('git add -A');
304
+ await run(`git commit -m "chore: update package-lock.json after merge"`);
305
+ logger.info('DEV_CHANGES_COMMITTED: Changes committed successfully | Files: package-lock.json | Status: committed');
306
+ }
307
+ } catch (error) {
308
+ if (error.message && error.message.includes('CONFLICT')) {
309
+ logger.error(`DEV_DEV_MERGE_CONFLICTS: Merge conflicts detected | Source: development | Target: ${workingBranch} | Status: conflicts-detected`);
310
+ logger.error(`DEV_DEV_CONFLICT_RESOLUTION: Manual conflict resolution required:`);
311
+ logger.error(` Step 1: Resolve conflicts in the files`);
312
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
313
+ logger.error(` Step 3: Complete merge | Command: git commit`);
314
+ logger.error(` Step 4: Update dependencies | Command: npm install`);
315
+ logger.error(` Step 5: Bump version | Command: npm version pre${incrementLevel} --preid=${prereleaseTag}`);
316
+ throw new Error(`Merge conflicts detected when merging development into ${workingBranch}. Please resolve conflicts manually.`);
317
+ } else {
318
+ logger.error(`DEV_DEV_MERGE_FAILED: Failed to merge development branch | Source: development | Target: ${workingBranch} | Error: ${error.message}`);
319
+ throw error;
320
+ }
321
+ }
322
+ } else {
323
+ logger.info('DEV_NO_DEV_BRANCH: Development branch does not exist | Branch: development | Action: Skipping merge step | Status: not-found');
324
+ }
325
+ } else {
326
+ logger.info('DEV_DEV_MERGE_DRY_RUN: Would merge development if exists | Mode: dry-run | Source: development | Target: working');
327
+ logger.info('DEV_INSTALL_DRY_RUN: Would run npm install after merge | Mode: dry-run | Command: npm install');
328
+ logger.info('DEV_COMMIT_DRY_RUN: Would commit npm install changes | Mode: dry-run | Files: package-lock.json');
329
+ }
330
+ // Step 4.5: Create retroactive tags if requested (one-time operation)
331
+ if ((_runConfig_development1 = runConfig.development) === null || _runConfig_development1 === void 0 ? void 0 : _runConfig_development1.createRetroactiveTags) {
332
+ var _runConfig_development3;
333
+ const tagPrefix = ((_runConfig_development3 = runConfig.development) === null || _runConfig_development3 === void 0 ? void 0 : _runConfig_development3.workingTagPrefix) || 'working/';
334
+ await createRetroactiveTags(workingBranch, isDryRun, logger, tagPrefix);
335
+ }
336
+ // Step 5: Check if we already have a proper development version
337
+ if (alreadyOnBranch && !mergedDevelopmentIntoWorking) {
338
+ // Check if current version is already a development version with the right tag
339
+ const fs = await import('fs/promises');
340
+ try {
341
+ const packageJson = JSON.parse(await fs.readFile('package.json', 'utf-8'));
342
+ const currentVersion = packageJson.version;
343
+ // If current version already has the dev tag, we're done
344
+ if (currentVersion.includes(`-${prereleaseTag}.`)) {
345
+ logger.info(`DEV_ALREADY_DEV_VERSION: Already on working branch with development version | Branch: ${workingBranch} | Version: ${currentVersion} | Status: no-bump-needed`);
346
+ return 'Already on working branch with development version';
347
+ }
348
+ } catch {
349
+ logger.debug('Could not check current version, proceeding with version bump');
350
+ }
351
+ }
352
+ // Step 5.5: Tag working branch with current release version BEFORE bumping
353
+ if (((_runConfig_development2 = runConfig.development) === null || _runConfig_development2 === void 0 ? void 0 : _runConfig_development2.tagWorkingBranch) !== false) {
354
+ try {
355
+ const fs = await import('fs/promises');
356
+ const packageJson = JSON.parse(await fs.readFile('package.json', 'utf-8'));
357
+ const currentVersion = packageJson.version;
358
+ // Only tag if current version is a release version (not already a dev version)
359
+ const isReleaseVersion = currentVersion && !currentVersion.includes('-dev.') && !currentVersion.includes('-alpha.') && !currentVersion.includes('-beta.') && !currentVersion.includes('-rc.');
360
+ if (isReleaseVersion) {
361
+ var _runConfig_development4;
362
+ const tagPrefix = ((_runConfig_development4 = runConfig.development) === null || _runConfig_development4 === void 0 ? void 0 : _runConfig_development4.workingTagPrefix) || 'working/';
363
+ const workingTagName = `${tagPrefix}v${currentVersion}`;
364
+ if (!isDryRun) {
365
+ logger.info(`DEV_TAG_RELEASE_VERSION: Current version is release version | Version: ${currentVersion} | Type: release | Action: Will tag before bump`);
366
+ logger.verbose(`Checking if tag ${workingTagName} exists...`);
367
+ // Check if tag already exists
368
+ const tagExistsResult = await run(`git tag -l "${workingTagName}"`);
369
+ const tagExists = tagExistsResult.stdout.trim() !== '';
370
+ if (tagExists) {
371
+ logger.info(`DEV_TAG_EXISTS: Tag already exists | Tag: ${workingTagName} | Action: Skipping tag creation | Status: already-tagged`);
372
+ } else {
373
+ // Create tag on current commit (working branch at release version)
374
+ logger.verbose(`Creating tag ${workingTagName} at current HEAD...`);
375
+ await run(`git tag ${workingTagName}`);
376
+ // Push tag to remote
377
+ logger.verbose(`Pushing tag ${workingTagName} to origin...`);
378
+ await run(`git push origin ${workingTagName}`);
379
+ logger.info(`DEV_TAG_CREATED: Tagged working branch | Tag: ${workingTagName} | Version: ${currentVersion} | Status: tagged-and-pushed`);
380
+ logger.info(`DEV_TAG_RELEASE_NOTES_HINT: Release notes can be generated | Version: v${currentVersion} | Command: kodrdriv release --from {previous-tag} --to ${workingTagName}`);
381
+ }
382
+ } else {
383
+ logger.info(`DEV_TAG_DRY_RUN: Would tag working branch | Mode: dry-run | Tag: ${workingTagName} | Version: ${currentVersion}`);
384
+ }
385
+ } else if (currentVersion) {
386
+ logger.verbose(`Current version is ${currentVersion} (prerelease), skipping tag creation`);
387
+ } else {
388
+ logger.debug('Could not determine current version, skipping tag creation');
389
+ }
390
+ } catch (error) {
391
+ if (!isDryRun) {
392
+ logger.warn(`DEV_TAG_FAILED: Could not tag working branch | Error: ${error.message} | Impact: Not critical | Alternative: Manual tagging`);
393
+ logger.warn('DEV_TAG_MANUAL: Manual tagging option available | Action: Tag manually later | Purpose: Mark release point');
394
+ } else {
395
+ logger.info('Would tag working branch with current release version if applicable');
396
+ }
397
+ // Don't throw - tagging is optional, continue with version bump
398
+ }
399
+ } else if (isDryRun) {
400
+ logger.info('Tagging disabled (--no-tag-working-branch)');
401
+ }
402
+ // Step 6: Run npm version to bump version with increment level
403
+ let versionCommand;
404
+ if ([
405
+ 'patch',
406
+ 'minor',
407
+ 'major'
408
+ ].includes(incrementLevel)) {
409
+ versionCommand = `pre${incrementLevel}`;
410
+ logger.info(`DEV_VERSION_BUMPING: Bumping version with prerelease tag | Level: ${incrementLevel} | Tag: ${prereleaseTag} | Command: npm version`);
411
+ } else {
412
+ // Explicit version like "3.5.0"
413
+ const cleanVersion = incrementLevel.replace(/^v/, '');
414
+ versionCommand = `${cleanVersion}-${prereleaseTag}.0`;
415
+ logger.info(`DEV_VERSION_EXPLICIT: Setting explicit version | Version: ${versionCommand} | Type: explicit`);
416
+ }
417
+ if (!isDryRun) {
418
+ try {
419
+ const versionResult = [
420
+ 'patch',
421
+ 'minor',
422
+ 'major'
423
+ ].includes(incrementLevel) ? await run(`npm version ${versionCommand} --preid=${prereleaseTag}`) : await run(`npm version ${versionCommand}`);
424
+ const newVersion = versionResult.stdout.trim();
425
+ logger.info(`DEV_VERSION_BUMPED: Version bumped successfully | New Version: ${newVersion} | Status: completed`);
426
+ // Return appropriate message based on what actions were taken
427
+ if (mergedDevelopmentIntoWorking) {
428
+ return 'Merged development into working and ready for development';
429
+ } else if (branchCreated) {
430
+ return 'Created working branch with development version';
431
+ } else if (branchUpdated) {
432
+ return 'Updated working branch with development version';
433
+ } else if (alreadyOnBranch) {
434
+ return 'Already on working branch with development version';
435
+ } else {
436
+ return `Ready for development on ${workingBranch} with version ${newVersion}`;
437
+ }
438
+ } catch (error) {
439
+ logger.error(`DEV_VERSION_BUMP_FAILED: Failed to bump version | Error: ${error.message} | Impact: Version not updated`);
440
+ throw new Error(`Failed to bump ${incrementLevel} version: ${error.message}`);
441
+ }
442
+ } else {
443
+ if ([
444
+ 'patch',
445
+ 'minor',
446
+ 'major'
447
+ ].includes(incrementLevel)) {
448
+ logger.info(`Would run: npm version ${versionCommand} --preid=${prereleaseTag}`);
449
+ } else {
450
+ logger.info(`Would run: npm version ${versionCommand}`);
451
+ }
452
+ // Return appropriate message based on what actions were taken
453
+ if (mergedDevelopmentIntoWorking) {
454
+ return 'Merged development into working and ready for development';
455
+ } else if (branchCreated) {
456
+ return 'Created working branch with development version';
457
+ } else if (branchUpdated) {
458
+ return 'Updated working branch with development version';
459
+ } else if (alreadyOnBranch) {
460
+ return 'Already on working branch with development version';
461
+ } else {
462
+ return `Ready for development on ${workingBranch} (dry run)`;
463
+ }
464
+ }
465
+ } catch (error) {
466
+ logger.error('Failed to prepare working branch for development:', error.message);
467
+ throw error;
468
+ }
469
+ };
470
+
471
+ // Helper function to read context files
472
+ async function readContextFiles(contextFiles, logger) {
473
+ if (!contextFiles || contextFiles.length === 0) {
474
+ return '';
475
+ }
476
+ const storage = createStorage();
477
+ const contextParts = [];
478
+ for (const filePath of contextFiles){
479
+ try {
480
+ const content = await storage.readFile(filePath, 'utf8');
481
+ contextParts.push(`## Context from ${filePath}\n\n${content}\n`);
482
+ logger.debug(`Read context from file: ${filePath}`);
483
+ } catch (error) {
484
+ logger.warn(`Failed to read context file ${filePath}: ${error.message}`);
485
+ }
486
+ }
487
+ return contextParts.join('\n---\n\n');
488
+ }
489
+ // Helper function to edit release notes using editor
490
+ async function editReleaseNotesInteractively(releaseSummary) {
491
+ const templateLines = [
492
+ '# Edit your release notes below. Lines starting with "#" will be ignored.',
493
+ '# The first line is the title, everything else is the body.',
494
+ '# Save and close the editor when you are done.'
495
+ ];
496
+ const content = `${releaseSummary.title}\n\n${releaseSummary.body}`;
497
+ const result = await editContentInEditor(content, templateLines, '.md');
498
+ const lines = result.content.split('\n');
499
+ const title = lines[0].trim();
500
+ const body = lines.slice(1).join('\n').trim();
501
+ return {
502
+ title,
503
+ body
504
+ };
505
+ }
506
+ // Helper function to improve release notes using LLM
507
+ async function improveReleaseNotesWithLLM(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, logContent, diffContent) {
508
+ // Get user feedback on what to improve using the editor
509
+ const releaseNotesContent = `${releaseSummary.title}\n\n${releaseSummary.body}`;
510
+ const userFeedback = await getLLMFeedbackInEditor('release notes', releaseNotesContent);
511
+ const improvementConfig = {
512
+ contentType: 'release notes',
513
+ createImprovedPrompt: async (promptConfig, currentSummary, promptContext)=>{
514
+ var _aiConfig_commands_release, _aiConfig_commands;
515
+ const improvementPromptContent = {
516
+ logContent: logContent,
517
+ diffContent: diffContent,
518
+ releaseFocus: `Please improve these release notes based on the user's feedback: "${userFeedback}".
519
+
520
+ Current release notes:
521
+ Title: "${currentSummary.title}"
522
+ Body: "${currentSummary.body}"
523
+
524
+ Please revise the release notes according to the user's feedback while maintaining accuracy and following good release note practices.`
525
+ };
526
+ const promptResult = await createReleasePrompt(promptConfig, improvementPromptContent, promptContext);
527
+ // Format the prompt into a proper request with messages
528
+ const aiConfig = toAIConfig(runConfig);
529
+ const modelToUse = ((_aiConfig_commands = aiConfig.commands) === null || _aiConfig_commands === void 0 ? void 0 : (_aiConfig_commands_release = _aiConfig_commands.release) === null || _aiConfig_commands_release === void 0 ? void 0 : _aiConfig_commands_release.model) || aiConfig.model || 'gpt-4o-mini';
530
+ return Formatter.create({
531
+ logger: getDryRunLogger(false)
532
+ }).formatPrompt(modelToUse, promptResult.prompt);
533
+ },
534
+ callLLM: async (request, runConfig, outputDirectory)=>{
535
+ var _aiConfig_commands_release, _aiConfig_commands, _aiConfig_commands_release1, _aiConfig_commands1;
536
+ const aiConfig = toAIConfig(runConfig);
537
+ const aiStorageAdapter = createStorageAdapter(outputDirectory);
538
+ const aiLogger = createLoggerAdapter(false);
539
+ const modelToUse = ((_aiConfig_commands = aiConfig.commands) === null || _aiConfig_commands === void 0 ? void 0 : (_aiConfig_commands_release = _aiConfig_commands.release) === null || _aiConfig_commands_release === void 0 ? void 0 : _aiConfig_commands_release.model) || aiConfig.model || 'gpt-4o-mini';
540
+ const openaiReasoning = ((_aiConfig_commands1 = aiConfig.commands) === null || _aiConfig_commands1 === void 0 ? void 0 : (_aiConfig_commands_release1 = _aiConfig_commands1.release) === null || _aiConfig_commands_release1 === void 0 ? void 0 : _aiConfig_commands_release1.reasoning) || aiConfig.reasoning;
541
+ return await createCompletionWithRetry(request.messages, {
542
+ model: modelToUse,
543
+ openaiReasoning,
544
+ responseFormat: {
545
+ type: 'json_object'
546
+ },
547
+ debug: runConfig.debug,
548
+ debugRequestFile: getOutputPath(outputDirectory, getTimestampedRequestFilename('release-improve')),
549
+ debugResponseFile: getOutputPath(outputDirectory, getTimestampedResponseFilename('release-improve')),
550
+ storage: aiStorageAdapter,
551
+ logger: aiLogger
552
+ });
553
+ },
554
+ processResponse: (response)=>{
555
+ return validateReleaseSummary(response);
556
+ }
557
+ };
558
+ return await improveContentWithLLM(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, improvementConfig);
559
+ }
560
+ // Helper function to generate self-reflection output for release notes using observability module
561
+ async function generateSelfReflection(agenticResult, outputDirectory, storage, logger) {
562
+ try {
563
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('.')[0];
564
+ const reflectionPath = getOutputPath(outputDirectory, `agentic-reflection-release-${timestamp}.md`);
565
+ // Use new observability reflection generator
566
+ const report = await generateReflectionReport({
567
+ iterations: agenticResult.iterations || 0,
568
+ toolCallsExecuted: agenticResult.toolCallsExecuted || 0,
569
+ maxIterations: agenticResult.maxIterations || 30,
570
+ toolMetrics: agenticResult.toolMetrics || [],
571
+ conversationHistory: agenticResult.conversationHistory || [],
572
+ releaseNotes: agenticResult.releaseNotes,
573
+ logger
574
+ });
575
+ // Save the report to output directory
576
+ await storage.writeFile(reflectionPath, report, 'utf8');
577
+ logger.info('');
578
+ logger.info('═'.repeat(80));
579
+ logger.info('📊 SELF-REFLECTION REPORT GENERATED');
580
+ logger.info('═'.repeat(80));
581
+ logger.info('');
582
+ logger.info('📁 Location: %s', reflectionPath);
583
+ logger.info('');
584
+ logger.info('📈 Report Summary:');
585
+ const iterations = agenticResult.iterations || 0;
586
+ const toolCalls = agenticResult.toolCallsExecuted || 0;
587
+ const uniqueTools = new Set((agenticResult.toolMetrics || []).map((m)=>m.name)).size;
588
+ logger.info(` • ${iterations} iterations completed`);
589
+ logger.info(` • ${toolCalls} tool calls executed`);
590
+ logger.info(` • ${uniqueTools} unique tools used`);
591
+ logger.info('');
592
+ logger.info('💡 Use this report to:');
593
+ logger.info(' • Understand which tools were most effective');
594
+ logger.info(' • Identify performance bottlenecks');
595
+ logger.info(' • Optimize tool selection and usage patterns');
596
+ logger.info(' • Improve agentic release notes generation');
597
+ logger.info('');
598
+ logger.info('═'.repeat(80));
599
+ } catch (error) {
600
+ logger.warn('Failed to generate self-reflection report: %s', error.message);
601
+ }
602
+ }
603
+ // Interactive feedback loop for release notes
604
+ async function handleInteractiveReleaseFeedback(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, storage, logContent, diffContent) {
605
+ const logger = getDryRunLogger(false);
606
+ let currentSummary = releaseSummary;
607
+ while(true){
608
+ // Display the current release notes
609
+ logger.info('\nRELEASE_NOTES_GENERATED: Generated release notes from AI | Title Length: ' + currentSummary.title.length + ' | Body Length: ' + currentSummary.body.length);
610
+ logger.info('─'.repeat(50));
611
+ logger.info('RELEASE_NOTES_TITLE: %s', currentSummary.title);
612
+ logger.info('');
613
+ logger.info('RELEASE_NOTES_BODY: Release notes content:');
614
+ logger.info(currentSummary.body);
615
+ logger.info('─'.repeat(50));
616
+ // Get user choice
617
+ const userChoice = await getUserChoice('\nWhat would you like to do with these release notes?', [
618
+ STANDARD_CHOICES.CONFIRM,
619
+ STANDARD_CHOICES.EDIT,
620
+ STANDARD_CHOICES.SKIP,
621
+ STANDARD_CHOICES.IMPROVE
622
+ ], {
623
+ nonTtyErrorSuggestions: [
624
+ 'Use --dry-run to see the generated content without interaction'
625
+ ]
626
+ });
627
+ switch(userChoice){
628
+ case 'c':
629
+ return {
630
+ action: 'confirm',
631
+ finalSummary: currentSummary
632
+ };
633
+ case 'e':
634
+ try {
635
+ currentSummary = await editReleaseNotesInteractively(currentSummary);
636
+ } catch (error) {
637
+ logger.error(`RELEASE_NOTES_EDIT_FAILED: Unable to edit release notes | Error: ${error.message} | Impact: Using original notes`);
638
+ // Continue the loop to show options again
639
+ }
640
+ break;
641
+ case 's':
642
+ return {
643
+ action: 'skip',
644
+ finalSummary: currentSummary
645
+ };
646
+ case 'i':
647
+ try {
648
+ currentSummary = await improveReleaseNotesWithLLM(currentSummary, runConfig, promptConfig, promptContext, outputDirectory, logContent, diffContent);
649
+ } catch (error) {
650
+ logger.error(`RELEASE_NOTES_IMPROVE_FAILED: Unable to improve release notes | Error: ${error.message} | Impact: Using current version`);
651
+ // Continue the loop to show options again
652
+ }
653
+ break;
654
+ }
655
+ }
656
+ }
657
+ const execute$1 = async (runConfig)=>{
658
+ var _ref, _ref1, _ref2, _runConfig_excludedPatterns;
659
+ 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, _runConfig_release10, _aiConfig_commands_release1, _aiConfig_commands1, _runConfig_release11, _runConfig_release12;
660
+ const isDryRun = runConfig.dryRun || false;
661
+ const logger = getDryRunLogger(isDryRun);
662
+ // Get current branch to help determine best tag comparison
663
+ const currentBranch = ((_runConfig_release = runConfig.release) === null || _runConfig_release === void 0 ? void 0 : _runConfig_release.currentBranch) || await getCurrentBranch();
664
+ // Resolve the from reference with fallback logic if not explicitly provided
665
+ const fromRef = (_ref = (_runConfig_release1 = runConfig.release) === null || _runConfig_release1 === void 0 ? void 0 : _runConfig_release1.from) !== null && _ref !== void 0 ? _ref : await getDefaultFromRef(((_runConfig_release2 = runConfig.release) === null || _runConfig_release2 === void 0 ? void 0 : _runConfig_release2.fromMain) || false, currentBranch);
666
+ const toRef = (_ref1 = (_runConfig_release3 = runConfig.release) === null || _runConfig_release3 === void 0 ? void 0 : _runConfig_release3.to) !== null && _ref1 !== void 0 ? _ref1 : DEFAULT_TO_COMMIT_ALIAS;
667
+ logger.debug(`Using git references: from=${fromRef}, to=${toRef}`);
668
+ const log = await Log.create({
669
+ from: fromRef,
670
+ to: toRef,
671
+ limit: (_runConfig_release4 = runConfig.release) === null || _runConfig_release4 === void 0 ? void 0 : _runConfig_release4.messageLimit
672
+ });
673
+ let logContent = '';
674
+ const maxDiffBytes = (_ref2 = (_runConfig_release5 = runConfig.release) === null || _runConfig_release5 === void 0 ? void 0 : _runConfig_release5.maxDiffBytes) !== null && _ref2 !== void 0 ? _ref2 : DEFAULT_MAX_DIFF_BYTES;
675
+ const diff = await Diff.create({
676
+ from: fromRef,
677
+ to: toRef,
678
+ excludedPatterns: (_runConfig_excludedPatterns = runConfig.excludedPatterns) !== null && _runConfig_excludedPatterns !== void 0 ? _runConfig_excludedPatterns : DEFAULT_EXCLUDED_PATTERNS,
679
+ maxDiffBytes
680
+ });
681
+ let diffContent = '';
682
+ diffContent = await diff.get();
683
+ logContent = await log.get();
684
+ const promptConfig = {
685
+ overridePaths: runConfig.discoveredConfigDirs || [],
686
+ overrides: runConfig.overrides || false
687
+ };
688
+ // Helper function to determine versions for milestone lookup
689
+ const determineVersionsForMilestones = async ()=>{
690
+ var _runConfig_publish;
691
+ const versions = [];
692
+ // Get current package.json version to determine likely release version
693
+ try {
694
+ const storage = createStorage();
695
+ const packageJsonContents = await storage.readFile('package.json', 'utf-8');
696
+ const packageJson = safeJsonParse(packageJsonContents, 'package.json');
697
+ const currentVersion = packageJson.version;
698
+ if (currentVersion) {
699
+ // If it's a dev version (e.g., "0.1.1-dev.0"), extract base version
700
+ if (currentVersion.includes('-dev.')) {
701
+ const baseVersion = currentVersion.split('-')[0];
702
+ versions.push(baseVersion);
703
+ logger.debug(`Detected dev version ${currentVersion}, will check milestone for ${baseVersion}`);
704
+ } else {
705
+ // Use current version as-is
706
+ versions.push(currentVersion);
707
+ logger.debug(`Using current version ${currentVersion} for milestone lookup`);
708
+ }
709
+ }
710
+ } catch (error) {
711
+ logger.debug(`Failed to read package.json version: ${error.message}`);
712
+ }
713
+ // Handle edge case: if publish targetVersion is different from current version
714
+ if (((_runConfig_publish = runConfig.publish) === null || _runConfig_publish === void 0 ? void 0 : _runConfig_publish.targetVersion) && runConfig.publish.targetVersion !== 'patch' && runConfig.publish.targetVersion !== 'minor' && runConfig.publish.targetVersion !== 'major') {
715
+ const targetVersion = runConfig.publish.targetVersion;
716
+ if (!versions.includes(targetVersion)) {
717
+ versions.push(targetVersion);
718
+ logger.debug(`Added target version ${targetVersion} for milestone lookup`);
719
+ }
720
+ }
721
+ return versions;
722
+ };
723
+ // Get milestone issues if enabled
724
+ let milestoneIssuesContent = '';
725
+ const milestonesEnabled = !((_runConfig_release6 = runConfig.release) === null || _runConfig_release6 === void 0 ? void 0 : _runConfig_release6.noMilestones);
726
+ if (milestonesEnabled) {
727
+ logger.info('RELEASE_MILESTONE_CHECK: Checking for milestone issues | Purpose: Include in release notes | Source: GitHub milestones');
728
+ const versions = await determineVersionsForMilestones();
729
+ if (versions.length > 0) {
730
+ milestoneIssuesContent = await GitHub.getMilestoneIssuesForRelease(versions, 50000);
731
+ if (milestoneIssuesContent) {
732
+ logger.info('RELEASE_MILESTONE_INCLUDED: Incorporated milestone issues into context | Count: ' + ((milestoneIssuesContent === null || milestoneIssuesContent === void 0 ? void 0 : milestoneIssuesContent.length) || 0) + ' | Purpose: Enrich release notes');
733
+ } else {
734
+ logger.debug('No milestone issues found to incorporate');
735
+ }
736
+ } else {
737
+ logger.debug('No versions determined for milestone lookup');
738
+ }
739
+ } else {
740
+ logger.debug('Milestone integration disabled via --no-milestones');
741
+ }
742
+ // Always ensure output directory exists for request/response files
743
+ const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
744
+ const storage = createStorage();
745
+ await storage.ensureDirectory(outputDirectory);
746
+ // Create adapters for ai-service
747
+ const aiConfig = toAIConfig(runConfig);
748
+ const aiStorageAdapter = createStorageAdapter(outputDirectory);
749
+ const aiLogger = createLoggerAdapter(isDryRun);
750
+ // Read context from files if provided
751
+ const contextFromFiles = await readContextFiles((_runConfig_release7 = runConfig.release) === null || _runConfig_release7 === void 0 ? void 0 : _runConfig_release7.contextFiles, logger);
752
+ // Combine file context with existing context
753
+ const combinedContext = [
754
+ (_runConfig_release8 = runConfig.release) === null || _runConfig_release8 === void 0 ? void 0 : _runConfig_release8.context,
755
+ contextFromFiles
756
+ ].filter(Boolean).join('\n\n---\n\n');
757
+ // Run agentic release notes generation
758
+ const agenticResult = await runAgenticRelease({
759
+ fromRef,
760
+ toRef,
761
+ logContent,
762
+ diffContent,
763
+ milestoneIssues: milestoneIssuesContent,
764
+ releaseFocus: (_runConfig_release9 = runConfig.release) === null || _runConfig_release9 === void 0 ? void 0 : _runConfig_release9.focus,
765
+ userContext: combinedContext || undefined,
766
+ model: ((_aiConfig_commands = aiConfig.commands) === null || _aiConfig_commands === void 0 ? void 0 : (_aiConfig_commands_release = _aiConfig_commands.release) === null || _aiConfig_commands_release === void 0 ? void 0 : _aiConfig_commands_release.model) || aiConfig.model || 'gpt-4o',
767
+ maxIterations: ((_runConfig_release10 = runConfig.release) === null || _runConfig_release10 === void 0 ? void 0 : _runConfig_release10.maxAgenticIterations) || 30,
768
+ debug: runConfig.debug,
769
+ debugRequestFile: getOutputPath(outputDirectory, getTimestampedRequestFilename('release')),
770
+ debugResponseFile: getOutputPath(outputDirectory, getTimestampedResponseFilename('release')),
771
+ storage: aiStorageAdapter,
772
+ logger: aiLogger,
773
+ openaiReasoning: ((_aiConfig_commands1 = aiConfig.commands) === null || _aiConfig_commands1 === void 0 ? void 0 : (_aiConfig_commands_release1 = _aiConfig_commands1.release) === null || _aiConfig_commands_release1 === void 0 ? void 0 : _aiConfig_commands_release1.reasoning) || aiConfig.reasoning
774
+ });
775
+ const iterations = agenticResult.iterations || 0;
776
+ const toolCalls = agenticResult.toolCallsExecuted || 0;
777
+ logger.info(`🔍 Analysis complete: ${iterations} iterations, ${toolCalls} tool calls`);
778
+ // Generate self-reflection output if enabled
779
+ if ((_runConfig_release11 = runConfig.release) === null || _runConfig_release11 === void 0 ? void 0 : _runConfig_release11.selfReflection) {
780
+ await generateSelfReflection(agenticResult, outputDirectory, storage, logger);
781
+ }
782
+ // Apply stop-context filtering to release notes
783
+ const titleFilterResult = filterContent(agenticResult.releaseNotes.title, runConfig.stopContext);
784
+ const bodyFilterResult = filterContent(agenticResult.releaseNotes.body, runConfig.stopContext);
785
+ let releaseSummary = {
786
+ title: titleFilterResult.filtered,
787
+ body: bodyFilterResult.filtered
788
+ };
789
+ // Handle interactive mode
790
+ if (((_runConfig_release12 = runConfig.release) === null || _runConfig_release12 === void 0 ? void 0 : _runConfig_release12.interactive) && !isDryRun) {
791
+ requireTTY('Interactive mode requires a terminal. Use --dry-run instead.');
792
+ const interactivePromptContext = {
793
+ context: combinedContext || undefined,
794
+ directories: runConfig.contextDirectories
795
+ };
796
+ const interactiveResult = await handleInteractiveReleaseFeedback(releaseSummary, runConfig, promptConfig, interactivePromptContext, outputDirectory, storage, logContent, diffContent);
797
+ if (interactiveResult.action === 'skip') {
798
+ logger.info('RELEASE_ABORTED: Release notes generation aborted by user | Reason: User choice | Status: cancelled');
799
+ } else {
800
+ logger.info('RELEASE_FINALIZED: Release notes finalized and accepted | Status: ready | Next: Create release or save');
801
+ }
802
+ releaseSummary = interactiveResult.finalSummary;
803
+ }
804
+ // Save timestamped copy of release notes to output directory
805
+ try {
806
+ const timestampedFilename = getTimestampedReleaseNotesFilename();
807
+ const outputPath = getOutputPath(outputDirectory, timestampedFilename);
808
+ // Format the release notes as markdown
809
+ const releaseNotesContent = `# ${releaseSummary.title}\n\n${releaseSummary.body}`;
810
+ await storage.writeFile(outputPath, releaseNotesContent, 'utf-8');
811
+ logger.debug('Saved timestamped release notes: %s', outputPath);
812
+ } catch (error) {
813
+ logger.warn('RELEASE_SAVE_FAILED: Failed to save timestamped release notes | Error: %s | Impact: Notes not persisted to file', error.message);
814
+ }
815
+ if (isDryRun) {
816
+ logger.info('RELEASE_SUMMARY_COMPLETE: Generated release summary successfully | Status: completed');
817
+ logger.info('RELEASE_SUMMARY_TITLE: %s', releaseSummary.title);
818
+ logger.info('RELEASE_SUMMARY_BODY: %s', releaseSummary.body);
819
+ }
820
+ return releaseSummary;
821
+ };
822
+
823
+ const scanNpmrcForEnvVars = async (storage)=>{
824
+ const logger = getLogger();
825
+ const npmrcPath = path.join(process.cwd(), '.npmrc');
826
+ const envVars = [];
827
+ if (await storage.exists(npmrcPath)) {
828
+ try {
829
+ const npmrcContent = await storage.readFile(npmrcPath, 'utf-8');
830
+ // Match environment variable patterns like ${VAR_NAME} or $VAR_NAME
831
+ const envVarMatches = npmrcContent.match(/\$\{([^}]+)\}|\$([A-Z_][A-Z0-9_]*)/g);
832
+ if (envVarMatches) {
833
+ for (const match of envVarMatches){
834
+ // Extract variable name from ${VAR_NAME} or $VAR_NAME format
835
+ const varName = match.replace(/\$\{|\}|\$/g, '');
836
+ if (varName && !envVars.includes(varName)) {
837
+ envVars.push(varName);
838
+ }
839
+ }
840
+ }
841
+ } catch (error) {
842
+ logger.warn(`NPMRC_READ_FAILED: Unable to read .npmrc configuration file | Path: ${npmrcPath} | Error: ${error.message}`);
843
+ logger.verbose('NPMRC_READ_IMPACT: Environment variable detection for publishing may be affected due to failed .npmrc read');
844
+ }
845
+ } else {
846
+ logger.debug('NPMRC_NOT_FOUND: No .npmrc file present in current directory | Action: Skipping environment variable scan | Path: ' + npmrcPath);
847
+ }
848
+ return envVars;
849
+ };
850
+ /**
851
+ * Checks if package-lock.json contains file: dependencies (from npm link)
852
+ * and cleans them up if found by removing package-lock.json and regenerating it.
853
+ */ const cleanupNpmLinkReferences = async (isDryRun)=>{
854
+ const logger = getDryRunLogger(isDryRun);
855
+ const packageLockPath = path.join(process.cwd(), 'package-lock.json');
856
+ try {
857
+ // Check if package-lock.json exists
858
+ try {
859
+ await fs.access(packageLockPath);
860
+ } catch {
861
+ // No package-lock.json, nothing to clean
862
+ logger.verbose('PACKAGE_LOCK_NOT_FOUND: No package-lock.json file exists | Action: Skipping npm link cleanup | Path: ' + packageLockPath);
863
+ return;
864
+ }
865
+ // Read and parse package-lock.json
866
+ const packageLockContent = await fs.readFile(packageLockPath, 'utf-8');
867
+ const packageLock = safeJsonParse(packageLockContent, packageLockPath);
868
+ // Check for file: dependencies in the lockfile
869
+ let hasFileReferences = false;
870
+ // Check in packages (npm v7+)
871
+ if (packageLock.packages) {
872
+ for (const [pkgPath, pkgInfo] of Object.entries(packageLock.packages)){
873
+ if (pkgInfo.resolved && typeof pkgInfo.resolved === 'string' && pkgInfo.resolved.startsWith('file:')) {
874
+ // Check if it's a relative path (from npm link) rather than a workspace path
875
+ const resolvedPath = pkgInfo.resolved.replace('file:', '');
876
+ if (resolvedPath.startsWith('../') || resolvedPath.startsWith('./')) {
877
+ hasFileReferences = true;
878
+ logger.verbose(`NPM_LINK_DETECTED: Found npm link reference in packages section | Package: ${pkgPath} | Resolved: ${pkgInfo.resolved} | Type: relative_file_dependency`);
879
+ break;
880
+ }
881
+ }
882
+ }
883
+ }
884
+ // Check in dependencies (npm v6)
885
+ if (!hasFileReferences && packageLock.dependencies) {
886
+ for (const [pkgName, pkgInfo] of Object.entries(packageLock.dependencies)){
887
+ if (pkgInfo.version && typeof pkgInfo.version === 'string' && pkgInfo.version.startsWith('file:')) {
888
+ const versionPath = pkgInfo.version.replace('file:', '');
889
+ if (versionPath.startsWith('../') || versionPath.startsWith('./')) {
890
+ hasFileReferences = true;
891
+ logger.verbose(`NPM_LINK_DETECTED: Found npm link reference in dependencies section | Package: ${pkgName} | Version: ${pkgInfo.version} | Type: relative_file_dependency`);
892
+ break;
893
+ }
894
+ }
895
+ }
896
+ }
897
+ if (hasFileReferences) {
898
+ logger.info('NPM_LINK_CLEANUP_REQUIRED: Detected npm link references in package-lock.json | File: package-lock.json | Impact: Must be cleaned before publish');
899
+ logger.info('NPM_LINK_CLEANUP_STARTING: Removing package-lock.json and regenerating clean version | Action: Remove file with relative dependencies');
900
+ if (isDryRun) {
901
+ logger.info('DRY_RUN_OPERATION: Would remove package-lock.json and regenerate it | Mode: dry-run | File: package-lock.json');
902
+ } else {
903
+ // Remove package-lock.json
904
+ await fs.unlink(packageLockPath);
905
+ logger.verbose('NPM_LINK_CLEANUP_FILE_REMOVED: Deleted package-lock.json containing npm link references | Path: ' + packageLockPath);
906
+ // Regenerate clean package-lock.json
907
+ logger.verbose('NPM_LOCK_REGENERATING: Executing npm install to regenerate package-lock.json from package.json | Command: npm install --package-lock-only --no-audit --no-fund');
908
+ await runWithDryRunSupport('npm install --package-lock-only --no-audit --no-fund', isDryRun);
909
+ logger.info('NPM_LOCK_REGENERATED: Successfully regenerated clean package-lock.json without link references | Path: ' + packageLockPath);
910
+ }
911
+ } else {
912
+ logger.verbose('NPM_LINK_CHECK_CLEAN: No npm link references found in package-lock.json | Status: Ready for publish | File: ' + packageLockPath);
913
+ }
914
+ } catch (error) {
915
+ // Log warning but don't fail - let npm update handle any issues
916
+ logger.warn(`NPM_LINK_CHECK_FAILED: Unable to check or clean npm link references | Error: ${error.message} | Impact: Continuing with publish, npm will handle issues`);
917
+ logger.verbose('PUBLISH_PROCESS_CONTINUING: Proceeding with publish workflow despite npm link check failure | Next: Standard npm publish validation');
918
+ }
919
+ };
920
+ const validateEnvironmentVariables = (requiredEnvVars, isDryRun)=>{
921
+ const logger = getDryRunLogger(isDryRun);
922
+ const missingEnvVars = [];
923
+ for (const envVar of requiredEnvVars){
924
+ if (!process.env[envVar]) {
925
+ missingEnvVars.push(envVar);
926
+ }
927
+ }
928
+ if (missingEnvVars.length > 0) {
929
+ if (isDryRun) {
930
+ logger.warn(`ENV_VARS_MISSING: Required environment variables not set | Variables: ${missingEnvVars.join(', ')} | Mode: dry-run | Impact: Would fail in real publish`);
931
+ } else {
932
+ logger.error(`ENV_VARS_MISSING: Required environment variables not set | Variables: ${missingEnvVars.join(', ')} | Action: Must set before publish | Source: .npmrc configuration`);
933
+ throw new Error(`Missing required environment variables: ${missingEnvVars.join(', ')}. Please set these environment variables before running publish.`);
934
+ }
935
+ }
936
+ };
937
+ const runPrechecks = async (runConfig, targetBranch)=>{
938
+ var _runConfig_publish, _runConfig_publish1;
939
+ const isDryRun = runConfig.dryRun || false;
940
+ const logger = getDryRunLogger(isDryRun);
941
+ const storage = createStorage();
942
+ logger.info('PRECHECK_STARTING: Executing publish prechecks | Phase: validation | Target: ' + (targetBranch || 'default'));
943
+ // Check if we're in a git repository
944
+ try {
945
+ if (isDryRun) {
946
+ logger.info('PRECHECK_GIT_REPO: Would verify git repository | Mode: dry-run | Command: git rev-parse --git-dir');
947
+ } else {
948
+ await run('git rev-parse --git-dir');
949
+ }
950
+ } catch (error) {
951
+ if (!isDryRun) {
952
+ // Preserve the original error message to help with debugging
953
+ const originalMessage = error.message || error.toString();
954
+ throw new Error(`Not in a git repository or git command failed: ${originalMessage}. Please run this command from within a git repository.`);
955
+ }
956
+ }
957
+ // Check for uncommitted changes
958
+ logger.info('PRECHECK_GIT_STATUS: Checking for uncommitted changes | Command: git status --porcelain | Requirement: Clean working directory');
959
+ try {
960
+ if (isDryRun) {
961
+ logger.info('PRECHECK_GIT_STATUS: Would verify clean working directory | Mode: dry-run | Command: git status --porcelain');
962
+ } else {
963
+ const { stdout } = await run('git status --porcelain');
964
+ if (stdout.trim()) {
965
+ throw new Error('Working directory has uncommitted changes. Please commit or stash your changes before running publish.');
966
+ }
967
+ }
968
+ } catch (error) {
969
+ if (!isDryRun) {
970
+ // Preserve the original error message to help with debugging
971
+ const originalMessage = error.message || error.toString();
972
+ throw new Error(`Failed to check git status: ${originalMessage}. Please ensure you are in a valid git repository and try again.`);
973
+ }
974
+ }
975
+ // Use the passed target branch or fallback to config/default
976
+ const effectiveTargetBranch = targetBranch || ((_runConfig_publish = runConfig.publish) === null || _runConfig_publish === void 0 ? void 0 : _runConfig_publish.targetBranch) || 'main';
977
+ // Check that we're not running from the target branch
978
+ logger.info('PRECHECK_BRANCH: Verifying current branch is not target branch | Target: ' + effectiveTargetBranch + ' | Requirement: Must run from feature branch');
979
+ if (isDryRun) {
980
+ logger.info(`PRECHECK_BRANCH: Would verify current branch is not target branch | Mode: dry-run | Target: ${effectiveTargetBranch}`);
981
+ } else {
982
+ const currentBranch = await GitHub.getCurrentBranchName();
983
+ if (currentBranch === effectiveTargetBranch) {
984
+ throw new Error(`Cannot run publish from the target branch '${effectiveTargetBranch}'. Please switch to a different branch before running publish.`);
985
+ }
986
+ }
987
+ // Check target branch sync with remote
988
+ logger.info(`PRECHECK_BRANCH_SYNC: Checking target branch sync with remote | Branch: ${effectiveTargetBranch} | Remote: origin | Requirement: Branches must be synchronized`);
989
+ if (isDryRun) {
990
+ logger.info(`PRECHECK_BRANCH_SYNC: Would verify target branch is in sync with remote | Mode: dry-run | Branch: ${effectiveTargetBranch} | Remote: origin`);
991
+ } else {
992
+ // Only check if local target branch exists (it's okay if it doesn't exist locally)
993
+ const targetBranchExists = await localBranchExists(effectiveTargetBranch);
994
+ if (targetBranchExists) {
995
+ const syncStatus = await isBranchInSyncWithRemote(effectiveTargetBranch);
996
+ if (!syncStatus.inSync) {
997
+ var _runConfig_publish2;
998
+ logger.error(`BRANCH_SYNC_FAILED: Target branch not synchronized with remote | Branch: ${effectiveTargetBranch} | Status: out-of-sync | Impact: Cannot proceed with publish`);
999
+ logger.error('');
1000
+ if (syncStatus.error) {
1001
+ logger.error(`BRANCH_SYNC_ERROR: ${syncStatus.error}`);
1002
+ } else if (syncStatus.localSha && syncStatus.remoteSha) {
1003
+ logger.error(`BRANCH_SYNC_DIVERGENCE: Local and remote commits differ | Local SHA: ${syncStatus.localSha.substring(0, 8)} | Remote SHA: ${syncStatus.remoteSha.substring(0, 8)}`);
1004
+ }
1005
+ // Check if agentic publish is enabled
1006
+ if ((_runConfig_publish2 = runConfig.publish) === null || _runConfig_publish2 === void 0 ? void 0 : _runConfig_publish2.agenticPublish) {
1007
+ logger.info('');
1008
+ logger.info('AGENTIC_PUBLISH_STARTING: Attempting automatic diagnosis and fix | Mode: agentic | Feature: AI-powered recovery');
1009
+ try {
1010
+ var _syncStatus_localSha, _syncStatus_remoteSha, _runConfig_publish3;
1011
+ const currentBranch = await GitHub.getCurrentBranchName();
1012
+ const agenticResult = await runAgenticPublish({
1013
+ targetBranch: effectiveTargetBranch,
1014
+ sourceBranch: currentBranch,
1015
+ issue: 'branch_sync',
1016
+ issueDetails: syncStatus.error || `Local SHA: ${(_syncStatus_localSha = syncStatus.localSha) === null || _syncStatus_localSha === void 0 ? void 0 : _syncStatus_localSha.substring(0, 8)}, Remote SHA: ${(_syncStatus_remoteSha = syncStatus.remoteSha) === null || _syncStatus_remoteSha === void 0 ? void 0 : _syncStatus_remoteSha.substring(0, 8)}`,
1017
+ workingDirectory: process.cwd(),
1018
+ maxIterations: ((_runConfig_publish3 = runConfig.publish) === null || _runConfig_publish3 === void 0 ? void 0 : _runConfig_publish3.agenticPublishMaxIterations) || 10,
1019
+ storage,
1020
+ logger,
1021
+ dryRun: runConfig.dryRun
1022
+ });
1023
+ // Display the formatted result
1024
+ const formattedResult = formatAgenticPublishResult(agenticResult);
1025
+ logger.info(formattedResult);
1026
+ if (agenticResult.success) {
1027
+ logger.info('AGENTIC_PUBLISH_SUCCESS: Issue resolved automatically | Status: ready-to-retry | Action: Re-running prechecks');
1028
+ // Re-run the sync check to verify it was fixed
1029
+ const reSyncStatus = await isBranchInSyncWithRemote(effectiveTargetBranch);
1030
+ if (reSyncStatus.inSync) {
1031
+ logger.info(`BRANCH_SYNC_VERIFIED: Target branch is now synchronized with remote | Branch: ${effectiveTargetBranch} | Status: in-sync`);
1032
+ return; // Continue with publish
1033
+ } else {
1034
+ logger.warn('AGENTIC_PUBLISH_VERIFICATION_FAILED: Branch still not in sync after agentic fix | Status: needs-attention');
1035
+ }
1036
+ }
1037
+ if (agenticResult.requiresManualIntervention) {
1038
+ throw new Error(`Target branch '${effectiveTargetBranch}' requires manual intervention. Please see the steps above.`);
1039
+ } else {
1040
+ throw new Error(`Agentic publish could not resolve the issue automatically. Please see the analysis above.`);
1041
+ }
1042
+ } catch (agenticError) {
1043
+ logger.warn(`AGENTIC_PUBLISH_FAILED: Agentic recovery failed | Error: ${agenticError.message} | Fallback: Manual steps`);
1044
+ // Fall through to manual steps
1045
+ }
1046
+ }
1047
+ logger.error('');
1048
+ logger.error('RESOLUTION_STEPS: Manual intervention required to sync branches:');
1049
+ logger.error(` Step 1: Switch to target branch | Command: git checkout ${effectiveTargetBranch}`);
1050
+ logger.error(` Step 2: Pull latest changes | Command: git pull origin ${effectiveTargetBranch}`);
1051
+ logger.error(' Step 3: Resolve merge conflicts if present');
1052
+ logger.error(' Step 4: Return to feature branch and retry publish');
1053
+ logger.error('');
1054
+ logger.error(`ALTERNATIVE_OPTION: Automatic sync available | Command: kodrdriv publish --sync-target | Branch: ${effectiveTargetBranch}`);
1055
+ logger.error(`ALTERNATIVE_OPTION_AI: AI-powered recovery available | Command: kodrdriv publish --agentic-publish | Branch: ${effectiveTargetBranch}`);
1056
+ throw new Error(`Target branch '${effectiveTargetBranch}' is not in sync with remote. Please sync the branch before running publish.`);
1057
+ } else {
1058
+ logger.info(`BRANCH_SYNC_VERIFIED: Target branch is synchronized with remote | Branch: ${effectiveTargetBranch} | Status: in-sync`);
1059
+ }
1060
+ } else {
1061
+ logger.info(`BRANCH_NOT_LOCAL: Target branch does not exist locally | Branch: ${effectiveTargetBranch} | Action: Will be created during publish process`);
1062
+ }
1063
+ }
1064
+ // Check GitHub Actions workflow configuration
1065
+ logger.info('PRECHECK_WORKFLOW: Checking GitHub Actions workflow configuration | Target: PR automation | Requirement: Workflows should trigger on pull requests');
1066
+ if (isDryRun) {
1067
+ logger.info('PRECHECK_WORKFLOW: Would check if GitHub Actions workflows are configured for pull requests | Mode: dry-run');
1068
+ } else {
1069
+ try {
1070
+ // TODO: Re-enable when checkWorkflowConfiguration is exported from github-tools
1071
+ // const workflowConfig = await GitHub.checkWorkflowConfiguration(effectiveTargetBranch);
1072
+ const workflowConfig = {
1073
+ hasWorkflows: true,
1074
+ hasPullRequestTriggers: true,
1075
+ workflowCount: 0,
1076
+ triggeredWorkflowNames: []
1077
+ };
1078
+ if (!workflowConfig.hasWorkflows) ; else if (!workflowConfig.hasPullRequestTriggers) ; else {
1079
+ logger.info(`WORKFLOW_CONFIGURED: Found workflows that will trigger on pull requests | Target Branch: ${effectiveTargetBranch} | Workflow Count: ${workflowConfig.triggeredWorkflowNames.length}`);
1080
+ for (const workflowName of workflowConfig.triggeredWorkflowNames){
1081
+ logger.info(`WORKFLOW_ACTIVE: ${workflowName} | Trigger: pull_request | Target: ${effectiveTargetBranch}`);
1082
+ }
1083
+ }
1084
+ } catch (error) {
1085
+ // Don't fail the precheck if we can't verify workflows
1086
+ // The wait logic will handle it later
1087
+ logger.debug(`WORKFLOW_CHECK_FAILED: Unable to verify workflow configuration | Error: ${error.message} | Impact: Will proceed with publish | Note: Wait logic will handle checks later`);
1088
+ }
1089
+ }
1090
+ // Check if prepublishOnly script exists in package.json
1091
+ logger.info('PRECHECK_PREPUBLISH: Checking for prepublishOnly script in package.json | Requirement: Must exist to run pre-flight checks | Expected: clean, lint, build, test');
1092
+ const packageJsonPath = path.join(process.cwd(), 'package.json');
1093
+ if (!await storage.exists(packageJsonPath)) {
1094
+ if (!isDryRun) {
1095
+ throw new Error('package.json not found in current directory.');
1096
+ } else {
1097
+ logger.warn('PACKAGE_JSON_NOT_FOUND: No package.json in current directory | Mode: dry-run | Impact: Cannot verify prepublishOnly script | Path: ' + packageJsonPath);
1098
+ }
1099
+ } else {
1100
+ var _packageJson_scripts;
1101
+ let packageJson;
1102
+ try {
1103
+ const packageJsonContents = await storage.readFile(packageJsonPath, 'utf-8');
1104
+ const parsed = safeJsonParse(packageJsonContents, packageJsonPath);
1105
+ packageJson = validatePackageJson(parsed, packageJsonPath);
1106
+ } catch (error) {
1107
+ if (!isDryRun) {
1108
+ throw new Error('Failed to parse package.json. Please ensure it contains valid JSON.');
1109
+ } else {
1110
+ logger.warn('PACKAGE_JSON_PARSE_FAILED: Unable to parse package.json | Mode: dry-run | Impact: Cannot verify prepublishOnly script | Path: ' + packageJsonPath + ' | Requirement: Valid JSON format');
1111
+ }
1112
+ }
1113
+ if (packageJson && !((_packageJson_scripts = packageJson.scripts) === null || _packageJson_scripts === void 0 ? void 0 : _packageJson_scripts.prepublishOnly)) {
1114
+ if (!isDryRun) {
1115
+ throw new Error('prepublishOnly script is required in package.json but was not found. Please add a prepublishOnly script that runs your pre-flight checks (e.g., clean, lint, build, test).');
1116
+ } else {
1117
+ logger.warn('PREPUBLISH_SCRIPT_MISSING: No prepublishOnly script found in package.json | Mode: dry-run | Requirement: Script must exist | Expected Tasks: clean, lint, build, test | Path: ' + packageJsonPath);
1118
+ }
1119
+ }
1120
+ }
1121
+ // Check required environment variables
1122
+ logger.verbose('PRECHECK_ENV_VARS: Checking required environment variables | Source: Configuration and .npmrc | Requirement: All required vars must be set');
1123
+ const coreRequiredEnvVars = ((_runConfig_publish1 = runConfig.publish) === null || _runConfig_publish1 === void 0 ? void 0 : _runConfig_publish1.requiredEnvVars) || [];
1124
+ const npmrcEnvVars = isDryRun ? [] : await scanNpmrcForEnvVars(storage); // Skip .npmrc scan in dry run
1125
+ const allRequiredEnvVars = [
1126
+ ...new Set([
1127
+ ...coreRequiredEnvVars,
1128
+ ...npmrcEnvVars
1129
+ ])
1130
+ ];
1131
+ if (allRequiredEnvVars.length > 0) {
1132
+ logger.verbose(`ENV_VARS_REQUIRED: Environment variables needed for publish | Variables: ${allRequiredEnvVars.join(', ')} | Count: ${allRequiredEnvVars.length} | Source: config + .npmrc`);
1133
+ validateEnvironmentVariables(allRequiredEnvVars, isDryRun);
1134
+ } else {
1135
+ logger.verbose('ENV_VARS_NONE: No required environment variables specified | Status: No validation needed | Source: config + .npmrc');
1136
+ }
1137
+ logger.info('PRECHECK_COMPLETE: All publish prechecks passed successfully | Status: Ready to proceed | Next: Execute publish workflow');
1138
+ };
1139
+ // Helper: deep-sort object keys for stable comparison
1140
+ const sortObjectKeys = (value)=>{
1141
+ if (Array.isArray(value)) {
1142
+ return value.map(sortObjectKeys);
1143
+ }
1144
+ if (value && typeof value === 'object') {
1145
+ const sorted = {};
1146
+ Object.keys(value).sort().forEach((key)=>{
1147
+ sorted[key] = sortObjectKeys(value[key]);
1148
+ });
1149
+ return sorted;
1150
+ }
1151
+ return value;
1152
+ };
1153
+ // Determine if there are substantive changes compared to the target branch (beyond just version bump)
1154
+ const isReleaseNecessaryComparedToTarget = async (targetBranch, isDryRun)=>{
1155
+ const logger = getDryRunLogger(isDryRun);
1156
+ // We compare current HEAD branch to the provided target branch
1157
+ const currentBranch = await GitHub.getCurrentBranchName();
1158
+ // Check if target branch exists before trying to compare
1159
+ try {
1160
+ // Validate target branch exists and is accessible
1161
+ await runSecure('git', [
1162
+ 'rev-parse',
1163
+ '--verify',
1164
+ targetBranch
1165
+ ]);
1166
+ } catch (error) {
1167
+ // Target branch doesn't exist or isn't accessible
1168
+ logger.verbose(`RELEASE_CHECK_NO_TARGET: Target branch does not exist or is not accessible | Branch: ${targetBranch} | Action: Proceeding with publish | Reason: First release to this branch`);
1169
+ return {
1170
+ necessary: true,
1171
+ reason: `Target branch '${targetBranch}' does not exist; first release to this branch`
1172
+ };
1173
+ }
1174
+ // If branches are identical, nothing to release
1175
+ const { stdout: namesStdout } = await runSecure('git', [
1176
+ 'diff',
1177
+ '--name-only',
1178
+ `${targetBranch}..${currentBranch}`
1179
+ ]);
1180
+ const changedFiles = namesStdout.split('\n').map((s)=>s.trim()).filter(Boolean);
1181
+ if (changedFiles.length === 0) {
1182
+ // No definitive signal; proceed with publish rather than skipping
1183
+ return {
1184
+ necessary: true,
1185
+ reason: 'No detectable changes via diff; proceeding conservatively'
1186
+ };
1187
+ }
1188
+ // If any files changed other than package.json or package-lock.json, a release is necessary
1189
+ const nonVersionFiles = changedFiles.filter((f)=>f !== 'package.json' && f !== 'package-lock.json');
1190
+ if (nonVersionFiles.length > 0) {
1191
+ return {
1192
+ necessary: true,
1193
+ reason: `Changed files beyond version bump: ${nonVersionFiles.join(', ')}`
1194
+ };
1195
+ }
1196
+ // Only package.json and/or package-lock.json changed. Verify package.json change is only the version field
1197
+ try {
1198
+ // Read package.json content from both branches
1199
+ const { stdout: basePkgStdout } = await runSecure('git', [
1200
+ 'show',
1201
+ `${targetBranch}:package.json`
1202
+ ]);
1203
+ const { stdout: headPkgStdout } = await runSecure('git', [
1204
+ 'show',
1205
+ `${currentBranch}:package.json`
1206
+ ]);
1207
+ const basePkg = validatePackageJson(safeJsonParse(basePkgStdout, `${targetBranch}:package.json`), `${targetBranch}:package.json`);
1208
+ const headPkg = validatePackageJson(safeJsonParse(headPkgStdout, `${currentBranch}:package.json`), `${currentBranch}:package.json`);
1209
+ const { version: _baseVersion, ...baseWithoutVersion } = basePkg;
1210
+ const { version: _headVersion, ...headWithoutVersion } = headPkg;
1211
+ const baseSorted = sortObjectKeys(baseWithoutVersion);
1212
+ const headSorted = sortObjectKeys(headWithoutVersion);
1213
+ const equalExceptVersion = JSON.stringify(baseSorted) === JSON.stringify(headSorted);
1214
+ if (equalExceptVersion) {
1215
+ const currentVersion = headPkg.version;
1216
+ const targetVersion = basePkg.version;
1217
+ return {
1218
+ necessary: false,
1219
+ reason: `No meaningful changes detected:\n • Current version: ${currentVersion}\n • Target branch version: ${targetVersion}\n • Only package.json version field differs\n\n To force republish: Add meaningful code changes or use --force (not yet implemented)`
1220
+ };
1221
+ }
1222
+ // Other fields changed inside package.json
1223
+ return {
1224
+ necessary: true,
1225
+ reason: 'package.json changes beyond version field'
1226
+ };
1227
+ } catch (error) {
1228
+ // Conservative: if we cannot prove it is only a version change, proceed with release
1229
+ logger.verbose(`RELEASE_CHECK_COMPARISON_FAILED: Unable to conclusively compare package.json changes | Error: ${error.message} | Action: Proceeding conservatively with publish | Reason: Cannot verify version-only change`);
1230
+ return {
1231
+ necessary: true,
1232
+ reason: 'Could not compare package.json safely'
1233
+ };
1234
+ }
1235
+ };
1236
+ const handleTargetBranchSyncRecovery = async (runConfig, targetBranch)=>{
1237
+ const isDryRun = runConfig.dryRun || false;
1238
+ const logger = getDryRunLogger(isDryRun);
1239
+ logger.info(`BRANCH_SYNC_ATTEMPTING: Initiating sync of target branch with remote | Branch: ${targetBranch} | Remote: origin | Operation: fetch + merge`);
1240
+ if (isDryRun) {
1241
+ logger.info(`BRANCH_SYNC_DRY_RUN: Would attempt to sync branch with remote | Mode: dry-run | Branch: ${targetBranch} | Remote: origin`);
1242
+ return;
1243
+ }
1244
+ const syncResult = await safeSyncBranchWithRemote(targetBranch);
1245
+ if (syncResult.success) {
1246
+ logger.info(`BRANCH_SYNC_SUCCESS: Successfully synchronized branch with remote | Branch: ${targetBranch} | Remote: origin | Status: in-sync`);
1247
+ logger.info('BRANCH_SYNC_NEXT_STEP: Ready to proceed with publish | Action: Re-run publish command | Branch: ' + targetBranch);
1248
+ } else if (syncResult.conflictResolutionRequired) {
1249
+ logger.error(`BRANCH_SYNC_CONFLICTS: Sync failed due to merge conflicts | Branch: ${targetBranch} | Status: conflicts-detected | Resolution: Manual intervention required`);
1250
+ logger.error('');
1251
+ logger.error('CONFLICT_RESOLUTION_STEPS: Manual conflict resolution required:');
1252
+ logger.error(` Step 1: Switch to target branch | Command: git checkout ${targetBranch}`);
1253
+ logger.error(` Step 2: Pull and resolve conflicts | Command: git pull origin ${targetBranch}`);
1254
+ logger.error(' Step 3: Commit resolved changes | Command: git commit');
1255
+ logger.error(' Step 4: Return to feature branch and retry | Command: kodrdriv publish');
1256
+ logger.error('');
1257
+ throw new Error(`Target branch '${targetBranch}' has conflicts that require manual resolution.`);
1258
+ } else {
1259
+ logger.error(`BRANCH_SYNC_FAILED: Sync operation failed | Branch: ${targetBranch} | Error: ${syncResult.error} | Remote: origin`);
1260
+ throw new Error(`Failed to sync target branch: ${syncResult.error}`);
1261
+ }
1262
+ };
1263
+ const execute = async (runConfig)=>{
1264
+ var _runConfig_publish, _runConfig_publish1, _runConfig_publish2, _runConfig_publish3;
1265
+ const isDryRun = runConfig.dryRun || false;
1266
+ const logger = getDryRunLogger(isDryRun);
1267
+ const storage = createStorage();
1268
+ // Get current branch for branch-dependent targeting
1269
+ let currentBranch;
1270
+ if (isDryRun) {
1271
+ currentBranch = 'mock-branch';
1272
+ } else {
1273
+ currentBranch = await GitHub.getCurrentBranchName();
1274
+ // Fetch latest remote information to avoid conflicts
1275
+ logger.info('GIT_FETCH_STARTING: Fetching latest remote information | Remote: origin | Purpose: Avoid conflicts during publish | Command: git fetch origin');
1276
+ try {
1277
+ await run('git fetch origin');
1278
+ logger.info('GIT_FETCH_SUCCESS: Successfully fetched latest remote information | Remote: origin | Status: up-to-date');
1279
+ } catch (error) {
1280
+ logger.warn(`GIT_FETCH_FAILED: Unable to fetch from remote | Remote: origin | Error: ${error.message} | Impact: May cause conflicts if remote has changes`);
1281
+ }
1282
+ // Sync current branch with remote to avoid conflicts
1283
+ logger.info(`CURRENT_BRANCH_SYNC: Synchronizing current branch with remote | Branch: ${currentBranch} | Remote: origin | Purpose: Avoid conflicts during publish`);
1284
+ try {
1285
+ const remoteExists = await run(`git ls-remote --exit-code --heads origin ${currentBranch}`).then(()=>true).catch(()=>false);
1286
+ if (remoteExists) {
1287
+ // Use explicit fetch+merge instead of pull to avoid git config conflicts
1288
+ await runGitWithLock(process.cwd(), async ()=>{
1289
+ await run(`git fetch origin ${currentBranch}`);
1290
+ await run(`git merge origin/${currentBranch} --no-edit`);
1291
+ }, `sync ${currentBranch}`);
1292
+ logger.info(`CURRENT_BRANCH_SYNCED: Successfully synchronized current branch with remote | Branch: ${currentBranch} | Remote: origin/${currentBranch} | Status: in-sync`);
1293
+ } else {
1294
+ logger.info(`REMOTE_BRANCH_NOT_FOUND: No remote branch exists | Branch: ${currentBranch} | Remote: origin | Action: Will be created on first push`);
1295
+ }
1296
+ } catch (error) {
1297
+ if (error.message && error.message.includes('CONFLICT')) {
1298
+ logger.error(`MERGE_CONFLICTS_DETECTED: Conflicts found when syncing current branch with remote | Branch: ${currentBranch} | Remote: origin/${currentBranch} | Status: conflicts-require-resolution`);
1299
+ logger.error(`CONFLICT_RESOLUTION_REQUIRED: Manual intervention needed to resolve conflicts and continue:`);
1300
+ logger.error(` Step 1: Resolve conflicts in affected files`);
1301
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
1302
+ logger.error(` Step 3: Commit resolution | Command: git commit`);
1303
+ logger.error(` Step 4: Retry publish | Command: kodrdriv publish`);
1304
+ throw new Error(`Merge conflicts detected when syncing ${currentBranch} with remote. Please resolve conflicts manually.`);
1305
+ } else {
1306
+ logger.warn(`CURRENT_BRANCH_SYNC_FAILED: Unable to sync current branch with remote | Branch: ${currentBranch} | Remote: origin/${currentBranch} | Error: ${error.message} | Impact: May cause issues during publish`);
1307
+ }
1308
+ }
1309
+ }
1310
+ // Determine target branch and version strategy based on branch configuration
1311
+ let targetBranch = ((_runConfig_publish = runConfig.publish) === null || _runConfig_publish === void 0 ? void 0 : _runConfig_publish.targetBranch) || 'main';
1312
+ let branchDependentVersioning = false;
1313
+ // Check for branches configuration
1314
+ if (runConfig.branches && runConfig.branches[currentBranch]) {
1315
+ branchDependentVersioning = true;
1316
+ const branchConfig = runConfig.branches[currentBranch];
1317
+ if (branchConfig.targetBranch) {
1318
+ targetBranch = branchConfig.targetBranch;
1319
+ }
1320
+ logger.info(`BRANCH_DEPENDENT_TARGETING: Branch-specific configuration active | Source: ${currentBranch} | Target: ${targetBranch} | Feature: Branch-dependent versioning and targeting`);
1321
+ logger.info(`BRANCH_CONFIGURATION_SOURCE: Current branch | Branch: ${currentBranch} | Type: source`);
1322
+ logger.info(`BRANCH_CONFIGURATION_TARGET: Target branch for publish | Branch: ${targetBranch} | Type: destination`);
1323
+ // Look at target branch config to show version strategy
1324
+ const targetBranchConfig = runConfig.branches[targetBranch];
1325
+ if (targetBranchConfig === null || targetBranchConfig === void 0 ? void 0 : targetBranchConfig.version) {
1326
+ const versionType = targetBranchConfig.version.type;
1327
+ const versionTag = targetBranchConfig.version.tag;
1328
+ const versionIncrement = targetBranchConfig.version.increment;
1329
+ logger.info(`VERSION_STRATEGY: Target branch version configuration | Branch: ${targetBranch} | Type: ${versionType} | Tag: ${versionTag || 'none'} | Increment: ${versionIncrement ? 'enabled' : 'disabled'}`);
1330
+ }
1331
+ } else {
1332
+ logger.debug(`BRANCH_TARGETING_DEFAULT: No branch-specific configuration found | Branch: ${currentBranch} | Action: Using default target | Target: ${targetBranch}`);
1333
+ }
1334
+ // Handle --sync-target flag
1335
+ if ((_runConfig_publish1 = runConfig.publish) === null || _runConfig_publish1 === void 0 ? void 0 : _runConfig_publish1.syncTarget) {
1336
+ await handleTargetBranchSyncRecovery(runConfig, targetBranch);
1337
+ return; // Exit after sync operation
1338
+ }
1339
+ // Check if target branch exists and create it if needed
1340
+ logger.info(`TARGET_BRANCH_CHECK: Verifying target branch existence | Branch: ${targetBranch} | Action: Create if missing | Source: Current HEAD`);
1341
+ if (isDryRun) {
1342
+ logger.info(`TARGET_BRANCH_CHECK: Would verify target branch exists and create if needed | Mode: dry-run | Branch: ${targetBranch}`);
1343
+ } else {
1344
+ const targetBranchExists = await localBranchExists(targetBranch);
1345
+ if (!targetBranchExists) {
1346
+ // Check if it exists on remote
1347
+ const remoteExists = await remoteBranchExists(targetBranch);
1348
+ if (remoteExists) {
1349
+ logger.info(`TARGET_BRANCH_TRACKING: Target branch exists on remote but not locally, tracking origin/${targetBranch} | Branch: ${targetBranch}`);
1350
+ try {
1351
+ await runGitWithLock(process.cwd(), async ()=>{
1352
+ // Create local branch tracking remote
1353
+ await runSecure('git', [
1354
+ 'branch',
1355
+ targetBranch,
1356
+ `origin/${targetBranch}`
1357
+ ]);
1358
+ logger.info(`TARGET_BRANCH_CREATED: Successfully created local tracking branch | Branch: ${targetBranch} | Source: origin/${targetBranch}`);
1359
+ }, `track target branch ${targetBranch}`);
1360
+ } catch (error) {
1361
+ throw new Error(`Failed to track target branch '${targetBranch}': ${error.message}`);
1362
+ }
1363
+ } else {
1364
+ logger.info(`TARGET_BRANCH_CREATING: Target branch does not exist locally or on remote, creating from current branch | Branch: ${targetBranch} | Source: HEAD | Remote: origin`);
1365
+ try {
1366
+ // Wrap git branch and push operations with lock
1367
+ await runGitWithLock(process.cwd(), async ()=>{
1368
+ // Create the target branch from the current HEAD
1369
+ await runSecure('git', [
1370
+ 'branch',
1371
+ targetBranch,
1372
+ 'HEAD'
1373
+ ]);
1374
+ logger.info(`TARGET_BRANCH_CREATED: Successfully created target branch locally | Branch: ${targetBranch} | Source: HEAD`);
1375
+ // Push the new branch to origin
1376
+ await runSecure('git', [
1377
+ 'push',
1378
+ 'origin',
1379
+ targetBranch
1380
+ ]);
1381
+ logger.info(`TARGET_BRANCH_PUSHED: Successfully pushed new target branch to remote | Branch: ${targetBranch} | Remote: origin/${targetBranch}`);
1382
+ }, `create and push target branch ${targetBranch}`);
1383
+ } catch (error) {
1384
+ throw new Error(`Failed to create target branch '${targetBranch}': ${error.message}`);
1385
+ }
1386
+ }
1387
+ } else {
1388
+ logger.info(`TARGET_BRANCH_EXISTS: Target branch already exists locally | Branch: ${targetBranch} | Status: ready`);
1389
+ }
1390
+ }
1391
+ // Run prechecks before starting any work
1392
+ await runPrechecks(runConfig, targetBranch);
1393
+ // Early check: determine if a release is necessary compared to target branch
1394
+ logger.info('RELEASE_NECESSITY_CHECK: Evaluating if release is required | Comparison: current branch vs target | Target: ' + targetBranch + ' | Purpose: Avoid unnecessary publishes');
1395
+ try {
1396
+ const necessity = await isReleaseNecessaryComparedToTarget(targetBranch, isDryRun);
1397
+ if (!necessity.necessary) {
1398
+ logger.info(`\nRELEASE_SKIPPED: No meaningful changes detected, skipping publish | Reason: ${necessity.reason} | Target: ${targetBranch}`);
1399
+ // Emit a machine-readable marker so tree mode can detect skip and avoid propagating versions
1400
+ // CRITICAL: Use console.log to write to stdout (logger.info goes to stderr via winston)
1401
+ // eslint-disable-next-line no-console
1402
+ console.log('KODRDRIV_PUBLISH_SKIPPED');
1403
+ return;
1404
+ } else {
1405
+ logger.verbose(`RELEASE_PROCEEDING: Meaningful changes detected, continuing with publish | Reason: ${necessity.reason} | Target: ${targetBranch}`);
1406
+ }
1407
+ } catch (error) {
1408
+ // On unexpected errors, proceed with publish to avoid false negatives blocking releases
1409
+ logger.verbose(`RELEASE_NECESSITY_CHECK_ERROR: Unable to determine release necessity | Error: ${error.message} | Action: Proceeding conservatively with publish | Rationale: Avoid blocking valid releases`);
1410
+ }
1411
+ logger.info('RELEASE_PROCESS_STARTING: Initiating release workflow | Target: ' + targetBranch + ' | Phase: dependency updates and version management');
1412
+ let pr = null;
1413
+ if (isDryRun) {
1414
+ logger.info('PR_CHECK: Would check for existing pull request | Mode: dry-run | Action: Skip PR lookup');
1415
+ logger.info('PR_ASSUMPTION: Assuming no existing PR found | Mode: dry-run | Purpose: Demo workflow');
1416
+ } else {
1417
+ const branchName = await GitHub.getCurrentBranchName();
1418
+ pr = await GitHub.findOpenPullRequestByHeadRef(branchName);
1419
+ }
1420
+ if (pr) {
1421
+ logger.info(`PR_FOUND: Existing pull request detected for current branch | URL: ${pr.html_url} | Status: open`);
1422
+ } else {
1423
+ var _runConfig_publish4, _runConfig_publish5, _runConfig_publish6, _runConfig_publish7, _runConfig_publish8, _runConfig_publish9, _runConfig_publish10, _runConfig_publish11, _runConfig_publish12, _releaseConfig_release;
1424
+ logger.info('PR_NOT_FOUND: No open pull request exists for current branch | Action: Starting new release publishing process | Next: Prepare dependencies and version');
1425
+ // STEP 1: Prepare for release (update dependencies and run prepublish checks) with NO version bump yet
1426
+ logger.verbose('RELEASE_PREP_STARTING: Preparing for release | Phase: dependency management | Action: Switch from workspace to remote dependencies | Version Bump: Not yet applied');
1427
+ // Clean up any npm link references before updating dependencies
1428
+ logger.verbose('NPM_LINK_CHECK: Scanning package-lock.json for npm link references | File: package-lock.json | Purpose: Remove development symlinks before publish');
1429
+ await cleanupNpmLinkReferences(isDryRun);
1430
+ // Update inter-project dependencies if --update-deps flag is present
1431
+ const updateDepsScope = (_runConfig_publish4 = runConfig.publish) === null || _runConfig_publish4 === void 0 ? void 0 : _runConfig_publish4.updateDeps;
1432
+ if (updateDepsScope) {
1433
+ logger.info(`INTER_PROJECT_DEPS_UPDATE: Updating inter-project dependencies | Scope: ${updateDepsScope} | Type: inter-project | Command: kodrdriv updates`);
1434
+ const Updates = await import('./index-D-RqK3Aj.js');
1435
+ const updatesConfig = {
1436
+ ...runConfig,
1437
+ dryRun: isDryRun,
1438
+ updates: {
1439
+ scope: updateDepsScope,
1440
+ interProject: true
1441
+ }
1442
+ };
1443
+ await Updates.updates(updatesConfig);
1444
+ }
1445
+ logger.verbose('DEPS_UPDATE_REGISTRY: Updating dependencies to latest versions from npm registry | Source: registry | Target: package.json');
1446
+ const updatePatterns = (_runConfig_publish5 = runConfig.publish) === null || _runConfig_publish5 === void 0 ? void 0 : _runConfig_publish5.dependencyUpdatePatterns;
1447
+ if (updatePatterns && updatePatterns.length > 0) {
1448
+ logger.verbose(`DEPS_UPDATE_PATTERNS: Updating dependencies matching specified patterns | Patterns: ${updatePatterns.join(', ')} | Count: ${updatePatterns.length} | Command: npm update`);
1449
+ const patternsArg = updatePatterns.join(' ');
1450
+ await runWithDryRunSupport(`npm update ${patternsArg}`, isDryRun);
1451
+ } else {
1452
+ logger.verbose('DEPS_UPDATE_ALL: No dependency patterns specified, updating all dependencies | Scope: all | Command: npm update');
1453
+ await runWithDryRunSupport('npm update', isDryRun);
1454
+ }
1455
+ logger.info('PREPUBLISH_SCRIPT_RUNNING: Executing prepublishOnly script | Script: prepublishOnly | Purpose: Run pre-flight checks (clean, lint, build, test)');
1456
+ await runWithDryRunSupport('npm run prepublishOnly', isDryRun, {}, true); // Use inherited stdio
1457
+ // STEP 2: Commit dependency updates if any (still no version bump)
1458
+ logger.verbose('DEPS_STAGING: Staging dependency updates for commit | Files: package.json + package-lock.json | Command: git add | Note: Version bump not yet applied');
1459
+ // Check if package-lock.json exists before trying to stage it
1460
+ const packageLockExists = await storage.exists('package-lock.json');
1461
+ const filesToStage = packageLockExists ? 'package.json package-lock.json' : 'package.json';
1462
+ // Wrap git operations with repository lock to prevent .git/index.lock conflicts
1463
+ await runGitWithLock(process.cwd(), async ()=>{
1464
+ await runWithDryRunSupport(`git add ${filesToStage}`, isDryRun);
1465
+ }, 'stage dependency updates');
1466
+ logger.verbose('DEPS_COMMIT_CHECK: Checking for staged dependency updates | Command: git status | Purpose: Determine if commit needed');
1467
+ if (isDryRun) {
1468
+ logger.verbose('DEPS_COMMIT_DRY_RUN: Would create dependency update commit if changes are staged | Mode: dry-run');
1469
+ } else {
1470
+ if (await Diff.hasStagedChanges()) {
1471
+ logger.verbose('DEPS_COMMIT_CREATING: Staged dependency changes detected, creating commit | Files: ' + filesToStage + ' | Action: Execute commit command');
1472
+ // Commit also needs git lock
1473
+ await runGitWithLock(process.cwd(), async ()=>{
1474
+ await Commit.commit(runConfig);
1475
+ }, 'commit dependency updates');
1476
+ } else {
1477
+ logger.verbose('DEPS_COMMIT_SKIPPED: No dependency changes to commit | Files: ' + filesToStage + ' | Action: Skipping commit step');
1478
+ }
1479
+ }
1480
+ // STEP 3: Merge target branch into working branch (optional - now skipped by default since post-publish sync keeps branches in sync)
1481
+ const skipPreMerge = ((_runConfig_publish6 = runConfig.publish) === null || _runConfig_publish6 === void 0 ? void 0 : _runConfig_publish6.skipPrePublishMerge) !== false; // Default to true (skip)
1482
+ if (skipPreMerge) {
1483
+ logger.verbose(`PRE_MERGE_SKIPPED: Skipping pre-publish merge of target branch | Reason: Post-publish sync handles branch synchronization | Target: ${targetBranch} | Config: skipPrePublishMerge=true`);
1484
+ } else {
1485
+ logger.info(`PRE_MERGE_STARTING: Merging target branch into current branch | Target: ${targetBranch} | Purpose: Avoid version conflicts | Phase: pre-publish`);
1486
+ if (isDryRun) {
1487
+ logger.info(`Would merge ${targetBranch} into current branch`);
1488
+ } else {
1489
+ // Wrap entire merge process with git lock (involves fetch, merge, checkout, add, commit)
1490
+ await runGitWithLock(process.cwd(), async ()=>{
1491
+ // Fetch the latest target branch
1492
+ try {
1493
+ await run(`git fetch origin ${targetBranch}:${targetBranch}`);
1494
+ logger.info(`TARGET_BRANCH_FETCHED: Successfully fetched latest target branch | Branch: ${targetBranch} | Remote: origin/${targetBranch} | Purpose: Pre-merge sync`);
1495
+ } catch (fetchError) {
1496
+ logger.warn(`TARGET_BRANCH_FETCH_FAILED: Unable to fetch target branch | Branch: ${targetBranch} | Error: ${fetchError.message} | Impact: Proceeding without merge, PR may have conflicts`);
1497
+ logger.warn('MERGE_SKIPPED_NO_FETCH: Continuing without pre-merge | Reason: Target branch fetch failed | Impact: PR may require manual conflict resolution');
1498
+ }
1499
+ // Check if merge is needed (avoid unnecessary merge commits)
1500
+ try {
1501
+ const { stdout: mergeBase } = await run(`git merge-base HEAD ${targetBranch}`);
1502
+ const { stdout: targetCommit } = await run(`git rev-parse ${targetBranch}`);
1503
+ if (mergeBase.trim() === targetCommit.trim()) {
1504
+ logger.info(`MERGE_NOT_NEEDED: Current branch already up-to-date with target | Branch: ${targetBranch} | Status: in-sync | Action: Skipping merge`);
1505
+ } else {
1506
+ // Try to merge target branch into current branch
1507
+ let mergeSucceeded = false;
1508
+ try {
1509
+ await run(`git merge ${targetBranch} --no-edit -m "Merge ${targetBranch} to sync before version bump"`);
1510
+ logger.info(`MERGE_SUCCESS: Successfully merged target branch into current branch | Target: ${targetBranch} | Purpose: Sync before version bump`);
1511
+ mergeSucceeded = true;
1512
+ } catch (mergeError) {
1513
+ // If merge conflicts occur, check if they're only in version-related files
1514
+ const errorText = [
1515
+ mergeError.message || '',
1516
+ mergeError.stdout || '',
1517
+ mergeError.stderr || ''
1518
+ ].join(' ');
1519
+ if (errorText.includes('CONFLICT')) {
1520
+ logger.warn(`MERGE_CONFLICTS_DETECTED: Merge conflicts found, attempting automatic resolution | Target: ${targetBranch} | Strategy: Auto-resolve version files`);
1521
+ // Get list of conflicted files
1522
+ const { stdout: conflictedFiles } = await run('git diff --name-only --diff-filter=U');
1523
+ const conflicts = conflictedFiles.trim().split('\n').filter(Boolean);
1524
+ logger.verbose(`MERGE_CONFLICTS_LIST: Conflicted files detected | Files: ${conflicts.join(', ')} | Count: ${conflicts.length}`);
1525
+ // Check if conflicts are only in package.json and package-lock.json
1526
+ const versionFiles = [
1527
+ 'package.json',
1528
+ 'package-lock.json'
1529
+ ];
1530
+ const nonVersionConflicts = conflicts.filter((f)=>!versionFiles.includes(f));
1531
+ if (nonVersionConflicts.length > 0) {
1532
+ logger.error(`MERGE_AUTO_RESOLVE_FAILED: Cannot auto-resolve conflicts in non-version files | Files: ${nonVersionConflicts.join(', ')} | Count: ${nonVersionConflicts.length} | Resolution: Manual intervention required`);
1533
+ logger.error('');
1534
+ logger.error('CONFLICT_RESOLUTION_REQUIRED: Manual steps to resolve conflicts:');
1535
+ logger.error(' Step 1: Resolve conflicts in the files listed above');
1536
+ logger.error(' Step 2: Stage resolved files | Command: git add <resolved-files>');
1537
+ logger.error(' Step 3: Complete merge commit | Command: git commit');
1538
+ logger.error(' Step 4: Resume publish process | Command: kodrdriv publish');
1539
+ logger.error('');
1540
+ throw new Error(`Merge conflicts in non-version files. Please resolve manually.`);
1541
+ }
1542
+ // Auto-resolve version conflicts by accepting current branch versions
1543
+ // (keep our working branch's version, which is likely already updated)
1544
+ logger.info(`MERGE_AUTO_RESOLVING: Automatically resolving version conflicts | Strategy: Keep current branch versions | Files: ${versionFiles.join(', ')}`);
1545
+ for (const file of conflicts){
1546
+ if (versionFiles.includes(file)) {
1547
+ await run(`git checkout --ours ${file}`);
1548
+ await run(`git add ${file}`);
1549
+ logger.verbose(`MERGE_FILE_RESOLVED: Resolved file using current branch version | File: ${file} | Strategy: checkout --ours`);
1550
+ }
1551
+ }
1552
+ // Complete the merge
1553
+ await run(`git commit --no-edit -m "Merge ${targetBranch} to sync before version bump (auto-resolved version conflicts)"`);
1554
+ logger.info(`MERGE_AUTO_RESOLVE_SUCCESS: Successfully auto-resolved version conflicts and completed merge | Target: ${targetBranch} | Files: ${versionFiles.join(', ')}`);
1555
+ mergeSucceeded = true;
1556
+ } else {
1557
+ // Not a conflict error, re-throw
1558
+ throw mergeError;
1559
+ }
1560
+ }
1561
+ // Only run npm install if merge actually happened
1562
+ if (mergeSucceeded) {
1563
+ // Run npm install to update package-lock.json based on merged package.json
1564
+ logger.info('POST_MERGE_NPM_INSTALL: Running npm install after merge | Purpose: Update package-lock.json based on merged package.json | Command: npm install');
1565
+ await run('npm install');
1566
+ logger.info('POST_MERGE_NPM_COMPLETE: npm install completed successfully | Status: Dependencies synchronized');
1567
+ // Commit any changes from npm install (e.g., package-lock.json updates)
1568
+ const { stdout: mergeChangesStatus } = await run('git status --porcelain');
1569
+ if (mergeChangesStatus.trim()) {
1570
+ logger.verbose('POST_MERGE_CHANGES_DETECTED: Changes detected after npm install | Action: Staging for commit | Command: git add');
1571
+ // Check if package-lock.json exists before trying to stage it
1572
+ const packageLockExistsPostMerge = await storage.exists('package-lock.json');
1573
+ const filesToStagePostMerge = packageLockExistsPostMerge ? 'package.json package-lock.json' : 'package.json';
1574
+ await run(`git add ${filesToStagePostMerge}`);
1575
+ if (await Diff.hasStagedChanges()) {
1576
+ logger.verbose('POST_MERGE_COMMIT: Committing post-merge changes | Files: ' + filesToStagePostMerge + ' | Purpose: Finalize merge');
1577
+ await Commit.commit(runConfig);
1578
+ }
1579
+ }
1580
+ }
1581
+ }
1582
+ } catch (error) {
1583
+ // Only catch truly unexpected errors here
1584
+ logger.error(`MERGE_UNEXPECTED_ERROR: Unexpected error during merge process | Error: ${error.message} | Target: ${targetBranch} | Action: Aborting publish`);
1585
+ throw error;
1586
+ }
1587
+ }, `merge ${targetBranch} into current branch`);
1588
+ }
1589
+ }
1590
+ // STEP 4: Determine and set target version AFTER checks, dependency commit, and target branch merge
1591
+ logger.info('Determining target version...');
1592
+ let newVersion;
1593
+ if (isDryRun) {
1594
+ logger.info('Would determine target version and update package.json');
1595
+ newVersion = '1.0.0'; // Mock version for dry run
1596
+ } else {
1597
+ var _runConfig_publish13;
1598
+ const packageJsonContents = await storage.readFile('package.json', 'utf-8');
1599
+ const parsed = safeJsonParse(packageJsonContents, 'package.json');
1600
+ const packageJson = validatePackageJson(parsed, 'package.json');
1601
+ const currentVersion = packageJson.version;
1602
+ let proposedVersion;
1603
+ let finalTargetBranch = targetBranch;
1604
+ if (branchDependentVersioning && runConfig.branches) {
1605
+ // Use branch-dependent versioning logic
1606
+ const branchDependentResult = await calculateBranchDependentVersion(currentVersion, currentBranch, runConfig.branches, targetBranch);
1607
+ proposedVersion = branchDependentResult.version;
1608
+ finalTargetBranch = branchDependentResult.targetBranch;
1609
+ logger.info(`VERSION_BRANCH_DEPENDENT_CALCULATED: Branch-dependent version calculated | Current: ${currentVersion} | Proposed: ${proposedVersion} | Strategy: branch-dependent`);
1610
+ logger.info(`TARGET_BRANCH_FINAL: Final target branch determined | Branch: ${finalTargetBranch} | Source: branch-dependent config`);
1611
+ // Update targetBranch for the rest of the function
1612
+ targetBranch = finalTargetBranch;
1613
+ } else {
1614
+ var _runConfig_publish14;
1615
+ // Use existing logic for backward compatibility
1616
+ const targetVersionInput = ((_runConfig_publish14 = runConfig.publish) === null || _runConfig_publish14 === void 0 ? void 0 : _runConfig_publish14.targetVersion) || 'patch';
1617
+ proposedVersion = calculateTargetVersion(currentVersion, targetVersionInput);
1618
+ }
1619
+ const targetTagName = `v${proposedVersion}`;
1620
+ const tagExists = await checkIfTagExists(targetTagName);
1621
+ // Smart tag conflict handling
1622
+ if (tagExists) {
1623
+ const { getNpmPublishedVersion, getTagInfo } = await import('@eldrforge/core');
1624
+ logger.warn(`TAG_ALREADY_EXISTS: Tag already exists in repository | Tag: ${targetTagName} | Status: conflict | Action: Check npm registry`);
1625
+ // Check if this version is published on npm
1626
+ const npmVersion = await getNpmPublishedVersion(packageJson.name);
1627
+ const tagInfo = await getTagInfo(targetTagName);
1628
+ if (npmVersion === proposedVersion) {
1629
+ var _runConfig_publish15;
1630
+ // Version is already published on npm
1631
+ logger.info(`VERSION_ALREADY_PUBLISHED: Version already published on npm registry | Version: ${proposedVersion} | Status: published | Action: Skipping`);
1632
+ logger.info(`PUBLISH_SKIPPED_DUPLICATE: Skipping publish operation | Reason: Package already at target version | Version: ${proposedVersion}`);
1633
+ logger.info('');
1634
+ logger.info('REPUBLISH_OPTIONS: Options if you need to republish:');
1635
+ logger.info(` Option 1: Bump version | Command: npm version patch (or minor/major)`);
1636
+ logger.info(` Option 2: Re-run publish | Command: kodrdriv publish`);
1637
+ logger.info('');
1638
+ if ((_runConfig_publish15 = runConfig.publish) === null || _runConfig_publish15 === void 0 ? void 0 : _runConfig_publish15.skipAlreadyPublished) {
1639
+ logger.info('PUBLISH_SKIPPED_FLAG: Skipping package due to flag | Flag: --skip-already-published | Version: ' + proposedVersion + ' | Status: skipped');
1640
+ // Emit skip marker for tree mode detection
1641
+ // eslint-disable-next-line no-console
1642
+ console.log('KODRDRIV_PUBLISH_SKIPPED');
1643
+ return; // Exit without error
1644
+ } else {
1645
+ throw new Error(`Version ${proposedVersion} already published. Use --skip-already-published to continue.`);
1646
+ }
1647
+ } else {
1648
+ var _tagInfo_commit, _runConfig_publish16;
1649
+ // Tag exists but version not on npm - likely failed previous publish
1650
+ logger.warn('');
1651
+ logger.warn('PUBLISH_SITUATION_ANALYSIS: Analyzing publish conflict situation | Tag: ' + targetTagName + ' | npm: ' + (npmVersion || 'not published'));
1652
+ logger.warn(`PUBLISH_ANALYSIS_TAG_EXISTS: Tag exists locally | Tag: ${targetTagName} | Commit: ${tagInfo === null || tagInfo === void 0 ? void 0 : (_tagInfo_commit = tagInfo.commit) === null || _tagInfo_commit === void 0 ? void 0 : _tagInfo_commit.substring(0, 8)}`);
1653
+ logger.warn(`PUBLISH_ANALYSIS_NPM_STATUS: npm registry status | Version: ${npmVersion || 'not published'} | Status: ${npmVersion ? 'published' : 'missing'}`);
1654
+ logger.warn(`PUBLISH_ANALYSIS_CONCLUSION: Previous publish likely failed after tag creation | Reason: Tag exists but not on npm | Resolution: Recovery needed`);
1655
+ logger.warn('');
1656
+ logger.warn('PUBLISH_RECOVERY_OPTIONS: Recovery options available:');
1657
+ logger.warn(' OPTION_1_FORCE: Force republish by deleting tag | Command: kodrdriv publish --force-republish');
1658
+ logger.warn(' OPTION_2_BUMP: Skip version and bump | Command: npm version patch && kodrdriv publish');
1659
+ logger.warn(' OPTION_3_MANUAL: Manually delete tag:');
1660
+ logger.warn(` Command: git tag -d ${targetTagName}`);
1661
+ logger.warn(` Command: git push origin :refs/tags/${targetTagName}`);
1662
+ logger.warn('');
1663
+ if ((_runConfig_publish16 = runConfig.publish) === null || _runConfig_publish16 === void 0 ? void 0 : _runConfig_publish16.forceRepublish) {
1664
+ logger.info('PUBLISH_FORCE_REPUBLISH: Force republish mode enabled | Action: Deleting existing tag | Tag: ' + targetTagName + ' | Purpose: Allow republish');
1665
+ if (!isDryRun) {
1666
+ const { runSecure } = await import('@eldrforge/git-tools');
1667
+ // Delete local tag
1668
+ try {
1669
+ await runSecure('git', [
1670
+ 'tag',
1671
+ '-d',
1672
+ targetTagName
1673
+ ]);
1674
+ logger.info(`TAG_DELETED_LOCAL: Deleted local tag | Tag: ${targetTagName} | Status: removed-local`);
1675
+ } catch (error) {
1676
+ logger.debug(`Could not delete local tag: ${error.message}`);
1677
+ }
1678
+ // Delete remote tag
1679
+ try {
1680
+ await runSecure('git', [
1681
+ 'push',
1682
+ 'origin',
1683
+ `:refs/tags/${targetTagName}`
1684
+ ]);
1685
+ logger.info(`TAG_DELETED_REMOTE: Deleted remote tag | Tag: ${targetTagName} | Remote: origin | Status: removed-remote`);
1686
+ } catch (error) {
1687
+ logger.debug(`Could not delete remote tag: ${error.message}`);
1688
+ }
1689
+ logger.info('PUBLISH_TAG_CLEANUP_COMPLETE: Tag deleted successfully | Status: ready-for-publish | Next: Continue with publish workflow');
1690
+ } else {
1691
+ logger.info('Would delete tags and continue with publish');
1692
+ }
1693
+ } else {
1694
+ throw new Error(`Tag ${targetTagName} already exists. Use --force-republish to override.`);
1695
+ }
1696
+ }
1697
+ }
1698
+ if ((_runConfig_publish13 = runConfig.publish) === null || _runConfig_publish13 === void 0 ? void 0 : _runConfig_publish13.interactive) {
1699
+ var _runConfig_publish17;
1700
+ newVersion = await confirmVersionInteractively(currentVersion, proposedVersion, (_runConfig_publish17 = runConfig.publish) === null || _runConfig_publish17 === void 0 ? void 0 : _runConfig_publish17.targetVersion);
1701
+ const confirmedTagName = `v${newVersion}`;
1702
+ const confirmedTagExists = await checkIfTagExists(confirmedTagName);
1703
+ if (confirmedTagExists) {
1704
+ var _runConfig_publish18;
1705
+ const { getNpmPublishedVersion } = await import('@eldrforge/core');
1706
+ const npmVersion = await getNpmPublishedVersion(packageJson.name);
1707
+ if (npmVersion === newVersion) {
1708
+ throw new Error(`Tag ${confirmedTagName} already exists and version is published on npm. Please choose a different version.`);
1709
+ } else if (!((_runConfig_publish18 = runConfig.publish) === null || _runConfig_publish18 === void 0 ? void 0 : _runConfig_publish18.forceRepublish)) {
1710
+ throw new Error(`Tag ${confirmedTagName} already exists. Use --force-republish to override.`);
1711
+ }
1712
+ // If forceRepublish is set, we'll continue (tag will be deleted later)
1713
+ }
1714
+ } else {
1715
+ newVersion = proposedVersion;
1716
+ }
1717
+ logger.info(`Bumping version from ${currentVersion} to ${newVersion}`);
1718
+ packageJson.version = newVersion;
1719
+ await storage.writeFile('package.json', JSON.stringify(packageJson, null, 2) + '\n', 'utf-8');
1720
+ logger.info(`Version updated in package.json: ${newVersion}`);
1721
+ }
1722
+ // STEP 5: Commit version bump as a separate commit
1723
+ logger.verbose('Staging version bump for commit');
1724
+ // Check if package-lock.json exists before trying to stage it
1725
+ const packageLockExistsVersionBump = await storage.exists('package-lock.json');
1726
+ const filesToStageVersionBump = packageLockExistsVersionBump ? 'package.json package-lock.json' : 'package.json';
1727
+ // Wrap git operations with lock
1728
+ await runGitWithLock(process.cwd(), async ()=>{
1729
+ await runWithDryRunSupport(`git add ${filesToStageVersionBump}`, isDryRun);
1730
+ }, 'stage version bump');
1731
+ if (isDryRun) {
1732
+ logger.verbose('Would create version bump commit');
1733
+ } else {
1734
+ if (await Diff.hasStagedChanges()) {
1735
+ logger.verbose('Creating version bump commit...');
1736
+ await runGitWithLock(process.cwd(), async ()=>{
1737
+ await Commit.commit(runConfig);
1738
+ }, 'commit version bump');
1739
+ } else {
1740
+ logger.verbose('No version changes to commit.');
1741
+ }
1742
+ }
1743
+ logger.info('Generating release notes...');
1744
+ // Use the existing currentBranch variable for tag detection
1745
+ logger.debug(`Current branch for release notes: ${currentBranch}`);
1746
+ // Create a modified config for release notes generation that includes the publish --from, --interactive, and --from-main options
1747
+ const releaseConfig = {
1748
+ ...runConfig
1749
+ };
1750
+ releaseConfig.release = {
1751
+ ...runConfig.release,
1752
+ currentBranch: currentBranch,
1753
+ ...((_runConfig_publish7 = runConfig.publish) === null || _runConfig_publish7 === void 0 ? void 0 : _runConfig_publish7.from) && {
1754
+ from: runConfig.publish.from
1755
+ },
1756
+ ...((_runConfig_publish8 = runConfig.publish) === null || _runConfig_publish8 === void 0 ? void 0 : _runConfig_publish8.interactive) && {
1757
+ interactive: runConfig.publish.interactive
1758
+ },
1759
+ ...((_runConfig_publish9 = runConfig.publish) === null || _runConfig_publish9 === void 0 ? void 0 : _runConfig_publish9.fromMain) && {
1760
+ fromMain: runConfig.publish.fromMain
1761
+ }
1762
+ };
1763
+ if ((_runConfig_publish10 = runConfig.publish) === null || _runConfig_publish10 === void 0 ? void 0 : _runConfig_publish10.from) {
1764
+ logger.verbose(`Using custom 'from' reference for release notes: ${runConfig.publish.from}`);
1765
+ }
1766
+ if ((_runConfig_publish11 = runConfig.publish) === null || _runConfig_publish11 === void 0 ? void 0 : _runConfig_publish11.interactive) {
1767
+ logger.verbose('Interactive mode enabled for release notes generation');
1768
+ }
1769
+ if ((_runConfig_publish12 = runConfig.publish) === null || _runConfig_publish12 === void 0 ? void 0 : _runConfig_publish12.fromMain) {
1770
+ logger.verbose('Forcing comparison against main branch for release notes');
1771
+ }
1772
+ // Log self-reflection settings for debugging
1773
+ if ((_releaseConfig_release = releaseConfig.release) === null || _releaseConfig_release === void 0 ? void 0 : _releaseConfig_release.selfReflection) {
1774
+ logger.verbose('Self-reflection enabled for release notes generation');
1775
+ }
1776
+ const releaseSummary = await execute$1(releaseConfig);
1777
+ if (isDryRun) {
1778
+ logger.info('Would write release notes to RELEASE_NOTES.md and RELEASE_TITLE.md in output directory');
1779
+ } else {
1780
+ const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
1781
+ await storage.ensureDirectory(outputDirectory);
1782
+ const releaseNotesPath = getOutputPath(outputDirectory, 'RELEASE_NOTES.md');
1783
+ const releaseTitlePath = getOutputPath(outputDirectory, 'RELEASE_TITLE.md');
1784
+ await storage.writeFile(releaseNotesPath, releaseSummary.body, 'utf-8');
1785
+ await storage.writeFile(releaseTitlePath, releaseSummary.title, 'utf-8');
1786
+ logger.info(`Release notes and title generated and saved to ${releaseNotesPath} and ${releaseTitlePath}.`);
1787
+ }
1788
+ logger.info('Pushing to origin...');
1789
+ // Get current branch name and push explicitly to avoid pushing to wrong remote/branch
1790
+ const branchName = await GitHub.getCurrentBranchName();
1791
+ // Wrap git push with lock
1792
+ await runGitWithLock(process.cwd(), async ()=>{
1793
+ await runWithDryRunSupport(`git push origin ${branchName}`, isDryRun);
1794
+ }, `push ${branchName}`);
1795
+ logger.info('Creating pull request...');
1796
+ if (isDryRun) {
1797
+ logger.info('Would get commit title and create PR with GitHub API');
1798
+ pr = {
1799
+ number: 123,
1800
+ html_url: 'https://github.com/mock/repo/pull/123',
1801
+ labels: []
1802
+ };
1803
+ } else {
1804
+ const { stdout: rawCommitTitle } = await run('git log -1 --pretty=%B');
1805
+ // Apply stop-context filtering to PR title and body
1806
+ const commitTitle = filterContent(rawCommitTitle, runConfig.stopContext).filtered;
1807
+ const prBody = filterContent('Automated release PR.', runConfig.stopContext).filtered;
1808
+ pr = await GitHub.createPullRequest(commitTitle, prBody, branchName, targetBranch);
1809
+ if (!pr) {
1810
+ throw new Error('Failed to create pull request.');
1811
+ }
1812
+ logger.info(`Pull request created: ${pr.html_url} (${branchName} → ${targetBranch})`);
1813
+ }
1814
+ }
1815
+ logger.info(`Waiting for PR #${pr.number} checks to complete...`);
1816
+ if (!isDryRun) {
1817
+ // Check if we already know from prechecks that no workflows will trigger
1818
+ let shouldSkipWait = false;
1819
+ try {
1820
+ // TODO: Re-enable when checkWorkflowConfiguration is exported from github-tools
1821
+ // const workflowConfig = await GitHub.checkWorkflowConfiguration(targetBranch);
1822
+ const workflowConfig = {
1823
+ hasWorkflows: true,
1824
+ hasPullRequestTriggers: true,
1825
+ workflowCount: 0,
1826
+ triggeredWorkflowNames: []
1827
+ };
1828
+ if (!workflowConfig.hasWorkflows || !workflowConfig.hasPullRequestTriggers) ;
1829
+ } catch (error) {
1830
+ // If we can't verify, proceed with waiting to be safe
1831
+ logger.debug(`Could not verify workflow configuration for wait skip: ${error.message}`);
1832
+ }
1833
+ if (!shouldSkipWait) {
1834
+ var _runConfig_publish19, _runConfig_publish20, _runConfig_publish21;
1835
+ // Configure timeout and user confirmation behavior
1836
+ const timeout = ((_runConfig_publish19 = runConfig.publish) === null || _runConfig_publish19 === void 0 ? void 0 : _runConfig_publish19.checksTimeout) || KODRDRIV_DEFAULTS.publish.checksTimeout;
1837
+ const senditMode = ((_runConfig_publish20 = runConfig.publish) === null || _runConfig_publish20 === void 0 ? void 0 : _runConfig_publish20.sendit) || false;
1838
+ // sendit flag overrides skipUserConfirmation - if sendit is true, skip confirmation
1839
+ const skipUserConfirmation = senditMode || ((_runConfig_publish21 = runConfig.publish) === null || _runConfig_publish21 === void 0 ? void 0 : _runConfig_publish21.skipUserConfirmation) || false;
1840
+ await GitHub.waitForPullRequestChecks(pr.number, {
1841
+ timeout,
1842
+ skipUserConfirmation
1843
+ });
1844
+ }
1845
+ }
1846
+ const mergeMethod = ((_runConfig_publish2 = runConfig.publish) === null || _runConfig_publish2 === void 0 ? void 0 : _runConfig_publish2.mergeMethod) || 'squash';
1847
+ if (isDryRun) {
1848
+ logger.info(`Would merge PR #${pr.number} using ${mergeMethod} method`);
1849
+ } else {
1850
+ try {
1851
+ await GitHub.mergePullRequest(pr.number, mergeMethod, false); // Don't delete branch
1852
+ } catch (error) {
1853
+ // Check if this is a merge conflict error
1854
+ if (error.message && (error.message.includes('not mergeable') || error.message.includes('Pull Request is not mergeable') || error.message.includes('merge conflict'))) {
1855
+ logger.error(`PR_MERGE_CONFLICTS: Pull request has merge conflicts | PR Number: ${pr.number} | Status: conflicts | Resolution: Manual intervention required`);
1856
+ logger.error('');
1857
+ logger.error('PR_CONFLICT_RESOLUTION: Steps to resolve conflicts:');
1858
+ logger.error(` Step 1: Visit pull request | URL: ${pr.html_url}`);
1859
+ logger.error(' Step 2: Resolve merge conflicts | Method: GitHub UI or local');
1860
+ logger.error(' Step 3: Re-run publish command | Command: kodrdriv publish');
1861
+ logger.error('');
1862
+ logger.error('PR_AUTO_CONTINUE: Command will auto-detect existing PR | Behavior: Continues from where it left off | No re-creation needed');
1863
+ throw new Error(`Merge conflicts detected in PR #${pr.number}. Please resolve conflicts and re-run the command.`);
1864
+ } else {
1865
+ // Re-throw other merge errors
1866
+ throw error;
1867
+ }
1868
+ }
1869
+ }
1870
+ // Switch to target branch and pull latest changes
1871
+ logger.info(`Checking out target branch: ${targetBranch}...`);
1872
+ // Check for uncommitted changes and stash them if necessary
1873
+ let hasStashedChanges = false;
1874
+ if (!isDryRun) {
1875
+ const { stdout: statusOutput } = await runSecure('git', [
1876
+ 'status',
1877
+ '--porcelain'
1878
+ ]);
1879
+ if (statusOutput.trim()) {
1880
+ logger.info('PUBLISH_STASH_SAVING: Stashing uncommitted changes before checkout | Command: git stash push | Purpose: Protect changes during branch switch');
1881
+ await runSecure('git', [
1882
+ 'stash',
1883
+ 'push',
1884
+ '-m',
1885
+ 'kodrdriv: stash before checkout target branch'
1886
+ ]);
1887
+ hasStashedChanges = true;
1888
+ logger.info('PUBLISH_STASH_SUCCESS: Successfully stashed uncommitted changes | Status: saved | Name: kodrdriv stash');
1889
+ }
1890
+ }
1891
+ try {
1892
+ // Wrap git checkout and pull with lock
1893
+ await runGitWithLock(process.cwd(), async ()=>{
1894
+ await runWithDryRunSupport(`git checkout ${targetBranch}`, isDryRun);
1895
+ }, `checkout ${targetBranch}`);
1896
+ // Sync target branch with remote to avoid conflicts during PR creation
1897
+ if (!isDryRun) {
1898
+ logger.info(`PUBLISH_TARGET_SYNCING: Syncing target branch with remote | Branch: ${targetBranch} | Remote: origin | Purpose: Avoid PR conflicts`);
1899
+ try {
1900
+ const remoteExists = await run(`git ls-remote --exit-code --heads origin ${targetBranch}`).then(()=>true).catch(()=>false);
1901
+ if (remoteExists) {
1902
+ await runGitWithLock(process.cwd(), async ()=>{
1903
+ await run(`git fetch origin ${targetBranch}`);
1904
+ await run(`git merge origin/${targetBranch} --no-edit`);
1905
+ }, `sync ${targetBranch}`);
1906
+ logger.info(`PUBLISH_TARGET_SYNCED: Successfully synced target with remote | Branch: ${targetBranch} | Remote: origin | Status: in-sync`);
1907
+ } else {
1908
+ logger.info(`PUBLISH_TARGET_NO_REMOTE: No remote target branch found | Branch: ${targetBranch} | Remote: origin | Action: Will be created on first push`);
1909
+ }
1910
+ } catch (syncError) {
1911
+ if (syncError.message && syncError.message.includes('CONFLICT')) {
1912
+ logger.error(`PUBLISH_SYNC_CONFLICTS: Merge conflicts during target sync | Branch: ${targetBranch} | Remote: origin | Status: conflicts-detected`);
1913
+ logger.error(`PUBLISH_SYNC_RESOLUTION: Manual conflict resolution steps:`);
1914
+ logger.error(` Step 1: Checkout target | Command: git checkout ${targetBranch}`);
1915
+ logger.error(` Step 2: Pull and merge | Command: git pull origin ${targetBranch}`);
1916
+ logger.error(` Step 3: Resolve conflicts in files`);
1917
+ logger.error(` Step 4: Stage resolved files | Command: git add <resolved-files>`);
1918
+ logger.error(` Step 5: Complete merge | Command: git commit`);
1919
+ logger.error(` Step 6: Return to branch | Command: git checkout ${currentBranch}`);
1920
+ logger.error(` Step 7: Resume publish | Command: kodrdriv publish`);
1921
+ throw syncError;
1922
+ } else {
1923
+ logger.warn(`PUBLISH_SYNC_WARNING: Could not sync target with remote | Branch: ${targetBranch} | Remote: origin | Error: ${syncError.message}`);
1924
+ // Continue with publish process, but log the warning
1925
+ }
1926
+ }
1927
+ } else {
1928
+ logger.info(`Would sync ${targetBranch} with remote to avoid PR conflicts`);
1929
+ }
1930
+ } catch (error) {
1931
+ // Check if this is a merge conflict or sync issue
1932
+ if (!isDryRun && (error.message.includes('conflict') || error.message.includes('CONFLICT') || error.message.includes('diverged') || error.message.includes('non-fast-forward'))) {
1933
+ logger.error(`PUBLISH_TARGET_SYNC_FAILED: Failed to sync target branch with remote | Branch: ${targetBranch} | Remote: origin | Impact: Cannot proceed safely`);
1934
+ logger.error('');
1935
+ logger.error('PUBLISH_SYNC_RECOVERY_OPTIONS: Available recovery options:');
1936
+ logger.error(` OPTION_1_AUTO: Attempt automatic resolution | Command: kodrdriv publish --sync-target`);
1937
+ logger.error(` OPTION_2_MANUAL: Manually resolve conflicts:`);
1938
+ logger.error(` Step 1: Checkout target | Command: git checkout ${targetBranch}`);
1939
+ logger.error(` Step 2: Pull from remote | Command: git pull origin ${targetBranch}`);
1940
+ logger.error(` Step 3: Resolve conflicts and commit`);
1941
+ logger.error(` Step 4: Re-run publish | Command: kodrdriv publish`);
1942
+ logger.error('');
1943
+ logger.error('PUBLISH_STOPPED_SAFETY: Publish process stopped | Reason: Prevent data loss | Status: safe-to-recover');
1944
+ throw new Error(`Target branch '${targetBranch}' sync failed. Use recovery options above to resolve.`);
1945
+ } else {
1946
+ // Re-throw other errors
1947
+ throw error;
1948
+ }
1949
+ }
1950
+ // Restore stashed changes if we stashed them
1951
+ if (hasStashedChanges) {
1952
+ logger.info('PUBLISH_STASH_RESTORING: Restoring previously stashed changes | Command: git stash pop | Purpose: Restore working directory state');
1953
+ try {
1954
+ await runSecure('git', [
1955
+ 'stash',
1956
+ 'pop'
1957
+ ]);
1958
+ logger.info('PUBLISH_STASH_RESTORED: Successfully restored stashed changes | Status: restored | Stash: removed');
1959
+ } catch (stashError) {
1960
+ logger.warn(`PUBLISH_STASH_RESTORE_FAILED: Could not restore stashed changes | Error: ${stashError.message} | Impact: Changes still in stash`);
1961
+ logger.warn('PUBLISH_STASH_AVAILABLE: Changes available in git stash | Command: git stash list | Purpose: View and restore manually');
1962
+ }
1963
+ }
1964
+ // Now create and push the tag on the target branch
1965
+ logger.info('Creating release tag...');
1966
+ let tagName;
1967
+ if (isDryRun) {
1968
+ logger.info('Would read package.json version and create git tag');
1969
+ tagName = 'v1.0.0'; // Mock version for dry run
1970
+ } else {
1971
+ const packageJsonContents = await storage.readFile('package.json', 'utf-8');
1972
+ const { version } = safeJsonParse(packageJsonContents, 'package.json');
1973
+ tagName = `v${version}`;
1974
+ // Check if tag already exists locally
1975
+ try {
1976
+ // Validate tag name to prevent injection
1977
+ if (!validateGitRef(tagName)) {
1978
+ throw new Error(`Invalid tag name: ${tagName}`);
1979
+ }
1980
+ const { stdout } = await runSecure('git', [
1981
+ 'tag',
1982
+ '-l',
1983
+ tagName
1984
+ ]);
1985
+ if (stdout.trim() === tagName) {
1986
+ logger.info(`Tag ${tagName} already exists locally, skipping tag creation`);
1987
+ } else {
1988
+ await runGitWithLock(process.cwd(), async ()=>{
1989
+ await runSecure('git', [
1990
+ 'tag',
1991
+ tagName
1992
+ ]);
1993
+ }, `create tag ${tagName}`);
1994
+ logger.info(`Created local tag: ${tagName}`);
1995
+ }
1996
+ } catch (error) {
1997
+ // If git tag -l fails, create the tag anyway
1998
+ await runGitWithLock(process.cwd(), async ()=>{
1999
+ await runSecure('git', [
2000
+ 'tag',
2001
+ tagName
2002
+ ]);
2003
+ }, `create tag ${tagName}`);
2004
+ logger.info(`Created local tag: ${tagName}`);
2005
+ }
2006
+ // Check if tag exists on remote before pushing
2007
+ let tagWasPushed = false;
2008
+ try {
2009
+ const { stdout } = await runSecure('git', [
2010
+ 'ls-remote',
2011
+ 'origin',
2012
+ `refs/tags/${tagName}`
2013
+ ]);
2014
+ if (stdout.trim()) {
2015
+ logger.info(`Tag ${tagName} already exists on remote, skipping push`);
2016
+ } else {
2017
+ await runGitWithLock(process.cwd(), async ()=>{
2018
+ await runSecure('git', [
2019
+ 'push',
2020
+ 'origin',
2021
+ tagName
2022
+ ]);
2023
+ }, `push tag ${tagName}`);
2024
+ logger.info(`Pushed tag to remote: ${tagName}`);
2025
+ tagWasPushed = true;
2026
+ }
2027
+ } catch (error) {
2028
+ // If ls-remote fails, try to push anyway (might be a new remote)
2029
+ try {
2030
+ await runSecure('git', [
2031
+ 'push',
2032
+ 'origin',
2033
+ tagName
2034
+ ]);
2035
+ logger.info(`Pushed tag to remote: ${tagName}`);
2036
+ tagWasPushed = true;
2037
+ } catch (pushError) {
2038
+ if (pushError.message && pushError.message.includes('already exists')) {
2039
+ logger.info(`Tag ${tagName} already exists on remote, continuing...`);
2040
+ } else {
2041
+ throw pushError;
2042
+ }
2043
+ }
2044
+ }
2045
+ // If we just pushed a new tag, wait for GitHub to process it
2046
+ if (tagWasPushed) {
2047
+ logger.verbose('Waiting for GitHub to process the pushed tag...');
2048
+ await new Promise((resolve)=>setTimeout(resolve, 5000)); // 5 second delay
2049
+ }
2050
+ }
2051
+ logger.info('Creating GitHub release...');
2052
+ if (isDryRun) {
2053
+ var _runConfig_publish22;
2054
+ logger.info('Would read package.json version and create GitHub release with retry logic');
2055
+ const milestonesEnabled = !((_runConfig_publish22 = runConfig.publish) === null || _runConfig_publish22 === void 0 ? void 0 : _runConfig_publish22.noMilestones);
2056
+ if (milestonesEnabled) {
2057
+ logger.info('Would close milestone for released version');
2058
+ } else {
2059
+ logger.info('Would skip milestone closure (--no-milestones)');
2060
+ }
2061
+ } else {
2062
+ const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
2063
+ const releaseNotesPath = getOutputPath(outputDirectory, 'RELEASE_NOTES.md');
2064
+ const releaseTitlePath = getOutputPath(outputDirectory, 'RELEASE_TITLE.md');
2065
+ const releaseNotesContent = await storage.readFile(releaseNotesPath, 'utf-8');
2066
+ const releaseTitle = await storage.readFile(releaseTitlePath, 'utf-8');
2067
+ // Create release with retry logic to handle GitHub tag processing delays
2068
+ let retries = 3;
2069
+ while(retries > 0){
2070
+ try {
2071
+ var _runConfig_publish23;
2072
+ await GitHub.createRelease(tagName, releaseTitle, releaseNotesContent);
2073
+ logger.info(`GitHub release created successfully for tag: ${tagName}`);
2074
+ // Close milestone for this version if enabled
2075
+ const milestonesEnabled = !((_runConfig_publish23 = runConfig.publish) === null || _runConfig_publish23 === void 0 ? void 0 : _runConfig_publish23.noMilestones);
2076
+ if (milestonesEnabled) {
2077
+ logger.info('PUBLISH_MILESTONE_CLOSING: Closing milestone for released version | Action: Close GitHub milestone | Purpose: Mark release complete');
2078
+ const version = tagName.replace(/^v/, ''); // Remove 'v' prefix if present
2079
+ await GitHub.closeMilestoneForVersion(version);
2080
+ } else {
2081
+ logger.debug('Milestone integration disabled via --no-milestones');
2082
+ }
2083
+ break; // Success - exit retry loop
2084
+ } catch (error) {
2085
+ // Check if this is a tag-not-found error that we can retry
2086
+ const isTagNotFoundError = error.message && (error.message.includes('not found') || error.message.includes('does not exist') || error.message.includes('Reference does not exist'));
2087
+ if (isTagNotFoundError && retries > 1) {
2088
+ logger.verbose(`Tag ${tagName} not yet available on GitHub, retrying in 3 seconds... (${retries - 1} retries left)`);
2089
+ await new Promise((resolve)=>setTimeout(resolve, 3000));
2090
+ retries--;
2091
+ } else if (isTagNotFoundError) {
2092
+ // Tag not found error and we're out of retries
2093
+ throw new Error(`Tag ${tagName} was not found on GitHub after ${3 - retries + 1} attempts. This may indicate a problem with tag creation or GitHub synchronization.`);
2094
+ } else {
2095
+ // Not a tag-not-found error - re-throw the original error
2096
+ throw error;
2097
+ }
2098
+ }
2099
+ }
2100
+ }
2101
+ // Wait for release workflows to complete (if enabled)
2102
+ const waitForWorkflows = ((_runConfig_publish3 = runConfig.publish) === null || _runConfig_publish3 === void 0 ? void 0 : _runConfig_publish3.waitForReleaseWorkflows) !== false; // default to true
2103
+ if (waitForWorkflows) {
2104
+ logger.info('Waiting for release workflows...');
2105
+ if (isDryRun) {
2106
+ logger.info('Would monitor GitHub Actions workflows triggered by release');
2107
+ } else {
2108
+ var _runConfig_publish24, _runConfig_publish25, _runConfig_publish26, _runConfig_publish27;
2109
+ const workflowTimeout = ((_runConfig_publish24 = runConfig.publish) === null || _runConfig_publish24 === void 0 ? void 0 : _runConfig_publish24.releaseWorkflowsTimeout) || KODRDRIV_DEFAULTS.publish.releaseWorkflowsTimeout;
2110
+ const senditMode = ((_runConfig_publish25 = runConfig.publish) === null || _runConfig_publish25 === void 0 ? void 0 : _runConfig_publish25.sendit) || false;
2111
+ const skipUserConfirmation = senditMode || ((_runConfig_publish26 = runConfig.publish) === null || _runConfig_publish26 === void 0 ? void 0 : _runConfig_publish26.skipUserConfirmation) || false;
2112
+ // Get workflow names - either from config or auto-detect
2113
+ let workflowNames = (_runConfig_publish27 = runConfig.publish) === null || _runConfig_publish27 === void 0 ? void 0 : _runConfig_publish27.releaseWorkflowNames;
2114
+ if (!workflowNames || workflowNames.length === 0) {
2115
+ logger.info('No specific workflow names configured, auto-detecting workflows triggered by release events...');
2116
+ try {
2117
+ workflowNames = await GitHub.getWorkflowsTriggeredByRelease();
2118
+ if (workflowNames.length === 0) {
2119
+ logger.info('No workflows found that are triggered by release events.');
2120
+ } else {
2121
+ logger.info(`Auto-detected release workflows: ${workflowNames.join(', ')}`);
2122
+ }
2123
+ } catch (error) {
2124
+ logger.warn(`Failed to auto-detect release workflows: ${error.message}`);
2125
+ workflowNames = undefined; // Fall back to monitoring all workflows
2126
+ }
2127
+ }
2128
+ await GitHub.waitForReleaseWorkflows(tagName, {
2129
+ timeout: workflowTimeout,
2130
+ workflowNames,
2131
+ skipUserConfirmation
2132
+ });
2133
+ }
2134
+ } else {
2135
+ logger.verbose('Skipping waiting for release workflows (disabled in config).');
2136
+ }
2137
+ // Switch back to source branch and sync with target
2138
+ logger.info('');
2139
+ logger.info(`PUBLISH_POST_SYNC: Syncing source branch with target after publish | Purpose: Keep branches synchronized | Strategy: Reset and force push`);
2140
+ await runWithDryRunSupport(`git checkout ${currentBranch}`, isDryRun);
2141
+ if (!isDryRun) {
2142
+ // Sync target into source
2143
+ // Note: With squash merging, fast-forward will fail because commit histories diverge
2144
+ if (mergeMethod === 'squash') {
2145
+ // For squash merges, reset to target branch to avoid conflicts
2146
+ // The squash merge created a single commit on target that represents all source commits
2147
+ logger.info(`Resetting ${currentBranch} to ${targetBranch} (squash merge)...`);
2148
+ await run(`git reset --hard ${targetBranch}`);
2149
+ logger.info(`PUBLISH_BRANCH_RESET: Reset source branch to target | Source: ${currentBranch} | Target: ${targetBranch} | Status: synchronized`);
2150
+ // After squash merge and reset, we need to force push
2151
+ // This is safe because we just merged to main and are syncing working branch
2152
+ logger.info(`PUBLISH_FORCE_PUSHING: Force pushing synchronized branch | Branch: ${currentBranch} | Remote: origin | Purpose: Complete post-publish sync`);
2153
+ try {
2154
+ // Verify that remote working branch is ancestor of main (safety check)
2155
+ try {
2156
+ await run(`git fetch origin ${currentBranch}`);
2157
+ await run(`git merge-base --is-ancestor origin/${currentBranch} ${targetBranch}`);
2158
+ logger.verbose(`✓ Safety check passed: origin/${currentBranch} is ancestor of ${targetBranch}`);
2159
+ } catch {
2160
+ // Remote branch might not exist yet, or already in sync - both OK
2161
+ logger.verbose(`Remote ${currentBranch} does not exist or is already synced`);
2162
+ }
2163
+ // Use --force-with-lease for safer force push
2164
+ await run(`git push --force-with-lease origin ${currentBranch}`);
2165
+ logger.info(`PUBLISH_FORCE_PUSH_SUCCESS: Successfully force pushed to remote | Branch: ${currentBranch} | Remote: origin | Status: synchronized`);
2166
+ } catch (pushError) {
2167
+ // If force push fails, provide helpful message
2168
+ logger.warn(`PUBLISH_FORCE_PUSH_FAILED: Could not force push branch | Branch: ${currentBranch} | Remote: origin | Error: ${pushError.message}`);
2169
+ logger.warn(`PUBLISH_MANUAL_PUSH_NEEDED: Manual force push required | Action: Push manually`);
2170
+ logger.warn(`PUBLISH_MANUAL_PUSH_COMMAND: Force push command | Command: git push --force-with-lease origin ${currentBranch}`);
2171
+ }
2172
+ } else {
2173
+ // For merge/rebase methods, try to merge target back into source
2174
+ logger.info(`PUBLISH_MERGE_TARGET_BACK: Merging target back into source | Target: ${targetBranch} | Source: ${currentBranch} | Purpose: Sync branches after publish`);
2175
+ // Try fast-forward first (works with merge/rebase methods)
2176
+ // Use runSecure to avoid error output for expected failure
2177
+ let fastForwardSucceeded = false;
2178
+ try {
2179
+ await runSecure('git', [
2180
+ 'merge',
2181
+ targetBranch,
2182
+ '--ff-only'
2183
+ ]);
2184
+ fastForwardSucceeded = true;
2185
+ logger.info(`PUBLISH_MERGE_FF_SUCCESS: Fast-forward merged target into source | Target: ${targetBranch} | Source: ${currentBranch} | Status: merged`);
2186
+ } catch {
2187
+ logger.verbose(`Fast-forward merge not possible, performing regular merge...`);
2188
+ }
2189
+ if (!fastForwardSucceeded) {
2190
+ await run(`git merge ${targetBranch} --no-edit`);
2191
+ logger.info(`PUBLISH_MERGE_SUCCESS: Merged target into source | Target: ${targetBranch} | Source: ${currentBranch} | Status: merged`);
2192
+ }
2193
+ }
2194
+ // Determine version bump based on branch configuration
2195
+ let versionCommand = 'prepatch'; // Default
2196
+ let versionTag = 'dev'; // Default
2197
+ if (branchDependentVersioning && runConfig.branches) {
2198
+ const sourceBranchConfig = runConfig.branches[currentBranch];
2199
+ if (sourceBranchConfig === null || sourceBranchConfig === void 0 ? void 0 : sourceBranchConfig.version) {
2200
+ // Use configured version strategy for source branch
2201
+ if (sourceBranchConfig.version.incrementLevel) {
2202
+ versionCommand = `pre${sourceBranchConfig.version.incrementLevel}`;
2203
+ }
2204
+ if (sourceBranchConfig.version.tag) {
2205
+ versionTag = sourceBranchConfig.version.tag;
2206
+ }
2207
+ }
2208
+ }
2209
+ // Bump to next development version
2210
+ logger.info(`PUBLISH_DEV_VERSION_BUMPING: Bumping to next development version | Command: ${versionCommand} | Tag: ${versionTag} | Purpose: Prepare for next cycle`);
2211
+ try {
2212
+ const { stdout: newVersion } = await run(`npm version ${versionCommand} --preid=${versionTag}`);
2213
+ logger.info(`PUBLISH_DEV_VERSION_BUMPED: Version bumped successfully | New Version: ${newVersion.trim()} | Type: development | Status: completed`);
2214
+ } catch (versionError) {
2215
+ logger.warn(`PUBLISH_DEV_VERSION_BUMP_FAILED: Failed to bump version | Error: ${versionError.message} | Impact: Version not updated`);
2216
+ logger.warn('PUBLISH_MANUAL_VERSION_BUMP: Manual version bump may be needed | Action: Bump manually for next cycle | Command: npm version');
2217
+ }
2218
+ // Push updated source branch
2219
+ logger.info(`PUBLISH_PUSH_SOURCE: Pushing updated source branch | Branch: ${currentBranch} | Remote: origin | Purpose: Push development version`);
2220
+ try {
2221
+ await runGitWithLock(process.cwd(), async ()=>{
2222
+ await run(`git push origin ${currentBranch}`);
2223
+ }, `push ${currentBranch}`);
2224
+ logger.info(`PUBLISH_PUSH_SOURCE_SUCCESS: Pushed source branch successfully | Branch: ${currentBranch} | Remote: origin | Status: pushed`);
2225
+ } catch (pushError) {
2226
+ logger.warn(`PUBLISH_PUSH_SOURCE_FAILED: Failed to push source branch | Branch: ${currentBranch} | Error: ${pushError.message} | Impact: Need manual push`);
2227
+ logger.warn(`PUBLISH_MANUAL_PUSH_COMMAND: Manual push command | Command: git push origin ${currentBranch}`);
2228
+ }
2229
+ } else {
2230
+ logger.info(`PUBLISH_MERGE_DRY_RUN: Would merge target into source | Mode: dry-run | Target: ${targetBranch} | Source: ${currentBranch} | Strategy: ff-only`);
2231
+ logger.info(`PUBLISH_VERSION_DRY_RUN: Would bump version to next development | Mode: dry-run | Action: Version bump`);
2232
+ logger.info(`PUBLISH_PUSH_DRY_RUN: Would push source to remote | Mode: dry-run | Branch: ${currentBranch} | Remote: origin`);
2233
+ }
2234
+ logger.info('');
2235
+ logger.info(`PUBLISH_COMPLETE: Publish workflow completed successfully | Branch: ${currentBranch} | Status: completed | Version: next-development`);
2236
+ };
2237
+
2238
+ export { execute$2 as development, execute as publish, execute$1 as release };
2239
+ //# sourceMappingURL=index.js.map