From 5137a60e70237b59983057f916bea25e55675090 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 21:47:34 +0100 Subject: [PATCH 1/9] feat: add quality_review task status and qualityReview initiative flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds two new fields to the database and propagates them through the repository layer: - Task status enum gains 'quality_review' (between in_progress and completed), enabling a QA gate before tasks are marked complete. - initiatives.quality_review (INTEGER DEFAULT 0) lets an initiative be flagged for quality-review workflow without a data migration (existing rows default to false). Includes: - Schema changes in schema.ts - Migration 0037 (ALTER TABLE initiatives ADD quality_review) - Snapshot chain repaired: deleted stale 0036 snapshot, fixed 0035 prevId to create a linear chain (0032 → 0034 → 0035), then generated clean 0037 snapshot - Repository adapter already uses SELECT * / spread-update pattern so no adapter code changes were needed - Initiative and task repository tests extended with qualityReview / quality_review_status describe blocks (7 new tests) - docs/database.md updated Co-Authored-By: Claude Sonnet 4.6 --- .../repositories/drizzle/initiative.test.ts | 28 + .../db/repositories/drizzle/task.test.ts | 34 + apps/server/db/schema.ts | 3 +- .../0037_worthless_princess_powerful.sql | 1 + apps/server/drizzle/meta/0035_snapshot.json | 9 +- ...{0036_snapshot.json => 0037_snapshot.json} | 1208 ++++++++++++++--- apps/server/drizzle/meta/_journal.json | 9 +- docs/database.md | 4 +- 8 files changed, 1103 insertions(+), 193 deletions(-) create mode 100644 apps/server/drizzle/0037_worthless_princess_powerful.sql rename apps/server/drizzle/meta/{0036_snapshot.json => 0037_snapshot.json} (53%) diff --git a/apps/server/db/repositories/drizzle/initiative.test.ts b/apps/server/db/repositories/drizzle/initiative.test.ts index 2d9fe32..3b2cf4a 100644 --- a/apps/server/db/repositories/drizzle/initiative.test.ts +++ b/apps/server/db/repositories/drizzle/initiative.test.ts @@ -147,4 +147,32 @@ describe('DrizzleInitiativeRepository', () => { expect(archived[0].name).toBe('Archived'); }); }); + + describe('qualityReview', () => { + it('defaults to false on create', async () => { + const initiative = await repo.create({ name: 'QR Test' }); + expect(initiative.qualityReview).toBe(false); + }); + + it('can be set to true via update', async () => { + const created = await repo.create({ name: 'QR Toggle' }); + const updated = await repo.update(created.id, { qualityReview: true }); + expect(updated.qualityReview).toBe(true); + }); + + it('round-trips through findById', async () => { + const created = await repo.create({ name: 'QR Round-trip' }); + await repo.update(created.id, { qualityReview: true }); + const found = await repo.findById(created.id); + expect(found!.qualityReview).toBe(true); + }); + + it('round-trips false value', async () => { + const created = await repo.create({ name: 'QR False' }); + await repo.update(created.id, { qualityReview: true }); + await repo.update(created.id, { qualityReview: false }); + const found = await repo.findById(created.id); + expect(found!.qualityReview).toBe(false); + }); + }); }); diff --git a/apps/server/db/repositories/drizzle/task.test.ts b/apps/server/db/repositories/drizzle/task.test.ts index 5f19065..1126e36 100644 --- a/apps/server/db/repositories/drizzle/task.test.ts +++ b/apps/server/db/repositories/drizzle/task.test.ts @@ -196,4 +196,38 @@ describe('DrizzleTaskRepository', () => { ); }); }); + + describe('quality_review status', () => { + it('can set status to quality_review', async () => { + const task = await taskRepo.create({ + phaseId: testPhaseId, + name: 'QR Status Task', + order: 99, + }); + const updated = await taskRepo.update(task.id, { status: 'quality_review' }); + expect(updated.status).toBe('quality_review'); + }); + + it('round-trips quality_review through findById', async () => { + const task = await taskRepo.create({ + phaseId: testPhaseId, + name: 'QR Round-trip Task', + order: 100, + }); + await taskRepo.update(task.id, { status: 'quality_review' }); + const found = await taskRepo.findById(task.id); + expect(found!.status).toBe('quality_review'); + }); + + it('can transition from quality_review to completed', async () => { + const task = await taskRepo.create({ + phaseId: testPhaseId, + name: 'QR to Completed', + order: 101, + }); + await taskRepo.update(task.id, { status: 'quality_review' }); + const completed = await taskRepo.update(task.id, { status: 'completed' }); + expect(completed.status).toBe('completed'); + }); + }); }); diff --git a/apps/server/db/schema.ts b/apps/server/db/schema.ts index ce35cec..c6cd84a 100644 --- a/apps/server/db/schema.ts +++ b/apps/server/db/schema.ts @@ -26,6 +26,7 @@ export const initiatives = sqliteTable('initiatives', { executionMode: text('execution_mode', { enum: ['yolo', 'review_per_phase'] }) .notNull() .default('review_per_phase'), + qualityReview: integer('quality_review', { mode: 'boolean' }).notNull().default(false), createdAt: integer('created_at', { mode: 'timestamp' }).notNull(), updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(), }); @@ -151,7 +152,7 @@ export const tasks = sqliteTable('tasks', { .notNull() .default('medium'), status: text('status', { - enum: ['pending', 'in_progress', 'completed', 'blocked'], + enum: ['pending', 'in_progress', 'quality_review', 'completed', 'blocked'], }) .notNull() .default('pending'), diff --git a/apps/server/drizzle/0037_worthless_princess_powerful.sql b/apps/server/drizzle/0037_worthless_princess_powerful.sql new file mode 100644 index 0000000..402633f --- /dev/null +++ b/apps/server/drizzle/0037_worthless_princess_powerful.sql @@ -0,0 +1 @@ +ALTER TABLE `initiatives` ADD `quality_review` integer DEFAULT false NOT NULL; \ No newline at end of file diff --git a/apps/server/drizzle/meta/0035_snapshot.json b/apps/server/drizzle/meta/0035_snapshot.json index d735a97..ace4c45 100644 --- a/apps/server/drizzle/meta/0035_snapshot.json +++ b/apps/server/drizzle/meta/0035_snapshot.json @@ -2,7 +2,7 @@ "version": "6", "dialect": "sqlite", "id": "c84e499f-7df8-4091-b2a5-6b12847898bd", - "prevId": "5fbe1151-1dfb-4b0c-a7fa-2177369543fd", + "prevId": "443071fe-31d6-498a-9f4a-4a3ff24a46fc", "tables": { "accounts": { "name": "accounts", @@ -238,6 +238,13 @@ "notNull": false, "autoincrement": false }, + "prompt": { + "name": "prompt", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, "exit_code": { "name": "exit_code", "type": "integer", diff --git a/apps/server/drizzle/meta/0036_snapshot.json b/apps/server/drizzle/meta/0037_snapshot.json similarity index 53% rename from apps/server/drizzle/meta/0036_snapshot.json rename to apps/server/drizzle/meta/0037_snapshot.json index f60484b..b710946 100644 --- a/apps/server/drizzle/meta/0036_snapshot.json +++ b/apps/server/drizzle/meta/0037_snapshot.json @@ -1,8 +1,8 @@ { - "id": "f85b9df3-dead-4c46-90ac-cf36bcaa6eb4", - "prevId": "c0b6d7d3-c9da-440a-9fb8-9dd88df5672a", "version": "6", "dialect": "sqlite", + "id": "ef1d4ce7-a9c4-4e86-9cac-2e9cbbd0e688", + "prevId": "c84e499f-7df8-4091-b2a5-6b12847898bd", "tables": { "accounts": { "name": "accounts", @@ -29,11 +29,18 @@ "autoincrement": false, "default": "'claude'" }, - "config_dir": { - "name": "config_dir", + "config_json": { + "name": "config_json", "type": "text", "primaryKey": false, - "notNull": true, + "notNull": false, + "autoincrement": false + }, + "credentials": { + "name": "credentials", + "type": "text", + "primaryKey": false, + "notNull": false, "autoincrement": false }, "is_exhausted": { @@ -87,6 +94,67 @@ "uniqueConstraints": {}, "checkConstraints": {} }, + "agent_log_chunks": { + "name": "agent_log_chunks", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "agent_id": { + "name": "agent_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "agent_name": { + "name": "agent_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "session_number": { + "name": "session_number", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 1 + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "agent_log_chunks_agent_id_idx": { + "name": "agent_log_chunks_agent_id_idx", + "columns": [ + "agent_id" + ], + "isUnique": false + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, "agents": { "name": "agents", "columns": { @@ -170,6 +238,13 @@ "notNull": false, "autoincrement": false }, + "exit_code": { + "name": "exit_code", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, "prompt": { "name": "prompt", "type": "text", @@ -211,6 +286,13 @@ "primaryKey": false, "notNull": true, "autoincrement": false + }, + "user_dismissed_at": { + "name": "user_dismissed_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false } }, "indexes": { @@ -226,41 +308,715 @@ "agents_task_id_tasks_id_fk": { "name": "agents_task_id_tasks_id_fk", "tableFrom": "agents", + "tableTo": "tasks", "columnsFrom": [ "task_id" ], - "tableTo": "tasks", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "set null", + "onUpdate": "no action" }, "agents_initiative_id_initiatives_id_fk": { "name": "agents_initiative_id_initiatives_id_fk", "tableFrom": "agents", + "tableTo": "initiatives", "columnsFrom": [ "initiative_id" ], - "tableTo": "initiatives", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "set null", + "onUpdate": "no action" }, "agents_account_id_accounts_id_fk": { "name": "agents_account_id_accounts_id_fk", "tableFrom": "agents", + "tableTo": "accounts", "columnsFrom": [ "account_id" ], - "tableTo": "accounts", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "change_set_entries": { + "name": "change_set_entries", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "change_set_id": { + "name": "change_set_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "entity_type": { + "name": "entity_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "entity_id": { + "name": "entity_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "action": { + "name": "action", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "previous_state": { + "name": "previous_state", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "new_state": { + "name": "new_state", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "change_set_entries_change_set_id_idx": { + "name": "change_set_entries_change_set_id_idx", + "columns": [ + "change_set_id" + ], + "isUnique": false + } + }, + "foreignKeys": { + "change_set_entries_change_set_id_change_sets_id_fk": { + "name": "change_set_entries_change_set_id_change_sets_id_fk", + "tableFrom": "change_set_entries", + "tableTo": "change_sets", + "columnsFrom": [ + "change_set_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "change_sets": { + "name": "change_sets", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "agent_id": { + "name": "agent_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "agent_name": { + "name": "agent_name", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "initiative_id": { + "name": "initiative_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "mode": { + "name": "mode", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "summary": { + "name": "summary", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'applied'" + }, + "reverted_at": { + "name": "reverted_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "change_sets_initiative_id_idx": { + "name": "change_sets_initiative_id_idx", + "columns": [ + "initiative_id" + ], + "isUnique": false + } + }, + "foreignKeys": { + "change_sets_agent_id_agents_id_fk": { + "name": "change_sets_agent_id_agents_id_fk", + "tableFrom": "change_sets", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "change_sets_initiative_id_initiatives_id_fk": { + "name": "change_sets_initiative_id_initiatives_id_fk", + "tableFrom": "change_sets", + "tableTo": "initiatives", + "columnsFrom": [ + "initiative_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "chat_messages": { + "name": "chat_messages", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "chat_session_id": { + "name": "chat_session_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "content": { + "name": "content", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "change_set_id": { + "name": "change_set_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "chat_messages_session_id_idx": { + "name": "chat_messages_session_id_idx", + "columns": [ + "chat_session_id" + ], + "isUnique": false + } + }, + "foreignKeys": { + "chat_messages_chat_session_id_chat_sessions_id_fk": { + "name": "chat_messages_chat_session_id_chat_sessions_id_fk", + "tableFrom": "chat_messages", + "tableTo": "chat_sessions", + "columnsFrom": [ + "chat_session_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "chat_messages_change_set_id_change_sets_id_fk": { + "name": "chat_messages_change_set_id_change_sets_id_fk", + "tableFrom": "chat_messages", + "tableTo": "change_sets", + "columnsFrom": [ + "change_set_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "chat_sessions": { + "name": "chat_sessions", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "target_type": { + "name": "target_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "target_id": { + "name": "target_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "initiative_id": { + "name": "initiative_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "agent_id": { + "name": "agent_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'active'" + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "chat_sessions_target_idx": { + "name": "chat_sessions_target_idx", + "columns": [ + "target_type", + "target_id" + ], + "isUnique": false + }, + "chat_sessions_initiative_id_idx": { + "name": "chat_sessions_initiative_id_idx", + "columns": [ + "initiative_id" + ], + "isUnique": false + } + }, + "foreignKeys": { + "chat_sessions_initiative_id_initiatives_id_fk": { + "name": "chat_sessions_initiative_id_initiatives_id_fk", + "tableFrom": "chat_sessions", + "tableTo": "initiatives", + "columnsFrom": [ + "initiative_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "chat_sessions_agent_id_agents_id_fk": { + "name": "chat_sessions_agent_id_agents_id_fk", + "tableFrom": "chat_sessions", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "conversations": { + "name": "conversations", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "from_agent_id": { + "name": "from_agent_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "to_agent_id": { + "name": "to_agent_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "initiative_id": { + "name": "initiative_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "phase_id": { + "name": "phase_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "task_id": { + "name": "task_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "question": { + "name": "question", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "answer": { + "name": "answer", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'pending'" + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "conversations_to_agent_status_idx": { + "name": "conversations_to_agent_status_idx", + "columns": [ + "to_agent_id", + "status" + ], + "isUnique": false + }, + "conversations_from_agent_idx": { + "name": "conversations_from_agent_idx", + "columns": [ + "from_agent_id" + ], + "isUnique": false + } + }, + "foreignKeys": { + "conversations_from_agent_id_agents_id_fk": { + "name": "conversations_from_agent_id_agents_id_fk", + "tableFrom": "conversations", + "tableTo": "agents", + "columnsFrom": [ + "from_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "conversations_to_agent_id_agents_id_fk": { + "name": "conversations_to_agent_id_agents_id_fk", + "tableFrom": "conversations", + "tableTo": "agents", + "columnsFrom": [ + "to_agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "conversations_initiative_id_initiatives_id_fk": { + "name": "conversations_initiative_id_initiatives_id_fk", + "tableFrom": "conversations", + "tableTo": "initiatives", + "columnsFrom": [ + "initiative_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "conversations_phase_id_phases_id_fk": { + "name": "conversations_phase_id_phases_id_fk", + "tableFrom": "conversations", + "tableTo": "phases", + "columnsFrom": [ + "phase_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "conversations_task_id_tasks_id_fk": { + "name": "conversations_task_id_tasks_id_fk", + "tableFrom": "conversations", + "tableTo": "tasks", + "columnsFrom": [ + "task_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, + "errands": { + "name": "errands", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "description": { + "name": "description", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "branch": { + "name": "branch", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "base_branch": { + "name": "base_branch", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'main'" + }, + "agent_id": { + "name": "agent_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "project_id": { + "name": "project_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'active'" + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": {}, + "foreignKeys": { + "errands_agent_id_agents_id_fk": { + "name": "errands_agent_id_agents_id_fk", + "tableFrom": "errands", + "tableTo": "agents", + "columnsFrom": [ + "agent_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "errands_project_id_projects_id_fk": { + "name": "errands_project_id_projects_id_fk", + "tableFrom": "errands", + "tableTo": "projects", + "columnsFrom": [ + "project_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -313,28 +1069,28 @@ "initiative_projects_initiative_id_initiatives_id_fk": { "name": "initiative_projects_initiative_id_initiatives_id_fk", "tableFrom": "initiative_projects", + "tableTo": "initiatives", "columnsFrom": [ "initiative_id" ], - "tableTo": "initiatives", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" }, "initiative_projects_project_id_projects_id_fk": { "name": "initiative_projects_project_id_projects_id_fk", "tableFrom": "initiative_projects", + "tableTo": "projects", "columnsFrom": [ "project_id" ], - "tableTo": "projects", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -358,13 +1114,6 @@ "notNull": true, "autoincrement": false }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false, - "autoincrement": false - }, "status": { "name": "status", "type": "text", @@ -373,21 +1122,29 @@ "autoincrement": false, "default": "'active'" }, - "merge_requires_approval": { - "name": "merge_requires_approval", - "type": "integer", - "primaryKey": false, - "notNull": true, - "autoincrement": false, - "default": true - }, - "merge_target": { - "name": "merge_target", + "branch": { + "name": "branch", "type": "text", "primaryKey": false, "notNull": false, "autoincrement": false }, + "execution_mode": { + "name": "execution_mode", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'review_per_phase'" + }, + "quality_review": { + "name": "quality_review", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, "created_at": { "name": "created_at", "type": "integer", @@ -505,41 +1262,41 @@ "messages_sender_id_agents_id_fk": { "name": "messages_sender_id_agents_id_fk", "tableFrom": "messages", + "tableTo": "agents", "columnsFrom": [ "sender_id" ], - "tableTo": "agents", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "set null", + "onUpdate": "no action" }, "messages_recipient_id_agents_id_fk": { "name": "messages_recipient_id_agents_id_fk", "tableFrom": "messages", + "tableTo": "agents", "columnsFrom": [ "recipient_id" ], - "tableTo": "agents", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "set null", + "onUpdate": "no action" }, "messages_parent_message_id_messages_id_fk": { "name": "messages_parent_message_id_messages_id_fk", "tableFrom": "messages", + "tableTo": "messages", "columnsFrom": [ "parent_message_id" ], - "tableTo": "messages", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "set null" + "onDelete": "set null", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -612,28 +1369,28 @@ "pages_initiative_id_initiatives_id_fk": { "name": "pages_initiative_id_initiatives_id_fk", "tableFrom": "pages", + "tableTo": "initiatives", "columnsFrom": [ "initiative_id" ], - "tableTo": "initiatives", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" }, "pages_parent_page_id_pages_id_fk": { "name": "pages_parent_page_id_pages_id_fk", "tableFrom": "pages", + "tableTo": "pages", "columnsFrom": [ "parent_page_id" ], - "tableTo": "pages", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -677,28 +1434,28 @@ "phase_dependencies_phase_id_phases_id_fk": { "name": "phase_dependencies_phase_id_phases_id_fk", "tableFrom": "phase_dependencies", + "tableTo": "phases", "columnsFrom": [ "phase_id" ], - "tableTo": "phases", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" }, "phase_dependencies_depends_on_phase_id_phases_id_fk": { "name": "phase_dependencies_depends_on_phase_id_phases_id_fk", "tableFrom": "phase_dependencies", + "tableTo": "phases", "columnsFrom": [ "depends_on_phase_id" ], - "tableTo": "phases", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -722,13 +1479,6 @@ "notNull": true, "autoincrement": false }, - "number": { - "name": "number", - "type": "integer", - "primaryKey": false, - "notNull": true, - "autoincrement": false - }, "name": { "name": "name", "type": "text", @@ -736,8 +1486,8 @@ "notNull": true, "autoincrement": false }, - "description": { - "name": "description", + "content": { + "name": "content", "type": "text", "primaryKey": false, "notNull": false, @@ -751,6 +1501,13 @@ "autoincrement": false, "default": "'pending'" }, + "merge_base": { + "name": "merge_base", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, "created_at": { "name": "created_at", "type": "integer", @@ -771,96 +1528,15 @@ "phases_initiative_id_initiatives_id_fk": { "name": "phases_initiative_id_initiatives_id_fk", "tableFrom": "phases", + "tableTo": "initiatives", "columnsFrom": [ "initiative_id" ], - "tableTo": "initiatives", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" - } - }, - "compositePrimaryKeys": {}, - "uniqueConstraints": {}, - "checkConstraints": {} - }, - "plans": { - "name": "plans", - "columns": { - "id": { - "name": "id", - "type": "text", - "primaryKey": true, - "notNull": true, - "autoincrement": false - }, - "phase_id": { - "name": "phase_id", - "type": "text", - "primaryKey": false, - "notNull": true, - "autoincrement": false - }, - "number": { - "name": "number", - "type": "integer", - "primaryKey": false, - "notNull": true, - "autoincrement": false - }, - "name": { - "name": "name", - "type": "text", - "primaryKey": false, - "notNull": true, - "autoincrement": false - }, - "description": { - "name": "description", - "type": "text", - "primaryKey": false, - "notNull": false, - "autoincrement": false - }, - "status": { - "name": "status", - "type": "text", - "primaryKey": false, - "notNull": true, - "autoincrement": false, - "default": "'pending'" - }, - "created_at": { - "name": "created_at", - "type": "integer", - "primaryKey": false, - "notNull": true, - "autoincrement": false - }, - "updated_at": { - "name": "updated_at", - "type": "integer", - "primaryKey": false, - "notNull": true, - "autoincrement": false - } - }, - "indexes": {}, - "foreignKeys": { - "plans_phase_id_phases_id_fk": { - "name": "plans_phase_id_phases_id_fk", - "tableFrom": "plans", - "columnsFrom": [ - "phase_id" - ], - "tableTo": "phases", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -891,6 +1567,21 @@ "notNull": true, "autoincrement": false }, + "default_branch": { + "name": "default_branch", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'main'" + }, + "last_fetched_at": { + "name": "last_fetched_at", + "type": "integer", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, "created_at": { "name": "created_at", "type": "integer", @@ -927,6 +1618,137 @@ "uniqueConstraints": {}, "checkConstraints": {} }, + "review_comments": { + "name": "review_comments", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true, + "autoincrement": false + }, + "phase_id": { + "name": "phase_id", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "file_path": { + "name": "file_path", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "line_number": { + "name": "line_number", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "line_type": { + "name": "line_type", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "body": { + "name": "body", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "author": { + "name": "author", + "type": "text", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": "'you'" + }, + "parent_comment_id": { + "name": "parent_comment_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, + "resolved": { + "name": "resolved", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + }, + "updated_at": { + "name": "updated_at", + "type": "integer", + "primaryKey": false, + "notNull": true, + "autoincrement": false + } + }, + "indexes": { + "review_comments_phase_id_idx": { + "name": "review_comments_phase_id_idx", + "columns": [ + "phase_id" + ], + "isUnique": false + }, + "review_comments_parent_id_idx": { + "name": "review_comments_parent_id_idx", + "columns": [ + "parent_comment_id" + ], + "isUnique": false + } + }, + "foreignKeys": { + "review_comments_phase_id_phases_id_fk": { + "name": "review_comments_phase_id_phases_id_fk", + "tableFrom": "review_comments", + "tableTo": "phases", + "columnsFrom": [ + "phase_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "review_comments_parent_comment_id_review_comments_id_fk": { + "name": "review_comments_parent_comment_id_review_comments_id_fk", + "tableFrom": "review_comments", + "tableTo": "review_comments", + "columnsFrom": [ + "parent_comment_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "checkConstraints": {} + }, "task_dependencies": { "name": "task_dependencies", "columns": { @@ -964,28 +1786,28 @@ "task_dependencies_task_id_tasks_id_fk": { "name": "task_dependencies_task_id_tasks_id_fk", "tableFrom": "task_dependencies", + "tableTo": "tasks", "columnsFrom": [ "task_id" ], - "tableTo": "tasks", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" }, "task_dependencies_depends_on_task_id_tasks_id_fk": { "name": "task_dependencies_depends_on_task_id_tasks_id_fk", "tableFrom": "task_dependencies", + "tableTo": "tasks", "columnsFrom": [ "depends_on_task_id" ], - "tableTo": "tasks", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -1002,13 +1824,6 @@ "notNull": true, "autoincrement": false }, - "plan_id": { - "name": "plan_id", - "type": "text", - "primaryKey": false, - "notNull": false, - "autoincrement": false - }, "phase_id": { "name": "phase_id", "type": "text", @@ -1023,6 +1838,13 @@ "notNull": false, "autoincrement": false }, + "parent_task_id": { + "name": "parent_task_id", + "type": "text", + "primaryKey": false, + "notNull": false, + "autoincrement": false + }, "name": { "name": "name", "type": "text", @@ -1069,15 +1891,23 @@ "autoincrement": false, "default": "'pending'" }, - "requires_approval": { - "name": "requires_approval", + "order": { + "name": "order", "type": "integer", "primaryKey": false, + "notNull": true, + "autoincrement": false, + "default": 0 + }, + "summary": { + "name": "summary", + "type": "text", + "primaryKey": false, "notNull": false, "autoincrement": false }, - "order": { - "name": "order", + "retry_count": { + "name": "retry_count", "type": "integer", "primaryKey": false, "notNull": true, @@ -1101,44 +1931,44 @@ }, "indexes": {}, "foreignKeys": { - "tasks_plan_id_plans_id_fk": { - "name": "tasks_plan_id_plans_id_fk", - "tableFrom": "tasks", - "columnsFrom": [ - "plan_id" - ], - "tableTo": "plans", - "columnsTo": [ - "id" - ], - "onUpdate": "no action", - "onDelete": "cascade" - }, "tasks_phase_id_phases_id_fk": { "name": "tasks_phase_id_phases_id_fk", "tableFrom": "tasks", + "tableTo": "phases", "columnsFrom": [ "phase_id" ], - "tableTo": "phases", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" }, "tasks_initiative_id_initiatives_id_fk": { "name": "tasks_initiative_id_initiatives_id_fk", "tableFrom": "tasks", + "tableTo": "initiatives", "columnsFrom": [ "initiative_id" ], - "tableTo": "initiatives", "columnsTo": [ "id" ], - "onUpdate": "no action", - "onDelete": "cascade" + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tasks_parent_task_id_tasks_id_fk": { + "name": "tasks_parent_task_id_tasks_id_fk", + "tableFrom": "tasks", + "tableTo": "tasks", + "columnsFrom": [ + "parent_task_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" } }, "compositePrimaryKeys": {}, @@ -1149,9 +1979,9 @@ "views": {}, "enums": {}, "_meta": { - "columns": {}, "schemas": {}, - "tables": {} + "tables": {}, + "columns": {} }, "internal": { "indexes": {} diff --git a/apps/server/drizzle/meta/_journal.json b/apps/server/drizzle/meta/_journal.json index a58f2cf..1f15581 100644 --- a/apps/server/drizzle/meta/_journal.json +++ b/apps/server/drizzle/meta/_journal.json @@ -260,6 +260,13 @@ "when": 1772798869413, "tag": "0036_icy_silvermane", "breakpoints": true + }, + { + "idx": 37, + "version": "6", + "when": 1772829916655, + "tag": "0037_worthless_princess_powerful", + "breakpoints": true } ] -} +} \ No newline at end of file diff --git a/docs/database.md b/docs/database.md index 3d7ccd5..f877251 100644 --- a/docs/database.md +++ b/docs/database.md @@ -20,6 +20,8 @@ All adapters use nanoid() for IDs, auto-manage timestamps, and use Drizzle's `.r | name | text NOT NULL | | | status | text enum | 'active' \| 'pending_review' \| 'completed' \| 'archived', default 'active' | | branch | text nullable | auto-generated initiative branch (e.g., 'cw/user-auth') | +| executionMode | text enum | 'yolo' \| 'review_per_phase', default 'review_per_phase' | +| qualityReview | integer (boolean) | default false; flags initiative for quality review workflow | | createdAt, updatedAt | integer/timestamp | | ### phases @@ -48,7 +50,7 @@ All adapters use nanoid() for IDs, auto-manage timestamps, and use Drizzle's `.r | type | text enum | 'auto' | | category | text enum | 'execute' \| 'research' \| 'discuss' \| 'plan' \| 'detail' \| 'refine' \| 'verify' \| 'merge' \| 'review' | | priority | text enum | 'low' \| 'medium' \| 'high' | -| status | text enum | 'pending' \| 'in_progress' \| 'completed' \| 'blocked' | +| status | text enum | 'pending' \| 'in_progress' \| 'quality_review' \| 'completed' \| 'blocked' | | order | integer | default 0 | | summary | text nullable | Agent result summary — propagated to dependent tasks as context | | retryCount | integer NOT NULL | default 0, incremented on agent crash auto-retry, reset on manual retry | From bb770407db993ec6bf81002630caa371bb517269 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 21:53:58 +0100 Subject: [PATCH 2/9] feat: expose qualityReview via updateInitiativeConfig tRPC mutation Adds qualityReview: z.boolean().optional() to the updateInitiativeConfig input schema so the field passes through to the repository layer. Includes integration tests verifying set-true, set-false, and omit-preserves-existing round-trip behavior. Co-Authored-By: Claude Sonnet 4.6 --- apps/server/trpc/routers/initiative.test.ts | 97 +++++++++++++++++++++ apps/server/trpc/routers/initiative.ts | 1 + 2 files changed, 98 insertions(+) create mode 100644 apps/server/trpc/routers/initiative.test.ts diff --git a/apps/server/trpc/routers/initiative.test.ts b/apps/server/trpc/routers/initiative.test.ts new file mode 100644 index 0000000..13c24b2 --- /dev/null +++ b/apps/server/trpc/routers/initiative.test.ts @@ -0,0 +1,97 @@ +/** + * Integration tests for initiative tRPC router — qualityReview field. + * + * Verifies that updateInitiativeConfig accepts and persists qualityReview. + */ + +import { describe, it, expect, vi } from 'vitest'; +import { router, publicProcedure, createCallerFactory } from '../trpc.js'; +import { initiativeProcedures } from './initiative.js'; +import type { TRPCContext } from '../context.js'; +import { createTestDatabase } from '../../db/repositories/drizzle/test-helpers.js'; +import { DrizzleInitiativeRepository } from '../../db/repositories/drizzle/index.js'; + +// ============================================================================= +// Mock ensureProjectClone — prevents actual git cloning +// ============================================================================= + +vi.mock('../../git/project-clones.js', () => ({ + ensureProjectClone: vi.fn().mockResolvedValue('/fake/clone/path'), + getProjectCloneDir: vi.fn().mockReturnValue('repos/fake-project-id'), +})); + +// ============================================================================= +// Test router +// ============================================================================= + +const testRouter = router({ + ...initiativeProcedures(publicProcedure), +}); + +const createCaller = createCallerFactory(testRouter); + +// ============================================================================= +// Setup helper +// ============================================================================= + +function createMockEventBus(): TRPCContext['eventBus'] { + return { + emit: vi.fn(), + on: vi.fn(), + off: vi.fn(), + once: vi.fn(), + }; +} + +async function setup() { + const db = createTestDatabase(); + const initiativeRepo = new DrizzleInitiativeRepository(db); + const ctx: TRPCContext = { + eventBus: createMockEventBus(), + serverStartedAt: null, + processCount: 0, + initiativeRepository: initiativeRepo, + }; + const caller = createCaller(ctx); + const initiative = await initiativeRepo.create({ name: 'Test Initiative' }); + return { caller, initiativeRepo, initiative }; +} + +// ============================================================================= +// Tests +// ============================================================================= + +describe('updateInitiativeConfig — qualityReview', () => { + it('sets qualityReview to true', async () => { + const { caller, initiative } = await setup(); + const result = await caller.updateInitiativeConfig({ + initiativeId: initiative.id, + qualityReview: true, + }); + expect(result.qualityReview).toBe(true); + }); + + it('sets qualityReview to false', async () => { + const { caller, initiative, initiativeRepo } = await setup(); + // First set it to true + await initiativeRepo.update(initiative.id, { qualityReview: true }); + // Now flip it back + const result = await caller.updateInitiativeConfig({ + initiativeId: initiative.id, + qualityReview: false, + }); + expect(result.qualityReview).toBe(false); + }); + + it('does not change qualityReview when omitted', async () => { + const { caller, initiative, initiativeRepo } = await setup(); + // Set to true first + await initiativeRepo.update(initiative.id, { qualityReview: true }); + // Update without qualityReview + const result = await caller.updateInitiativeConfig({ + initiativeId: initiative.id, + executionMode: 'yolo', + }); + expect(result.qualityReview).toBe(true); // unchanged + }); +}); diff --git a/apps/server/trpc/routers/initiative.ts b/apps/server/trpc/routers/initiative.ts index 0077ad9..1696565 100644 --- a/apps/server/trpc/routers/initiative.ts +++ b/apps/server/trpc/routers/initiative.ts @@ -221,6 +221,7 @@ export function initiativeProcedures(publicProcedure: ProcedureBuilder) { initiativeId: z.string().min(1), executionMode: z.enum(['yolo', 'review_per_phase']).optional(), branch: z.string().nullable().optional(), + qualityReview: z.boolean().optional(), })) .mutation(async ({ ctx, input }) => { const repo = requireInitiativeRepository(ctx); From 1416e6bf62fd2f1eeb50c506f09f3fbffbd03b14 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 21:54:17 +0100 Subject: [PATCH 3/9] docs: update server-api.md to include qualityReview in updateInitiativeConfig Co-Authored-By: Claude Sonnet 4.6 --- docs/server-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/server-api.md b/docs/server-api.md index 7ac57ab..42e31b5 100644 --- a/docs/server-api.md +++ b/docs/server-api.md @@ -98,7 +98,7 @@ Each procedure uses `require*Repository(ctx)` helpers that throw `TRPCError(INTE | getInitiative | query | With projects array | | updateInitiative | mutation | Name, status | | deleteInitiative | mutation | Cascade delete initiative and all children | -| updateInitiativeConfig | mutation | executionMode, branch | +| updateInitiativeConfig | mutation | executionMode, branch, qualityReview | | getInitiativeReviewDiff | query | Full diff of initiative branch vs project default branch | | getInitiativeReviewCommits | query | Commits on initiative branch not on default branch | | getInitiativeCommitDiff | query | Single commit diff for initiative review | From 9200891a5d612ef684b6ab152e84b79e20b3fbf4 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 21:56:18 +0100 Subject: [PATCH 4/9] feat: add quality-review service with qualifying file detection and agent spawning Adds apps/server/execution/quality-review.ts with three exported functions: - computeQualifyingFiles: diffs task branch vs base, filters out *.gen.ts and dist/ paths - shouldRunQualityReview: evaluates all six guard conditions (task_complete, execute mode, in_progress status, initiative membership, qualityReview flag, non-empty changeset) and returns { run, qualifyingFiles } to avoid recomputing the diff in the orchestrator - runQualityReview: transitions task to quality_review, spawns execute-mode review agent on the task branch, logs the review agent ID, and falls back to completed on spawn failure Co-Authored-By: Claude Sonnet 4.6 --- apps/server/execution/quality-review.test.ts | 582 +++++++++++++++++++ apps/server/execution/quality-review.ts | 152 +++++ 2 files changed, 734 insertions(+) create mode 100644 apps/server/execution/quality-review.test.ts create mode 100644 apps/server/execution/quality-review.ts diff --git a/apps/server/execution/quality-review.test.ts b/apps/server/execution/quality-review.test.ts new file mode 100644 index 0000000..ed8f780 --- /dev/null +++ b/apps/server/execution/quality-review.test.ts @@ -0,0 +1,582 @@ +/** + * Quality Review Service Tests + */ + +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { computeQualifyingFiles, shouldRunQualityReview, runQualityReview } from './quality-review.js'; +import type { BranchManager } from '../git/branch-manager.js'; +import type { AgentRepository } from '../db/repositories/agent-repository.js'; +import type { TaskRepository } from '../db/repositories/task-repository.js'; +import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; +import type { AgentManager } from '../agent/types.js'; + +function makeBranchManager(overrides: Partial = {}): BranchManager { + return { + ensureBranch: vi.fn(), + mergeBranch: vi.fn(), + diffBranches: vi.fn(), + diffBranchesStat: vi.fn().mockResolvedValue([]), + diffFileSingle: vi.fn(), + getHeadCommitHash: vi.fn(), + deleteBranch: vi.fn(), + branchExists: vi.fn(), + remoteBranchExists: vi.fn(), + listCommits: vi.fn(), + diffCommit: vi.fn(), + getMergeBase: vi.fn(), + pushBranch: vi.fn(), + checkMergeability: vi.fn(), + fetchRemote: vi.fn(), + fastForwardBranch: vi.fn(), + updateRef: vi.fn(), + ...overrides, + }; +} + +function makeAgentRepository(overrides: Partial = {}): AgentRepository { + return { + create: vi.fn(), + findById: vi.fn().mockResolvedValue(null), + findByName: vi.fn(), + findByTaskId: vi.fn(), + findBySessionId: vi.fn(), + findAll: vi.fn(), + findByStatus: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + ...overrides, + } as unknown as AgentRepository; +} + +function makeTaskRepository(overrides: Partial = {}): TaskRepository { + return { + create: vi.fn(), + findById: vi.fn().mockResolvedValue(null), + findByParentTaskId: vi.fn(), + findByInitiativeId: vi.fn(), + findByPhaseId: vi.fn(), + update: vi.fn().mockResolvedValue({}), + delete: vi.fn(), + createDependency: vi.fn(), + getDependencies: vi.fn(), + ...overrides, + } as unknown as TaskRepository; +} + +function makeInitiativeRepository(overrides: Partial = {}): InitiativeRepository { + return { + create: vi.fn(), + findById: vi.fn().mockResolvedValue(null), + findAll: vi.fn(), + findByStatus: vi.fn(), + update: vi.fn(), + findByProjectId: vi.fn(), + delete: vi.fn(), + ...overrides, + } as unknown as InitiativeRepository; +} + +function makeAgentManager(overrides: Partial = {}): AgentManager { + return { + spawn: vi.fn().mockResolvedValue({ id: 'review-agent-1' }), + stop: vi.fn(), + list: vi.fn(), + get: vi.fn(), + getByName: vi.fn(), + resume: vi.fn(), + getResult: vi.fn(), + getPendingQuestions: vi.fn(), + delete: vi.fn(), + dismiss: vi.fn(), + resumeForConversation: vi.fn(), + sendUserMessage: vi.fn(), + ...overrides, + } as unknown as AgentManager; +} + +// --------------------------------------------------------------------------- +// computeQualifyingFiles +// --------------------------------------------------------------------------- + +describe('computeQualifyingFiles', () => { + it('includes .ts files', async () => { + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + ]), + }); + const result = await computeQualifyingFiles('/repo', 'task-branch', 'main', branchManager); + expect(result).toEqual(['src/foo.ts']); + }); + + it('includes .tsx, .js, .css, .json and other non-excluded types', async () => { + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockResolvedValue([ + { path: 'src/App.tsx', status: 'modified', additions: 1, deletions: 0 }, + { path: 'src/utils.js', status: 'added', additions: 10, deletions: 0 }, + { path: 'src/style.css', status: 'modified', additions: 3, deletions: 1 }, + { path: 'config.json', status: 'modified', additions: 2, deletions: 2 }, + ]), + }); + const result = await computeQualifyingFiles('/repo', 'task-branch', 'main', branchManager); + expect(result).toEqual(['src/App.tsx', 'src/utils.js', 'src/style.css', 'config.json']); + }); + + it('excludes files ending with .gen.ts', async () => { + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + { path: 'src/routes.gen.ts', status: 'modified', additions: 100, deletions: 0 }, + { path: 'types.gen.ts', status: 'added', additions: 50, deletions: 0 }, + ]), + }); + const result = await computeQualifyingFiles('/repo', 'task-branch', 'main', branchManager); + expect(result).toEqual(['src/foo.ts']); + }); + + it('excludes files under dist/', async () => { + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + { path: 'dist/bundle.js', status: 'modified', additions: 1, deletions: 0 }, + { path: 'apps/server/dist/index.js', status: 'added', additions: 10, deletions: 0 }, + { path: 'packages/foo/dist/foo.js', status: 'modified', additions: 3, deletions: 0 }, + ]), + }); + const result = await computeQualifyingFiles('/repo', 'task-branch', 'main', branchManager); + expect(result).toEqual(['src/foo.ts']); + }); + + it('returns empty array when diff throws', async () => { + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockRejectedValue(new Error('branch not found')), + }); + const result = await computeQualifyingFiles('/repo', 'task-branch', 'main', branchManager); + expect(result).toEqual([]); + }); + + it('passes baseBranch as first branch arg and taskBranch as second to diffBranchesStat', async () => { + const diffSpy = vi.fn().mockResolvedValue([]); + const branchManager = makeBranchManager({ diffBranchesStat: diffSpy }); + await computeQualifyingFiles('/repo', 'task-branch', 'main', branchManager); + expect(diffSpy).toHaveBeenCalledWith('/repo', 'main', 'task-branch'); + }); +}); + +// --------------------------------------------------------------------------- +// shouldRunQualityReview +// --------------------------------------------------------------------------- + +describe('shouldRunQualityReview', () => { + const BASE_PARAMS = { + agentId: 'agent-1', + taskId: 'task-1', + stopReason: 'task_complete', + repoPath: '/repo', + taskBranch: 'cw/init-task-task-1', + baseBranch: 'main', + }; + + it('returns false when stopReason is not task_complete', async () => { + const agentRepository = makeAgentRepository(); + const taskRepository = makeTaskRepository(); + const initiativeRepository = makeInitiativeRepository(); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + stopReason: 'error', + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + // Should short-circuit: no repo lookups + expect(agentRepository.findById).not.toHaveBeenCalled(); + }); + + it('returns false when agent mode is errand', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'errand' }), + }); + const taskRepository = makeTaskRepository(); + const initiativeRepository = makeInitiativeRepository(); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + expect(taskRepository.findById).not.toHaveBeenCalled(); + }); + + it('returns false when agent is not found', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue(null), + }); + const taskRepository = makeTaskRepository(); + const initiativeRepository = makeInitiativeRepository(); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task status is quality_review (recursion guard)', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue({ + id: 'task-1', + status: 'quality_review', + initiativeId: 'init-1', + }), + }); + const initiativeRepository = makeInitiativeRepository(); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + expect(initiativeRepository.findById).not.toHaveBeenCalled(); + }); + + it('returns false when task status is not in_progress and not quality_review', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue({ + id: 'task-1', + status: 'completed', + initiativeId: 'init-1', + }), + }); + const initiativeRepository = makeInitiativeRepository(); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task is not found', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue(null), + }); + const initiativeRepository = makeInitiativeRepository(); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task has no initiativeId', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue({ + id: 'task-1', + status: 'in_progress', + initiativeId: null, + }), + }); + const initiativeRepository = makeInitiativeRepository(); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + expect(initiativeRepository.findById).not.toHaveBeenCalled(); + }); + + it('returns false when initiative.qualityReview is false', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue({ + id: 'task-1', + status: 'in_progress', + initiativeId: 'init-1', + }), + }); + const initiativeRepository = makeInitiativeRepository({ + findById: vi.fn().mockResolvedValue({ id: 'init-1', qualityReview: false }), + }); + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + ]), + }); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + expect(branchManager.diffBranchesStat).not.toHaveBeenCalled(); + }); + + it('returns false when initiative is not found', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue({ + id: 'task-1', + status: 'in_progress', + initiativeId: 'init-1', + }), + }); + const initiativeRepository = makeInitiativeRepository({ + findById: vi.fn().mockResolvedValue(null), + }); + const branchManager = makeBranchManager(); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when no qualifying files in changeset', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue({ + id: 'task-1', + status: 'in_progress', + initiativeId: 'init-1', + }), + }); + const initiativeRepository = makeInitiativeRepository({ + findById: vi.fn().mockResolvedValue({ id: 'init-1', qualityReview: true }), + }); + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockResolvedValue([ + { path: 'dist/bundle.js', status: 'modified', additions: 10, deletions: 0 }, + { path: 'src/routes.gen.ts', status: 'added', additions: 50, deletions: 0 }, + ]), + }); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns true with qualifying files when all conditions pass', async () => { + const agentRepository = makeAgentRepository({ + findById: vi.fn().mockResolvedValue({ id: 'agent-1', mode: 'execute' }), + }); + const taskRepository = makeTaskRepository({ + findById: vi.fn().mockResolvedValue({ + id: 'task-1', + status: 'in_progress', + initiativeId: 'init-1', + }), + }); + const initiativeRepository = makeInitiativeRepository({ + findById: vi.fn().mockResolvedValue({ id: 'init-1', qualityReview: true }), + }); + const branchManager = makeBranchManager({ + diffBranchesStat: vi.fn().mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + { path: 'src/bar.ts', status: 'added', additions: 20, deletions: 0 }, + ]), + }); + + const result = await shouldRunQualityReview({ + ...BASE_PARAMS, + agentRepository, + taskRepository, + initiativeRepository, + branchManager, + }); + + expect(result).toEqual({ run: true, qualifyingFiles: ['src/foo.ts', 'src/bar.ts'] }); + }); +}); + +// --------------------------------------------------------------------------- +// runQualityReview +// --------------------------------------------------------------------------- + +describe('runQualityReview', () => { + const BASE_RUN_PARAMS = { + taskId: 'task-1', + taskBranch: 'cw/init-task-task-1', + baseBranch: 'main', + initiativeId: 'init-1', + qualifyingFiles: ['src/foo.ts', 'src/bar.ts'], + }; + + const makeLog = () => ({ + info: vi.fn(), + error: vi.fn(), + warn: vi.fn(), + debug: vi.fn(), + trace: vi.fn(), + fatal: vi.fn(), + child: vi.fn(), + }); + + it('transitions task status to quality_review before spawning', async () => { + const taskRepository = makeTaskRepository(); + const agentManager = makeAgentManager(); + const log = makeLog(); + + await runQualityReview({ + ...BASE_RUN_PARAMS, + taskRepository, + agentManager, + log: log as any, + }); + + const updateCalls = vi.mocked(taskRepository.update).mock.calls; + expect(updateCalls[0]).toEqual(['task-1', { status: 'quality_review' }]); + // spawn should come after the update + expect(agentManager.spawn).toHaveBeenCalled(); + }); + + it('calls agentManager.spawn with mode execute and correct branchName', async () => { + const taskRepository = makeTaskRepository(); + const agentManager = makeAgentManager(); + const log = makeLog(); + + await runQualityReview({ + ...BASE_RUN_PARAMS, + taskRepository, + agentManager, + log: log as any, + }); + + expect(agentManager.spawn).toHaveBeenCalledWith( + expect.objectContaining({ + taskId: 'task-1', + initiativeId: 'init-1', + mode: 'execute', + baseBranch: 'main', + branchName: 'cw/init-task-task-1', + }), + ); + }); + + it('prompt includes /simplify instruction and qualifying files', async () => { + const taskRepository = makeTaskRepository(); + const agentManager = makeAgentManager(); + const log = makeLog(); + + await runQualityReview({ + ...BASE_RUN_PARAMS, + taskRepository, + agentManager, + log: log as any, + }); + + const spawnCall = vi.mocked(agentManager.spawn).mock.calls[0][0]; + expect(spawnCall.prompt).toContain('/simplify'); + expect(spawnCall.prompt).toContain('src/foo.ts'); + expect(spawnCall.prompt).toContain('src/bar.ts'); + }); + + it('logs reviewAgentId at info level after spawn', async () => { + const taskRepository = makeTaskRepository(); + const agentManager = makeAgentManager({ + spawn: vi.fn().mockResolvedValue({ id: 'review-agent-42' }), + }); + const log = makeLog(); + + await runQualityReview({ + ...BASE_RUN_PARAMS, + taskRepository, + agentManager, + log: log as any, + }); + + expect(log.info).toHaveBeenCalledWith( + expect.objectContaining({ taskId: 'task-1', reviewAgentId: 'review-agent-42' }), + expect.any(String), + ); + }); + + it('on spawn failure: transitions task to completed and does not throw', async () => { + const taskRepository = makeTaskRepository(); + const agentManager = makeAgentManager({ + spawn: vi.fn().mockRejectedValue(new Error('spawn failed')), + }); + const log = makeLog(); + + await expect( + runQualityReview({ + ...BASE_RUN_PARAMS, + taskRepository, + agentManager, + log: log as any, + }), + ).resolves.toBeUndefined(); + + expect(log.error).toHaveBeenCalled(); + expect(taskRepository.update).toHaveBeenCalledWith('task-1', { status: 'completed' }); + }); +}); diff --git a/apps/server/execution/quality-review.ts b/apps/server/execution/quality-review.ts new file mode 100644 index 0000000..f54eb35 --- /dev/null +++ b/apps/server/execution/quality-review.ts @@ -0,0 +1,152 @@ +/** + * Quality Review Service + * + * Decides whether to run a quality review after a task agent completes, + * and orchestrates spawning the review agent. + * + * All dependencies are passed as function parameters (hexagonal DI pattern). + */ + +import type { BranchManager } from '../git/branch-manager.js'; +import type { AgentRepository } from '../db/repositories/agent-repository.js'; +import type { TaskRepository } from '../db/repositories/task-repository.js'; +import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; +import type { AgentManager } from '../agent/types.js'; +import type { Logger } from 'pino'; + +// --------------------------------------------------------------------------- +// computeQualifyingFiles +// --------------------------------------------------------------------------- + +/** + * Compute source files in the diff between taskBranch and baseBranch that + * qualify for a quality review (excludes *.gen.ts and dist/ paths). + * + * Returns [] if the diff throws (treated as no qualifying files). + */ +export async function computeQualifyingFiles( + repoPath: string, + taskBranch: string, + baseBranch: string, + branchManager: BranchManager, +): Promise { + try { + const entries = await branchManager.diffBranchesStat(repoPath, baseBranch, taskBranch); + return entries + .map((e) => e.path) + .filter((p) => !p.endsWith('.gen.ts')) + .filter((p) => !p.startsWith('dist/') && !p.includes('/dist/')); + } catch { + return []; + } +} + +// --------------------------------------------------------------------------- +// shouldRunQualityReview +// --------------------------------------------------------------------------- + +export interface QualityReviewCheckParams { + agentId: string; + taskId: string; + stopReason: string; + repoPath: string; + taskBranch: string; + baseBranch: string; + agentRepository: AgentRepository; + taskRepository: TaskRepository; + initiativeRepository: InitiativeRepository; + branchManager: BranchManager; +} + +/** + * Determine whether a quality review should be run for the given agent stop event. + * + * Returns { run: true, qualifyingFiles } only when all six conditions pass: + * 1. stopReason === 'task_complete' + * 2. Agent mode is 'execute' + * 3. Task status is 'in_progress' (not 'quality_review' — recursion guard) + * 4. task.initiativeId is non-null + * 5. initiative.qualityReview === true + * 6. computeQualifyingFiles() returns a non-empty array + */ +export async function shouldRunQualityReview( + params: QualityReviewCheckParams, +): Promise<{ run: boolean; qualifyingFiles: string[] }> { + const { agentId, taskId, stopReason, repoPath, taskBranch, baseBranch, agentRepository, taskRepository, initiativeRepository, branchManager } = params; + const NO = { run: false, qualifyingFiles: [] }; + + // 1. Only fire on task_complete + if (stopReason !== 'task_complete') return NO; + + // 2. Agent mode must be 'execute' + const agent = await agentRepository.findById(agentId); + if (!agent || agent.mode !== 'execute') return NO; + + // 3. Task status must be 'in_progress' (recursion guard: skip if already quality_review) + const task = await taskRepository.findById(taskId); + if (!task) return NO; + if (task.status === 'quality_review') return NO; + if (task.status !== 'in_progress') return NO; + + // 4. Task must belong to an initiative + if (!task.initiativeId) return NO; + + // 5. Initiative must have qualityReview enabled + const initiative = await initiativeRepository.findById(task.initiativeId); + if (!initiative || !initiative.qualityReview) return NO; + + // 6. Must have qualifying files in the changeset + const qualifyingFiles = await computeQualifyingFiles(repoPath, taskBranch, baseBranch, branchManager); + if (qualifyingFiles.length === 0) return NO; + + return { run: true, qualifyingFiles }; +} + +// --------------------------------------------------------------------------- +// runQualityReview +// --------------------------------------------------------------------------- + +export interface QualityReviewRunParams { + taskId: string; + taskBranch: string; + baseBranch: string; + initiativeId: string; + qualifyingFiles: string[]; + taskRepository: TaskRepository; + agentManager: AgentManager; + log: Logger; +} + +/** + * Spawn a quality review agent on the task branch. + * + * 1. Transitions task to 'quality_review' + * 2. Builds /simplify prompt with qualifying files + * 3. Spawns execute-mode agent on the same task branch + * 4. Logs the review agent ID + * 5. On spawn error: logs and transitions task to 'completed' — never throws + */ +export async function runQualityReview(params: QualityReviewRunParams): Promise { + const { taskId, taskBranch, baseBranch, initiativeId, qualifyingFiles, taskRepository, agentManager, log } = params; + + await taskRepository.update(taskId, { status: 'quality_review' }); + + const fileList = qualifyingFiles.join('\n'); + const prompt = `Run /simplify to review and fix code quality in this branch.\n\n${fileList}`; + + try { + const reviewAgent = await agentManager.spawn({ + taskId, + initiativeId, + prompt, + mode: 'execute', + baseBranch, + branchName: taskBranch, + }); + + log.info({ taskId, reviewAgentId: reviewAgent.id }, 'quality review agent spawned'); + } catch (err) { + log.error({ taskId, err: err instanceof Error ? err.message : String(err) }, 'quality review agent spawn failed'); + await taskRepository.update(taskId, { status: 'completed' }); + } +} From c3cace7604a2452dbe6bae0bc70a2688dffd136d Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 22:01:02 +0100 Subject: [PATCH 5/9] feat: add quality-review dispatch hook to intercept agent:stopped events When an execute-mode agent stops with task_complete and the initiative has qualityReview=true, the orchestrator now spawns a fresh execute-mode agent to run /simplify on changed .ts/.tsx/.js files before marking the task completed. The task transitions through quality_review status as a recursion guard so the review agent's stop event is handled normally. - Add apps/server/execution/quality-review.ts with three exported functions: computeQualifyingFiles, shouldRunQualityReview, runQualityReview - Add apps/server/execution/quality-review.test.ts (28 tests) - Update ExecutionOrchestrator to accept agentManager, replace handleAgentStopped with quality-review-aware logic, add getRepoPathForTask - Update orchestrator.test.ts with 3 quality-review integration tests - Update container.ts to pass agentManager to ExecutionOrchestrator - Update docs/dispatch-events.md to reflect new agent:stopped behavior Co-Authored-By: Claude Sonnet 4.6 --- apps/server/container.ts | 1 + apps/server/execution/orchestrator.test.ts | 112 +++++ apps/server/execution/orchestrator.ts | 57 ++- apps/server/execution/quality-review.test.ts | 434 +++++++++++++++++++ apps/server/execution/quality-review.ts | 175 ++++++++ docs/dispatch-events.md | 2 +- 6 files changed, 776 insertions(+), 5 deletions(-) create mode 100644 apps/server/execution/quality-review.test.ts create mode 100644 apps/server/execution/quality-review.ts diff --git a/apps/server/container.ts b/apps/server/container.ts index daa6151..bfa0c55 100644 --- a/apps/server/container.ts +++ b/apps/server/container.ts @@ -250,6 +250,7 @@ export async function createContainer(options?: ContainerOptions): Promise ({ ensureProjectClone: vi.fn().mockResolvedValue('/tmp/test-workspace/clones/test'), })); + +vi.mock('./quality-review.js', () => ({ + shouldRunQualityReview: vi.fn(), + runQualityReview: vi.fn(), + computeQualifyingFiles: vi.fn(), +})); import type { PhaseRepository } from '../db/repositories/phase-repository.js'; import type { TaskRepository } from '../db/repositories/task-repository.js'; import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; @@ -110,6 +117,23 @@ function createMocks() { const eventBus = createMockEventBus(); + const agentManager = { + spawn: vi.fn().mockResolvedValue({ id: 'review-agent-1' }), + stop: vi.fn(), + list: vi.fn(), + resume: vi.fn(), + delete: vi.fn(), + }; + + const agentRepository = { + findById: vi.fn().mockResolvedValue({ id: 'a1', mode: 'execute' }), + findByTaskId: vi.fn().mockResolvedValue(null), + findAll: vi.fn().mockResolvedValue([]), + create: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + }; + return { branchManager, phaseRepository, @@ -120,6 +144,8 @@ function createMocks() { dispatchManager, conflictResolutionService, eventBus, + agentManager, + agentRepository, }; } @@ -135,6 +161,8 @@ function createOrchestrator(mocks: ReturnType) { mocks.conflictResolutionService, mocks.eventBus, '/tmp/test-workspace', + mocks.agentManager as any, + mocks.agentRepository as any, ); orchestrator.start(); return orchestrator; @@ -370,3 +398,87 @@ describe('ExecutionOrchestrator', () => { }); }); }); + +describe('handleAgentStopped — quality review integration', () => { + let mocks: ReturnType; + + beforeEach(() => { + mocks = createMocks(); + vi.mocked(shouldRunQualityReview).mockReset(); + vi.mocked(runQualityReview).mockReset(); + }); + + it('calls runQualityReview and skips completeTask when shouldRunQualityReview returns run:true', async () => { + vi.mocked(shouldRunQualityReview).mockResolvedValue({ + run: true, + qualifyingFiles: ['src/foo.ts'], + }); + vi.mocked(runQualityReview).mockResolvedValue(undefined); + + // Provide task data for re-fetch inside runQualityReview branch + vi.mocked(mocks.taskRepository.findById).mockResolvedValue({ + id: 't1', + status: 'in_progress', + initiativeId: 'i1', + phaseId: 'p1', + } as any); + vi.mocked(mocks.initiativeRepository.findById).mockResolvedValue({ + id: 'i1', + branch: 'cw/test', + qualityReview: true, + } as any); + vi.mocked(mocks.phaseRepository.findById).mockResolvedValue({ + id: 'p1', + name: 'impl', + initiativeId: 'i1', + } as any); + + createOrchestrator(mocks); + + mocks.eventBus.emit({ + type: 'agent:stopped', + timestamp: new Date(), + payload: { taskId: 't1', reason: 'task_complete', agentId: 'a1' }, + }); + + await vi.waitFor(() => { + expect(runQualityReview).toHaveBeenCalledWith( + expect.objectContaining({ taskId: 't1', qualifyingFiles: ['src/foo.ts'] }), + ); + }); + expect(mocks.dispatchManager.completeTask).not.toHaveBeenCalled(); + }); + + it('calls completeTask and skips runQualityReview when shouldRunQualityReview returns run:false', async () => { + vi.mocked(shouldRunQualityReview).mockResolvedValue({ run: false, qualifyingFiles: [] }); + vi.mocked(runQualityReview).mockResolvedValue(undefined); + + createOrchestrator(mocks); + + mocks.eventBus.emit({ + type: 'agent:stopped', + timestamp: new Date(), + payload: { taskId: 't1', reason: 'task_complete', agentId: 'a1' }, + }); + + await vi.waitFor(() => { + expect(mocks.dispatchManager.completeTask).toHaveBeenCalledWith('t1', 'a1'); + }); + expect(runQualityReview).not.toHaveBeenCalled(); + }); + + it('skips both paths for user_requested reason', async () => { + createOrchestrator(mocks); + + mocks.eventBus.emit({ + type: 'agent:stopped', + timestamp: new Date(), + payload: { taskId: 't1', reason: 'user_requested', agentId: 'a1' }, + }); + + // Wait for scheduleDispatch to be triggered (dispatchNext is called in the cycle) + await vi.waitFor(() => expect(mocks.dispatchManager.dispatchNext).toHaveBeenCalled()); + expect(shouldRunQualityReview).not.toHaveBeenCalled(); + expect(mocks.dispatchManager.completeTask).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/server/execution/orchestrator.ts b/apps/server/execution/orchestrator.ts index 138aaa3..5cf9581 100644 --- a/apps/server/execution/orchestrator.ts +++ b/apps/server/execution/orchestrator.ts @@ -18,12 +18,14 @@ import type { TaskRepository } from '../db/repositories/task-repository.js'; import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; import type { ProjectRepository } from '../db/repositories/project-repository.js'; import type { AgentRepository } from '../db/repositories/agent-repository.js'; +import type { AgentManager } from '../agent/types.js'; import type { DispatchManager, PhaseDispatchManager } from '../dispatch/types.js'; import type { ConflictResolutionService } from '../coordination/conflict-resolution-service.js'; import { phaseBranchName, taskBranchName } from '../git/branch-naming.js'; import { ensureProjectClone } from '../git/project-clones.js'; import { createModuleLogger } from '../logger/index.js'; import { phaseMetaCache, fileDiffCache } from '../review/diff-cache.js'; +import { shouldRunQualityReview, runQualityReview } from './quality-review.js'; const log = createModuleLogger('execution-orchestrator'); @@ -49,6 +51,7 @@ export class ExecutionOrchestrator { private conflictResolutionService: ConflictResolutionService, private eventBus: EventBus, private workspaceRoot: string, + private agentManager: AgentManager, private agentRepository?: AgentRepository, ) {} @@ -108,15 +111,53 @@ export class ExecutionOrchestrator { private async handleAgentStopped(event: AgentStoppedEvent): Promise { const { taskId, reason, agentId } = event.payload; - // Auto-complete task for successful agent completions, not manual stops if (taskId && reason !== 'user_requested') { try { - await this.dispatchManager.completeTask(taskId, agentId); - log.info({ taskId, agentId, reason }, 'task auto-completed on agent stop'); + if (!this.agentRepository) { + // No agent repository — skip quality review, complete task directly + log.warn({ taskId, agentId }, 'agentRepository not available; skipping quality review'); + await this.dispatchManager.completeTask(taskId, agentId); + log.info({ taskId, agentId, reason }, 'task auto-completed on agent stop'); + } else { + // Get repoPath from first project in initiative (for branch diffing) + const repoPath = await this.getRepoPathForTask(taskId); + + const result = await shouldRunQualityReview({ + agentId, + taskId, + stopReason: reason, + agentRepository: this.agentRepository, + taskRepository: this.taskRepository, + initiativeRepository: this.initiativeRepository, + phaseRepository: this.phaseRepository, + branchManager: this.branchManager, + repoPath, + }); + + if (result.run) { + const task = await this.taskRepository.findById(taskId); + const initiative = await this.initiativeRepository.findById(task!.initiativeId!); + const phase = await this.phaseRepository.findById(task!.phaseId!); + const initBranch = initiative!.branch!; + await runQualityReview({ + taskId, + taskBranch: taskBranchName(initBranch, taskId), + baseBranch: phaseBranchName(initBranch, phase!.name), + initiativeId: task!.initiativeId!, + qualifyingFiles: result.qualifyingFiles, + taskRepository: this.taskRepository, + agentManager: this.agentManager, + log, + }); + } else { + await this.dispatchManager.completeTask(taskId, agentId); + log.info({ taskId, agentId, reason }, 'task auto-completed on agent stop'); + } + } } catch (err) { log.warn( { taskId, agentId, reason, err: err instanceof Error ? err.message : String(err) }, - 'failed to auto-complete task on agent stop', + 'failed to handle agent stop', ); } } @@ -124,6 +165,14 @@ export class ExecutionOrchestrator { this.scheduleDispatch(); } + private async getRepoPathForTask(taskId: string): Promise { + const task = await this.taskRepository.findById(taskId); + if (!task?.initiativeId) return this.workspaceRoot; + const projects = await this.projectRepository.findProjectsByInitiativeId(task.initiativeId); + if (!projects.length) return this.workspaceRoot; + return ensureProjectClone(projects[0], this.workspaceRoot); + } + private async handleAgentCrashed(event: AgentCrashedEvent): Promise { const { taskId, agentId, error } = event.payload; if (!taskId) return; diff --git a/apps/server/execution/quality-review.test.ts b/apps/server/execution/quality-review.test.ts new file mode 100644 index 0000000..3aec70b --- /dev/null +++ b/apps/server/execution/quality-review.test.ts @@ -0,0 +1,434 @@ +/** + * Quality Review Tests + * + * Tests for the quality-review dispatch hook that intercepts agent:stopped + * events and spawns a review agent when conditions are met. + */ + +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { computeQualifyingFiles, shouldRunQualityReview, runQualityReview } from './quality-review.js'; +import type { BranchManager } from '../git/branch-manager.js'; +import type { AgentRepository } from '../db/repositories/agent-repository.js'; +import type { TaskRepository } from '../db/repositories/task-repository.js'; +import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; +import type { PhaseRepository } from '../db/repositories/phase-repository.js'; +import type { AgentManager } from '../agent/types.js'; +import type { createModuleLogger } from '../logger/index.js'; + +type Logger = ReturnType; + +// --------------------------------------------------------------------------- +// Mock helpers +// --------------------------------------------------------------------------- + +function createBranchManagerMock(): BranchManager { + return { + ensureBranch: vi.fn(), + mergeBranch: vi.fn(), + diffBranches: vi.fn(), + diffBranchesStat: vi.fn().mockResolvedValue([]), + diffFileSingle: vi.fn(), + getHeadCommitHash: vi.fn(), + deleteBranch: vi.fn(), + branchExists: vi.fn(), + remoteBranchExists: vi.fn(), + listCommits: vi.fn(), + diffCommit: vi.fn(), + getMergeBase: vi.fn(), + pushBranch: vi.fn(), + checkMergeability: vi.fn(), + fetchRemote: vi.fn(), + fastForwardBranch: vi.fn(), + updateRef: vi.fn(), + } as unknown as BranchManager; +} + +function createAgentRepositoryMock(): AgentRepository { + return { + findById: vi.fn().mockResolvedValue({ id: 'a1', mode: 'execute' }), + findByTaskId: vi.fn(), + findAll: vi.fn(), + create: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + } as unknown as AgentRepository; +} + +function createTaskRepositoryMock(): TaskRepository { + return { + findById: vi.fn().mockResolvedValue({ + id: 't1', + status: 'in_progress', + initiativeId: 'i1', + phaseId: 'p1', + }), + findByPhaseId: vi.fn(), + findByInitiativeId: vi.fn(), + create: vi.fn(), + update: vi.fn().mockResolvedValue(undefined), + delete: vi.fn(), + } as unknown as TaskRepository; +} + +function createInitiativeRepositoryMock(): InitiativeRepository { + return { + findById: vi.fn().mockResolvedValue({ + id: 'i1', + qualityReview: true, + branch: 'cw/test', + }), + findAll: vi.fn(), + findByStatus: vi.fn(), + create: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + } as unknown as InitiativeRepository; +} + +function createPhaseRepositoryMock(): PhaseRepository { + return { + findById: vi.fn().mockResolvedValue({ id: 'p1', name: 'impl', initiativeId: 'i1' }), + findByInitiativeId: vi.fn(), + create: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + } as unknown as PhaseRepository; +} + +function createAgentManagerMock(): AgentManager { + return { + spawn: vi.fn().mockResolvedValue({ id: 'review-agent-1' }), + stop: vi.fn(), + list: vi.fn(), + resume: vi.fn(), + delete: vi.fn(), + getStatus: vi.fn(), + answerQuestion: vi.fn(), + spawnWithLifecycle: vi.fn(), + } as unknown as AgentManager; +} + +function createLoggerMock(): Logger { + return { + info: vi.fn(), + error: vi.fn(), + warn: vi.fn(), + debug: vi.fn(), + trace: vi.fn(), + fatal: vi.fn(), + child: vi.fn(), + } as unknown as Logger; +} + +// --------------------------------------------------------------------------- +// computeQualifyingFiles +// --------------------------------------------------------------------------- + +describe('computeQualifyingFiles', () => { + let branchManager: BranchManager; + + beforeEach(() => { + branchManager = createBranchManagerMock(); + }); + + it('includes .ts files', async () => { + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + ]); + expect(await computeQualifyingFiles(branchManager, '/repo', 'task-branch', 'base')).toEqual(['src/foo.ts']); + }); + + it('includes .tsx and .js files', async () => { + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'src/Comp.tsx', status: 'modified', additions: 3, deletions: 1 }, + { path: 'src/util.js', status: 'added', additions: 10, deletions: 0 }, + ]); + const result = await computeQualifyingFiles(branchManager, '/repo', 'task-branch', 'base'); + expect(result).toContain('src/Comp.tsx'); + expect(result).toContain('src/util.js'); + }); + + it('excludes *.gen.ts files', async () => { + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'src/routeTree.gen.ts', status: 'modified', additions: 1, deletions: 0 }, + ]); + expect(await computeQualifyingFiles(branchManager, '/repo', 'branch', 'base')).toEqual([]); + }); + + it('excludes files starting with dist/', async () => { + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'dist/index.js', status: 'modified', additions: 1, deletions: 0 }, + ]); + expect(await computeQualifyingFiles(branchManager, '/repo', 'branch', 'base')).toEqual([]); + }); + + it('excludes files containing /dist/', async () => { + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'packages/foo/dist/bar.js', status: 'modified', additions: 1, deletions: 0 }, + ]); + expect(await computeQualifyingFiles(branchManager, '/repo', 'branch', 'base')).toEqual([]); + }); + + it('returns empty array when diffBranchesStat throws', async () => { + vi.mocked(branchManager.diffBranchesStat).mockRejectedValue(new Error('branch not found')); + expect(await computeQualifyingFiles(branchManager, '/repo', 'branch', 'base')).toEqual([]); + }); + + it('returns only qualifying files from a mixed set', async () => { + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + { path: 'src/routeTree.gen.ts', status: 'modified', additions: 1, deletions: 0 }, + { path: 'dist/bundle.js', status: 'modified', additions: 1, deletions: 0 }, + { path: 'src/bar.tsx', status: 'added', additions: 10, deletions: 0 }, + { path: 'README.md', status: 'modified', additions: 2, deletions: 0 }, + ]); + const result = await computeQualifyingFiles(branchManager, '/repo', 'branch', 'base'); + expect(result).toEqual(['src/foo.ts', 'src/bar.tsx']); + }); +}); + +// --------------------------------------------------------------------------- +// shouldRunQualityReview +// --------------------------------------------------------------------------- + +describe('shouldRunQualityReview', () => { + let branchManager: BranchManager; + let agentRepository: AgentRepository; + let taskRepository: TaskRepository; + let initiativeRepository: InitiativeRepository; + let phaseRepository: PhaseRepository; + + // Base params where all conditions pass + let params: Parameters[0]; + + beforeEach(() => { + branchManager = createBranchManagerMock(); + agentRepository = createAgentRepositoryMock(); + taskRepository = createTaskRepositoryMock(); + initiativeRepository = createInitiativeRepositoryMock(); + phaseRepository = createPhaseRepositoryMock(); + + // Default diffBranchesStat returns qualifying file + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'src/foo.ts', status: 'modified', additions: 5, deletions: 2 }, + ]); + + params = { + agentId: 'a1', + taskId: 't1', + stopReason: 'task_complete', + agentRepository, + taskRepository, + initiativeRepository, + phaseRepository, + branchManager, + repoPath: '/repo', + }; + }); + + it('returns false when stopReason is not task_complete', async () => { + const result = await shouldRunQualityReview({ ...params, stopReason: 'error' }); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when agent is not found', async () => { + vi.mocked(agentRepository.findById).mockResolvedValue(undefined as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when agent mode is errand', async () => { + vi.mocked(agentRepository.findById).mockResolvedValue({ id: 'a1', mode: 'errand' } as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task is not found', async () => { + vi.mocked(taskRepository.findById).mockResolvedValue(undefined as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task status is quality_review (recursion guard)', async () => { + vi.mocked(taskRepository.findById).mockResolvedValue({ + id: 't1', + status: 'quality_review', + initiativeId: 'i1', + phaseId: 'p1', + } as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task status is not in_progress', async () => { + vi.mocked(taskRepository.findById).mockResolvedValue({ + id: 't1', + status: 'pending', + initiativeId: 'i1', + phaseId: 'p1', + } as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task has no initiativeId', async () => { + vi.mocked(taskRepository.findById).mockResolvedValue({ + id: 't1', + status: 'in_progress', + initiativeId: null, + phaseId: 'p1', + } as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when initiative is not found', async () => { + vi.mocked(initiativeRepository.findById).mockResolvedValue(undefined as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when initiative qualityReview is false', async () => { + vi.mocked(initiativeRepository.findById).mockResolvedValue({ + id: 'i1', + qualityReview: false, + branch: 'cw/test', + } as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when task has no phaseId', async () => { + vi.mocked(taskRepository.findById).mockResolvedValue({ + id: 't1', + status: 'in_progress', + initiativeId: 'i1', + phaseId: null, + } as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when phase is not found', async () => { + vi.mocked(phaseRepository.findById).mockResolvedValue(undefined as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when initiative has no branch', async () => { + vi.mocked(initiativeRepository.findById).mockResolvedValue({ + id: 'i1', + qualityReview: true, + branch: null, + } as any); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns false when no qualifying files in changeset', async () => { + vi.mocked(branchManager.diffBranchesStat).mockResolvedValue([ + { path: 'src/routeTree.gen.ts', status: 'modified', additions: 1, deletions: 0 }, + ]); + const result = await shouldRunQualityReview(params); + expect(result).toEqual({ run: false, qualifyingFiles: [] }); + }); + + it('returns true with qualifying files when all conditions pass', async () => { + const result = await shouldRunQualityReview(params); + expect(result.run).toBe(true); + expect(result.qualifyingFiles).toContain('src/foo.ts'); + }); +}); + +// --------------------------------------------------------------------------- +// runQualityReview +// --------------------------------------------------------------------------- + +describe('runQualityReview', () => { + let taskRepository: TaskRepository; + let agentManager: AgentManager; + let log: Logger; + + let params: Parameters[0]; + + beforeEach(() => { + taskRepository = createTaskRepositoryMock(); + agentManager = createAgentManagerMock(); + log = createLoggerMock(); + + params = { + taskId: 't1', + taskBranch: 'cw/test-task-t1', + baseBranch: 'cw/test-phase-impl', + initiativeId: 'i1', + qualifyingFiles: ['src/foo.ts', 'src/bar.ts'], + taskRepository, + agentManager, + log, + }; + }); + + it('transitions task to quality_review before spawning', async () => { + await runQualityReview(params); + expect(taskRepository.update).toHaveBeenCalledWith('t1', { status: 'quality_review' }); + // update called BEFORE spawn + const updateOrder = vi.mocked(taskRepository.update).mock.invocationCallOrder[0]!; + const spawnOrder = vi.mocked(agentManager.spawn).mock.invocationCallOrder[0]!; + expect(updateOrder).toBeLessThan(spawnOrder); + }); + + it('spawns agent with mode execute on the task branch', async () => { + await runQualityReview(params); + expect(agentManager.spawn).toHaveBeenCalledWith( + expect.objectContaining({ + mode: 'execute', + branchName: 'cw/test-task-t1', + baseBranch: 'cw/test-phase-impl', + }), + ); + }); + + it('includes qualifying files in the prompt', async () => { + await runQualityReview(params); + const spawnArgs = vi.mocked(agentManager.spawn).mock.calls[0]![0]; + expect(spawnArgs.prompt).toContain('src/foo.ts'); + expect(spawnArgs.prompt).toContain('src/bar.ts'); + expect(spawnArgs.prompt).toContain('/simplify'); + }); + + it('spawns with taskId and initiativeId', async () => { + await runQualityReview(params); + expect(agentManager.spawn).toHaveBeenCalledWith( + expect.objectContaining({ + taskId: 't1', + initiativeId: 'i1', + }), + ); + }); + + it('on spawn failure: marks task completed and does not throw', async () => { + vi.mocked(agentManager.spawn).mockRejectedValue(new Error('spawn failed')); + await expect(runQualityReview(params)).resolves.toBeUndefined(); + // Last call to update should set status to completed + const updateCalls = vi.mocked(taskRepository.update).mock.calls; + const lastCall = updateCalls[updateCalls.length - 1]!; + expect(lastCall).toEqual(['t1', { status: 'completed' }]); + }); + + it('logs info after successful spawn', async () => { + await runQualityReview(params); + expect(log.info).toHaveBeenCalledWith( + expect.objectContaining({ taskId: 't1', reviewAgentId: 'review-agent-1' }), + expect.any(String), + ); + }); + + it('logs error on spawn failure', async () => { + vi.mocked(agentManager.spawn).mockRejectedValue(new Error('spawn failed')); + await runQualityReview(params); + expect(log.error).toHaveBeenCalledWith( + expect.objectContaining({ taskId: 't1' }), + expect.any(String), + ); + }); +}); diff --git a/apps/server/execution/quality-review.ts b/apps/server/execution/quality-review.ts new file mode 100644 index 0000000..b8a3a6b --- /dev/null +++ b/apps/server/execution/quality-review.ts @@ -0,0 +1,175 @@ +/** + * Quality Review Dispatch Hook + * + * Intercepts agent:stopped events and, when conditions are met, spawns + * a fresh execute-mode agent to run /simplify on changed files before + * the task reaches 'completed' status. + */ + +import type { BranchManager } from '../git/branch-manager.js'; +import type { AgentManager } from '../agent/types.js'; +import type { AgentRepository } from '../db/repositories/agent-repository.js'; +import type { TaskRepository } from '../db/repositories/task-repository.js'; +import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; +import type { PhaseRepository } from '../db/repositories/phase-repository.js'; +import { phaseBranchName, taskBranchName } from '../git/branch-naming.js'; +import type { createModuleLogger } from '../logger/index.js'; + +type Logger = ReturnType; + +// --------------------------------------------------------------------------- +// computeQualifyingFiles +// --------------------------------------------------------------------------- + +/** + * Returns the list of .ts/.tsx/.js files changed between taskBranch and baseBranch, + * excluding generated files and dist artifacts. + */ +export async function computeQualifyingFiles( + branchManager: BranchManager, + repoPath: string, + taskBranch: string, + baseBranch: string, +): Promise { + try { + const entries = await branchManager.diffBranchesStat(repoPath, baseBranch, taskBranch); + return entries + .map((e) => e.path) + .filter( + (p) => + /\.(ts|tsx|js)$/.test(p) && + !p.endsWith('.gen.ts') && + !p.startsWith('dist/') && + !p.includes('/dist/'), + ); + } catch { + return []; + } +} + +// --------------------------------------------------------------------------- +// shouldRunQualityReview +// --------------------------------------------------------------------------- + +interface ShouldRunParams { + agentId: string; + taskId: string; + stopReason: string; + agentRepository: AgentRepository; + taskRepository: TaskRepository; + initiativeRepository: InitiativeRepository; + phaseRepository: PhaseRepository; + branchManager: BranchManager; + repoPath: string; +} + +/** + * Evaluates whether a quality review should be run for the stopped agent. + * Returns `{ run: true, qualifyingFiles }` only when all conditions pass. + * Short-circuits on first false condition. + */ +export async function shouldRunQualityReview( + params: ShouldRunParams, +): Promise<{ run: boolean; qualifyingFiles: string[] }> { + const { + agentId, + taskId, + stopReason, + agentRepository, + taskRepository, + initiativeRepository, + phaseRepository, + branchManager, + repoPath, + } = params; + + const NO = { run: false, qualifyingFiles: [] }; + + // 1. Only act on task_complete stops + if (stopReason !== 'task_complete') return NO; + + // 2. Agent must be in execute mode (guards against errand agents) + const agent = await agentRepository.findById(agentId); + if (!agent || agent.mode !== 'execute') return NO; + + // 3. Task must be in_progress; quality_review is the recursion guard + const task = await taskRepository.findById(taskId); + if (!task) return NO; + if (task.status === 'quality_review') return NO; + if (task.status !== 'in_progress') return NO; + + // 4. Task must belong to an initiative + if (!task.initiativeId) return NO; + + // 5. Initiative must have qualityReview enabled + const initiative = await initiativeRepository.findById(task.initiativeId); + if (!initiative || initiative.qualityReview !== true) return NO; + + // 6. Compute branch names from task context + if (!task.phaseId) return NO; + const phase = await phaseRepository.findById(task.phaseId); + if (!phase) return NO; + + const initBranch = initiative.branch; + if (!initBranch) return NO; + + const base = phaseBranchName(initBranch, phase.name); + const branch = taskBranchName(initBranch, task.id); + + // 7. Must have qualifying files in the changeset + const qualifyingFiles = await computeQualifyingFiles(branchManager, repoPath, branch, base); + if (qualifyingFiles.length === 0) return NO; + + return { run: true, qualifyingFiles }; +} + +// --------------------------------------------------------------------------- +// runQualityReview +// --------------------------------------------------------------------------- + +interface RunQualityReviewParams { + taskId: string; + taskBranch: string; + baseBranch: string; + initiativeId: string; + qualifyingFiles: string[]; + taskRepository: TaskRepository; + agentManager: AgentManager; + log: Logger; +} + +/** + * Transitions the task to quality_review and spawns a fresh execute-mode + * agent to run /simplify on the changed files. + * + * On spawn failure: marks task completed and returns (never throws). + */ +export async function runQualityReview(params: RunQualityReviewParams): Promise { + const { taskId, taskBranch, baseBranch, initiativeId, qualifyingFiles, taskRepository, agentManager, log } = params; + + // 1. Transition BEFORE spawning + await taskRepository.update(taskId, { status: 'quality_review' }); + + // 2. Build prompt + const fileList = qualifyingFiles.map((f) => `- ${f}`).join('\n'); + const reviewPrompt = `Run /simplify to review and fix code quality in this branch.\n\nFiles changed in this task:\n${fileList}`; + + // 3. Spawn fresh execute-mode agent on the same task branch + try { + const reviewAgent = await agentManager.spawn({ + taskId, + initiativeId, + prompt: reviewPrompt, + mode: 'execute', + baseBranch, + branchName: taskBranch, + }); + + // 4. Log success + log.info({ taskId, reviewAgentId: reviewAgent.id }, 'quality review agent spawned'); + } catch (err) { + // 5. On spawn failure: mark completed and return — never block task completion + log.error({ taskId, err }, 'quality review spawn failed; marking task completed'); + await taskRepository.update(taskId, { status: 'completed' }); + } +} diff --git a/docs/dispatch-events.md b/docs/dispatch-events.md index 5d1b4e9..fea1ddb 100644 --- a/docs/dispatch-events.md +++ b/docs/dispatch-events.md @@ -113,7 +113,7 @@ InitiativeChangesRequestedEvent { initiativeId, phaseId, taskId } | Event | Action | |-------|--------| | `phase:queued` | Dispatch ready phases → dispatch their tasks to idle agents | -| `agent:stopped` | Auto-complete task (unless user_requested), re-dispatch queued tasks (freed agent slot) | +| `agent:stopped` | When `task_complete`: check `shouldRunQualityReview()` — if conditions met, spawn quality-review agent and set task to `quality_review`; otherwise auto-complete task. Manual stops (`user_requested`) are skipped. Re-dispatch queued tasks after either path. | | `agent:crashed` | Auto-retry crashed task up to `MAX_TASK_RETRIES` (3). Increments `retryCount`, resets status to `pending`, re-queues. Exceeding retries leaves task `in_progress` for manual intervention. | | `task:completed` | Merge task branch (if branch exists), check phase completion, dispatch next queued task | From 4bc65bfe3d93c654a9285bb32aee06d35052160d Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 22:05:42 +0100 Subject: [PATCH 6/9] feat: wire quality review into orchestrator handleAgentStopped When an agent stops, check whether a quality review should run before auto-completing the task. If shouldRunQualityReview returns run:true, delegate to runQualityReview (which transitions task to quality_review and spawns a review agent) instead of calling completeTask directly. Falls back to completeTask when agentRepository or agentManager are not injected, or when the task lacks phaseId/initiativeId context. - Add agentManager optional param to ExecutionOrchestrator constructor - Extract tryQualityReview() private method to compute branch names and repo path before delegating to the quality-review service - Pass agentManager to ExecutionOrchestrator in container.ts - Add orchestrator integration tests for the agent:stopped quality hook Co-Authored-By: Claude Sonnet 4.6 --- apps/server/container.ts | 1 + apps/server/execution/orchestrator.test.ts | 129 ++++++++++++++++++++- apps/server/execution/orchestrator.ts | 79 ++++++++++++- 3 files changed, 203 insertions(+), 6 deletions(-) diff --git a/apps/server/container.ts b/apps/server/container.ts index daa6151..7a76973 100644 --- a/apps/server/container.ts +++ b/apps/server/container.ts @@ -251,6 +251,7 @@ export async function createContainer(options?: ContainerOptions): Promise ({ ensureProjectClone: vi.fn().mockResolvedValue('/tmp/test-workspace/clones/test'), })); + +vi.mock('./quality-review.js', () => ({ + shouldRunQualityReview: vi.fn().mockResolvedValue({ run: false, qualifyingFiles: [] }), + runQualityReview: vi.fn().mockResolvedValue(undefined), + computeQualifyingFiles: vi.fn().mockResolvedValue([]), +})); import type { PhaseRepository } from '../db/repositories/phase-repository.js'; import type { TaskRepository } from '../db/repositories/task-repository.js'; import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; import type { ProjectRepository } from '../db/repositories/project-repository.js'; import type { DispatchManager, PhaseDispatchManager } from '../dispatch/types.js'; import type { ConflictResolutionService } from '../coordination/conflict-resolution-service.js'; -import type { EventBus, TaskCompletedEvent, DomainEvent } from '../events/types.js'; +import type { EventBus, TaskCompletedEvent, AgentStoppedEvent, DomainEvent } from '../events/types.js'; +import { shouldRunQualityReview, runQualityReview } from './quality-review.js'; function createMockEventBus(): EventBus & { handlers: Map; emitted: DomainEvent[] } { const handlers = new Map(); @@ -108,6 +117,33 @@ function createMocks() { handleConflict: vi.fn(), }; + const agentRepository = { + create: vi.fn(), + findById: vi.fn().mockResolvedValue(null), + findByName: vi.fn(), + findByTaskId: vi.fn(), + findBySessionId: vi.fn(), + findAll: vi.fn(), + findByStatus: vi.fn(), + update: vi.fn(), + delete: vi.fn(), + } as unknown as AgentRepository; + + const agentManager = { + spawn: vi.fn().mockResolvedValue({ id: 'review-agent-1', name: 'review-agent' }), + stop: vi.fn(), + list: vi.fn().mockResolvedValue([]), + get: vi.fn(), + getByName: vi.fn(), + resume: vi.fn(), + getResult: vi.fn(), + getPendingQuestions: vi.fn(), + delete: vi.fn(), + dismiss: vi.fn(), + resumeForConversation: vi.fn(), + sendUserMessage: vi.fn(), + } as unknown as AgentManager; + const eventBus = createMockEventBus(); return { @@ -119,11 +155,13 @@ function createMocks() { phaseDispatchManager, dispatchManager, conflictResolutionService, + agentRepository, + agentManager, eventBus, }; } -function createOrchestrator(mocks: ReturnType) { +function createOrchestrator(mocks: ReturnType, opts: { withAgentManager?: boolean; withAgentRepository?: boolean } = {}) { const orchestrator = new ExecutionOrchestrator( mocks.branchManager, mocks.phaseRepository, @@ -135,6 +173,8 @@ function createOrchestrator(mocks: ReturnType) { mocks.conflictResolutionService, mocks.eventBus, '/tmp/test-workspace', + opts.withAgentRepository !== false ? mocks.agentRepository : undefined, + opts.withAgentManager !== false ? mocks.agentManager : undefined, ); orchestrator.start(); return orchestrator; @@ -369,4 +409,89 @@ describe('ExecutionOrchestrator', () => { expect(mocks.branchManager.updateRef).not.toHaveBeenCalled(); }); }); + + describe('handleAgentStopped quality review hook', () => { + function emitAgentStopped(eventBus: ReturnType, payload: { taskId?: string; agentId: string; reason: AgentStoppedEvent['payload']['reason'] }) { + const event: AgentStoppedEvent = { + type: 'agent:stopped', + timestamp: new Date(), + payload: { taskId: payload.taskId ?? null, agentId: payload.agentId, name: 'test-agent', reason: payload.reason }, + }; + eventBus.emit(event); + } + + function setupQualityReviewMocks() { + const task = { id: 'task-1', phaseId: 'phase-1', initiativeId: 'init-1', category: 'execute', status: 'in_progress' }; + const initiative = { id: 'init-1', branch: 'cw/test-initiative', executionMode: 'yolo', qualityReview: true }; + const phase = { id: 'phase-1', initiativeId: 'init-1', name: 'Phase 1', status: 'in_progress' }; + const project = { id: 'proj-1', name: 'test', url: 'https://example.com', defaultBranch: 'main' }; + + vi.mocked(mocks.taskRepository.findById).mockResolvedValue(task as any); + vi.mocked(mocks.initiativeRepository.findById).mockResolvedValue(initiative as any); + vi.mocked(mocks.phaseRepository.findById).mockResolvedValue(phase as any); + vi.mocked(mocks.projectRepository.findProjectsByInitiativeId).mockResolvedValue([project] as any); + vi.mocked(ensureProjectClone).mockResolvedValue('/tmp/test-workspace/clones/test'); + } + + beforeEach(() => { + vi.mocked(shouldRunQualityReview).mockClear().mockResolvedValue({ run: false, qualifyingFiles: [] }); + vi.mocked(runQualityReview).mockClear().mockResolvedValue(undefined); + }); + + it('should not call shouldRunQualityReview when reason is user_requested', async () => { + createOrchestrator(mocks); + + emitAgentStopped(mocks.eventBus, { taskId: 'task-1', agentId: 'agent-1', reason: 'user_requested' }); + + await new Promise((r) => setTimeout(r, 50)); + + expect(shouldRunQualityReview).not.toHaveBeenCalled(); + expect(mocks.dispatchManager.completeTask).not.toHaveBeenCalled(); + }); + + it('should call dispatchManager.completeTask when shouldRunQualityReview returns run: false', async () => { + setupQualityReviewMocks(); + vi.mocked(shouldRunQualityReview).mockResolvedValue({ run: false, qualifyingFiles: [] }); + + createOrchestrator(mocks); + + emitAgentStopped(mocks.eventBus, { taskId: 'task-1', agentId: 'agent-1', reason: 'task_complete' }); + + await vi.waitFor(() => { + expect(mocks.dispatchManager.completeTask).toHaveBeenCalledWith('task-1', 'agent-1'); + }); + expect(runQualityReview).not.toHaveBeenCalled(); + }); + + it('should call runQualityReview and NOT call completeTask when shouldRunQualityReview returns run: true', async () => { + setupQualityReviewMocks(); + vi.mocked(shouldRunQualityReview).mockResolvedValue({ run: true, qualifyingFiles: ['src/foo.ts'] }); + + createOrchestrator(mocks); + + emitAgentStopped(mocks.eventBus, { taskId: 'task-1', agentId: 'agent-1', reason: 'task_complete' }); + + await vi.waitFor(() => { + expect(runQualityReview).toHaveBeenCalledWith( + expect.objectContaining({ + taskId: 'task-1', + qualifyingFiles: ['src/foo.ts'], + taskRepository: mocks.taskRepository, + }), + ); + }); + expect(mocks.dispatchManager.completeTask).not.toHaveBeenCalled(); + }); + + it('should fall back to completeTask when agentRepository is not available', async () => { + createOrchestrator(mocks, { withAgentRepository: false, withAgentManager: false }); + + emitAgentStopped(mocks.eventBus, { taskId: 'task-1', agentId: 'agent-1', reason: 'task_complete' }); + + await vi.waitFor(() => { + expect(mocks.dispatchManager.completeTask).toHaveBeenCalledWith('task-1', 'agent-1'); + }); + expect(shouldRunQualityReview).not.toHaveBeenCalled(); + }); + }); }); diff --git a/apps/server/execution/orchestrator.ts b/apps/server/execution/orchestrator.ts index 138aaa3..dceeab3 100644 --- a/apps/server/execution/orchestrator.ts +++ b/apps/server/execution/orchestrator.ts @@ -18,12 +18,14 @@ import type { TaskRepository } from '../db/repositories/task-repository.js'; import type { InitiativeRepository } from '../db/repositories/initiative-repository.js'; import type { ProjectRepository } from '../db/repositories/project-repository.js'; import type { AgentRepository } from '../db/repositories/agent-repository.js'; +import type { AgentManager } from '../agent/types.js'; import type { DispatchManager, PhaseDispatchManager } from '../dispatch/types.js'; import type { ConflictResolutionService } from '../coordination/conflict-resolution-service.js'; import { phaseBranchName, taskBranchName } from '../git/branch-naming.js'; import { ensureProjectClone } from '../git/project-clones.js'; import { createModuleLogger } from '../logger/index.js'; import { phaseMetaCache, fileDiffCache } from '../review/diff-cache.js'; +import { shouldRunQualityReview, runQualityReview } from './quality-review.js'; const log = createModuleLogger('execution-orchestrator'); @@ -50,6 +52,7 @@ export class ExecutionOrchestrator { private eventBus: EventBus, private workspaceRoot: string, private agentRepository?: AgentRepository, + private agentManager?: AgentManager, ) {} /** @@ -108,15 +111,17 @@ export class ExecutionOrchestrator { private async handleAgentStopped(event: AgentStoppedEvent): Promise { const { taskId, reason, agentId } = event.payload; - // Auto-complete task for successful agent completions, not manual stops if (taskId && reason !== 'user_requested') { try { - await this.dispatchManager.completeTask(taskId, agentId); - log.info({ taskId, agentId, reason }, 'task auto-completed on agent stop'); + const result = await this.tryQualityReview(taskId, agentId, reason); + if (!result.reviewStarted) { + await this.dispatchManager.completeTask(taskId, agentId); + log.info({ taskId, agentId, reason }, 'task auto-completed on agent stop'); + } } catch (err) { log.warn( { taskId, agentId, reason, err: err instanceof Error ? err.message : String(err) }, - 'failed to auto-complete task on agent stop', + 'failed to handle agent stop', ); } } @@ -124,6 +129,72 @@ export class ExecutionOrchestrator { this.scheduleDispatch(); } + /** + * Attempt to run quality review for a stopping agent. + * Returns { reviewStarted: true } if quality review was initiated (callers must NOT call completeTask). + * Returns { reviewStarted: false } if no review needed (caller should call completeTask). + */ + private async tryQualityReview(taskId: string, agentId: string, reason: string): Promise<{ reviewStarted: boolean }> { + if (!this.agentRepository || !this.agentManager) { + return { reviewStarted: false }; + } + + const task = await this.taskRepository.findById(taskId); + if (!task?.phaseId || !task.initiativeId) { + return { reviewStarted: false }; + } + + const initiative = await this.initiativeRepository.findById(task.initiativeId); + if (!initiative?.branch) { + return { reviewStarted: false }; + } + + const phase = await this.phaseRepository.findById(task.phaseId); + if (!phase) { + return { reviewStarted: false }; + } + + const taskBranch = taskBranchName(initiative.branch, taskId); + const baseBranch = phaseBranchName(initiative.branch, phase.name); + + const projects = await this.projectRepository.findProjectsByInitiativeId(task.initiativeId); + if (projects.length === 0) { + return { reviewStarted: false }; + } + + const repoPath = await ensureProjectClone(projects[0], this.workspaceRoot); + + const result = await shouldRunQualityReview({ + agentId, + taskId, + stopReason: reason, + repoPath, + taskBranch, + baseBranch, + agentRepository: this.agentRepository, + taskRepository: this.taskRepository, + initiativeRepository: this.initiativeRepository, + branchManager: this.branchManager, + }); + + if (!result.run) { + return { reviewStarted: false }; + } + + await runQualityReview({ + taskId, + taskBranch, + baseBranch, + initiativeId: task.initiativeId, + qualifyingFiles: result.qualifyingFiles, + taskRepository: this.taskRepository, + agentManager: this.agentManager, + log, + }); + + return { reviewStarted: true }; + } + private async handleAgentCrashed(event: AgentCrashedEvent): Promise { const { taskId, agentId, error } = event.payload; if (!taskId) return; From 953fe2e295f209adb2ca229a6991828b2510ede4 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 22:06:00 +0100 Subject: [PATCH 7/9] docs: update dispatch-events.md to reflect quality review on agent:stopped Co-Authored-By: Claude Sonnet 4.6 --- docs/dispatch-events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dispatch-events.md b/docs/dispatch-events.md index 5d1b4e9..4d6b22f 100644 --- a/docs/dispatch-events.md +++ b/docs/dispatch-events.md @@ -113,7 +113,7 @@ InitiativeChangesRequestedEvent { initiativeId, phaseId, taskId } | Event | Action | |-------|--------| | `phase:queued` | Dispatch ready phases → dispatch their tasks to idle agents | -| `agent:stopped` | Auto-complete task (unless user_requested), re-dispatch queued tasks (freed agent slot) | +| `agent:stopped` | Check quality review eligibility; if eligible: transition task to `quality_review` and spawn review agent; otherwise auto-complete task. Skipped for `user_requested` stops. Re-dispatches queued tasks. | | `agent:crashed` | Auto-retry crashed task up to `MAX_TASK_RETRIES` (3). Increments `retryCount`, resets status to `pending`, re-queues. Exceeding retries leaves task `in_progress` for manual intervention. | | `task:completed` | Merge task branch (if branch exists), check phase completion, dispatch next queued task | From 30dcb8340a9f72d969b1d33ad416047e2003d01c Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 22:10:15 +0100 Subject: [PATCH 8/9] test: add error resilience case to orchestrator quality review hook tests Adds the fourth test case from the spec: when shouldRunQualityReview throws, the orchestrator must not crash, must log a warning (verified implicitly by the catch block), and must still call scheduleDispatch() so dispatch continuity is maintained. Co-Authored-By: Claude Sonnet 4.6 --- apps/server/execution/orchestrator.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/apps/server/execution/orchestrator.test.ts b/apps/server/execution/orchestrator.test.ts index 511a7cd..e3e0bf7 100644 --- a/apps/server/execution/orchestrator.test.ts +++ b/apps/server/execution/orchestrator.test.ts @@ -481,4 +481,21 @@ describe('handleAgentStopped — quality review integration', () => { expect(shouldRunQualityReview).not.toHaveBeenCalled(); expect(mocks.dispatchManager.completeTask).not.toHaveBeenCalled(); }); + + it('does not crash and still calls scheduleDispatch when shouldRunQualityReview throws', async () => { + vi.mocked(shouldRunQualityReview).mockRejectedValue(new Error('quality review check failed')); + + createOrchestrator(mocks); + + mocks.eventBus.emit({ + type: 'agent:stopped', + timestamp: new Date(), + payload: { taskId: 't1', reason: 'task_complete', agentId: 'a1' }, + }); + + // scheduleDispatch() must still run — verifiable via dispatchNext being called in the dispatch cycle + await vi.waitFor(() => expect(mocks.dispatchManager.dispatchNext).toHaveBeenCalled()); + // completeTask is not called from the catch block — error is swallowed after logging a warning + expect(mocks.dispatchManager.completeTask).not.toHaveBeenCalled(); + }); }); From 753b2e9fb8aa94ccfc27e3571c539005b3ec4a35 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 6 Mar 2026 22:16:53 +0100 Subject: [PATCH 9/9] fix: resolve integration issues after phase branch merges - Register errandProcedures in appRouter (was defined but never spread) - Fix nullable projectId guard in errand delete/abandon procedures - Add sendUserMessage stub to MockAgentManager in headquarters and radar-procedures tests (AgentManager interface gained this method) - Add missing qualityReview field to Initiative fixture in file-io test (schema gained this column from the quality-review phase) - Cast conflictFiles access in CLI errand resolve command Co-Authored-By: Claude Sonnet 4.6 --- apps/server/agent/file-io.test.ts | 1 + apps/server/cli/index.ts | 2 +- apps/server/test/unit/headquarters.test.ts | 1 + .../server/test/unit/radar-procedures.test.ts | 1 + apps/server/trpc/router.ts | 2 ++ apps/server/trpc/routers/errand.ts | 28 +++++++++++-------- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/apps/server/agent/file-io.test.ts b/apps/server/agent/file-io.test.ts index ae0fb9a..96bc6a1 100644 --- a/apps/server/agent/file-io.test.ts +++ b/apps/server/agent/file-io.test.ts @@ -52,6 +52,7 @@ describe('writeInputFiles', () => { status: 'active', branch: 'cw/test-initiative', executionMode: 'review_per_phase', + qualityReview: false, createdAt: new Date('2026-01-01'), updatedAt: new Date('2026-01-02'), }; diff --git a/apps/server/cli/index.ts b/apps/server/cli/index.ts index 8fc0425..cda8766 100644 --- a/apps/server/cli/index.ts +++ b/apps/server/cli/index.ts @@ -1878,7 +1878,7 @@ See the Codewalkers documentation for .cw-preview.yml format and options.`; : `.cw-worktrees/${id}`; console.log(`Resolve conflicts in worktree: ${worktreePath}`); console.log('Conflicting files:'); - for (const f of errand.conflictFiles ?? []) { + for (const f of (errand as any).conflictFiles ?? []) { console.log(` ${f}`); } console.log('After resolving: stage and commit changes in the worktree, then run:'); diff --git a/apps/server/test/unit/headquarters.test.ts b/apps/server/test/unit/headquarters.test.ts index bc94e09..0176c5e 100644 --- a/apps/server/test/unit/headquarters.test.ts +++ b/apps/server/test/unit/headquarters.test.ts @@ -63,6 +63,7 @@ class MockAgentManager implements AgentManager { async delete(): Promise { throw new Error('Not implemented'); } async dismiss(): Promise { throw new Error('Not implemented'); } async resumeForConversation(): Promise { return false; } + async sendUserMessage(): Promise { throw new Error('Not implemented'); } } // ============================================================================= diff --git a/apps/server/test/unit/radar-procedures.test.ts b/apps/server/test/unit/radar-procedures.test.ts index d7acba6..01d642e 100644 --- a/apps/server/test/unit/radar-procedures.test.ts +++ b/apps/server/test/unit/radar-procedures.test.ts @@ -69,6 +69,7 @@ class MockAgentManager implements AgentManager { async delete(): Promise { throw new Error('Not implemented'); } async dismiss(): Promise { throw new Error('Not implemented'); } async resumeForConversation(): Promise { return false; } + async sendUserMessage(): Promise { throw new Error('Not implemented'); } } // ============================================================================= diff --git a/apps/server/trpc/router.ts b/apps/server/trpc/router.ts index 43ad5d3..9f24841 100644 --- a/apps/server/trpc/router.ts +++ b/apps/server/trpc/router.ts @@ -25,6 +25,7 @@ import { previewProcedures } from './routers/preview.js'; import { conversationProcedures } from './routers/conversation.js'; import { chatSessionProcedures } from './routers/chat-session.js'; import { headquartersProcedures } from './routers/headquarters.js'; +import { errandProcedures } from './routers/errand.js'; // Re-export tRPC primitives (preserves existing import paths) export { router, publicProcedure, middleware, createCallerFactory } from './trpc.js'; @@ -65,6 +66,7 @@ export const appRouter = router({ ...conversationProcedures(publicProcedure), ...chatSessionProcedures(publicProcedure), ...headquartersProcedures(publicProcedure), + ...errandProcedures(publicProcedure), }); export type AppRouter = typeof appRouter; diff --git a/apps/server/trpc/routers/errand.ts b/apps/server/trpc/routers/errand.ts index 57fb738..39b144c 100644 --- a/apps/server/trpc/routers/errand.ts +++ b/apps/server/trpc/routers/errand.ts @@ -350,12 +350,14 @@ export function errandProcedures(publicProcedure: ProcedureBuilder) { } // Remove worktree and branch (best-effort) - const project = await requireProjectRepository(ctx).findById(errand.projectId); - if (project) { - const clonePath = await resolveClonePath(project, ctx); - const worktreeManager = new SimpleGitWorktreeManager(clonePath); - try { await worktreeManager.remove(errand.id); } catch { /* no-op if already gone */ } - try { await requireBranchManager(ctx).deleteBranch(clonePath, errand.branch); } catch { /* no-op */ } + if (errand.projectId) { + const project = await requireProjectRepository(ctx).findById(errand.projectId); + if (project) { + const clonePath = await resolveClonePath(project, ctx); + const worktreeManager = new SimpleGitWorktreeManager(clonePath); + try { await worktreeManager.remove(errand.id); } catch { /* no-op if already gone */ } + try { await requireBranchManager(ctx).deleteBranch(clonePath, errand.branch); } catch { /* no-op */ } + } } await repo.delete(errand.id); @@ -426,12 +428,14 @@ export function errandProcedures(publicProcedure: ProcedureBuilder) { } // Remove worktree and branch (best-effort) - const project = await requireProjectRepository(ctx).findById(errand.projectId); - if (project) { - const clonePath = await resolveClonePath(project, ctx); - const worktreeManager = new SimpleGitWorktreeManager(clonePath); - try { await worktreeManager.remove(errand.id); } catch { /* no-op if already gone */ } - try { await branchManager.deleteBranch(clonePath, errand.branch); } catch { /* no-op */ } + if (errand.projectId) { + const project = await requireProjectRepository(ctx).findById(errand.projectId); + if (project) { + const clonePath = await resolveClonePath(project, ctx); + const worktreeManager = new SimpleGitWorktreeManager(clonePath); + try { await worktreeManager.remove(errand.id); } catch { /* no-op if already gone */ } + try { await branchManager.deleteBranch(clonePath, errand.branch); } catch { /* no-op */ } + } } const updated = await repo.update(input.id, { status: 'abandoned' });