@mcoda/core 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +21 -0
  3. package/README.md +9 -0
  4. package/dist/api/AgentsApi.d.ts +36 -0
  5. package/dist/api/AgentsApi.d.ts.map +1 -0
  6. package/dist/api/AgentsApi.js +176 -0
  7. package/dist/api/QaTasksApi.d.ts +8 -0
  8. package/dist/api/QaTasksApi.d.ts.map +1 -0
  9. package/dist/api/QaTasksApi.js +36 -0
  10. package/dist/api/TasksApi.d.ts +7 -0
  11. package/dist/api/TasksApi.d.ts.map +1 -0
  12. package/dist/api/TasksApi.js +34 -0
  13. package/dist/config/ConfigService.d.ts +3 -0
  14. package/dist/config/ConfigService.d.ts.map +1 -0
  15. package/dist/config/ConfigService.js +2 -0
  16. package/dist/domain/dependencies/Dependency.d.ts +3 -0
  17. package/dist/domain/dependencies/Dependency.d.ts.map +1 -0
  18. package/dist/domain/dependencies/Dependency.js +2 -0
  19. package/dist/domain/epics/Epic.d.ts +3 -0
  20. package/dist/domain/epics/Epic.d.ts.map +1 -0
  21. package/dist/domain/epics/Epic.js +2 -0
  22. package/dist/domain/projects/Project.d.ts +3 -0
  23. package/dist/domain/projects/Project.d.ts.map +1 -0
  24. package/dist/domain/projects/Project.js +2 -0
  25. package/dist/domain/tasks/Task.d.ts +3 -0
  26. package/dist/domain/tasks/Task.d.ts.map +1 -0
  27. package/dist/domain/tasks/Task.js +2 -0
  28. package/dist/domain/userStories/UserStory.d.ts +3 -0
  29. package/dist/domain/userStories/UserStory.d.ts.map +1 -0
  30. package/dist/domain/userStories/UserStory.js +2 -0
  31. package/dist/index.d.ts +28 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +27 -0
  34. package/dist/prompts/PdrPrompts.d.ts +4 -0
  35. package/dist/prompts/PdrPrompts.d.ts.map +1 -0
  36. package/dist/prompts/PdrPrompts.js +21 -0
  37. package/dist/prompts/PromptLoader.d.ts +3 -0
  38. package/dist/prompts/PromptLoader.d.ts.map +1 -0
  39. package/dist/prompts/PromptLoader.js +2 -0
  40. package/dist/prompts/SdsPrompts.d.ts +5 -0
  41. package/dist/prompts/SdsPrompts.d.ts.map +1 -0
  42. package/dist/prompts/SdsPrompts.js +44 -0
  43. package/dist/services/agents/AgentManagementService.d.ts +3 -0
  44. package/dist/services/agents/AgentManagementService.d.ts.map +1 -0
  45. package/dist/services/agents/AgentManagementService.js +2 -0
  46. package/dist/services/agents/GatewayAgentService.d.ts +92 -0
  47. package/dist/services/agents/GatewayAgentService.d.ts.map +1 -0
  48. package/dist/services/agents/GatewayAgentService.js +870 -0
  49. package/dist/services/agents/RoutingApiClient.d.ts +23 -0
  50. package/dist/services/agents/RoutingApiClient.d.ts.map +1 -0
  51. package/dist/services/agents/RoutingApiClient.js +62 -0
  52. package/dist/services/agents/RoutingService.d.ts +50 -0
  53. package/dist/services/agents/RoutingService.d.ts.map +1 -0
  54. package/dist/services/agents/RoutingService.js +386 -0
  55. package/dist/services/agents/generated/RoutingApiClient.d.ts +21 -0
  56. package/dist/services/agents/generated/RoutingApiClient.d.ts.map +1 -0
  57. package/dist/services/agents/generated/RoutingApiClient.js +68 -0
  58. package/dist/services/backlog/BacklogService.d.ts +98 -0
  59. package/dist/services/backlog/BacklogService.d.ts.map +1 -0
  60. package/dist/services/backlog/BacklogService.js +453 -0
  61. package/dist/services/backlog/TaskOrderingService.d.ts +88 -0
  62. package/dist/services/backlog/TaskOrderingService.d.ts.map +1 -0
  63. package/dist/services/backlog/TaskOrderingService.js +675 -0
  64. package/dist/services/docs/DocsService.d.ts +82 -0
  65. package/dist/services/docs/DocsService.d.ts.map +1 -0
  66. package/dist/services/docs/DocsService.js +1631 -0
  67. package/dist/services/estimate/EstimateService.d.ts +12 -0
  68. package/dist/services/estimate/EstimateService.d.ts.map +1 -0
  69. package/dist/services/estimate/EstimateService.js +103 -0
  70. package/dist/services/estimate/VelocityService.d.ts +19 -0
  71. package/dist/services/estimate/VelocityService.d.ts.map +1 -0
  72. package/dist/services/estimate/VelocityService.js +237 -0
  73. package/dist/services/estimate/types.d.ts +30 -0
  74. package/dist/services/estimate/types.d.ts.map +1 -0
  75. package/dist/services/estimate/types.js +1 -0
  76. package/dist/services/execution/ExecutionService.d.ts +3 -0
  77. package/dist/services/execution/ExecutionService.d.ts.map +1 -0
  78. package/dist/services/execution/ExecutionService.js +2 -0
  79. package/dist/services/execution/QaFollowupService.d.ts +38 -0
  80. package/dist/services/execution/QaFollowupService.d.ts.map +1 -0
  81. package/dist/services/execution/QaFollowupService.js +236 -0
  82. package/dist/services/execution/QaProfileService.d.ts +22 -0
  83. package/dist/services/execution/QaProfileService.d.ts.map +1 -0
  84. package/dist/services/execution/QaProfileService.js +142 -0
  85. package/dist/services/execution/QaTasksService.d.ts +101 -0
  86. package/dist/services/execution/QaTasksService.d.ts.map +1 -0
  87. package/dist/services/execution/QaTasksService.js +1117 -0
  88. package/dist/services/execution/TaskSelectionService.d.ts +50 -0
  89. package/dist/services/execution/TaskSelectionService.d.ts.map +1 -0
  90. package/dist/services/execution/TaskSelectionService.js +281 -0
  91. package/dist/services/execution/TaskStateService.d.ts +19 -0
  92. package/dist/services/execution/TaskStateService.d.ts.map +1 -0
  93. package/dist/services/execution/TaskStateService.js +59 -0
  94. package/dist/services/execution/WorkOnTasksService.d.ts +80 -0
  95. package/dist/services/execution/WorkOnTasksService.d.ts.map +1 -0
  96. package/dist/services/execution/WorkOnTasksService.js +1833 -0
  97. package/dist/services/jobs/JobInsightsService.d.ts +97 -0
  98. package/dist/services/jobs/JobInsightsService.d.ts.map +1 -0
  99. package/dist/services/jobs/JobInsightsService.js +263 -0
  100. package/dist/services/jobs/JobResumeService.d.ts +16 -0
  101. package/dist/services/jobs/JobResumeService.d.ts.map +1 -0
  102. package/dist/services/jobs/JobResumeService.js +113 -0
  103. package/dist/services/jobs/JobService.d.ts +149 -0
  104. package/dist/services/jobs/JobService.d.ts.map +1 -0
  105. package/dist/services/jobs/JobService.js +490 -0
  106. package/dist/services/jobs/JobsApiClient.d.ts +73 -0
  107. package/dist/services/jobs/JobsApiClient.d.ts.map +1 -0
  108. package/dist/services/jobs/JobsApiClient.js +67 -0
  109. package/dist/services/openapi/OpenApiService.d.ts +54 -0
  110. package/dist/services/openapi/OpenApiService.d.ts.map +1 -0
  111. package/dist/services/openapi/OpenApiService.js +503 -0
  112. package/dist/services/planning/CreateTasksService.d.ts +68 -0
  113. package/dist/services/planning/CreateTasksService.d.ts.map +1 -0
  114. package/dist/services/planning/CreateTasksService.js +989 -0
  115. package/dist/services/planning/KeyHelpers.d.ts +5 -0
  116. package/dist/services/planning/KeyHelpers.d.ts.map +1 -0
  117. package/dist/services/planning/KeyHelpers.js +62 -0
  118. package/dist/services/planning/PlanningService.d.ts +3 -0
  119. package/dist/services/planning/PlanningService.d.ts.map +1 -0
  120. package/dist/services/planning/PlanningService.js +2 -0
  121. package/dist/services/planning/RefineTasksService.d.ts +56 -0
  122. package/dist/services/planning/RefineTasksService.d.ts.map +1 -0
  123. package/dist/services/planning/RefineTasksService.js +1328 -0
  124. package/dist/services/review/CodeReviewService.d.ts +103 -0
  125. package/dist/services/review/CodeReviewService.d.ts.map +1 -0
  126. package/dist/services/review/CodeReviewService.js +1187 -0
  127. package/dist/services/system/SystemUpdateService.d.ts +55 -0
  128. package/dist/services/system/SystemUpdateService.d.ts.map +1 -0
  129. package/dist/services/system/SystemUpdateService.js +136 -0
  130. package/dist/services/tasks/TaskApiResolver.d.ts +7 -0
  131. package/dist/services/tasks/TaskApiResolver.d.ts.map +1 -0
  132. package/dist/services/tasks/TaskApiResolver.js +41 -0
  133. package/dist/services/tasks/TaskDetailService.d.ts +106 -0
  134. package/dist/services/tasks/TaskDetailService.d.ts.map +1 -0
  135. package/dist/services/tasks/TaskDetailService.js +332 -0
  136. package/dist/services/telemetry/TelemetryService.d.ts +53 -0
  137. package/dist/services/telemetry/TelemetryService.d.ts.map +1 -0
  138. package/dist/services/telemetry/TelemetryService.js +434 -0
  139. package/dist/workspace/WorkspaceManager.d.ts +35 -0
  140. package/dist/workspace/WorkspaceManager.d.ts.map +1 -0
  141. package/dist/workspace/WorkspaceManager.js +201 -0
  142. package/package.json +45 -0
@@ -0,0 +1,98 @@
1
+ import { WorkspaceResolution } from "../../workspace/WorkspaceManager.js";
2
+ export interface BacklogTotals {
3
+ implementation: {
4
+ tasks: number;
5
+ story_points: number;
6
+ };
7
+ review: {
8
+ tasks: number;
9
+ story_points: number;
10
+ };
11
+ qa: {
12
+ tasks: number;
13
+ story_points: number;
14
+ };
15
+ done: {
16
+ tasks: number;
17
+ story_points: number;
18
+ };
19
+ }
20
+ export interface EpicBacklogSummary {
21
+ epic_id: string;
22
+ epic_key: string;
23
+ title: string;
24
+ priority: number | null;
25
+ description?: string;
26
+ totals: BacklogTotals;
27
+ stories: StoryBacklogSummary[];
28
+ }
29
+ export interface StoryBacklogSummary {
30
+ user_story_id: string;
31
+ user_story_key: string;
32
+ epic_key: string;
33
+ title: string;
34
+ description?: string;
35
+ priority: number | null;
36
+ status?: string;
37
+ totals: BacklogTotals;
38
+ }
39
+ export interface TaskBacklogRow {
40
+ task_id: string;
41
+ task_key: string;
42
+ epic_key: string;
43
+ user_story_key: string;
44
+ title: string;
45
+ description: string;
46
+ status: string;
47
+ story_points: number | null;
48
+ priority: number | null;
49
+ assignee: string | null;
50
+ dependency_keys: string[];
51
+ }
52
+ export interface BacklogSummary {
53
+ scope: {
54
+ project_id: string | null;
55
+ project_key: string | null;
56
+ epic_key?: string;
57
+ user_story_key?: string;
58
+ assignee?: string;
59
+ };
60
+ totals: BacklogTotals;
61
+ epics: EpicBacklogSummary[];
62
+ tasks: TaskBacklogRow[];
63
+ }
64
+ export interface BacklogQueryOptions {
65
+ projectKey?: string;
66
+ epicKey?: string;
67
+ storyKey?: string;
68
+ assignee?: string;
69
+ statuses?: string[];
70
+ orderByDependencies?: boolean;
71
+ verbose?: boolean;
72
+ }
73
+ export interface BacklogResult {
74
+ summary: BacklogSummary;
75
+ warnings: string[];
76
+ }
77
+ export declare class BacklogService {
78
+ private workspace;
79
+ private db;
80
+ private connection;
81
+ private warnings;
82
+ private epicPriority;
83
+ private storyPriority;
84
+ private constructor();
85
+ static create(workspace: WorkspaceResolution): Promise<BacklogService>;
86
+ close(): Promise<void>;
87
+ getBacklog(options?: BacklogQueryOptions): Promise<BacklogResult>;
88
+ private getProject;
89
+ private getEpic;
90
+ private getStory;
91
+ private fetchTasks;
92
+ private fetchDependencies;
93
+ private defaultOrder;
94
+ private orderTasks;
95
+ private topologicalSort;
96
+ private sortEpics;
97
+ }
98
+ //# sourceMappingURL=BacklogService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BacklogService.d.ts","sourceRoot":"","sources":["../../../src/services/backlog/BacklogService.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAS1E,MAAM,WAAW,aAAa;IAC5B,cAAc,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC;IACxD,MAAM,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC;IAChD,EAAE,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC;IAC5C,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC;CAC/C;AAED,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,aAAa,CAAC;IACt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AM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,cAAc,CAAC;IACxB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAqED,qBAAa,cAAc;IAMvB,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,EAAE;IACV,OAAO,CAAC,UAAU;IAPpB,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,YAAY,CAAoC;IACxD,OAAO,CAAC,aAAa,CAAoC;IAEzD,OAAO;WAMM,MAAM,CAAC,SAAS,EAAE,mBAAmB,GAAG,OAAO,CAAC,cAAc,CAAC;IAgBtE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,UAAU,CAAC,OAAO,GAAE,mBAAwB,GAAG,OAAO,CAAC,aAAa,CAAC;YAoK7D,UAAU;YAMV,OAAO;YASP,QAAQ;YASR,UAAU;YA8DV,iBAAiB;IAqB/B,OAAO,CAAC,YAAY;IA2BpB,OAAO,CAAC,UAAU;IAkBlB,OAAO,CAAC,eAAe;IA8DvB,OAAO,CAAC,SAAS;CAoBlB"}
@@ -0,0 +1,453 @@
1
+ import fs from "node:fs/promises";
2
+ import { Connection } from "@mcoda/db";
3
+ import { PathHelper } from "@mcoda/shared";
4
+ import { TaskOrderingService } from "./TaskOrderingService.js";
5
+ const IMPLEMENTATION_STATUSES = new Set(["not_started", "in_progress", "blocked"]);
6
+ const REVIEW_STATUSES = new Set(["ready_to_review"]);
7
+ const QA_STATUSES = new Set(["ready_to_qa"]);
8
+ const DONE_STATUSES = new Set(["completed", "cancelled"]);
9
+ const emptyTotals = () => ({
10
+ implementation: { tasks: 0, story_points: 0 },
11
+ review: { tasks: 0, story_points: 0 },
12
+ qa: { tasks: 0, story_points: 0 },
13
+ done: { tasks: 0, story_points: 0 },
14
+ });
15
+ const bucketForStatus = (status) => {
16
+ const normalized = status?.toLowerCase() ?? "";
17
+ if (IMPLEMENTATION_STATUSES.has(normalized))
18
+ return "implementation";
19
+ if (REVIEW_STATUSES.has(normalized))
20
+ return "review";
21
+ if (QA_STATUSES.has(normalized))
22
+ return "qa";
23
+ if (DONE_STATUSES.has(normalized))
24
+ return "done";
25
+ return "implementation";
26
+ };
27
+ const addToTotals = (totals, lane, storyPoints) => {
28
+ totals[lane].tasks += 1;
29
+ if (typeof storyPoints === "number" && Number.isFinite(storyPoints)) {
30
+ totals[lane].story_points += storyPoints;
31
+ }
32
+ };
33
+ const truncate = (value, max = 100) => {
34
+ if (!value)
35
+ return "";
36
+ if (value.length <= max)
37
+ return value;
38
+ return `${value.slice(0, max - 3)}...`;
39
+ };
40
+ const hasTables = async (db, required) => {
41
+ const placeholders = required.map(() => "?").join(", ");
42
+ const rows = await db.all(`SELECT name FROM sqlite_master WHERE type = 'table' AND name IN (${placeholders})`, required);
43
+ return rows.length === required.length;
44
+ };
45
+ const deriveStoryStatus = (statuses) => {
46
+ const order = ["completed", "cancelled", "ready_to_qa", "ready_to_review", "in_progress", "blocked", "not_started"];
47
+ for (const status of order) {
48
+ if (statuses.has(status))
49
+ return status;
50
+ }
51
+ return undefined;
52
+ };
53
+ export class BacklogService {
54
+ constructor(workspace, db, connection) {
55
+ this.workspace = workspace;
56
+ this.db = db;
57
+ this.connection = connection;
58
+ this.warnings = [];
59
+ this.epicPriority = new Map();
60
+ this.storyPriority = new Map();
61
+ }
62
+ static async create(workspace) {
63
+ const dbPath = PathHelper.getWorkspaceDbPath(workspace.workspaceRoot);
64
+ try {
65
+ await fs.access(dbPath);
66
+ }
67
+ catch {
68
+ throw new Error(`No workspace DB found at ${dbPath}. Run mcoda init or create-tasks first.`);
69
+ }
70
+ const connection = await Connection.open(dbPath);
71
+ const ok = await hasTables(connection.db, ["projects", "epics", "user_stories", "tasks"]);
72
+ if (!ok) {
73
+ await connection.close();
74
+ throw new Error(`Workspace DB at ${dbPath} is missing required tables. Re-run create-tasks to seed it.`);
75
+ }
76
+ return new BacklogService(workspace, connection.db, connection);
77
+ }
78
+ async close() {
79
+ await this.connection.close();
80
+ }
81
+ async getBacklog(options = {}) {
82
+ this.warnings = [];
83
+ this.epicPriority.clear();
84
+ this.storyPriority.clear();
85
+ const project = options.projectKey ? await this.getProject(options.projectKey) : undefined;
86
+ if (options.projectKey && !project) {
87
+ throw new Error(`Unknown project key: ${options.projectKey}`);
88
+ }
89
+ const epic = options.epicKey ? await this.getEpic(options.epicKey, project?.id) : undefined;
90
+ if (options.epicKey && !epic) {
91
+ throw new Error(`Unknown epic key: ${options.epicKey}`);
92
+ }
93
+ const story = options.storyKey ? await this.getStory(options.storyKey, epic?.id ?? project?.id) : undefined;
94
+ if (options.storyKey && !story) {
95
+ throw new Error(`Unknown user story key: ${options.storyKey}`);
96
+ }
97
+ const tasks = await this.fetchTasks({
98
+ projectId: project?.id,
99
+ epicId: epic?.id,
100
+ storyId: story?.id,
101
+ assignee: options.assignee,
102
+ statuses: options.statuses,
103
+ });
104
+ const dependencyMap = await this.fetchDependencies(tasks.map((t) => t.task_id));
105
+ const totals = emptyTotals();
106
+ const epics = new Map();
107
+ const tasksRows = [];
108
+ for (const row of tasks) {
109
+ const lane = bucketForStatus(row.task_status);
110
+ addToTotals(totals, lane, row.task_story_points);
111
+ let epicSummary = epics.get(row.epic_id);
112
+ if (!epicSummary) {
113
+ epicSummary = {
114
+ epic_id: row.epic_id,
115
+ epic_key: row.epic_key,
116
+ title: row.epic_title,
117
+ priority: row.epic_priority ?? null,
118
+ description: row.epic_description ?? undefined,
119
+ totals: emptyTotals(),
120
+ stories: [],
121
+ storiesMap: new Map(),
122
+ };
123
+ epics.set(row.epic_id, epicSummary);
124
+ }
125
+ this.epicPriority.set(row.epic_key, row.epic_priority ?? null);
126
+ addToTotals(epicSummary.totals, lane, row.task_story_points);
127
+ let storySummary = epicSummary.storiesMap.get(row.story_id);
128
+ if (!storySummary) {
129
+ storySummary = {
130
+ user_story_id: row.story_id,
131
+ user_story_key: row.story_key,
132
+ epic_key: row.epic_key,
133
+ title: row.story_title,
134
+ description: row.story_description ?? undefined,
135
+ priority: row.story_priority ?? null,
136
+ status: undefined,
137
+ totals: emptyTotals(),
138
+ statuses: new Set(),
139
+ };
140
+ epicSummary.storiesMap.set(row.story_id, storySummary);
141
+ }
142
+ this.storyPriority.set(row.story_key, row.story_priority ?? null);
143
+ addToTotals(storySummary.totals, lane, row.task_story_points);
144
+ storySummary.statuses.add(row.task_status.toLowerCase());
145
+ const dependencies = dependencyMap.get(row.task_id) ?? [];
146
+ tasksRows.push({
147
+ task_id: row.task_id,
148
+ task_key: row.task_key,
149
+ epic_key: row.epic_key,
150
+ user_story_key: row.story_key,
151
+ title: row.task_title,
152
+ description: row.task_description ?? "",
153
+ status: row.task_status,
154
+ story_points: row.task_story_points ?? null,
155
+ priority: row.task_priority ?? null,
156
+ assignee: row.assignee_human ?? null,
157
+ dependency_keys: dependencies,
158
+ });
159
+ }
160
+ let orderedTasks;
161
+ if (options.orderByDependencies) {
162
+ if (!project) {
163
+ this.warnings.push("Dependency ordering requires a project scope; using default ordering.");
164
+ orderedTasks = this.orderTasks(tasksRows, options.verbose === true);
165
+ }
166
+ else {
167
+ const orderingService = await TaskOrderingService.create(this.workspace, { recordTelemetry: false });
168
+ try {
169
+ const ordering = await orderingService.orderTasks({
170
+ projectKey: project.key,
171
+ epicKey: epic?.key,
172
+ storyKey: story?.key,
173
+ assignee: options.assignee,
174
+ statusFilter: options.statuses,
175
+ includeBlocked: true,
176
+ });
177
+ const orderMap = new Map(ordering.ordered.map((t, idx) => [t.taskId, idx]));
178
+ orderedTasks = tasksRows
179
+ .slice()
180
+ .sort((a, b) => {
181
+ const ai = orderMap.get(a.task_id) ?? Number.MAX_SAFE_INTEGER;
182
+ const bi = orderMap.get(b.task_id) ?? Number.MAX_SAFE_INTEGER;
183
+ if (ai !== bi)
184
+ return ai - bi;
185
+ return a.task_key.localeCompare(b.task_key);
186
+ });
187
+ this.warnings.push(...ordering.warnings);
188
+ }
189
+ catch (error) {
190
+ if (options.verbose) {
191
+ this.warnings.push(`Dependency ordering failed; falling back to heuristic ordering. ${error.message}`);
192
+ }
193
+ orderedTasks = this.orderTasks(tasksRows, options.verbose === true);
194
+ }
195
+ finally {
196
+ await orderingService.close();
197
+ }
198
+ }
199
+ }
200
+ else {
201
+ orderedTasks = this.defaultOrder(tasksRows);
202
+ }
203
+ const epicSummaries = Array.from(epics.values()).map((e) => {
204
+ const stories = Array.from(e.storiesMap.values()).map((s) => ({
205
+ user_story_id: s.user_story_id,
206
+ user_story_key: s.user_story_key,
207
+ epic_key: s.epic_key,
208
+ title: s.title,
209
+ description: s.description,
210
+ priority: s.priority,
211
+ status: deriveStoryStatus(s.statuses),
212
+ totals: s.totals,
213
+ }));
214
+ return {
215
+ epic_id: e.epic_id,
216
+ epic_key: e.epic_key,
217
+ title: e.title,
218
+ priority: e.priority,
219
+ description: e.description,
220
+ totals: e.totals,
221
+ stories,
222
+ };
223
+ });
224
+ return {
225
+ summary: {
226
+ scope: {
227
+ project_id: project?.id ?? null,
228
+ project_key: project?.key ?? null,
229
+ epic_key: epic?.key,
230
+ user_story_key: story?.key,
231
+ assignee: options.assignee,
232
+ },
233
+ totals,
234
+ epics: this.sortEpics(epicSummaries),
235
+ tasks: orderedTasks,
236
+ },
237
+ warnings: this.warnings,
238
+ };
239
+ }
240
+ async getProject(projectKey) {
241
+ const row = await this.db.get(`SELECT id, key FROM projects WHERE key = ?`, projectKey);
242
+ if (!row)
243
+ return undefined;
244
+ return { id: row.id, key: row.key };
245
+ }
246
+ async getEpic(epicKey, projectId) {
247
+ const row = await this.db.get(`SELECT id, key, project_id FROM epics WHERE key = ? ${projectId ? "AND project_id = ?" : ""}`, projectId ? [epicKey, projectId] : [epicKey]);
248
+ if (!row)
249
+ return undefined;
250
+ return { id: row.id, key: row.key, project_id: row.project_id };
251
+ }
252
+ async getStory(storyKey, scopeId) {
253
+ const row = await this.db.get(`SELECT id, key, epic_id FROM user_stories WHERE key = ? ${scopeId ? "AND (epic_id = ? OR project_id = ?)" : ""}`, scopeId ? [storyKey, scopeId, scopeId] : [storyKey]);
254
+ if (!row)
255
+ return undefined;
256
+ return { id: row.id, key: row.key, epic_id: row.epic_id };
257
+ }
258
+ async fetchTasks(filters) {
259
+ const clauses = [];
260
+ const params = [];
261
+ if (filters.projectId) {
262
+ clauses.push("t.project_id = ?");
263
+ params.push(filters.projectId);
264
+ }
265
+ if (filters.epicId) {
266
+ clauses.push("t.epic_id = ?");
267
+ params.push(filters.epicId);
268
+ }
269
+ if (filters.storyId) {
270
+ clauses.push("t.user_story_id = ?");
271
+ params.push(filters.storyId);
272
+ }
273
+ if (filters.assignee) {
274
+ clauses.push("LOWER(t.assignee_human) = LOWER(?)");
275
+ params.push(filters.assignee);
276
+ }
277
+ if (filters.statuses && filters.statuses.length > 0) {
278
+ const placeholders = filters.statuses.map(() => "?").join(", ");
279
+ clauses.push(`LOWER(t.status) IN (${placeholders})`);
280
+ params.push(...filters.statuses.map((s) => s.toLowerCase()));
281
+ }
282
+ const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
283
+ const rows = await this.db.all(`
284
+ SELECT
285
+ t.id AS task_id,
286
+ t.key AS task_key,
287
+ t.title AS task_title,
288
+ t.description AS task_description,
289
+ t.status AS task_status,
290
+ t.story_points AS task_story_points,
291
+ t.priority AS task_priority,
292
+ t.assignee_human AS assignee_human,
293
+ e.id AS epic_id,
294
+ e.key AS epic_key,
295
+ e.title AS epic_title,
296
+ e.priority AS epic_priority,
297
+ e.description AS epic_description,
298
+ s.id AS story_id,
299
+ s.key AS story_key,
300
+ s.title AS story_title,
301
+ s.priority AS story_priority,
302
+ s.description AS story_description
303
+ FROM tasks t
304
+ INNER JOIN epics e ON t.epic_id = e.id
305
+ INNER JOIN user_stories s ON t.user_story_id = s.id
306
+ ${where}
307
+ `, params);
308
+ return rows;
309
+ }
310
+ async fetchDependencies(taskIds) {
311
+ const map = new Map();
312
+ if (taskIds.length === 0)
313
+ return map;
314
+ const placeholders = taskIds.map(() => "?").join(", ");
315
+ const rows = await this.db.all(`
316
+ SELECT td.task_id, dep.key AS dependency_key
317
+ FROM task_dependencies td
318
+ INNER JOIN tasks dep ON dep.id = td.depends_on_task_id
319
+ WHERE td.task_id IN (${placeholders})
320
+ `, taskIds);
321
+ for (const row of rows) {
322
+ const list = map.get(row.task_id) ?? [];
323
+ list.push(row.dependency_key);
324
+ map.set(row.task_id, list);
325
+ }
326
+ return map;
327
+ }
328
+ defaultOrder(tasks) {
329
+ const bucketOrder = {
330
+ implementation: 0,
331
+ review: 1,
332
+ qa: 2,
333
+ done: 3,
334
+ };
335
+ const toNum = (value) => (value === null || value === undefined ? Number.MAX_SAFE_INTEGER : value);
336
+ return tasks
337
+ .slice()
338
+ .sort((a, b) => {
339
+ const laneA = bucketOrder[bucketForStatus(a.status)];
340
+ const laneB = bucketOrder[bucketForStatus(b.status)];
341
+ if (laneA !== laneB)
342
+ return laneA - laneB;
343
+ const epicPriorityA = toNum(this.epicPriority.get(a.epic_key) ?? null);
344
+ const epicPriorityB = toNum(this.epicPriority.get(b.epic_key) ?? null);
345
+ if (epicPriorityA !== epicPriorityB)
346
+ return epicPriorityA - epicPriorityB;
347
+ const storyPriorityA = toNum(this.storyPriority.get(a.user_story_key) ?? null);
348
+ const storyPriorityB = toNum(this.storyPriority.get(b.user_story_key) ?? null);
349
+ if (storyPriorityA !== storyPriorityB)
350
+ return storyPriorityA - storyPriorityB;
351
+ const taskPriorityA = toNum(a.priority);
352
+ const taskPriorityB = toNum(b.priority);
353
+ if (taskPriorityA !== taskPriorityB)
354
+ return taskPriorityA - taskPriorityB;
355
+ return a.task_key.localeCompare(b.task_key);
356
+ });
357
+ }
358
+ orderTasks(tasks, verbose) {
359
+ const bucketOrder = ["implementation", "review", "qa", "done"];
360
+ const allKeys = new Set(tasks.map((t) => t.task_key));
361
+ const ordered = [];
362
+ for (const bucket of bucketOrder) {
363
+ const bucketTasks = tasks.filter((t) => bucketForStatus(t.status) === bucket);
364
+ const { sorted, hadCycle, missingReference } = this.topologicalSort(bucketTasks, allKeys);
365
+ if (hadCycle && verbose) {
366
+ this.warnings.push(`Dependency cycle detected in ${bucket} bucket. Falling back to priority order.`);
367
+ }
368
+ if (missingReference && verbose) {
369
+ this.warnings.push(`Missing dependency reference in ${bucket} bucket. Ordering may be partial.`);
370
+ }
371
+ ordered.push(...sorted);
372
+ }
373
+ return ordered;
374
+ }
375
+ topologicalSort(tasks, allKeys) {
376
+ if (tasks.length === 0)
377
+ return { sorted: [], hadCycle: false, missingReference: false };
378
+ const idByKey = new Map(tasks.map((t, idx) => [t.task_key, idx]));
379
+ const indegree = new Array(tasks.length).fill(0);
380
+ const edges = new Map();
381
+ const fallback = this.defaultOrder(tasks);
382
+ const indexByTask = new Map();
383
+ fallback.forEach((task, idx) => indexByTask.set(task.task_id, idx));
384
+ let missingReference = false;
385
+ tasks.forEach((task, idx) => {
386
+ for (const depKey of task.dependency_keys) {
387
+ if (!allKeys.has(depKey)) {
388
+ missingReference = true;
389
+ continue;
390
+ }
391
+ const depIdx = idByKey.get(depKey);
392
+ if (depIdx === undefined) {
393
+ continue;
394
+ }
395
+ indegree[idx] += 1;
396
+ const list = edges.get(depIdx) ?? [];
397
+ list.push(idx);
398
+ edges.set(depIdx, list);
399
+ }
400
+ });
401
+ const queue = [];
402
+ indegree.forEach((value, idx) => {
403
+ if (value === 0)
404
+ queue.push(idx);
405
+ });
406
+ const stableSort = (arr) => arr.sort((a, b) => {
407
+ const fallbackA = indexByTask.get(tasks[a].task_id) ?? 0;
408
+ const fallbackB = indexByTask.get(tasks[b].task_id) ?? 0;
409
+ return fallbackA - fallbackB;
410
+ });
411
+ stableSort(queue);
412
+ const result = [];
413
+ while (queue.length > 0) {
414
+ const current = queue.shift();
415
+ result.push(tasks[current]);
416
+ const neighbors = edges.get(current) ?? [];
417
+ for (const next of neighbors) {
418
+ indegree[next] -= 1;
419
+ if (indegree[next] === 0) {
420
+ queue.push(next);
421
+ }
422
+ }
423
+ stableSort(queue);
424
+ }
425
+ const hadCycle = result.length !== tasks.length;
426
+ if (hadCycle) {
427
+ return { sorted: fallback, hadCycle: true, missingReference };
428
+ }
429
+ return { sorted: result, hadCycle: false, missingReference };
430
+ }
431
+ sortEpics(epics) {
432
+ const toNum = (value) => (value === null || value === undefined ? Number.MAX_SAFE_INTEGER : value);
433
+ return epics
434
+ .slice()
435
+ .sort((a, b) => {
436
+ const priority = toNum(a.priority) - toNum(b.priority);
437
+ if (priority !== 0)
438
+ return priority;
439
+ return a.epic_key.localeCompare(b.epic_key);
440
+ })
441
+ .map((epic) => ({
442
+ ...epic,
443
+ stories: epic.stories
444
+ .slice()
445
+ .sort((a, b) => {
446
+ const priority = toNum(a.priority) - toNum(b.priority);
447
+ if (priority !== 0)
448
+ return priority;
449
+ return a.user_story_key.localeCompare(b.user_story_key);
450
+ }),
451
+ }));
452
+ }
453
+ }
@@ -0,0 +1,88 @@
1
+ import { WorkspaceResolution } from "../../workspace/WorkspaceManager.js";
2
+ interface ProjectRow {
3
+ id: string;
4
+ key: string;
5
+ name?: string | null;
6
+ }
7
+ interface EpicRow {
8
+ id: string;
9
+ key: string;
10
+ project_id: string;
11
+ title: string;
12
+ }
13
+ interface DependencyImpact {
14
+ direct: number;
15
+ total: number;
16
+ }
17
+ export interface TaskOrderItem {
18
+ taskId: string;
19
+ taskKey: string;
20
+ title: string;
21
+ status: string;
22
+ storyPoints: number | null;
23
+ priority: number;
24
+ epicId: string;
25
+ epicKey: string;
26
+ storyId: string;
27
+ storyKey: string;
28
+ storyTitle: string;
29
+ blocked: boolean;
30
+ blockedBy: string[];
31
+ dependencyKeys: string[];
32
+ dependencyImpact: DependencyImpact;
33
+ cycleDetected?: boolean;
34
+ metadata?: Record<string, unknown> | null;
35
+ }
36
+ export interface TaskOrderingResult {
37
+ project: ProjectRow;
38
+ epic?: EpicRow;
39
+ ordered: TaskOrderItem[];
40
+ blocked: TaskOrderItem[];
41
+ warnings: string[];
42
+ jobId?: string;
43
+ commandRunId?: string;
44
+ }
45
+ export interface TaskOrderingRequest {
46
+ projectKey: string;
47
+ epicKey?: string;
48
+ storyKey?: string;
49
+ assignee?: string;
50
+ statusFilter?: string[];
51
+ includeBlocked?: boolean;
52
+ agentName?: string;
53
+ agentStream?: boolean;
54
+ }
55
+ export declare class TaskOrderingService {
56
+ private workspace;
57
+ private db;
58
+ private repo;
59
+ private jobService;
60
+ private agentService;
61
+ private globalRepo;
62
+ private routingService;
63
+ private docdex;
64
+ private recordTelemetry;
65
+ private constructor();
66
+ static create(workspace: WorkspaceResolution, options?: {
67
+ recordTelemetry?: boolean;
68
+ }): Promise<TaskOrderingService>;
69
+ private buildDocContext;
70
+ close(): Promise<void>;
71
+ private getProject;
72
+ private getEpic;
73
+ private getStory;
74
+ private fetchTasks;
75
+ private fetchDependencies;
76
+ private dependencyImpactMap;
77
+ private compareTasks;
78
+ private topologicalSort;
79
+ private buildNodes;
80
+ private resolveAgent;
81
+ private invokeAgent;
82
+ private applyAgentRanking;
83
+ private persistPriorities;
84
+ private mapResult;
85
+ orderTasks(request: TaskOrderingRequest): Promise<TaskOrderingResult>;
86
+ }
87
+ export {};
88
+ //# sourceMappingURL=TaskOrderingService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TaskOrderingService.d.ts","sourceRoot":"","sources":["../../../src/services/backlog/TaskOrderingService.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AA+C1E,UAAU,UAAU;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB;AAED,UAAU,OAAO;IACf,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,MAAM,CAAC;IACZ,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