agent-steward 0.0.4 → 0.0.6

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 (279) hide show
  1. package/CHANGELOG.md +309 -2
  2. package/CROSS_PLATFORM.md +19 -0
  3. package/README.md +55 -8
  4. package/ROADMAP-STRATEGY.md +22 -19
  5. package/ROADMAP.md +230 -159
  6. package/dist/cli.d.ts +3 -0
  7. package/dist/cli.d.ts.map +1 -0
  8. package/dist/cli.js +356 -0
  9. package/dist/core/analytics.d.ts +6 -0
  10. package/dist/core/analytics.d.ts.map +1 -1
  11. package/dist/core/analytics.js +20 -3
  12. package/dist/core/audit.d.ts +6 -0
  13. package/dist/core/audit.d.ts.map +1 -1
  14. package/dist/core/audit.js +24 -0
  15. package/dist/core/budget.d.ts +118 -0
  16. package/dist/core/budget.d.ts.map +1 -0
  17. package/dist/core/budget.js +206 -0
  18. package/dist/core/cron-to-schtasks.d.ts.map +1 -1
  19. package/dist/core/cron-to-schtasks.js +38 -0
  20. package/dist/core/dry-run.d.ts +26 -1
  21. package/dist/core/dry-run.d.ts.map +1 -1
  22. package/dist/core/dry-run.js +199 -8
  23. package/dist/core/execution.d.ts +203 -0
  24. package/dist/core/execution.d.ts.map +1 -0
  25. package/dist/core/execution.js +309 -0
  26. package/dist/core/feedback.d.ts +192 -0
  27. package/dist/core/feedback.d.ts.map +1 -0
  28. package/dist/core/feedback.js +426 -0
  29. package/dist/core/gate-integrity.d.ts +71 -0
  30. package/dist/core/gate-integrity.d.ts.map +1 -0
  31. package/dist/core/gate-integrity.js +275 -0
  32. package/dist/core/git-trigger.d.ts.map +1 -1
  33. package/dist/core/git-trigger.js +14 -2
  34. package/dist/core/guard-run.d.ts +4 -0
  35. package/dist/core/guard-run.d.ts.map +1 -0
  36. package/dist/core/guard-run.js +101 -0
  37. package/dist/core/guard.d.ts +52 -0
  38. package/dist/core/guard.d.ts.map +1 -0
  39. package/dist/core/guard.js +362 -0
  40. package/dist/core/local-ai.d.ts.map +1 -1
  41. package/dist/core/local-ai.js +7 -3
  42. package/dist/core/lock.d.ts +41 -0
  43. package/dist/core/lock.d.ts.map +1 -1
  44. package/dist/core/lock.js +141 -29
  45. package/dist/core/matrix.d.ts +8 -0
  46. package/dist/core/matrix.d.ts.map +1 -1
  47. package/dist/core/matrix.js +132 -17
  48. package/dist/core/mission.d.ts +103 -0
  49. package/dist/core/mission.d.ts.map +1 -0
  50. package/dist/core/mission.js +374 -0
  51. package/dist/core/opencode-models.d.ts +2 -2
  52. package/dist/core/opencode-models.d.ts.map +1 -1
  53. package/dist/core/opencode-models.js +28 -21
  54. package/dist/core/opencode-usage.d.ts +35 -0
  55. package/dist/core/opencode-usage.d.ts.map +1 -0
  56. package/dist/core/opencode-usage.js +132 -0
  57. package/dist/core/pipeline-apply.d.ts +38 -0
  58. package/dist/core/pipeline-apply.d.ts.map +1 -0
  59. package/dist/core/pipeline-apply.js +117 -0
  60. package/dist/core/pipeline-io.d.ts +13 -0
  61. package/dist/core/pipeline-io.d.ts.map +1 -0
  62. package/dist/core/pipeline-io.js +78 -0
  63. package/dist/core/pipeline-map.d.ts +52 -0
  64. package/dist/core/pipeline-map.d.ts.map +1 -0
  65. package/dist/core/pipeline-map.js +208 -0
  66. package/dist/core/pipeline-spec.d.ts +109 -0
  67. package/dist/core/pipeline-spec.d.ts.map +1 -0
  68. package/dist/core/pipeline-spec.js +554 -0
  69. package/dist/core/ps-wrapper.d.ts +27 -2
  70. package/dist/core/ps-wrapper.d.ts.map +1 -1
  71. package/dist/core/ps-wrapper.js +424 -65
  72. package/dist/core/record-history.d.ts.map +1 -1
  73. package/dist/core/record-history.js +206 -33
  74. package/dist/core/router/rules.d.ts.map +1 -1
  75. package/dist/core/router/rules.js +6 -0
  76. package/dist/core/run-mode.d.ts +12 -0
  77. package/dist/core/run-mode.d.ts.map +1 -0
  78. package/dist/core/run-mode.js +17 -0
  79. package/dist/core/scheduler/linux.d.ts +8 -1
  80. package/dist/core/scheduler/linux.d.ts.map +1 -1
  81. package/dist/core/scheduler/linux.js +34 -9
  82. package/dist/core/scheduler/logs.d.ts +4 -0
  83. package/dist/core/scheduler/logs.d.ts.map +1 -1
  84. package/dist/core/scheduler/logs.js +25 -1
  85. package/dist/core/scheduler/mac.d.ts +44 -2
  86. package/dist/core/scheduler/mac.d.ts.map +1 -1
  87. package/dist/core/scheduler/mac.js +126 -15
  88. package/dist/core/scheduler/windows.d.ts.map +1 -1
  89. package/dist/core/scheduler/windows.js +15 -12
  90. package/dist/core/secrets.d.ts.map +1 -1
  91. package/dist/core/secrets.js +18 -8
  92. package/dist/core/self-analysis.d.ts +32 -2
  93. package/dist/core/self-analysis.d.ts.map +1 -1
  94. package/dist/core/self-analysis.js +38 -27
  95. package/dist/core/shutdown.d.ts +33 -2
  96. package/dist/core/shutdown.d.ts.map +1 -1
  97. package/dist/core/shutdown.js +58 -2
  98. package/dist/core/structured-log.d.ts +37 -1
  99. package/dist/core/structured-log.d.ts.map +1 -1
  100. package/dist/core/structured-log.js +37 -0
  101. package/dist/core/usage-surface.d.ts +25 -0
  102. package/dist/core/usage-surface.d.ts.map +1 -0
  103. package/dist/core/usage-surface.js +41 -0
  104. package/dist/core/worktree-run.d.ts +4 -0
  105. package/dist/core/worktree-run.d.ts.map +1 -0
  106. package/dist/core/worktree-run.js +164 -0
  107. package/dist/core/worktree.d.ts +115 -0
  108. package/dist/core/worktree.d.ts.map +1 -0
  109. package/dist/core/worktree.js +462 -0
  110. package/dist/core/wrappers/bash.d.ts +19 -0
  111. package/dist/core/wrappers/bash.d.ts.map +1 -1
  112. package/dist/core/wrappers/bash.js +230 -27
  113. package/dist/db/capability-repo.d.ts +43 -2
  114. package/dist/db/capability-repo.d.ts.map +1 -1
  115. package/dist/db/capability-repo.js +96 -11
  116. package/dist/db/history-repo.d.ts +70 -5
  117. package/dist/db/history-repo.d.ts.map +1 -1
  118. package/dist/db/history-repo.js +170 -29
  119. package/dist/db/metrics-repo.d.ts +20 -2
  120. package/dist/db/metrics-repo.d.ts.map +1 -1
  121. package/dist/db/metrics-repo.js +35 -13
  122. package/dist/db/migrate.d.ts.map +1 -1
  123. package/dist/db/migrate.js +37 -17
  124. package/dist/db/recovery.d.ts +3 -1
  125. package/dist/db/recovery.d.ts.map +1 -1
  126. package/dist/db/recovery.js +11 -5
  127. package/dist/db/run-journal.d.ts +33 -5
  128. package/dist/db/run-journal.d.ts.map +1 -1
  129. package/dist/db/run-journal.js +78 -14
  130. package/dist/db/run-resume.d.ts +5 -0
  131. package/dist/db/run-resume.d.ts.map +1 -1
  132. package/dist/db/run-resume.js +9 -2
  133. package/dist/db/runs-repo.d.ts +19 -2
  134. package/dist/db/runs-repo.d.ts.map +1 -1
  135. package/dist/db/runs-repo.js +91 -60
  136. package/dist/db/schema.d.ts +2 -2
  137. package/dist/db/schema.d.ts.map +1 -1
  138. package/dist/db/schema.js +132 -2
  139. package/dist/db/templates-repo.d.ts +2 -2
  140. package/dist/db/templates-repo.d.ts.map +1 -1
  141. package/dist/db/templates-repo.js +5 -3
  142. package/dist/history.d.ts +7 -2
  143. package/dist/history.d.ts.map +1 -1
  144. package/dist/history.js +4 -2
  145. package/dist/hook-output.d.ts +2 -0
  146. package/dist/hook-output.d.ts.map +1 -0
  147. package/dist/hook-output.js +31 -0
  148. package/dist/i18n.d.ts +5 -0
  149. package/dist/i18n.d.ts.map +1 -1
  150. package/dist/i18n.js +11 -4
  151. package/dist/index.d.ts +375 -47
  152. package/dist/index.d.ts.map +1 -1
  153. package/dist/index.js +1344 -192
  154. package/dist/notifications.d.ts +53 -2
  155. package/dist/notifications.d.ts.map +1 -1
  156. package/dist/notifications.js +168 -21
  157. package/dist/profiles.d.ts.map +1 -1
  158. package/dist/profiles.js +82 -10
  159. package/dist/rpc.d.ts +354 -0
  160. package/dist/rpc.d.ts.map +1 -1
  161. package/dist/rpc.js +145 -0
  162. package/dist/server.d.ts +32 -2
  163. package/dist/server.d.ts.map +1 -1
  164. package/dist/server.js +154 -30
  165. package/dist/settings.d.ts +44 -2
  166. package/dist/settings.d.ts.map +1 -1
  167. package/dist/settings.js +85 -3
  168. package/dist/shared/jobs-view.d.ts +1 -1
  169. package/dist/shared/jobs-view.d.ts.map +1 -1
  170. package/dist/shared/jobs-view.js +2 -0
  171. package/dist/task-transfer.d.ts +21 -0
  172. package/dist/task-transfer.d.ts.map +1 -1
  173. package/dist/task-transfer.js +64 -3
  174. package/dist/templates.d.ts +2 -2
  175. package/dist/templates.d.ts.map +1 -1
  176. package/dist/templates.js +3 -3
  177. package/dist/types.d.ts +22 -2
  178. package/dist/types.d.ts.map +1 -1
  179. package/dist/workspaces.d.ts.map +1 -1
  180. package/dist/workspaces.js +34 -11
  181. package/docs/ARCHITECTURE.md +319 -66
  182. package/docs/COMPATIBILITY.md +2 -2
  183. package/docs/CONCEPT.md +18 -4
  184. package/docs/CROSS_PLATFORM.ru.md +20 -0
  185. package/docs/CROSS_PLATFORM.zh.md +18 -0
  186. package/docs/EXECUTION-LOOP.md +28 -10
  187. package/docs/FEATURES.md +129 -7
  188. package/docs/FEATURES.zh.md +97 -6
  189. package/docs/MIGRATION.md +2 -2
  190. package/docs/PIPELINE.md +293 -0
  191. package/docs/QA-A2-GUI-TUI.md +205 -0
  192. package/docs/README.ru.md +43 -4
  193. package/docs/README.zh.md +44 -4
  194. package/docs/SAFETY.md +23 -3
  195. package/gui-experimental.cjs +9 -8
  196. package/locales/en.json +58 -3
  197. package/locales/ru.json +58 -3
  198. package/locales/zh.json +58 -3
  199. package/opencode.pipeline.yaml +35 -0
  200. package/package.json +36 -4
  201. package/scripts/check-eslint-baseline.mjs +139 -0
  202. package/scripts/gen-test-plan-counters.mjs +117 -0
  203. package/scripts/github-ci-fetch.ps1 +37 -0
  204. package/scripts/pl-commit.ps1 +112 -0
  205. package/scripts/qa-a2-commit.ps1 +104 -0
  206. package/scripts/test-home.mjs +107 -0
  207. package/skills/scheduled-job-best-practices/SKILL.md +95 -1
  208. package/skills/scheduled-job-best-practices/references/recipes.md +139 -0
  209. package/src/cli.ts +417 -0
  210. package/src/core/analytics.ts +27 -4
  211. package/src/core/audit.ts +25 -0
  212. package/src/core/budget.ts +340 -0
  213. package/src/core/cron-to-schtasks.ts +42 -0
  214. package/src/core/dry-run.ts +235 -12
  215. package/src/core/execution.ts +538 -0
  216. package/src/core/feedback.ts +582 -0
  217. package/src/core/file-watcher.ts +2 -2
  218. package/src/core/gate-integrity.ts +330 -0
  219. package/src/core/git-trigger.ts +18 -5
  220. package/src/core/guard-run.ts +119 -0
  221. package/src/core/guard.ts +431 -0
  222. package/src/core/local-ai.ts +7 -3
  223. package/src/core/lock.ts +178 -37
  224. package/src/core/matrix.ts +158 -52
  225. package/src/core/mission.ts +531 -0
  226. package/src/core/native-notify.ts +1 -1
  227. package/src/core/opencode-models.ts +30 -21
  228. package/src/core/opencode-usage.ts +182 -0
  229. package/src/core/pipeline-apply.ts +158 -0
  230. package/src/core/pipeline-io.ts +96 -0
  231. package/src/core/pipeline-map.ts +283 -0
  232. package/src/core/pipeline-spec.ts +775 -0
  233. package/src/core/ps-wrapper.ts +435 -66
  234. package/src/core/record-history.ts +258 -81
  235. package/src/core/router/rules.ts +6 -0
  236. package/src/core/run-mode.ts +19 -0
  237. package/src/core/schedule-gate.ts +1 -1
  238. package/src/core/scheduler/at-fallback.ts +1 -1
  239. package/src/core/scheduler/linux.ts +40 -11
  240. package/src/core/scheduler/logs.ts +34 -3
  241. package/src/core/scheduler/mac.ts +174 -14
  242. package/src/core/scheduler/registry.ts +2 -2
  243. package/src/core/scheduler/windows.ts +19 -15
  244. package/src/core/secrets.ts +26 -19
  245. package/src/core/self-analysis.ts +75 -29
  246. package/src/core/shutdown.ts +90 -4
  247. package/src/core/structured-log.ts +89 -5
  248. package/src/core/telegram.ts +2 -2
  249. package/src/core/triage-worker.ts +7 -7
  250. package/src/core/triage.ts +2 -2
  251. package/src/core/usage-surface.ts +70 -0
  252. package/src/core/worktree-run.ts +199 -0
  253. package/src/core/worktree.ts +600 -0
  254. package/src/core/wrappers/bash.ts +248 -26
  255. package/src/db/capability-repo.ts +127 -10
  256. package/src/db/history-repo.ts +281 -40
  257. package/src/db/metrics-repo.ts +40 -13
  258. package/src/db/migrate.ts +57 -32
  259. package/src/db/recovery.ts +12 -5
  260. package/src/db/run-journal.ts +100 -13
  261. package/src/db/run-resume.ts +17 -1
  262. package/src/db/runs-repo.ts +147 -65
  263. package/src/db/schema.ts +134 -2
  264. package/src/db/templates-repo.ts +5 -3
  265. package/src/gui.tsx +438 -18
  266. package/src/history.ts +11 -4
  267. package/src/hook-output.ts +39 -0
  268. package/src/i18n.ts +12 -4
  269. package/src/index.ts +1902 -320
  270. package/src/notifications.ts +227 -47
  271. package/src/profiles.ts +88 -10
  272. package/src/rpc.ts +158 -0
  273. package/src/server.ts +201 -41
  274. package/src/settings.ts +125 -5
  275. package/src/shared/jobs-view.ts +3 -1
  276. package/src/task-transfer.ts +90 -6
  277. package/src/templates.ts +3 -3
  278. package/src/types.ts +23 -2
  279. package/src/workspaces.ts +37 -14
@@ -0,0 +1,283 @@
1
+ /**
2
+ * Спринт 32 (Pipeline-as-code, 0.0.5): маппинг `PipelineSpec` → `TaskDefinition[]`.
3
+ *
4
+ * Модуль чистый: не читает БД и не трогает планировщик. Всё, что требует
5
+ * состояния (разрешение имён workspace в id), передаётся через опции.
6
+ * Маркеры `pipelineName` / `pipelineStepId` сохраняются в definition_json,
7
+ * чтобы идемпотентный apply мог отличать шаги pipeline от «чужих» задач.
8
+ */
9
+ import type { MatrixConfig, TaskDefinition } from "../task-transfer.js";
10
+ import {
11
+ PIPELINE_VERSION,
12
+ pipelineStepTaskName,
13
+ type PipelineMatrix,
14
+ type PipelineSpec,
15
+ type PipelineStep,
16
+ type PipelineTrigger,
17
+ type PipelineWindowPolicy,
18
+ } from "./pipeline-spec.js";
19
+
20
+ export interface PipelineMapOptions {
21
+ /**
22
+ * Разрешение имени workspace в id. Если не задано, значения
23
+ * `matrix.workspaces` считаются уже id. Apply передаёт резолвер из БД.
24
+ */
25
+ resolveWorkspaceId?: (nameOrId: string) => string | undefined;
26
+ }
27
+
28
+ export interface PipelineTaskMapping {
29
+ tasks: TaskDefinition[];
30
+ warnings: string[];
31
+ }
32
+
33
+ /**
34
+ * Преобразует провалидированный pipeline в определения задач.
35
+ *
36
+ * Правила:
37
+ * - `trigger: dag` → задача без собственного триггера (`triggerType: "file"`),
38
+ * как матричные родители: её запускает только DAG-движок по `dependsOn`;
39
+ * - `needs` → `dependsOn` с именами задач (`<pipeline> <step>`);
40
+ * - `defaults` наследуются шагом;
41
+ * - матрица `workspaces` переносится в `matrix.workspaceIds`.
42
+ */
43
+ export function pipelineSpecToTasks(
44
+ spec: PipelineSpec,
45
+ options: PipelineMapOptions = {},
46
+ ): PipelineTaskMapping {
47
+ const defaults = spec.defaults ?? {};
48
+ const warnings: string[] = [];
49
+
50
+ const nameByStep = new Map<string, string>();
51
+ for (const step of spec.steps) {
52
+ const explicit = step.name?.trim();
53
+ nameByStep.set(
54
+ step.id.toLowerCase(),
55
+ explicit || pipelineStepTaskName(spec.name, step.id),
56
+ );
57
+ }
58
+
59
+ const tasks = spec.steps.map((step) => {
60
+ const trigger = step.trigger ?? defaults.trigger ?? "cron";
61
+ const scheduleValue = step.schedule ?? defaults.schedule ?? "";
62
+ const schedule = trigger === "cron" ? scheduleValue : "";
63
+
64
+ const dependsOn = step.needs?.length
65
+ ? step.needs.map((need) => nameByStep.get(need.toLowerCase())!)
66
+ : undefined;
67
+
68
+ const task: TaskDefinition = {
69
+ name: nameByStep.get(step.id.toLowerCase())!,
70
+ schedule,
71
+ prompt: step.prompt,
72
+ // TaskDefinition не знает "dag": ручной триггер "file" не регистрирует
73
+ // cron, задачу запускает DAG-движок после успеха зависимостей.
74
+ triggerType: trigger === "dag" ? "file" : trigger,
75
+ ...(dependsOn ? { dependsOn } : {}),
76
+ ...(step.qualityGate !== undefined ? { qualityGate: step.qualityGate } : {}),
77
+ ...(step.beforeScript !== undefined ? { beforeScript: step.beforeScript } : {}),
78
+ ...(step.afterScript !== undefined ? { afterScript: step.afterScript } : {}),
79
+ ...(step.guards !== undefined ? { guards: step.guards } : {}),
80
+ ...(step.model !== undefined || defaults.model !== undefined
81
+ ? { model: step.model ?? defaults.model }
82
+ : {}),
83
+ ...(step.priority !== undefined || defaults.priority !== undefined
84
+ ? { priority: step.priority ?? defaults.priority }
85
+ : {}),
86
+ ...(step.timeoutSeconds !== undefined || defaults.timeoutSeconds !== undefined
87
+ ? { timeoutSeconds: step.timeoutSeconds ?? defaults.timeoutSeconds }
88
+ : {}),
89
+ ...(step.workingDirectory !== undefined || defaults.workingDirectory !== undefined
90
+ ? { workingDirectory: step.workingDirectory ?? defaults.workingDirectory }
91
+ : {}),
92
+ ...(step.workspaceId !== undefined ? { workspaceId: step.workspaceId } : {}),
93
+ ...(step.retryCount !== undefined || defaults.retryCount !== undefined
94
+ ? { retryCount: step.retryCount ?? defaults.retryCount }
95
+ : {}),
96
+ ...(step.retryDelay !== undefined || defaults.retryDelay !== undefined
97
+ ? { retryDelay: step.retryDelay ?? defaults.retryDelay }
98
+ : {}),
99
+ ...(step.allowedTools !== undefined || defaults.allowedTools !== undefined
100
+ ? { allowedTools: step.allowedTools ?? defaults.allowedTools }
101
+ : {}),
102
+ ...(step.deniedTools !== undefined || defaults.deniedTools !== undefined
103
+ ? { deniedTools: step.deniedTools ?? defaults.deniedTools }
104
+ : {}),
105
+ ...(step.timezone !== undefined || defaults.timezone !== undefined
106
+ ? { timezone: step.timezone ?? defaults.timezone }
107
+ : {}),
108
+ ...(step.notBefore !== undefined ? { notBefore: step.notBefore } : {}),
109
+ ...(step.notAfter !== undefined ? { notAfter: step.notAfter } : {}),
110
+ ...(step.activeDays !== undefined ? { activeDays: step.activeDays } : {}),
111
+ ...(step.windowPolicy !== undefined ? { windowPolicy: step.windowPolicy } : {}),
112
+ ...(trigger === "once" && step.runAt !== undefined ? { runAt: step.runAt } : {}),
113
+ ...(step.matrix ? { matrix: mapMatrix(step.matrix, options, warnings) } : {}),
114
+ pipelineName: spec.name,
115
+ pipelineStepId: step.id,
116
+ };
117
+ return task;
118
+ });
119
+
120
+ return { tasks, warnings };
121
+ }
122
+
123
+ function mapMatrix(
124
+ matrix: PipelineMatrix,
125
+ options: PipelineMapOptions,
126
+ warnings: string[],
127
+ ): MatrixConfig {
128
+ const workspaceIds = matrix.workspaces.map((raw) => {
129
+ const value = raw.trim();
130
+ const resolved = options.resolveWorkspaceId?.(value);
131
+ if (options.resolveWorkspaceId && !resolved) {
132
+ warnings.push(`workspace «${value}» не найден — шаг будет отклонён при apply.`);
133
+ return value;
134
+ }
135
+ return resolved ?? value;
136
+ });
137
+ return {
138
+ workspaceIds,
139
+ ...(matrix.strategy ? { strategy: matrix.strategy } : {}),
140
+ ...(matrix.failureMode ? { failureMode: matrix.failureMode } : {}),
141
+ ...(matrix.aggregation ? { aggregation: matrix.aggregation } : {}),
142
+ ...(matrix.concurrency !== undefined ? { concurrency: matrix.concurrency } : {}),
143
+ ...(matrix.createAggregator !== undefined
144
+ ? { createAggregator: matrix.createAggregator }
145
+ : {}),
146
+ };
147
+ }
148
+
149
+ export interface PipelineExportResult {
150
+ spec: PipelineSpec;
151
+ warnings: string[];
152
+ }
153
+
154
+ /**
155
+ * QA-P3.2: проверка, что конвейер действительно существует в наборе задач.
156
+ * `tasksToPipelineSpec` всегда возвращает spec (в т.ч. с нулём шагов), поэтому
157
+ * «не найден» должен определяться здесь, а не по `!result.spec`.
158
+ */
159
+ export function pipelineExportLookupError(
160
+ tasks: TaskDefinition[],
161
+ pipelineName: string,
162
+ ): string | null {
163
+ const hasSteps = tasks.some(
164
+ (task) =>
165
+ task.pipelineName?.toLowerCase() === pipelineName.toLowerCase() &&
166
+ Boolean(task.pipelineStepId),
167
+ );
168
+ return hasSteps
169
+ ? null
170
+ : `Конвейер "${pipelineName}" не найден (не найдены задачи с pipelineName=${pipelineName} и pipelineStepId).`;
171
+ }
172
+
173
+ /**
174
+ * Обратный маппинг: задачи одного pipeline из БД → `PipelineSpec` для export.
175
+ *
176
+ * Восстанавливает `needs` по `dependsOn`, `matrix.workspaces` по
177
+ * `matrix.workspaceIds`, trigger по `triggerType`. Шаг `dag` неотличим от
178
+ * ручного `file`-шага с зависимостями, поэтому восстанавливается эвристикой:
179
+ * `file`, без cron и watchPath, но с `dependsOn` → `dag`.
180
+ */
181
+ export function tasksToPipelineSpec(
182
+ tasks: TaskDefinition[],
183
+ pipelineName: string,
184
+ ): PipelineExportResult {
185
+ const warnings: string[] = [];
186
+ const own = tasks.filter(
187
+ (task) =>
188
+ task.pipelineName?.toLowerCase() === pipelineName.toLowerCase() &&
189
+ Boolean(task.pipelineStepId),
190
+ );
191
+
192
+ const stepIdByTaskName = new Map<string, string>();
193
+ for (const task of own) {
194
+ const key = task.name.trim().toLowerCase();
195
+ if (key && !stepIdByTaskName.has(key)) {
196
+ stepIdByTaskName.set(key, task.pipelineStepId!);
197
+ }
198
+ }
199
+
200
+ const steps: PipelineStep[] = own.map((task) => {
201
+ const stepId = task.pipelineStepId!;
202
+ const trigger = inferTrigger(task);
203
+ const needs = task.dependsOn
204
+ ?.map((dep) => {
205
+ const id = stepIdByTaskName.get(dep.trim().toLowerCase());
206
+ if (!id) {
207
+ warnings.push(
208
+ `Шаг «${stepId}»: зависимость «${dep}» не входит в pipeline и опущена.`,
209
+ );
210
+ }
211
+ return id;
212
+ })
213
+ .filter((value): value is string => Boolean(value));
214
+
215
+ const defaultName = pipelineStepTaskName(pipelineName, stepId);
216
+ const step: PipelineStep = {
217
+ id: stepId,
218
+ ...(task.name.trim() !== defaultName ? { name: task.name } : {}),
219
+ prompt: task.prompt,
220
+ ...(needs?.length ? { needs } : {}),
221
+ ...(trigger === "cron" && task.schedule ? { schedule: task.schedule } : {}),
222
+ ...(trigger !== "cron" ? { trigger } : {}),
223
+ ...(trigger === "once" && task.runAt !== undefined ? { runAt: task.runAt } : {}),
224
+ ...(task.model !== undefined ? { model: task.model } : {}),
225
+ ...(task.priority !== undefined ? { priority: task.priority } : {}),
226
+ ...(task.timeoutSeconds !== undefined ? { timeoutSeconds: task.timeoutSeconds } : {}),
227
+ ...(task.workingDirectory !== undefined
228
+ ? { workingDirectory: task.workingDirectory }
229
+ : {}),
230
+ ...(task.workspaceId !== undefined ? { workspaceId: task.workspaceId } : {}),
231
+ ...(task.retryCount !== undefined ? { retryCount: task.retryCount } : {}),
232
+ ...(task.retryDelay !== undefined ? { retryDelay: task.retryDelay } : {}),
233
+ ...(task.qualityGate !== undefined ? { qualityGate: task.qualityGate } : {}),
234
+ ...(task.beforeScript !== undefined ? { beforeScript: task.beforeScript } : {}),
235
+ ...(task.afterScript !== undefined ? { afterScript: task.afterScript } : {}),
236
+ ...(task.guards !== undefined ? { guards: task.guards } : {}),
237
+ ...(task.allowedTools !== undefined ? { allowedTools: task.allowedTools } : {}),
238
+ ...(task.deniedTools !== undefined ? { deniedTools: task.deniedTools } : {}),
239
+ ...(task.matrix
240
+ ? {
241
+ matrix: {
242
+ workspaces: task.matrix.workspaceIds,
243
+ ...(task.matrix.strategy ? { strategy: task.matrix.strategy } : {}),
244
+ ...(task.matrix.failureMode ? { failureMode: task.matrix.failureMode } : {}),
245
+ ...(task.matrix.aggregation ? { aggregation: task.matrix.aggregation } : {}),
246
+ ...(task.matrix.concurrency !== undefined
247
+ ? { concurrency: task.matrix.concurrency }
248
+ : {}),
249
+ ...(task.matrix.createAggregator !== undefined
250
+ ? { createAggregator: task.matrix.createAggregator }
251
+ : {}),
252
+ },
253
+ }
254
+ : {}),
255
+ ...(task.timezone !== undefined ? { timezone: task.timezone } : {}),
256
+ ...(task.notBefore !== undefined ? { notBefore: task.notBefore } : {}),
257
+ ...(task.notAfter !== undefined ? { notAfter: task.notAfter } : {}),
258
+ ...(task.activeDays !== undefined ? { activeDays: task.activeDays } : {}),
259
+ ...(task.windowPolicy !== undefined
260
+ ? { windowPolicy: task.windowPolicy }
261
+ : {}),
262
+ };
263
+ return step;
264
+ });
265
+
266
+ return {
267
+ spec: { version: PIPELINE_VERSION, name: pipelineName, steps },
268
+ warnings,
269
+ };
270
+ }
271
+
272
+ function inferTrigger(task: TaskDefinition): PipelineTrigger {
273
+ const trigger = task.triggerType ?? "cron";
274
+ if (
275
+ trigger === "file" &&
276
+ !task.schedule &&
277
+ !task.watchPath &&
278
+ Boolean(task.dependsOn?.length)
279
+ ) {
280
+ return "dag";
281
+ }
282
+ return trigger;
283
+ }