@moo.md/hum 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +28 -33
- 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 +22 -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 +126 -30
- 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,182 @@
|
|
|
1
|
+
import { cpSync, existsSync, lstatSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { type ClaimResult, Holder, Item, ItemId, type Questions, type WorkSource } from "../adapter-api.ts";
|
|
5
|
+
import { gitIn } from "../adapters/git.ts";
|
|
6
|
+
import type { ThinResult } from "../worker/thin-path.ts";
|
|
7
|
+
import type { BaseCheck } from "../worker/base-check.ts";
|
|
8
|
+
import { Intent } from "./intents.ts";
|
|
9
|
+
|
|
10
|
+
const text = z.string().min(1);
|
|
11
|
+
|
|
12
|
+
const sha = z.string().regex(/^[0-9a-f]{40}$/);
|
|
13
|
+
|
|
14
|
+
/** Where an inbox or outbox keeps the state folder, beside its JSON file. */
|
|
15
|
+
const STATE_FOLDER = "state";
|
|
16
|
+
|
|
17
|
+
const INBOX_FILE = "inbox.json";
|
|
18
|
+
|
|
19
|
+
const OUTBOX_FILE = "outbox.json";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What gather settled for a wake, which publish trusts and reads from gather's own job outputs, never from an
|
|
23
|
+
* artifact the work job could replace: who claimed, the base and hum branch heads it saw, the items it claimed, and
|
|
24
|
+
* the state commit it saved.
|
|
25
|
+
*/
|
|
26
|
+
export const Gathered = z.strictObject({
|
|
27
|
+
holder: Holder,
|
|
28
|
+
base: text,
|
|
29
|
+
baseSha: sha,
|
|
30
|
+
/** Each hum branch gather saw, at its head. */
|
|
31
|
+
heads: z.record(text, sha),
|
|
32
|
+
claimed: z.array(ItemId),
|
|
33
|
+
/** The state commit gather saved, which publish must find still there and replaces; absent when it saved none. */
|
|
34
|
+
stateRead: sha.optional(),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
export type Gathered = z.infer<typeof Gathered>;
|
|
38
|
+
|
|
39
|
+
/** What gather hands the work job, which holds no token: what it settled, and each item it claimed as it read it last. */
|
|
40
|
+
export const Inbox = z.strictObject({ gathered: Gathered, items: z.array(Item) });
|
|
41
|
+
|
|
42
|
+
export type Inbox = z.infer<typeof Inbox>;
|
|
43
|
+
|
|
44
|
+
/** A pass's result as the work job reports it; publish alone merges or lands, so it takes neither from the work job. */
|
|
45
|
+
const WorkResult = z.discriminatedUnion("outcome", [
|
|
46
|
+
z.strictObject({ outcome: z.enum(["lost", "waiting"]), item: text }),
|
|
47
|
+
z.strictObject({ outcome: z.literal("moved"), item: text, kind: text }),
|
|
48
|
+
z.strictObject({ outcome: z.enum(["asked", "stuck", "review"]), item: text, detail: z.string().optional() }),
|
|
49
|
+
z.strictObject({ outcome: z.literal("limited"), item: text, until: z.int().nonnegative() }),
|
|
50
|
+
z.strictObject({ outcome: z.literal("held"), item: text, why: z.string() }),
|
|
51
|
+
]);
|
|
52
|
+
|
|
53
|
+
export type WorkResult = z.infer<typeof WorkResult>;
|
|
54
|
+
|
|
55
|
+
const BUNDLE_NAME = /^[\w.-]+\.bundle$/;
|
|
56
|
+
|
|
57
|
+
/** One pass the work job ran: its item, its result, what it asks publish to do and the bundle file it made. */
|
|
58
|
+
const OutboxPass = z.strictObject({ item: ItemId, result: WorkResult, intents: z.array(Intent), bundle: z.string().regex(BUNDLE_NAME).optional() });
|
|
59
|
+
|
|
60
|
+
/** The repo's checks the work job ran on gather's base for the merges no check had covered: their failure, absent when they passed. */
|
|
61
|
+
const BaseCheckReport = z.strictObject({ items: z.array(ItemId).min(1), failure: z.string().optional() });
|
|
62
|
+
|
|
63
|
+
export const OutboxFile = z.strictObject({ passes: z.array(OutboxPass), baseCheck: BaseCheckReport.optional() });
|
|
64
|
+
|
|
65
|
+
type OutboxFile = z.infer<typeof OutboxFile>;
|
|
66
|
+
|
|
67
|
+
const readOutboxFile = (folder: string): OutboxFile => {
|
|
68
|
+
const file = join(folder, OUTBOX_FILE);
|
|
69
|
+
|
|
70
|
+
return existsSync(file) ? OutboxFile.parse(JSON.parse(readFileSync(file, "utf8"))) : { passes: [] };
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const writeOutboxFile = (folder: string, outbox: z.input<typeof OutboxFile>): void => {
|
|
74
|
+
mkdirSync(folder, { recursive: true });
|
|
75
|
+
writeFileSync(join(folder, OUTBOX_FILE), `${JSON.stringify(OutboxFile.parse(outbox))}\n`);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type OutboxPass = z.infer<typeof OutboxPass>;
|
|
79
|
+
|
|
80
|
+
/** Writes the inbox, with a copy of the state folder, into `folder`. */
|
|
81
|
+
export const writeInbox = (folder: string, inbox: Inbox, stateDir: string): void => {
|
|
82
|
+
mkdirSync(folder, { recursive: true });
|
|
83
|
+
writeFileSync(join(folder, INBOX_FILE), `${JSON.stringify(Inbox.parse(inbox))}\n`);
|
|
84
|
+
cpSync(stateDir, join(folder, STATE_FOLDER), { recursive: true });
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** The inbox in `folder`, its state folder copied to `stateDir`. */
|
|
88
|
+
export const readInbox = (folder: string, stateDir: string): Inbox => {
|
|
89
|
+
const inbox = Inbox.parse(JSON.parse(readFileSync(join(folder, INBOX_FILE), "utf8")));
|
|
90
|
+
|
|
91
|
+
if (existsSync(join(folder, STATE_FOLDER))) cpSync(join(folder, STATE_FOLDER), stateDir, { recursive: true });
|
|
92
|
+
|
|
93
|
+
return inbox;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const refused = (what: string): never => {
|
|
97
|
+
throw new Error(`the work job's ${what}, so this wake publishes nothing`);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export type WorkIntegrations = { readonly workSource: WorkSource; readonly questions: Questions };
|
|
101
|
+
|
|
102
|
+
/** The integrations a work job runs on: the inbox's reads, its claims as gather took them, and no write at all. */
|
|
103
|
+
export const inboxIntegrations = (inbox: Inbox, now: () => number): WorkIntegrations => {
|
|
104
|
+
const items = new Map(inbox.items.map((item) => [item.id, item]));
|
|
105
|
+
|
|
106
|
+
const claimed = async (id: ItemId, _holder: Holder, ttlMs: number): Promise<ClaimResult> => ({ won: items.has(id), claim: { item: id, holder: inbox.gathered.holder, expiresAt: now() + ttlMs } });
|
|
107
|
+
|
|
108
|
+
const writes = async (): Promise<never> => refused("integrations take no write: a pass writes through its outbox");
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
workSource: {
|
|
112
|
+
listReady: async () => inbox.items.map(({ id, title }) => ({ id, title })),
|
|
113
|
+
read: async (id) => items.get(id) ?? refused(`inbox holds no item ${id}`),
|
|
114
|
+
claim: claimed,
|
|
115
|
+
renew: claimed,
|
|
116
|
+
comment: writes,
|
|
117
|
+
fileFollowUp: writes,
|
|
118
|
+
finish: writes,
|
|
119
|
+
},
|
|
120
|
+
questions: { ask: writes, decide: writes },
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/** Adds a pass to the outbox in `folder`, its bundle moved beside it. */
|
|
125
|
+
export const writePass = (folder: string, { item, result, intents, bundle }: { readonly item: string; readonly result: ThinResult; readonly intents: readonly z.input<typeof Intent>[]; readonly bundle: string | undefined }): void => {
|
|
126
|
+
const outbox = readOutboxFile(folder);
|
|
127
|
+
const kept = bundle === undefined ? undefined : `${item}-${outbox.passes.length}.bundle`;
|
|
128
|
+
|
|
129
|
+
mkdirSync(folder, { recursive: true });
|
|
130
|
+
|
|
131
|
+
if (bundle !== undefined && kept !== undefined) cpSync(bundle, join(folder, kept));
|
|
132
|
+
|
|
133
|
+
writeOutboxFile(folder, { ...outbox, passes: [...outbox.passes, { item, result: WorkResult.parse(result), intents: [...intents], bundle: kept }] });
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** Puts the base check the work job ran into the outbox in `folder`. */
|
|
137
|
+
export const writeBaseCheck = (folder: string, { items, failure }: BaseCheck): void => writeOutboxFile(folder, { ...readOutboxFile(folder), baseCheck: { items: [...items], failure } });
|
|
138
|
+
|
|
139
|
+
export type ReadOutbox = { readonly passes: readonly OutboxPass[]; readonly baseCheck: BaseCheck | undefined; readonly stateDir: string; readonly bundleOf: (pass: OutboxPass) => string | undefined };
|
|
140
|
+
|
|
141
|
+
/** Ends the work job's outbox: an empty one where nothing was written, and a copy of its state folder for publish to take its own records from. */
|
|
142
|
+
export const writeOutboxState = (folder: string, stateDir: string): void => {
|
|
143
|
+
writeOutboxFile(folder, readOutboxFile(folder));
|
|
144
|
+
|
|
145
|
+
if (existsSync(stateDir)) cpSync(stateDir, join(folder, STATE_FOLDER), { recursive: true });
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The outbox in `folder`, refused whole before any write when it is larger than `maxBytes`, fails its schema, names a
|
|
150
|
+
* bundle outside the folder or an item gather did not claim. Its state folder is the work job's, which publish reads
|
|
151
|
+
* only through `takeWorkRecords`.
|
|
152
|
+
*/
|
|
153
|
+
export const readOutbox = (folder: string, { maxBytes, claimed }: { readonly maxBytes: number; readonly claimed: ReadonlySet<string> }): ReadOutbox => {
|
|
154
|
+
const size = sizeOf(folder);
|
|
155
|
+
|
|
156
|
+
if (size > maxBytes) refused(`outbox holds ${size} bytes, over the ${maxBytes} bytes publish reads`);
|
|
157
|
+
|
|
158
|
+
const { passes, baseCheck } = OutboxFile.parse(JSON.parse(readFileSync(join(folder, OUTBOX_FILE), "utf8")));
|
|
159
|
+
const stranger = passes.find(({ item }) => !claimed.has(item));
|
|
160
|
+
|
|
161
|
+
if (stranger !== undefined) refused(`outbox names ${stranger.item}, which gather did not claim`);
|
|
162
|
+
|
|
163
|
+
return { passes, baseCheck, stateDir: join(folder, STATE_FOLDER), bundleOf: (pass) => pass.bundle && join(folder, basename(pass.bundle)) };
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** The bytes of every file under `folder`, links not followed. */
|
|
167
|
+
const sizeOf = (folder: string): number =>
|
|
168
|
+
[...new Bun.Glob("**").scanSync({ cwd: folder, onlyFiles: true, dot: true, followSymlinks: false })].reduce((total, file) => total + lstatSync(join(folder, file)).size, 0);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A bare repo of the checkout with the base and each hum branch at the commits gather saw: the remote a work job's runs
|
|
172
|
+
* clone, so they start where publish checks them from. A commit the checkout lacks throws.
|
|
173
|
+
*/
|
|
174
|
+
export const workRemote = async ({ checkout, gathered, folder, timeoutMs }: { readonly checkout: string; readonly gathered: Gathered; readonly folder: string; readonly timeoutMs: number }): Promise<string> => {
|
|
175
|
+
await gitIn(checkout, { timeoutMs }).raw(["clone", "--bare", "-q", checkout, folder]);
|
|
176
|
+
|
|
177
|
+
const git = gitIn(folder, { timeoutMs });
|
|
178
|
+
|
|
179
|
+
for (const [branch, at] of [[gathered.base, gathered.baseSha], ...Object.entries(gathered.heads)] as const) await git.raw(["update-ref", `refs/heads/${branch}`, at]);
|
|
180
|
+
|
|
181
|
+
return folder;
|
|
182
|
+
};
|
|
@@ -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
|
+
};
|