@moo.md/hum 0.3.0 → 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/defaults/hum.json +5 -0
- package/defaults/roles/build.md +1 -1
- package/defaults/roles/fix.md +1 -1
- package/npm-shrinkwrap.json +3297 -0
- package/package.json +6 -2
- package/release.json +3 -0
- package/schema/hum.schema.json +47 -7
- package/scripts/eval.ts +21 -5
- package/src/adapter-api.ts +3 -1
- package/src/adapters/claude-bg.ts +2 -2
- package/src/adapters/claude-p.ts +44 -18
- package/src/adapters/contract.ts +12 -0
- package/src/adapters/fake.ts +5 -0
- package/src/adapters/git.ts +61 -9
- package/src/adapters/github-client.ts +15 -0
- package/src/adapters/github-commits.ts +84 -0
- package/src/adapters/github.ts +9 -0
- package/src/adapters/guard.ts +1 -0
- package/src/adapters/item-file.ts +1 -1
- package/src/adapters/markdown.ts +10 -1
- package/src/adapters/pull-requests.ts +8 -8
- package/src/adapters/registry.ts +3 -6
- package/src/adapters/roundup.ts +3 -0
- package/src/bytes.ts +1 -0
- package/src/cli/service.ts +176 -29
- package/src/cli/setup.ts +4 -5
- package/src/config/checks.ts +21 -50
- package/src/config/load.ts +2 -3
- package/src/config/machine.ts +29 -0
- package/src/config/refusal.ts +5 -6
- package/src/config/schema.ts +23 -7
- package/src/config/wake-job.ts +6 -2
- package/src/decide/risk.ts +2 -2
- package/src/exec.ts +10 -0
- package/src/index.ts +1 -1
- package/src/loop/records.ts +8 -1
- package/src/measure/rate.ts +14 -0
- package/src/publish/bundle.ts +128 -0
- package/src/publish/handoff.ts +107 -0
- package/src/publish/handover.ts +182 -0
- package/src/publish/intents.ts +61 -0
- package/src/publish/publisher.ts +259 -0
- package/src/publish/wake.ts +78 -0
- package/src/service/ci-state.ts +99 -0
- package/src/service/plugins.ts +137 -0
- package/src/service/wake.ts +3 -1
- package/src/service/workflows.ts +122 -28
- package/src/setup/propose.ts +17 -4
- package/src/setup/prove.ts +3 -3
- package/src/setup/write.ts +2 -2
- package/src/state/items.ts +17 -8
- package/src/state/open.ts +20 -6
- package/src/state/records.ts +7 -0
- package/src/state/runs.ts +5 -0
- package/src/state/saved.ts +17 -2
- package/src/worker/base-check.ts +41 -13
- package/src/worker/checks.ts +3 -2
- package/src/worker/evidence.ts +72 -0
- package/src/worker/kind.ts +4 -7
- package/src/worker/land.ts +173 -81
- package/src/worker/merged.ts +20 -8
- package/src/worker/open.ts +33 -17
- package/src/worker/questions.ts +16 -8
- package/src/worker/signed-push.ts +167 -0
- package/src/worker/thin-path.ts +245 -133
- package/src/worker/worker.ts +50 -9
- package/surface.json +185 -56
package/src/adapters/markdown.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { MergeMethod } from "../config/schema.ts";
|
|
|
9
9
|
import { parseArgument } from "./argument.ts";
|
|
10
10
|
import { claimCommitMessage, claimer, claimOfCommit, isLive } from "./claim.ts";
|
|
11
11
|
import { type GitIdentity, gitIn, push, readOrAbsent } from "./git.ts";
|
|
12
|
-
import { appendEntry, type ItemFile, newItemFile, parseItemFile, READY_STATE } from "./item-file.ts";
|
|
12
|
+
import { appendEntry, DONE_STATE, type ItemFile, markDone, newItemFile, parseItemFile, READY_STATE } from "./item-file.ts";
|
|
13
13
|
import { answerMarker, followUpBody, followUpKey, questionText } from "./marked.ts";
|
|
14
14
|
import { type CommitLanded, landOwnerCommit } from "./owner-commit.ts";
|
|
15
15
|
import type { PullRequests } from "./pull-requests.ts";
|
|
@@ -352,6 +352,15 @@ export const markdownAdapter = ({ repo, claimPrefix, itemsDir, identity, writeRa
|
|
|
352
352
|
|
|
353
353
|
return failed ?? { id, title };
|
|
354
354
|
},
|
|
355
|
+
/** A landed change already carries the item file's done state, so this writes only where it does not. */
|
|
356
|
+
finish: async (id) =>
|
|
357
|
+
write(`hum: finish ${id}`, async () => {
|
|
358
|
+
const source = await itemSource(id);
|
|
359
|
+
|
|
360
|
+
if (source === undefined) throw new PermanentError(`no item "${id}" under ${itemsDir}/`);
|
|
361
|
+
|
|
362
|
+
return parseItemFile(source).state === DONE_STATE ? undefined : { path: pathOf(id), content: markDone(source) };
|
|
363
|
+
}),
|
|
355
364
|
},
|
|
356
365
|
questions: {
|
|
357
366
|
ask: (id, question) => appendTo(id, { kind: "question" }, questionText(question)),
|
|
@@ -3,12 +3,13 @@ import pMemoize from "p-memoize";
|
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import type { Json } from "../config/json.ts";
|
|
5
5
|
import { MERGE_METHODS, type MergeMethod } from "../config/schema.ts";
|
|
6
|
-
import type
|
|
7
|
-
import { type GitHubClient, MERGE_METHOD, ownerGitHub, raise } from "./github-client.ts";
|
|
6
|
+
import { MERGE_METHOD, raise, type RepoClientOptions, repoClient } from "./github-client.ts";
|
|
8
7
|
|
|
9
8
|
/** What the target branch's rules ask of a change that lands on it. */
|
|
10
9
|
export type BranchRules = {
|
|
11
10
|
readonly requiresPullRequest: boolean;
|
|
11
|
+
/** Every commit that reaches the branch must carry a signature GitHub verifies. */
|
|
12
|
+
readonly requiresSignatures: boolean;
|
|
12
13
|
/** The merge methods a pull request may use; undefined when no rule narrows them. */
|
|
13
14
|
readonly mergeMethods: readonly MergeMethod[] | undefined;
|
|
14
15
|
};
|
|
@@ -59,6 +60,8 @@ const Rule = z.looseObject({
|
|
|
59
60
|
|
|
60
61
|
const PULL_REQUEST_RULE = "pull_request";
|
|
61
62
|
|
|
63
|
+
const SIGNATURES_RULE = "required_signatures";
|
|
64
|
+
|
|
62
65
|
const PullNode = z.object({
|
|
63
66
|
id: z.string(),
|
|
64
67
|
number: z.int(),
|
|
@@ -137,12 +140,8 @@ export const methodFor = (
|
|
|
137
140
|
: (allowed[0] ?? preferred);
|
|
138
141
|
|
|
139
142
|
/** Pull requests and branch rules of a GitHub repo, on the owner's own login unless a client is given. `timeoutMs` bounds the `gh auth token` that reads that login. */
|
|
140
|
-
export const gitHubPullRequests = (
|
|
141
|
-
repo
|
|
142
|
-
options: { readonly timeoutMs: number } & ({ readonly client: () => Promise<GitHubClient> } | { readonly env: Env }),
|
|
143
|
-
): PullRequests => {
|
|
144
|
-
const [owner = "", name = ""] = repo.split("/");
|
|
145
|
-
const connect = "client" in options ? options.client : () => ownerGitHub(options.timeoutMs, options.env);
|
|
143
|
+
export const gitHubPullRequests = (repo: string, options: RepoClientOptions): PullRequests => {
|
|
144
|
+
const { owner, name, connect } = repoClient(repo, options);
|
|
146
145
|
|
|
147
146
|
const rulesCache = new Map<string, BranchRules>();
|
|
148
147
|
|
|
@@ -176,6 +175,7 @@ export const gitHubPullRequests = (
|
|
|
176
175
|
|
|
177
176
|
return {
|
|
178
177
|
requiresPullRequest: pullRule !== undefined,
|
|
178
|
+
requiresSignatures: rules.some((rule) => rule.type === SIGNATURES_RULE),
|
|
179
179
|
mergeMethods: pullRule?.parameters?.allowed_merge_methods,
|
|
180
180
|
};
|
|
181
181
|
},
|
package/src/adapters/registry.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { pathToFileURL } from "node:url";
|
|
|
4
4
|
import { z } from "zod";
|
|
5
5
|
import { ADAPTER_API_VERSION, type Adapter, type AdapterContext, type AdapterFactory, type Integrations, PermanentError } from "../adapter-api.ts";
|
|
6
6
|
import { readSettings } from "../config/settings.ts";
|
|
7
|
-
import { HumConfig } from "../config/schema.ts";
|
|
7
|
+
import { HumConfig, SLOTS, type Slot } from "../config/schema.ts";
|
|
8
8
|
import { commandTimeoutMs, type Env } from "../exec.ts";
|
|
9
9
|
import type { Faults } from "../state/faults.ts";
|
|
10
10
|
import type { Card } from "../status/format.ts";
|
|
@@ -15,10 +15,7 @@ import { markdownAdapter } from "./markdown.ts";
|
|
|
15
15
|
import { gitHubPullRequests, type PullRequests } from "./pull-requests.ts";
|
|
16
16
|
import { roundupAdapter } from "./roundup.ts";
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
export const SLOTS = ["work-source", "questions", "answers", "release"] as const;
|
|
20
|
-
|
|
21
|
-
export type Slot = (typeof SLOTS)[number];
|
|
18
|
+
export { SLOTS, type Slot } from "../config/schema.ts";
|
|
22
19
|
|
|
23
20
|
const MARKDOWN = "markdown";
|
|
24
21
|
const FALLBACK_BASE = "main";
|
|
@@ -28,7 +25,7 @@ const MARKDOWN_SLOTS: readonly Slot[] = ["work-source", "questions", "answers"];
|
|
|
28
25
|
|
|
29
26
|
/** What the adapters read from `hum.json`; every other key belongs to the worker. */
|
|
30
27
|
const AdapterSettings = z.looseObject({
|
|
31
|
-
integrations:
|
|
28
|
+
integrations: HumConfig.shape.integrations,
|
|
32
29
|
claimPrefix: HumConfig.shape.claimPrefix,
|
|
33
30
|
itemsDir: HumConfig.shape.itemsDir,
|
|
34
31
|
commitIdentity: HumConfig.shape.commitIdentity,
|
package/src/adapters/roundup.ts
CHANGED
|
@@ -267,6 +267,9 @@ export const roundupAdapter = (options: RoundupOptions, { now, env }: AdapterCon
|
|
|
267
267
|
|
|
268
268
|
return { id, title };
|
|
269
269
|
},
|
|
270
|
+
finish: async (id) => {
|
|
271
|
+
if (!(await todo(id)).done) await call("todo_complete", { id: parseArgument(TodoId, id) });
|
|
272
|
+
},
|
|
270
273
|
},
|
|
271
274
|
questions: {
|
|
272
275
|
ask: (id, question) => postMarked(id, questionMarker(question.options), questionText(question)),
|
package/src/bytes.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const MEGABYTE = 1024 * 1024;
|
package/src/cli/service.ts
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
|
+
import * as core from "@actions/core";
|
|
1
2
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
+
import { mkdtempSync } from "node:fs";
|
|
4
|
+
import { hostname, tmpdir } from "node:os";
|
|
3
5
|
import { join } from "node:path";
|
|
4
6
|
import type { Command } from "commander";
|
|
5
7
|
import { x } from "tinyexec";
|
|
6
8
|
import { z } from "zod";
|
|
7
|
-
import {
|
|
9
|
+
import { listPluginSkills } from "../config/load.ts";
|
|
8
10
|
import { HumConfig } from "../config/schema.ts";
|
|
9
11
|
import { readSettings } from "../config/settings.ts";
|
|
10
12
|
import { commandTimeoutMs, exec, SHIPPED_COMMAND_TIMEOUT_MS } from "../exec.ts";
|
|
13
|
+
import { CI_STATE_REF, CiStateRaced, restoreCiState, saveCiState } from "../service/ci-state.ts";
|
|
11
14
|
import { readCiTick, recordCiTick } from "../service/ci-tick.ts";
|
|
12
15
|
import { EXIT_UPDATED, supervise } from "../service/supervise.ts";
|
|
16
|
+
import { installPlugins } from "../service/plugins.ts";
|
|
13
17
|
import { reportCards, wakeUntilQuiet } from "../service/wake.ts";
|
|
14
|
-
import { defaultStateDir, openState } from "../state/open.ts";
|
|
18
|
+
import { defaultStateDir, openState, STATUS_FILE } from "../state/open.ts";
|
|
15
19
|
import { SECOND_MS } from "../time.ts";
|
|
16
20
|
import { readStatus } from "../status/file.ts";
|
|
17
|
-
import { npmLatest,
|
|
21
|
+
import { npmLatest, type OpenWorkerOptions, workerOptions } from "../worker/open.ts";
|
|
22
|
+
import { Worker, type WorkerOptions } from "../worker/worker.ts";
|
|
23
|
+
import { DEFAULT_BASE, publisherOf } from "../worker/thin-path.ts";
|
|
24
|
+
import { tallyOf } from "../worker/tally.ts";
|
|
25
|
+
import { Gathered, inboxIntegrations, readInbox, readOutbox, workRemote, writeBaseCheck, writeInbox, writeOutboxState, writePass } from "../publish/handover.ts";
|
|
26
|
+
import { checkMergesAt } from "../worker/base-check.ts";
|
|
27
|
+
import { publishWake } from "../publish/wake.ts";
|
|
28
|
+
import { configuredModelCall } from "../adapters/model-call.ts";
|
|
29
|
+
import { type Git, gitIn } from "../adapters/git.ts";
|
|
30
|
+
import { APP_TOKEN_MINUTES, WAKE_ENV } from "../config/wake-job.ts";
|
|
31
|
+
import { MINUTE_MS } from "../time.ts";
|
|
18
32
|
import type { Io } from "./ops.ts";
|
|
19
|
-
import { Holder } from "../adapter-api.ts";
|
|
33
|
+
import { Holder, type Item, ItemId } from "../adapter-api.ts";
|
|
20
34
|
import { parseArgument } from "../adapters/argument.ts";
|
|
35
|
+
import { MEGABYTE } from "../bytes.ts";
|
|
21
36
|
|
|
22
37
|
const EXIT_OK = 0;
|
|
23
38
|
|
|
@@ -26,6 +41,8 @@ const EXIT_STAY_DOWN = EXIT_OK;
|
|
|
26
41
|
|
|
27
42
|
const EXIT_CARDS_SHOWN = 1;
|
|
28
43
|
|
|
44
|
+
const EXIT_FAILED = 1;
|
|
45
|
+
|
|
29
46
|
const SCHEDULE_EVENT = "schedule";
|
|
30
47
|
|
|
31
48
|
type WorkFlags = { holder?: string; base?: string };
|
|
@@ -36,18 +53,23 @@ const withWorkOptions = (cmd: Command): Command =>
|
|
|
36
53
|
.option("--holder <name>", "the name this worker claims items by; default <where>@<host>")
|
|
37
54
|
.option("--base <branch>", "the branch items merge into; default main");
|
|
38
55
|
|
|
39
|
-
const PluginList = z.array(z.looseObject({ id: z.string(), installPath: z.string() }));
|
|
56
|
+
const PluginList = z.array(z.looseObject({ id: z.string(), installPath: z.string(), enabled: z.boolean().default(true) }));
|
|
40
57
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
58
|
+
const HOPE = "hope";
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The skills of every enabled plugin, as a role names them (`sound:prime`), read from the folders `claude plugin list`
|
|
62
|
+
* gives. `HUM_HOPE_ROOT` names hope's folder instead, and then hope's skills are all there is. hope must be there.
|
|
63
|
+
*/
|
|
64
|
+
export const installedSkills = async (): Promise<ReadonlySet<string>> => {
|
|
65
|
+
if (process.env.HUM_HOPE_ROOT) return new Set(listPluginSkills(HOPE, process.env.HUM_HOPE_ROOT));
|
|
44
66
|
|
|
45
67
|
const listed = await exec("claude", ["plugin", "list", "--json"], { timeoutMs: SHIPPED_COMMAND_TIMEOUT_MS });
|
|
46
|
-
const
|
|
68
|
+
const plugins = listed.exitCode === 0 ? PluginList.parse(JSON.parse(listed.stdout)).filter(({ enabled }) => enabled).map(({ id, installPath }) => ({ name: id.split("@")[0] ?? id, installPath })) : [];
|
|
47
69
|
|
|
48
|
-
if (!
|
|
70
|
+
if (!plugins.some(({ name }) => name === HOPE)) throw new Error("the hope plugin is not installed: run `claude plugin install hope@moo.md`, or set HUM_HOPE_ROOT to its folder");
|
|
49
71
|
|
|
50
|
-
return
|
|
72
|
+
return new Set(plugins.flatMap(({ name, installPath }) => listPluginSkills(name, installPath)));
|
|
51
73
|
};
|
|
52
74
|
|
|
53
75
|
const ServiceSettings = z.looseObject({
|
|
@@ -72,19 +94,20 @@ const serviceSettings = (repo: string): z.infer<typeof ServiceSettings> => {
|
|
|
72
94
|
};
|
|
73
95
|
|
|
74
96
|
/** The Mac reads the npm version and when the scheduled workflow last ran; a CI job is that workflow. */
|
|
75
|
-
const
|
|
97
|
+
const optionsFor = async ({ cwd: repo, env }: Io, flags: WorkFlags, where: "mac" | "ci", extra: Partial<OpenWorkerOptions> = {}): Promise<WorkerOptions> => {
|
|
76
98
|
const timeoutMs = commandTimeoutMs(serviceSettings(repo));
|
|
77
99
|
|
|
78
|
-
return
|
|
100
|
+
return workerOptions(repo, {
|
|
79
101
|
now: Date.now,
|
|
80
102
|
env,
|
|
81
103
|
newSessionId: randomUUID,
|
|
82
|
-
|
|
104
|
+
installedSkills: extra.judgmentsOnly ? undefined : await installedSkills(),
|
|
83
105
|
holder: parseArgument(Holder, flags.holder ?? `${where}@${hostname()}`),
|
|
84
106
|
base: flags.base,
|
|
85
107
|
where,
|
|
86
108
|
latestVersion: where === "mac" ? npmLatest : undefined,
|
|
87
109
|
ciTick: where === "mac" ? () => readCiTick({ repo, remote: "origin", timeoutMs }) : undefined,
|
|
110
|
+
...extra,
|
|
88
111
|
});
|
|
89
112
|
};
|
|
90
113
|
|
|
@@ -100,8 +123,9 @@ const spawnWorker = (cwd: string, workArgs: readonly string[]) => {
|
|
|
100
123
|
};
|
|
101
124
|
|
|
102
125
|
/**
|
|
103
|
-
* `hum work`, `hum serve` and `hum wake
|
|
104
|
-
* roles and machines never name them, so they stay out of the table `hum help --json` prints.
|
|
126
|
+
* `hum work`, `hum serve` and `hum wake`'s jobs: the processes a service unit and a CI workflow start. They are not ops:
|
|
127
|
+
* roles and machines never name them, so they stay out of the table `hum help --json` prints. A CI job's git dir dies
|
|
128
|
+
* with it, so gather brings back the state the last wake saved first and saves it for publish, which saves it again.
|
|
105
129
|
*/
|
|
106
130
|
export const addServiceCommands = (root: Command, io: Io, done: (code: number) => void): void => {
|
|
107
131
|
withWorkOptions(root.command("work").description("Run the worker on this machine until it is stopped or an update arrives; exits 75 for an update."))
|
|
@@ -110,7 +134,7 @@ export const addServiceCommands = (root: Command, io: Io, done: (code: number) =
|
|
|
110
134
|
|
|
111
135
|
onStop(() => abort.abort());
|
|
112
136
|
|
|
113
|
-
const worker = await
|
|
137
|
+
const worker = new Worker(await optionsFor(io, flags, "mac"));
|
|
114
138
|
const ended = await worker.serve(abort.signal);
|
|
115
139
|
|
|
116
140
|
io.stderr(`hum work: stopped, ${ended.reason}\n`);
|
|
@@ -144,19 +168,142 @@ export const addServiceCommands = (root: Command, io: Io, done: (code: number) =
|
|
|
144
168
|
done(EXIT_STAY_DOWN);
|
|
145
169
|
});
|
|
146
170
|
|
|
147
|
-
|
|
148
|
-
.
|
|
149
|
-
.
|
|
150
|
-
|
|
171
|
+
root
|
|
172
|
+
.command("plugins")
|
|
173
|
+
.description("Install hope and the repo's own Claude Code plugins for a CI wake: moo.md at the commit this engine shipped with, then each marketplace and enabled plugin .claude/settings.json names.")
|
|
174
|
+
.action(async () => {
|
|
175
|
+
try {
|
|
176
|
+
for (const args of await installPlugins({ repo: io.cwd, env: io.env })) io.stdout(`claude ${args.join(" ")}\n`);
|
|
151
177
|
|
|
152
|
-
|
|
178
|
+
done(EXIT_OK);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
io.stderr(`hum plugins: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
181
|
+
done(EXIT_FAILED);
|
|
182
|
+
}
|
|
183
|
+
});
|
|
153
184
|
|
|
154
|
-
|
|
185
|
+
const wake = root.command("wake").description("One CI wake, as three jobs: gather claims and settles with GitHub's token, work runs with none, and publish checks and applies what work made.");
|
|
155
186
|
|
|
156
|
-
|
|
157
|
-
|
|
187
|
+
withWorkOptions(wake.command("gather").description("Bring back the state the last wake saved, claim each item and settle its answers and kind, save the state, and write the inbox the work job reads; sets the job output `gathered` for publish."))
|
|
188
|
+
.requiredOption("--inbox <folder>", "where to write the inbox")
|
|
189
|
+
.action((flags: WorkFlags & { inbox: string }) => wakeJob(io, done, async () => {
|
|
190
|
+
const dir = await defaultStateDir(io.cwd);
|
|
191
|
+
const timeoutMs = commandTimeoutMs(serviceSettings(io.cwd));
|
|
192
|
+
const where = { repo: io.cwd, remote: "origin", dir, timeoutMs };
|
|
193
|
+
const read = await restoreCiState(where);
|
|
194
|
+
const claimed = new Map<string, Item>();
|
|
195
|
+
const options = await optionsFor(io, flags, "ci", { ciJob: { kind: "gather", gathered: claimed }, judgmentsOnly: true });
|
|
196
|
+
const worker = new Worker(options);
|
|
158
197
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
198
|
+
for (let before = -1; claimed.size > before; ) {
|
|
199
|
+
before = claimed.size;
|
|
200
|
+
await worker.tick();
|
|
201
|
+
await worker.idle();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const base = options.base ?? DEFAULT_BASE;
|
|
205
|
+
const saved = await saveCiState({ ...where, ...read, identity: options.config.hum.commitIdentity, at: Date.now() });
|
|
206
|
+
const heads = await remoteHeads(gitIn(io.cwd, { timeoutMs }), options.config.hum.runBranchPrefix);
|
|
207
|
+
const baseSha = (await remoteHeads(gitIn(io.cwd, { timeoutMs }), base)).get(base);
|
|
208
|
+
|
|
209
|
+
if (baseSha === undefined) throw new Error(`origin has no branch ${base}`);
|
|
210
|
+
|
|
211
|
+
heads.delete(base);
|
|
212
|
+
|
|
213
|
+
const gathered: Gathered = { holder: options.holder, base, baseSha, heads: Object.fromEntries(heads), claimed: [...claimed.keys()].map((id) => ItemId.parse(id)), stateRead: saved.read };
|
|
214
|
+
|
|
215
|
+
writeInbox(flags.inbox, { gathered, items: [...claimed.values()] }, dir);
|
|
216
|
+
core.setOutput("gathered", JSON.stringify(gathered));
|
|
217
|
+
|
|
218
|
+
return dir;
|
|
219
|
+
}));
|
|
220
|
+
|
|
221
|
+
wake
|
|
222
|
+
.command("work")
|
|
223
|
+
.description("Run each item the inbox holds with no GitHub token: runs clone the base and hum branches gather saw, and each pass leaves what it asks publish to do in the outbox, with hum's state.")
|
|
224
|
+
.requiredOption("--inbox <folder>", "the inbox gather wrote")
|
|
225
|
+
.requiredOption("--outbox <folder>", "where to write the outbox")
|
|
226
|
+
.action((flags: { inbox: string; outbox: string }) => wakeJob(io, done, async () => {
|
|
227
|
+
const dir = await defaultStateDir(io.cwd);
|
|
228
|
+
const timeoutMs = commandTimeoutMs(serviceSettings(io.cwd));
|
|
229
|
+
const inbox = readInbox(flags.inbox, dir);
|
|
230
|
+
const { gathered } = inbox;
|
|
231
|
+
const remote = await workRemote({ checkout: io.cwd, gathered, folder: mkdtempSync(join(io.env.RUNNER_TEMP ?? tmpdir(), "hum-remote-")), timeoutMs });
|
|
232
|
+
|
|
233
|
+
const options = await optionsFor(io, { holder: gathered.holder, base: gathered.base }, "ci", {
|
|
234
|
+
remote,
|
|
235
|
+
integrations: inboxIntegrations(inbox, Date.now),
|
|
236
|
+
ciJob: { kind: "work", publish: async (pass, worked) => void writePass(flags.outbox, { ...pass, result: worked }) },
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const baseCheck = await checkMergesAt({ ...options, git: gitIn(io.cwd, { timeoutMs }) }, gathered.baseSha);
|
|
240
|
+
|
|
241
|
+
if (baseCheck !== undefined) writeBaseCheck(flags.outbox, baseCheck);
|
|
242
|
+
|
|
243
|
+
await wakeUntilQuiet(new Worker(options));
|
|
244
|
+
writeOutboxState(flags.outbox, dir);
|
|
245
|
+
|
|
246
|
+
return dir;
|
|
247
|
+
}));
|
|
248
|
+
|
|
249
|
+
wake
|
|
250
|
+
.command("publish")
|
|
251
|
+
.description(`Check and apply the work job's outbox with GitHub's token, from what gather settled in ${WAKE_ENV.gathered}, save the state, and record the tick when --event is schedule.`)
|
|
252
|
+
.requiredOption("--outbox <folder>", "the outbox the work job wrote")
|
|
253
|
+
.option("--event <name>", "the GitHub event that started this wake")
|
|
254
|
+
.action((flags: { outbox: string; event?: string }) => wakeJob(io, done, async () => {
|
|
255
|
+
const started = Date.now();
|
|
256
|
+
const gathered = Gathered.parse(JSON.parse(io.env[WAKE_ENV.gathered] ?? "null"));
|
|
257
|
+
const dir = await defaultStateDir(io.cwd);
|
|
258
|
+
const timeoutMs = commandTimeoutMs(serviceSettings(io.cwd));
|
|
259
|
+
const where = { repo: io.cwd, remote: "origin", dir, timeoutMs };
|
|
260
|
+
const read = await restoreCiState(where);
|
|
261
|
+
|
|
262
|
+
if (read.read !== gathered.stateRead) throw new CiStateRaced(`hum's state at ${CI_STATE_REF} is not the one gather saved, so another wake ran since; this wake publishes nothing`);
|
|
263
|
+
|
|
264
|
+
const options = await optionsFor(io, { holder: gathered.holder, base: gathered.base }, "ci", { judgmentsOnly: true });
|
|
265
|
+
const { hum } = options.config;
|
|
266
|
+
const outbox = readOutbox(flags.outbox, { maxBytes: hum.ciOutboxMaxMegabytes * MEGABYTE, claimed: new Set(gathered.claimed) });
|
|
267
|
+
const judge = options.judge ?? configuredModelCall(options.runner, hum, (item, tokens) => tallyOf(options.state, item).spend(tokens));
|
|
268
|
+
const publisher = publisherOf({ ...options, workSource: options.adapters.workSource, questions: options.adapters.questions }, judge, started + (APP_TOKEN_MINUTES - hum.ciMergeMarginMinutes) * MINUTE_MS);
|
|
269
|
+
|
|
270
|
+
let saved = read;
|
|
271
|
+
|
|
272
|
+
const save = async () => {
|
|
273
|
+
saved = await saveCiState({ ...where, ...saved, identity: hum.commitIdentity, at: Date.now() });
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
await publishWake({ publisher, gathered, outbox, work: openState(outbox.stateDir, { now: Date.now }), save });
|
|
277
|
+
|
|
278
|
+
if (flags.event === SCHEDULE_EVENT) await recordCiTick({ repo: io.cwd, remote: "origin", at: Date.now(), identity: hum.commitIdentity, timeoutMs });
|
|
279
|
+
|
|
280
|
+
return dir;
|
|
281
|
+
}));
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/** Runs one job of a wake, then shows the cards its state holds; the job exits 1 when it shows any or fails. */
|
|
285
|
+
const wakeJob = async (io: Io, done: (code: number) => void, job: () => Promise<string>): Promise<void> => {
|
|
286
|
+
try {
|
|
287
|
+
const status = readStatus(join(await job(), STATUS_FILE));
|
|
288
|
+
const cards = status.ok ? status.status.cards : [];
|
|
289
|
+
|
|
290
|
+
await reportCards(cards, { hasStepSummary: Boolean(process.env.GITHUB_STEP_SUMMARY) });
|
|
291
|
+
done(cards.length > 0 ? EXIT_CARDS_SHOWN : EXIT_OK);
|
|
292
|
+
} catch (error) {
|
|
293
|
+
io.stderr(`hum wake: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
294
|
+
done(EXIT_FAILED);
|
|
295
|
+
}
|
|
162
296
|
};
|
|
297
|
+
|
|
298
|
+
/** Each branch on origin under `prefix`, or named `prefix`, at its head. */
|
|
299
|
+
const remoteHeads = async (git: Git, prefix: string): Promise<Map<string, string>> =>
|
|
300
|
+
new Map(
|
|
301
|
+
(await git.raw(["ls-remote", "--heads", "origin", `refs/heads/${prefix}*`]))
|
|
302
|
+
.split("\n")
|
|
303
|
+
.filter(Boolean)
|
|
304
|
+
.map((line) => {
|
|
305
|
+
const [sha = "", ref = ""] = line.split("\t");
|
|
306
|
+
|
|
307
|
+
return [ref.slice("refs/heads/".length), sha] as const;
|
|
308
|
+
}),
|
|
309
|
+
);
|
package/src/cli/setup.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { x } from "tinyexec";
|
|
|
5
5
|
import { z } from "zod";
|
|
6
6
|
import type { Item } from "../adapter-api.ts";
|
|
7
7
|
import { gitIn, readGitIdentity } from "../adapters/git.ts";
|
|
8
|
-
import { listHopeSkills } from "../config/load.ts";
|
|
9
8
|
import { HumConfig } from "../config/schema.ts";
|
|
10
9
|
import { CommandTimedOut, exec, SHIPPED_COMMAND_TIMEOUT_MS } from "../exec.ts";
|
|
11
10
|
import { readSettings } from "../config/settings.ts";
|
|
@@ -16,7 +15,7 @@ import { shippedConfig } from "../setup/shipped.ts";
|
|
|
16
15
|
import { waitsOf } from "../setup/waits.ts";
|
|
17
16
|
import { readAnswers, writeSetup } from "../setup/write.ts";
|
|
18
17
|
import type { Io } from "./ops.ts";
|
|
19
|
-
import {
|
|
18
|
+
import { installedSkills } from "./service.ts";
|
|
20
19
|
|
|
21
20
|
const EXIT_OK = 0;
|
|
22
21
|
const EXIT_FAILED = 1;
|
|
@@ -107,7 +106,7 @@ export const addSetupCommands = (root: Command, io: Io, done: (code: number) =>
|
|
|
107
106
|
const scan = await scanRepo(io.cwd, base);
|
|
108
107
|
const facts = await githubFacts(io.cwd);
|
|
109
108
|
const isPublic = await isPublicRepo(io.cwd);
|
|
110
|
-
const proposal = proposeSetup(scan, shipped.hum, { labels: facts?.labels, isPublic: isPublic ?? true });
|
|
109
|
+
const proposal = proposeSetup(scan, shipped.hum, { labels: facts?.labels, isPublic: isPublic ?? true, openIssues: facts?.items.length });
|
|
111
110
|
const { tracked, folders, ...shown } = scan;
|
|
112
111
|
const coverage = facts && kindCoverage(facts.items, shipped.hum.kinds, proposal.kindLabels);
|
|
113
112
|
const gitIdentity = await readGitIdentity(setupGit(io.cwd));
|
|
@@ -121,7 +120,7 @@ export const addSetupCommands = (root: Command, io: Io, done: (code: number) =>
|
|
|
121
120
|
.requiredOption("--answers <file>", 'JSON: { "hum": {settings}, "waitHours": { "kind/state": hours }, "schemaVersion": 2 }')
|
|
122
121
|
.option("--home <folder>", "where the service unit goes; default the user's home")
|
|
123
122
|
.action(async ({ answers, home }: { answers: string; home?: string }) => {
|
|
124
|
-
const result = writeSetup(io.cwd, readAnswers(answers), {
|
|
123
|
+
const result = writeSetup(io.cwd, readAnswers(answers), { installedSkills: await installedSkills(), home, gitIdentity: await readGitIdentity(setupGit(io.cwd)), isPublicRepo: await isPublicRepo(io.cwd) });
|
|
125
124
|
|
|
126
125
|
io.stdout(`${JSON.stringify(result.ok ? { written: result.written } : { refused: result.refusal.message }, null, "\t")}\n`);
|
|
127
126
|
done(result.ok ? EXIT_OK : EXIT_FAILED);
|
|
@@ -133,7 +132,7 @@ export const addSetupCommands = (root: Command, io: Io, done: (code: number) =>
|
|
|
133
132
|
.option("--base <branch>", "the branch items merge into", DEFAULT_BASE)
|
|
134
133
|
.option("--item", "run one item through the real claude on a scratch copy; spends live calls")
|
|
135
134
|
.action(async ({ base, item }: { base: string; item?: true }) => {
|
|
136
|
-
const steps = await proveSetup(io.cwd, { now: Date.now, processEnv: io.env, newSessionId: randomUUID,
|
|
135
|
+
const steps = await proveSetup(io.cwd, { now: Date.now, processEnv: io.env, newSessionId: randomUUID, installedSkills: await installedSkills(), base, item: item === true });
|
|
137
136
|
|
|
138
137
|
io.stdout(`${steps.map(printStep).join("\n")}\n`);
|
|
139
138
|
done(steps.some(({ status }) => status === "failed") ? EXIT_FAILED : EXIT_OK);
|
package/src/config/checks.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { toDirectedGraph } from "xstate/graph";
|
|
2
1
|
import { APP_TOKEN_MINUTES, WAKE_OWN_ENV } from "./wake-job.ts";
|
|
3
|
-
import { kindMachine } from "./machine.ts";
|
|
2
|
+
import { kindMachine, nextStates, reachedFrom } from "./machine.ts";
|
|
4
3
|
import type { Problem } from "./refusal.ts";
|
|
5
4
|
import {
|
|
6
5
|
FIXED_PARTS,
|
|
@@ -15,8 +14,8 @@ import {
|
|
|
15
14
|
WAIT_TAG,
|
|
16
15
|
} from "./schema.ts";
|
|
17
16
|
|
|
18
|
-
/** What the checks read besides the config:
|
|
19
|
-
export type CheckEnv = { readonly
|
|
17
|
+
/** What the checks read besides the config: the installed plugins' skills, as a role names them (`hope:intent`); absent where no role runs, so no role's skills are checked. */
|
|
18
|
+
export type CheckEnv = { readonly installedSkills?: ReadonlySet<string> };
|
|
20
19
|
|
|
21
20
|
type Transitions = StateNodeJSON["onTimeout"];
|
|
22
21
|
|
|
@@ -37,39 +36,14 @@ const isXstateEvent = (event: string): boolean => event.startsWith("xstate.") ||
|
|
|
37
36
|
/** Every state is reachable and reaches done or stuck; every wait has a timeout and an action. */
|
|
38
37
|
const graphProblems = (kind: string, config: Kind): Problem[] => {
|
|
39
38
|
const { states, initial } = config.machine;
|
|
40
|
-
const
|
|
41
|
-
const forward = new Map<string, string[]>();
|
|
39
|
+
const forward = nextStates(config);
|
|
42
40
|
const backward = new Map<string, string[]>();
|
|
43
41
|
|
|
44
|
-
for (const
|
|
45
|
-
for (const edge of node.edges) {
|
|
46
|
-
forward.set(edge.source.key, [...(forward.get(edge.source.key) ?? []), edge.target.key]);
|
|
47
|
-
backward.set(edge.target.key, [...(backward.get(edge.target.key) ?? []), edge.source.key]);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const walk = (from: Iterable<string>, edges: Map<string, string[]>) => {
|
|
52
|
-
const seen = new Set<string>(from);
|
|
53
|
-
const todo = [...seen];
|
|
42
|
+
for (const [source, targets] of forward) for (const target of targets) backward.set(target, [...(backward.get(target) ?? []), source]);
|
|
54
43
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
seen.add(next);
|
|
60
|
-
todo.push(next);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return seen;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const reachable = walk([initial], forward);
|
|
68
|
-
|
|
69
|
-
const finishing = walk(
|
|
70
|
-
Object.entries(states).flatMap(([name, node]) => (isTerminal(node) ? [name] : [])),
|
|
71
|
-
backward,
|
|
72
|
-
);
|
|
44
|
+
const reachable = reachedFrom(initial, forward);
|
|
45
|
+
const terminal = Object.entries(states).flatMap(([name, node]) => (isTerminal(node) ? [name] : []));
|
|
46
|
+
const finishing = new Set(terminal.flatMap((state) => [...reachedFrom(state, backward)]));
|
|
73
47
|
|
|
74
48
|
return Object.entries(states).flatMap(([state, node]): Problem[] => [
|
|
75
49
|
...(reachable.has(state) ? [] : [{ problem: "unreachable-state", kind, state } as const]),
|
|
@@ -157,15 +131,14 @@ const eventProblems = (hum: HumConfig, kinds: Readonly<Record<string, Kind>>): P
|
|
|
157
131
|
];
|
|
158
132
|
};
|
|
159
133
|
|
|
160
|
-
/**
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
),
|
|
168
|
-
);
|
|
134
|
+
/** A plugin's skill is named `<plugin>:<skill>`; a bare name is the repo's or the user's own, which no plugin list holds. */
|
|
135
|
+
const isPluginSkill = (skill: string): boolean => skill.includes(":");
|
|
136
|
+
|
|
137
|
+
/** Each plugin skill a role names is in an installed plugin, where roles run. */
|
|
138
|
+
const skillProblems = (hum: HumConfig, { installedSkills }: CheckEnv): Problem[] =>
|
|
139
|
+
installedSkills === undefined
|
|
140
|
+
? []
|
|
141
|
+
: Object.entries(hum.roles).flatMap(([role, { skills }]) => skills.flatMap((skill): Problem[] => (isPluginSkill(skill) && !installedSkills.has(skill) ? [{ problem: "missing-skill", skill, role }] : [])));
|
|
169
142
|
|
|
170
143
|
/** A run branch could then share a name, or a folder, with a claim branch. */
|
|
171
144
|
const doPrefixesOverlap = ({ runBranchPrefix, claimPrefix }: Naming): boolean =>
|
|
@@ -194,15 +167,13 @@ const pathProblems = (hum: HumConfig): Problem[] => {
|
|
|
194
167
|
];
|
|
195
168
|
};
|
|
196
169
|
|
|
197
|
-
/** `ciEnv` sets none of the env the wake
|
|
170
|
+
/** `ciEnv` sets none of the env the wake's jobs set themselves. */
|
|
198
171
|
const wakeEnvProblems = ({ ciEnv }: HumConfig): Problem[] =>
|
|
199
172
|
Object.keys(ciEnv).flatMap((name): Problem[] => (WAKE_OWN_ENV.has(name) ? [{ problem: "wake-env-taken", name }] : []));
|
|
200
173
|
|
|
201
|
-
/**
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
? [{ problem: "wake-outlasts-app-token", minutes: ciWakeTimeoutMinutes, tokenMinutes: APP_TOKEN_MINUTES }]
|
|
205
|
-
: [];
|
|
174
|
+
/** Publish keeps less than the App token's hour in hand, so it has time left to merge. */
|
|
175
|
+
const mergeMarginProblems = ({ ciMergeMarginMinutes }: HumConfig): Problem[] =>
|
|
176
|
+
ciMergeMarginMinutes < APP_TOKEN_MINUTES ? [] : [{ problem: "merge-margin-fills-token-hour", minutes: ciMergeMarginMinutes, tokenMinutes: APP_TOKEN_MINUTES }];
|
|
206
177
|
|
|
207
178
|
/** The proof a path names for a kind: its own, else the one for every kind. */
|
|
208
179
|
export const proofFor = (rule: PathRule, kind: string): string | undefined => rule.proofs?.[kind] ?? rule.proofs?.[ALL_KINDS];
|
|
@@ -222,5 +193,5 @@ export const checkConfig = (
|
|
|
222
193
|
...skillProblems(hum, env),
|
|
223
194
|
...namingProblems(hum),
|
|
224
195
|
...wakeEnvProblems(hum),
|
|
225
|
-
...
|
|
196
|
+
...mergeMarginProblems(hum),
|
|
226
197
|
];
|
package/src/config/load.ts
CHANGED
|
@@ -83,6 +83,5 @@ export const readConfigDir = (dir: string): RawConfig => ({
|
|
|
83
83
|
roles: Object.fromEntries(filesIn(join(dir, "roles"), ".md").map((file) => [`roles/${file}`, readFileSync(join(dir, "roles", file), "utf8")])),
|
|
84
84
|
});
|
|
85
85
|
|
|
86
|
-
/** The
|
|
87
|
-
export const
|
|
88
|
-
new Set(scan("*/SKILL.md", join(hopeRoot, "skills")).map((file) => dirname(file)));
|
|
86
|
+
/** The skills in a plugin folder's `skills/*\/SKILL.md`, as a role names them: `hope:intent` for the plugin `hope`. */
|
|
87
|
+
export const listPluginSkills = (plugin: string, root: string): string[] => scan("*/SKILL.md", join(root, "skills")).map((file) => `${plugin}:${dirname(file)}`);
|
package/src/config/machine.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type AnyActorLogic, type AnyStateMachine, createAsyncLogic, machineFromJSON } from "xstate";
|
|
2
|
+
import { toDirectedGraph } from "xstate/graph";
|
|
2
3
|
import type { Kind } from "./schema.ts";
|
|
3
4
|
|
|
4
5
|
const placeholder = createAsyncLogic({ run: async () => {} });
|
|
@@ -16,3 +17,31 @@ export const kindMachine = (kind: Kind, sources: Readonly<Record<string, AnyActo
|
|
|
16
17
|
/** An actor for each source the kind's machine invokes: the one `sources` gives, else `fallback`. */
|
|
17
18
|
export const invokedSources = (kind: Kind, fallback: AnyActorLogic, sources: Readonly<Record<string, AnyActorLogic>> = {}): Record<string, AnyActorLogic> =>
|
|
18
19
|
Object.fromEntries(Object.values(kind.machine.states).flatMap(({ invoke }) => (invoke ? [[invoke.src, sources[invoke.src] ?? fallback]] : [])));
|
|
20
|
+
|
|
21
|
+
/** Each state's next states, by every transition of the kind's machine. */
|
|
22
|
+
export const nextStates = (kind: Kind): ReadonlyMap<string, readonly string[]> => {
|
|
23
|
+
const next = new Map<string, string[]>();
|
|
24
|
+
|
|
25
|
+
for (const node of toDirectedGraph(kindMachine(kind)).children) for (const edge of node.edges) next.set(edge.source.key, [...(next.get(edge.source.key) ?? []), edge.target.key]);
|
|
26
|
+
|
|
27
|
+
return next;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** The states `from` reaches through `next`, `from` among them; `expands` says which reached states lead on. */
|
|
31
|
+
export const reachedFrom = (from: string, next: ReadonlyMap<string, readonly string[]>, expands: (state: string) => boolean = () => true): ReadonlySet<string> => {
|
|
32
|
+
const seen = new Set([from]);
|
|
33
|
+
const todo = [from];
|
|
34
|
+
|
|
35
|
+
for (let state = todo.pop(); state !== undefined; state = todo.pop()) {
|
|
36
|
+
if (!expands(state)) continue;
|
|
37
|
+
|
|
38
|
+
for (const target of next.get(state) ?? []) {
|
|
39
|
+
if (seen.has(target)) continue;
|
|
40
|
+
|
|
41
|
+
seen.add(target);
|
|
42
|
+
todo.push(target);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return seen;
|
|
47
|
+
};
|