@moo.md/hum 0.0.0-stage → 0.0.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 +321 -0
- package/defaults/kinds/check.json +183 -0
- package/defaults/kinds/docs.json +151 -0
- package/defaults/kinds/feature.json +202 -0
- package/defaults/kinds/fix.json +183 -0
- package/defaults/kinds/hotfix.json +164 -0
- package/defaults/roles/build.md +3 -0
- package/defaults/roles/fix.md +1 -0
- package/defaults/roles/plan.md +3 -0
- package/defaults/roles/review.md +3 -0
- package/evals/alignment/RESULTS.md +8 -0
- package/evals/alignment/cases.jsonl +128 -0
- package/evals/alignment/sample.jsonl +20 -0
- package/evals/kind/RESULTS.md +5 -0
- package/evals/kind/cases.jsonl +24 -0
- package/evals/plan-outcome/RESULTS.md +4 -0
- package/evals/plan-outcome/cases.jsonl +12 -0
- package/evals/repeat-pair/RESULTS.md +4 -0
- package/evals/repeat-pair/cases.jsonl +51 -0
- package/evals/reply-option/RESULTS.md +3 -0
- package/evals/reply-option/cases.jsonl +45 -0
- package/evals/review-verdict/RESULTS.md +4 -0
- package/evals/review-verdict/cases.jsonl +12 -0
- package/package.json +88 -4
- package/schema/hum.schema.json +1355 -0
- package/scripts/eval.ts +244 -0
- package/src/adapter-api.ts +134 -0
- package/src/adapters/admit.ts +111 -0
- package/src/adapters/argument.ts +11 -0
- package/src/adapters/claim.ts +93 -0
- package/src/adapters/claude-bg.ts +375 -0
- package/src/adapters/claude-p.ts +593 -0
- package/src/adapters/contract.ts +231 -0
- package/src/adapters/fake.ts +157 -0
- package/src/adapters/git.ts +155 -0
- package/src/adapters/github.ts +457 -0
- package/src/adapters/guard.ts +152 -0
- package/src/adapters/history.ts +215 -0
- package/src/adapters/item-file.ts +102 -0
- package/src/adapters/limit.ts +67 -0
- package/src/adapters/markdown.ts +365 -0
- package/src/adapters/marked.ts +119 -0
- package/src/adapters/model-call.ts +75 -0
- package/src/adapters/once.ts +45 -0
- package/src/adapters/owner-commit.ts +60 -0
- package/src/adapters/pull-requests.ts +253 -0
- package/src/adapters/registry.ts +156 -0
- package/src/adapters/roundup.ts +291 -0
- package/src/adapters/serial.ts +22 -0
- package/src/adapters/transcript.ts +99 -0
- package/src/cli/commands.ts +46 -0
- package/src/cli/main.ts +8 -0
- package/src/cli/ops.ts +224 -0
- package/src/cli/service.ts +156 -0
- package/src/cli/setup.ts +141 -0
- package/src/cli/shared-watch.ts +127 -0
- package/src/config/change.ts +54 -0
- package/src/config/checks.ts +217 -0
- package/src/config/git-ref-name.d.ts +3 -0
- package/src/config/hash.ts +6 -0
- package/src/config/json.ts +6 -0
- package/src/config/load.ts +88 -0
- package/src/config/machine.ts +20 -0
- package/src/config/migrate.ts +61 -0
- package/src/config/refusal.ts +58 -0
- package/src/config/schema.ts +465 -0
- package/src/config/settings.ts +6 -0
- package/src/config/state-map.ts +76 -0
- package/src/decide/alignment.ts +79 -0
- package/src/decide/answer.ts +131 -0
- package/src/decide/kind.ts +39 -0
- package/src/decide/labels.ts +137 -0
- package/src/decide/merge.ts +102 -0
- package/src/decide/prose.ts +109 -0
- package/src/decide/question.ts +159 -0
- package/src/decide/review.ts +10 -0
- package/src/decide/risk.ts +216 -0
- package/src/decide/taste.ts +37 -0
- package/src/exec.ts +80 -0
- package/src/index.ts +200 -0
- package/src/loop/breaks.ts +8 -0
- package/src/loop/check-item.ts +76 -0
- package/src/loop/ci.ts +8 -0
- package/src/loop/github.ts +81 -0
- package/src/loop/groups.ts +108 -0
- package/src/loop/memory.ts +70 -0
- package/src/loop/notes.ts +76 -0
- package/src/loop/pair.ts +39 -0
- package/src/loop/pass.ts +99 -0
- package/src/loop/proof.ts +85 -0
- package/src/loop/prove-item.ts +33 -0
- package/src/loop/records.ts +75 -0
- package/src/loop/signals.ts +130 -0
- package/src/measure/inputs.ts +28 -0
- package/src/measure/measure.ts +68 -0
- package/src/service/cards.ts +24 -0
- package/src/service/ci-tick.ts +28 -0
- package/src/service/launchd.ts +16 -0
- package/src/service/supervise.ts +145 -0
- package/src/service/systemd.ts +44 -0
- package/src/service/token.ts +21 -0
- package/src/service/unit.ts +29 -0
- package/src/service/wake.ts +30 -0
- package/src/service/workflows.ts +97 -0
- package/src/setup/propose.ts +120 -0
- package/src/setup/prove.ts +216 -0
- package/src/setup/scan.ts +186 -0
- package/src/setup/scratch.ts +81 -0
- package/src/setup/shipped.ts +15 -0
- package/src/setup/tables.ts +101 -0
- package/src/setup/waits.ts +58 -0
- package/src/setup/write.ts +161 -0
- package/src/slots/env.ts +42 -0
- package/src/slots/pool.ts +329 -0
- package/src/slots/scan.ts +26 -0
- package/src/slots/worktree.ts +17 -0
- package/src/state/events.ts +52 -0
- package/src/state/faults.ts +73 -0
- package/src/state/format.ts +50 -0
- package/src/state/fs.ts +100 -0
- package/src/state/items.ts +189 -0
- package/src/state/open.ts +80 -0
- package/src/state/ops.ts +76 -0
- package/src/state/records.ts +35 -0
- package/src/state/runs.ts +99 -0
- package/src/state/saved.ts +119 -0
- package/src/status/file.ts +49 -0
- package/src/status/format.ts +132 -0
- package/src/status/sections.ts +14 -0
- package/src/status/version.ts +2 -0
- package/src/time.ts +7 -0
- package/src/worker/alignment-report.ts +18 -0
- package/src/worker/answers.ts +153 -0
- package/src/worker/base-check.ts +46 -0
- package/src/worker/checks.ts +26 -0
- package/src/worker/kind.ts +68 -0
- package/src/worker/land.ts +508 -0
- package/src/worker/lifecycle.ts +97 -0
- package/src/worker/merged.ts +29 -0
- package/src/worker/open.ts +100 -0
- package/src/worker/prose-run.ts +56 -0
- package/src/worker/questions.ts +89 -0
- package/src/worker/scheduler.ts +135 -0
- package/src/worker/signature.ts +41 -0
- package/src/worker/tally.ts +25 -0
- package/src/worker/taste.ts +38 -0
- package/src/worker/thin-path.ts +886 -0
- package/src/worker/worker.ts +728 -0
- package/surface.json +8787 -0
- package/README.md +0 -3
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { x } from "tinyexec";
|
|
2
|
+
|
|
3
|
+
export type ShellOptions = {
|
|
4
|
+
/** Added to the worker's own environment. */
|
|
5
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
6
|
+
/** A command still running after this many ms is killed and counts as failed. */
|
|
7
|
+
readonly timeoutMs?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** A check runs with no stdin and the worker's own PATH, as in the owner's shell; the clone's `node_modules/.bin` is not added. */
|
|
11
|
+
const shellOut = async (command: string, cwd: string, { env, timeoutMs }: ShellOptions): Promise<{ code: number | undefined; output: string }> => {
|
|
12
|
+
const { exitCode, stdout, stderr } = await x("sh", ["-c", command], { nodePath: false, timeout: timeoutMs, nodeOptions: { cwd, env: { ...process.env, ...env }, stdio: ["ignore", "pipe", "pipe"] } });
|
|
13
|
+
|
|
14
|
+
return { code: exitCode, output: `${stdout}${stderr}` };
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** The first failing check as its command, exit code and output; undefined when all pass. */
|
|
18
|
+
export const runChecks = async (commands: readonly string[], cwd: string, options: ShellOptions = {}): Promise<string | undefined> => {
|
|
19
|
+
for (const command of commands) {
|
|
20
|
+
const ran = await shellOut(command, cwd, options);
|
|
21
|
+
|
|
22
|
+
if (ran.code !== 0) return `${command} exited ${ran.code}:\n${ran.output}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Item, Questions, WorkSource } from "../adapter-api.ts";
|
|
2
|
+
import type { ModelCall } from "../adapters/model-call.ts";
|
|
3
|
+
import type { Config } from "../config/load.ts";
|
|
4
|
+
import { declaredKind, kindQuestion, UNSURE } from "../decide/kind.ts";
|
|
5
|
+
import { PLAIN_QUESTIONS } from "../decide/question.ts";
|
|
6
|
+
import type { State } from "../state/open.ts";
|
|
7
|
+
import type { ItemKind } from "../state/saved.ts";
|
|
8
|
+
import { queueQuestion, releaseQuestions } from "./questions.ts";
|
|
9
|
+
|
|
10
|
+
export type KindContext = {
|
|
11
|
+
readonly state: State;
|
|
12
|
+
readonly config: Config;
|
|
13
|
+
readonly judge: ModelCall;
|
|
14
|
+
readonly workSource: WorkSource;
|
|
15
|
+
readonly questions: Questions;
|
|
16
|
+
readonly now: () => number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Why the model call gave no kind it may use. */
|
|
20
|
+
const noKind = async (o: KindContext, item: Item, kinds: readonly string[]): Promise<{ kind: string } | { why: string }> => {
|
|
21
|
+
const judged = await o.judge({ ...kindQuestion(item, { kinds, kindMeanings: o.config.hum.kindMeanings }), item: item.id });
|
|
22
|
+
|
|
23
|
+
if (judged.kind === "abstained") return { why: judged.why };
|
|
24
|
+
|
|
25
|
+
return judged.value.kind === UNSURE ? { why: "the model call could not tell" } : { kind: judged.value.kind };
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An item's kind, decided once and saved: the name its source gives it, else the model call's pick from the kinds,
|
|
30
|
+
* else the fallback kind. A fallback is an abstention: the item is unclear, and one question asks the owner for its kind.
|
|
31
|
+
* `except` leaves one kind out, for an item whose change showed it is not that kind.
|
|
32
|
+
*/
|
|
33
|
+
export const decideKind = async (o: KindContext, item: Item, except?: string): Promise<ItemKind> => {
|
|
34
|
+
const kinds = o.config.hum.kinds.filter((kind) => kind !== except);
|
|
35
|
+
const declared = declaredKind(item, kinds, o.config.hum.kindLabels);
|
|
36
|
+
const picked = declared === undefined ? await noKind(o, item, kinds) : undefined;
|
|
37
|
+
|
|
38
|
+
const decided: ItemKind =
|
|
39
|
+
declared !== undefined
|
|
40
|
+
? { item: item.id, kind: declared, from: "source" }
|
|
41
|
+
: picked && "kind" in picked
|
|
42
|
+
? { item: item.id, kind: picked.kind, from: "model" }
|
|
43
|
+
: { item: item.id, kind: o.config.hum.fallbackKind, from: "fallback", why: picked?.why ?? "no kind named" };
|
|
44
|
+
|
|
45
|
+
o.state.itemKinds.put(item.id, decided);
|
|
46
|
+
|
|
47
|
+
if (decided.from === "fallback") {
|
|
48
|
+
queueQuestion(o, item, { text: PLAIN_QUESTIONS.kind, options: kinds, detail: `It runs as ${decided.kind} for now.` }, "kind");
|
|
49
|
+
await releaseQuestions(o);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return decided;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** The kind the owner named in answer to the kind question, for an item still on its fallback kind. */
|
|
56
|
+
export const kindAnswered = (known: ItemKind, item: Item, kinds: readonly string[]): ItemKind | undefined => {
|
|
57
|
+
const answer = known.from === "fallback" ? item.thread.findLast((entry) => entry.kind === "answer" && entry.option !== undefined && kinds.includes(entry.option)) : undefined;
|
|
58
|
+
|
|
59
|
+
return answer?.option === undefined ? undefined : { item: item.id, kind: answer.option, from: "source" };
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/** Drops what an item's run on its earlier kind left: its snapshot, its landing, its plan and its verdict. Its branch stays. */
|
|
63
|
+
export const forgetProgress = (state: State, id: string): void => {
|
|
64
|
+
state.items.remove(id);
|
|
65
|
+
state.landings.remove(id);
|
|
66
|
+
state.plans.remove(id);
|
|
67
|
+
state.verdicts.remove(id);
|
|
68
|
+
};
|
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import pRetry from "p-retry";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import {
|
|
7
|
+
type Item,
|
|
8
|
+
type Questions,
|
|
9
|
+
type WorkSource,
|
|
10
|
+
} from "../adapter-api.ts";
|
|
11
|
+
import {
|
|
12
|
+
type Git,
|
|
13
|
+
gitIn,
|
|
14
|
+
isAncestor,
|
|
15
|
+
PATCH,
|
|
16
|
+
push,
|
|
17
|
+
PushRefused,
|
|
18
|
+
} from "../adapters/git.ts";
|
|
19
|
+
import { type CommitLanded, landOwnerCommit } from "../adapters/owner-commit.ts";
|
|
20
|
+
import { commandTimeoutMs } from "../exec.ts";
|
|
21
|
+
import { changeOf, historiesOf, readHistory } from "../adapters/history.ts";
|
|
22
|
+
import { markDone } from "../adapters/item-file.ts";
|
|
23
|
+
import {
|
|
24
|
+
type BranchRules,
|
|
25
|
+
methodFor,
|
|
26
|
+
type PullRequests,
|
|
27
|
+
} from "../adapters/pull-requests.ts";
|
|
28
|
+
import type { ModelCall } from "../adapters/model-call.ts";
|
|
29
|
+
import type { Config } from "../config/load.ts";
|
|
30
|
+
import type { Boundary } from "../config/schema.ts";
|
|
31
|
+
import { alignedPlacement, alignmentQuestion, gatesAlignment } from "../decide/alignment.ts";
|
|
32
|
+
import {
|
|
33
|
+
isCleared,
|
|
34
|
+
type Landing,
|
|
35
|
+
landingOf,
|
|
36
|
+
needsReviewRun,
|
|
37
|
+
OWNER_APPROVAL,
|
|
38
|
+
type Route,
|
|
39
|
+
routeOf,
|
|
40
|
+
type Unclear,
|
|
41
|
+
waitReason,
|
|
42
|
+
} from "../decide/merge.ts";
|
|
43
|
+
import { isTrusted } from "../decide/prose.ts";
|
|
44
|
+
import {
|
|
45
|
+
type Change,
|
|
46
|
+
placeChange,
|
|
47
|
+
type Placement,
|
|
48
|
+
type Policy,
|
|
49
|
+
} from "../decide/risk.ts";
|
|
50
|
+
import type { State } from "../state/open.ts";
|
|
51
|
+
import type { Landing as SavedLanding, Verdict } from "../state/saved.ts";
|
|
52
|
+
import { PLAIN_QUESTIONS } from "../decide/question.ts";
|
|
53
|
+
import { runChecks } from "./checks.ts";
|
|
54
|
+
import { recordMerged } from "./merged.ts";
|
|
55
|
+
import { blockedPaths, brokenCommits } from "../loop/breaks.ts";
|
|
56
|
+
import { queueQuestion, releaseQuestions } from "./questions.ts";
|
|
57
|
+
import { reportAlignment } from "./alignment-report.ts";
|
|
58
|
+
import { reportTaste } from "./taste.ts";
|
|
59
|
+
import type { ThinResult } from "./thin-path.ts";
|
|
60
|
+
|
|
61
|
+
const SHORT_EXCERPT = 200;
|
|
62
|
+
|
|
63
|
+
const HUM_CONFIG = ".hum/hum.json";
|
|
64
|
+
|
|
65
|
+
const NO_RULES: BranchRules = {
|
|
66
|
+
requiresPullRequest: false,
|
|
67
|
+
mergeMethods: undefined,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** Everything landing a finished change reads and writes. The clone has the run branch checked out and the base fetched. */
|
|
71
|
+
export type LandContext = {
|
|
72
|
+
readonly state: State;
|
|
73
|
+
readonly config: Config;
|
|
74
|
+
readonly kind: string;
|
|
75
|
+
readonly id: string;
|
|
76
|
+
readonly item: Item;
|
|
77
|
+
readonly branch: string;
|
|
78
|
+
readonly base: string;
|
|
79
|
+
readonly remote: string;
|
|
80
|
+
readonly clone: string;
|
|
81
|
+
/** What the run's folder adds to the environment of the repo's checks. */
|
|
82
|
+
readonly env: Readonly<Record<string, string>>;
|
|
83
|
+
readonly dir: string;
|
|
84
|
+
readonly git: Git;
|
|
85
|
+
readonly checks: readonly string[];
|
|
86
|
+
readonly workSource: WorkSource;
|
|
87
|
+
readonly questions: Questions;
|
|
88
|
+
/** The remote's pull requests and branch rules; undefined for a remote that is not on GitHub. */
|
|
89
|
+
readonly pulls: PullRequests | undefined;
|
|
90
|
+
readonly now: () => number;
|
|
91
|
+
readonly judge: ModelCall;
|
|
92
|
+
/** A fresh review run on the change that ends at `head`, at its level's tier; it saves its verdict. */
|
|
93
|
+
readonly reviewRun: (change: { readonly head: string; readonly diff: string; readonly level: string }) => Promise<ReviewOutcome>;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** What a review run at landing came to: its verdict, or the result that stopped it, such as a usage limit. */
|
|
97
|
+
export type ReviewOutcome = { readonly verdict: Verdict } | { readonly halted: ThinResult };
|
|
98
|
+
|
|
99
|
+
/** The review verdict that lets a change merge. */
|
|
100
|
+
export const REVIEW_APPROVAL = "approve";
|
|
101
|
+
|
|
102
|
+
/** The change cannot merge on the newest base: it conflicts, or its checks fail there. */
|
|
103
|
+
class StaleMerge extends Error {
|
|
104
|
+
override name = "StaleMerge";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Another writer moved the base between the fetch and the push. */
|
|
108
|
+
class BaseMoved extends Error {
|
|
109
|
+
override name = "BaseMoved";
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The glob a new path rule takes for a path no rule places: its folder, or the file itself at the top. */
|
|
113
|
+
export const ruleGlobFor = (path: string): string =>
|
|
114
|
+
dirname(path) === "." ? path : `${dirname(path)}/**`;
|
|
115
|
+
|
|
116
|
+
const levelNames = (config: Config): readonly string[] =>
|
|
117
|
+
config.hum.levels.map(({ name }) => name);
|
|
118
|
+
|
|
119
|
+
/** The config's risk policy with every rule the owner's answers added since it was loaded. */
|
|
120
|
+
const policyOf = ({
|
|
121
|
+
config,
|
|
122
|
+
state,
|
|
123
|
+
}: Pick<LandContext, "config" | "state">): Policy => ({
|
|
124
|
+
...config.hum,
|
|
125
|
+
paths: [
|
|
126
|
+
...config.hum.paths,
|
|
127
|
+
...state.learned.list().map(({ glob, level }) => ({ glob, level })),
|
|
128
|
+
],
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
/** The change's level under the current policy and the base's own history. */
|
|
132
|
+
export const placementOf = async (
|
|
133
|
+
ctx: Pick<LandContext, "config" | "state" | "git" | "base" | "now"> & { readonly kind?: string },
|
|
134
|
+
change: Change,
|
|
135
|
+
): Promise<{ placement: Placement; policy: Policy }> => {
|
|
136
|
+
const policy = policyOf(ctx);
|
|
137
|
+
|
|
138
|
+
const histories = historiesOf(
|
|
139
|
+
await readHistory(ctx.git, `origin/${ctx.base}`, ctx.config.hum),
|
|
140
|
+
{ ...ctx.config.hum, paths: policy.paths },
|
|
141
|
+
brokenCommits(ctx.state),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
placement: placeChange(change, policy, histories, {
|
|
146
|
+
now: ctx.now(),
|
|
147
|
+
base: ctx.base,
|
|
148
|
+
kindFloor: ctx.kind === undefined ? undefined : ctx.config.hum.kindFloors[ctx.kind],
|
|
149
|
+
blocked: blockedPaths(ctx.state),
|
|
150
|
+
}),
|
|
151
|
+
policy,
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/** The diff of the run branch against the base, read as the risk level reads it. */
|
|
156
|
+
export const changeOn = async ({
|
|
157
|
+
git,
|
|
158
|
+
base,
|
|
159
|
+
config,
|
|
160
|
+
}: Pick<LandContext, "git" | "base" | "config">): Promise<Change> =>
|
|
161
|
+
changeOf(await git.raw(["diff", ...PATCH, `origin/${base}...HEAD`]), config.hum);
|
|
162
|
+
|
|
163
|
+
const isOwnerApproval =
|
|
164
|
+
(since: number) =>
|
|
165
|
+
(entry: Item["thread"][number]): boolean =>
|
|
166
|
+
entry.kind === "answer" &&
|
|
167
|
+
entry.option === OWNER_APPROVAL &&
|
|
168
|
+
entry.at >= since;
|
|
169
|
+
|
|
170
|
+
/** The level the owner picked for unclear paths, answered since the change was saved. */
|
|
171
|
+
const answeredLevel = (
|
|
172
|
+
item: Item,
|
|
173
|
+
since: number,
|
|
174
|
+
levels: readonly string[],
|
|
175
|
+
): string | undefined =>
|
|
176
|
+
item.thread.findLast(
|
|
177
|
+
(entry) =>
|
|
178
|
+
entry.kind === "answer" &&
|
|
179
|
+
entry.at >= since &&
|
|
180
|
+
entry.option !== undefined &&
|
|
181
|
+
levels.includes(entry.option),
|
|
182
|
+
)?.option;
|
|
183
|
+
|
|
184
|
+
const askUnclear = async (ctx: LandContext, unclear: readonly string[]): Promise<void> => {
|
|
185
|
+
const globs = [...new Set(unclear.map(ruleGlobFor))];
|
|
186
|
+
|
|
187
|
+
queueQuestion(ctx, ctx.item, { text: PLAIN_QUESTIONS.level, options: levelNames(ctx.config), detail: `${globs.join(", ")} match no path rule.` }, "level");
|
|
188
|
+
await releaseQuestions(ctx);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const RawPaths = z.looseObject({ paths: z.array(z.unknown()).default([]) });
|
|
192
|
+
|
|
193
|
+
/** Commits the owner's answer as path rules in the base's `hum.json`, with hooks off, and saves them for the view's undo. */
|
|
194
|
+
const learnRules = async (
|
|
195
|
+
ctx: LandContext,
|
|
196
|
+
unclear: readonly string[],
|
|
197
|
+
level: string,
|
|
198
|
+
): Promise<CommitLanded> => {
|
|
199
|
+
const { state, config, id, base, remote, now } = ctx;
|
|
200
|
+
const globs = [...new Set(unclear.map(ruleGlobFor))];
|
|
201
|
+
const scratch = mkdtempSync(join(tmpdir(), "hum-rule-"));
|
|
202
|
+
const timeoutMs = commandTimeoutMs(config.hum);
|
|
203
|
+
const git = gitIn(scratch, { identity: config.hum.commitIdentity, timeoutMs });
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
await gitIn(tmpdir(), { timeoutMs }).raw([
|
|
207
|
+
"clone",
|
|
208
|
+
"-q",
|
|
209
|
+
"--depth",
|
|
210
|
+
"1",
|
|
211
|
+
"--branch",
|
|
212
|
+
base,
|
|
213
|
+
remote,
|
|
214
|
+
scratch,
|
|
215
|
+
]);
|
|
216
|
+
|
|
217
|
+
const file = join(scratch, HUM_CONFIG);
|
|
218
|
+
const raw = RawPaths.parse(JSON.parse(readFileSync(file, "utf8")));
|
|
219
|
+
|
|
220
|
+
writeFileSync(
|
|
221
|
+
file,
|
|
222
|
+
`${JSON.stringify({ ...raw, paths: [...raw.paths, ...globs.map((glob) => ({ glob, level }))] }, null, "\t")}\n`,
|
|
223
|
+
);
|
|
224
|
+
await git.raw([
|
|
225
|
+
"commit",
|
|
226
|
+
"-q",
|
|
227
|
+
"-a",
|
|
228
|
+
"-m",
|
|
229
|
+
`hum: ${globs.join(", ")} at level ${level}`,
|
|
230
|
+
"-m",
|
|
231
|
+
`The owner answered on item ${id}.`,
|
|
232
|
+
]);
|
|
233
|
+
|
|
234
|
+
const commit = (await git.raw(["rev-parse", "HEAD"])).trim();
|
|
235
|
+
|
|
236
|
+
const landed = await landOwnerCommit({
|
|
237
|
+
sha: commit,
|
|
238
|
+
base,
|
|
239
|
+
label: id,
|
|
240
|
+
title: `hum: ${globs.join(", ")} at level ${level}`,
|
|
241
|
+
body: `The owner answered on item ${id}: ${globs.join(", ")} at level ${level}.`,
|
|
242
|
+
branchPrefix: config.hum.ownerBranchPrefix,
|
|
243
|
+
mergeMethod: config.hum.mergeMethod,
|
|
244
|
+
pulls: ctx.pulls,
|
|
245
|
+
push: (refspecs) => push(git, ["origin", ...refspecs]),
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
if (landed.via === "conflict")
|
|
249
|
+
throw new Error(
|
|
250
|
+
`another push moved ${base} before the path rule for ${id} landed`,
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
for (const glob of globs)
|
|
254
|
+
state.learned.put(glob, { glob, level, item: id, commit, pull: landed.via === "pull-request" ? landed.pull.url : undefined, at: now() });
|
|
255
|
+
|
|
256
|
+
return landed;
|
|
257
|
+
} finally {
|
|
258
|
+
rmSync(scratch, { recursive: true, force: true });
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
/** Marks the item done on the run branch, pushes the branch and saves what its diff showed, so a retry lands it without a run. `ownerApproved` records that the owner approved this change in its review state. */
|
|
263
|
+
export const prepareLanding = async (ctx: LandContext, ownerApproved = false): Promise<void> => {
|
|
264
|
+
const { git, clone, dir, id, branch, state, now } = ctx;
|
|
265
|
+
const change = await changeOn(ctx);
|
|
266
|
+
const head = (await git.raw(["rev-parse", "HEAD"])).trim();
|
|
267
|
+
const file = join(clone, dir, `${id}.md`);
|
|
268
|
+
|
|
269
|
+
writeFileSync(file, markDone(readFileSync(file, "utf8")));
|
|
270
|
+
await git.raw(["add", `${dir}/${id}.md`]);
|
|
271
|
+
await git.raw(["commit", "-q", "-m", `hum: finish ${id}`]);
|
|
272
|
+
await push(git, ["--force", "origin", `HEAD:refs/heads/${branch}`]);
|
|
273
|
+
state.landings.put(id, { item: id, branch, since: now(), change: { ...change, paths: [...change.paths] }, head, ownerApproved });
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/** A merge is rechecked on the newest base: when the base moved, the change is rebased and the checks run again. */
|
|
277
|
+
const pushOntoNewestBase = async ({
|
|
278
|
+
git,
|
|
279
|
+
base,
|
|
280
|
+
branch,
|
|
281
|
+
clone,
|
|
282
|
+
env,
|
|
283
|
+
checks,
|
|
284
|
+
}: LandContext): Promise<void> => {
|
|
285
|
+
await git.raw(["fetch", "-q", "origin", base]);
|
|
286
|
+
|
|
287
|
+
if (!(await isAncestor(git, `origin/${base}`, "HEAD"))) {
|
|
288
|
+
const rebased = await git.run(["rebase", "-q", `origin/${base}`]);
|
|
289
|
+
|
|
290
|
+
if (rebased.code !== 0) {
|
|
291
|
+
await git.run(["rebase", "--abort"]);
|
|
292
|
+
|
|
293
|
+
throw new StaleMerge(
|
|
294
|
+
`the change conflicts with the newest ${base}: ${rebased.stderr.trim().slice(0, SHORT_EXCERPT)}`,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const failure = await runChecks(checks, clone, { env });
|
|
299
|
+
|
|
300
|
+
if (failure)
|
|
301
|
+
throw new StaleMerge(
|
|
302
|
+
`the checks fail on the newest ${base}: ${failure.slice(0, SHORT_EXCERPT)}`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
await push(git, ["--force", "origin", `HEAD:refs/heads/${branch}`]);
|
|
307
|
+
|
|
308
|
+
if ((await push(git, ["origin", `HEAD:refs/heads/${base}`])) === "conflict")
|
|
309
|
+
throw new BaseMoved(`another push moved ${base} first`);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const finish = async ({ state, git, id, now }: LandContext): Promise<ThinResult> => {
|
|
313
|
+
recordMerged(state, id, (await git.raw(["rev-parse", "HEAD"])).trim(), now());
|
|
314
|
+
|
|
315
|
+
return { outcome: "merged", item: id };
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const pushToBase = async (ctx: LandContext): Promise<void> => {
|
|
319
|
+
await ctx.state.ops.once(
|
|
320
|
+
{ itemId: ctx.id, run: "merge", attempt: 0, op: "push-base" },
|
|
321
|
+
{
|
|
322
|
+
holds: async () => {
|
|
323
|
+
await ctx.git.raw(["fetch", "-q", "origin", ctx.base]);
|
|
324
|
+
|
|
325
|
+
return isAncestor(ctx.git, "HEAD", `origin/${ctx.base}`);
|
|
326
|
+
},
|
|
327
|
+
run: () =>
|
|
328
|
+
pRetry(() => pushOntoNewestBase(ctx), {
|
|
329
|
+
retries: ctx.config.hum.mergeTries - 1,
|
|
330
|
+
minTimeout: 0,
|
|
331
|
+
shouldRetry: ({ error }) => error instanceof BaseMoved,
|
|
332
|
+
}),
|
|
333
|
+
},
|
|
334
|
+
);
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
/** One pull request from the run branch: it merges itself once GitHub's required checks pass when the change is cleared, and waits for the owner otherwise. */
|
|
338
|
+
const throughPullRequest = async (
|
|
339
|
+
ctx: LandContext,
|
|
340
|
+
pulls: PullRequests,
|
|
341
|
+
autoMerge: boolean,
|
|
342
|
+
route: Route,
|
|
343
|
+
rules: BranchRules,
|
|
344
|
+
why: string,
|
|
345
|
+
): Promise<ThinResult> => {
|
|
346
|
+
const { id, branch, base, item, config } = ctx;
|
|
347
|
+
const body = `${autoMerge ? `Level ${route.level.name}: it merges once the required checks pass.` : `Not merged by hum: ${why}.`}\n\nItem ${id}.`;
|
|
348
|
+
|
|
349
|
+
const pull =
|
|
350
|
+
(await pulls.find(branch)) ??
|
|
351
|
+
(await pulls.open({
|
|
352
|
+
head: branch,
|
|
353
|
+
base,
|
|
354
|
+
title: `hum: ${item.title}`,
|
|
355
|
+
body,
|
|
356
|
+
}));
|
|
357
|
+
|
|
358
|
+
if (pull.state === "MERGED") return finish(ctx);
|
|
359
|
+
|
|
360
|
+
if (pull.state === "CLOSED")
|
|
361
|
+
return {
|
|
362
|
+
outcome: "review",
|
|
363
|
+
item: id,
|
|
364
|
+
detail: `its pull request was closed unmerged: ${pull.url}`,
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const head = ctx.state.landings.get(id)?.head ?? branch;
|
|
368
|
+
|
|
369
|
+
await reportTaste(ctx, pulls, pull, head);
|
|
370
|
+
await reportAlignment(ctx, pulls, pull, head);
|
|
371
|
+
|
|
372
|
+
const armed = await pulls.autoMerge(
|
|
373
|
+
pull,
|
|
374
|
+
autoMerge,
|
|
375
|
+
methodFor(config.hum.mergeMethod, rules.mergeMethods),
|
|
376
|
+
);
|
|
377
|
+
|
|
378
|
+
if (armed === "merged") return finish(ctx);
|
|
379
|
+
|
|
380
|
+
if (armed === "armed" && pull.behind) await pulls.update(pull);
|
|
381
|
+
|
|
382
|
+
return { outcome: "landing", item: id, detail: pull.url };
|
|
383
|
+
};
|
|
384
|
+
|
|
385
|
+
/** A push the remote refused, on GitHub: the rules are read again and the change goes through a pull request. */
|
|
386
|
+
const afterRefusal = async (
|
|
387
|
+
ctx: LandContext,
|
|
388
|
+
cleared: boolean,
|
|
389
|
+
route: Route,
|
|
390
|
+
why: string,
|
|
391
|
+
): Promise<ThinResult> => {
|
|
392
|
+
if (!ctx.pulls)
|
|
393
|
+
throw new PushRefused(
|
|
394
|
+
`the remote refused the push of ${ctx.id} to ${ctx.base}, and it is not on GitHub`,
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
return throughPullRequest(
|
|
398
|
+
ctx,
|
|
399
|
+
ctx.pulls,
|
|
400
|
+
cleared,
|
|
401
|
+
route,
|
|
402
|
+
await ctx.pulls.rules(ctx.base, true),
|
|
403
|
+
why,
|
|
404
|
+
);
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const landBy = async (
|
|
408
|
+
ctx: LandContext,
|
|
409
|
+
landing: Landing,
|
|
410
|
+
route: Route,
|
|
411
|
+
rules: BranchRules,
|
|
412
|
+
why: string,
|
|
413
|
+
): Promise<ThinResult> => {
|
|
414
|
+
if (landing.via === "wait")
|
|
415
|
+
return { outcome: "review", item: ctx.id, detail: why };
|
|
416
|
+
|
|
417
|
+
if (landing.via === "pull-request" && ctx.pulls)
|
|
418
|
+
return throughPullRequest(ctx, ctx.pulls, landing.autoMerge, route, rules, why);
|
|
419
|
+
|
|
420
|
+
try {
|
|
421
|
+
await pushToBase(ctx);
|
|
422
|
+
|
|
423
|
+
return await finish(ctx);
|
|
424
|
+
} catch (error) {
|
|
425
|
+
if (error instanceof StaleMerge)
|
|
426
|
+
return { outcome: "review", item: ctx.id, detail: error.message };
|
|
427
|
+
|
|
428
|
+
if (error instanceof PushRefused) return afterRefusal(ctx, true, route, why);
|
|
429
|
+
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Lands a change saved by `prepareLanding`, by its level under the policy as it stands now: an answered level
|
|
436
|
+
* becomes a path rule first, an unclear change asks once, and the target branch's rules pick a push or a pull request.
|
|
437
|
+
*/
|
|
438
|
+
export const land = async (ctx: LandContext): Promise<ThinResult> => {
|
|
439
|
+
const saved = ctx.state.landings.get(ctx.id);
|
|
440
|
+
|
|
441
|
+
if (!saved) throw new Error(`no landing is saved for ${ctx.id}`);
|
|
442
|
+
|
|
443
|
+
const first = await placementOf(ctx, saved.change);
|
|
444
|
+
const answered = answeredLevel(ctx.item, saved.since, levelNames(ctx.config));
|
|
445
|
+
const learns = first.placement.unclear.length > 0 && answered !== undefined;
|
|
446
|
+
|
|
447
|
+
if (learns) await learnRules(ctx, first.placement.unclear, answered);
|
|
448
|
+
|
|
449
|
+
const { placement, policy } = learns
|
|
450
|
+
? await placementOf(ctx, saved.change)
|
|
451
|
+
: first;
|
|
452
|
+
|
|
453
|
+
if (placement.unclear.length > 0) await askUnclear(ctx, placement.unclear);
|
|
454
|
+
|
|
455
|
+
const diff = await ctx.git.raw(["diff", ...PATCH, `origin/${ctx.base}...${saved.head}`]);
|
|
456
|
+
const alignment = saved.change.paths.length > 0 ? await alignmentOf(ctx, saved, diff) : undefined;
|
|
457
|
+
const aligned = gatesAlignment(ctx.config.hum) && alignment && "graded" in alignment ? alignedPlacement(placement, alignment.graded, ctx.config.hum) : placement;
|
|
458
|
+
const routeWith = (verdict?: Verdict): Route => routeOf(aligned, policy.paths, ctx.kind, saved.change.paths, abstentionsOf(ctx, alignment, verdict));
|
|
459
|
+
const placed = routeWith();
|
|
460
|
+
const reviewed = needsReviewRun(placed) ? await verdictOn(ctx, { head: saved.head, diff, level: placed.level.name }) : undefined;
|
|
461
|
+
|
|
462
|
+
if (reviewed && "halted" in reviewed) return reviewed.halted;
|
|
463
|
+
|
|
464
|
+
const verdict = reviewed?.verdict;
|
|
465
|
+
const route = routeWith(verdict);
|
|
466
|
+
const approvals = { review: verdict?.event === REVIEW_APPROVAL, owner: saved.ownerApproved || ctx.item.thread.some(isOwnerApproval(saved.since)) };
|
|
467
|
+
const why = verdict && verdict.event !== REVIEW_APPROVAL ? `the review run answered ${verdict.event}: ${verdict.reason}` : waitReason(route);
|
|
468
|
+
const rules = ctx.pulls ? await ctx.pulls.rules(ctx.base) : NO_RULES;
|
|
469
|
+
|
|
470
|
+
return landBy(ctx, landingOf(rules.requiresPullRequest, isCleared(route, approvals)), route, rules, why);
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
/** The alignment call's grades for a saved change, made once and kept with the landing. */
|
|
474
|
+
const alignmentOf = async (ctx: LandContext, saved: SavedLanding, diff: string): Promise<NonNullable<SavedLanding["alignment"]>> => {
|
|
475
|
+
if (saved.alignment) return saved.alignment;
|
|
476
|
+
|
|
477
|
+
const judged = await ctx.judge({ ...alignmentQuestion(ctx.item, diff, ctx.config.hum), item: ctx.id });
|
|
478
|
+
const alignment = judged.kind === "picked" ? { graded: judged.value } : { abstained: judged.why };
|
|
479
|
+
|
|
480
|
+
ctx.state.landings.put(ctx.id, { ...saved, alignment });
|
|
481
|
+
|
|
482
|
+
return alignment;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
/** The verdict on the change at `head`: the one saved for it, else a fresh review run. */
|
|
486
|
+
const verdictOn = async (ctx: LandContext, change: Parameters<LandContext["reviewRun"]>[0]): Promise<ReviewOutcome> => {
|
|
487
|
+
const saved = ctx.state.verdicts.get(ctx.id);
|
|
488
|
+
|
|
489
|
+
return saved?.head === change.head ? { verdict: saved } : ctx.reviewRun(change);
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const abstained = (boundary: Boundary, why: string): Unclear => ({ cause: "abstained", boundary, why });
|
|
493
|
+
|
|
494
|
+
const unmeasured = (models: readonly string[]): string => `${models.join(", ") || "no model"} has no measured rate that meets the bar`;
|
|
495
|
+
|
|
496
|
+
/** Every model judgment behind the change that abstained, or came from a model id its boundary was not measured on. */
|
|
497
|
+
const abstentionsOf = (ctx: LandContext, alignment: SavedLanding["alignment"], verdict: Verdict | undefined): Unclear[] => {
|
|
498
|
+
const measured = (boundary: Boundary, models: readonly string[]): boolean => isTrusted(models, boundary, ctx.config.hum);
|
|
499
|
+
const kind = ctx.state.itemKinds.get(ctx.id);
|
|
500
|
+
const plan = ctx.state.plans.get(ctx.id);
|
|
501
|
+
|
|
502
|
+
return [
|
|
503
|
+
...(kind?.from === "fallback" ? [abstained("kind", kind.why)] : []),
|
|
504
|
+
...(gatesAlignment(ctx.config.hum) && alignment && "abstained" in alignment ? [abstained("alignment", alignment.abstained)] : []),
|
|
505
|
+
...(plan && !measured("plan-outcome", plan.models) ? [abstained("plan-outcome", unmeasured(plan.models))] : []),
|
|
506
|
+
...(verdict && !measured("review-verdict", verdict.models) ? [abstained("review-verdict", unmeasured(verdict.models))] : []),
|
|
507
|
+
];
|
|
508
|
+
};
|