@soleone/factory 0.0.0-stage → 0.1.0
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 +21 -0
- package/README.md +62 -2
- package/dist/cli.mjs +650 -0
- package/package.json +52 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Soleone
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# factory
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Runs your coding agents overnight. Say `factory start` before bed: it takes the candidates [workwork](https://github.com/Soleone/workwork) finds across your repos, cheapest human attention first, and hands them one at a time to headless `claude` and `pi`, each in a fresh git worktree, until their usage limits run out or the clock says stop. In the morning `factory report` lists what each agent did, with the PR or issue it opened.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install -g @soleone/factory
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Needs node 22+, git, [workwork](https://github.com/Soleone/workwork) 0.2+ (for `--json`), and whichever of `claude` and `pi` you use, logged in.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
factory start [--until 08:00] [--windows 2] [--agents claude,pi] [--levels auto,skim,decide] [--max-items N]
|
|
13
|
+
factory run [same flags] # the same loop in the foreground; start = run, detached
|
|
14
|
+
factory status # running or not, what each worker is doing, counts
|
|
15
|
+
factory stop # stops the loop and its agents; unfinished items go back in the queue
|
|
16
|
+
factory report [--all] # the last run (or every run): outcome, agent, item, PR/issue link or log
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Also `--timeout MIN` (default 60) per item, and `--claude-model` / `--pi-model`, passed through as `--model`. There is no config file.
|
|
20
|
+
|
|
21
|
+
## How a night goes
|
|
22
|
+
|
|
23
|
+
One worker per agent, all taking from one queue that a single process hands out, so two agents never get the same item. The queue is `workwork --all --json`, minus what the ledger says is done, auto before skim before decide, taking turns between repos so one repo doesn't take the night. When it runs dry it asks workwork again; when workwork has nothing new, the run ends.
|
|
24
|
+
|
|
25
|
+
For each item: `git fetch`, `git worktree add --detach` at `origin/HEAD` (your checkout is never touched), `workwork prompt <id>` on the agent's stdin, output to `agent.log`. The agent runs in its own process group and is killed with everything it started after `--timeout`. A worktree with commits that aren't on a remote, or with uncommitted changes, is kept and the report says where; otherwise it's removed.
|
|
26
|
+
|
|
27
|
+
A failed or timed-out item gets one more try, by the other agent. After two failures it's skipped for good.
|
|
28
|
+
|
|
29
|
+
## Limits and windows
|
|
30
|
+
|
|
31
|
+
When an agent hits its usage limit, the item goes back to the front of the queue and that worker sleeps until the reset time in the message, plus 2 minutes (30 minutes when the message doesn't say). After its `--windows`th limit (default 2: the current 5-hour window and the next one) the worker stops. The run ends when every worker has stopped, `--until` has passed (nothing new starts after it), or `--max-items` have started.
|
|
32
|
+
|
|
33
|
+
The limit messages are the ones the tools really print: Claude Code's `You've hit your session limit · resets 3pm (Europe/Berlin)` (and weekly, Opus, Sonnet, spend limits, with or without a date) and the older `Claude AI usage limit reached|<epoch>`; pi's `You have hit your ChatGPT usage limit (plus plan). Try again in ~42 min.` and the raw `usage_limit_reached` / `resets_in_seconds`. Three failures in a row that each took under a minute count as a limit too, so a broken login or a message nobody has seen yet can't burn through the queue.
|
|
34
|
+
|
|
35
|
+
`--until` and resets without a time zone use the machine's local time; set `TZ` if that isn't yours.
|
|
36
|
+
|
|
37
|
+
## State
|
|
38
|
+
|
|
39
|
+
In `$DATA/apps/factory/` when `DATA` is set, otherwise `~/.local/share/factory/`:
|
|
40
|
+
|
|
41
|
+
- `ledger.jsonl`: one line per finished attempt, `{run, id, repo, play, level, agent, start, end, outcome, url?, log, worktree?, error?}`; outcome is `done`, `failed`, `limited` or `timeout`
|
|
42
|
+
- `runs/<run>/<item>/`: `prompt.md` and `agent.log`
|
|
43
|
+
- `worktrees/<run>/<item>/`: kept worktrees
|
|
44
|
+
- `factory.pid`, `factory.log`: the running loop and its output
|
|
45
|
+
- `status.json`: rewritten on every change, read by `factory status`
|
|
46
|
+
|
|
47
|
+
An item that was running when the loop stopped has no ledger line, so the next run takes it again.
|
|
48
|
+
|
|
49
|
+
## Why not an LLM loop
|
|
50
|
+
|
|
51
|
+
This replaces an assistant that ran the night from inside chat turns. It stopped whenever a turn ended, built its own queue, guards and "authorization" out of prompts, then got blocked by them, and rewrote its own instructions after every failure. The tasks themselves went fine. So here the loop is plain code, and a model only ever does one task, as a child process that exits. Nothing a model says decides what runs next, who gets it, or whether to keep going.
|
|
52
|
+
|
|
53
|
+
## Development
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
pnpm install
|
|
57
|
+
vp check && vp test run && pnpm exec knip
|
|
58
|
+
pnpm release # tag, changelog, npm publish
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execFile, spawn } from "node:child_process";
|
|
3
|
+
import { appendFileSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { basename, join } from "node:path";
|
|
5
|
+
import { parseArgs, promisify } from "node:util";
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
//#region src/agents.ts
|
|
8
|
+
const LIMITS = {
|
|
9
|
+
claude: /usage limit|You've hit your [\w' ]*limit|You've reached your [\w ]*limit|rate_limit_error/i,
|
|
10
|
+
pi: /usage limit|usage_limit_reached|usage_not_included|rate_limit_exceeded/i
|
|
11
|
+
};
|
|
12
|
+
const AGENT_NAMES = Object.keys(LIMITS);
|
|
13
|
+
function agent(name, model) {
|
|
14
|
+
const pick = model ? ["--model", model] : [];
|
|
15
|
+
if (name === "claude") return {
|
|
16
|
+
name,
|
|
17
|
+
command: "claude",
|
|
18
|
+
args: [
|
|
19
|
+
"-p",
|
|
20
|
+
"--permission-mode",
|
|
21
|
+
"bypassPermissions",
|
|
22
|
+
"--output-format",
|
|
23
|
+
"json",
|
|
24
|
+
...pick
|
|
25
|
+
],
|
|
26
|
+
limit: LIMITS.claude
|
|
27
|
+
};
|
|
28
|
+
if (name === "pi") return {
|
|
29
|
+
name,
|
|
30
|
+
command: "pi",
|
|
31
|
+
args: ["-p", ...pick],
|
|
32
|
+
limit: LIMITS.pi
|
|
33
|
+
};
|
|
34
|
+
throw new Error(`unknown agent ${name} (known: ${AGENT_NAMES.join(", ")})`);
|
|
35
|
+
}
|
|
36
|
+
/** Only the end of the log decides: the agent's own work earlier on may mention limits too. */
|
|
37
|
+
function classify(agent, code, log) {
|
|
38
|
+
const tail = log.slice(-8e3);
|
|
39
|
+
if (code === 0 && !/"is_error":\s*true/.test(tail)) return "done";
|
|
40
|
+
return agent.limit.test(tail) ? "limited" : "failed";
|
|
41
|
+
}
|
|
42
|
+
const MONTHS = [
|
|
43
|
+
"Jan",
|
|
44
|
+
"Feb",
|
|
45
|
+
"Mar",
|
|
46
|
+
"Apr",
|
|
47
|
+
"May",
|
|
48
|
+
"Jun",
|
|
49
|
+
"Jul",
|
|
50
|
+
"Aug",
|
|
51
|
+
"Sep",
|
|
52
|
+
"Oct",
|
|
53
|
+
"Nov",
|
|
54
|
+
"Dec"
|
|
55
|
+
];
|
|
56
|
+
const UNIT_MS = {
|
|
57
|
+
s: 1e3,
|
|
58
|
+
m: 6e4,
|
|
59
|
+
h: 36e5
|
|
60
|
+
};
|
|
61
|
+
/** When a limit message says the limit resets, in epoch ms; undefined when it doesn't say. */
|
|
62
|
+
function resetTime(text, now) {
|
|
63
|
+
const epoch = /limit reached\|(\d{10})|"resets_at"\s*:\s*(\d{10})/.exec(text);
|
|
64
|
+
if (epoch) return Number(epoch[1] ?? epoch[2]) * 1e3;
|
|
65
|
+
const seconds = /"?resets_in_seconds"?\s*[:=]\s*(\d+)/.exec(text);
|
|
66
|
+
if (seconds) return now + Number(seconds[1]) * 1e3;
|
|
67
|
+
const after = /try again in ~?(\d+)\s*(s|m|h)/i.exec(text);
|
|
68
|
+
if (after) return now + Number(after[1]) * UNIT_MS[after[2].toLowerCase()];
|
|
69
|
+
const clock = /resets (?:at )?(?:([A-Z][a-z]{2}) (\d{1,2}),? )?(\d{1,2})(?::(\d{2}))?\s*([ap]m)(?: \(([^)]+)\))?/i.exec(text);
|
|
70
|
+
if (!clock) return void 0;
|
|
71
|
+
const [, month, day, hour, minute, half, zone] = clock;
|
|
72
|
+
const hour24 = Number(hour) % 12 + (half.toLowerCase() === "pm" ? 12 : 0);
|
|
73
|
+
try {
|
|
74
|
+
return nextWallClock(now, zone, hour24, Number(minute ?? 0), month, day);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error instanceof RangeError) return void 0;
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** The first instant after now when the clock in zone (default: local) shows hour:minute, on month/day if given. */
|
|
81
|
+
function nextWallClock(now, zone, hour, minute, month, day) {
|
|
82
|
+
const today = wallClock(now, zone);
|
|
83
|
+
const monthIndex = month ? MONTHS.indexOf(month.slice(0, 3)) : today.month;
|
|
84
|
+
const date = day ? Number(day) : today.day;
|
|
85
|
+
for (let step = 0; step < 2; step++) {
|
|
86
|
+
const target = day ? instant(zone, today.year + step, monthIndex, date, hour, minute) : instant(zone, today.year, monthIndex, date + step, hour, minute);
|
|
87
|
+
if (target > now) return target;
|
|
88
|
+
}
|
|
89
|
+
throw new RangeError("no future reset time");
|
|
90
|
+
}
|
|
91
|
+
function instant(zone, ...fields) {
|
|
92
|
+
const guess = Date.UTC(...fields);
|
|
93
|
+
return guess - offset(guess - offset(guess, zone), zone);
|
|
94
|
+
}
|
|
95
|
+
function offset(time, zone) {
|
|
96
|
+
const clock = wallClock(time, zone);
|
|
97
|
+
const wall = Date.UTC(clock.year, clock.month, clock.day, clock.hour, clock.minute);
|
|
98
|
+
return Math.round((wall - time) / 6e4) * 6e4;
|
|
99
|
+
}
|
|
100
|
+
function wallClock(time, zone) {
|
|
101
|
+
const parts = Object.fromEntries(new Intl.DateTimeFormat("en-US", {
|
|
102
|
+
timeZone: zone,
|
|
103
|
+
hourCycle: "h23",
|
|
104
|
+
year: "numeric",
|
|
105
|
+
month: "numeric",
|
|
106
|
+
day: "numeric",
|
|
107
|
+
hour: "numeric",
|
|
108
|
+
minute: "numeric"
|
|
109
|
+
}).formatToParts(new Date(time)).map((part) => [part.type, Number(part.value)]));
|
|
110
|
+
return {
|
|
111
|
+
year: parts.year,
|
|
112
|
+
month: parts.month - 1,
|
|
113
|
+
day: parts.day,
|
|
114
|
+
hour: parts.hour,
|
|
115
|
+
minute: parts.minute
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/state.ts
|
|
120
|
+
const STATE = process.env.DATA ? join(process.env.DATA, "apps", "factory") : join(homedir(), ".local", "share", "factory");
|
|
121
|
+
const key = (item) => `${item.repo} ${item.id}`;
|
|
122
|
+
function readLedger(state) {
|
|
123
|
+
const path = join(state, "ledger.jsonl");
|
|
124
|
+
if (!existsSync(path)) return [];
|
|
125
|
+
return readFileSync(path, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line));
|
|
126
|
+
}
|
|
127
|
+
function appendLedger(state, entry) {
|
|
128
|
+
appendFileSync(join(state, "ledger.jsonl"), `${JSON.stringify(entry)}\n`);
|
|
129
|
+
}
|
|
130
|
+
/** What the ledger says about each item: finished for good, or which agents already failed it. */
|
|
131
|
+
function replay(ledger) {
|
|
132
|
+
const finished = /* @__PURE__ */ new Set();
|
|
133
|
+
const failedBy = /* @__PURE__ */ new Map();
|
|
134
|
+
for (const entry of ledger) {
|
|
135
|
+
if (entry.outcome === "done") finished.add(key(entry));
|
|
136
|
+
if (entry.outcome !== "failed" && entry.outcome !== "timeout") continue;
|
|
137
|
+
const agents = [...failedBy.get(key(entry)) ?? [], entry.agent];
|
|
138
|
+
failedBy.set(key(entry), agents);
|
|
139
|
+
if (agents.length >= 2) finished.add(key(entry));
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
finished,
|
|
143
|
+
failedBy
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
const LEVELS = [
|
|
147
|
+
"auto",
|
|
148
|
+
"skim",
|
|
149
|
+
"decide"
|
|
150
|
+
];
|
|
151
|
+
/** Cheapest attention first, and within a level one item per repo in turn so no repo takes the night. */
|
|
152
|
+
function order(items, levels) {
|
|
153
|
+
return LEVELS.filter((level) => levels.includes(level)).flatMap((level) => {
|
|
154
|
+
const byRepo = /* @__PURE__ */ new Map();
|
|
155
|
+
for (const item of items.filter((item) => item.level === level)) byRepo.set(item.repo, [...byRepo.get(item.repo) ?? [], item]);
|
|
156
|
+
const queues = [...byRepo.values()];
|
|
157
|
+
const turns = [];
|
|
158
|
+
for (let round = 0; queues.some((queue) => queue.length > round); round++) for (const queue of queues) if (queue[round]) turns.push(queue[round]);
|
|
159
|
+
return turns;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/loop.ts
|
|
164
|
+
const MINUTE = 6e4;
|
|
165
|
+
const URL$1 = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/(?:pull|issues)\/\d+/g;
|
|
166
|
+
/** Runs workers until each has stopped; returns the run id. */
|
|
167
|
+
async function run(options) {
|
|
168
|
+
const id = (/* @__PURE__ */ new Date()).toISOString().slice(0, 19).replaceAll(":", "");
|
|
169
|
+
const runDir = join(options.state, "runs", id);
|
|
170
|
+
mkdirSync(runDir, { recursive: true });
|
|
171
|
+
const queue = [];
|
|
172
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
173
|
+
const children = /* @__PURE__ */ new Set();
|
|
174
|
+
const wakers = /* @__PURE__ */ new Set();
|
|
175
|
+
const counts = {
|
|
176
|
+
done: 0,
|
|
177
|
+
failed: 0,
|
|
178
|
+
limited: 0,
|
|
179
|
+
timeout: 0
|
|
180
|
+
};
|
|
181
|
+
const workers = options.agents.map((agent) => ({
|
|
182
|
+
agent: agent.name,
|
|
183
|
+
state: "idle",
|
|
184
|
+
since: (/* @__PURE__ */ new Date()).toISOString(),
|
|
185
|
+
limits: 0
|
|
186
|
+
}));
|
|
187
|
+
let started = 0;
|
|
188
|
+
let stopping = false;
|
|
189
|
+
function writeStatus() {
|
|
190
|
+
const until = options.until && new Date(options.until).toISOString();
|
|
191
|
+
const status = {
|
|
192
|
+
pid: process.pid,
|
|
193
|
+
run: id,
|
|
194
|
+
until,
|
|
195
|
+
workers,
|
|
196
|
+
counts,
|
|
197
|
+
queued: queue.length
|
|
198
|
+
};
|
|
199
|
+
writeFileSync(join(options.state, "status.json"), `${JSON.stringify(status, null, 2)}\n`);
|
|
200
|
+
}
|
|
201
|
+
function set(worker, change) {
|
|
202
|
+
Object.assign(worker, {
|
|
203
|
+
item: void 0,
|
|
204
|
+
until: void 0
|
|
205
|
+
}, change);
|
|
206
|
+
worker.since = (/* @__PURE__ */ new Date()).toISOString();
|
|
207
|
+
writeStatus();
|
|
208
|
+
}
|
|
209
|
+
function stop() {
|
|
210
|
+
stopping = true;
|
|
211
|
+
for (const child of children) killGroup(child);
|
|
212
|
+
for (const wake of wakers) wake();
|
|
213
|
+
}
|
|
214
|
+
function ended() {
|
|
215
|
+
if (stopping) return "stopped";
|
|
216
|
+
if (options.until && Date.now() >= options.until) return "--until passed";
|
|
217
|
+
if (options.maxItems && started >= options.maxItems) return "--max-items reached";
|
|
218
|
+
}
|
|
219
|
+
function nap(ms) {
|
|
220
|
+
if (options.sleep) return options.sleep(ms);
|
|
221
|
+
return new Promise((resolve) => {
|
|
222
|
+
const wake = () => {
|
|
223
|
+
clearTimeout(timer);
|
|
224
|
+
wakers.delete(wake);
|
|
225
|
+
resolve();
|
|
226
|
+
};
|
|
227
|
+
const timer = setTimeout(wake, ms);
|
|
228
|
+
wakers.add(wake);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
function take(agent) {
|
|
232
|
+
const { failedBy } = replay(readLedger(options.state));
|
|
233
|
+
const index = queue.findIndex((item) => !failedBy.get(key(item))?.includes(agent));
|
|
234
|
+
return index < 0 ? void 0 : queue.splice(index, 1)[0];
|
|
235
|
+
}
|
|
236
|
+
async function refill() {
|
|
237
|
+
const candidates = JSON.parse(await output(options.workwork, ["--all", "--json"], process.cwd()));
|
|
238
|
+
const { finished, failedBy } = replay(readLedger(options.state));
|
|
239
|
+
const agents = workers.filter((worker) => worker.state !== "stopped").map((worker) => worker.agent);
|
|
240
|
+
const known = /* @__PURE__ */ new Set([...queue.map(key), ...claimed]);
|
|
241
|
+
const fresh = candidates.filter((item) => !finished.has(key(item)) && !known.has(key(item)) && agents.some((agent) => !failedBy.get(key(item))?.includes(agent)));
|
|
242
|
+
queue.push(...order(fresh, options.levels));
|
|
243
|
+
writeStatus();
|
|
244
|
+
}
|
|
245
|
+
async function work(agent, worker) {
|
|
246
|
+
let quickFailures = 0;
|
|
247
|
+
while (!ended()) {
|
|
248
|
+
const item = take(agent.name) ?? (await refill(), take(agent.name));
|
|
249
|
+
if (!item) return set(worker, {
|
|
250
|
+
state: "stopped",
|
|
251
|
+
reason: "nothing left to do"
|
|
252
|
+
});
|
|
253
|
+
started++;
|
|
254
|
+
claimed.add(key(item));
|
|
255
|
+
set(worker, {
|
|
256
|
+
state: "working",
|
|
257
|
+
item: `${item.label} ${item.id}`
|
|
258
|
+
});
|
|
259
|
+
log(`${agent.name} starts ${item.label} ${item.id}`);
|
|
260
|
+
const entry = await attempt(agent, item);
|
|
261
|
+
claimed.delete(key(item));
|
|
262
|
+
if (!entry) break;
|
|
263
|
+
appendLedger(options.state, entry);
|
|
264
|
+
counts[entry.outcome]++;
|
|
265
|
+
log(`${agent.name} ${entry.outcome} ${item.label} ${item.id} ${entry.url ?? entry.error ?? ""}`);
|
|
266
|
+
const seconds = (Date.parse(entry.end) - Date.parse(entry.start)) / 1e3;
|
|
267
|
+
quickFailures = entry.outcome === "failed" && !entry.error && seconds < 60 ? quickFailures + 1 : 0;
|
|
268
|
+
if (entry.outcome === "limited") {
|
|
269
|
+
started--;
|
|
270
|
+
queue.unshift(item);
|
|
271
|
+
}
|
|
272
|
+
if (entry.outcome !== "limited" && quickFailures < 3) {
|
|
273
|
+
set(worker, { state: "idle" });
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
quickFailures = 0;
|
|
277
|
+
worker.limits++;
|
|
278
|
+
if (worker.limits >= options.windows) return set(worker, {
|
|
279
|
+
state: "stopped",
|
|
280
|
+
reason: `limit ${worker.limits} of ${options.windows}`
|
|
281
|
+
});
|
|
282
|
+
const reset = entry.outcome === "limited" ? resetTime(readFileSync(entry.log, "utf8"), Date.now()) : void 0;
|
|
283
|
+
const wake = Math.min(reset === void 0 ? Date.now() + 30 * MINUTE : reset + 2 * MINUTE, options.until ?? Infinity);
|
|
284
|
+
set(worker, {
|
|
285
|
+
state: "sleeping",
|
|
286
|
+
until: new Date(wake).toISOString()
|
|
287
|
+
});
|
|
288
|
+
log(`${agent.name} sleeps until ${new Date(wake).toISOString()}`);
|
|
289
|
+
await nap(wake - Date.now());
|
|
290
|
+
}
|
|
291
|
+
set(worker, {
|
|
292
|
+
state: "stopped",
|
|
293
|
+
reason: ended()
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
/** One attempt at an item in a fresh worktree; undefined when the run was stopped meanwhile. */
|
|
297
|
+
async function attempt(agent, item) {
|
|
298
|
+
const slug = `${item.label}-${item.id}`.replace(/[^\w.-]+/g, "-");
|
|
299
|
+
const dir = join(runDir, slug);
|
|
300
|
+
mkdirSync(dir, { recursive: true });
|
|
301
|
+
const logPath = join(dir, "agent.log");
|
|
302
|
+
const worktree = join(options.state, "worktrees", id, slug);
|
|
303
|
+
const start = (/* @__PURE__ */ new Date()).toISOString();
|
|
304
|
+
const entry = (outcome, rest = {}) => ({
|
|
305
|
+
run: id,
|
|
306
|
+
id: item.id,
|
|
307
|
+
repo: item.repo,
|
|
308
|
+
play: item.play,
|
|
309
|
+
level: item.level,
|
|
310
|
+
agent: agent.name,
|
|
311
|
+
start,
|
|
312
|
+
end: (/* @__PURE__ */ new Date()).toISOString(),
|
|
313
|
+
outcome,
|
|
314
|
+
log: logPath,
|
|
315
|
+
...rest
|
|
316
|
+
});
|
|
317
|
+
let prompt;
|
|
318
|
+
try {
|
|
319
|
+
await output("git", [
|
|
320
|
+
"fetch",
|
|
321
|
+
"--quiet",
|
|
322
|
+
"origin"
|
|
323
|
+
], item.repo);
|
|
324
|
+
await output("git", [
|
|
325
|
+
"worktree",
|
|
326
|
+
"add",
|
|
327
|
+
"--detach",
|
|
328
|
+
worktree,
|
|
329
|
+
(await output("git", [
|
|
330
|
+
"symbolic-ref",
|
|
331
|
+
"--short",
|
|
332
|
+
"refs/remotes/origin/HEAD"
|
|
333
|
+
], item.repo)).trim()
|
|
334
|
+
], item.repo);
|
|
335
|
+
prompt = await output(options.workwork, ["prompt", item.id], item.repo);
|
|
336
|
+
} catch (error) {
|
|
337
|
+
const kept = await settle(worktree, item.repo);
|
|
338
|
+
return entry("failed", {
|
|
339
|
+
error: error.message.trim(),
|
|
340
|
+
worktree: kept ? worktree : void 0
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
writeFileSync(join(dir, "prompt.md"), prompt);
|
|
344
|
+
const { code, timedOut } = await spawnAgent(agent, prompt, worktree, logPath);
|
|
345
|
+
const kept = await settle(worktree, item.repo);
|
|
346
|
+
if (stopping) return void 0;
|
|
347
|
+
const text = readFileSync(logPath, "utf8");
|
|
348
|
+
return entry(timedOut ? "timeout" : classify(agent, code, text), {
|
|
349
|
+
url: text.match(URL$1)?.at(-1),
|
|
350
|
+
worktree: kept ? worktree : void 0
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
function spawnAgent(agent, prompt, cwd, logPath) {
|
|
354
|
+
return new Promise((resolve) => {
|
|
355
|
+
const logFile = openSync(logPath, "w");
|
|
356
|
+
const child = spawn(agent.command, agent.args, {
|
|
357
|
+
cwd,
|
|
358
|
+
detached: true,
|
|
359
|
+
stdio: [
|
|
360
|
+
"pipe",
|
|
361
|
+
logFile,
|
|
362
|
+
logFile
|
|
363
|
+
]
|
|
364
|
+
});
|
|
365
|
+
closeSync(logFile);
|
|
366
|
+
children.add(child);
|
|
367
|
+
let timedOut = false;
|
|
368
|
+
const timer = setTimeout(() => {
|
|
369
|
+
timedOut = true;
|
|
370
|
+
killGroup(child);
|
|
371
|
+
}, options.timeoutMs);
|
|
372
|
+
const done = (code) => {
|
|
373
|
+
clearTimeout(timer);
|
|
374
|
+
children.delete(child);
|
|
375
|
+
resolve({
|
|
376
|
+
code,
|
|
377
|
+
timedOut
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
child.on("error", (error) => {
|
|
381
|
+
appendFileSync(logPath, `factory: could not start ${agent.command}: ${error.message}\n`);
|
|
382
|
+
done(127);
|
|
383
|
+
});
|
|
384
|
+
child.on("close", (code) => {
|
|
385
|
+
killGroup(child);
|
|
386
|
+
done(code ?? 1);
|
|
387
|
+
});
|
|
388
|
+
child.stdin.on("error", () => {});
|
|
389
|
+
child.stdin.end(prompt);
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
process.on("SIGTERM", stop);
|
|
393
|
+
process.on("SIGINT", stop);
|
|
394
|
+
writeStatus();
|
|
395
|
+
try {
|
|
396
|
+
await Promise.all(options.agents.map((agent, index) => work(agent, workers[index])));
|
|
397
|
+
} catch (error) {
|
|
398
|
+
stop();
|
|
399
|
+
throw error;
|
|
400
|
+
} finally {
|
|
401
|
+
process.off("SIGTERM", stop);
|
|
402
|
+
process.off("SIGINT", stop);
|
|
403
|
+
writeStatus();
|
|
404
|
+
}
|
|
405
|
+
return id;
|
|
406
|
+
}
|
|
407
|
+
/** Keeps a worktree holding unpushed commits or changes; removes it otherwise. Returns whether it was kept. */
|
|
408
|
+
async function settle(worktree, repo) {
|
|
409
|
+
if (!existsSync(worktree)) return false;
|
|
410
|
+
const unpushed = await output("git", [
|
|
411
|
+
"log",
|
|
412
|
+
"--oneline",
|
|
413
|
+
"HEAD",
|
|
414
|
+
"--not",
|
|
415
|
+
"--remotes"
|
|
416
|
+
], worktree);
|
|
417
|
+
const changes = await output("git", ["status", "--porcelain"], worktree);
|
|
418
|
+
if (unpushed.trim() || changes.trim()) return true;
|
|
419
|
+
await output("git", [
|
|
420
|
+
"worktree",
|
|
421
|
+
"remove",
|
|
422
|
+
"--force",
|
|
423
|
+
worktree
|
|
424
|
+
], repo);
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
function killGroup(child) {
|
|
428
|
+
const pid = child.pid;
|
|
429
|
+
if (pid === void 0) return;
|
|
430
|
+
for (const [signal, delay] of [["SIGTERM", 0], ["SIGKILL", 1e4]]) setTimeout(() => {
|
|
431
|
+
try {
|
|
432
|
+
process.kill(-pid, signal);
|
|
433
|
+
} catch (error) {
|
|
434
|
+
if (error.code !== "ESRCH") throw error;
|
|
435
|
+
}
|
|
436
|
+
}, delay).unref();
|
|
437
|
+
}
|
|
438
|
+
async function output(command, args, cwd) {
|
|
439
|
+
const { stdout, stderr } = await promisify(execFile)(command, args, {
|
|
440
|
+
cwd,
|
|
441
|
+
maxBuffer: 67108864
|
|
442
|
+
});
|
|
443
|
+
process.stderr.write(stderr);
|
|
444
|
+
return stdout;
|
|
445
|
+
}
|
|
446
|
+
function log(message) {
|
|
447
|
+
console.log(`${(/* @__PURE__ */ new Date()).toISOString()} ${message}`);
|
|
448
|
+
}
|
|
449
|
+
//#endregion
|
|
450
|
+
//#region src/cli.ts
|
|
451
|
+
const USAGE = `factory: works through workwork candidates with headless coding agents until the limits run out
|
|
452
|
+
|
|
453
|
+
factory start [options] the loop in the background; prints where the log is
|
|
454
|
+
factory run [options] the same loop in the foreground
|
|
455
|
+
factory status running or not, what each worker is doing, counts
|
|
456
|
+
factory stop stops the loop and its agents; unfinished items go back in the queue
|
|
457
|
+
factory report [--all] the last run (or every run): outcome, agent, item, PR/issue link or log
|
|
458
|
+
|
|
459
|
+
Options:
|
|
460
|
+
--until HH:MM start nothing new after this local time
|
|
461
|
+
--windows N usage-limit windows per agent before it stops (default 2)
|
|
462
|
+
--agents claude,pi one worker per agent (default claude,pi)
|
|
463
|
+
--levels auto,skim,decide which attention levels to take (default all)
|
|
464
|
+
--max-items N stop after starting N items
|
|
465
|
+
--timeout MIN minutes before an agent is killed (default 60)
|
|
466
|
+
--claude-model, --pi-model passed to the agent as --model
|
|
467
|
+
|
|
468
|
+
State: ${STATE}`;
|
|
469
|
+
const PID = join(STATE, "factory.pid");
|
|
470
|
+
const LOG = join(STATE, "factory.log");
|
|
471
|
+
async function main() {
|
|
472
|
+
const { values, positionals } = parseArgs({
|
|
473
|
+
allowPositionals: true,
|
|
474
|
+
options: {
|
|
475
|
+
until: { type: "string" },
|
|
476
|
+
windows: {
|
|
477
|
+
type: "string",
|
|
478
|
+
default: "2"
|
|
479
|
+
},
|
|
480
|
+
agents: {
|
|
481
|
+
type: "string",
|
|
482
|
+
default: "claude,pi"
|
|
483
|
+
},
|
|
484
|
+
levels: {
|
|
485
|
+
type: "string",
|
|
486
|
+
default: "auto,skim,decide"
|
|
487
|
+
},
|
|
488
|
+
"max-items": { type: "string" },
|
|
489
|
+
timeout: {
|
|
490
|
+
type: "string",
|
|
491
|
+
default: "60"
|
|
492
|
+
},
|
|
493
|
+
"claude-model": { type: "string" },
|
|
494
|
+
"pi-model": { type: "string" },
|
|
495
|
+
all: { type: "boolean" },
|
|
496
|
+
help: {
|
|
497
|
+
type: "boolean",
|
|
498
|
+
short: "h"
|
|
499
|
+
},
|
|
500
|
+
version: {
|
|
501
|
+
type: "boolean",
|
|
502
|
+
short: "v"
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
const [command] = positionals;
|
|
507
|
+
if (values.help || command === void 0) return console.log(USAGE);
|
|
508
|
+
if (values.version) return console.log(version());
|
|
509
|
+
mkdirSync(STATE, { recursive: true });
|
|
510
|
+
if (command === "status") return status();
|
|
511
|
+
if (command === "stop") return stop();
|
|
512
|
+
if (command === "report") return console.log(report(values.all ?? false));
|
|
513
|
+
if (command !== "start" && command !== "run") throw new Error(`unknown command ${command}\n\n${USAGE}`);
|
|
514
|
+
const models = {
|
|
515
|
+
claude: values["claude-model"],
|
|
516
|
+
pi: values["pi-model"]
|
|
517
|
+
};
|
|
518
|
+
const levels = values.levels.split(",");
|
|
519
|
+
for (const level of levels) if (![
|
|
520
|
+
"auto",
|
|
521
|
+
"skim",
|
|
522
|
+
"decide"
|
|
523
|
+
].includes(level)) throw new Error(`unknown level ${level}`);
|
|
524
|
+
const options = {
|
|
525
|
+
agents: values.agents.split(",").map((name) => agent(name, models[name])),
|
|
526
|
+
levels,
|
|
527
|
+
windows: count(values.windows, "--windows"),
|
|
528
|
+
until: values.until === void 0 ? void 0 : nextTime(values.until),
|
|
529
|
+
maxItems: values["max-items"] === void 0 ? void 0 : count(values["max-items"], "--max-items"),
|
|
530
|
+
timeoutMs: count(values.timeout, "--timeout") * 6e4,
|
|
531
|
+
state: STATE,
|
|
532
|
+
workwork: "workwork"
|
|
533
|
+
};
|
|
534
|
+
const pid = runningPid();
|
|
535
|
+
if (pid && pid !== process.pid) throw new Error(`already running (pid ${pid}); factory status, or factory stop`);
|
|
536
|
+
if (command === "start") return start();
|
|
537
|
+
writeFileSync(PID, `${process.pid}\n`);
|
|
538
|
+
try {
|
|
539
|
+
const id = await run(options);
|
|
540
|
+
console.log(report(false, id));
|
|
541
|
+
} finally {
|
|
542
|
+
rmSync(PID, { force: true });
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
/** `factory run` with the same arguments, outliving this shell and whoever started it. */
|
|
546
|
+
function start() {
|
|
547
|
+
const log = openSync(LOG, "a");
|
|
548
|
+
const child = spawn(process.execPath, [
|
|
549
|
+
process.argv[1],
|
|
550
|
+
"run",
|
|
551
|
+
...process.argv.slice(2).filter((arg) => arg !== "start")
|
|
552
|
+
], {
|
|
553
|
+
detached: true,
|
|
554
|
+
stdio: [
|
|
555
|
+
"ignore",
|
|
556
|
+
log,
|
|
557
|
+
log
|
|
558
|
+
]
|
|
559
|
+
});
|
|
560
|
+
child.unref();
|
|
561
|
+
writeFileSync(PID, `${child.pid}\n`);
|
|
562
|
+
console.log(`factory: started (pid ${child.pid}); log: ${LOG}`);
|
|
563
|
+
}
|
|
564
|
+
function status() {
|
|
565
|
+
const pid = runningPid();
|
|
566
|
+
const path = join(STATE, "status.json");
|
|
567
|
+
if (!existsSync(path)) return console.log(pid ? `running (pid ${pid}), starting up` : "not running, never ran");
|
|
568
|
+
const state = JSON.parse(readFileSync(path, "utf8"));
|
|
569
|
+
const until = state.until ? `, until ${clock(state.until)}` : "";
|
|
570
|
+
console.log(`${pid ? `running (pid ${pid})` : "not running"}, run ${state.run}${until}`);
|
|
571
|
+
for (const worker of state.workers) {
|
|
572
|
+
const what = worker.item ?? (worker.until ? `until ${clock(worker.until)}` : worker.reason ?? "");
|
|
573
|
+
console.log(` ${worker.agent.padEnd(7)} ${worker.state.padEnd(9)} ${what} (since ${clock(worker.since)}, limits ${worker.limits})`);
|
|
574
|
+
}
|
|
575
|
+
const { done, failed, limited, timeout } = state.counts;
|
|
576
|
+
console.log(`done ${done}, failed ${failed}, timeout ${timeout}, limited ${limited}, queued ${state.queued}`);
|
|
577
|
+
}
|
|
578
|
+
async function stop() {
|
|
579
|
+
const pid = runningPid();
|
|
580
|
+
if (!pid) return console.log("not running");
|
|
581
|
+
process.kill(pid, "SIGTERM");
|
|
582
|
+
for (let waited = 0; alive(pid); waited++) {
|
|
583
|
+
if (waited === 60) throw new Error(`pid ${pid} is still running after 30 seconds`);
|
|
584
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
585
|
+
}
|
|
586
|
+
rmSync(PID, { force: true });
|
|
587
|
+
console.log(`stopped (pid ${pid})`);
|
|
588
|
+
}
|
|
589
|
+
function report(all, run) {
|
|
590
|
+
const ledger = readLedger(STATE);
|
|
591
|
+
const last = run ?? ledger.at(-1)?.run;
|
|
592
|
+
const entries = all ? ledger : ledger.filter((entry) => entry.run === last);
|
|
593
|
+
if (entries.length === 0) return "nothing done yet";
|
|
594
|
+
const lines = [];
|
|
595
|
+
for (const [i, entry] of entries.entries()) {
|
|
596
|
+
if (entry.run !== entries[i - 1]?.run) lines.push(`run ${entry.run}`);
|
|
597
|
+
lines.push(` ${entry.outcome.padEnd(7)} ${entry.agent.padEnd(6)} ${basename(entry.repo)} ${entry.id}`);
|
|
598
|
+
lines.push(` ${detail(entry)}`);
|
|
599
|
+
}
|
|
600
|
+
return lines.join("\n");
|
|
601
|
+
}
|
|
602
|
+
function detail(entry) {
|
|
603
|
+
const parts = [entry.url ?? entry.log];
|
|
604
|
+
if (entry.error) parts.push(entry.error.split("\n")[0]);
|
|
605
|
+
if (entry.worktree) parts.push(`unpushed work kept in ${entry.worktree}`);
|
|
606
|
+
return parts.join("\n ");
|
|
607
|
+
}
|
|
608
|
+
function runningPid() {
|
|
609
|
+
if (!existsSync(PID)) return void 0;
|
|
610
|
+
const pid = Number(readFileSync(PID, "utf8"));
|
|
611
|
+
return alive(pid) ? pid : void 0;
|
|
612
|
+
}
|
|
613
|
+
function alive(pid) {
|
|
614
|
+
try {
|
|
615
|
+
process.kill(pid, 0);
|
|
616
|
+
return true;
|
|
617
|
+
} catch (error) {
|
|
618
|
+
if (error.code === "ESRCH") return false;
|
|
619
|
+
throw error;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
/** The next time the local clock shows HH:MM. */
|
|
623
|
+
function nextTime(text) {
|
|
624
|
+
const match = /^(\d{1,2}):(\d{2})$/.exec(text);
|
|
625
|
+
if (!match) throw new Error(`--until wants HH:MM, got ${text}`);
|
|
626
|
+
const time = /* @__PURE__ */ new Date();
|
|
627
|
+
time.setHours(Number(match[1]), Number(match[2]), 0, 0);
|
|
628
|
+
if (time.getTime() <= Date.now()) time.setDate(time.getDate() + 1);
|
|
629
|
+
return time.getTime();
|
|
630
|
+
}
|
|
631
|
+
function count(text, flag) {
|
|
632
|
+
const value = Number(text);
|
|
633
|
+
if (!Number.isInteger(value) || value < 1) throw new Error(`${flag} wants a whole number above 0, got ${text}`);
|
|
634
|
+
return value;
|
|
635
|
+
}
|
|
636
|
+
function clock(iso) {
|
|
637
|
+
return new Date(iso).toLocaleTimeString("en-GB", {
|
|
638
|
+
hour: "2-digit",
|
|
639
|
+
minute: "2-digit"
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
function version() {
|
|
643
|
+
return JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
644
|
+
}
|
|
645
|
+
main().catch((error) => {
|
|
646
|
+
console.error(`factory: ${error.message}`);
|
|
647
|
+
process.exitCode = 1;
|
|
648
|
+
});
|
|
649
|
+
//#endregion
|
|
650
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soleone/factory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Works through workwork candidates with headless coding agents overnight, until the usage limits run out.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"agents",
|
|
7
|
+
"claude-code",
|
|
8
|
+
"cli",
|
|
9
|
+
"coding-agent",
|
|
10
|
+
"overnight",
|
|
11
|
+
"pi"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/Soleone/factory#readme",
|
|
14
|
+
"bugs": "https://github.com/Soleone/factory/issues",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Soleone",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/Soleone/factory.git"
|
|
20
|
+
},
|
|
21
|
+
"bin": {
|
|
22
|
+
"factory": "dist/cli.mjs"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@release-it/conventional-changelog": "^12.0.2",
|
|
33
|
+
"@types/node": "^26.6.5",
|
|
34
|
+
"knip": "^6.45.1",
|
|
35
|
+
"release-it": "^21.1.2",
|
|
36
|
+
"typescript": "^7.0.2",
|
|
37
|
+
"vite-plus": "1.2.0"
|
|
38
|
+
},
|
|
39
|
+
"devEngines": {
|
|
40
|
+
"packageManager": {
|
|
41
|
+
"name": "pnpm",
|
|
42
|
+
"version": "12.21.0",
|
|
43
|
+
"onFail": "download"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=22"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "vp pack",
|
|
51
|
+
"test": "vp test run",
|
|
52
|
+
"check": "vp check",
|
|
53
|
+
"release": "release-it"
|
|
54
|
+
}
|
|
6
55
|
}
|