@critical-path/core 0.35.0 → 0.36.1

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 (187) hide show
  1. package/README.md +11 -0
  2. package/dist/engine/index.d.ts +2 -0
  3. package/dist/engine/index.d.ts.map +1 -1
  4. package/dist/engine/index.js +25 -2
  5. package/dist/engine/index.js.map +1 -1
  6. package/dist/types/index.d.ts +11 -0
  7. package/dist/types/index.d.ts.map +1 -1
  8. package/package.json +5 -1
  9. package/CHANGELOG.md +0 -584
  10. package/dist/deliverables.test.d.ts +0 -2
  11. package/dist/deliverables.test.d.ts.map +0 -1
  12. package/dist/deliverables.test.js +0 -232
  13. package/dist/deliverables.test.js.map +0 -1
  14. package/dist/domain/calendar.test.d.ts +0 -2
  15. package/dist/domain/calendar.test.d.ts.map +0 -1
  16. package/dist/domain/calendar.test.js +0 -181
  17. package/dist/domain/calendar.test.js.map +0 -1
  18. package/dist/domain/cpm.test.d.ts +0 -2
  19. package/dist/domain/cpm.test.d.ts.map +0 -1
  20. package/dist/domain/cpm.test.js +0 -166
  21. package/dist/domain/cpm.test.js.map +0 -1
  22. package/dist/domain/ddd.test.d.ts +0 -2
  23. package/dist/domain/ddd.test.d.ts.map +0 -1
  24. package/dist/domain/ddd.test.js +0 -349
  25. package/dist/domain/ddd.test.js.map +0 -1
  26. package/dist/domain/ladder.test.d.ts +0 -2
  27. package/dist/domain/ladder.test.d.ts.map +0 -1
  28. package/dist/domain/ladder.test.js +0 -213
  29. package/dist/domain/ladder.test.js.map +0 -1
  30. package/dist/domain/metrics.test.d.ts +0 -2
  31. package/dist/domain/metrics.test.d.ts.map +0 -1
  32. package/dist/domain/metrics.test.js +0 -252
  33. package/dist/domain/metrics.test.js.map +0 -1
  34. package/dist/domain/task-duration.test.d.ts +0 -2
  35. package/dist/domain/task-duration.test.d.ts.map +0 -1
  36. package/dist/domain/task-duration.test.js +0 -307
  37. package/dist/domain/task-duration.test.js.map +0 -1
  38. package/dist/domain/workload.test.d.ts +0 -2
  39. package/dist/domain/workload.test.d.ts.map +0 -1
  40. package/dist/domain/workload.test.js +0 -261
  41. package/dist/domain/workload.test.js.map +0 -1
  42. package/dist/engine/actor.test.d.ts +0 -2
  43. package/dist/engine/actor.test.d.ts.map +0 -1
  44. package/dist/engine/actor.test.js +0 -54
  45. package/dist/engine/actor.test.js.map +0 -1
  46. package/dist/engine/authorization.test.d.ts +0 -2
  47. package/dist/engine/authorization.test.d.ts.map +0 -1
  48. package/dist/engine/authorization.test.js +0 -154
  49. package/dist/engine/authorization.test.js.map +0 -1
  50. package/dist/engine/cascade.test.d.ts +0 -2
  51. package/dist/engine/cascade.test.d.ts.map +0 -1
  52. package/dist/engine/cascade.test.js +0 -99
  53. package/dist/engine/cascade.test.js.map +0 -1
  54. package/dist/engine/config.test.d.ts +0 -2
  55. package/dist/engine/config.test.d.ts.map +0 -1
  56. package/dist/engine/config.test.js +0 -46
  57. package/dist/engine/config.test.js.map +0 -1
  58. package/dist/engine/entity-events.test.d.ts +0 -2
  59. package/dist/engine/entity-events.test.d.ts.map +0 -1
  60. package/dist/engine/entity-events.test.js +0 -61
  61. package/dist/engine/entity-events.test.js.map +0 -1
  62. package/dist/index.test.d.ts +0 -2
  63. package/dist/index.test.d.ts.map +0 -1
  64. package/dist/index.test.js +0 -429
  65. package/dist/index.test.js.map +0 -1
  66. package/dist/plugins/plugins.test.d.ts +0 -2
  67. package/dist/plugins/plugins.test.d.ts.map +0 -1
  68. package/dist/plugins/plugins.test.js +0 -137
  69. package/dist/plugins/plugins.test.js.map +0 -1
  70. package/dist/schemas/schemas.test.d.ts +0 -2
  71. package/dist/schemas/schemas.test.d.ts.map +0 -1
  72. package/dist/schemas/schemas.test.js +0 -58
  73. package/dist/schemas/schemas.test.js.map +0 -1
  74. package/dist/status.test.d.ts +0 -2
  75. package/dist/status.test.d.ts.map +0 -1
  76. package/dist/status.test.js +0 -295
  77. package/dist/status.test.js.map +0 -1
  78. package/dist/storage/storage.test.d.ts +0 -2
  79. package/dist/storage/storage.test.d.ts.map +0 -1
  80. package/dist/storage/storage.test.js +0 -236
  81. package/dist/storage/storage.test.js.map +0 -1
  82. package/dist/store/conformance.test.d.ts +0 -2
  83. package/dist/store/conformance.test.d.ts.map +0 -1
  84. package/dist/store/conformance.test.js +0 -15
  85. package/dist/store/conformance.test.js.map +0 -1
  86. package/dist/store/firebase.test.d.ts +0 -2
  87. package/dist/store/firebase.test.d.ts.map +0 -1
  88. package/dist/store/firebase.test.js +0 -103
  89. package/dist/store/firebase.test.js.map +0 -1
  90. package/dist/store/query.test.d.ts +0 -2
  91. package/dist/store/query.test.d.ts.map +0 -1
  92. package/dist/store/query.test.js +0 -89
  93. package/dist/store/query.test.js.map +0 -1
  94. package/dist/store/sqlite.test.d.ts +0 -2
  95. package/dist/store/sqlite.test.d.ts.map +0 -1
  96. package/dist/store/sqlite.test.js +0 -177
  97. package/dist/store/sqlite.test.js.map +0 -1
  98. package/dist/task-state.test.d.ts +0 -2
  99. package/dist/task-state.test.d.ts.map +0 -1
  100. package/dist/task-state.test.js +0 -423
  101. package/dist/task-state.test.js.map +0 -1
  102. package/dist/use-case-validation.test.d.ts +0 -2
  103. package/dist/use-case-validation.test.d.ts.map +0 -1
  104. package/dist/use-case-validation.test.js +0 -281
  105. package/dist/use-case-validation.test.js.map +0 -1
  106. package/dist/utils/fractional-index.test.d.ts +0 -2
  107. package/dist/utils/fractional-index.test.d.ts.map +0 -1
  108. package/dist/utils/fractional-index.test.js +0 -182
  109. package/dist/utils/fractional-index.test.js.map +0 -1
  110. package/dist/utils/key.test.d.ts +0 -2
  111. package/dist/utils/key.test.d.ts.map +0 -1
  112. package/dist/utils/key.test.js +0 -65
  113. package/dist/utils/key.test.js.map +0 -1
  114. package/dist/utils/mentions.test.d.ts +0 -2
  115. package/dist/utils/mentions.test.d.ts.map +0 -1
  116. package/dist/utils/mentions.test.js +0 -114
  117. package/dist/utils/mentions.test.js.map +0 -1
  118. package/dist/webhooks/webhooks.test.d.ts +0 -2
  119. package/dist/webhooks/webhooks.test.d.ts.map +0 -1
  120. package/dist/webhooks/webhooks.test.js +0 -154
  121. package/dist/webhooks/webhooks.test.js.map +0 -1
  122. package/src/deliverables.test.ts +0 -284
  123. package/src/domain/authorization.ts +0 -82
  124. package/src/domain/calendar.test.ts +0 -216
  125. package/src/domain/calendar.ts +0 -502
  126. package/src/domain/cpm.test.ts +0 -189
  127. package/src/domain/cpm.ts +0 -258
  128. package/src/domain/custom-fields.ts +0 -172
  129. package/src/domain/ddd.test.ts +0 -442
  130. package/src/domain/entities.ts +0 -798
  131. package/src/domain/errors.ts +0 -38
  132. package/src/domain/events.ts +0 -369
  133. package/src/domain/graph.ts +0 -70
  134. package/src/domain/index.ts +0 -12
  135. package/src/domain/ladder.test.ts +0 -251
  136. package/src/domain/ladder.ts +0 -474
  137. package/src/domain/metrics.test.ts +0 -288
  138. package/src/domain/metrics.ts +0 -497
  139. package/src/domain/task-duration.test.ts +0 -354
  140. package/src/domain/workload.test.ts +0 -311
  141. package/src/domain/workload.ts +0 -457
  142. package/src/engine/actor.test.ts +0 -63
  143. package/src/engine/authorization.test.ts +0 -199
  144. package/src/engine/cascade.test.ts +0 -121
  145. package/src/engine/config.test.ts +0 -51
  146. package/src/engine/entity-events.test.ts +0 -67
  147. package/src/engine/index.ts +0 -2472
  148. package/src/index.test.ts +0 -514
  149. package/src/index.ts +0 -9
  150. package/src/plugins/index.ts +0 -103
  151. package/src/plugins/plugins.test.ts +0 -161
  152. package/src/schemas/index.ts +0 -402
  153. package/src/schemas/schemas.test.ts +0 -116
  154. package/src/status.test.ts +0 -354
  155. package/src/storage/file-storage.ts +0 -200
  156. package/src/storage/firebase-storage.ts +0 -209
  157. package/src/storage/index.ts +0 -3
  158. package/src/storage/s3.ts +0 -151
  159. package/src/storage/storage.test.ts +0 -274
  160. package/src/store/conformance.test.ts +0 -15
  161. package/src/store/firebase.test.ts +0 -131
  162. package/src/store/firebase.ts +0 -658
  163. package/src/store/index.ts +0 -623
  164. package/src/store/query.test.ts +0 -110
  165. package/src/store/query.ts +0 -113
  166. package/src/store/sqlite.test.ts +0 -218
  167. package/src/store/sqlite.ts +0 -1480
  168. package/src/task-state.test.ts +0 -506
  169. package/src/testing/conformance.ts +0 -319
  170. package/src/testing/index.ts +0 -1
  171. package/src/types/index.ts +0 -859
  172. package/src/use-case-validation.test.ts +0 -324
  173. package/src/utils/fractional-index.test.ts +0 -228
  174. package/src/utils/fractional-index.ts +0 -254
  175. package/src/utils/index.ts +0 -7
  176. package/src/utils/key.test.ts +0 -76
  177. package/src/utils/key.ts +0 -62
  178. package/src/utils/mentions.test.ts +0 -132
  179. package/src/utils/mentions.ts +0 -131
  180. package/src/utils/status.ts +0 -419
  181. package/src/utils/workflow.ts +0 -306
  182. package/src/webhooks/dispatcher.ts +0 -190
  183. package/src/webhooks/index.ts +0 -2
  184. package/src/webhooks/signature.ts +0 -57
  185. package/src/webhooks/webhooks.test.ts +0 -201
  186. package/tsconfig.json +0 -8
  187. package/tsconfig.tsbuildinfo +0 -1
@@ -1,474 +0,0 @@
1
- import type {
2
- Project,
3
- Task,
4
- TaskDependency,
5
- TaskContainer,
6
- Iteration,
7
- Deliverable,
8
- Attachment,
9
- TimeEntry,
10
- Activity,
11
- TimelineLadder,
12
- TimelineLadderOptions,
13
- MacroTimelineSummary,
14
- MacroPhaseRollup,
15
- MacroPhaseHealth,
16
- StandardTaskTimelineItem,
17
- ConcreteTaskEvidence,
18
- ConcreteEvidenceSummary,
19
- RealityDelta,
20
- TaskLadderView,
21
- WorkSchedule
22
- } from '../types/index.js';
23
- import { calculateCPM, getTaskDurationHours } from './cpm.js';
24
- import { DEFAULT_WORK_SCHEDULE, getWorkingDaysBetween } from './calendar.js';
25
-
26
- export interface LadderContext {
27
- project: Project;
28
- tasks: Task[];
29
- dependencies: TaskDependency[];
30
- containers?: TaskContainer[];
31
- iterations?: Iteration[];
32
- deliverables?: Deliverable[];
33
- attachments?: Attachment[];
34
- timeEntries?: TimeEntry[];
35
- activities?: Activity[];
36
- schedule?: WorkSchedule;
37
- }
38
-
39
- export function aggregateConcreteEvidenceForTask(
40
- task: Task,
41
- attachments: Attachment[] = [],
42
- deliverables: Deliverable[] = [],
43
- timeEntries: TimeEntry[] = [],
44
- activities: Activity[] = [],
45
- schedule?: WorkSchedule
46
- ): ConcreteTaskEvidence {
47
- const taskAttachments = attachments.filter((a) => a.taskId === task.id);
48
- const taskDeliverables = deliverables.filter(
49
- (d) => d.id === task.deliverableId || (task.customFields?.deliverableId === d.id)
50
- );
51
- const taskTimeEntries = timeEntries.filter((te) => te.taskId === task.id);
52
- const taskActivities = activities.filter((act) => act.taskId === task.id);
53
- const todos = task.todos || [];
54
-
55
- // Group time entries by date for daily effort distribution
56
- const effortByDate = new Map<string, number>();
57
- for (const entry of taskTimeEntries) {
58
- const dateStr = entry.loggedAt.includes('T') ? entry.loggedAt.split('T')[0] : entry.loggedAt.substring(0, 10);
59
- const existing = effortByDate.get(dateStr) || 0;
60
- effortByDate.set(dateStr, Math.round((existing + entry.hours) * 100) / 100);
61
- }
62
-
63
- const dailyEffortDistribution = Array.from(effortByDate.entries())
64
- .map(([date, hours]) => ({ date, hours }))
65
- .sort((a, b) => a.date.localeCompare(b.date));
66
-
67
- const loggedHoursFromEntries = taskTimeEntries.reduce((sum, e) => sum + e.hours, 0);
68
- const effectiveLoggedHours = Math.max(loggedHoursFromEntries, task.loggedHours || 0, task.actualHours || 0);
69
- const estimatedHours = task.estimatedHours || (task.estimatedDurationMinutes ? task.estimatedDurationMinutes / 60 : 0);
70
- const varianceHours = Math.round((effectiveLoggedHours - estimatedHours) * 100) / 100;
71
-
72
- const nowIso = new Date().toISOString();
73
- const isOverdue = Boolean(task.dueDate && task.dueDate < nowIso && task.semanticStatus !== 'completed' && task.semanticStatus !== 'canceled');
74
- const isOverEstimate = Boolean(estimatedHours > 0 && effectiveLoggedHours > estimatedHours);
75
-
76
- let scheduleVarianceDays: number | undefined;
77
- let scheduleVarianceWorkingDays: number | undefined;
78
- if (task.dueDate && task.actualEndDate) {
79
- const dueTime = new Date(task.dueDate).getTime();
80
- const actualTime = new Date(task.actualEndDate).getTime();
81
- scheduleVarianceDays = Math.round((actualTime - dueTime) / (1000 * 60 * 60 * 24));
82
- const effectiveSched = schedule || DEFAULT_WORK_SCHEDULE;
83
- if (actualTime > dueTime) {
84
- scheduleVarianceWorkingDays = Math.max(0, getWorkingDaysBetween(task.dueDate, task.actualEndDate, effectiveSched) - 1);
85
- } else if (actualTime < dueTime) {
86
- scheduleVarianceWorkingDays = -Math.max(0, getWorkingDaysBetween(task.actualEndDate, task.dueDate, effectiveSched) - 1);
87
- } else {
88
- scheduleVarianceWorkingDays = 0;
89
- }
90
- }
91
-
92
- let durationVarianceHours: number | undefined;
93
- if (task.actualStartDate && task.actualEndDate && task.plannedStartDate && task.dueDate) {
94
- const actualDuration = (new Date(task.actualEndDate).getTime() - new Date(task.actualStartDate).getTime()) / (1000 * 60 * 60);
95
- const plannedDuration = (new Date(task.dueDate).getTime() - new Date(task.plannedStartDate).getTime()) / (1000 * 60 * 60);
96
- if (!isNaN(actualDuration) && !isNaN(plannedDuration)) {
97
- durationVarianceHours = Math.round((actualDuration - plannedDuration) * 100) / 100;
98
- }
99
- }
100
-
101
- const accuracyRatio = estimatedHours > 0
102
- ? Math.round((effectiveLoggedHours / estimatedHours) * 100) / 100
103
- : undefined;
104
-
105
- const realityDelta: RealityDelta = {
106
- plannedStartDate: task.plannedStartDate,
107
- actualStartDate: task.actualStartDate,
108
- dueDate: task.dueDate,
109
- actualEndDate: task.actualEndDate,
110
- estimatedHours: Math.round(estimatedHours * 100) / 100,
111
- loggedHours: Math.round(effectiveLoggedHours * 100) / 100,
112
- varianceHours,
113
- effortVarianceHours: varianceHours,
114
- durationVarianceHours,
115
- scheduleVarianceDays,
116
- scheduleVarianceWorkingDays,
117
- accuracyRatio,
118
- isOverdue,
119
- isOverEstimate
120
- };
121
-
122
- const hasVisualAsset = taskAttachments.some(
123
- (a) => a.mimeType?.startsWith('image/') || a.mimeType?.startsWith('video/')
124
- ) || taskDeliverables.some((d) => (d.outputUrls && d.outputUrls.length > 0) || Boolean(d.format));
125
-
126
- const timestamps = [
127
- ...taskActivities.map((a) => a.createdAt),
128
- ...taskTimeEntries.map((te) => te.loggedAt),
129
- task.updatedAt
130
- ].filter(Boolean);
131
- timestamps.sort();
132
- const lastActivityAt = timestamps.length > 0 ? timestamps[timestamps.length - 1] : undefined;
133
-
134
- const completedTodoCount = todos.filter((t) => t.completed).length;
135
-
136
- const evidenceSummary: ConcreteEvidenceSummary = {
137
- attachmentCount: taskAttachments.length,
138
- deliverableCount: taskDeliverables.length,
139
- todoCount: todos.length,
140
- completedTodoCount,
141
- timeEntryCount: taskTimeEntries.length,
142
- totalLoggedHours: Math.round(effectiveLoggedHours * 100) / 100,
143
- hasVisualAsset,
144
- lastActivityAt
145
- };
146
-
147
- return {
148
- taskId: task.id,
149
- attachments: taskAttachments,
150
- deliverables: taskDeliverables,
151
- todos,
152
- timeEntries: taskTimeEntries,
153
- dailyEffortDistribution,
154
- activities: taskActivities,
155
- realityDelta,
156
- evidenceSummary
157
- };
158
- }
159
-
160
- export function computeMacroPhases(
161
- tasks: Task[],
162
- containers: TaskContainer[] = [],
163
- iterations: Iteration[] = [],
164
- criticalTaskIds: Set<string>
165
- ): MacroPhaseRollup[] {
166
- const containerMap = new Map<string, TaskContainer>(containers.map((c) => [c.id, c]));
167
- const iterationMap = new Map<string, Iteration>(iterations.map((it) => [it.id, it]));
168
-
169
- // Group tasks by container if any exist; fallback to iteration or unassigned
170
- const phaseBuckets = new Map<string, { type: 'container' | 'iteration' | 'phase'; name: string; description?: string; tasks: Task[] }>();
171
-
172
- if (containers.length > 0) {
173
- for (const c of containers) {
174
- phaseBuckets.set(c.id, {
175
- type: 'container',
176
- name: c.name,
177
- description: c.description,
178
- tasks: []
179
- });
180
- }
181
- } else if (iterations.length > 0) {
182
- for (const it of iterations) {
183
- phaseBuckets.set(it.id, {
184
- type: 'iteration',
185
- name: it.name,
186
- description: it.goal,
187
- tasks: []
188
- });
189
- }
190
- }
191
-
192
- // Populate tasks into buckets
193
- for (const t of tasks) {
194
- let bucketId: string | null = null;
195
- if (t.containerId && phaseBuckets.has(t.containerId)) {
196
- bucketId = t.containerId;
197
- } else if (t.iterationId && phaseBuckets.has(t.iterationId)) {
198
- bucketId = t.iterationId;
199
- }
200
-
201
- if (!bucketId) {
202
- const fallbackId = 'unassigned';
203
- if (!phaseBuckets.has(fallbackId)) {
204
- phaseBuckets.set(fallbackId, {
205
- type: 'phase',
206
- name: containers.length > 0 || iterations.length > 0 ? 'General Tasks' : 'All Tasks',
207
- tasks: []
208
- });
209
- }
210
- bucketId = fallbackId;
211
- }
212
-
213
- phaseBuckets.get(bucketId)!.tasks.push(t);
214
- }
215
-
216
- const nowIso = new Date().toISOString();
217
- const phases: MacroPhaseRollup[] = [];
218
-
219
- for (const [id, bucket] of phaseBuckets.entries()) {
220
- const bucketTasks = bucket.tasks;
221
- if (bucketTasks.length === 0 && id === 'unassigned') continue;
222
-
223
- const taskIds = bucketTasks.map((t) => t.id);
224
- const totalTasks = bucketTasks.length;
225
- const completedTasks = bucketTasks.filter((t) => t.semanticStatus === 'completed' || t.status === 'done').length;
226
- const inProgressTasks = bucketTasks.filter((t) => t.semanticStatus === 'in_progress' || t.status === 'in_progress').length;
227
- const blockedTasks = bucketTasks.filter((t) => t.tags?.includes('blocked') || (t as any).isBlocked).length;
228
-
229
- let totalEstimatedHours = 0;
230
- let totalLoggedHours = 0;
231
- let weightedProgressSum = 0;
232
- let totalProgressWeight = 0;
233
-
234
- const startDates: string[] = [];
235
- const endDates: string[] = [];
236
-
237
- // Inherit container/iteration dates if set
238
- if (bucket.type === 'iteration' && iterationMap.has(id)) {
239
- const it = iterationMap.get(id)!;
240
- if (it.startDate) startDates.push(it.startDate);
241
- if (it.endDate) endDates.push(it.endDate);
242
- }
243
-
244
- let criticalCount = 0;
245
-
246
- for (const t of bucketTasks) {
247
- const duration = getTaskDurationHours(t);
248
- const est = t.estimatedHours || duration;
249
- const logged = t.loggedHours || 0;
250
- totalEstimatedHours += est;
251
- totalLoggedHours += logged;
252
-
253
- const progress = typeof t.progress === 'number'
254
- ? t.progress
255
- : (t.semanticStatus === 'completed' || t.status === 'done' ? 100 : 0);
256
-
257
- weightedProgressSum += progress * Math.max(est, 1);
258
- totalProgressWeight += Math.max(est, 1);
259
-
260
- if (t.plannedStartDate) startDates.push(t.plannedStartDate);
261
- if (t.actualStartDate) startDates.push(t.actualStartDate);
262
- if (t.dueDate) endDates.push(t.dueDate);
263
- if (t.actualEndDate) endDates.push(t.actualEndDate);
264
-
265
- if (criticalTaskIds.has(t.id)) {
266
- criticalCount++;
267
- }
268
- }
269
-
270
- startDates.sort();
271
- endDates.sort();
272
-
273
- const startDate = startDates.length > 0 ? startDates[0] : undefined;
274
- const endDate = endDates.length > 0 ? endDates[endDates.length - 1] : undefined;
275
-
276
- const progressPercentage = totalProgressWeight > 0
277
- ? Math.round(weightedProgressSum / totalProgressWeight)
278
- : (totalTasks > 0 ? Math.round((completedTasks / totalTasks) * 100) : 0);
279
-
280
- // Derive health
281
- let health: MacroPhaseHealth = 'on_track';
282
- if (totalTasks > 0 && completedTasks === totalTasks) {
283
- health = 'completed';
284
- } else if (blockedTasks > 0) {
285
- health = 'blocked';
286
- } else if (endDate && endDate < nowIso) {
287
- health = 'overdue';
288
- } else if (criticalCount > 0 && progressPercentage < 50 && endDate && (new Date(endDate).getTime() - Date.now() < 3 * 24 * 3600 * 1000)) {
289
- health = 'at_risk';
290
- }
291
-
292
- phases.push({
293
- id,
294
- type: bucket.type,
295
- name: bucket.name,
296
- description: bucket.description,
297
- startDate,
298
- endDate,
299
- durationHours: Math.round(totalEstimatedHours * 100) / 100,
300
- totalTasks,
301
- completedTasks,
302
- inProgressTasks,
303
- blockedTasks,
304
- progressPercentage,
305
- totalEstimatedHours: Math.round(totalEstimatedHours * 100) / 100,
306
- totalLoggedHours: Math.round(totalLoggedHours * 100) / 100,
307
- isCritical: criticalCount > 0,
308
- criticalTaskCount: criticalCount,
309
- health,
310
- taskIds
311
- });
312
- }
313
-
314
- return phases;
315
- }
316
-
317
- export function buildTimelineLadder(
318
- context: LadderContext,
319
- options: TimelineLadderOptions = {}
320
- ): TimelineLadder {
321
- const { project, tasks, dependencies, containers = [], iterations = [], deliverables = [], attachments = [], timeEntries = [], activities = [] } = context;
322
-
323
- const targetLevel = options.level || 'all';
324
- const generatedAt = new Date().toISOString();
325
-
326
- const effectiveSchedule = context.schedule || project.schedule || DEFAULT_WORK_SCHEDULE;
327
- const projectStartDate = project.startDate || (tasks.length > 0 ? tasks.find((t) => t.plannedStartDate)?.plannedStartDate : undefined);
328
-
329
- // Run Critical Path Method (CPM)
330
- const cpm = calculateCPM(project.id, tasks, dependencies, {
331
- schedule: effectiveSchedule,
332
- projectStartDate
333
- });
334
- const criticalSet = new Set(cpm.criticalTaskIds);
335
- const scheduleMap = new Map(cpm.tasks.map((s) => [s.taskId, s]));
336
-
337
- // Adjacency for dependencies
338
- const blockingMap = new Map<string, string[]>();
339
- const dependentMap = new Map<string, string[]>();
340
- for (const t of tasks) {
341
- blockingMap.set(t.id, []);
342
- dependentMap.set(t.id, []);
343
- }
344
-
345
- for (const dep of dependencies) {
346
- if (dep.type === 'blocking') {
347
- // dep.taskId depends on dep.dependsOnTaskId
348
- // dep.dependsOnTaskId blocks dep.taskId
349
- blockingMap.get(dep.taskId)?.push(dep.dependsOnTaskId);
350
- dependentMap.get(dep.dependsOnTaskId)?.push(dep.taskId);
351
- } else if (dep.type === 'blocked_by') {
352
- blockingMap.get(dep.dependsOnTaskId)?.push(dep.taskId);
353
- dependentMap.get(dep.taskId)?.push(dep.dependsOnTaskId);
354
- }
355
- }
356
-
357
- // Subtask hierarchy (parentId -> children)
358
- const childMap = new Map<string, string[]>();
359
- for (const t of tasks) {
360
- if (t.parentId) {
361
- const list = childMap.get(t.parentId) || [];
362
- list.push(t.id);
363
- childMap.set(t.parentId, list);
364
- }
365
- }
366
-
367
- // 1. Concrete View (Grounding)
368
- const concreteRecords: Record<string, ConcreteTaskEvidence> = {};
369
- for (const t of tasks) {
370
- concreteRecords[t.id] = aggregateConcreteEvidenceForTask(
371
- t,
372
- attachments,
373
- deliverables,
374
- timeEntries,
375
- activities,
376
- effectiveSchedule
377
- );
378
- }
379
-
380
- // 2. Standard View (Tasks & CPM)
381
- const standardTasks: StandardTaskTimelineItem[] = tasks.map((t) => {
382
- const evidence = concreteRecords[t.id];
383
- return {
384
- ...t,
385
- cpm: scheduleMap.get(t.id),
386
- blockingTaskIds: blockingMap.get(t.id) || [],
387
- dependentTaskIds: dependentMap.get(t.id) || [],
388
- childTaskIds: childMap.get(t.id) || [],
389
- concreteEvidenceSummary: evidence.evidenceSummary
390
- };
391
- });
392
-
393
- // 3. Macro View (Phases & Project Envelope)
394
- const phases = computeMacroPhases(tasks, containers, iterations, criticalSet);
395
-
396
- const startDates = phases.map((p) => p.startDate).filter(Boolean) as string[];
397
- const endDates = phases.map((p) => p.endDate).filter(Boolean) as string[];
398
- startDates.sort();
399
- endDates.sort();
400
-
401
- const overallStartDate = startDates.length > 0 ? startDates[0] : undefined;
402
- const overallEndDate = endDates.length > 0 ? endDates[endDates.length - 1] : undefined;
403
-
404
- const totalTasks = tasks.length;
405
- const completedTasks = tasks.filter((t) => t.semanticStatus === 'completed' || t.status === 'done').length;
406
- const inProgressTasks = tasks.filter((t) => t.semanticStatus === 'in_progress' || t.status === 'in_progress').length;
407
- const blockedTasks = tasks.filter((t) => t.tags?.includes('blocked') || (t as any).isBlocked).length;
408
- const totalEstimatedHours = standardTasks.reduce((sum, t) => sum + (t.estimatedHours || 0), 0);
409
- const totalLoggedHours = standardTasks.reduce((sum, t) => sum + (t.loggedHours || 0), 0);
410
-
411
- const weightedProgress = standardTasks.reduce(
412
- (sum, t) => sum + ((t.progress || 0) * Math.max(t.estimatedHours || 1, 1)),
413
- 0
414
- );
415
- const totalWeight = standardTasks.reduce((sum, t) => sum + Math.max(t.estimatedHours || 1, 1), 0);
416
- const overallProgressPercentage = totalWeight > 0
417
- ? Math.round(weightedProgress / totalWeight)
418
- : (totalTasks > 0 ? Math.round((completedTasks / totalTasks) * 100) : 0);
419
-
420
- let overallHealth: MacroPhaseHealth = 'on_track';
421
- if (totalTasks > 0 && completedTasks === totalTasks) {
422
- overallHealth = 'completed';
423
- } else if (phases.some((p) => p.health === 'blocked')) {
424
- overallHealth = 'blocked';
425
- } else if (phases.some((p) => p.health === 'overdue')) {
426
- overallHealth = 'overdue';
427
- } else if (phases.some((p) => p.health === 'at_risk')) {
428
- overallHealth = 'at_risk';
429
- }
430
-
431
- const macro: MacroTimelineSummary = {
432
- projectId: project.id,
433
- projectName: project.name,
434
- overallStartDate,
435
- overallEndDate,
436
- projectedFinishDate: overallEndDate,
437
- totalDurationHours: Math.round(totalEstimatedHours * 100) / 100,
438
- criticalPathDurationHours: cpm.totalDurationHours,
439
- overallProgressPercentage,
440
- health: overallHealth,
441
- totalTasks,
442
- completedTasks,
443
- inProgressTasks,
444
- blockedTasks,
445
- totalEstimatedHours: Math.round(totalEstimatedHours * 100) / 100,
446
- totalLoggedHours: Math.round(totalLoggedHours * 100) / 100,
447
- phases
448
- };
449
-
450
- const ladder: TimelineLadder = {
451
- projectId: project.id,
452
- generatedAt,
453
- level: targetLevel
454
- };
455
-
456
- if (targetLevel === 'all' || targetLevel === 'macro') {
457
- ladder.macro = macro;
458
- }
459
-
460
- if (targetLevel === 'all' || targetLevel === 'standard') {
461
- ladder.standard = {
462
- tasks: standardTasks,
463
- criticalPathTaskIds: cpm.criticalTaskIds,
464
- dependencies,
465
- totalDurationHours: cpm.totalDurationHours
466
- };
467
- }
468
-
469
- if (targetLevel === 'all' || targetLevel === 'concrete') {
470
- ladder.concrete = concreteRecords;
471
- }
472
-
473
- return ladder;
474
- }