loop-task 2.1.3 → 2.1.5

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 (148) hide show
  1. package/dist/app/App.js +119 -0
  2. package/dist/{tui → app}/index.js +4 -5
  3. package/dist/app/providers/index.js +1 -0
  4. package/dist/cli/import-writer.js +1 -1
  5. package/dist/cli.js +9 -9
  6. package/dist/client/cli-format.js +7 -0
  7. package/dist/client/commands.js +2 -164
  8. package/dist/client/ipc.js +3 -3
  9. package/dist/client/project-commands.js +159 -0
  10. package/dist/core/{command-runner.js → command/command-runner.js} +6 -13
  11. package/dist/core/{foreground-loop.js → foreground/index.js} +4 -4
  12. package/dist/core/{log-rotator.js → logging/log-rotator.js} +1 -1
  13. package/dist/core/loop/chain-executor.js +67 -0
  14. package/dist/core/loop/delay-utils.js +65 -0
  15. package/dist/core/loop/loop-controller.js +191 -0
  16. package/dist/core/loop/loop-runner.js +133 -0
  17. package/dist/core/loop/run-executor.js +90 -0
  18. package/dist/core/loop/types.js +1 -0
  19. package/dist/daemon/daemon-log.js +1 -1
  20. package/dist/daemon/http/helpers.js +81 -0
  21. package/dist/daemon/http/openapi.js +92 -0
  22. package/dist/daemon/http/route-loops.js +218 -0
  23. package/dist/daemon/http/route-misc.js +36 -0
  24. package/dist/daemon/http/route-projects.js +55 -0
  25. package/dist/daemon/http/route-tasks.js +53 -0
  26. package/dist/daemon/http/routes.js +17 -0
  27. package/dist/daemon/http/server.js +64 -0
  28. package/dist/daemon/http/sse.js +36 -0
  29. package/dist/daemon/index.js +7 -9
  30. package/dist/{ipc/handlers → daemon/ipc}/logs-stream.js +1 -1
  31. package/dist/daemon/managers/loop-entry.js +24 -0
  32. package/dist/daemon/{manager.js → managers/loop-manager.js} +32 -152
  33. package/dist/daemon/managers/loop-options.js +16 -0
  34. package/dist/daemon/managers/loop-serialization.js +43 -0
  35. package/dist/daemon/{projects.js → managers/project-manager.js} +6 -4
  36. package/dist/daemon/{task-manager.js → managers/task-manager.js} +5 -3
  37. package/dist/daemon/server/handlers/index.js +86 -0
  38. package/dist/daemon/server/handlers/log-handlers.js +130 -0
  39. package/dist/daemon/server/handlers/loop-handlers.js +54 -0
  40. package/dist/daemon/server/handlers/project-handlers.js +37 -0
  41. package/dist/daemon/server/handlers/task-handlers.js +20 -0
  42. package/dist/daemon/server/index.js +113 -0
  43. package/dist/daemon/{spawner.js → spawner/index.js} +4 -4
  44. package/dist/daemon/{state.js → state/index.js} +4 -4
  45. package/dist/daemon/{file-watcher.js → watcher/index.js} +8 -6
  46. package/dist/duration.js +1 -1
  47. package/dist/{tui/state.js → entities/loops/filters.js} +0 -76
  48. package/dist/entities/loops/index.js +1 -0
  49. package/dist/entities/projects/filters.js +69 -0
  50. package/dist/entities/projects/index.js +1 -0
  51. package/dist/entities/tasks/filters.js +41 -0
  52. package/dist/entities/tasks/index.js +1 -0
  53. package/dist/{tui/components → features/chain-editor}/ChainEditor.js +7 -5
  54. package/dist/features/code-editor/CodeEditorModal.js +84 -0
  55. package/dist/{tui/components → features/code-editor}/CodeEditorPreview.js +3 -3
  56. package/dist/features/code-editor/useEditorKeyboard.js +147 -0
  57. package/dist/features/code-editor/useModalDimensions.js +9 -0
  58. package/dist/{tui → features/commands}/commands.js +2 -2
  59. package/dist/features/commands/useCommandHandlers.js +147 -0
  60. package/dist/features/commands/useContextualActions.js +75 -0
  61. package/dist/features/commands/useGlobalShortcuts.js +152 -0
  62. package/dist/features/forms/FormRouter.js +44 -0
  63. package/dist/{tui/components → features/overlays}/ContextHelpModal.js +3 -3
  64. package/dist/{tui/components → features/overlays}/ExportModal.js +3 -3
  65. package/dist/{tui/components → features/overlays}/HelpGuideModal.js +3 -3
  66. package/dist/{tui/components → features/overlays}/HelpModal.js +3 -3
  67. package/dist/features/overlays/OverlayStack.js +9 -0
  68. package/dist/{tui/components → features/overlays}/ProjectsModal.js +2 -2
  69. package/dist/{tui/components → features/overlays}/TaskPickerModal.js +2 -2
  70. package/dist/{tui/components → features/overlays}/WelcomeScreen.js +5 -3
  71. package/dist/features/overlays/useOverlayStack.js +51 -0
  72. package/dist/features/state/useAppState.js +150 -0
  73. package/dist/logger.js +0 -1
  74. package/dist/loop-config.js +9 -4
  75. package/dist/{config → shared/config}/constants.js +1 -1
  76. package/dist/shared/container/index.js +22 -0
  77. package/dist/{tui → shared}/hooks/useBreakpoint.js +1 -1
  78. package/dist/shared/hooks/useInject.js +6 -0
  79. package/dist/{tui → shared}/hooks/useLogStream.js +7 -5
  80. package/dist/{hooks → shared/hooks}/useLoopFormValidation.js +2 -2
  81. package/dist/{tui → shared}/hooks/useLoopPolling.js +6 -4
  82. package/dist/shared/{useUndoRedo.js → hooks/useUndoRedo.js} +2 -4
  83. package/dist/shared/providers/InversifyProvider.js +6 -0
  84. package/dist/shared/services/export-service.js +43 -0
  85. package/dist/shared/services/log-service.js +28 -0
  86. package/dist/shared/services/loop-service.js +78 -0
  87. package/dist/shared/services/project-service.js +39 -0
  88. package/dist/shared/services/task-service.js +41 -0
  89. package/dist/shared/services/types.js +7 -0
  90. package/dist/{tui/components → shared/ui}/Button.js +1 -1
  91. package/dist/{tui/components → shared/ui}/DebugPanel.js +1 -1
  92. package/dist/{tui/components → shared/ui}/FocusableButton.js +1 -1
  93. package/dist/{tui/components → shared/ui}/FocusableInput.js +1 -1
  94. package/dist/{tui/components → shared/ui}/FocusableList.js +1 -1
  95. package/dist/{tui/components → shared/ui}/Modal.js +1 -1
  96. package/dist/{tui/components → shared/ui}/SelectModal.js +2 -2
  97. package/dist/{tui/components → shared/ui}/Toast.js +1 -1
  98. package/dist/shared/ui/state.js +9 -0
  99. package/dist/{tui → shared}/utils/paste.js +2 -2
  100. package/dist/{tui → shared}/utils/validation.js +1 -1
  101. package/dist/widgets/command-input/CommandDropdown.js +69 -0
  102. package/dist/widgets/command-input/CommandInput.js +129 -0
  103. package/dist/widgets/command-input/HintBar.js +9 -0
  104. package/dist/widgets/command-input/InputModes.js +68 -0
  105. package/dist/{tui/components → widgets/commands-browser}/CommandsBrowserModal.js +3 -3
  106. package/dist/{tui/components → widgets/header}/Header.js +3 -3
  107. package/dist/{tui/components → widgets/header}/TabBar.js +1 -1
  108. package/dist/{tui/components → widgets/left-panel}/LeftPanel.js +3 -3
  109. package/dist/{tui/components → widgets/left-panel}/Navigator.js +3 -3
  110. package/dist/{tui/components → widgets/left-panel}/ProjectsPage.js +9 -34
  111. package/dist/widgets/left-panel/ProjectsPageParts.js +34 -0
  112. package/dist/{tui/components → widgets/left-panel}/TaskBrowser.js +4 -4
  113. package/dist/{tui/components → widgets/log-modal}/LogModal.js +8 -6
  114. package/dist/widgets/loop-form/CreateForm.js +75 -0
  115. package/dist/widgets/loop-form/TextField.js +9 -0
  116. package/dist/{tui/components → widgets/loop-form}/WizardForm.js +5 -10
  117. package/dist/widgets/loop-form/useCreateSteps.js +107 -0
  118. package/dist/widgets/loop-form/useHandleComplete.js +74 -0
  119. package/dist/{tui/components → widgets/project-form}/ProjectForm.js +9 -7
  120. package/dist/{tui/components → widgets/right-panel}/Inspector.js +4 -4
  121. package/dist/{tui/components → widgets/right-panel}/RightPanel.js +4 -4
  122. package/dist/{tui/components → widgets/right-panel}/RunHistory.js +52 -4
  123. package/dist/{tui/components → widgets/task-form}/TaskForm.js +11 -9
  124. package/package.json +2 -1
  125. package/dist/core/loop-controller.js +0 -511
  126. package/dist/daemon/http-server.js +0 -599
  127. package/dist/daemon/server.js +0 -374
  128. package/dist/tui/App.js +0 -680
  129. package/dist/tui/components/CodeEditorModal.js +0 -244
  130. package/dist/tui/components/CommandInput.js +0 -273
  131. package/dist/tui/components/CreateForm.js +0 -224
  132. package/dist/tui/daemon.js +0 -142
  133. package/dist/tui/format.js +0 -111
  134. /package/dist/{tui/router.js → app/router/index.js} +0 -0
  135. /package/dist/{tui → app}/types.js +0 -0
  136. /package/dist/core/{resolve-cwd.js → command/resolve-cwd.js} +0 -0
  137. /package/dist/core/{context-parser.js → context/context-parser.js} +0 -0
  138. /package/dist/core/{template.js → context/template.js} +0 -0
  139. /package/dist/core/{log-parser.js → logging/log-parser.js} +0 -0
  140. /package/dist/core/{scheduling.js → scheduling/index.js} +0 -0
  141. /package/dist/{ipc → daemon/ipc}/send.js +0 -0
  142. /package/dist/{config → shared/config}/paths.js +0 -0
  143. /package/dist/{i18n → shared/i18n}/en.json +0 -0
  144. /package/dist/{i18n → shared/i18n}/index.js +0 -0
  145. /package/dist/{board → shared/ui}/format.js +0 -0
  146. /package/dist/{tui → shared/ui}/hooks/useHoverState.js +0 -0
  147. /package/dist/{tui → shared/ui}/theme.js +0 -0
  148. /package/dist/{tui → shared}/utils/syntax.js +0 -0
@@ -1,511 +0,0 @@
1
- import { EventEmitter } from "node:events";
2
- import fs from "node:fs";
3
- import crypto from "node:crypto";
4
- import { sleep } from "../shared/sleep.js";
5
- import { SLEEP_CHUNK_MS } from "../config/constants.js";
6
- import { executeCommand } from "./command-runner.js";
7
- import { rotateLogIfNeeded } from "./log-rotator.js";
8
- import { computePhase, alignToPhase } from "./scheduling.js";
9
- import { t } from "../i18n/index.js";
10
- import { parseStdout } from "./context-parser.js";
11
- import { interpolate } from "./template.js";
12
- import { resolveEffectiveCwd } from "./resolve-cwd.js";
13
- export class LoopController extends EventEmitter {
14
- abortController;
15
- runAbortController = null;
16
- _paused = false;
17
- _forceRun = false;
18
- _savedRemainingMs = null;
19
- _resetSchedule = false;
20
- _stopAfterRun = false;
21
- _maxRunsReached = false;
22
- _status = "running";
23
- resumeResolve = null;
24
- runCount = 0;
25
- lastRunAt = null;
26
- lastExitCode = null;
27
- lastDuration = null;
28
- nextRunAt = null;
29
- createdAt;
30
- id;
31
- options;
32
- logPath;
33
- taskResolver;
34
- projectDirectory;
35
- remainingDelayMs = null;
36
- logStream = null;
37
- loopPromise = null;
38
- sessionStartedAt = null;
39
- runHistory = [];
40
- currentRunStartOffset = 0;
41
- skippedCount = 0;
42
- constructor(id, options, logPath, taskResolver, state, projectDirectory) {
43
- super();
44
- this.id = id;
45
- this.options = options;
46
- this.logPath = logPath;
47
- this.taskResolver = taskResolver;
48
- this.projectDirectory = projectDirectory;
49
- this.abortController = new AbortController();
50
- this.createdAt = state?.createdAt ?? new Date().toISOString();
51
- this.runCount = state?.runCount ?? 0;
52
- this._maxRunsReached = state?.maxRunsReached ?? false;
53
- this.sessionStartedAt = state?.sessionStartedAt ?? null;
54
- this.lastRunAt = state?.lastRunAt ?? null;
55
- this.lastExitCode = state?.lastExitCode ?? null;
56
- this.lastDuration = state?.lastDuration ?? null;
57
- this.nextRunAt = state?.nextRunAt ?? null;
58
- this.remainingDelayMs = state?.remainingDelayMs ?? null;
59
- this._status = state?.status ?? "running";
60
- this._paused = state?.status === "paused" || state?.status === "idle";
61
- this.runHistory = (state?.runHistory ?? []).map((r) => r.status === "running" ? { ...r, status: "completed" } : r).map((r) => ({ ...r, logOffset: r.logOffset ?? 0 }));
62
- this.skippedCount = state?.skippedCount ?? 0;
63
- }
64
- get status() {
65
- return this._status;
66
- }
67
- start() {
68
- this.skippedCount = 0;
69
- this.logStream?.end();
70
- this.abortController = new AbortController();
71
- this.logStream = fs.createWriteStream(this.logPath, { flags: "a" });
72
- this.loopPromise = this.run();
73
- if (this.sessionStartedAt === null) {
74
- this.sessionStartedAt = new Date().toISOString();
75
- }
76
- }
77
- pause(interruptCurrentRun = false) {
78
- if (this._status === "running" || this._status === "waiting") {
79
- this._paused = true;
80
- this._status = "paused";
81
- if (interruptCurrentRun && this._status === "paused") {
82
- this.runAbortController?.abort();
83
- }
84
- this.emit("paused");
85
- }
86
- }
87
- stopLoop(interruptCurrentRun = false) {
88
- if (this._status === "running" || this._status === "waiting" || this._status === "paused") {
89
- this._paused = true;
90
- this._status = "idle";
91
- this.sessionStartedAt = null;
92
- this.remainingDelayMs = null;
93
- this.nextRunAt = null;
94
- if (interruptCurrentRun) {
95
- this.runAbortController?.abort();
96
- }
97
- this.abortController.abort();
98
- if (this.resumeResolve) {
99
- this.resumeResolve();
100
- this.resumeResolve = null;
101
- }
102
- this.emit("stopped");
103
- }
104
- }
105
- resume() {
106
- if (this._status === "paused" && this._paused && this.resumeResolve) {
107
- this._paused = false;
108
- if (this.remainingDelayMs !== null) {
109
- this._status = "waiting";
110
- this.nextRunAt = new Date(Date.now() + this.remainingDelayMs).toISOString();
111
- }
112
- this.resumeResolve();
113
- this.resumeResolve = null;
114
- this.emit("resumed");
115
- }
116
- }
117
- playLoop() {
118
- if (this._status !== "idle" && this._status !== "stopped")
119
- return false;
120
- if (this._maxRunsReached)
121
- return false;
122
- if (this.options.maxRuns !== null && this.runCount >= this.options.maxRuns) {
123
- this._maxRunsReached = true;
124
- return false;
125
- }
126
- this.sessionStartedAt = new Date().toISOString();
127
- this._resetSchedule = true;
128
- this._paused = false;
129
- this._status = "waiting";
130
- const phase = this.options.offset !== null
131
- ? this.options.offset
132
- : computePhase(this.id, this.options.interval);
133
- const delay = alignToPhase(Date.now(), this.options.interval, phase);
134
- this.nextRunAt = new Date(Date.now() + delay).toISOString();
135
- if (this.resumeResolve) {
136
- this.resumeResolve();
137
- this.resumeResolve = null;
138
- }
139
- this.start();
140
- this.emit("resumed");
141
- return true;
142
- }
143
- triggerNow() {
144
- if (this._status === "running")
145
- return false;
146
- if (this._maxRunsReached)
147
- return false;
148
- const needsStart = this._status === "stopped" || this._status === "idle";
149
- this._savedRemainingMs = this.remainingDelayMs;
150
- this._forceRun = true;
151
- if (needsStart) {
152
- this._stopAfterRun = true;
153
- this._paused = false;
154
- this._status = "running";
155
- this.start();
156
- }
157
- else {
158
- if (this._paused)
159
- this.resume();
160
- }
161
- this.emit("triggered");
162
- return true;
163
- }
164
- async stop() {
165
- this.abortController.abort();
166
- if (this._paused)
167
- this.resume();
168
- if (this.loopPromise) {
169
- await this.loopPromise;
170
- }
171
- this.logStream?.end();
172
- this.logStream = null;
173
- }
174
- getMeta() {
175
- return {
176
- id: this.id,
177
- taskId: this.options.taskId,
178
- status: this._status,
179
- createdAt: this.createdAt,
180
- maxRunsReached: this._maxRunsReached,
181
- sessionStartedAt: this.sessionStartedAt,
182
- runCount: this.runCount,
183
- lastRunAt: this.lastRunAt,
184
- lastExitCode: this.lastExitCode,
185
- lastDuration: this.lastDuration,
186
- nextRunAt: this.nextRunAt,
187
- remainingDelayMs: this.remainingDelayMs,
188
- runHistory: this.runHistory,
189
- skippedCount: this.skippedCount,
190
- };
191
- }
192
- clearMaxRunsReached() {
193
- this._maxRunsReached = false;
194
- this._paused = false;
195
- this._status = "idle";
196
- this.remainingDelayMs = null;
197
- this.nextRunAt = null;
198
- }
199
- isMaxRunsReached() {
200
- return this._maxRunsReached;
201
- }
202
- async waitForResume() {
203
- const savedStatus = this._status;
204
- this._status = savedStatus === "idle" ? "idle" : "paused";
205
- return new Promise((resolve) => {
206
- this.resumeResolve = resolve;
207
- });
208
- }
209
- async waitForDelay(ms, signal) {
210
- let remaining = ms;
211
- this.remainingDelayMs = remaining;
212
- let announced = false;
213
- while (remaining > 0) {
214
- if (this._forceRun) {
215
- this._savedRemainingMs = remaining;
216
- this.remainingDelayMs = null;
217
- this.nextRunAt = null;
218
- return true;
219
- }
220
- if (this._resetSchedule) {
221
- this._resetSchedule = false;
222
- remaining = ms;
223
- this.remainingDelayMs = remaining;
224
- announced = false;
225
- }
226
- if (this._paused) {
227
- if (this._status !== "idle") {
228
- this._status = "paused";
229
- }
230
- this.emit("paused");
231
- await this.waitForResume();
232
- announced = false;
233
- if (signal.aborted) {
234
- this.remainingDelayMs = null;
235
- return false;
236
- }
237
- if (this._resetSchedule) {
238
- this._resetSchedule = false;
239
- remaining = ms;
240
- this.remainingDelayMs = remaining;
241
- }
242
- }
243
- if (!announced) {
244
- this._status = "waiting";
245
- this.nextRunAt = new Date(Date.now() + remaining).toISOString();
246
- this.emit("waiting");
247
- announced = true;
248
- }
249
- const chunk = Math.min(remaining, SLEEP_CHUNK_MS);
250
- const startedAt = Date.now();
251
- try {
252
- await sleep(chunk, signal);
253
- }
254
- catch {
255
- this.remainingDelayMs = null;
256
- return false;
257
- }
258
- remaining = Math.max(0, remaining - (Date.now() - startedAt));
259
- this.remainingDelayMs = remaining;
260
- }
261
- this.remainingDelayMs = null;
262
- this.nextRunAt = null;
263
- return true;
264
- }
265
- async run() {
266
- const signal = this.abortController.signal;
267
- let isFirstRun = true;
268
- try {
269
- while (!signal.aborted) {
270
- if (this.options.maxRuns !== null && this.runCount >= this.options.maxRuns) {
271
- this._maxRunsReached = true;
272
- this._paused = true;
273
- this._status = "paused";
274
- this.emit("paused");
275
- return;
276
- }
277
- if (isFirstRun && this._paused) {
278
- await this.waitForResume();
279
- if (signal.aborted)
280
- break;
281
- }
282
- if (isFirstRun && this.nextRunAt) {
283
- const delay = Math.max(0, new Date(this.nextRunAt).getTime() - Date.now());
284
- if (delay > 0) {
285
- const completed = await this.waitForDelay(delay, signal);
286
- if (!completed) {
287
- break;
288
- }
289
- }
290
- }
291
- else if (isFirstRun && !this.options.immediate) {
292
- const phase = this.options.offset !== null
293
- ? this.options.offset
294
- : computePhase(this.id, this.options.interval);
295
- const delay = alignToPhase(Date.now(), this.options.interval, phase);
296
- if (delay > 0) {
297
- this.nextRunAt = new Date(Date.now() + delay).toISOString();
298
- }
299
- const completed = await this.waitForDelay(delay, signal);
300
- if (!completed) {
301
- break;
302
- }
303
- }
304
- isFirstRun = false;
305
- if (this._paused) {
306
- await this.waitForResume();
307
- if (signal.aborted)
308
- break;
309
- }
310
- this._status = "running";
311
- this._forceRun = false;
312
- this.runCount++;
313
- this.lastRunAt = new Date().toISOString();
314
- const runStartedAtMs = Date.now();
315
- if (this.sessionStartedAt === null) {
316
- this.sessionStartedAt = this.lastRunAt;
317
- }
318
- this.nextRunAt = null;
319
- this.emit("run:start", this.runCount);
320
- this.logStream = rotateLogIfNeeded(this.logPath, this.logStream);
321
- this.currentRunStartOffset = fs.existsSync(this.logPath) ? fs.statSync(this.logPath).size : 0;
322
- this.runHistory.push({
323
- runNumber: this.runCount,
324
- startedAt: this.lastRunAt,
325
- exitCode: -1,
326
- duration: 0,
327
- logSize: 0,
328
- status: "running",
329
- logOffset: this.currentRunStartOffset,
330
- });
331
- this.runAbortController = new AbortController();
332
- const task = this.options.taskId ? this.taskResolver(this.options.taskId) : null;
333
- const cwd = resolveEffectiveCwd(this.options.cwd, this.projectDirectory);
334
- const chainContext = {};
335
- const hasChainTasks = !!(task?.onSuccessTaskId || task?.onFailureTaskId);
336
- const singleCommandFallback = {
337
- command: task?.command ?? this.options.command,
338
- commandArgs: task?.commandArgs ?? this.options.commandArgs,
339
- commandRaw: task?.commandRaw ?? this.options.commandRaw,
340
- };
341
- const taskSteps = task?.steps?.length
342
- ? task.steps
343
- : [{ commands: [singleCommandFallback] }];
344
- const shouldCaptureStdout = hasChainTasks || taskSteps.length > 1 || taskSteps[0].commands.length > 1;
345
- let exitCode = 0;
346
- let totalDuration = 0;
347
- let combinedStdout = "";
348
- for (const step of taskSteps) {
349
- const stepResults = await Promise.allSettled(step.commands.map((cmd) => executeCommand(interpolate(cmd.command, chainContext), cmd.commandArgs.map(a => interpolate(a, chainContext)), cwd, this.logStream, AbortSignal.any([signal, this.runAbortController.signal]), this.runCount, shouldCaptureStdout)));
350
- let stepStdout = "";
351
- for (const r of stepResults) {
352
- if (r.status === "fulfilled") {
353
- totalDuration += r.value.duration;
354
- if (r.value.stdout)
355
- stepStdout += (stepStdout ? "\n" : "") + r.value.stdout;
356
- }
357
- }
358
- combinedStdout += (combinedStdout && stepStdout ? "\n" : "") + stepStdout;
359
- if (shouldCaptureStdout && stepStdout) {
360
- const parsed = parseStdout(stepStdout);
361
- if (parsed !== null) {
362
- Object.assign(chainContext, parsed);
363
- }
364
- }
365
- const stepFailure = stepResults.some((r) => r.status === "rejected" || (r.status === "fulfilled" && r.value.exitCode !== 0));
366
- if (stepFailure) {
367
- const failed = stepResults.find((r) => r.status === "fulfilled" && r.value.exitCode !== 0);
368
- exitCode = failed ? failed.value.exitCode : 1;
369
- break;
370
- }
371
- }
372
- this.runAbortController = null;
373
- const result = {
374
- exitCode,
375
- duration: totalDuration,
376
- stdout: combinedStdout || undefined,
377
- };
378
- this.lastExitCode = result.exitCode;
379
- this.lastDuration = result.duration;
380
- const logSize = fs.existsSync(this.logPath) ? fs.statSync(this.logPath).size - this.currentRunStartOffset : 0;
381
- const runningRecord = this.runHistory.find((r) => r.runNumber === this.runCount);
382
- if (runningRecord) {
383
- runningRecord.exitCode = result.exitCode;
384
- runningRecord.duration = result.duration;
385
- runningRecord.logSize = Math.max(0, logSize);
386
- runningRecord.status = "completed";
387
- }
388
- else {
389
- this.runHistory.push({
390
- runNumber: this.runCount,
391
- startedAt: this.lastRunAt,
392
- exitCode: result.exitCode,
393
- duration: result.duration,
394
- logSize: Math.max(0, logSize),
395
- status: "completed",
396
- logOffset: this.currentRunStartOffset,
397
- });
398
- }
399
- const chainTargetId = result.exitCode === 0 ? task?.onSuccessTaskId : task?.onFailureTaskId;
400
- if (chainTargetId) {
401
- const chainGroupId = crypto.randomUUID().slice(0, 8);
402
- const mainRecord = this.runHistory[this.runHistory.length - 1];
403
- if (mainRecord)
404
- mainRecord.chainGroupId = chainGroupId;
405
- let currentTargetId = chainTargetId;
406
- let prevBranch = result.exitCode === 0 ? "onSuccess" : "onFailure";
407
- let prevExit = result.exitCode;
408
- while (currentTargetId) {
409
- const chainTask = this.taskResolver(currentTargetId);
410
- if (!chainTask)
411
- break;
412
- if (this.logStream) {
413
- this.logStream.write(t("loop.chainHeader", { name: chainTask.name, branch: prevBranch, prevExit }));
414
- }
415
- const chainStartedAt = new Date().toISOString();
416
- const chainOffset = fs.existsSync(this.logPath) ? fs.statSync(this.logPath).size : 0;
417
- this.runHistory.push({
418
- runNumber: this.runCount,
419
- startedAt: chainStartedAt,
420
- exitCode: -1,
421
- duration: 0,
422
- logSize: 0,
423
- status: "running",
424
- logOffset: chainOffset,
425
- chainGroupId,
426
- chainName: chainTask.name,
427
- });
428
- const interpolatedCommand = interpolate(chainTask.command, chainContext);
429
- const interpolatedArgs = chainTask.commandArgs.map(a => interpolate(a, chainContext));
430
- const chainResult = await executeCommand(interpolatedCommand, interpolatedArgs, cwd, this.logStream, signal, this.runCount, true, true);
431
- if (chainResult.stdout) {
432
- const parsed = parseStdout(chainResult.stdout);
433
- if (parsed !== null) {
434
- Object.assign(chainContext, parsed);
435
- }
436
- }
437
- const chainLogSize = fs.existsSync(this.logPath) ? fs.statSync(this.logPath).size - chainOffset : 0;
438
- const chainRecord = this.runHistory.find((r) => r.chainGroupId === chainGroupId && r.status === "running" && r.chainName === chainTask.name);
439
- if (chainRecord) {
440
- chainRecord.exitCode = chainResult.exitCode;
441
- chainRecord.duration = chainResult.duration;
442
- chainRecord.logSize = Math.max(0, chainLogSize);
443
- chainRecord.status = "completed";
444
- }
445
- this.lastExitCode = chainResult.exitCode;
446
- this.lastDuration = (this.lastDuration ?? 0) + chainResult.duration;
447
- currentTargetId = (chainResult.exitCode === 0 ? chainTask.onSuccessTaskId : chainTask.onFailureTaskId) ?? null;
448
- prevBranch = chainResult.exitCode === 0 ? "onSuccess" : "onFailure";
449
- prevExit = chainResult.exitCode;
450
- }
451
- }
452
- if (this.runHistory.length > 50) {
453
- this.runHistory = this.runHistory.slice(-50);
454
- }
455
- this.emit("run:end", result);
456
- if (signal.aborted)
457
- break;
458
- if (this.options.maxRuns !== null && this.runCount >= this.options.maxRuns) {
459
- this._maxRunsReached = true;
460
- this._paused = true;
461
- this._status = "paused";
462
- this.remainingDelayMs = null;
463
- this.nextRunAt = null;
464
- this.emit("paused");
465
- return;
466
- }
467
- if (this._stopAfterRun) {
468
- this._stopAfterRun = false;
469
- this._paused = true;
470
- this._status = "idle";
471
- this.remainingDelayMs = null;
472
- this.nextRunAt = null;
473
- this.emit("stopped");
474
- return;
475
- }
476
- const saved = this._savedRemainingMs;
477
- this._savedRemainingMs = null;
478
- if (saved !== null) {
479
- const remaining = Math.max(0, saved - result.duration);
480
- if (remaining > 0) {
481
- const completed = await this.waitForDelay(remaining, signal);
482
- if (!completed)
483
- break;
484
- }
485
- }
486
- else {
487
- const nextSlotMs = runStartedAtMs + this.options.interval;
488
- const overrunMs = Date.now() - nextSlotMs;
489
- if (overrunMs >= 0) {
490
- const missed = Math.floor(overrunMs / this.options.interval) + 1;
491
- this.skippedCount += missed;
492
- const adjustedDelay = this.options.interval - (overrunMs % this.options.interval);
493
- const completed = await this.waitForDelay(adjustedDelay, signal);
494
- if (!completed)
495
- break;
496
- }
497
- else {
498
- const completed = await this.waitForDelay(this.options.interval, signal);
499
- if (!completed)
500
- break;
501
- }
502
- }
503
- }
504
- }
505
- finally {
506
- if (this._status !== "stopped" && this._status !== "idle" && this._status !== "paused" && !this._maxRunsReached) {
507
- this._status = "stopped";
508
- }
509
- }
510
- }
511
- }