deployontime 0.0.0-stage → 0.1.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.
- package/LICENSE +661 -0
- package/README.md +266 -2
- package/SECURITY.md +61 -0
- package/mcp/jobs.mjs +137 -0
- package/mcp/runner.mjs +64 -0
- package/mcp/server.mjs +209 -0
- package/mcp/tools.mjs +380 -0
- package/package.json +48 -4
- package/queue/README.md +885 -0
- package/queue/args.mjs +136 -0
- package/queue/cli.mjs +207 -0
- package/queue/clock.mjs +49 -0
- package/queue/commands.mjs +35 -0
- package/queue/failstop.mjs +45 -0
- package/queue/files.mjs +71 -0
- package/queue/fingerprint.mjs +63 -0
- package/queue/gate.mjs +37 -0
- package/queue/git.mjs +61 -0
- package/queue/health.mjs +154 -0
- package/queue/idle.mjs +63 -0
- package/queue/job-wrapper.mjs +116 -0
- package/queue/journal.mjs +216 -0
- package/queue/kinds.mjs +95 -0
- package/queue/limits.mjs +79 -0
- package/queue/linux.mjs +72 -0
- package/queue/nested.mjs +83 -0
- package/queue/notices.mjs +114 -0
- package/queue/notify.mjs +47 -0
- package/queue/probes.mjs +162 -0
- package/queue/proctree.mjs +124 -0
- package/queue/project.mjs +26 -0
- package/queue/ps.mjs +54 -0
- package/queue/reaper.mjs +278 -0
- package/queue/run.mjs +628 -0
- package/queue/schedule-check.mjs +177 -0
- package/queue/settings.mjs +145 -0
- package/queue/setup.mjs +270 -0
- package/queue/slots.mjs +144 -0
- package/queue/stats.mjs +8 -0
- package/queue/stray.mjs +38 -0
- package/queue/timer.mjs +47 -0
- package/queue/units.mjs +8 -0
package/README.md
CHANGED
|
@@ -1,3 +1,267 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="docs/assets/banner.svg" width="100%" alt="DeployOnTime, an open-source software factory: you hand a task to the DeployOnTime orchestrator, which plans it, builds it with coding agents, validates it, runs security checks and delivers a finished feature for you to review. Available now: the machine queue for macOS and Linux. The orchestrator is planned; validate, secure and deliver come next.">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://github.com/Moezat220/deployontime/actions/workflows/test.yml"><img src="https://github.com/Moezat220/deployontime/actions/workflows/test.yml/badge.svg" alt="tests"></a>
|
|
7
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/license-AGPL--3.0-blue" alt="license AGPL-3.0"></a>
|
|
8
|
+
<img src="https://img.shields.io/badge/node-%E2%89%A522-339933" alt="node 22 or newer">
|
|
9
|
+
<img src="https://img.shields.io/badge/platform-macOS%20%7C%20Linux-lightgrey" alt="platform macOS and Linux">
|
|
10
|
+
<img src="https://img.shields.io/badge/dependencies-none-brightgreen" alt="no dependencies">
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
<p align="center">
|
|
14
|
+
<a href="#install">Install</a> ·
|
|
15
|
+
<a href="#how-it-works">How it works</a> ·
|
|
16
|
+
<a href="#faq">FAQ</a> ·
|
|
17
|
+
<a href="queue/README.md">Manual</a> ·
|
|
18
|
+
<a href="#roadmap">Roadmap</a>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<br>
|
|
22
|
+
|
|
23
|
+
**DeployOnTime is an open-source autonomous software factory in the making** for building with AI coding agents:
|
|
24
|
+
several agents working side by side today; tests, reviews and security checks, safe live releases and AI orchestration
|
|
25
|
+
next. It is published one part at a time, and the first part, the machine queue, works on its own.
|
|
26
|
+
|
|
27
|
+
**Ready to use today: the machine queue** (macOS and Linux). It lets several coding agents share one computer without
|
|
28
|
+
overloading it. The next parts are on the [roadmap](#roadmap).
|
|
29
|
+
|
|
30
|
+
<p align="center">
|
|
31
|
+
<img src="docs/assets/status.svg" alt="dotime-queue status: two jobs running, one waiting for a free slot" width="860">
|
|
32
|
+
<br>
|
|
33
|
+
<sub>Example output, shortened.</sub>
|
|
34
|
+
</p>
|
|
35
|
+
|
|
36
|
+
## The problem
|
|
37
|
+
|
|
38
|
+
Coding agents each start their own test runs and builds. Three agents on one computer can mean three full test suites
|
|
39
|
+
at once: memory and CPU run out, tests that pass on their own start timing out, and an agent then "fixes" code that
|
|
40
|
+
was never broken. Each agent sees only its own run, so neither you nor the agents can see the real cause.
|
|
41
|
+
|
|
42
|
+
## What you get
|
|
43
|
+
|
|
44
|
+
| | Without a queue | With `dotime-queue` |
|
|
45
|
+
| --- | --- | --- |
|
|
46
|
+
| **Several agents start test suites** | They all run at once | At most 2 run together (`slots`); the rest wait their turn |
|
|
47
|
+
| **The computer is already busy** | New jobs pile on | Jobs wait while CPU, memory or disk is short, or the battery is low |
|
|
48
|
+
| **A job hangs** | It can run until someone notices | It is stopped at its time limit, with everything it started (exit 124) |
|
|
49
|
+
| **A check keeps failing on the same code** | An agent may keep re-running it | A third try is blocked for up to 6 hours (exit 3) until the code changes or `--after-fix` is given |
|
|
50
|
+
| **This commit already has a green run** | It may run again | A test or lint check that already passed on the same clean commit, in the same folder, is skipped (exit 0); `--no-skip` runs it anyway |
|
|
51
|
+
| **Where did the time go?** | No record | `dotime-queue history` totals waits and results per project |
|
|
52
|
+
| **Dev servers left running** | They keep using memory | An optional reaper stops the idle ones |
|
|
53
|
+
|
|
54
|
+
## How it works
|
|
55
|
+
|
|
56
|
+
The queue is a command that agents put in front of their heavy commands:
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
dotime-queue run --label "shop-api: unit tests" -- npm test
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
1. **Turn.** The job takes the next free slot, or waits its turn.
|
|
63
|
+
2. **Health.** Has the computer room for it? Before it starts, the queue looks at CPU load, free memory, free disk
|
|
64
|
+
and, on battery, the charge.
|
|
65
|
+
3. **Limit.** It runs under a time limit learned from earlier passed runs: first the command's own, then those of its
|
|
66
|
+
kind of job in the project, once either has five; before that, 30 minutes. A new command that takes longer than
|
|
67
|
+
what its kind has learned (which can be as short as 10 minutes) needs `--max-run <minutes>`.
|
|
68
|
+
4. **Record.** Its wait, run time and result go into a journal that later runs and `history` use.
|
|
69
|
+
|
|
70
|
+
Only commands started through the queue are queued. A command an agent runs directly, such as a plain `npm test`, is
|
|
71
|
+
not. The queue never edits your repository; it keeps its state in `~/.dotime`.
|
|
72
|
+
|
|
73
|
+
## Measured
|
|
74
|
+
|
|
75
|
+
On a Mac, 4 agents start the same test suite at the same moment. Each suite runs 5 threads (100 timed tests) and holds 2
|
|
76
|
+
GB, and the Mac has half as many cores as the 4 suites have busy threads, so started together they ask for twice the CPU
|
|
77
|
+
there is; through the queue at most 2 suites run together (its default of 2 slots). A test fails when it takes more than
|
|
78
|
+
1.6 times as long as when one suite runs by itself, so this setup shows what doubling the load does; a looser timeout or
|
|
79
|
+
a lighter load would narrow the gap. After a failure an agent waits 5 to 30 s (the same random waits both ways) and runs
|
|
80
|
+
the suite again, up to 8 runs in all. Three runs each way:
|
|
81
|
+
|
|
82
|
+
| | Started directly | Through `dotime-queue` |
|
|
83
|
+
| --- | --- | --- |
|
|
84
|
+
| **Tests that failed on the first try** | 400 of 400, in every run | 0 of 400, in every run |
|
|
85
|
+
| **Time until every agent had a green run** | 373 s in one run; in the other two, some agents were still red after 8 runs (447 s and 469 s) | 57 to 58 s |
|
|
86
|
+
| **Suite runs** | 25 to 32 | 4 |
|
|
87
|
+
|
|
88
|
+
The code under test never changes, so every failure comes from the busy computer. The suite is a stand-in, not a real
|
|
89
|
+
project, and your numbers will differ with your machine and your tests. To run it yourself (it keeps every core busy
|
|
90
|
+
for about half an hour; it uses a temporary state folder and repository):
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
node bench/run.mjs
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Anyone can also run it on GitHub's macOS runner: in Actions, pick "Benchmark (manual)" and press Run workflow. It is
|
|
97
|
+
not one of the required checks and only starts by hand. The runner is smaller than the Mac above, so the settings are
|
|
98
|
+
scaled to it by the same rule (about twice as many busy threads as cores; less memory per thread only if 400 MB would
|
|
99
|
+
not fit in half the RAM); the runner's cores and memory are recorded in the results. Every run uploads all its results
|
|
100
|
+
(the raw JSON, the full output and the queue journals) as one artifact named benchmark-results, whatever they show.
|
|
101
|
+
|
|
102
|
+
## Install
|
|
103
|
+
|
|
104
|
+
Requirements: macOS or Linux, Node 22 or newer, git 2.31 or newer. The package has no dependencies.
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
npm install -g deployontime # puts dotime-queue and dotime-mcp on your path
|
|
108
|
+
dotime-queue status
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
To work from the source instead: `git clone https://github.com/Moezat220/deployontime.git`, then `npm link` in its
|
|
112
|
+
top folder.
|
|
113
|
+
|
|
114
|
+
A job waits while the computer is short of room: by default below 20% free memory or 10 GB free disk, and while the
|
|
115
|
+
CPU is overloaded. On a small machine, `dotime-queue status` shows whether that gate is shut, and the limits can be
|
|
116
|
+
lowered in `~/.dotime/config.json`, for example `{ "health": { "minFreeMemPct": 5, "minFreeDiskGB": 2 } }`.
|
|
117
|
+
|
|
118
|
+
Then tell your agents to use it. In the folder of each project they work in, run:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
dotime-queue setup # shows what it would add, and changes nothing
|
|
122
|
+
dotime-queue setup --write # adds it
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
It works in the top folder of the git repository. It adds the queue instruction to the project's `AGENTS.md`,
|
|
126
|
+
`CLAUDE.md` and `.claude/CLAUDE.md`, between a begin and an end marker, or creates `AGENTS.md` when there is none; a
|
|
127
|
+
second run updates that block and leaves the rest of each file alone.
|
|
128
|
+
`--mcp` also prints the settings for the [MCP server](#use-with-an-mcp-client). The block says, in short:
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
Start every test run, build and other heavy command through the queue:
|
|
132
|
+
dotime-queue run --label "<project>: <task>" -- <command>
|
|
133
|
+
A test or lint run that prints "skipped:" already passed on this exact commit and exits 0; add --no-skip in front
|
|
134
|
+
of the -- only to run it again on purpose. Exit 75 means the job gave up waiting for a slot, 124 that it hit
|
|
135
|
+
its run limit, 3 that the same check already failed twice on unchanged code.
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
A coding agent that opens this repository finds what it needs in [AGENTS.md](AGENTS.md).
|
|
139
|
+
|
|
140
|
+
You do not need to tell agents to call `dotime-queue passed` first: `run` checks for a pass by itself, in the same
|
|
141
|
+
folder, with the same label, command and code. `--no-skip` on an outer job also reaches the checks it runs through the
|
|
142
|
+
queue.
|
|
143
|
+
|
|
144
|
+
The text before the colon in a label is the project name; limits and history are kept per project.
|
|
145
|
+
|
|
146
|
+
To remove it: `npm uninstall -g deployontime`, delete `~/.dotime`, and take the reaper out of your scheduler if you
|
|
147
|
+
added it.
|
|
148
|
+
|
|
149
|
+
Every command, flag, exit code and setting is in the [queue manual](queue/README.md).
|
|
150
|
+
|
|
151
|
+
## Use with an MCP client
|
|
152
|
+
|
|
153
|
+
`dotime-mcp` is an MCP server on standard input and output that offers the queue to agents as tools. It has no
|
|
154
|
+
dependencies either, and the install puts it on your path next to `dotime-queue`. Add one entry to your client's MCP
|
|
155
|
+
server settings:
|
|
156
|
+
|
|
157
|
+
```json
|
|
158
|
+
{
|
|
159
|
+
"mcpServers": {
|
|
160
|
+
"dotime-queue": { "command": "dotime-mcp" }
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Clients differ in where that file lives and in how its top level is named; see your client's documentation. Without
|
|
166
|
+
installing it first, `"command": "npx", "args": ["-y", "-p", "deployontime", "dotime-mcp"]` fetches the package and
|
|
167
|
+
starts the server. From a clone that was not linked, use
|
|
168
|
+
`"command": "node", "args": ["<path to the clone>/mcp/server.mjs"]`. To use another state folder, add
|
|
169
|
+
`"env": { "DOTIME_HOME": "<absolute path of the folder>" }`, as for the command. A relative path is taken from the
|
|
170
|
+
folder the server starts in, and its jobs are handed that absolute path.
|
|
171
|
+
|
|
172
|
+
| Tool | Arguments | What it gives |
|
|
173
|
+
| --- | --- | --- |
|
|
174
|
+
| `queue_status` | none | The lines of `dotime-queue status`, with the slot and line counts as fields |
|
|
175
|
+
| `queue_passed` | `match`, `cwd` | The stamp or no-stamp line of `dotime-queue passed`, with `stamp` as `true` or `false` |
|
|
176
|
+
| `queue_history` | `days` (1 to 365, default 7) | The lines of `dotime-queue history` |
|
|
177
|
+
| `queue_run` | `label`, `command` (a list of words), `cwd`, `kind`, `maxRunMin`, `noSkip`, `afterFix` | Starts `dotime-queue run` in the background and returns a job id at once |
|
|
178
|
+
| `queue_job` | `jobId`, `tailLines` (0 to 500, default 40) | `starting`, `waiting`, `running`, `finished` or `lost`; for a finished job its exit code and outcome (`ran`, `skipped`, `refused`, `timed-out`, `gave-up` and the others), and the last lines of its output |
|
|
179
|
+
|
|
180
|
+
`cwd` is an absolute path to an existing folder; without it, the folder the server was started in is used. The label
|
|
181
|
+
is `"<project>: <task>"`. The command is a list of words and never goes through a shell. A job from `queue_run` waits
|
|
182
|
+
and runs under the same rules as one started from the shell, in a process group of its own, and keeps running when
|
|
183
|
+
the server exits. Its output and pid files are kept in `mcp-jobs/` in the state folder for 7 days, and at most 200
|
|
184
|
+
finished jobs are kept. `--priority` and `--max-wait` are not offered: such a job waits at most the usual 60 minutes.
|
|
185
|
+
Each waiting job costs two Node processes, so `queue_run` refuses a new job while 20 that it started still wait or
|
|
186
|
+
run; `mcpMaxJobs` in the queue's `config.json` sets that number. Every tool result has the readable lines as text,
|
|
187
|
+
then the same fields as JSON text, and the fields in `structuredContent`.
|
|
188
|
+
|
|
189
|
+
The shell commands keep working as before; both share the same state folder, slots and journal. The server speaks MCP
|
|
190
|
+
revision 2026-07-28 (no handshake: each request names its version in `_meta`, and `server/discover` lists the
|
|
191
|
+
versions) and 2025-11-25 and earlier (opened with `initialize`).
|
|
192
|
+
|
|
193
|
+
## FAQ
|
|
194
|
+
|
|
195
|
+
<details>
|
|
196
|
+
<summary><strong>Does it work with my coding agent?</strong></summary>
|
|
197
|
+
|
|
198
|
+
Yes, with any agent that can run a shell command. There is no plugin; the agent only needs the instruction above.
|
|
199
|
+
</details>
|
|
200
|
+
|
|
201
|
+
<details>
|
|
202
|
+
<summary><strong>Does it watch everything on my computer?</strong></summary>
|
|
203
|
+
|
|
204
|
+
No. Only commands started with `dotime-queue run` are queued, held back or limited. Two optional extras look wider,
|
|
205
|
+
and only if you schedule them: the reaper (idle dev servers) and the scheduled-job report (your own scheduled jobs).
|
|
206
|
+
</details>
|
|
207
|
+
|
|
208
|
+
<details>
|
|
209
|
+
<summary><strong>Does it change my code or my files?</strong></summary>
|
|
210
|
+
|
|
211
|
+
No. In your repository it only reads: read-only git commands, and files to tell whether a check's code changed. Its
|
|
212
|
+
own state (slots, journal, settings) lives in `~/.dotime`.
|
|
213
|
+
</details>
|
|
214
|
+
|
|
215
|
+
<details>
|
|
216
|
+
<summary><strong>What does the reaper stop?</strong></summary>
|
|
217
|
+
|
|
218
|
+
When you schedule it, it stops dev servers of your user account that sit idle anywhere on the computer, including ones
|
|
219
|
+
started outside the queue. A trial run with `--dry` lists the servers it would stop and stops none, and a keep pattern
|
|
220
|
+
protects the servers you want left alone. See [the reaper](queue/README.md#reaper).
|
|
221
|
+
</details>
|
|
222
|
+
|
|
223
|
+
<details>
|
|
224
|
+
<summary><strong>Can agents in containers or virtual machines share it?</strong></summary>
|
|
225
|
+
|
|
226
|
+
No. Agents share one queue only when they use the same state folder on the same computer. An agent in a container or a
|
|
227
|
+
virtual machine sees a different process list, so it must not share the folder: it gets a queue of its own.
|
|
228
|
+
</details>
|
|
229
|
+
|
|
230
|
+
<details>
|
|
231
|
+
<summary><strong>Does it run on Linux or Windows?</strong></summary>
|
|
232
|
+
|
|
233
|
+
Linux, yes: the queue and the reaper work on Linux servers and desktops, x64 and arm64, and the tests run on macOS
|
|
234
|
+
and Linux in CI. On Linux the readings come from `/proc` and `/sys`, desktop notices go through `notify-send`, and the
|
|
235
|
+
optional scheduled-job report, which reads macOS launchd jobs, is not available yet. The
|
|
236
|
+
[manual](queue/README.md#what-differs-on-linux) lists the differences. Windows: not yet.
|
|
237
|
+
</details>
|
|
238
|
+
|
|
239
|
+
<details>
|
|
240
|
+
<summary><strong>What does the AGPL license mean for me?</strong></summary>
|
|
241
|
+
|
|
242
|
+
Using the queue on your own computer does not put your projects under this license. Its conditions apply when you pass
|
|
243
|
+
this code on, changed or not, and when you let others use a changed version of it over a network.
|
|
244
|
+
</details>
|
|
245
|
+
|
|
246
|
+
## Roadmap
|
|
247
|
+
|
|
248
|
+
The machine queue is complete and usable on its own. These parts come next, in this order:
|
|
249
|
+
|
|
250
|
+
| Next | What it will do |
|
|
251
|
+
| --- | --- |
|
|
252
|
+
| Release engine | Validate an agent's pull request with tests, reviews and security checks, then deploy it as a safe live release (stages 03 and 04 in the banner) |
|
|
253
|
+
| Install and quickstart | Set everything up with one command, with a trial mode that changes nothing |
|
|
254
|
+
| Docker | Run the queue in containers |
|
|
255
|
+
| Orchestration | Plan the work and hand it out to several coding agents (stage 02 in the banner) |
|
|
256
|
+
|
|
257
|
+
## Tests
|
|
258
|
+
|
|
259
|
+
From the repository's top folder:
|
|
260
|
+
|
|
261
|
+
```
|
|
262
|
+
npm test
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
## License
|
|
266
|
+
|
|
267
|
+
Copyright (C) 2026 Moezat220 · [AGPL-3.0-only](LICENSE)
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
Thank you for helping keep DeployOnTime and its users safe.
|
|
4
|
+
|
|
5
|
+
## Supported versions
|
|
6
|
+
|
|
7
|
+
DeployOnTime is published one part at a time. Security fixes go into the latest code on the `main` branch, so please update to the latest `main` before reporting, in case the problem is already fixed.
|
|
8
|
+
|
|
9
|
+
| Part | Supported |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| Machine queue (`dotime-queue`), latest `main` | Yes |
|
|
12
|
+
| Older commits | No |
|
|
13
|
+
|
|
14
|
+
## Reporting a vulnerability
|
|
15
|
+
|
|
16
|
+
Please **do not** report security problems in a public issue, discussion or pull request.
|
|
17
|
+
|
|
18
|
+
Report them privately instead:
|
|
19
|
+
|
|
20
|
+
1. Open the **Security** tab of this repository.
|
|
21
|
+
2. Click **Report a vulnerability**.
|
|
22
|
+
3. Fill in the form. Only you and the maintainer can see it.
|
|
23
|
+
|
|
24
|
+
It helps a lot if you include:
|
|
25
|
+
|
|
26
|
+
- What the problem is and what someone could do with it
|
|
27
|
+
- Steps to reproduce it, or a small proof of concept
|
|
28
|
+
- Your operating system (macOS or Linux, with its version), Node and git versions, and the commit you tested
|
|
29
|
+
- Any idea you have for a fix
|
|
30
|
+
|
|
31
|
+
## What to expect
|
|
32
|
+
|
|
33
|
+
DeployOnTime is maintained by one person, so these are goals rather than guarantees:
|
|
34
|
+
|
|
35
|
+
- A reply confirming your report within 3 business days
|
|
36
|
+
- A first assessment within 7 days
|
|
37
|
+
- Updates as the fix progresses
|
|
38
|
+
- Once fixed, a published GitHub security advisory that credits you, unless you would rather stay anonymous
|
|
39
|
+
|
|
40
|
+
Please allow reasonable time for a fix before sharing details publicly. The aim is to agree on a disclosure date with you, within 90 days of your report.
|
|
41
|
+
|
|
42
|
+
## Scope
|
|
43
|
+
|
|
44
|
+
In scope: problems in DeployOnTime's own code, for example:
|
|
45
|
+
|
|
46
|
+
- A label, flag, setting or file that makes `dotime-queue` run something the user did not ask for
|
|
47
|
+
- The time limit or the reaper stopping processes it was not meant to stop
|
|
48
|
+
- Reading or writing files outside `~/.dotime`, or unsafe handling of files inside it (such as symlink tricks)
|
|
49
|
+
- Secrets from commands or environment variables leaking into the journal, history or other saved state
|
|
50
|
+
- Another user on the same computer being able to read or change the queue's state
|
|
51
|
+
|
|
52
|
+
Out of scope:
|
|
53
|
+
|
|
54
|
+
- Problems in the commands you run through the queue
|
|
55
|
+
- Problems in Node.js, git, macOS or Linux themselves (please report those to their own projects)
|
|
56
|
+
- Attacks that need someone to already control your user account
|
|
57
|
+
- The queue working as designed, such as jobs waiting, time limits or the retry block (open a normal issue if the behavior seems wrong)
|
|
58
|
+
|
|
59
|
+
## Good-faith research
|
|
60
|
+
|
|
61
|
+
If you research and report in good faith under this policy, no legal action will be taken against you. Please only test on your own computer and data, and do not access or change other people's data.
|
package/mcp/jobs.mjs
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// The jobs the MCP server starts, kept in mcp-jobs/ in the state folder. A job id names four files there:
|
|
2
|
+
// <id>.json (the request, written once by the server), <id>.pid (the runner and queue pids, written by the runner),
|
|
3
|
+
// <id>.exit (how the queue process ended, written by the runner) and <id>.log (everything the queue process and the
|
|
4
|
+
// job printed). An id is 24 lowercase hex characters, and every file name is built from a checked id only, so no
|
|
5
|
+
// caller can name a file outside this folder. Old jobs are pruned; a job whose runner is still alive never is.
|
|
6
|
+
import { randomBytes } from "node:crypto";
|
|
7
|
+
import { closeSync, constants, fstatSync, mkdirSync, openSync, readdirSync, readSync, rmSync, statSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { readJson, writeJsonAtomic } from "../queue/files.mjs";
|
|
10
|
+
import { alive } from "../queue/proctree.mjs";
|
|
11
|
+
import { probeProcessAges } from "../queue/probes.mjs";
|
|
12
|
+
import { parseElapsed } from "../queue/ps.mjs";
|
|
13
|
+
|
|
14
|
+
export const JOB_ID_RE = /^[0-9a-f]{24}$/;
|
|
15
|
+
export const KEEP_DAYS = 7;
|
|
16
|
+
export const KEEP_JOBS = 200;
|
|
17
|
+
/** How long a job may go without a pid file before it counts as lost: its runner never started. */
|
|
18
|
+
export const START_GRACE_MS = 30_000;
|
|
19
|
+
const DAY = 24 * 60 * 60 * 1000;
|
|
20
|
+
const SAME_START_MS = 5000;
|
|
21
|
+
const TAIL_BYTES = 256 * 1024;
|
|
22
|
+
const MAX_LINE = 2000;
|
|
23
|
+
const EXTENSIONS = [".json", ".pid", ".exit", ".log"];
|
|
24
|
+
|
|
25
|
+
export const jobsDir = (home) => join(home, "mcp-jobs");
|
|
26
|
+
export const newJobId = () => randomBytes(12).toString("hex");
|
|
27
|
+
|
|
28
|
+
/** The files of one job. Throws for an id that is not a job id, so no path is ever built from other text. */
|
|
29
|
+
export function jobFiles(dir, id) {
|
|
30
|
+
if (typeof id !== "string" || !JOB_ID_RE.test(id)) throw new Error("not a job id");
|
|
31
|
+
const file = (ext) => join(dir, `${id}${ext}`);
|
|
32
|
+
return { request: file(".json"), pid: file(".pid"), exit: file(".exit"), log: file(".log") };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Writes the request of a new job and returns its files. */
|
|
36
|
+
export function createJob(dir, id, request) {
|
|
37
|
+
mkdirSync(dir, { recursive: true });
|
|
38
|
+
const files = jobFiles(dir, id);
|
|
39
|
+
writeJsonAtomic(files.request, request);
|
|
40
|
+
return files;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** { request, pid, exit } of a job, each null when its file is missing or broken; request null means no such job. */
|
|
44
|
+
export function readJob(dir, id) {
|
|
45
|
+
const files = jobFiles(dir, id);
|
|
46
|
+
return { files, request: readJson(files.request), pid: readJson(files.pid), exit: readJson(files.exit) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** True while the process with this pid is the one that wrote startedAt (ms) when it began: a pid that a later
|
|
50
|
+
* process was given counts as gone. ages: pid to seconds run, as `ps` reports it. */
|
|
51
|
+
export function sameProcess(pid, startedAt, { isAlive = alive, ages = (pids) => parseElapsed(probeProcessAges(pids)) } = {}) {
|
|
52
|
+
if (!Number.isInteger(pid) || pid <= 1 || !isAlive(pid)) return false;
|
|
53
|
+
if (!Number.isFinite(startedAt)) return true;
|
|
54
|
+
const seconds = ages([pid]).get(pid);
|
|
55
|
+
// ps counts whole seconds, and the runner writes its start a moment after it began.
|
|
56
|
+
return seconds === undefined || Math.abs(Date.now() - seconds * 1000 - startedAt) <= SAME_START_MS + 1000;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Whether a job's runner may still be working: it has no exit file and its runner is alive, or it is younger than
|
|
60
|
+
* START_GRACE_MS and its runner has not written a pid file yet. */
|
|
61
|
+
export function runnerActive(job, now = Date.now(), check = sameProcess) {
|
|
62
|
+
if (job.exit) return false;
|
|
63
|
+
if (job.pid) return check(job.pid.runnerPid, job.pid.runnerStartedAt);
|
|
64
|
+
return now - (Number(job.request?.createdAt) || 0) < START_GRACE_MS;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The ids that have at least one job file in the folder. */
|
|
68
|
+
function jobIds(dir) {
|
|
69
|
+
let names = [];
|
|
70
|
+
try { names = readdirSync(dir); } catch { return new Set(); }
|
|
71
|
+
const ids = new Set();
|
|
72
|
+
for (const name of names) {
|
|
73
|
+
const id = name.slice(0, 24);
|
|
74
|
+
if (JOB_ID_RE.test(id) && EXTENSIONS.includes(name.slice(24))) ids.add(id);
|
|
75
|
+
}
|
|
76
|
+
return ids;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The ids of the jobs whose runner may still be working (see runnerActive). */
|
|
80
|
+
export function activeJobs(dir, { now = Date.now(), check = sameProcess } = {}) {
|
|
81
|
+
return [...jobIds(dir)].filter((id) => {
|
|
82
|
+
const job = readJob(dir, id);
|
|
83
|
+
return job.request && runnerActive(job, now, check);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Removes the jobs older than keepDays whose runner is done, then the oldest done ones beyond keepJobs, and any
|
|
88
|
+
* stray file of a job whose request is gone once it is older than keepDays. Returns the ids removed. */
|
|
89
|
+
export function pruneJobs(dir, { now = Date.now(), keepDays = KEEP_DAYS, keepJobs = KEEP_JOBS, check = sameProcess } = {}) {
|
|
90
|
+
const ids = jobIds(dir);
|
|
91
|
+
const done = [];
|
|
92
|
+
const removed = [];
|
|
93
|
+
const drop = (id) => {
|
|
94
|
+
for (const file of Object.values(jobFiles(dir, id))) rmSync(file, { force: true });
|
|
95
|
+
removed.push(id);
|
|
96
|
+
};
|
|
97
|
+
for (const id of ids) {
|
|
98
|
+
const job = readJob(dir, id);
|
|
99
|
+
if (!job.request) {
|
|
100
|
+
const files = Object.values(job.files);
|
|
101
|
+
const newest = Math.max(0, ...files.map((file) => { try { return statSync(file).mtimeMs; } catch { return 0; } }));
|
|
102
|
+
if (now - newest > keepDays * DAY) drop(id);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (runnerActive(job, now, check)) continue;
|
|
106
|
+
const at = Number(job.request.createdAt) || 0;
|
|
107
|
+
if (now - at > keepDays * DAY) drop(id);
|
|
108
|
+
else done.push({ id, at });
|
|
109
|
+
}
|
|
110
|
+
done.sort((a, b) => a.at - b.at);
|
|
111
|
+
for (const { id } of done.slice(0, Math.max(0, done.length - keepJobs))) drop(id);
|
|
112
|
+
return removed;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The last count lines of a job log, each cut to MAX_LINE characters. Only the end of the file is read. Anything
|
|
116
|
+
* but a regular file in place of the log is refused: a symlink is never followed, and a FIFO is opened without
|
|
117
|
+
* waiting for a writer. */
|
|
118
|
+
export function tailLines(file, count) {
|
|
119
|
+
if (count <= 0) return [];
|
|
120
|
+
let fd;
|
|
121
|
+
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); } catch { return []; }
|
|
122
|
+
try {
|
|
123
|
+
const stat = fstatSync(fd);
|
|
124
|
+
if (!stat.isFile()) return [];
|
|
125
|
+
const size = stat.size;
|
|
126
|
+
const length = Math.min(size, TAIL_BYTES);
|
|
127
|
+
const buffer = Buffer.alloc(length);
|
|
128
|
+
readSync(fd, buffer, 0, length, size - length);
|
|
129
|
+
const lines = buffer.toString("utf8").split("\n");
|
|
130
|
+
// The first line may be cut by the read window, and a final newline leaves an empty last item.
|
|
131
|
+
if (length < size) lines.shift();
|
|
132
|
+
if (lines.at(-1) === "") lines.pop();
|
|
133
|
+
return lines.slice(-count).map((line) => (line.length > MAX_LINE ? `${line.slice(0, MAX_LINE)} [cut]` : line));
|
|
134
|
+
} finally {
|
|
135
|
+
closeSync(fd);
|
|
136
|
+
}
|
|
137
|
+
}
|
package/mcp/runner.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The runner of one job the MCP server started: `node runner.mjs <jobs folder> <job id>`. The server starts it
|
|
3
|
+
// detached, so it leads a process group and a session of its own and keeps going after the server exits or is
|
|
4
|
+
// stopped with Ctrl-C. It starts `dotime-queue run` with the words the server stored in the job's request, sends that
|
|
5
|
+
// command's output to the job log, writes the pids to the pid file, and once the command has ended writes its exit
|
|
6
|
+
// code to the exit file. The queue's own rules apply unchanged: the queue process waits for a slot, runs the job in
|
|
7
|
+
// a process group of its own and journals it. INT, TERM and HUP sent to the runner are passed on to it.
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { closeSync, openSync } from "node:fs";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { writeJsonAtomic } from "../queue/files.mjs";
|
|
12
|
+
import { isMainModule } from "../queue/settings.mjs";
|
|
13
|
+
import { jobFiles, readJob } from "./jobs.mjs";
|
|
14
|
+
|
|
15
|
+
const CLI = fileURLToPath(new URL("../queue/cli.mjs", import.meta.url));
|
|
16
|
+
const FORWARDED = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
17
|
+
// When this process started (wall clock, ms), to tell it from a later process that is given the same pid.
|
|
18
|
+
const BORN = Math.round(Date.now() - process.uptime() * 1000);
|
|
19
|
+
|
|
20
|
+
/** Runs the job and resolves with the runner's exit code: 0 once the exit file is written, 2 when the job cannot be
|
|
21
|
+
* read. Nothing in here ends the process. */
|
|
22
|
+
export function runJobFromRequest(dir, id) {
|
|
23
|
+
let files;
|
|
24
|
+
try { files = jobFiles(dir, id); } catch { return Promise.resolve(2); }
|
|
25
|
+
const { request } = readJob(dir, id);
|
|
26
|
+
if (!request || !Array.isArray(request.queueArgs) || typeof request.cwd !== "string") return Promise.resolve(2);
|
|
27
|
+
const finish = (fields) => writeJsonAtomic(files.exit, { ...fields, endedAt: Date.now() });
|
|
28
|
+
let log;
|
|
29
|
+
try {
|
|
30
|
+
log = openSync(files.log, "a");
|
|
31
|
+
} catch (e) {
|
|
32
|
+
finish({ exit: null, signal: null, error: `the job log could not be opened (${e.code ?? e.message})` });
|
|
33
|
+
return Promise.resolve(0);
|
|
34
|
+
}
|
|
35
|
+
const startedAt = Date.now();
|
|
36
|
+
let child;
|
|
37
|
+
try {
|
|
38
|
+
child = spawn(process.execPath, [CLI, ...request.queueArgs], { cwd: request.cwd, stdio: ["ignore", log, log], env: process.env });
|
|
39
|
+
} catch (e) {
|
|
40
|
+
finish({ exit: null, signal: null, error: `the queue could not be started (${e.message})` });
|
|
41
|
+
return Promise.resolve(0);
|
|
42
|
+
} finally {
|
|
43
|
+
closeSync(log);
|
|
44
|
+
}
|
|
45
|
+
const forward = FORWARDED.map((signal) => [signal, () => { try { child.kill(signal); } catch { /* gone */ } }]);
|
|
46
|
+
for (const [signal, handler] of forward) process.on(signal, handler);
|
|
47
|
+
try {
|
|
48
|
+
if (child.pid) writeJsonAtomic(files.pid, { runnerPid: process.pid, runnerStartedAt: BORN, queuePid: child.pid, startedAt });
|
|
49
|
+
} catch { /* the job still runs; queue_job reports it as lost once START_GRACE_MS has passed */ }
|
|
50
|
+
return new Promise((resolve) => {
|
|
51
|
+
let ended = false;
|
|
52
|
+
const end = (fields) => {
|
|
53
|
+
if (ended) return;
|
|
54
|
+
ended = true;
|
|
55
|
+
for (const [signal, handler] of forward) process.off(signal, handler);
|
|
56
|
+
finish(fields);
|
|
57
|
+
resolve(0);
|
|
58
|
+
};
|
|
59
|
+
child.on("error", (e) => end({ exit: null, signal: null, error: `the queue could not be started (${e.message})` }));
|
|
60
|
+
child.on("exit", (code, signal) => end({ exit: code, signal }));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (isMainModule(import.meta.url)) process.exit(await runJobFromRequest(process.argv[2], process.argv[3]));
|