@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
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { FollowUp, ItemId } from "../adapter-api.ts";
|
|
3
|
+
import type { CallScope } from "../adapters/once.ts";
|
|
4
|
+
|
|
5
|
+
const text = z.string().min(1);
|
|
6
|
+
|
|
7
|
+
const Scope = z.strictObject({ run: text, attempt: z.int().nonnegative() });
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What a pass asks publish to do for its item, on the item's run branch. A pass never writes to the work source, the
|
|
11
|
+
* questions, GitHub or the remote itself: publish applies each intent under its op key, after it checked the pass's
|
|
12
|
+
* bundle.
|
|
13
|
+
*/
|
|
14
|
+
export const Intent = z.discriminatedUnion("kind", [
|
|
15
|
+
z.strictObject({ kind: z.literal("comment"), text, scope: Scope }),
|
|
16
|
+
z.strictObject({ kind: z.literal("decide"), text, scope: Scope }),
|
|
17
|
+
z.strictObject({ kind: z.literal("file-follow-up"), title: text, body: text }),
|
|
18
|
+
z.strictObject({ kind: z.literal("push-branch"), head: text }),
|
|
19
|
+
z.strictObject({ kind: z.literal("keep-evidence") }),
|
|
20
|
+
...(["land", "save-landing"] as const).map((kind) => z.strictObject({ kind: z.literal(kind), head: text })),
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
export type Intent = z.infer<typeof Intent>;
|
|
24
|
+
|
|
25
|
+
/** The writes a pass makes through its outbox, shaped as the work source's and the questions' own. */
|
|
26
|
+
export type PassWrites = {
|
|
27
|
+
readonly comment: (id: ItemId, text: string) => Promise<void>;
|
|
28
|
+
readonly decide: (id: ItemId, text: string) => Promise<void>;
|
|
29
|
+
readonly fileFollowUp: (from: ItemId, followUp: FollowUp) => Promise<void>;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** One pass's intents for its item, in the order the pass made them. */
|
|
33
|
+
export class Outbox {
|
|
34
|
+
readonly #intents: Intent[] = [];
|
|
35
|
+
|
|
36
|
+
constructor(readonly item: string) {}
|
|
37
|
+
|
|
38
|
+
add(intent: Intent): void {
|
|
39
|
+
this.#intents.push(Intent.parse(intent));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The writes of one run and attempt, each recorded as an intent publish applies under that scope. */
|
|
43
|
+
calls(scope: CallScope): PassWrites {
|
|
44
|
+
return {
|
|
45
|
+
comment: async (item, text) => this.add({ kind: "comment", text: this.#own(item, text), scope }),
|
|
46
|
+
decide: async (item, text) => this.add({ kind: "decide", text: this.#own(item, text), scope }),
|
|
47
|
+
fileFollowUp: async (from, { title, body }) => this.add({ kind: "file-follow-up", title: this.#own(from, title), body }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Takes every intent recorded so far, leaving the outbox empty. */
|
|
52
|
+
drain(): Intent[] {
|
|
53
|
+
return this.#intents.splice(0);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
#own<T>(item: string, value: T): T {
|
|
57
|
+
if (item !== this.item) throw new Error(`the outbox of ${this.item} takes no write for ${item}`);
|
|
58
|
+
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import pMemoize from "p-memoize";
|
|
5
|
+
import { ADAPTER_API_VERSION, type Item, ItemId, type Questions, type WorkSource } from "../adapter-api.ts";
|
|
6
|
+
import { type Git, gitIn, push } from "../adapters/git.ts";
|
|
7
|
+
import type { GitHubCommits } from "../adapters/github-commits.ts";
|
|
8
|
+
import type { ModelCall } from "../adapters/model-call.ts";
|
|
9
|
+
import { onceCalls } from "../adapters/once.ts";
|
|
10
|
+
import type { PullRequests } from "../adapters/pull-requests.ts";
|
|
11
|
+
import type { Config } from "../config/load.ts";
|
|
12
|
+
import { commandTimeoutMs } from "../exec.ts";
|
|
13
|
+
import type { State } from "../state/open.ts";
|
|
14
|
+
import { evidenceRef } from "../worker/evidence.ts";
|
|
15
|
+
import { land, type LandContext, prepareLanding, unpushed } from "../worker/land.ts";
|
|
16
|
+
import { releaseQuestions, roomToday } from "../worker/questions.ts";
|
|
17
|
+
import { pushRunBranch, signsCommits } from "../worker/signed-push.ts";
|
|
18
|
+
import { claimTtlMs, runBranch, type ThinResult } from "../worker/thin-path.ts";
|
|
19
|
+
import { type BundleCheck, type BundleRefs, checkBundle, OUTBOX_REFS } from "./bundle.ts";
|
|
20
|
+
import type { Intent } from "./intents.ts";
|
|
21
|
+
import { MEGABYTE } from "../bytes.ts";
|
|
22
|
+
|
|
23
|
+
type Scope = { readonly run: string; readonly attempt: number };
|
|
24
|
+
|
|
25
|
+
/** Applies one kind of intent; a result ends the pass's publish with it. */
|
|
26
|
+
type Appliers = { readonly [K in Intent["kind"]]: (intent: Extract<Intent, { kind: K }>) => Promise<ThinResult | undefined> };
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The order publish applies a pass's intents in: the change first, so a work source that keeps its threads on the
|
|
30
|
+
* base, as markdown does, moves the base only after the change landed on the base the pass checked it on.
|
|
31
|
+
*/
|
|
32
|
+
const APPLY_ORDER: readonly Intent["kind"][] = ["keep-evidence", "push-branch", "save-landing", "land", "file-follow-up", "comment", "decide"];
|
|
33
|
+
|
|
34
|
+
/** What gather saw for a pass, which publish checks the pass's bundle against: the base commit and each hum branch at its head. */
|
|
35
|
+
export type GatherTrust = { readonly baseSha: string; readonly heads: Readonly<Record<string, string>> };
|
|
36
|
+
|
|
37
|
+
/** One pass's outbox: its item, its intents in order, and the bundle of the refs it made, absent when it made none. */
|
|
38
|
+
export type PassOutbox = { readonly item: string; readonly intents: readonly Intent[]; readonly bundle: string | undefined };
|
|
39
|
+
|
|
40
|
+
/** Everything publish reads and writes. It runs none of the repo's code, and its model calls use no tool. */
|
|
41
|
+
export type Publisher = {
|
|
42
|
+
readonly state: State;
|
|
43
|
+
readonly config: Config;
|
|
44
|
+
readonly workSource: WorkSource;
|
|
45
|
+
readonly questions: Questions;
|
|
46
|
+
readonly pulls: PullRequests | undefined;
|
|
47
|
+
readonly commits: GitHubCommits | undefined;
|
|
48
|
+
readonly judge: ModelCall;
|
|
49
|
+
/** A review of a change from its item and diff alone, for a level that asks for one. */
|
|
50
|
+
readonly reviewRun: (item: Item, kind: string) => LandContext["reviewRun"];
|
|
51
|
+
/** The item's kind from what publish trusts: its labels, or the kind gather saved. */
|
|
52
|
+
readonly kindOf: (item: Item) => string;
|
|
53
|
+
readonly holder: Parameters<WorkSource["claim"]>[1];
|
|
54
|
+
readonly now: () => number;
|
|
55
|
+
/** The remote's URL or path. */
|
|
56
|
+
readonly remote: string;
|
|
57
|
+
readonly base: string;
|
|
58
|
+
/** The items folder, where a markdown item's file is marked done. */
|
|
59
|
+
readonly dir: string;
|
|
60
|
+
/** Publish's own repo: the remote's objects and each pass's bundle; no work tree of it runs anything. */
|
|
61
|
+
readonly repo: string;
|
|
62
|
+
/** The time past which publish starts no merge: its GitHub token's hour less `ciMergeMarginMinutes`; unset on a Mac. */
|
|
63
|
+
readonly mergesBefore?: number;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const timeoutOf = (p: Pick<Publisher, "config">): number => commandTimeoutMs(p.config.hum);
|
|
67
|
+
|
|
68
|
+
/** Makes publish's repo at `repo` with `remote` as `origin`, once: passes that open it at the same time share the one making. */
|
|
69
|
+
const makePublishRepo = pMemoize(
|
|
70
|
+
async (repo: string, remote: string, git: Git): Promise<void> => {
|
|
71
|
+
if (existsSync(join(repo, ".git"))) return;
|
|
72
|
+
|
|
73
|
+
mkdirSync(repo, { recursive: true });
|
|
74
|
+
await git.raw(["init", "-q"]);
|
|
75
|
+
await git.raw(["remote", "add", "origin", remote]);
|
|
76
|
+
},
|
|
77
|
+
{ cacheKey: ([repo]) => repo },
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
/** Publish's repo, made on first use, with the remote as `origin`. */
|
|
81
|
+
export const openPublishRepo = async (p: Pick<Publisher, "repo" | "remote" | "config">): Promise<Git> => {
|
|
82
|
+
const git = gitIn(p.repo, { identity: p.config.hum.commitIdentity, timeoutMs: timeoutOf(p) });
|
|
83
|
+
|
|
84
|
+
await makePublishRepo(p.repo, p.remote, git);
|
|
85
|
+
|
|
86
|
+
return git;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** Publish's repo with the remote's base, as `origin/<base>`, and the commits `heads` fetched into it. */
|
|
90
|
+
export const fetchedPublishRepo = async (p: Pick<Publisher, "repo" | "remote" | "config" | "base">, heads: readonly string[] = []): Promise<Git> => {
|
|
91
|
+
const git = await openPublishRepo(p);
|
|
92
|
+
|
|
93
|
+
await git.raw(["fetch", "-q", "origin", `+refs/heads/${p.base}:refs/remotes/origin/${p.base}`, ...heads]);
|
|
94
|
+
|
|
95
|
+
return git;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/** A worktree of publish's repo at `ref`, on `branch`, removed after `use`. Checking files out runs no hook and no filter of the repo. */
|
|
99
|
+
const onBranch = async <T>(git: Git, p: Publisher, { ref, branch }: { readonly ref: string; readonly branch: string }, use: (worktree: { readonly git: Git; readonly folder: string }) => Promise<T>): Promise<T> => {
|
|
100
|
+
const folder = mkdtempSync(join(tmpdir(), "hum-publish-"));
|
|
101
|
+
|
|
102
|
+
await git.raw(["worktree", "add", "-q", "-B", branch, folder, ref]);
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
return await use({ git: gitIn(folder, { identity: p.config.hum.commitIdentity, timeoutMs: timeoutOf(p) }), folder });
|
|
106
|
+
} finally {
|
|
107
|
+
await git.run(["worktree", "remove", "--force", folder]);
|
|
108
|
+
await git.run(["branch", "-D", branch]);
|
|
109
|
+
rmSync(folder, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const outboxRef = (ref: string): string => `${OUTBOX_REFS}${ref.slice("refs/".length)}`;
|
|
114
|
+
|
|
115
|
+
const landContext = (p: Publisher, item: Item, { git, folder }: { readonly git: Git; readonly folder: string }, branch: string): LandContext => ({
|
|
116
|
+
state: p.state,
|
|
117
|
+
config: p.config,
|
|
118
|
+
kind: p.kindOf(item),
|
|
119
|
+
id: item.id,
|
|
120
|
+
item,
|
|
121
|
+
branch,
|
|
122
|
+
base: p.base,
|
|
123
|
+
remote: p.remote,
|
|
124
|
+
clone: folder,
|
|
125
|
+
dir: p.dir,
|
|
126
|
+
git,
|
|
127
|
+
workSource: p.workSource,
|
|
128
|
+
questions: p.questions,
|
|
129
|
+
pulls: p.pulls,
|
|
130
|
+
commits: p.commits,
|
|
131
|
+
now: p.now,
|
|
132
|
+
judge: p.judge,
|
|
133
|
+
reviewRun: p.reviewRun(item, p.kindOf(item)),
|
|
134
|
+
mergesBefore: p.mergesBefore,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const NOTHING_BUNDLED: BundleCheck = { ok: true, refs: new Map() };
|
|
138
|
+
|
|
139
|
+
/** A pass publish refused before any write, and why. */
|
|
140
|
+
export const refusedPass = (item: string, why: string): ThinResult => ({ outcome: "stuck", item, detail: `publish refused the pass: ${why}` });
|
|
141
|
+
|
|
142
|
+
/** The bytes of every file the tree at `commit` holds, which a worktree of it writes to disk. */
|
|
143
|
+
const treeBytes = async (git: Git, commit: string): Promise<number> =>
|
|
144
|
+
(await git.raw(["ls-tree", "-r", "-l", commit])).split("\n").reduce((total, line) => total + (Number(line.split(/\s+/)[3]) || 0), 0);
|
|
145
|
+
|
|
146
|
+
type PassCheck = { readonly p: Publisher; readonly git: Git; readonly trust: GatherTrust; readonly pass: PassOutbox; readonly refs: BundleRefs; readonly branch: string };
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Why publish refuses a pass whose bundle passed, before any write: an intent names a head that is neither its
|
|
150
|
+
* bundle's tip of the item's run branch, gather's base, nor that branch's head as gather saw it; it keeps evidence
|
|
151
|
+
* its bundle does not hold; it files more follow-ups than `followUpsPerPass` and the daily question cap leave room
|
|
152
|
+
* for; or a head's tree is larger than `publishTreeMaxMegabytes`.
|
|
153
|
+
*/
|
|
154
|
+
const passFault = async ({ p, git, trust, pass, refs, branch }: PassCheck): Promise<string | undefined> => {
|
|
155
|
+
const heads = [...new Set(pass.intents.flatMap((intent) => ("head" in intent ? [intent.head] : [])))];
|
|
156
|
+
const known = new Set([refs.get(`refs/heads/${branch}`), trust.baseSha, trust.heads[branch]]);
|
|
157
|
+
const stranger = heads.find((head) => !known.has(head));
|
|
158
|
+
|
|
159
|
+
if (stranger !== undefined) return `it names ${stranger} as the head of ${branch}, which is neither its bundle's tip, the base gather saw, nor the head gather saw for ${branch}`;
|
|
160
|
+
|
|
161
|
+
const evidence = evidenceRef(pass.item);
|
|
162
|
+
|
|
163
|
+
if (pass.intents.some(({ kind }) => kind === "keep-evidence") && !refs.has(evidence)) return `it asks to keep evidence its bundle does not hold on ${evidence}`;
|
|
164
|
+
|
|
165
|
+
const followUps = pass.intents.filter(({ kind }) => kind === "file-follow-up").length;
|
|
166
|
+
const room = Math.min(p.config.hum.followUpsPerPass, roomToday(p));
|
|
167
|
+
|
|
168
|
+
if (followUps > room) return `it files ${followUps} follow-ups, and followUpsPerPass and the daily question cap leave room for ${room}`;
|
|
169
|
+
|
|
170
|
+
const cap = p.config.hum.publishTreeMaxMegabytes * MEGABYTE;
|
|
171
|
+
|
|
172
|
+
for (const head of heads) {
|
|
173
|
+
const bytes = await treeBytes(git, head);
|
|
174
|
+
|
|
175
|
+
if (bytes > cap) return `the tree at ${head} holds ${bytes} bytes, over publishTreeMaxMegabytes (${p.config.hum.publishTreeMaxMegabytes})`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return undefined;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Applies one pass's outbox: the bundle must pass every check and the pass every check of its intents, before any
|
|
183
|
+
* write; then the claim must still be the worker's, each intent runs in order under its op key, and the questions the
|
|
184
|
+
* daily cap has room for go out. A landing's result is the pass's result; nothing else decides the item.
|
|
185
|
+
*/
|
|
186
|
+
export const publishPass = async (p: Publisher, trust: GatherTrust, pass: PassOutbox): Promise<ThinResult | undefined> => {
|
|
187
|
+
const id = ItemId.parse(pass.item);
|
|
188
|
+
const heads = Object.values(trust.heads);
|
|
189
|
+
const git = await fetchedPublishRepo(p, heads);
|
|
190
|
+
const branch = runBranch(p.config.hum, pass.item);
|
|
191
|
+
|
|
192
|
+
const checked = pass.bundle === undefined ? NOTHING_BUNDLED : await checkBundle(git, pass.bundle, {
|
|
193
|
+
base: trust.baseSha,
|
|
194
|
+
heads,
|
|
195
|
+
identity: p.config.hum.commitIdentity,
|
|
196
|
+
branchPrefixes: [p.config.hum.runBranchPrefix, p.config.hum.ownerBranchPrefix],
|
|
197
|
+
signs: await signsCommits({ pulls: p.pulls, base: p.base }),
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (!checked.ok) return refusedPass(pass.item, checked.why);
|
|
201
|
+
|
|
202
|
+
const fault = await passFault({ p, git, trust, pass, refs: checked.refs, branch });
|
|
203
|
+
|
|
204
|
+
if (fault !== undefined) return refusedPass(pass.item, fault);
|
|
205
|
+
|
|
206
|
+
if (!(await p.workSource.renew(id, p.holder, claimTtlMs(p.config))).won) return { outcome: "lost", item: pass.item };
|
|
207
|
+
|
|
208
|
+
const calls = (scope: Scope) => onceCalls({ apiVersion: ADAPTER_API_VERSION, workSource: p.workSource, questions: p.questions }, p.state.ops, scope);
|
|
209
|
+
const item = await p.workSource.read(id);
|
|
210
|
+
const onRunBranch = (head: string) => ({ ref: head, branch });
|
|
211
|
+
|
|
212
|
+
/** Saves the change on the run branch as the item's landing, then goes on with it. */
|
|
213
|
+
const landing = (head: string, then: (ctx: LandContext) => Promise<ThinResult | undefined>) =>
|
|
214
|
+
onBranch(git, p, onRunBranch(head), async (worktree) => {
|
|
215
|
+
const ctx = landContext(p, item, worktree, branch);
|
|
216
|
+
const pushed = await prepareLanding(ctx);
|
|
217
|
+
|
|
218
|
+
return pushed.kind === "pushed" ? then(ctx) : unpushed(pass.item, branch, pushed);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
const apply = {
|
|
222
|
+
comment: async ({ text, scope }) => void (await calls(scope).workSource?.comment(id, text)),
|
|
223
|
+
decide: async ({ text, scope }) => void (await calls(scope).questions?.decide(id, text)),
|
|
224
|
+
"file-follow-up": async ({ title, body }) => {
|
|
225
|
+
const filed = await p.workSource.fileFollowUp(id, { title, body });
|
|
226
|
+
|
|
227
|
+
p.state.followUps.put(filed.id, { item: filed.id, from: pass.item, at: p.now() });
|
|
228
|
+
|
|
229
|
+
return undefined;
|
|
230
|
+
},
|
|
231
|
+
"keep-evidence": async () => {
|
|
232
|
+
const ref = evidenceRef(pass.item);
|
|
233
|
+
const pushed = await push(git, ["--force", "origin", `${outboxRef(ref)}:${ref}`]);
|
|
234
|
+
|
|
235
|
+
return pushed.kind === "refused" ? unpushed(pass.item, ref, pushed) : undefined;
|
|
236
|
+
},
|
|
237
|
+
"push-branch": async ({ head }) => {
|
|
238
|
+
const pushed = await onBranch(git, p, onRunBranch(head), (worktree) => pushRunBranch({ git: worktree.git, base: p.base, pulls: p.pulls, commits: p.commits }, branch));
|
|
239
|
+
|
|
240
|
+
return pushed.kind === "pushed" ? undefined : unpushed(pass.item, branch, pushed);
|
|
241
|
+
},
|
|
242
|
+
"save-landing": async ({ head }) => landing(head, async () => undefined),
|
|
243
|
+
land: async ({ head }) => landing(head, land),
|
|
244
|
+
} satisfies Appliers;
|
|
245
|
+
|
|
246
|
+
let result: ThinResult | undefined;
|
|
247
|
+
|
|
248
|
+
for (const intent of pass.intents.toSorted((a, b) => APPLY_ORDER.indexOf(a.kind) - APPLY_ORDER.indexOf(b.kind))) {
|
|
249
|
+
// SAFETY: each handler takes the variant its key names.
|
|
250
|
+
result = (await (apply[intent.kind] as (intent: Intent) => Promise<ThinResult | undefined>)(intent)) ?? result;
|
|
251
|
+
|
|
252
|
+
if (result !== undefined && result.outcome !== "landing" && result.outcome !== "merged" && result.outcome !== "review") break;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
await releaseQuestions({ state: p.state, config: p.config, workSource: p.workSource, questions: p.questions, now: p.now });
|
|
256
|
+
|
|
257
|
+
return result;
|
|
258
|
+
};
|
|
259
|
+
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { ItemId } from "../adapter-api.ts";
|
|
2
|
+
import type { State } from "../state/open.ts";
|
|
3
|
+
import { HOUR_MS } from "../time.ts";
|
|
4
|
+
import { forgetProgress } from "../worker/kind.ts";
|
|
5
|
+
import { releaseQuestions } from "../worker/questions.ts";
|
|
6
|
+
import { answerTo, park, type ThinResult } from "../worker/thin-path.ts";
|
|
7
|
+
import { kindMoveFault, takeWorkRecords } from "./handoff.ts";
|
|
8
|
+
import type { Gathered, OutboxPass, ReadOutbox } from "./handover.ts";
|
|
9
|
+
import { fetchedPublishRepo, type Publisher, publishPass, refusedPass } from "./publisher.ts";
|
|
10
|
+
import { settleBaseCheck } from "../worker/base-check.ts";
|
|
11
|
+
|
|
12
|
+
/** What publish works from in a CI wake: its own root, what gather settled, the work job's outbox and its state, and how it saves its own state. */
|
|
13
|
+
export type PublishWake = { readonly publisher: Publisher; readonly gathered: Gathered; readonly outbox: ReadOutbox; readonly work: State; readonly save: () => Promise<void> };
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Publishes one pass of a work job: the work job's own records for the item, a kind it moved the item to only where
|
|
17
|
+
* that kind is another whose risk floor is no lower, kept a fallback where publish's was one, then the pass's outbox,
|
|
18
|
+
* checked as on a Mac. The pass's result is the landing's, else the work job's own, which never merges.
|
|
19
|
+
*/
|
|
20
|
+
const publishWorked = async ({ publisher: p, gathered, outbox, work }: PublishWake, pass: OutboxPass): Promise<ThinResult> => {
|
|
21
|
+
const known = p.state.itemKinds.get(pass.item);
|
|
22
|
+
const kind = known && p.config.kinds[known.kind];
|
|
23
|
+
|
|
24
|
+
if (known === undefined || kind === undefined) return refusedPass(pass.item, `publish holds no kind for ${pass.item}`);
|
|
25
|
+
|
|
26
|
+
const item = await p.workSource.read(ItemId.parse(pass.item));
|
|
27
|
+
const at = p.state.items.saved().find(({ itemId }) => itemId === pass.item)?.state;
|
|
28
|
+
const refused = takeWorkRecords(work, p.state, { item: pass.item, kind, at, answered: answerTo(item) !== undefined }, { now: p.now(), usageLimitMaxMs: p.config.hum.ciUsageLimitMaxHours * HOUR_MS });
|
|
29
|
+
|
|
30
|
+
if (refused !== undefined) return refusedPass(pass.item, refused);
|
|
31
|
+
|
|
32
|
+
if (pass.result.outcome === "moved") {
|
|
33
|
+
const fault = kindMoveFault(p.config, { from: known.kind, to: pass.result.kind });
|
|
34
|
+
|
|
35
|
+
if (fault !== undefined) return refusedPass(pass.item, fault);
|
|
36
|
+
|
|
37
|
+
forgetProgress(p.state, pass.item);
|
|
38
|
+
p.state.itemKinds.put(pass.item, known.from === "fallback" ? { ...known, kind: pass.result.kind } : { item: pass.item, kind: pass.result.kind, from: "model" });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const published = await publishPass(p, { baseSha: gathered.baseSha, heads: gathered.heads }, { item: pass.item, intents: pass.intents, bundle: outbox.bundleOf(pass) });
|
|
42
|
+
|
|
43
|
+
return published ?? pass.result;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Settles the base check the work job ran, so its breaks place every pass after it, publishes every pass of its
|
|
48
|
+
* outbox in order, then asks the questions the daily cap has room for. A pass that throws ends refused, and the state
|
|
49
|
+
* is saved after each pass and once more however the wake ends, so what one pass wrote is never written again.
|
|
50
|
+
*/
|
|
51
|
+
export const publishWake = async (wake: PublishWake): Promise<ThinResult[]> => {
|
|
52
|
+
const { publisher: p } = wake;
|
|
53
|
+
const results: ThinResult[] = [];
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const { baseCheck } = wake.outbox;
|
|
57
|
+
|
|
58
|
+
if (baseCheck !== undefined) {
|
|
59
|
+
await settleBaseCheck({ state: p.state, config: p.config, workSource: p.workSource, git: await fetchedPublishRepo(p), base: p.base, now: p.now }, baseCheck);
|
|
60
|
+
await wake.save();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const pass of wake.outbox.passes) {
|
|
64
|
+
const result = park(p, await publishWorked(wake, pass).catch((error: Error) => refusedPass(pass.item, error.message)));
|
|
65
|
+
|
|
66
|
+
if (result.outcome === "merged") p.state.items.remove(pass.item);
|
|
67
|
+
|
|
68
|
+
results.push(result);
|
|
69
|
+
await wake.save();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
await releaseQuestions({ state: p.state, config: p.config, workSource: p.workSource, questions: p.questions, now: p.now });
|
|
73
|
+
|
|
74
|
+
return results;
|
|
75
|
+
} finally {
|
|
76
|
+
await wake.save();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { type GitIdentity, gitIn, push } from "../adapters/git.ts";
|
|
5
|
+
import { HEARTBEAT_FILE, INBOX_DIR, PUBLISH_DIR, SLOTS_DIR, STATUS_FILE, STOPS_DIR } from "../state/open.ts";
|
|
6
|
+
|
|
7
|
+
/** Where a CI wake keeps hum's state for the next one. A ref holds no file, so no branch or checkout changes. Anyone who can read the repo can read it. */
|
|
8
|
+
export const CI_STATE_REF = "refs/hum/state";
|
|
9
|
+
|
|
10
|
+
/** The state files a job keeps to itself: what the view and the service unit poll, what a Mac's hooks and slots leave, and publish's repo. */
|
|
11
|
+
const JOB_ONLY = [STATUS_FILE, HEARTBEAT_FILE, `${INBOX_DIR}/`, `${STOPS_DIR}/`, `${SLOTS_DIR}/`, `${PUBLISH_DIR}/`];
|
|
12
|
+
|
|
13
|
+
const isJobOnly = (path: string): boolean => JOB_ONLY.some((name) => (name.endsWith("/") ? path.startsWith(name) : path === name));
|
|
14
|
+
|
|
15
|
+
const NO_REF = /couldn't find remote ref/i;
|
|
16
|
+
|
|
17
|
+
type CiStateOptions = {
|
|
18
|
+
/** The checkout whose git holds the objects. */
|
|
19
|
+
readonly repo: string;
|
|
20
|
+
readonly remote: string;
|
|
21
|
+
/** The state folder. */
|
|
22
|
+
readonly dir: string;
|
|
23
|
+
readonly timeoutMs: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** The state commit a wake read, which its save must replace; undefined where no wake saved before. */
|
|
27
|
+
export type CiStateRead = { readonly read: string | undefined };
|
|
28
|
+
|
|
29
|
+
/** A save lost the race: another wake saved after this one read, and hum never writes over that. */
|
|
30
|
+
export class CiStateRaced extends Error {
|
|
31
|
+
override name = "CiStateRaced";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Runs `use` with a scratch git index, removed after. */
|
|
35
|
+
const withScratchIndex = async <T>(use: (env: { readonly GIT_INDEX_FILE: string }) => Promise<T>): Promise<T> => {
|
|
36
|
+
const scratch = mkdtempSync(join(tmpdir(), "hum-state-index-"));
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
return await use({ GIT_INDEX_FILE: join(scratch, "index") });
|
|
40
|
+
} finally {
|
|
41
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Writes the state the last wake saved into `dir`, and returns the commit read; a remote that holds none leaves `dir` as it is. */
|
|
46
|
+
export const restoreCiState = async ({ repo, remote, dir, timeoutMs }: CiStateOptions): Promise<CiStateRead> => {
|
|
47
|
+
const git = gitIn(repo, { timeoutMs });
|
|
48
|
+
const fetched = await git.run(["fetch", "--quiet", "--no-tags", remote, `+${CI_STATE_REF}:${CI_STATE_REF}`]);
|
|
49
|
+
|
|
50
|
+
if (fetched.code !== 0 && NO_REF.test(fetched.stderr)) return { read: undefined };
|
|
51
|
+
|
|
52
|
+
if (fetched.code !== 0) throw new Error(`hum could not read its saved state from ${remote}: ${fetched.stderr.trim()}`);
|
|
53
|
+
|
|
54
|
+
const read = (await git.raw(["rev-parse", CI_STATE_REF])).trim();
|
|
55
|
+
|
|
56
|
+
mkdirSync(dir, { recursive: true });
|
|
57
|
+
await withScratchIndex(async (env) => {
|
|
58
|
+
const indexed = gitIn(repo, { timeoutMs, env });
|
|
59
|
+
|
|
60
|
+
await indexed.raw(["read-tree", read]);
|
|
61
|
+
await indexed.raw(["checkout-index", "--all", "--force", `--prefix=${join(dir, "/")}`]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return { read };
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** The tree of every state file the next wake needs, built through a scratch index so no work tree is touched. */
|
|
68
|
+
const stateTree = async ({ repo, dir, timeoutMs }: Omit<CiStateOptions, "remote">): Promise<string> => {
|
|
69
|
+
const git = gitIn(repo, { timeoutMs });
|
|
70
|
+
const files = existsSync(dir) ? [...new Bun.Glob("**").scanSync({ cwd: dir, onlyFiles: true, dot: true })].filter((path) => !isJobOnly(path)).sort() : [];
|
|
71
|
+
|
|
72
|
+
if (files.length === 0) return (await git.raw(["hash-object", "-w", "-t", "tree", "/dev/null"])).trim();
|
|
73
|
+
|
|
74
|
+
const blobs = (await git.raw(["hash-object", "-w", "--stdin-paths"], `${files.map((path) => join(dir, path)).join("\n")}\n`)).trim().split("\n");
|
|
75
|
+
|
|
76
|
+
return withScratchIndex(async (env) => {
|
|
77
|
+
const indexed = gitIn(repo, { timeoutMs, env });
|
|
78
|
+
|
|
79
|
+
await indexed.raw(["update-index", "--add", "--index-info"], files.map((path, at) => `100644 ${blobs[at]}\t${path}\n`).join(""));
|
|
80
|
+
|
|
81
|
+
return (await indexed.raw(["write-tree"])).trim();
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Saves the state folder to the remote as one commit with no parent, so the ref never grows, in place of the commit
|
|
87
|
+
* this wake read: a compare-and-swap, which throws `CiStateRaced` when another wake saved first. Returns the new read.
|
|
88
|
+
*/
|
|
89
|
+
export const saveCiState = async ({ read, identity, at, ...options }: CiStateOptions & CiStateRead & { readonly identity: GitIdentity; readonly at: number }): Promise<CiStateRead> => {
|
|
90
|
+
const git = gitIn(options.repo, { identity, timeoutMs: options.timeoutMs });
|
|
91
|
+
const commit = (await git.raw(["commit-tree", await stateTree(options), "-m", `hum state ${new Date(at).toISOString()}`])).trim();
|
|
92
|
+
const pushed = await push(git, [`--force-with-lease=${CI_STATE_REF}:${read ?? ""}`, options.remote, `${commit}:${CI_STATE_REF}`]);
|
|
93
|
+
|
|
94
|
+
if (pushed.kind === "conflict") throw new CiStateRaced(`another wake saved hum's state to ${CI_STATE_REF} after this one read it, so this wake's state was not saved`);
|
|
95
|
+
|
|
96
|
+
if (pushed.kind === "refused") throw new Error(`${options.remote} refused hum's state at ${CI_STATE_REF}: ${pushed.said}`);
|
|
97
|
+
|
|
98
|
+
return { read: commit };
|
|
99
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, readFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { gitIn, whenGitFailed } from "../adapters/git.ts";
|
|
6
|
+
import { type Env, exec, SHIPPED_COMMAND_TIMEOUT_MS } from "../exec.ts";
|
|
7
|
+
|
|
8
|
+
/** The marketplace hope comes from, by the name its own manifest gives it. */
|
|
9
|
+
const MOO_MARKETPLACE = "moo.md";
|
|
10
|
+
|
|
11
|
+
const MOO_REPO = "https://github.com/saadshahd/moo.md";
|
|
12
|
+
|
|
13
|
+
const HOPE_PLUGIN = `hope@${MOO_MARKETPLACE}`;
|
|
14
|
+
|
|
15
|
+
/** The engine's folder: `hum-engine` in a moo.md checkout, `node_modules/@moo.md/hum` in an owner's repo. */
|
|
16
|
+
const ENGINE_ROOT = resolve(import.meta.dir, "..", "..");
|
|
17
|
+
|
|
18
|
+
/** The file the publish job writes beside the package: the moo.md commit it was published from. */
|
|
19
|
+
export const RELEASE_FILE = "release.json";
|
|
20
|
+
|
|
21
|
+
/** What `release.json` holds: the full sha of the moo.md commit the package was published from. */
|
|
22
|
+
export const Release = z.object({ commit: z.string().regex(/^[0-9a-f]{40}$/) });
|
|
23
|
+
|
|
24
|
+
/** A marketplace as a settings file declares it, in the forms `claude plugin marketplace add` takes. */
|
|
25
|
+
const Source = z.discriminatedUnion("source", [
|
|
26
|
+
z.looseObject({ source: z.literal("github"), repo: z.string().min(1), ref: z.string().min(1).optional(), path: z.never().optional() }),
|
|
27
|
+
z.looseObject({ source: z.literal("git"), url: z.string().min(1), ref: z.string().min(1).optional(), path: z.never().optional() }),
|
|
28
|
+
z.looseObject({ source: z.literal("url"), url: z.string().min(1) }),
|
|
29
|
+
z.looseObject({ source: z.literal("directory"), path: z.string().min(1) }),
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
/** What a repo's `.claude/settings.json` says about plugins; every other key is Claude Code's alone. */
|
|
33
|
+
export const RepoPlugins = z.looseObject({
|
|
34
|
+
extraKnownMarketplaces: z.record(z.string(), z.looseObject({ source: Source })).default({}),
|
|
35
|
+
enabledPlugins: z.record(z.string(), z.unknown()).default({}),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export type RepoPlugins = z.infer<typeof RepoPlugins>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The moo.md commit this engine shipped with: the one its package was published from, or, where the engine runs from
|
|
42
|
+
* a moo.md checkout, that checkout's HEAD. Any other engine has none, and that throws: an unpinned hope could be one
|
|
43
|
+
* this engine was never released beside.
|
|
44
|
+
*/
|
|
45
|
+
export const shippedCommit = async (): Promise<string> => {
|
|
46
|
+
const file = join(ENGINE_ROOT, RELEASE_FILE);
|
|
47
|
+
|
|
48
|
+
if (existsSync(file)) return Release.parse(JSON.parse(readFileSync(file, "utf8"))).commit;
|
|
49
|
+
|
|
50
|
+
const git = gitIn(ENGINE_ROOT, { timeoutMs: SHIPPED_COMMAND_TIMEOUT_MS });
|
|
51
|
+
const top = (await git.raw(["rev-parse", "--show-toplevel"]).catch(whenGitFailed(""))).trim();
|
|
52
|
+
|
|
53
|
+
if (top === "" || resolve(top) !== resolve(ENGINE_ROOT, "..")) throw new Error(`this engine holds no ${RELEASE_FILE} and sits in no moo.md checkout, so it names no moo.md commit to install hope from`);
|
|
54
|
+
|
|
55
|
+
return (await git.raw(["rev-parse", "HEAD"])).trim();
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const marketplaceArgument = (source: z.infer<typeof Source>, repo: string): string => {
|
|
59
|
+
switch (source.source) {
|
|
60
|
+
case "github":
|
|
61
|
+
return source.ref === undefined ? source.repo : `${source.repo}#${source.ref}`;
|
|
62
|
+
case "git":
|
|
63
|
+
return source.ref === undefined ? source.url : `${source.url}#${source.ref}`;
|
|
64
|
+
case "url":
|
|
65
|
+
return source.url;
|
|
66
|
+
case "directory":
|
|
67
|
+
return isAbsolute(source.path) ? source.path : join(repo, source.path);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const COMMIT_SHA = /^[0-9a-f]{40}$/;
|
|
72
|
+
|
|
73
|
+
/** A marketplace fetched from a repo by anything but a commit SHA installs whatever its branch or tag holds at the wake. */
|
|
74
|
+
const isUnpinned = (source: z.infer<typeof Source>): boolean => (source.source === "github" || source.source === "git") && !COMMIT_SHA.test(source.ref ?? "");
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The `claude` command lines that install hope and the repo's plugins: moo.md from `mooFolder`, its checkout at the
|
|
78
|
+
* shipped commit, which stands in for any moo.md the settings declare; each other marketplace they declare; then hope
|
|
79
|
+
* and every plugin they enable. A marketplace from a repo whose `ref` is no full commit SHA throws: its hooks would run
|
|
80
|
+
* in every run as whatever its branch or tag holds that day.
|
|
81
|
+
*/
|
|
82
|
+
export const pluginCommands = (settings: RepoPlugins, { repo, mooFolder }: { readonly repo: string; readonly mooFolder: string }): string[][] => {
|
|
83
|
+
const declared = Object.entries(settings.extraKnownMarketplaces).filter(([name]) => name !== MOO_MARKETPLACE);
|
|
84
|
+
const unpinned = declared.flatMap(([name, { source }]) => (isUnpinned(source) ? [name] : []));
|
|
85
|
+
|
|
86
|
+
if (unpinned.length > 0) throw new Error(`the marketplaces ${unpinned.join(", ")} in .claude/settings.json name no commit SHA as "ref", so a wake would install whatever their branch or tag holds, and their hooks run in every run. Give each a "ref" of a full 40-character commit SHA, which no push can move.`);
|
|
87
|
+
|
|
88
|
+
const marketplaces = declared.map(([, { source }]) => marketplaceArgument(source, repo));
|
|
89
|
+
const enabled = Object.entries(settings.enabledPlugins).flatMap(([id, on]) => (on === true ? [id] : []));
|
|
90
|
+
|
|
91
|
+
return [mooFolder, ...marketplaces].map((source) => ["plugin", "marketplace", "add", source]).concat([...new Set([HOPE_PLUGIN, ...enabled])].map((id) => ["plugin", "install", id]));
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** The plugin settings of the repo's `.claude/settings.json`; none where it has no such file. */
|
|
95
|
+
export const repoPlugins = (repo: string): RepoPlugins => {
|
|
96
|
+
const file = join(repo, ".claude", "settings.json");
|
|
97
|
+
|
|
98
|
+
return RepoPlugins.parse(existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : {});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** moo.md at `commit`, fetched alone into a new folder under `parent`. */
|
|
102
|
+
const checkOutMoo = async (commit: string, parent: string): Promise<string> => {
|
|
103
|
+
const folder = mkdtempSync(join(parent, "hum-moo-"));
|
|
104
|
+
const git = gitIn(folder, { timeoutMs: SHIPPED_COMMAND_TIMEOUT_MS });
|
|
105
|
+
|
|
106
|
+
await git.raw(["init", "-q"]);
|
|
107
|
+
await git.raw(["fetch", "-q", "--depth", "1", MOO_REPO, commit]);
|
|
108
|
+
await git.raw(["checkout", "-q", "FETCH_HEAD"]);
|
|
109
|
+
|
|
110
|
+
return folder;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export type PluginInstall = {
|
|
114
|
+
readonly repo: string;
|
|
115
|
+
readonly env: Env;
|
|
116
|
+
/** Runs `claude` with these arguments; the CLI runs the real binary. */
|
|
117
|
+
readonly claude?: (args: readonly string[]) => Promise<{ readonly exitCode: number | undefined; readonly stdout: string; readonly stderr: string }>;
|
|
118
|
+
/** The moo.md folder to install from; by default a checkout of the shipped commit. */
|
|
119
|
+
readonly mooFolder?: string;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Installs hope and the repo's plugins for Claude Code in this job. A fresh checkout is a folder Claude Code does not
|
|
124
|
+
* trust, so it skips the marketplaces the repo's settings declare; this adds them as the user's own. The first command
|
|
125
|
+
* that fails throws, naming itself and what claude said.
|
|
126
|
+
*/
|
|
127
|
+
export const installPlugins = async ({ repo, env, claude = (args) => exec("claude", args, { throwOnError: false, timeoutMs: SHIPPED_COMMAND_TIMEOUT_MS, nodeOptions: { env } }), mooFolder }: PluginInstall): Promise<readonly string[][]> => {
|
|
128
|
+
const commands = pluginCommands(repoPlugins(repo), { repo, mooFolder: mooFolder ?? (await checkOutMoo(await shippedCommit(), env.RUNNER_TEMP ?? tmpdir())) });
|
|
129
|
+
|
|
130
|
+
for (const args of commands) {
|
|
131
|
+
const ran = await claude(args);
|
|
132
|
+
|
|
133
|
+
if (ran.exitCode !== 0) throw new Error(`claude ${args.join(" ")} exited ${ran.exitCode}: ${(ran.stderr || ran.stdout).trim()}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return commands;
|
|
137
|
+
};
|
package/src/service/wake.ts
CHANGED
|
@@ -5,12 +5,14 @@ import type { Worker } from "../worker/worker.ts";
|
|
|
5
5
|
/**
|
|
6
6
|
* CI wake: ticks, lets the runs it started end, and ticks again until a tick starts nothing. A CI job holds no
|
|
7
7
|
* session across a wait for the owner: a run that needs them has pushed its branch, asked on the item and ended.
|
|
8
|
+
* `afterTick` runs once each tick's runs have ended, so a job stopped at its time limit loses only the tick in flight.
|
|
8
9
|
*/
|
|
9
|
-
export const wakeUntilQuiet = async (worker: Worker): Promise<void> => {
|
|
10
|
+
export const wakeUntilQuiet = async (worker: Pick<Worker, "tick" | "idle">, afterTick: () => Promise<void> = async () => {}): Promise<void> => {
|
|
10
11
|
for (;;) {
|
|
11
12
|
const result = await worker.tick();
|
|
12
13
|
|
|
13
14
|
await worker.idle();
|
|
15
|
+
await afterTick();
|
|
14
16
|
|
|
15
17
|
if (result.kind !== "ran" || result.started.length === 0) return;
|
|
16
18
|
}
|