refactor: Enhance session management and error handling in AgentService and related components

- Improved session handling by implementing ensureSession to load sessions from disk if not in memory, reducing "session not found" errors.
- Enhanced error messages for non-existent sessions, providing clearer diagnostics.
- Updated CodexProvider and OpencodeProvider to improve error handling and messaging.
- Refactored various routes to use async/await for better readability and error handling.
- Added event emission for merge and stash operations in the MergeService and StashService.
- Cleaned up error messages in AgentExecutor to remove redundant prefixes and ANSI codes for better clarity.
This commit is contained in:
gsxdsm
2026-02-18 17:30:12 -08:00
parent 6903d3c508
commit df9a6314da
22 changed files with 827 additions and 148 deletions

View File

@@ -5,7 +5,7 @@
*/
import { createLogger } from '@automaker/utils';
import { createEventEmitter } from '../lib/events.js';
import { type EventEmitter } from '../lib/events.js';
import { execGitCommand } from '../lib/git.js';
const logger = createLogger('MergeService');
@@ -52,10 +52,9 @@ export async function performMerge(
branchName: string,
worktreePath: string,
targetBranch: string = 'main',
options?: MergeOptions
options?: MergeOptions,
emitter?: EventEmitter
): Promise<MergeServiceResult> {
const emitter = createEventEmitter();
if (!projectPath || !branchName || !worktreePath) {
return {
success: false,
@@ -100,7 +99,7 @@ export async function performMerge(
}
// Emit merge:start after validating inputs
emitter.emit('merge:start', { branchName, targetBranch: mergeTo, worktreePath });
emitter?.emit('merge:start', { branchName, targetBranch: mergeTo, worktreePath });
// Merge the feature branch into the target branch (using safe array-based commands)
const mergeMessage = options?.message || `Merge ${branchName} into ${mergeTo}`;
@@ -134,7 +133,7 @@ export async function performMerge(
}
// Emit merge:conflict event with conflict details
emitter.emit('merge:conflict', { branchName, targetBranch: mergeTo, conflictFiles });
emitter?.emit('merge:conflict', { branchName, targetBranch: mergeTo, conflictFiles });
return {
success: false,
@@ -145,7 +144,7 @@ export async function performMerge(
}
// Emit merge:error for non-conflict errors before re-throwing
emitter.emit('merge:error', {
emitter?.emit('merge:error', {
branchName,
targetBranch: mergeTo,
error: err.message || String(mergeError),
@@ -196,7 +195,7 @@ export async function performMerge(
}
// Emit merge:success with merged branch, target branch, and deletion info
emitter.emit('merge:success', {
emitter?.emit('merge:success', {
mergedBranch: branchName,
targetBranch: mergeTo,
deleted: options?.deleteWorktreeAndBranch ? { worktreeDeleted, branchDeleted } : undefined,