@soleone/factory 0.3.2 → 0.4.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/README.md +4 -2
- package/dist/cli.mjs +260 -87
- package/dist/web/assets/index-2UZaUTUZ.js +80 -0
- package/dist/web/assets/index-DcONJvgj.css +2 -0
- package/dist/web/index.html +2 -2
- package/package.json +1 -1
- package/dist/web/assets/index-BEi1vT_v.js +0 -80
- package/dist/web/assets/index-BrN7zEGq.css +0 -2
package/dist/cli.mjs
CHANGED
|
@@ -202,15 +202,30 @@ const TIERS = [
|
|
|
202
202
|
"builder",
|
|
203
203
|
"grunt"
|
|
204
204
|
];
|
|
205
|
-
/** The
|
|
205
|
+
/** The blueprints of factory's own jobs that write a goal's plan. */
|
|
206
206
|
const PLANNING = ["decompose", "replan"];
|
|
207
207
|
const presetId = (preset) => preset.id ?? [
|
|
208
208
|
preset.agent,
|
|
209
209
|
preset.model,
|
|
210
210
|
preset.effort
|
|
211
211
|
].filter(Boolean).join("/");
|
|
212
|
+
function upgradeItem(item) {
|
|
213
|
+
if (!("play" in item)) return item;
|
|
214
|
+
const { play, level, ...rest } = item;
|
|
215
|
+
return {
|
|
216
|
+
...rest,
|
|
217
|
+
blueprint: play,
|
|
218
|
+
attention: level === "auto" ? "none" : level
|
|
219
|
+
};
|
|
220
|
+
}
|
|
212
221
|
/** A candidate's identity across workwork runs. */
|
|
213
222
|
const itemKey = (item) => `${item.repo} ${item.id}`;
|
|
223
|
+
/** The pools whose 5-hour usage factory can read, so a reserve can be kept on them. */
|
|
224
|
+
const USAGE_POOLS = [
|
|
225
|
+
"claude",
|
|
226
|
+
"anthropic",
|
|
227
|
+
"openai-codex"
|
|
228
|
+
];
|
|
214
229
|
const onTrain = (car) => car !== void 0 && car.step !== "merged" && car.step !== "skipped";
|
|
215
230
|
/** The PR factory opened for a goal node, or else the PR of the job's latest attempt that linked one. */
|
|
216
231
|
const prUrl = (job) => job.notes.findLast((note) => note.kind === "factory" && note.url?.includes("/pull/"))?.url ?? job.attempts.findLast((attempt) => attempt.url?.includes("/pull/"))?.url;
|
|
@@ -287,8 +302,8 @@ function alive(pid) {
|
|
|
287
302
|
throw error;
|
|
288
303
|
}
|
|
289
304
|
}
|
|
290
|
-
const
|
|
291
|
-
"
|
|
305
|
+
const ATTENTION = [
|
|
306
|
+
"none",
|
|
292
307
|
"skim",
|
|
293
308
|
"decide"
|
|
294
309
|
];
|
|
@@ -300,6 +315,7 @@ const DEFAULTS = {
|
|
|
300
315
|
chat: { agent: "pi" },
|
|
301
316
|
presets: [],
|
|
302
317
|
limit: 1,
|
|
318
|
+
reserves: {},
|
|
303
319
|
idle: 10,
|
|
304
320
|
tiers: {
|
|
305
321
|
communicator: [],
|
|
@@ -311,8 +327,8 @@ const DEFAULTS = {
|
|
|
311
327
|
env: {},
|
|
312
328
|
auto: {
|
|
313
329
|
on: false,
|
|
314
|
-
|
|
315
|
-
|
|
330
|
+
attention: ATTENTION,
|
|
331
|
+
blueprints: [],
|
|
316
332
|
repos: []
|
|
317
333
|
}
|
|
318
334
|
};
|
|
@@ -336,7 +352,7 @@ function checkSettings(raw) {
|
|
|
336
352
|
},
|
|
337
353
|
auto: {
|
|
338
354
|
...DEFAULTS.auto,
|
|
339
|
-
...rest.auto
|
|
355
|
+
...upgradeAuto(rest.auto)
|
|
340
356
|
}
|
|
341
357
|
};
|
|
342
358
|
for (const name of Object.keys(settings.agents)) if (!AGENT_NAMES.includes(name)) throw new Error(`unknown agent ${name} (known: ${AGENT_NAMES.join(", ")})`);
|
|
@@ -344,6 +360,10 @@ function checkSettings(raw) {
|
|
|
344
360
|
for (const preset of settings.presets) if (!AGENT_NAMES.includes(preset.agent)) throw new Error(`unknown preset agent ${preset.agent} (known: ${AGENT_NAMES.join(", ")})`);
|
|
345
361
|
if (Object.keys(settings.agents).length === 0) throw new Error("settings need at least one agent");
|
|
346
362
|
if (settings.limit !== null && !(Number.isInteger(settings.limit) && settings.limit > 0)) throw new Error(`limit wants a whole number above 0 or null, got ${settings.limit}`);
|
|
363
|
+
for (const [pool, reserve] of Object.entries(settings.reserves)) {
|
|
364
|
+
if (!USAGE_POOLS.includes(pool)) throw new Error(`can't read ${pool}'s usage to keep a reserve (can: ${USAGE_POOLS.join(", ")})`);
|
|
365
|
+
if (!(Number.isInteger(reserve) && reserve >= 0 && reserve < 100)) throw new Error(`reserve for ${pool} wants a whole percent from 0 to 99, got ${reserve}`);
|
|
366
|
+
}
|
|
347
367
|
if (settings.until !== void 0 && !/^\d{1,2}:\d{2}$/.test(settings.until)) throw new Error(`until wants HH:MM, got ${settings.until}`);
|
|
348
368
|
if (!(Number.isInteger(settings.idle) && settings.idle > 0)) throw new Error(`idle wants whole minutes above 0, got ${settings.idle}`);
|
|
349
369
|
const ids = settings.presets.map(presetId);
|
|
@@ -354,14 +374,27 @@ function checkSettings(raw) {
|
|
|
354
374
|
}
|
|
355
375
|
if (settings.store !== "local" && settings.store !== "tasks") throw new Error(`store wants local or tasks, got ${String(settings.store)}`);
|
|
356
376
|
if (settings.store === "tasks" && !settings.tasks?.home) throw new Error("store tasks wants tasks.home, the Tasks home to mirror goals into");
|
|
357
|
-
for (const
|
|
377
|
+
for (const attention of settings.auto.attention) if (!ATTENTION.includes(attention)) throw new Error(`unknown attention ${attention}`);
|
|
358
378
|
return settings;
|
|
359
379
|
}
|
|
380
|
+
/** Before workwork 0.3 auto mode filtered on levels and plays, with auto for none. */
|
|
381
|
+
function upgradeAuto(auto = {}) {
|
|
382
|
+
const { levels, plays, ...rest } = auto;
|
|
383
|
+
return {
|
|
384
|
+
...rest,
|
|
385
|
+
...levels && { attention: levels.map((level) => level === "auto" ? "none" : level) },
|
|
386
|
+
...plays && { blueprints: plays }
|
|
387
|
+
};
|
|
388
|
+
}
|
|
360
389
|
const candidatesPath = (state) => join(state, "candidates.json");
|
|
361
390
|
const refreshLock = (state) => join(state, "candidates.lock");
|
|
362
391
|
function readCandidates(state) {
|
|
363
392
|
if (!existsSync(candidatesPath(state))) return void 0;
|
|
364
|
-
|
|
393
|
+
const { at, items } = JSON.parse(readFileSync(candidatesPath(state), "utf8"));
|
|
394
|
+
return {
|
|
395
|
+
at,
|
|
396
|
+
items: items.map(upgradeItem)
|
|
397
|
+
};
|
|
365
398
|
}
|
|
366
399
|
function refreshingCandidates(state) {
|
|
367
400
|
if (!existsSync(refreshLock(state))) return false;
|
|
@@ -389,11 +422,13 @@ async function refreshCandidates(state, workwork) {
|
|
|
389
422
|
rmSync(refreshLock(state), { force: true });
|
|
390
423
|
}
|
|
391
424
|
}
|
|
392
|
-
async function output(command, args, cwd) {
|
|
393
|
-
const
|
|
425
|
+
async function output(command, args, cwd, input) {
|
|
426
|
+
const run = promisify(execFile)(command, args, {
|
|
394
427
|
cwd,
|
|
395
428
|
maxBuffer: 67108864
|
|
396
429
|
});
|
|
430
|
+
run.child.stdin.end(input);
|
|
431
|
+
const { stdout, stderr } = await run;
|
|
397
432
|
process.stderr.write(stderr);
|
|
398
433
|
return stdout;
|
|
399
434
|
}
|
|
@@ -434,7 +469,7 @@ async function readPr(url) {
|
|
|
434
469
|
state: raw.state,
|
|
435
470
|
draft: raw.isDraft,
|
|
436
471
|
title: raw.title,
|
|
437
|
-
|
|
472
|
+
attention: /^(?:Attention|Level): (\w+)\s*$/m.exec(raw.body)?.[1].replace(/^auto$/, "none"),
|
|
438
473
|
additions: raw.additions,
|
|
439
474
|
deletions: raw.deletions,
|
|
440
475
|
files: raw.files.map((file) => file.path),
|
|
@@ -602,6 +637,7 @@ function record(state, event) {
|
|
|
602
637
|
}
|
|
603
638
|
const fresh = (job, at) => ({
|
|
604
639
|
...job,
|
|
640
|
+
item: upgradeItem(job.item),
|
|
605
641
|
after: typeof job.after === "string" ? [job.after] : job.after ?? [],
|
|
606
642
|
gates: job.gates ?? [],
|
|
607
643
|
needs: job.needs ?? [],
|
|
@@ -671,7 +707,7 @@ function replay(events) {
|
|
|
671
707
|
job.blocked = void 0;
|
|
672
708
|
job.why = void 0;
|
|
673
709
|
}
|
|
674
|
-
if (event.type === "update") Object.assign(job, event.change);
|
|
710
|
+
if (event.type === "update") Object.assign(job, event.change, event.change.item && { item: upgradeItem(event.change.item) });
|
|
675
711
|
if (event.type === "verify") job.unverified = event.need ? job.unverified.filter((need) => need !== event.need) : [];
|
|
676
712
|
if (event.type === "note") {
|
|
677
713
|
const { kind } = event.note;
|
|
@@ -694,7 +730,7 @@ function replay(events) {
|
|
|
694
730
|
const said = job.notes.filter((note) => note.attempt === number);
|
|
695
731
|
const asked = said.findLastIndex((note) => note.kind === "ask") > said.findLastIndex((note) => note.kind === "answer" && note.re === void 0);
|
|
696
732
|
const lacking = [...job.notes.findLast((note) => note.kind === "result" && note.attempt === number)?.unverified ?? [], ...event.attempt.missing ?? []];
|
|
697
|
-
const planning = PLANNING.includes(job.item.
|
|
733
|
+
const planning = PLANNING.includes(job.item.blueprint);
|
|
698
734
|
if (outcome === "done" && lacking.length && !planning) job.unverified = [.../* @__PURE__ */ new Set([...job.unverified, ...lacking])];
|
|
699
735
|
if (asked) {
|
|
700
736
|
job.state = "blocked";
|
|
@@ -811,7 +847,7 @@ const ACTIVE = [
|
|
|
811
847
|
"running",
|
|
812
848
|
"blocked"
|
|
813
849
|
];
|
|
814
|
-
const sameWork = (item) => `${itemKey(item)} ${item.
|
|
850
|
+
const sameWork = (item) => `${itemKey(item)} ${item.blueprint} ${item.title}`;
|
|
815
851
|
/** Queues the items, skipping those already in the queue; returns the jobs it added. */
|
|
816
852
|
function addJobs(state, items, source, pin = {}) {
|
|
817
853
|
const at = (/* @__PURE__ */ new Date()).toISOString();
|
|
@@ -833,6 +869,28 @@ function addJob(state, job, at = (/* @__PURE__ */ new Date()).toISOString()) {
|
|
|
833
869
|
});
|
|
834
870
|
return fresh(job, at);
|
|
835
871
|
}
|
|
872
|
+
/** A job of the user's own, from a prompt for a tier, without workwork. */
|
|
873
|
+
function addOwnJob(state, own) {
|
|
874
|
+
if (!TIERS.includes(own.tier)) throw new Error(`a tier is ${TIERS.join(", ")}, not ${own.tier}`);
|
|
875
|
+
const first = own.prompt.split("\n").find((line) => line.trim());
|
|
876
|
+
if (!first) throw new Error("the prompt is empty");
|
|
877
|
+
const title = own.title?.trim() || first.replace(/^#+\s*/, "").slice(0, 100);
|
|
878
|
+
return addJob(state, {
|
|
879
|
+
id: newId(),
|
|
880
|
+
item: {
|
|
881
|
+
repo: own.repo,
|
|
882
|
+
label: basename(own.repo),
|
|
883
|
+
id: `own-${Date.now()}`,
|
|
884
|
+
blueprint: "own",
|
|
885
|
+
attention: "decide",
|
|
886
|
+
title
|
|
887
|
+
},
|
|
888
|
+
source: "pick",
|
|
889
|
+
tier: own.tier,
|
|
890
|
+
prompt: own.prompt,
|
|
891
|
+
after: own.after
|
|
892
|
+
});
|
|
893
|
+
}
|
|
836
894
|
/**
|
|
837
895
|
* Queues a job to rebase the job's PR and resolve its conflicts, unless one is already waiting. It
|
|
838
896
|
* keeps the job's workwork id, so the attempt links the same PR.
|
|
@@ -841,7 +899,7 @@ function queueConflicts(state, id, url, files) {
|
|
|
841
899
|
const job = readJobs(state).find((job) => job.id === id);
|
|
842
900
|
addJobs(state, [{
|
|
843
901
|
...job.item,
|
|
844
|
-
|
|
902
|
+
blueprint: "conflicts",
|
|
845
903
|
title: `Resolve conflicts: ${job.item.title}`
|
|
846
904
|
}], "pick", { prompt: conflictsPrompt(url, files) });
|
|
847
905
|
}
|
|
@@ -867,7 +925,7 @@ function queueReview(state, id, agent) {
|
|
|
867
925
|
const branch = workworkBranch(job.item);
|
|
868
926
|
const item = {
|
|
869
927
|
...job.item,
|
|
870
|
-
|
|
928
|
+
blueprint: "review",
|
|
871
929
|
title: `Review: ${job.item.title}`
|
|
872
930
|
};
|
|
873
931
|
const prompt = reviewPrompt$1(branch, job.agent);
|
|
@@ -897,7 +955,7 @@ function queueFollowup(state, id, task) {
|
|
|
897
955
|
const item = {
|
|
898
956
|
...job.item,
|
|
899
957
|
id: `${job.item.id}+followup`,
|
|
900
|
-
|
|
958
|
+
blueprint: "followup",
|
|
901
959
|
title: task
|
|
902
960
|
};
|
|
903
961
|
const url = prUrl(job);
|
|
@@ -1024,7 +1082,7 @@ Change the work to follow the answer. ${prUrl(job) ? `Its pull request is ${prUr
|
|
|
1024
1082
|
item: {
|
|
1025
1083
|
...job.item,
|
|
1026
1084
|
id: `${job.item.id}+answer`,
|
|
1027
|
-
|
|
1085
|
+
blueprint: "answer",
|
|
1028
1086
|
title: `Follow the answer: ${job.item.title}`
|
|
1029
1087
|
},
|
|
1030
1088
|
source: job.of ? "goal" : "pick",
|
|
@@ -1056,11 +1114,11 @@ function endJob(state, id, attempt) {
|
|
|
1056
1114
|
attempt
|
|
1057
1115
|
});
|
|
1058
1116
|
}
|
|
1059
|
-
/** Cheapest attention first, and within
|
|
1060
|
-
function order(items,
|
|
1061
|
-
return
|
|
1117
|
+
/** Cheapest attention first, and within each one item per repo in turn so no repo takes the night. */
|
|
1118
|
+
function order(items, wanted) {
|
|
1119
|
+
return ATTENTION.filter((attention) => wanted.includes(attention)).flatMap((attention) => {
|
|
1062
1120
|
const byRepo = /* @__PURE__ */ new Map();
|
|
1063
|
-
for (const item of items.filter((item) => item.
|
|
1121
|
+
for (const item of items.filter((item) => item.attention === attention)) byRepo.set(item.repo, [...byRepo.get(item.repo) ?? [], item]);
|
|
1064
1122
|
const queues = [...byRepo.values()];
|
|
1065
1123
|
const turns = [];
|
|
1066
1124
|
for (let round = 0; queues.some((queue) => queue.length > round); round++) for (const queue of queues) if (queue[round]) turns.push(queue[round]);
|
|
@@ -1070,7 +1128,7 @@ function order(items, levels) {
|
|
|
1070
1128
|
/** What auto mode would add next: wanted candidates that never had a job. */
|
|
1071
1129
|
function autoPicks(candidates, jobs, auto, count) {
|
|
1072
1130
|
const seen = new Set(jobs.map((job) => itemKey(job.item)));
|
|
1073
|
-
return order(candidates.filter((item) => !seen.has(itemKey(item)) && (auto.
|
|
1131
|
+
return order(candidates.filter((item) => !seen.has(itemKey(item)) && (auto.blueprints.length === 0 || auto.blueprints.includes(item.blueprint)) && (auto.repos.length === 0 || auto.repos.includes(item.repo))), auto.attention).slice(0, count);
|
|
1074
1132
|
}
|
|
1075
1133
|
//#endregion
|
|
1076
1134
|
//#region node_modules/.pnpm/yaml@2.9.1/node_modules/yaml/dist/nodes/identity.js
|
|
@@ -7808,7 +7866,7 @@ async function release(worktree) {
|
|
|
7808
7866
|
//#endregion
|
|
7809
7867
|
//#region src/backend/goals.ts
|
|
7810
7868
|
const PLAN_FORMAT = "factory-plan/1";
|
|
7811
|
-
/** Factory's own jobs inside a goal, told apart by their
|
|
7869
|
+
/** Factory's own jobs inside a goal, told apart by their blueprint. */
|
|
7812
7870
|
const [DECOMPOSE, REPLAN] = PLANNING;
|
|
7813
7871
|
const REVIEW = "review";
|
|
7814
7872
|
const FIX = "fix";
|
|
@@ -7897,12 +7955,12 @@ const planPath = (slug) => `.factory/${slug}.plan.json`;
|
|
|
7897
7955
|
const replanPath = (slug) => `.factory/${slug}.replan.json`;
|
|
7898
7956
|
const integration = (goal) => `goal/${goal.slug}`;
|
|
7899
7957
|
const nodeBranch = (goal, key) => `node/${goal.slug}/${key}`;
|
|
7900
|
-
const goalItem = (repo, id,
|
|
7958
|
+
const goalItem = (repo, id, blueprint, title) => ({
|
|
7901
7959
|
repo,
|
|
7902
7960
|
label: basename(repo),
|
|
7903
7961
|
id,
|
|
7904
|
-
|
|
7905
|
-
|
|
7962
|
+
blueprint,
|
|
7963
|
+
attention: "decide",
|
|
7906
7964
|
title
|
|
7907
7965
|
});
|
|
7908
7966
|
/**
|
|
@@ -7928,7 +7986,7 @@ async function importPlan(state, plan, into) {
|
|
|
7928
7986
|
goal
|
|
7929
7987
|
});
|
|
7930
7988
|
if (!into) {
|
|
7931
|
-
checkpoint(state, job.id,
|
|
7989
|
+
checkpoint(state, job.id, planReady(plan));
|
|
7932
7990
|
await createBranch(goal);
|
|
7933
7991
|
}
|
|
7934
7992
|
await commitFiles(goal, `factory: plan for ${plan.goal.title}`, {
|
|
@@ -8029,6 +8087,7 @@ async function newGoal(state, title, repo, brief) {
|
|
|
8029
8087
|
const goal = {
|
|
8030
8088
|
repo,
|
|
8031
8089
|
slug,
|
|
8090
|
+
brief: brief.trim() || void 0,
|
|
8032
8091
|
summary,
|
|
8033
8092
|
gates: [],
|
|
8034
8093
|
allowUp: false
|
|
@@ -8095,8 +8154,8 @@ Read the repo enough to plan well. Write the plan to \`${planPath(slug)}\` in th
|
|
|
8095
8154
|
/** Checks factory itself runs after the agent says it's done, before the job's gates: the plan files. */
|
|
8096
8155
|
function builtinCheck(job, jobs, worktree) {
|
|
8097
8156
|
const goal = jobs.find((each) => each.id === job.of)?.goal;
|
|
8098
|
-
if (!goal || job.item.
|
|
8099
|
-
const path = join(worktree, job.item.
|
|
8157
|
+
if (!goal || job.item.blueprint !== DECOMPOSE && job.item.blueprint !== REPLAN) return void 0;
|
|
8158
|
+
const path = join(worktree, job.item.blueprint === DECOMPOSE ? planPath(goal.slug) : replanPath(goal.slug));
|
|
8100
8159
|
if (!existsSync(path)) return `There's no plan at ${path}. Write it there.`;
|
|
8101
8160
|
let plan;
|
|
8102
8161
|
try {
|
|
@@ -8104,7 +8163,7 @@ function builtinCheck(job, jobs, worktree) {
|
|
|
8104
8163
|
} catch (error) {
|
|
8105
8164
|
return `${path} isn't valid JSON: ${error.message}`;
|
|
8106
8165
|
}
|
|
8107
|
-
const problems = [...checkPlan(plan), ...job.item.
|
|
8166
|
+
const problems = [...checkPlan(plan), ...job.item.blueprint === REPLAN ? replanProblems(plan, job, jobs) : []];
|
|
8108
8167
|
if (problems.length) return `The plan at ${path} has problems; fix them all:\n${problems.map((each) => `- ${each}`).join("\n")}`;
|
|
8109
8168
|
}
|
|
8110
8169
|
/** The prompt for a job: workwork's, or a goal node's with its goal, memory and dependencies; checks out its worktree. */
|
|
@@ -8135,15 +8194,15 @@ async function prepare(state, job, jobs, worktree, workwork) {
|
|
|
8135
8194
|
"-B",
|
|
8136
8195
|
branch
|
|
8137
8196
|
], worktree);
|
|
8138
|
-
if (job.item.
|
|
8139
|
-
if (job.item.
|
|
8197
|
+
if (job.item.blueprint === DECOMPOSE || job.item.blueprint === REVIEW) return job.prompt;
|
|
8198
|
+
if (job.item.blueprint === REPLAN) return job.prompt;
|
|
8140
8199
|
return nodePrompt(job, jobs, goalJob, branch);
|
|
8141
8200
|
}
|
|
8142
8201
|
/** The branch a goal job works on: its own, or for factory's helpers the one they serve. */
|
|
8143
8202
|
function branchFor(job, jobs, goal) {
|
|
8144
8203
|
const served = job.branchOf && jobs.find((each) => each.id === job.branchOf);
|
|
8145
8204
|
if (served) return nodeBranch(goal, served.key);
|
|
8146
|
-
return nodeBranch(goal, job.key ?? `${job.item.
|
|
8205
|
+
return nodeBranch(goal, job.key ?? `${job.item.blueprint}-${job.id}`);
|
|
8147
8206
|
}
|
|
8148
8207
|
function nodePrompt(job, jobs, goalJob, branch) {
|
|
8149
8208
|
const goal = goalJob.goal;
|
|
@@ -8182,6 +8241,7 @@ async function afterAttempt(state, id, worktree, log) {
|
|
|
8182
8241
|
if (job.state === "failed") queueReplan(state, goalJob, `${job.item.title} failed for good: ${job.attempts.at(-1)?.error ?? "see its notes"}`);
|
|
8183
8242
|
else if (job.attempts.at(-1)?.outcome === "done") await delivered$1(state, job, jobs, goalJob, worktree, log);
|
|
8184
8243
|
checkBudget(state, goalJob.id);
|
|
8244
|
+
await finishGoals(state, log);
|
|
8185
8245
|
} catch (error) {
|
|
8186
8246
|
const message = error.message.trim().split("\n").slice(0, 5).join("\n");
|
|
8187
8247
|
log(`goal ${goalJob.goal.slug}: ${job.id} didn't deliver: ${message}`);
|
|
@@ -8197,8 +8257,8 @@ async function afterAttempt(state, id, worktree, log) {
|
|
|
8197
8257
|
}
|
|
8198
8258
|
async function delivered$1(state, job, jobs, goalJob, worktree, log) {
|
|
8199
8259
|
const goal = goalJob.goal;
|
|
8200
|
-
const
|
|
8201
|
-
if (
|
|
8260
|
+
const blueprint = job.item.blueprint;
|
|
8261
|
+
if (blueprint === DECOMPOSE) {
|
|
8202
8262
|
const plan = JSON.parse(readFileSync(join(worktree, planPath(goal.slug)), "utf8"));
|
|
8203
8263
|
const { summary, gates, budget, allowUp, replanTier } = plan.goal;
|
|
8204
8264
|
record(state, {
|
|
@@ -8213,7 +8273,7 @@ async function delivered$1(state, job, jobs, goalJob, worktree, log) {
|
|
|
8213
8273
|
replanTier
|
|
8214
8274
|
} }
|
|
8215
8275
|
});
|
|
8216
|
-
checkpoint(state, goalJob.id,
|
|
8276
|
+
checkpoint(state, goalJob.id, planReady(plan));
|
|
8217
8277
|
await importPlan(state, plan, goalJob);
|
|
8218
8278
|
record(state, {
|
|
8219
8279
|
type: "merged",
|
|
@@ -8221,8 +8281,8 @@ async function delivered$1(state, job, jobs, goalJob, worktree, log) {
|
|
|
8221
8281
|
});
|
|
8222
8282
|
return;
|
|
8223
8283
|
}
|
|
8224
|
-
if (
|
|
8225
|
-
if (
|
|
8284
|
+
if (blueprint === REPLAN) return applyReplan(state, job, jobs, goalJob, worktree);
|
|
8285
|
+
if (blueprint === MAIN) {
|
|
8226
8286
|
await output("git", [
|
|
8227
8287
|
"push",
|
|
8228
8288
|
"--quiet",
|
|
@@ -8237,7 +8297,7 @@ async function delivered$1(state, job, jobs, goalJob, worktree, log) {
|
|
|
8237
8297
|
return afterMerge(state, job, goalJob.id, worktree, log);
|
|
8238
8298
|
}
|
|
8239
8299
|
const served = job.branchOf ? jobs.find((each) => each.id === job.branchOf) : job;
|
|
8240
|
-
if (
|
|
8300
|
+
if (blueprint === REVIEW) {
|
|
8241
8301
|
const findings = job.notes.findLast((note) => note.kind === "result")?.followups ?? [];
|
|
8242
8302
|
record(state, {
|
|
8243
8303
|
type: "merged",
|
|
@@ -8247,7 +8307,7 @@ async function delivered$1(state, job, jobs, goalJob, worktree, log) {
|
|
|
8247
8307
|
addJob(state, helper(served, goalJob, FIX, `Fix review findings: ${served.item.title}`, served.tier, `A review of branch \`${nodeBranch(goal, served.key)}\` found:\n\n${findings.map((each) => `- ${each}`).join("\n")}\n\nFix each one on that branch (it's checked out here), run the checks, commit and push. Don't open a pull request, and keep what the branch added to the goal memory, \`${memoryPath(goal.slug)}\`.\n\nThe original task:\n\n${served.prompt}`));
|
|
8248
8308
|
return;
|
|
8249
8309
|
}
|
|
8250
|
-
if (served === job && job.review && !jobs.some((each) => each.branchOf === job.id && each.item.
|
|
8310
|
+
if (served === job && job.review && !jobs.some((each) => each.branchOf === job.id && each.item.blueprint === REVIEW)) {
|
|
8251
8311
|
await output("git", [
|
|
8252
8312
|
"push",
|
|
8253
8313
|
"--quiet",
|
|
@@ -8261,10 +8321,10 @@ async function delivered$1(state, job, jobs, goalJob, worktree, log) {
|
|
|
8261
8321
|
return merge(state, served, goalJob, worktree, log, job === served ? void 0 : job);
|
|
8262
8322
|
}
|
|
8263
8323
|
/** One of factory's jobs for a node's branch, at the front of the goal. */
|
|
8264
|
-
function helper(node, goalJob,
|
|
8324
|
+
function helper(node, goalJob, blueprint, title, tier, prompt) {
|
|
8265
8325
|
return {
|
|
8266
8326
|
id: newId(),
|
|
8267
|
-
item: goalItem(goalJob.goal.repo, `${node.key ?? node.item.id}+${
|
|
8327
|
+
item: goalItem(goalJob.goal.repo, `${node.key ?? node.item.id}+${blueprint}`, blueprint, title),
|
|
8268
8328
|
source: "goal",
|
|
8269
8329
|
of: goalJob.id,
|
|
8270
8330
|
parent: node.parent,
|
|
@@ -8272,7 +8332,7 @@ function helper(node, goalJob, play, title, tier, prompt) {
|
|
|
8272
8332
|
tier,
|
|
8273
8333
|
front: true,
|
|
8274
8334
|
prompt,
|
|
8275
|
-
gates:
|
|
8335
|
+
gates: blueprint === REVIEW ? [] : node.gates,
|
|
8276
8336
|
touches: node.touches,
|
|
8277
8337
|
needs: node.needs
|
|
8278
8338
|
};
|
|
@@ -8296,7 +8356,7 @@ ${job.prompt}`;
|
|
|
8296
8356
|
async function merge(state, node, goalJob, worktree, log, by) {
|
|
8297
8357
|
const goal = goalJob.goal;
|
|
8298
8358
|
const git = (...args) => output("git", args, worktree);
|
|
8299
|
-
const branch = nodeBranch(goal, node.key ?? `${node.item.
|
|
8359
|
+
const branch = nodeBranch(goal, node.key ?? `${node.item.blueprint}-${node.id}`);
|
|
8300
8360
|
const base = integration(goal);
|
|
8301
8361
|
for (let tries = 0;; tries++) {
|
|
8302
8362
|
await git("fetch", "--quiet", "origin");
|
|
@@ -8323,7 +8383,7 @@ async function merge(state, node, goalJob, worktree, log, by) {
|
|
|
8323
8383
|
}
|
|
8324
8384
|
}
|
|
8325
8385
|
const said = (by ?? node).notes.findLast((note) => note.kind === "result");
|
|
8326
|
-
const url = node.item.
|
|
8386
|
+
const url = node.item.blueprint === MAIN ? void 0 : await createPr(goal.repo, base, branch, `${node.item.title}`, `${said?.text ?? node.item.title}\n\nPart of goal ${goalJob.item.title}; factory merged it into ${base}.`).catch(() => void 0);
|
|
8327
8387
|
record(state, {
|
|
8328
8388
|
type: "note",
|
|
8329
8389
|
id: node.id,
|
|
@@ -8367,7 +8427,7 @@ async function remember(goal, node, worktree) {
|
|
|
8367
8427
|
`HEAD:${integration(goal)}`
|
|
8368
8428
|
], worktree);
|
|
8369
8429
|
}
|
|
8370
|
-
/** After a merge: the goal's gates, checkpoints, milestones
|
|
8430
|
+
/** After a merge: the goal's gates, checkpoints, milestones and a red goal turning green. */
|
|
8371
8431
|
async function afterMerge(state, node, goalId, worktree, log) {
|
|
8372
8432
|
const goalJob = readJobs(state).find((each) => each.id === goalId);
|
|
8373
8433
|
const goal = goalJob.goal;
|
|
@@ -8404,20 +8464,30 @@ async function afterMerge(state, node, goalId, worktree, log) {
|
|
|
8404
8464
|
type: "unblock",
|
|
8405
8465
|
id: goalId
|
|
8406
8466
|
});
|
|
8407
|
-
if (node.checkpoint) checkpoint(state, goalId,
|
|
8467
|
+
if (node.checkpoint) checkpoint(state, goalId, `${node.item.title} is merged into ${integration(goal)}, and the plan stops here so you can check it. Approving starts the rest`);
|
|
8408
8468
|
const parent = node.parent && readJobs(state).find((each) => each.id === node.parent);
|
|
8409
8469
|
if (parent && parent.state === "done") {
|
|
8410
8470
|
await rebaseOntoMain(state, goalJob, worktree, log);
|
|
8411
8471
|
queueReplan(state, goalJob, `milestone ${parent.item.title} is done`);
|
|
8412
8472
|
}
|
|
8413
|
-
|
|
8414
|
-
|
|
8473
|
+
}
|
|
8474
|
+
/**
|
|
8475
|
+
* Opens the final PR of every goal whose nodes are all done. Runs after each goal attempt, not only
|
|
8476
|
+
* a merge: a replan can be the last to finish, and a run's start picks up any goal left waiting.
|
|
8477
|
+
*/
|
|
8478
|
+
async function finishGoals(state, log) {
|
|
8479
|
+
const jobs = readJobs(state);
|
|
8480
|
+
for (const goalJob of jobs) {
|
|
8481
|
+
if (!goalJob.goal || goalJob.state !== "queued") continue;
|
|
8482
|
+
const rest = jobs.filter((each) => each.of === goalJob.id && each.state !== "removed");
|
|
8483
|
+
if (rest.length === 0 || rest.some((each) => each.state !== "done")) continue;
|
|
8484
|
+
const goal = goalJob.goal;
|
|
8415
8485
|
const main = await originHead(goal.repo);
|
|
8416
8486
|
const unverified = rest.flatMap((each) => each.unverified.map((need) => `- [ ] ${each.item.title}: ${need}`));
|
|
8417
8487
|
const url = await createPr(goal.repo, main, integration(goal), goalJob.item.title, `${goal.summary}\n${unverified.length ? `\n## Unverified\n\n${unverified.join("\n")}\n` : ""}\nFactory worked this goal; merging it is yours.`);
|
|
8418
8488
|
record(state, {
|
|
8419
8489
|
type: "note",
|
|
8420
|
-
id:
|
|
8490
|
+
id: goalJob.id,
|
|
8421
8491
|
note: {
|
|
8422
8492
|
kind: "factory",
|
|
8423
8493
|
text: `every node is done; the final PR is open`,
|
|
@@ -8426,7 +8496,7 @@ async function afterMerge(state, node, goalId, worktree, log) {
|
|
|
8426
8496
|
});
|
|
8427
8497
|
record(state, {
|
|
8428
8498
|
type: "merged",
|
|
8429
|
-
id:
|
|
8499
|
+
id: goalJob.id
|
|
8430
8500
|
});
|
|
8431
8501
|
log(`goal ${goal.slug} is done: ${url}`);
|
|
8432
8502
|
}
|
|
@@ -8469,7 +8539,7 @@ async function gatesFail(gates, cwd) {
|
|
|
8469
8539
|
/** Queues a replan of the goal, unless one waits already. */
|
|
8470
8540
|
function queueReplan(state, goalJob, why) {
|
|
8471
8541
|
const jobs = readJobs(state);
|
|
8472
|
-
if (jobs.some((each) => each.of === goalJob.id && each.item.
|
|
8542
|
+
if (jobs.some((each) => each.of === goalJob.id && each.item.blueprint === REPLAN && ["queued", "running"].includes(each.state))) return void 0;
|
|
8473
8543
|
const goal = goalJob.goal;
|
|
8474
8544
|
return addJob(state, {
|
|
8475
8545
|
id: newId(),
|
|
@@ -8595,6 +8665,7 @@ function applyReplan(state, job, jobs, goalJob, worktree) {
|
|
|
8595
8665
|
});
|
|
8596
8666
|
if (changed > unstarted.length / 4) checkpoint(state, goalJob.id, "the replan changed more than a quarter of what's left; approve it to go on");
|
|
8597
8667
|
}
|
|
8668
|
+
const planReady = (plan) => `the plan has ${plan.nodes.length} jobs, shown below. Approving starts them, and nothing runs until then; replan first if it needs changes`;
|
|
8598
8669
|
/** Stops the goal for the user's approval, saying why on the goal and in its notes. */
|
|
8599
8670
|
function checkpoint(state, id, why) {
|
|
8600
8671
|
record(state, {
|
|
@@ -8621,7 +8692,7 @@ function checkBudget(state, goalId) {
|
|
|
8621
8692
|
const used = attemptsUsed(jobs, goalId);
|
|
8622
8693
|
const allowed = budget * (1 + goalJob.notes.filter((note) => note.kind === "factory" && note.text.startsWith("budget raised")).length);
|
|
8623
8694
|
if (used < allowed) return;
|
|
8624
|
-
checkpoint(state, goalId, `
|
|
8695
|
+
checkpoint(state, goalId, `the goal used ${used} of its ${allowed} attempts. Approving gives it ${budget} more; pause or cancel it if it's going nowhere`);
|
|
8625
8696
|
}
|
|
8626
8697
|
const attemptsUsed = (jobs, goalId) => jobs.filter((each) => each.of === goalId).reduce((sum, each) => sum + each.attempts.length, 0);
|
|
8627
8698
|
/** The user's go: after the plan, a checkpoint or a used budget. */
|
|
@@ -8958,8 +9029,8 @@ function pull(state, tasks) {
|
|
|
8958
9029
|
repo: goal.goal.repo,
|
|
8959
9030
|
label: goal.item.label,
|
|
8960
9031
|
id: key,
|
|
8961
|
-
|
|
8962
|
-
|
|
9032
|
+
blueprint: "goal",
|
|
9033
|
+
attention: "decide",
|
|
8963
9034
|
title: task.title
|
|
8964
9035
|
},
|
|
8965
9036
|
source: "goal",
|
|
@@ -9256,6 +9327,49 @@ function describe(tool, input) {
|
|
|
9256
9327
|
return tool;
|
|
9257
9328
|
}
|
|
9258
9329
|
//#endregion
|
|
9330
|
+
//#region src/backend/usage.ts
|
|
9331
|
+
const CLAUDE = "https://api.anthropic.com/api/oauth/usage";
|
|
9332
|
+
const CODEX = "https://chatgpt.com/backend-api/wham/usage";
|
|
9333
|
+
const readJson = (...path) => JSON.parse(readFileSync(join(homedir(), ...path), "utf8"));
|
|
9334
|
+
const piAuth = () => readJson(".pi", "agent", "auth.json");
|
|
9335
|
+
const READERS = {
|
|
9336
|
+
claude: () => claudeWindow(readJson(".claude", ".credentials.json").claudeAiOauth.accessToken),
|
|
9337
|
+
anthropic: () => claudeWindow(piAuth().anthropic.access),
|
|
9338
|
+
"openai-codex": async () => {
|
|
9339
|
+
const { access, accountId } = piAuth()["openai-codex"];
|
|
9340
|
+
const window = (await get(CODEX, {
|
|
9341
|
+
Authorization: `Bearer ${access}`,
|
|
9342
|
+
"ChatGPT-Account-Id": accountId
|
|
9343
|
+
})).rate_limit.primary_window;
|
|
9344
|
+
return {
|
|
9345
|
+
used: window.used_percent,
|
|
9346
|
+
resets: window.reset_at * 1e3
|
|
9347
|
+
};
|
|
9348
|
+
}
|
|
9349
|
+
};
|
|
9350
|
+
const fiveHour = (pool) => READERS[pool]();
|
|
9351
|
+
async function claudeWindow(token) {
|
|
9352
|
+
const window = (await get(CLAUDE, {
|
|
9353
|
+
Authorization: `Bearer ${token}`,
|
|
9354
|
+
"anthropic-beta": "oauth-2025-04-20"
|
|
9355
|
+
})).five_hour ?? {
|
|
9356
|
+
utilization: 0,
|
|
9357
|
+
resets_at: ""
|
|
9358
|
+
};
|
|
9359
|
+
return {
|
|
9360
|
+
used: window.utilization,
|
|
9361
|
+
resets: Date.parse(window.resets_at)
|
|
9362
|
+
};
|
|
9363
|
+
}
|
|
9364
|
+
async function get(url, headers) {
|
|
9365
|
+
const response = await fetch(url, {
|
|
9366
|
+
headers,
|
|
9367
|
+
signal: AbortSignal.timeout(1e4)
|
|
9368
|
+
});
|
|
9369
|
+
if (!response.ok) throw new Error(`${url} answered ${response.status}`);
|
|
9370
|
+
return await response.json();
|
|
9371
|
+
}
|
|
9372
|
+
//#endregion
|
|
9259
9373
|
//#region src/backend/loop.ts
|
|
9260
9374
|
const MINUTE = 6e4;
|
|
9261
9375
|
/** Auto mode keeps this many of its jobs queued per worker, so picks never drown in them. */
|
|
@@ -9287,6 +9401,8 @@ async function run(options) {
|
|
|
9287
9401
|
let killing = false;
|
|
9288
9402
|
let topping;
|
|
9289
9403
|
const borrowed = /* @__PURE__ */ new Set();
|
|
9404
|
+
/** Each pool's last usage read, shared by its workers so one read serves them all. */
|
|
9405
|
+
const reads = /* @__PURE__ */ new Map();
|
|
9290
9406
|
const piPresets = slots.map((slot) => slot.preset).filter((preset) => preset.agent === "pi" && preset.model);
|
|
9291
9407
|
if (piPresets.length) piWindows().then((windows) => piSettingsWarnings(piPresets, windows).forEach(log)).catch((error) => log(`can't check pi's compaction settings: ${error.message}`));
|
|
9292
9408
|
function writeStatus() {
|
|
@@ -9338,6 +9454,34 @@ async function run(options) {
|
|
|
9338
9454
|
wakers.add(wake);
|
|
9339
9455
|
});
|
|
9340
9456
|
}
|
|
9457
|
+
/**
|
|
9458
|
+
* Holds a pool until its 5-hour window resets once the window reaches the pool's reserve, so
|
|
9459
|
+
* other apps on the account keep some. Reads at most once a minute; a failed read holds nothing.
|
|
9460
|
+
* Reads the settings each time, so a changed reserve applies at once.
|
|
9461
|
+
*/
|
|
9462
|
+
function keepReserve(pool) {
|
|
9463
|
+
const reserve = readSettings(state).reserves[pool.name];
|
|
9464
|
+
const last = reads.get(pool.name);
|
|
9465
|
+
if (reserve === void 0 || pool.until) return Promise.resolve();
|
|
9466
|
+
if (last && Date.now() - last.at < MINUTE) return last.done;
|
|
9467
|
+
const done = (options.usage ?? fiveHour)(pool.name).then(({ used, resets }) => {
|
|
9468
|
+
pool.used = used;
|
|
9469
|
+
if (used >= 100 - reserve && resets > Date.now()) {
|
|
9470
|
+
const wake = Math.min(resets + 2 * MINUTE, options.until ?? Infinity);
|
|
9471
|
+
Object.assign(pool, {
|
|
9472
|
+
until: new Date(wake).toISOString(),
|
|
9473
|
+
reserved: true
|
|
9474
|
+
});
|
|
9475
|
+
log(`${pool.name} used ${used}% of its 5 hours, keeps ${reserve}% until ${pool.until}`);
|
|
9476
|
+
}
|
|
9477
|
+
writeStatus();
|
|
9478
|
+
}, (error) => log(`can't read ${pool.name}'s usage: ${error.message}`));
|
|
9479
|
+
reads.set(pool.name, {
|
|
9480
|
+
at: Date.now(),
|
|
9481
|
+
done
|
|
9482
|
+
});
|
|
9483
|
+
return done;
|
|
9484
|
+
}
|
|
9341
9485
|
/** Auto mode's part: keep a few of its jobs queued. Reads settings each time, so the toggle applies at once. */
|
|
9342
9486
|
function topUp() {
|
|
9343
9487
|
topping ??= fillFromAuto().catch((error) => log(`auto mode could not ask workwork: ${error.message}`)).finally(() => topping = void 0);
|
|
@@ -9388,6 +9532,7 @@ async function run(options) {
|
|
|
9388
9532
|
if (chosen === worker.preset) return job;
|
|
9389
9533
|
if (chosen !== null) continue;
|
|
9390
9534
|
}
|
|
9535
|
+
if (!(worker.preset in options.agents)) continue;
|
|
9391
9536
|
if ((job.agent ?? worker.agent) === worker.agent) return job;
|
|
9392
9537
|
}
|
|
9393
9538
|
}
|
|
@@ -9400,13 +9545,17 @@ async function run(options) {
|
|
|
9400
9545
|
state: "stopped",
|
|
9401
9546
|
reason: pool.stopped
|
|
9402
9547
|
});
|
|
9548
|
+
if (!handed) await keepReserve(pool);
|
|
9403
9549
|
if (pool.until) {
|
|
9404
9550
|
set(worker, {
|
|
9405
9551
|
state: "sleeping",
|
|
9406
9552
|
until: pool.until
|
|
9407
9553
|
});
|
|
9408
9554
|
await (options.sleep ?? nap)(Math.max(0, Date.parse(pool.until) - Date.now()));
|
|
9409
|
-
pool
|
|
9555
|
+
Object.assign(pool, {
|
|
9556
|
+
until: void 0,
|
|
9557
|
+
reserved: void 0
|
|
9558
|
+
});
|
|
9410
9559
|
continue;
|
|
9411
9560
|
}
|
|
9412
9561
|
let job;
|
|
@@ -9764,6 +9913,7 @@ async function run(options) {
|
|
|
9764
9913
|
const stopMirror = mirrorTasks(state, settings, log);
|
|
9765
9914
|
writeStatus();
|
|
9766
9915
|
try {
|
|
9916
|
+
await finishGoals(state, log);
|
|
9767
9917
|
await Promise.all(slots.map((slot) => work(slot, handed.get(slot))));
|
|
9768
9918
|
} catch (error) {
|
|
9769
9919
|
kill();
|
|
@@ -10437,11 +10587,12 @@ function context() {
|
|
|
10437
10587
|
const factory = `"${process.execPath}" "${process.argv[1]}"`;
|
|
10438
10588
|
return `You run inside the web page of factory, which works a queue of workwork candidates with headless coding agents, often overnight. You help the user decide what to put in the queue.
|
|
10439
10589
|
|
|
10440
|
-
- The current folder is factory's state. candidates.json holds every repo's candidates from workwork ({ at, items: [{ repo, label, id,
|
|
10590
|
+
- The current folder is factory's state. candidates.json holds every repo's candidates from workwork ({ at, items: [{ repo, label, id, blueprint, attention, title }] }); jobs.jsonl is the queue and results, one event per line. runs/ holds each attempt's prompt.md and agent.log.
|
|
10441
10591
|
- \`${factory} status\` shows the queue and \`${factory} report --all\` past results. \`workwork prompt <id>\`, run in the candidate's repo, shows the full task.
|
|
10442
10592
|
- To queue candidates: \`${factory} add <id>... --repo <repo>\`, optionally with \`--agent ${AGENT_NAMES.join("|")}\`. Picked jobs go ahead of auto ones.
|
|
10593
|
+
- A blueprint is the reusable prompt behind candidates; \`workwork blueprints\` lists them, and on-request ones never show up in candidates.json. \`${factory} add --blueprint <name> --repo <repo>\` queues what one finds in a repo.
|
|
10443
10594
|
- \`--after <job>\` holds a job until that one is done. \`${factory} review <job> --agent <name>\` queues a review of a job's PR by another agent than the one that wrote it.
|
|
10444
|
-
-
|
|
10595
|
+
- Attention: none needs no review, skim a quick look, decide needs the user's judgment. Prefer a few that fit together over many that touch the same files, since those conflict when merged.
|
|
10445
10596
|
|
|
10446
10597
|
- Goals are one large task, planned into a graph of jobs. When the user wants one, work out a brief with them: what done looks like, constraints, links to specs, and how to verify it (the commands that prove it works). Write it to a file in this folder, then \`${factory} goal new "<title>" --repo <repo> --brief <file>\`, or \`--project <name>\` instead of \`--repo\` for a new project, which creates the repo under the source folder and on GitHub. A decomposer plans it, and the user approves the plan on the Goals tab: never approve one yourself.
|
|
10447
10598
|
|
|
@@ -10560,6 +10711,9 @@ function serve(state, port, host) {
|
|
|
10560
10711
|
}));
|
|
10561
10712
|
return models.get(name);
|
|
10562
10713
|
}
|
|
10714
|
+
function refresh() {
|
|
10715
|
+
refreshCandidates(state, "workwork").catch((error) => console.error(`factory: workwork failed: ${error.message}`));
|
|
10716
|
+
}
|
|
10563
10717
|
/** Only cached candidates, so the page can't queue or run workwork for anything it names. */
|
|
10564
10718
|
function candidates(wanted) {
|
|
10565
10719
|
const cached = new Map(readCandidates(state)?.items.map((item) => [itemKey(item), item]));
|
|
@@ -10609,12 +10763,12 @@ function serve(state, port, host) {
|
|
|
10609
10763
|
};
|
|
10610
10764
|
},
|
|
10611
10765
|
"POST /api/candidates/refresh": () => {
|
|
10612
|
-
|
|
10766
|
+
refresh();
|
|
10613
10767
|
},
|
|
10614
10768
|
"GET /api/repos": () => clonableRepos(),
|
|
10615
10769
|
"POST /api/repos/clone": async (body) => {
|
|
10616
10770
|
await cloneRepo(body.name);
|
|
10617
|
-
|
|
10771
|
+
refresh();
|
|
10618
10772
|
},
|
|
10619
10773
|
/** A goal from a brief, in a repo under $SRC or, with `project`, a new one. */
|
|
10620
10774
|
"POST /api/goals": async (body) => {
|
|
@@ -10623,12 +10777,34 @@ function serve(state, port, host) {
|
|
|
10623
10777
|
return newGoal(state, body.title.trim(), repo, body.brief ?? "");
|
|
10624
10778
|
},
|
|
10625
10779
|
"POST /api/goals/import": (body) => importPlanText(state, body.plan),
|
|
10626
|
-
"GET /api/
|
|
10780
|
+
"GET /api/blueprints": async () => JSON.parse(await output("workwork", ["blueprints", "--json"], process.cwd())),
|
|
10781
|
+
"PUT /api/blueprints": async (body) => {
|
|
10782
|
+
await output("workwork", ["blueprints", "save"], process.cwd(), JSON.stringify(body));
|
|
10783
|
+
refresh();
|
|
10784
|
+
},
|
|
10785
|
+
"POST /api/blueprints/delete": async (body) => {
|
|
10786
|
+
await output("workwork", [
|
|
10787
|
+
"blueprints",
|
|
10788
|
+
"delete",
|
|
10789
|
+
body.name
|
|
10790
|
+
], process.cwd());
|
|
10791
|
+
refresh();
|
|
10792
|
+
},
|
|
10793
|
+
"POST /api/blueprints/run": async (body) => {
|
|
10794
|
+
const found = JSON.parse(await output("workwork", [
|
|
10795
|
+
"--blueprint",
|
|
10796
|
+
body.name,
|
|
10797
|
+
"--json"
|
|
10798
|
+
], body.repo));
|
|
10799
|
+
if (found.length === 0) throw new Error(`${body.name} finds nothing to do in ${body.repo}`);
|
|
10800
|
+
return addJobs(state, found, "pick");
|
|
10801
|
+
},
|
|
10627
10802
|
"POST /api/candidates/prompt": (body) => {
|
|
10628
10803
|
const [item] = candidates([body]);
|
|
10629
10804
|
return output("workwork", ["prompt", item.id], item.repo);
|
|
10630
10805
|
},
|
|
10631
10806
|
"POST /api/jobs": (body) => addJobs(state, candidates(body.items), "pick", body.pin),
|
|
10807
|
+
"POST /api/jobs/own": (body) => addOwnJob(state, body),
|
|
10632
10808
|
"POST /api/jobs/remove": (_, id) => removeJob(state, id),
|
|
10633
10809
|
"POST /api/jobs/retry": (_, id) => retryJob(state, id),
|
|
10634
10810
|
"POST /api/jobs/answer": (body, id) => answer(state, id, body.text),
|
|
@@ -10776,7 +10952,7 @@ function send(response, type, body) {
|
|
|
10776
10952
|
}
|
|
10777
10953
|
//#endregion
|
|
10778
10954
|
//#region package.json
|
|
10779
|
-
var version = "0.
|
|
10955
|
+
var version = "0.4.0";
|
|
10780
10956
|
//#endregion
|
|
10781
10957
|
//#region src/backend/cli.ts
|
|
10782
10958
|
const USAGE = `factory: works workwork candidates and long goals with headless coding agents
|
|
@@ -10789,6 +10965,8 @@ const USAGE = `factory: works workwork candidates and long goals with headless c
|
|
|
10789
10965
|
factory add <id>... [--repo DIR] [--agent NAME] [--model M] [--effort E] [--after JOB]...
|
|
10790
10966
|
queue workwork candidates from DIR (default: here), ahead of auto jobs;
|
|
10791
10967
|
--after: start once that job is done
|
|
10968
|
+
factory add --blueprint NAME [--repo DIR] [<id>...]
|
|
10969
|
+
queue what a blueprint finds in DIR, on-request ones too; ids pick some
|
|
10792
10970
|
factory add --prompt FILE --tier T [--repo DIR] [--title TEXT]
|
|
10793
10971
|
one job of your own, without workwork, for a tier
|
|
10794
10972
|
factory review <job> --agent NAME
|
|
@@ -10858,6 +11036,7 @@ async function main() {
|
|
|
10858
11036
|
repo: { type: "string" },
|
|
10859
11037
|
project: { type: "string" },
|
|
10860
11038
|
prompt: { type: "string" },
|
|
11039
|
+
blueprint: { type: "string" },
|
|
10861
11040
|
tier: { type: "string" },
|
|
10862
11041
|
title: { type: "string" },
|
|
10863
11042
|
brief: { type: "string" },
|
|
@@ -10989,10 +11168,16 @@ function runOptions(values) {
|
|
|
10989
11168
|
}
|
|
10990
11169
|
async function add(ids, values) {
|
|
10991
11170
|
if (values.prompt) return addOwn(values);
|
|
10992
|
-
if (ids.length === 0) throw new Error("factory add wants
|
|
11171
|
+
if (ids.length === 0 && !values.blueprint) throw new Error("factory add wants workwork ids, --blueprint NAME or --prompt FILE");
|
|
10993
11172
|
const repo = resolve(values.repo ?? ".");
|
|
10994
|
-
const
|
|
10995
|
-
|
|
11173
|
+
const listing = values.blueprint ? [
|
|
11174
|
+
"--blueprint",
|
|
11175
|
+
values.blueprint,
|
|
11176
|
+
"--json"
|
|
11177
|
+
] : ["--json"];
|
|
11178
|
+
const candidates = JSON.parse(await output("workwork", listing, repo));
|
|
11179
|
+
if (values.blueprint && candidates.length === 0) throw new Error(`${values.blueprint} finds nothing to do in ${repo}`);
|
|
11180
|
+
const items = (ids.length ? ids : candidates.map((each) => each.id)).map((id) => {
|
|
10996
11181
|
const item = candidates.find((candidate) => candidate.id === id);
|
|
10997
11182
|
if (!item) throw new Error(`workwork has no candidate ${id} in ${repo}`);
|
|
10998
11183
|
return item;
|
|
@@ -11011,27 +11196,14 @@ async function add(ids, values) {
|
|
|
11011
11196
|
}
|
|
11012
11197
|
/** A job from a prompt file, for a tier, without workwork. */
|
|
11013
11198
|
function addOwn(values) {
|
|
11014
|
-
const
|
|
11015
|
-
|
|
11016
|
-
|
|
11017
|
-
|
|
11018
|
-
|
|
11019
|
-
const job = addJob(STATE, {
|
|
11020
|
-
id: newId(),
|
|
11021
|
-
item: {
|
|
11022
|
-
repo,
|
|
11023
|
-
label: basename(repo),
|
|
11024
|
-
id: `own-${Date.now()}`,
|
|
11025
|
-
play: "own",
|
|
11026
|
-
level: "decide",
|
|
11027
|
-
title
|
|
11028
|
-
},
|
|
11029
|
-
source: "pick",
|
|
11030
|
-
tier,
|
|
11031
|
-
prompt,
|
|
11199
|
+
const job = addOwnJob(STATE, {
|
|
11200
|
+
repo: resolve(values.repo ?? "."),
|
|
11201
|
+
prompt: readFileSync(values.prompt, "utf8"),
|
|
11202
|
+
tier: values.tier,
|
|
11203
|
+
title: values.title,
|
|
11032
11204
|
after: values.after
|
|
11033
11205
|
});
|
|
11034
|
-
console.log(`queued ${job.id}: ${title} (${tier})`);
|
|
11206
|
+
console.log(`queued ${job.id}: ${job.item.title} (${job.tier})`);
|
|
11035
11207
|
}
|
|
11036
11208
|
function step(what, values) {
|
|
11037
11209
|
const done = values.done === void 0 ? void 0 : Number(values.done);
|
|
@@ -11200,8 +11372,9 @@ function status() {
|
|
|
11200
11372
|
const until = status.until ? `, until ${clock(status.until)}` : "";
|
|
11201
11373
|
console.log(`run ${status.run}, limit ${status.limit ?? "none"}${until}`);
|
|
11202
11374
|
for (const pool of status.pools) {
|
|
11203
|
-
const pause = pool.stopped ?? (pool.until ?
|
|
11204
|
-
|
|
11375
|
+
const pause = pool.stopped ?? (pool.until ? `${pool.reserved ? "keeps its reserve" : "sleeps"} until ${clock(pool.until)}` : "");
|
|
11376
|
+
const used = pool.used === void 0 ? "" : `, ${pool.used}% of 5h used`;
|
|
11377
|
+
console.log(` pool ${pool.name}: limits hit ${pool.limits}${used}${pause ? `, ${pause}` : ""}`);
|
|
11205
11378
|
for (const worker of status.workers.filter((each) => each.pool === pool.name)) {
|
|
11206
11379
|
const what = worker.job ?? (worker.until ? `until ${clock(worker.until)}` : worker.reason ?? "");
|
|
11207
11380
|
const doing = worker.activity ? ` ${worker.activity}` : "";
|