loop-task 2.1.4 → 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.
- package/dist/app/App.js +119 -0
- package/dist/{tui → app}/index.js +4 -5
- package/dist/app/providers/index.js +1 -0
- package/dist/cli/import-writer.js +1 -1
- package/dist/cli.js +9 -9
- package/dist/client/cli-format.js +7 -0
- package/dist/client/commands.js +2 -164
- package/dist/client/ipc.js +3 -3
- package/dist/client/project-commands.js +159 -0
- package/dist/core/{command-runner.js → command/command-runner.js} +3 -3
- package/dist/core/{foreground-loop.js → foreground/index.js} +4 -4
- package/dist/core/{log-rotator.js → logging/log-rotator.js} +1 -1
- package/dist/core/loop/chain-executor.js +67 -0
- package/dist/core/loop/delay-utils.js +65 -0
- package/dist/core/loop/loop-controller.js +191 -0
- package/dist/core/loop/loop-runner.js +133 -0
- package/dist/core/loop/run-executor.js +90 -0
- package/dist/core/loop/types.js +1 -0
- package/dist/daemon/daemon-log.js +1 -1
- package/dist/daemon/http/helpers.js +81 -0
- package/dist/daemon/http/openapi.js +92 -0
- package/dist/daemon/http/route-loops.js +218 -0
- package/dist/daemon/http/route-misc.js +36 -0
- package/dist/daemon/http/route-projects.js +55 -0
- package/dist/daemon/http/route-tasks.js +53 -0
- package/dist/daemon/http/routes.js +17 -0
- package/dist/daemon/http/server.js +64 -0
- package/dist/daemon/http/sse.js +36 -0
- package/dist/daemon/index.js +7 -9
- package/dist/{ipc/handlers → daemon/ipc}/logs-stream.js +1 -1
- package/dist/daemon/managers/loop-entry.js +24 -0
- package/dist/daemon/{manager.js → managers/loop-manager.js} +32 -152
- package/dist/daemon/managers/loop-options.js +16 -0
- package/dist/daemon/managers/loop-serialization.js +43 -0
- package/dist/daemon/{projects.js → managers/project-manager.js} +6 -4
- package/dist/daemon/{task-manager.js → managers/task-manager.js} +5 -3
- package/dist/daemon/server/handlers/index.js +86 -0
- package/dist/daemon/server/handlers/log-handlers.js +130 -0
- package/dist/daemon/server/handlers/loop-handlers.js +54 -0
- package/dist/daemon/server/handlers/project-handlers.js +37 -0
- package/dist/daemon/server/handlers/task-handlers.js +20 -0
- package/dist/daemon/server/index.js +113 -0
- package/dist/daemon/{spawner.js → spawner/index.js} +4 -4
- package/dist/daemon/{state.js → state/index.js} +4 -4
- package/dist/daemon/{file-watcher.js → watcher/index.js} +8 -6
- package/dist/duration.js +1 -1
- package/dist/{tui/state.js → entities/loops/filters.js} +0 -76
- package/dist/entities/loops/index.js +1 -0
- package/dist/entities/projects/filters.js +69 -0
- package/dist/entities/projects/index.js +1 -0
- package/dist/entities/tasks/filters.js +41 -0
- package/dist/entities/tasks/index.js +1 -0
- package/dist/{tui/components → features/chain-editor}/ChainEditor.js +7 -5
- package/dist/features/code-editor/CodeEditorModal.js +84 -0
- package/dist/{tui/components → features/code-editor}/CodeEditorPreview.js +3 -3
- package/dist/features/code-editor/useEditorKeyboard.js +147 -0
- package/dist/features/code-editor/useModalDimensions.js +9 -0
- package/dist/{tui → features/commands}/commands.js +2 -2
- package/dist/features/commands/useCommandHandlers.js +147 -0
- package/dist/features/commands/useContextualActions.js +75 -0
- package/dist/features/commands/useGlobalShortcuts.js +152 -0
- package/dist/features/forms/FormRouter.js +44 -0
- package/dist/{tui/components → features/overlays}/ContextHelpModal.js +3 -3
- package/dist/{tui/components → features/overlays}/ExportModal.js +3 -3
- package/dist/{tui/components → features/overlays}/HelpGuideModal.js +3 -3
- package/dist/{tui/components → features/overlays}/HelpModal.js +3 -3
- package/dist/features/overlays/OverlayStack.js +9 -0
- package/dist/{tui/components → features/overlays}/ProjectsModal.js +2 -2
- package/dist/{tui/components → features/overlays}/TaskPickerModal.js +2 -2
- package/dist/{tui/components → features/overlays}/WelcomeScreen.js +5 -3
- package/dist/features/overlays/useOverlayStack.js +51 -0
- package/dist/features/state/useAppState.js +150 -0
- package/dist/logger.js +0 -1
- package/dist/loop-config.js +9 -4
- package/dist/{config → shared/config}/constants.js +1 -1
- package/dist/shared/container/index.js +22 -0
- package/dist/{tui → shared}/hooks/useBreakpoint.js +1 -1
- package/dist/shared/hooks/useInject.js +6 -0
- package/dist/{tui → shared}/hooks/useLogStream.js +7 -5
- package/dist/{hooks → shared/hooks}/useLoopFormValidation.js +2 -2
- package/dist/{tui → shared}/hooks/useLoopPolling.js +6 -4
- package/dist/shared/{useUndoRedo.js → hooks/useUndoRedo.js} +2 -4
- package/dist/shared/providers/InversifyProvider.js +6 -0
- package/dist/shared/services/export-service.js +43 -0
- package/dist/shared/services/log-service.js +28 -0
- package/dist/shared/services/loop-service.js +78 -0
- package/dist/shared/services/project-service.js +39 -0
- package/dist/shared/services/task-service.js +41 -0
- package/dist/shared/services/types.js +7 -0
- package/dist/{tui/components → shared/ui}/Button.js +1 -1
- package/dist/{tui/components → shared/ui}/DebugPanel.js +1 -1
- package/dist/{tui/components → shared/ui}/FocusableButton.js +1 -1
- package/dist/{tui/components → shared/ui}/FocusableInput.js +1 -1
- package/dist/{tui/components → shared/ui}/FocusableList.js +1 -1
- package/dist/{tui/components → shared/ui}/Modal.js +1 -1
- package/dist/{tui/components → shared/ui}/SelectModal.js +2 -2
- package/dist/{tui/components → shared/ui}/Toast.js +1 -1
- package/dist/shared/ui/state.js +9 -0
- package/dist/{tui → shared}/utils/paste.js +2 -2
- package/dist/{tui → shared}/utils/validation.js +1 -1
- package/dist/widgets/command-input/CommandDropdown.js +69 -0
- package/dist/widgets/command-input/CommandInput.js +129 -0
- package/dist/widgets/command-input/HintBar.js +9 -0
- package/dist/widgets/command-input/InputModes.js +68 -0
- package/dist/{tui/components → widgets/commands-browser}/CommandsBrowserModal.js +3 -3
- package/dist/{tui/components → widgets/header}/Header.js +3 -3
- package/dist/{tui/components → widgets/header}/TabBar.js +1 -1
- package/dist/{tui/components → widgets/left-panel}/LeftPanel.js +3 -3
- package/dist/{tui/components → widgets/left-panel}/Navigator.js +3 -3
- package/dist/{tui/components → widgets/left-panel}/ProjectsPage.js +9 -34
- package/dist/widgets/left-panel/ProjectsPageParts.js +34 -0
- package/dist/{tui/components → widgets/left-panel}/TaskBrowser.js +4 -4
- package/dist/{tui/components → widgets/log-modal}/LogModal.js +8 -6
- package/dist/widgets/loop-form/CreateForm.js +75 -0
- package/dist/widgets/loop-form/TextField.js +9 -0
- package/dist/{tui/components → widgets/loop-form}/WizardForm.js +5 -10
- package/dist/widgets/loop-form/useCreateSteps.js +107 -0
- package/dist/widgets/loop-form/useHandleComplete.js +74 -0
- package/dist/{tui/components → widgets/project-form}/ProjectForm.js +9 -7
- package/dist/{tui/components → widgets/right-panel}/Inspector.js +4 -4
- package/dist/{tui/components → widgets/right-panel}/RightPanel.js +4 -4
- package/dist/{tui/components → widgets/right-panel}/RunHistory.js +52 -4
- package/dist/{tui/components → widgets/task-form}/TaskForm.js +11 -9
- package/package.json +2 -1
- package/dist/core/loop-controller.js +0 -511
- package/dist/daemon/http-server.js +0 -599
- package/dist/daemon/server.js +0 -374
- package/dist/tui/App.js +0 -680
- package/dist/tui/components/CodeEditorModal.js +0 -244
- package/dist/tui/components/CommandInput.js +0 -273
- package/dist/tui/components/CreateForm.js +0 -224
- package/dist/tui/daemon.js +0 -142
- package/dist/tui/format.js +0 -111
- /package/dist/{tui/router.js → app/router/index.js} +0 -0
- /package/dist/{tui → app}/types.js +0 -0
- /package/dist/core/{resolve-cwd.js → command/resolve-cwd.js} +0 -0
- /package/dist/core/{context-parser.js → context/context-parser.js} +0 -0
- /package/dist/core/{template.js → context/template.js} +0 -0
- /package/dist/core/{log-parser.js → logging/log-parser.js} +0 -0
- /package/dist/core/{scheduling.js → scheduling/index.js} +0 -0
- /package/dist/{ipc → daemon/ipc}/send.js +0 -0
- /package/dist/{config → shared/config}/paths.js +0 -0
- /package/dist/{i18n → shared/i18n}/en.json +0 -0
- /package/dist/{i18n → shared/i18n}/index.js +0 -0
- /package/dist/{board → shared/ui}/format.js +0 -0
- /package/dist/{tui → shared/ui}/hooks/useHoverState.js +0 -0
- /package/dist/{tui → shared/ui}/theme.js +0 -0
- /package/dist/{tui → shared}/utils/syntax.js +0 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { executeCommand } from "../command/command-runner.js";
|
|
4
|
+
import { t } from "../../shared/i18n/index.js";
|
|
5
|
+
import { parseStdout } from "../context/context-parser.js";
|
|
6
|
+
import { interpolate } from "../context/template.js";
|
|
7
|
+
export function executeChain(options) {
|
|
8
|
+
const { chainTargetId, exitCode, task, chainContext, cwd, signal, runCount, logPath, runHistory, logStream, controller } = options;
|
|
9
|
+
if (!chainTargetId) {
|
|
10
|
+
return Promise.resolve({ runHistory, lastExitCode: exitCode, lastDuration: 0 });
|
|
11
|
+
}
|
|
12
|
+
const chainGroupId = crypto.randomUUID().slice(0, 8);
|
|
13
|
+
const mainRecord = runHistory[runHistory.length - 1];
|
|
14
|
+
if (mainRecord)
|
|
15
|
+
mainRecord.chainGroupId = chainGroupId;
|
|
16
|
+
let currentTargetId = chainTargetId;
|
|
17
|
+
let prevBranch = exitCode === 0 ? "onSuccess" : "onFailure";
|
|
18
|
+
let prevExit = exitCode;
|
|
19
|
+
let totalExtraDuration = 0;
|
|
20
|
+
let finalExitCode = exitCode;
|
|
21
|
+
return (async () => {
|
|
22
|
+
while (currentTargetId) {
|
|
23
|
+
const chainTask = controller.taskResolver(currentTargetId);
|
|
24
|
+
if (!chainTask)
|
|
25
|
+
break;
|
|
26
|
+
if (logStream) {
|
|
27
|
+
logStream.write(t("loop.chainHeader", { name: chainTask.name, branch: prevBranch, prevExit }));
|
|
28
|
+
}
|
|
29
|
+
const chainStartedAt = new Date().toISOString();
|
|
30
|
+
const chainOffset = fs.existsSync(logPath) ? fs.statSync(logPath).size : 0;
|
|
31
|
+
runHistory.push({
|
|
32
|
+
runNumber: runCount,
|
|
33
|
+
startedAt: chainStartedAt,
|
|
34
|
+
exitCode: -1,
|
|
35
|
+
duration: 0,
|
|
36
|
+
logSize: 0,
|
|
37
|
+
status: "running",
|
|
38
|
+
logOffset: chainOffset,
|
|
39
|
+
chainGroupId,
|
|
40
|
+
chainName: chainTask.name,
|
|
41
|
+
});
|
|
42
|
+
const interpolatedCommand = interpolate(chainTask.command, chainContext);
|
|
43
|
+
const interpolatedArgs = chainTask.commandArgs.map(a => interpolate(a, chainContext));
|
|
44
|
+
const chainResult = await executeCommand(interpolatedCommand, interpolatedArgs, cwd, logStream, signal, runCount, true, true);
|
|
45
|
+
if (chainResult.stdout) {
|
|
46
|
+
const parsed = parseStdout(chainResult.stdout);
|
|
47
|
+
if (parsed !== null) {
|
|
48
|
+
Object.assign(chainContext, parsed);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const chainLogSize = fs.existsSync(logPath) ? fs.statSync(logPath).size - chainOffset : 0;
|
|
52
|
+
const chainRecord = runHistory.find(r => r.chainGroupId === chainGroupId && r.status === "running" && r.chainName === chainTask.name);
|
|
53
|
+
if (chainRecord) {
|
|
54
|
+
chainRecord.exitCode = chainResult.exitCode;
|
|
55
|
+
chainRecord.duration = chainResult.duration;
|
|
56
|
+
chainRecord.logSize = Math.max(0, chainLogSize);
|
|
57
|
+
chainRecord.status = "completed";
|
|
58
|
+
}
|
|
59
|
+
totalExtraDuration += chainResult.duration;
|
|
60
|
+
finalExitCode = chainResult.exitCode;
|
|
61
|
+
currentTargetId = (chainResult.exitCode === 0 ? chainTask.onSuccessTaskId : chainTask.onFailureTaskId) ?? null;
|
|
62
|
+
prevBranch = chainResult.exitCode === 0 ? "onSuccess" : "onFailure";
|
|
63
|
+
prevExit = chainResult.exitCode;
|
|
64
|
+
}
|
|
65
|
+
return { runHistory, lastExitCode: finalExitCode, lastDuration: totalExtraDuration };
|
|
66
|
+
})();
|
|
67
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { sleep } from "../../shared/sleep.js";
|
|
2
|
+
import { SLEEP_CHUNK_MS } from "../../shared/config/constants.js";
|
|
3
|
+
export function waitForResume(ctrl) {
|
|
4
|
+
const savedStatus = ctrl._status;
|
|
5
|
+
ctrl._status = savedStatus === "idle" ? "idle" : "paused";
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
ctrl.resumeResolve = resolve;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
export async function waitForDelay(ctrl, ms, signal) {
|
|
11
|
+
let remaining = ms;
|
|
12
|
+
ctrl.remainingDelayMs = remaining;
|
|
13
|
+
let announced = false;
|
|
14
|
+
while (remaining > 0) {
|
|
15
|
+
if (ctrl._forceRun) {
|
|
16
|
+
ctrl._savedRemainingMs = remaining;
|
|
17
|
+
ctrl.remainingDelayMs = null;
|
|
18
|
+
ctrl.nextRunAt = null;
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
if (ctrl._resetSchedule) {
|
|
22
|
+
ctrl._resetSchedule = false;
|
|
23
|
+
remaining = ms;
|
|
24
|
+
ctrl.remainingDelayMs = remaining;
|
|
25
|
+
announced = false;
|
|
26
|
+
}
|
|
27
|
+
if (ctrl._paused) {
|
|
28
|
+
if (ctrl._status !== "idle") {
|
|
29
|
+
ctrl._status = "paused";
|
|
30
|
+
}
|
|
31
|
+
ctrl.emit("paused");
|
|
32
|
+
await waitForResume(ctrl);
|
|
33
|
+
announced = false;
|
|
34
|
+
if (signal.aborted) {
|
|
35
|
+
ctrl.remainingDelayMs = null;
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if (ctrl._resetSchedule) {
|
|
39
|
+
ctrl._resetSchedule = false;
|
|
40
|
+
remaining = ms;
|
|
41
|
+
ctrl.remainingDelayMs = remaining;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!announced) {
|
|
45
|
+
ctrl._status = "waiting";
|
|
46
|
+
ctrl.nextRunAt = new Date(Date.now() + remaining).toISOString();
|
|
47
|
+
ctrl.emit("waiting");
|
|
48
|
+
announced = true;
|
|
49
|
+
}
|
|
50
|
+
const chunk = Math.min(remaining, SLEEP_CHUNK_MS);
|
|
51
|
+
const startedAt = Date.now();
|
|
52
|
+
try {
|
|
53
|
+
await sleep(chunk, signal);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
ctrl.remainingDelayMs = null;
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
remaining = Math.max(0, remaining - (Date.now() - startedAt));
|
|
60
|
+
ctrl.remainingDelayMs = remaining;
|
|
61
|
+
}
|
|
62
|
+
ctrl.remainingDelayMs = null;
|
|
63
|
+
ctrl.nextRunAt = null;
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import { computePhase, alignToPhase } from "../scheduling/index.js";
|
|
4
|
+
import { runLoop } from "./loop-runner.js";
|
|
5
|
+
export class LoopController extends EventEmitter {
|
|
6
|
+
constructor(id, options, logPath, taskResolver, state, projectDirectory) {
|
|
7
|
+
super();
|
|
8
|
+
this.runAbortController = null;
|
|
9
|
+
this._paused = false;
|
|
10
|
+
this._forceRun = false;
|
|
11
|
+
this._savedRemainingMs = null;
|
|
12
|
+
this._resetSchedule = false;
|
|
13
|
+
this._stopAfterRun = false;
|
|
14
|
+
this._maxRunsReached = false;
|
|
15
|
+
this._status = "running";
|
|
16
|
+
this.resumeResolve = null;
|
|
17
|
+
this.runCount = 0;
|
|
18
|
+
this.lastRunAt = null;
|
|
19
|
+
this.lastExitCode = null;
|
|
20
|
+
this.lastDuration = null;
|
|
21
|
+
this.nextRunAt = null;
|
|
22
|
+
this.remainingDelayMs = null;
|
|
23
|
+
this.logStream = null;
|
|
24
|
+
this.loopPromise = null;
|
|
25
|
+
this.sessionStartedAt = null;
|
|
26
|
+
this.runHistory = [];
|
|
27
|
+
this.currentRunStartOffset = 0;
|
|
28
|
+
this.skippedCount = 0;
|
|
29
|
+
this.id = id;
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.logPath = logPath;
|
|
32
|
+
this.taskResolver = taskResolver;
|
|
33
|
+
this.projectDirectory = projectDirectory;
|
|
34
|
+
this.abortController = new AbortController();
|
|
35
|
+
this.createdAt = state?.createdAt ?? new Date().toISOString();
|
|
36
|
+
this.runCount = state?.runCount ?? 0;
|
|
37
|
+
this._maxRunsReached = state?.maxRunsReached ?? false;
|
|
38
|
+
this.sessionStartedAt = state?.sessionStartedAt ?? null;
|
|
39
|
+
this.lastRunAt = state?.lastRunAt ?? null;
|
|
40
|
+
this.lastExitCode = state?.lastExitCode ?? null;
|
|
41
|
+
this.lastDuration = state?.lastDuration ?? null;
|
|
42
|
+
this.nextRunAt = state?.nextRunAt ?? null;
|
|
43
|
+
this.remainingDelayMs = state?.remainingDelayMs ?? null;
|
|
44
|
+
this._status = state?.status ?? "running";
|
|
45
|
+
this._paused = state?.status === "paused" || state?.status === "idle";
|
|
46
|
+
this.runHistory = (state?.runHistory ?? []).map((r) => r.status === "running" ? { ...r, status: "completed" } : r).map((r) => ({ ...r, logOffset: r.logOffset ?? 0 }));
|
|
47
|
+
this.skippedCount = state?.skippedCount ?? 0;
|
|
48
|
+
}
|
|
49
|
+
get status() {
|
|
50
|
+
return this._status;
|
|
51
|
+
}
|
|
52
|
+
start() {
|
|
53
|
+
this.skippedCount = 0;
|
|
54
|
+
this.logStream?.end();
|
|
55
|
+
this.abortController = new AbortController();
|
|
56
|
+
this.logStream = fs.createWriteStream(this.logPath, { flags: "a" });
|
|
57
|
+
this.loopPromise = this.run();
|
|
58
|
+
if (this.sessionStartedAt === null) {
|
|
59
|
+
this.sessionStartedAt = new Date().toISOString();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
pause(interruptCurrentRun = false) {
|
|
63
|
+
if (this._status === "running" || this._status === "waiting") {
|
|
64
|
+
this._paused = true;
|
|
65
|
+
this._status = "paused";
|
|
66
|
+
if (interruptCurrentRun && this._status === "paused") {
|
|
67
|
+
this.runAbortController?.abort();
|
|
68
|
+
}
|
|
69
|
+
this.emit("paused");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
stopLoop(interruptCurrentRun = false) {
|
|
73
|
+
if (this._status === "running" || this._status === "waiting" || this._status === "paused") {
|
|
74
|
+
this._paused = true;
|
|
75
|
+
this._status = "idle";
|
|
76
|
+
this.sessionStartedAt = null;
|
|
77
|
+
this.remainingDelayMs = null;
|
|
78
|
+
this.nextRunAt = null;
|
|
79
|
+
if (interruptCurrentRun) {
|
|
80
|
+
this.runAbortController?.abort();
|
|
81
|
+
}
|
|
82
|
+
this.abortController.abort();
|
|
83
|
+
if (this.resumeResolve) {
|
|
84
|
+
this.resumeResolve();
|
|
85
|
+
this.resumeResolve = null;
|
|
86
|
+
}
|
|
87
|
+
this.emit("stopped");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
resume() {
|
|
91
|
+
if (this._status === "paused" && this._paused && this.resumeResolve) {
|
|
92
|
+
this._paused = false;
|
|
93
|
+
if (this.remainingDelayMs !== null) {
|
|
94
|
+
this._status = "waiting";
|
|
95
|
+
this.nextRunAt = new Date(Date.now() + this.remainingDelayMs).toISOString();
|
|
96
|
+
}
|
|
97
|
+
this.resumeResolve();
|
|
98
|
+
this.resumeResolve = null;
|
|
99
|
+
this.emit("resumed");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
playLoop() {
|
|
103
|
+
if (this._status !== "idle" && this._status !== "stopped")
|
|
104
|
+
return false;
|
|
105
|
+
if (this._maxRunsReached)
|
|
106
|
+
return false;
|
|
107
|
+
if (this.options.maxRuns !== null && this.runCount >= this.options.maxRuns) {
|
|
108
|
+
this._maxRunsReached = true;
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
this.sessionStartedAt = new Date().toISOString();
|
|
112
|
+
this._resetSchedule = true;
|
|
113
|
+
this._paused = false;
|
|
114
|
+
this._status = "waiting";
|
|
115
|
+
const phase = this.options.offset !== null
|
|
116
|
+
? this.options.offset
|
|
117
|
+
: computePhase(this.id, this.options.interval);
|
|
118
|
+
const delay = alignToPhase(Date.now(), this.options.interval, phase);
|
|
119
|
+
this.nextRunAt = new Date(Date.now() + delay).toISOString();
|
|
120
|
+
if (this.resumeResolve) {
|
|
121
|
+
this.resumeResolve();
|
|
122
|
+
this.resumeResolve = null;
|
|
123
|
+
}
|
|
124
|
+
this.start();
|
|
125
|
+
this.emit("resumed");
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
triggerNow() {
|
|
129
|
+
if (this._status === "running")
|
|
130
|
+
return false;
|
|
131
|
+
if (this._maxRunsReached)
|
|
132
|
+
return false;
|
|
133
|
+
const needsStart = this._status === "stopped" || this._status === "idle";
|
|
134
|
+
this._savedRemainingMs = this.remainingDelayMs;
|
|
135
|
+
this._forceRun = true;
|
|
136
|
+
if (needsStart) {
|
|
137
|
+
this._stopAfterRun = true;
|
|
138
|
+
this._paused = false;
|
|
139
|
+
this._status = "running";
|
|
140
|
+
this.start();
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
if (this._paused)
|
|
144
|
+
this.resume();
|
|
145
|
+
}
|
|
146
|
+
this.emit("triggered");
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
async stop() {
|
|
150
|
+
this.abortController.abort();
|
|
151
|
+
if (this._paused)
|
|
152
|
+
this.resume();
|
|
153
|
+
if (this.loopPromise) {
|
|
154
|
+
await this.loopPromise;
|
|
155
|
+
}
|
|
156
|
+
this.logStream?.end();
|
|
157
|
+
this.logStream = null;
|
|
158
|
+
}
|
|
159
|
+
getMeta() {
|
|
160
|
+
return {
|
|
161
|
+
id: this.id,
|
|
162
|
+
taskId: this.options.taskId,
|
|
163
|
+
status: this._status,
|
|
164
|
+
createdAt: this.createdAt,
|
|
165
|
+
maxRunsReached: this._maxRunsReached,
|
|
166
|
+
sessionStartedAt: this.sessionStartedAt,
|
|
167
|
+
runCount: this.runCount,
|
|
168
|
+
lastRunAt: this.lastRunAt,
|
|
169
|
+
lastExitCode: this.lastExitCode,
|
|
170
|
+
lastDuration: this.lastDuration,
|
|
171
|
+
nextRunAt: this.nextRunAt,
|
|
172
|
+
remainingDelayMs: this.remainingDelayMs,
|
|
173
|
+
runHistory: this.runHistory,
|
|
174
|
+
skippedCount: this.skippedCount,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
clearMaxRunsReached() {
|
|
178
|
+
this._maxRunsReached = false;
|
|
179
|
+
this._paused = false;
|
|
180
|
+
this._status = "idle";
|
|
181
|
+
this.remainingDelayMs = null;
|
|
182
|
+
this.nextRunAt = null;
|
|
183
|
+
this.runCount = 0;
|
|
184
|
+
}
|
|
185
|
+
isMaxRunsReached() {
|
|
186
|
+
return this._maxRunsReached;
|
|
187
|
+
}
|
|
188
|
+
run() {
|
|
189
|
+
return runLoop(this);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { computePhase, alignToPhase } from "../scheduling/index.js";
|
|
2
|
+
import { resolveEffectiveCwd } from "../command/resolve-cwd.js";
|
|
3
|
+
import { executeChain } from "./chain-executor.js";
|
|
4
|
+
import { executeRunImpl } from "./run-executor.js";
|
|
5
|
+
import { waitForResume, waitForDelay } from "./delay-utils.js";
|
|
6
|
+
export async function runLoop(ctrl) {
|
|
7
|
+
const signal = ctrl.abortController.signal;
|
|
8
|
+
let isFirstRun = true;
|
|
9
|
+
try {
|
|
10
|
+
while (!signal.aborted) {
|
|
11
|
+
if (ctrl.options.maxRuns !== null && ctrl.runCount >= ctrl.options.maxRuns) {
|
|
12
|
+
ctrl._maxRunsReached = true;
|
|
13
|
+
ctrl._paused = true;
|
|
14
|
+
ctrl._status = "paused";
|
|
15
|
+
ctrl.emit("paused");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (isFirstRun && ctrl._paused) {
|
|
19
|
+
await waitForResume(ctrl);
|
|
20
|
+
if (signal.aborted)
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
if (isFirstRun && ctrl.nextRunAt) {
|
|
24
|
+
const delay = Math.max(0, new Date(ctrl.nextRunAt).getTime() - Date.now());
|
|
25
|
+
if (delay > 0) {
|
|
26
|
+
const completed = await waitForDelay(ctrl, delay, signal);
|
|
27
|
+
if (!completed)
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else if (isFirstRun && !ctrl.options.immediate) {
|
|
32
|
+
const phase = ctrl.options.offset !== null
|
|
33
|
+
? ctrl.options.offset
|
|
34
|
+
: computePhase(ctrl.id, ctrl.options.interval);
|
|
35
|
+
const delay = alignToPhase(Date.now(), ctrl.options.interval, phase);
|
|
36
|
+
if (delay > 0) {
|
|
37
|
+
ctrl.nextRunAt = new Date(Date.now() + delay).toISOString();
|
|
38
|
+
}
|
|
39
|
+
const completed = await waitForDelay(ctrl, delay, signal);
|
|
40
|
+
if (!completed)
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
isFirstRun = false;
|
|
44
|
+
if (ctrl._paused) {
|
|
45
|
+
await waitForResume(ctrl);
|
|
46
|
+
if (signal.aborted)
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
const runStartedAtMs = Date.now();
|
|
50
|
+
const { exitCode, totalDuration } = await executeRunImpl(ctrl, signal);
|
|
51
|
+
const chainTargetId = exitCode === 0
|
|
52
|
+
? (ctrl.options.taskId ? ctrl.taskResolver(ctrl.options.taskId)?.onSuccessTaskId : undefined)
|
|
53
|
+
: (ctrl.options.taskId ? ctrl.taskResolver(ctrl.options.taskId)?.onFailureTaskId : undefined);
|
|
54
|
+
if (chainTargetId) {
|
|
55
|
+
const task = ctrl.options.taskId ? ctrl.taskResolver(ctrl.options.taskId) : null;
|
|
56
|
+
const cwd = resolveEffectiveCwd(ctrl.options.cwd, ctrl.projectDirectory);
|
|
57
|
+
const chainContext = {};
|
|
58
|
+
const chainResult = await executeChain({
|
|
59
|
+
chainTargetId,
|
|
60
|
+
exitCode,
|
|
61
|
+
task,
|
|
62
|
+
chainContext,
|
|
63
|
+
cwd,
|
|
64
|
+
signal,
|
|
65
|
+
runCount: ctrl.runCount,
|
|
66
|
+
logPath: ctrl.logPath,
|
|
67
|
+
runHistory: ctrl.runHistory,
|
|
68
|
+
logStream: ctrl.logStream,
|
|
69
|
+
controller: ctrl,
|
|
70
|
+
});
|
|
71
|
+
ctrl.runHistory = chainResult.runHistory;
|
|
72
|
+
ctrl.lastExitCode = chainResult.lastExitCode;
|
|
73
|
+
ctrl.lastDuration = (ctrl.lastDuration ?? 0) + chainResult.lastDuration;
|
|
74
|
+
}
|
|
75
|
+
if (ctrl.runHistory.length > 50) {
|
|
76
|
+
ctrl.runHistory = ctrl.runHistory.slice(-50);
|
|
77
|
+
}
|
|
78
|
+
ctrl.emit("run:end", { exitCode, duration: totalDuration });
|
|
79
|
+
if (signal.aborted)
|
|
80
|
+
break;
|
|
81
|
+
if (ctrl.options.maxRuns !== null && ctrl.runCount >= ctrl.options.maxRuns) {
|
|
82
|
+
ctrl._maxRunsReached = true;
|
|
83
|
+
ctrl._paused = true;
|
|
84
|
+
ctrl._status = "paused";
|
|
85
|
+
ctrl.remainingDelayMs = null;
|
|
86
|
+
ctrl.nextRunAt = null;
|
|
87
|
+
ctrl.emit("paused");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (ctrl._stopAfterRun) {
|
|
91
|
+
ctrl._stopAfterRun = false;
|
|
92
|
+
ctrl._paused = true;
|
|
93
|
+
ctrl._status = "idle";
|
|
94
|
+
ctrl.remainingDelayMs = null;
|
|
95
|
+
ctrl.nextRunAt = null;
|
|
96
|
+
ctrl.emit("stopped");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const saved = ctrl._savedRemainingMs;
|
|
100
|
+
ctrl._savedRemainingMs = null;
|
|
101
|
+
if (saved !== null) {
|
|
102
|
+
const remaining = Math.max(0, saved - totalDuration);
|
|
103
|
+
if (remaining > 0) {
|
|
104
|
+
const completed = await waitForDelay(ctrl, remaining, signal);
|
|
105
|
+
if (!completed)
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const nextSlotMs = runStartedAtMs + ctrl.options.interval;
|
|
111
|
+
const overrunMs = Date.now() - nextSlotMs;
|
|
112
|
+
if (overrunMs >= 0) {
|
|
113
|
+
const missed = Math.floor(overrunMs / ctrl.options.interval) + 1;
|
|
114
|
+
ctrl.skippedCount += missed;
|
|
115
|
+
const adjustedDelay = ctrl.options.interval - (overrunMs % ctrl.options.interval);
|
|
116
|
+
const completed = await waitForDelay(ctrl, adjustedDelay, signal);
|
|
117
|
+
if (!completed)
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const completed = await waitForDelay(ctrl, ctrl.options.interval, signal);
|
|
122
|
+
if (!completed)
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
if (ctrl._status !== "stopped" && ctrl._status !== "idle" && ctrl._status !== "paused" && !ctrl._maxRunsReached) {
|
|
130
|
+
ctrl._status = "stopped";
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { executeCommand } from "../command/command-runner.js";
|
|
3
|
+
import { rotateLogIfNeeded } from "../logging/log-rotator.js";
|
|
4
|
+
import { parseStdout } from "../context/context-parser.js";
|
|
5
|
+
import { interpolate } from "../context/template.js";
|
|
6
|
+
import { resolveEffectiveCwd } from "../command/resolve-cwd.js";
|
|
7
|
+
export async function executeRunImpl(ctrl, signal) {
|
|
8
|
+
ctrl._status = "running";
|
|
9
|
+
ctrl._forceRun = false;
|
|
10
|
+
ctrl.runCount++;
|
|
11
|
+
ctrl.lastRunAt = new Date().toISOString();
|
|
12
|
+
if (ctrl.sessionStartedAt === null) {
|
|
13
|
+
ctrl.sessionStartedAt = ctrl.lastRunAt;
|
|
14
|
+
}
|
|
15
|
+
ctrl.nextRunAt = null;
|
|
16
|
+
ctrl.emit("run:start", ctrl.runCount);
|
|
17
|
+
ctrl.logStream = rotateLogIfNeeded(ctrl.logPath, ctrl.logStream);
|
|
18
|
+
ctrl.currentRunStartOffset = fs.existsSync(ctrl.logPath) ? fs.statSync(ctrl.logPath).size : 0;
|
|
19
|
+
ctrl.runHistory.push({
|
|
20
|
+
runNumber: ctrl.runCount,
|
|
21
|
+
startedAt: ctrl.lastRunAt,
|
|
22
|
+
exitCode: -1,
|
|
23
|
+
duration: 0,
|
|
24
|
+
logSize: 0,
|
|
25
|
+
status: "running",
|
|
26
|
+
logOffset: ctrl.currentRunStartOffset,
|
|
27
|
+
});
|
|
28
|
+
ctrl.runAbortController = new AbortController();
|
|
29
|
+
const task = ctrl.options.taskId ? ctrl.taskResolver(ctrl.options.taskId) : null;
|
|
30
|
+
const cwd = resolveEffectiveCwd(ctrl.options.cwd, ctrl.projectDirectory);
|
|
31
|
+
const chainContext = {};
|
|
32
|
+
const hasChainTasks = !!(task?.onSuccessTaskId || task?.onFailureTaskId);
|
|
33
|
+
const singleCommandFallback = {
|
|
34
|
+
command: task?.command ?? ctrl.options.command,
|
|
35
|
+
commandArgs: task?.commandArgs ?? ctrl.options.commandArgs,
|
|
36
|
+
commandRaw: task?.commandRaw ?? ctrl.options.commandRaw,
|
|
37
|
+
};
|
|
38
|
+
const taskSteps = task?.steps?.length
|
|
39
|
+
? task.steps
|
|
40
|
+
: [{ commands: [singleCommandFallback] }];
|
|
41
|
+
const shouldCaptureStdout = hasChainTasks || taskSteps.length > 1 || taskSteps[0].commands.length > 1;
|
|
42
|
+
let exitCode = 0;
|
|
43
|
+
let totalDuration = 0;
|
|
44
|
+
for (const step of taskSteps) {
|
|
45
|
+
const stepResults = await Promise.allSettled(step.commands.map((cmd) => executeCommand(interpolate(cmd.command, chainContext), cmd.commandArgs.map(a => interpolate(a, chainContext)), cwd, ctrl.logStream, AbortSignal.any([signal, ctrl.runAbortController.signal]), ctrl.runCount, shouldCaptureStdout)));
|
|
46
|
+
let stepStdout = "";
|
|
47
|
+
for (const r of stepResults) {
|
|
48
|
+
if (r.status === "fulfilled") {
|
|
49
|
+
totalDuration += r.value.duration;
|
|
50
|
+
if (r.value.stdout)
|
|
51
|
+
stepStdout += (stepStdout ? "\n" : "") + r.value.stdout;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (shouldCaptureStdout && stepStdout) {
|
|
55
|
+
const parsed = parseStdout(stepStdout);
|
|
56
|
+
if (parsed !== null) {
|
|
57
|
+
Object.assign(chainContext, parsed);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const stepFailure = stepResults.some((r) => r.status === "rejected" || (r.status === "fulfilled" && r.value.exitCode !== 0));
|
|
61
|
+
if (stepFailure) {
|
|
62
|
+
const failed = stepResults.find((r) => r.status === "fulfilled" && r.value.exitCode !== 0);
|
|
63
|
+
exitCode = failed ? failed.value.exitCode : 1;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
ctrl.runAbortController = null;
|
|
68
|
+
ctrl.lastExitCode = exitCode;
|
|
69
|
+
ctrl.lastDuration = totalDuration;
|
|
70
|
+
const logSize = fs.existsSync(ctrl.logPath) ? fs.statSync(ctrl.logPath).size - ctrl.currentRunStartOffset : 0;
|
|
71
|
+
const runningRecord = ctrl.runHistory.find((r) => r.runNumber === ctrl.runCount);
|
|
72
|
+
if (runningRecord) {
|
|
73
|
+
runningRecord.exitCode = exitCode;
|
|
74
|
+
runningRecord.duration = totalDuration;
|
|
75
|
+
runningRecord.logSize = Math.max(0, logSize);
|
|
76
|
+
runningRecord.status = "completed";
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
ctrl.runHistory.push({
|
|
80
|
+
runNumber: ctrl.runCount,
|
|
81
|
+
startedAt: ctrl.lastRunAt,
|
|
82
|
+
exitCode,
|
|
83
|
+
duration: totalDuration,
|
|
84
|
+
logSize: Math.max(0, logSize),
|
|
85
|
+
status: "completed",
|
|
86
|
+
logOffset: ctrl.currentRunStartOffset,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return { exitCode, totalDuration };
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// --- Response helpers ---
|
|
2
|
+
export function sendJson(res, status, data) {
|
|
3
|
+
const body = JSON.stringify(data);
|
|
4
|
+
res.writeHead(status, {
|
|
5
|
+
"Content-Type": "application/json",
|
|
6
|
+
"Content-Length": Buffer.byteLength(body),
|
|
7
|
+
});
|
|
8
|
+
res.end(body);
|
|
9
|
+
}
|
|
10
|
+
export function sendOk(res, data, status = 200) {
|
|
11
|
+
sendJson(res, status, { ok: true, data: data ?? null });
|
|
12
|
+
}
|
|
13
|
+
export function sendError(res, status, message) {
|
|
14
|
+
sendJson(res, status, { ok: false, error: { message } });
|
|
15
|
+
}
|
|
16
|
+
export function sendNotFound(res, id) {
|
|
17
|
+
sendError(res, 404, `Not found: ${id}`);
|
|
18
|
+
}
|
|
19
|
+
export function matchRoute(routes, method, pathSegments) {
|
|
20
|
+
for (const route of routes) {
|
|
21
|
+
if (route.method !== method)
|
|
22
|
+
continue;
|
|
23
|
+
if (route.segments.length !== pathSegments.length)
|
|
24
|
+
continue;
|
|
25
|
+
const params = {};
|
|
26
|
+
let matched = true;
|
|
27
|
+
for (let i = 0; i < route.segments.length; i++) {
|
|
28
|
+
const routeSeg = route.segments[i];
|
|
29
|
+
const pathSeg = pathSegments[i];
|
|
30
|
+
if (routeSeg.startsWith(":")) {
|
|
31
|
+
params[routeSeg.slice(1)] = decodeURIComponent(pathSeg);
|
|
32
|
+
}
|
|
33
|
+
else if (routeSeg !== pathSeg) {
|
|
34
|
+
matched = false;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (matched) {
|
|
39
|
+
return { params, handler: route.handler };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
// --- Request parsing ---
|
|
45
|
+
export function parsePath(url) {
|
|
46
|
+
if (!url)
|
|
47
|
+
return [];
|
|
48
|
+
const pathname = url.split("?")[0] ?? "";
|
|
49
|
+
return pathname.split("/").filter((s) => s.length > 0);
|
|
50
|
+
}
|
|
51
|
+
export function parseQuery(url) {
|
|
52
|
+
if (!url)
|
|
53
|
+
return new URLSearchParams();
|
|
54
|
+
const qs = url.split("?")[1] ?? "";
|
|
55
|
+
return new URLSearchParams(qs);
|
|
56
|
+
}
|
|
57
|
+
export async function readBody(req) {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
let body = "";
|
|
60
|
+
req.on("data", (chunk) => {
|
|
61
|
+
body += chunk.toString();
|
|
62
|
+
if (body.length > 1024 * 1024) {
|
|
63
|
+
reject(new Error("Body too large"));
|
|
64
|
+
req.destroy();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
req.on("end", () => {
|
|
68
|
+
if (!body.trim()) {
|
|
69
|
+
resolve({});
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
resolve(JSON.parse(body));
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
reject(new Error("Invalid JSON body"));
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
req.on("error", reject);
|
|
80
|
+
});
|
|
81
|
+
}
|