@@ -50,16 +50,18 @@ export default class TaskManagerWorker extends Worker {
5050 public async start ( ) : Promise < void > {
5151 await this . accountsDb . connect ( ) ;
5252 await this . eventsDb . connect ( ) ;
53- await super . start ( ) ;
53+
54+ // await super.start();
55+ this . handle ( { type : 'auto-task-creation' } )
5456 }
5557
5658 /**
5759 * Finish everything
5860 */
5961 public async finish ( ) : Promise < void > {
60- await super . finish ( ) ;
6162 await this . accountsDb . close ( ) ;
6263 await this . eventsDb . close ( ) ;
64+ await super . finish ( ) ;
6365 }
6466
6567 /**
@@ -208,60 +210,105 @@ export default class TaskManagerWorker extends Worker {
208210 const eventsToProcess = events . slice ( 0 , remainingBudget ) ;
209211
210212 for ( const event of eventsToProcess ) {
211- /**
212- * Atomically increment usage.autoTasksCreated
213- */
214- const incrementSuccess = await this . incrementAutoTasksCreated ( projectId , dayStartUtc ) ;
213+ await this . processEventForAutoTaskCreation ( {
214+ project,
215+ projectId,
216+ taskManager,
217+ event,
218+ dayStartUtc,
219+ } ) ;
220+ }
221+ }
215222
216- if ( ! incrementSuccess ) {
217- this . logger . warn ( `Failed to increment usage for project ${ projectId } , budget may be exhausted` ) ;
223+ /**
224+ * Process a single event for auto task creation
225+ *
226+ * @param params - method params
227+ * @param params.project - project
228+ * @param params.projectId - project id
229+ * @param params.taskManager - task manager config
230+ * @param params.event - grouped event
231+ * @param params.dayStartUtc - day start UTC used for usage increment
232+ */
233+ private async processEventForAutoTaskCreation ( params : {
234+ project : ProjectDBScheme ;
235+ projectId : string ;
236+ taskManager : ProjectTaskManagerConfig ;
237+ event : GroupedEventDBScheme ;
238+ dayStartUtc : Date ;
239+ } ) : Promise < void > {
240+ const { project, projectId, taskManager, event, dayStartUtc } = params ;
218241
219- break ;
220- }
242+ /**
243+ * Format Issue data from event
244+ */
245+ const issueData = formatIssueFromEvent ( event , project ) ;
221246
222- /**
223- * Format Issue data from event
224- */
225- const issueData = formatIssueFromEvent ( event , project ) ;
247+ /**
248+ * Create GitHub Issue
249+ */
250+ let githubIssue : { number : number ; html_url : string } | null = null ;
226251
227- /**
228- * Create GitHub Issue
229- */
230- const githubIssue = await this . githubService . createIssue (
252+ try {
253+ githubIssue = await this . githubService . createIssue (
231254 taskManager . config . repoFullName ,
232255 taskManager . config . installationId ,
233256 issueData
234257 ) ;
258+ } catch ( error ) {
259+ this . logger . error ( `Failed to create GitHub issue for event ${ event . groupHash } (project ${ projectId } ):` , error ) ;
235260
236261 /**
237- * Assign Copilot if enabled
262+ * Do not increment usage and do not save taskManagerItem if issue creation failed
238263 */
239- if ( taskManager . assignAgent ) {
240- try {
241- await this . githubService . assignCopilot (
242- taskManager . config . repoFullName ,
243- githubIssue . number ,
244- taskManager . config . installationId
245- ) ;
246- } catch ( error ) {
247- /**
248- * Log error but don't fail the task creation
249- */
250- this . logger . warn ( `Failed to assign Copilot to issue #${ githubIssue . number } :` , error ) ;
251- }
252- }
264+ return ;
265+ }
266+
267+ /**
268+ * Atomically increment usage.autoTasksCreated (only after successful issue creation)
269+ */
270+ const incrementSuccess = await this . incrementAutoTasksCreated ( projectId , dayStartUtc ) ;
271+
272+ if ( ! incrementSuccess ) {
273+ this . logger . warn (
274+ `Issue #${ githubIssue . number } was created but usage increment failed for project ${ projectId } (budget may be exhausted)`
275+ ) ;
253276
254277 /**
255- * Save taskManagerItem to event
278+ * We still link the created issue to the event to avoid duplicates.
256279 */
257- await this . saveTaskManagerItem ( projectId , event , githubIssue . number , taskManager , githubIssue . html_url ) ;
280+ }
258281
259- this . logger . info ( `Created task for event ${ event . groupHash } in project ${ projectId } ` , {
260- issueNumber : githubIssue . number ,
261- issueUrl : githubIssue . html_url ,
262- assignAgent : taskManager . assignAgent ,
263- } ) ;
282+ this . logger . verbose ( `Project ${ projectId } has Copilot assigning ${ taskManager . assignAgent ? 'enabled' : 'disabled' } ` )
283+
284+ /**
285+ * Assign Copilot if enabled
286+ */
287+ if ( taskManager . assignAgent ) {
288+ try {
289+ await this . githubService . assignCopilot (
290+ taskManager . config . repoFullName ,
291+ githubIssue . number ,
292+ taskManager . config . installationId
293+ ) ;
294+ } catch ( error ) {
295+ /**
296+ * Log error but don't fail the task creation
297+ */
298+ this . logger . warn ( `Failed to assign Copilot to issue #${ githubIssue . number } :` , error ) ;
299+ }
264300 }
301+
302+ /**
303+ * Save taskManagerItem to event
304+ */
305+ await this . saveTaskManagerItem ( projectId , event , githubIssue . number , taskManager , githubIssue . html_url ) ;
306+
307+ this . logger . info ( `Created task for event ${ event . groupHash } in project ${ projectId } ` , {
308+ issueNumber : githubIssue . number ,
309+ issueUrl : githubIssue . html_url ,
310+ assignAgent : taskManager . assignAgent ,
311+ } ) ;
265312 }
266313
267314 /**
0 commit comments