@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,159 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { Question } from "../adapter-api.ts";
|
|
3
|
+
import type { HumConfig } from "../config/schema.ts";
|
|
4
|
+
import { DAY_MS } from "../time.ts";
|
|
5
|
+
|
|
6
|
+
/** The reasons a run gives for needing the owner, each with the facts code checks it against. */
|
|
7
|
+
export const ASK_REASONS = ["missing-secret", "past-undoing", "spends-money", "publishes", "choice", "unsure"] as const;
|
|
8
|
+
|
|
9
|
+
export type AskReason = (typeof ASK_REASONS)[number];
|
|
10
|
+
|
|
11
|
+
const ChoiceOption = z.strictObject({ word: z.string().min(1), paths: z.array(z.string().min(1)) });
|
|
12
|
+
|
|
13
|
+
/** Why a run needs the owner, in the fields code can check. */
|
|
14
|
+
export const RunReason = z.strictObject({
|
|
15
|
+
kind: z.enum(ASK_REASONS),
|
|
16
|
+
/** The name of the secret the run lacks. */
|
|
17
|
+
variable: z.string().min(1).optional(),
|
|
18
|
+
/** The command that cannot be taken back. */
|
|
19
|
+
command: z.string().min(1).optional(),
|
|
20
|
+
options: z.array(ChoiceOption).optional(),
|
|
21
|
+
default: z.string().min(1).optional(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export type RunReason = z.infer<typeof RunReason>;
|
|
25
|
+
|
|
26
|
+
/** What code knows that a run's reason is checked against. */
|
|
27
|
+
export type AskFacts = {
|
|
28
|
+
/** The names, never the values, of the variables the runner has. */
|
|
29
|
+
readonly runnerVariables: ReadonlySet<string>;
|
|
30
|
+
/** The files the change holds so far. */
|
|
31
|
+
readonly changedPaths: readonly string[];
|
|
32
|
+
readonly level: string;
|
|
33
|
+
readonly undoableLevels: readonly string[];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type AskDecision =
|
|
37
|
+
/** `confirmed`: the facts bear the reason out, so its own question is asked; otherwise the owner gets the plain one. */
|
|
38
|
+
| { readonly action: "ask"; readonly reason: AskReason; readonly confirmed: boolean }
|
|
39
|
+
| { readonly action: "decide"; readonly choice: string; readonly over: readonly string[] };
|
|
40
|
+
|
|
41
|
+
/** A choice is cheap to undo when every option touches only the change's own files, on a level where that is safe. */
|
|
42
|
+
const isCheapChoice = ({ options = [], default: chosen }: RunReason, { changedPaths, level, undoableLevels }: AskFacts): boolean =>
|
|
43
|
+
options.some(({ word }) => word === chosen) && undoableLevels.includes(level) && options.every(({ paths }) => paths.every((path) => changedPaths.includes(path)));
|
|
44
|
+
|
|
45
|
+
const CONFIRMS: Readonly<Record<AskReason, (reason: RunReason, facts: AskFacts) => boolean>> = {
|
|
46
|
+
"missing-secret": ({ variable }, { runnerVariables }) => variable !== undefined && !runnerVariables.has(variable),
|
|
47
|
+
"past-undoing": ({ command }) => command !== undefined,
|
|
48
|
+
"spends-money": () => true,
|
|
49
|
+
publishes: () => true,
|
|
50
|
+
choice: () => false,
|
|
51
|
+
unsure: () => true,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** Whether the owner is asked: a reason the facts cannot confirm still asks, and a cheap choice is made without them. */
|
|
55
|
+
export const askDecision = (reason: RunReason, facts: AskFacts): AskDecision => {
|
|
56
|
+
if (reason.kind === "choice" && isCheapChoice(reason, facts)) {
|
|
57
|
+
return { action: "decide", choice: reason.default ?? "", over: (reason.options ?? []).map(({ word }) => word) };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return { action: "ask", reason: reason.kind, confirmed: CONFIRMS[reason.kind](reason, facts) };
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The plain question each reason falls back to when the run's own text fails the rules; each passes them. */
|
|
64
|
+
export const PLAIN_QUESTIONS = {
|
|
65
|
+
"missing-secret": "Can you set the secret the run lacks?",
|
|
66
|
+
"past-undoing": "May the run take a step it cannot undo?",
|
|
67
|
+
"spends-money": "May the run spend money?",
|
|
68
|
+
publishes: "May the run publish this?",
|
|
69
|
+
choice: "Which option should the run take?",
|
|
70
|
+
unsure: "How should the run go on?",
|
|
71
|
+
"over-cap": "Keep working past the token cap?",
|
|
72
|
+
"owner-review": "Approve this change?",
|
|
73
|
+
kind: "Which kind of work is this?",
|
|
74
|
+
level: "Which level do these paths belong to?",
|
|
75
|
+
} as const satisfies Record<string, string>;
|
|
76
|
+
|
|
77
|
+
export type PlainQuestion = keyof typeof PLAIN_QUESTIONS;
|
|
78
|
+
|
|
79
|
+
/** The question each confirmed reason asks, from the facts the run named. */
|
|
80
|
+
const ASKED = {
|
|
81
|
+
"missing-secret": ({ variable }) => `Can you set ${variable} for the runner?`,
|
|
82
|
+
"past-undoing": ({ command }) => `May the run take ${command}?`,
|
|
83
|
+
"spends-money": () => PLAIN_QUESTIONS["spends-money"],
|
|
84
|
+
publishes: () => PLAIN_QUESTIONS.publishes,
|
|
85
|
+
choice: () => PLAIN_QUESTIONS.choice,
|
|
86
|
+
unsure: () => PLAIN_QUESTIONS.unsure,
|
|
87
|
+
} as const satisfies Record<AskReason, (reason: RunReason) => string>;
|
|
88
|
+
|
|
89
|
+
export type AskedQuestion = { readonly question: Question; readonly plain: PlainQuestion };
|
|
90
|
+
|
|
91
|
+
/** The question a run's need becomes. A reason the facts bear out asks its own question with the run's words folded; any other asks in the run's words. */
|
|
92
|
+
export const askedQuestion = (reason: RunReason, confirmed: boolean, runsWords: string | undefined): AskedQuestion => {
|
|
93
|
+
const kind = confirmed ? reason.kind : "unsure";
|
|
94
|
+
const options = reason.options?.map(({ word }) => word);
|
|
95
|
+
|
|
96
|
+
return kind === "unsure"
|
|
97
|
+
? { question: { text: runsWords ?? ASKED.unsure(), options }, plain: kind }
|
|
98
|
+
: { question: { text: ASKED[kind](reason), options, detail: runsWords }, plain: kind };
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** Questions that settle a side matter while the item goes on; no run waits on them. */
|
|
102
|
+
const SIDE_QUESTIONS: readonly string[] = [PLAIN_QUESTIONS.kind, PLAIN_QUESTIONS.level];
|
|
103
|
+
|
|
104
|
+
/** Whether a question is one an item waits on, rather than a side question. */
|
|
105
|
+
export const isWaitQuestion = (text: string): boolean => !SIDE_QUESTIONS.includes(text);
|
|
106
|
+
|
|
107
|
+
/** Why a question text is refused. */
|
|
108
|
+
export type QuestionIssue = "too-wide" | "no-question-mark" | "names-item" | "long-word";
|
|
109
|
+
|
|
110
|
+
const QUESTION_MARK = "?";
|
|
111
|
+
|
|
112
|
+
const words = (text: string): string[] => text.split(/\s+/).filter(Boolean);
|
|
113
|
+
|
|
114
|
+
const cellsOf = (text: string): number => Bun.stringWidth(text);
|
|
115
|
+
|
|
116
|
+
const WORD_EDGES = /^\W+|\W+$/g;
|
|
117
|
+
|
|
118
|
+
/** The question text rules: at most `cells` wide, ends in "?", names no item id, and no word over `wordCells`. */
|
|
119
|
+
export const questionIssues = (text: string, item: string, { cells, wordCells }: HumConfig["questionLimits"]): QuestionIssue[] => {
|
|
120
|
+
const mentionsItem = words(text).some((word) => word.replaceAll(WORD_EDGES, "") === item);
|
|
121
|
+
|
|
122
|
+
return [
|
|
123
|
+
...(cellsOf(text) > cells ? (["too-wide"] as const) : []),
|
|
124
|
+
...(text.trimEnd().endsWith(QUESTION_MARK) ? [] : (["no-question-mark"] as const)),
|
|
125
|
+
...(mentionsItem ? (["names-item"] as const) : []),
|
|
126
|
+
...(words(text).some((word) => cellsOf(word) > wordCells) ? (["long-word"] as const) : []),
|
|
127
|
+
];
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/** A question to put on the thread: the run's own text when it passes the rules, else the plain one with the run's text folded. */
|
|
131
|
+
export const composeQuestion = (draft: Question, plain: PlainQuestion, item: string, limits: HumConfig["questionLimits"]): Question => {
|
|
132
|
+
if (questionIssues(draft.text, item, limits).length === 0) return draft;
|
|
133
|
+
|
|
134
|
+
return { ...draft, text: PLAIN_QUESTIONS[plain], detail: [draft.text, draft.detail].filter(Boolean).join("\n\n") };
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/** A question that waits for room under the daily cap. */
|
|
138
|
+
export type Waiting = {
|
|
139
|
+
readonly item: string;
|
|
140
|
+
/** The index of its best priority label; larger is later, and no label is last. */
|
|
141
|
+
readonly priority: number;
|
|
142
|
+
/** How many other waiting items clash with it, and so wait on its item. */
|
|
143
|
+
readonly behind: number;
|
|
144
|
+
readonly since: number;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** Row 4's order: the owner's priority label, then how many items wait on it, then age. */
|
|
148
|
+
export const questionOrder = (a: Waiting, b: Waiting): number => a.priority - b.priority || b.behind - a.behind || a.since - b.since;
|
|
149
|
+
|
|
150
|
+
/** The priority of an item: the index of its first label in the owner's list, or past the end when it has none. */
|
|
151
|
+
export const priorityOf = (labels: readonly string[], priorityLabels: readonly string[]): number => {
|
|
152
|
+
const found = priorityLabels.findIndex((label) => labels.includes(label));
|
|
153
|
+
|
|
154
|
+
return found < 0 ? priorityLabels.length : found;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/** How many more questions the owner can be asked now: the cap less those asked within the last day. No cap leaves no limit. */
|
|
158
|
+
export const roomToAsk = (askedAt: readonly number[], cap: number | undefined, now: number): number =>
|
|
159
|
+
cap === undefined ? Number.POSITIVE_INFINITY : Math.max(0, cap - askedAt.filter((at) => now - at < DAY_MS).length);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Item } from "../adapter-api.ts";
|
|
2
|
+
import type { HumConfig } from "../config/schema.ts";
|
|
3
|
+
import { DATA_RULE, fenced } from "./prose.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What a fresh review run reads: the item as the owner asked it and the change's diff. It takes no thread, so the
|
|
7
|
+
* author's plan, questions and answers never reach the reviewer.
|
|
8
|
+
*/
|
|
9
|
+
export const reviewInput = (item: Pick<Item, "id" | "title" | "body">, diff: string, { diffLimit }: Pick<HumConfig, "diffLimit">): string =>
|
|
10
|
+
[`# Item ${item.id}: ${item.title}`, DATA_RULE, fenced("item", item.body.trim()), `## The change`, fenced("diff", diff.slice(0, diffLimit))].join("\n\n");
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { quantile } from "simple-statistics";
|
|
2
|
+
import type { HumConfig, Launch, Level, PathRule } from "../config/schema.ts";
|
|
3
|
+
|
|
4
|
+
/** What only the owner changes: hum's own config, the owner's adapters and the CI workflows. */
|
|
5
|
+
const OWN_PATHS = [".hum/**", "adapters/**", ".github/workflows/**"] as const;
|
|
6
|
+
|
|
7
|
+
/** What hum reads from a change's diff. */
|
|
8
|
+
export type Change = {
|
|
9
|
+
readonly paths: readonly string[];
|
|
10
|
+
/** Lines added plus lines deleted. */
|
|
11
|
+
readonly size: number;
|
|
12
|
+
/** An added or removed line matches the project's loosened-check rule. */
|
|
13
|
+
readonly loosened: boolean;
|
|
14
|
+
/** A changed line in `package.json` or a lockfile names the engine package. */
|
|
15
|
+
readonly pinChanged: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const LABELS = ["reverted", "fixed", "clean"] as const;
|
|
19
|
+
|
|
20
|
+
export type Label = (typeof LABELS)[number];
|
|
21
|
+
|
|
22
|
+
/** A path rule's merged changes on the base, newest first. */
|
|
23
|
+
export type PathHistory = {
|
|
24
|
+
readonly sizes: readonly number[];
|
|
25
|
+
readonly labels: readonly Label[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type Histories = ReadonlyMap<string, PathHistory>;
|
|
29
|
+
|
|
30
|
+
export type Policy = Pick<
|
|
31
|
+
HumConfig,
|
|
32
|
+
"levels" | "paths" | "strictPaths" | "launches" | "sizeRange" | "hookPaths"
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
/** When the change is placed, and onto which base: launches depend on both. */
|
|
36
|
+
export type Moment = {
|
|
37
|
+
readonly now: number;
|
|
38
|
+
readonly base: string;
|
|
39
|
+
/** The lowest level the change's kind takes. */
|
|
40
|
+
readonly kindFloor?: string;
|
|
41
|
+
/** Globs of paths with a break the owner's level blocks until its follow-up merges. */
|
|
42
|
+
readonly blocked?: readonly string[];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** One reason the level is what it is. */
|
|
46
|
+
export type Step =
|
|
47
|
+
| { readonly step: "path"; readonly glob: string; readonly level: string }
|
|
48
|
+
| { readonly step: "launch"; readonly glob: string; readonly level: string }
|
|
49
|
+
| {
|
|
50
|
+
readonly step: "fix-limit";
|
|
51
|
+
readonly glob: string;
|
|
52
|
+
readonly level: string;
|
|
53
|
+
readonly breaks: number;
|
|
54
|
+
readonly limit: number;
|
|
55
|
+
}
|
|
56
|
+
| { readonly step: "data-or-secret"; readonly path: string }
|
|
57
|
+
| { readonly step: "own-config"; readonly path: string }
|
|
58
|
+
| { readonly step: "kind-floor"; readonly level: string }
|
|
59
|
+
| { readonly step: "blocked-path"; readonly path: string }
|
|
60
|
+
| { readonly step: "engine-pin" }
|
|
61
|
+
| {
|
|
62
|
+
readonly step: "size";
|
|
63
|
+
readonly glob: string;
|
|
64
|
+
readonly size: number;
|
|
65
|
+
readonly usual: number;
|
|
66
|
+
}
|
|
67
|
+
| { readonly step: "loosened-check" }
|
|
68
|
+
| { readonly step: "alignment" };
|
|
69
|
+
|
|
70
|
+
type SetsLevel = Extract<Step, { level: string }>;
|
|
71
|
+
|
|
72
|
+
export type Placement = {
|
|
73
|
+
readonly level: Level;
|
|
74
|
+
/** Paths no rule places. The change runs at the level of the rest and never merges on its own. */
|
|
75
|
+
readonly unclear: readonly string[];
|
|
76
|
+
readonly steps: readonly Step[];
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const matches = (glob: string, path: string): boolean =>
|
|
80
|
+
new Bun.Glob(glob).match(path);
|
|
81
|
+
|
|
82
|
+
const isLaunchOn = (launch: Launch, { now, base }: Moment): boolean =>
|
|
83
|
+
"base" in launch
|
|
84
|
+
? matches(launch.base, base)
|
|
85
|
+
: Date.parse(launch.from) <= now && now < Date.parse(launch.until);
|
|
86
|
+
|
|
87
|
+
/** Fixed or reverted changes among a path's last `of` merged changes. */
|
|
88
|
+
const breaksIn = (labels: readonly Label[], of: number): number =>
|
|
89
|
+
labels.slice(0, of).filter((label) => label !== "clean").length;
|
|
90
|
+
|
|
91
|
+
/** The top of a path's usual size range, or undefined while it has too few merged changes to have one. */
|
|
92
|
+
export const usualSize = (
|
|
93
|
+
sizes: readonly number[],
|
|
94
|
+
{ changes, fewest, middle }: Policy["sizeRange"],
|
|
95
|
+
): number | undefined => {
|
|
96
|
+
const recent = sizes.slice(0, changes);
|
|
97
|
+
const upperShare = (1 + middle) / 2;
|
|
98
|
+
|
|
99
|
+
return recent.length < fewest ? undefined : quantile([...recent], upperShare);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A change's risk level: the highest level among the paths it touches, each raised by a launch or by its
|
|
104
|
+
* fix-and-revert limit; the strictest level for data, secrets and hum's own config; then one step up above a
|
|
105
|
+
* path's usual size range and one for a loosened check. A path no rule places marks the change unclear and
|
|
106
|
+
* never raises its level.
|
|
107
|
+
*/
|
|
108
|
+
export const placeChange = (
|
|
109
|
+
change: Change,
|
|
110
|
+
policy: Policy,
|
|
111
|
+
histories: Histories,
|
|
112
|
+
moment: Moment,
|
|
113
|
+
): Placement => {
|
|
114
|
+
const names = policy.levels.map(({ name }) => name);
|
|
115
|
+
const strictest = names.length - 1;
|
|
116
|
+
const rank = (name: string): number => names.indexOf(name);
|
|
117
|
+
|
|
118
|
+
const above = (name: string): string =>
|
|
119
|
+
names[Math.min(strictest, rank(name) + 1)] ?? name;
|
|
120
|
+
|
|
121
|
+
const rulesOn = (path: string): readonly PathRule[] =>
|
|
122
|
+
policy.paths.filter((rule) => matches(rule.glob, path));
|
|
123
|
+
|
|
124
|
+
const isStrict = (path: string): boolean =>
|
|
125
|
+
policy.strictPaths.some((glob) => matches(glob, path));
|
|
126
|
+
|
|
127
|
+
const isOwn = (path: string): boolean =>
|
|
128
|
+
[...OWN_PATHS, ...policy.hookPaths].some((glob) => matches(glob, path));
|
|
129
|
+
|
|
130
|
+
const touched = [
|
|
131
|
+
...new Map(
|
|
132
|
+
change.paths.flatMap(rulesOn).map((rule) => [rule.glob, rule]),
|
|
133
|
+
).values(),
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
const ruleSteps = touched.flatMap((rule): SetsLevel[] => {
|
|
137
|
+
const limit = rule.limit;
|
|
138
|
+
|
|
139
|
+
const breaks = limit
|
|
140
|
+
? breaksIn(histories.get(rule.glob)?.labels ?? [], limit.of)
|
|
141
|
+
: 0;
|
|
142
|
+
|
|
143
|
+
const isPastLimit = limit !== undefined && breaks > limit.fixes;
|
|
144
|
+
|
|
145
|
+
return [
|
|
146
|
+
{ step: "path", glob: rule.glob, level: rule.level },
|
|
147
|
+
...(isPastLimit
|
|
148
|
+
? [
|
|
149
|
+
{
|
|
150
|
+
step: "fix-limit",
|
|
151
|
+
glob: rule.glob,
|
|
152
|
+
level: above(rule.level),
|
|
153
|
+
breaks,
|
|
154
|
+
limit: limit.fixes,
|
|
155
|
+
} as const,
|
|
156
|
+
]
|
|
157
|
+
: []),
|
|
158
|
+
];
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const kindFloorSteps: SetsLevel[] = moment.kindFloor === undefined ? [] : [{ step: "kind-floor", level: moment.kindFloor }];
|
|
162
|
+
|
|
163
|
+
const launchSteps = policy.launches.flatMap((launch): SetsLevel[] =>
|
|
164
|
+
isLaunchOn(launch, moment) &&
|
|
165
|
+
change.paths.some((path) => matches(launch.glob, path))
|
|
166
|
+
? [{ step: "launch", glob: launch.glob, level: launch.level }]
|
|
167
|
+
: [],
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const floors: Step[] = [
|
|
171
|
+
...change.paths.flatMap((path): Step[] =>
|
|
172
|
+
isStrict(path) ? [{ step: "data-or-secret", path }] : [],
|
|
173
|
+
),
|
|
174
|
+
...change.paths.flatMap((path): Step[] =>
|
|
175
|
+
isOwn(path) ? [{ step: "own-config", path }] : [],
|
|
176
|
+
),
|
|
177
|
+
...(change.pinChanged ? [{ step: "engine-pin" } as const] : []),
|
|
178
|
+
...change.paths.flatMap((path): Step[] =>
|
|
179
|
+
(moment.blocked ?? []).some((glob) => matches(glob, path)) ? [{ step: "blocked-path", path }] : [],
|
|
180
|
+
),
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
const oversized = touched.flatMap((rule): Step[] => {
|
|
184
|
+
const usual = usualSize(
|
|
185
|
+
histories.get(rule.glob)?.sizes ?? [],
|
|
186
|
+
policy.sizeRange,
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
return usual !== undefined && change.size > usual
|
|
190
|
+
? [{ step: "size", glob: rule.glob, size: change.size, usual }]
|
|
191
|
+
: [];
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const bumps: Step[] = [
|
|
195
|
+
...oversized.slice(0, 1),
|
|
196
|
+
...(change.loosened ? [{ step: "loosened-check" } as const] : []),
|
|
197
|
+
];
|
|
198
|
+
|
|
199
|
+
const highest = Math.max(
|
|
200
|
+
0,
|
|
201
|
+
...[...ruleSteps, ...launchSteps, ...kindFloorSteps].map(({ level }) => rank(level)),
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const floored = floors.length > 0 ? strictest : highest;
|
|
205
|
+
const level = policy.levels[Math.min(strictest, floored + bumps.length)];
|
|
206
|
+
|
|
207
|
+
if (!level) throw new Error("a config has at least one level");
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
level,
|
|
211
|
+
unclear: change.paths.filter(
|
|
212
|
+
(path) => rulesOn(path).length === 0 && !isStrict(path) && !isOwn(path),
|
|
213
|
+
),
|
|
214
|
+
steps: [...ruleSteps, ...launchSteps, ...kindFloorSteps, ...floors, ...bumps],
|
|
215
|
+
};
|
|
216
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { HumConfig } from "../config/schema.ts";
|
|
3
|
+
import { DATA_RULE, fenced } from "./prose.ts";
|
|
4
|
+
|
|
5
|
+
/** Whether the diff keeps one taste rule, and the line of the diff that shows it. */
|
|
6
|
+
export const TasteAnswer = z.strictObject({ keeps: z.boolean(), evidence: z.string() });
|
|
7
|
+
|
|
8
|
+
export type TasteAnswer = z.infer<typeof TasteAnswer>;
|
|
9
|
+
|
|
10
|
+
/** The cheap model call that checks a diff against one taste rule. Its pick is for a report and decides nothing. */
|
|
11
|
+
export const tasteQuestion = (rule: string, diff: string, { diffLimit }: Pick<HumConfig, "diffLimit">) => ({
|
|
12
|
+
boundary: "taste" as const,
|
|
13
|
+
reportOnly: true as const,
|
|
14
|
+
prompt: [
|
|
15
|
+
"Check this change against the owner's rule. keeps: true when the diff keeps the rule or does not touch it, false when it breaks it. evidence: the line of the diff that shows it, or an empty string.",
|
|
16
|
+
DATA_RULE,
|
|
17
|
+
fenced("rule", rule),
|
|
18
|
+
fenced("diff", diff.slice(0, diffLimit)),
|
|
19
|
+
].join("\n\n"),
|
|
20
|
+
answer: TasteAnswer,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/** One rule's result: its answer, or why the call gave none. */
|
|
24
|
+
export type TasteResult = { readonly rule: string; readonly answer: TasteAnswer | { readonly abstained: string } };
|
|
25
|
+
|
|
26
|
+
/** The pull request comment. It only reports; `measured` says whether the model's rate at this boundary meets the bar. */
|
|
27
|
+
export const tasteComment = (results: readonly TasteResult[], measured: boolean): string => {
|
|
28
|
+
const lines = results.map(({ rule, answer }) => {
|
|
29
|
+
if ("abstained" in answer) return `- ? ${rule} (not checked: ${answer.abstained})`;
|
|
30
|
+
|
|
31
|
+
return answer.keeps ? `- kept: ${rule}` : `- broken: ${rule}${answer.evidence === "" ? "" : `\n > ${answer.evidence}`}`;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const status = measured ? "The model's rate on the owner's past violations meets the bar." : "The model's rate on the owner's past violations is not measured, so this is a report only.";
|
|
35
|
+
|
|
36
|
+
return ["hum checked this change against the owner's taste rules.", lines.join("\n"), `${status} It blocks nothing.`].join("\n\n");
|
|
37
|
+
};
|
package/src/exec.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import pTimeout from "p-timeout";
|
|
2
|
+
import { type Options, type Output, x } from "tinyexec";
|
|
3
|
+
import { type HumConfig, SHIPPED_DEFAULTS } from "./config/schema.ts";
|
|
4
|
+
import { SECOND_MS } from "./time.ts";
|
|
5
|
+
|
|
6
|
+
/** A command ran past its limit and was told to stop. */
|
|
7
|
+
export class CommandTimedOut extends Error {
|
|
8
|
+
override name = "CommandTimedOut";
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
readonly command: string,
|
|
12
|
+
readonly args: readonly string[],
|
|
13
|
+
readonly timeoutMs: number,
|
|
14
|
+
) {
|
|
15
|
+
super(`\`${[command, ...args].join(" ")}\` ran longer than ${timeoutMs / SECOND_MS} seconds and was stopped`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** The owner's `commandTimeoutSeconds` in milliseconds. */
|
|
20
|
+
export const commandTimeoutMs = ({ commandTimeoutSeconds }: Pick<HumConfig, "commandTimeoutSeconds">): number => commandTimeoutSeconds * SECOND_MS;
|
|
21
|
+
|
|
22
|
+
/** The limit where no `hum.json` is read yet. */
|
|
23
|
+
export const SHIPPED_COMMAND_TIMEOUT_MS = commandTimeoutMs(SHIPPED_DEFAULTS);
|
|
24
|
+
|
|
25
|
+
/** How long a command's processes get to end after the stop signal before they are killed. */
|
|
26
|
+
const STOP_GRACE_MS = 2000;
|
|
27
|
+
const POLL_MS = 50;
|
|
28
|
+
|
|
29
|
+
export type ExecOptions = Partial<Omit<Options, "timeout" | "signal" | "persist">> & {
|
|
30
|
+
readonly timeoutMs: number;
|
|
31
|
+
readonly graceMs?: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const signalGroup = (group: number, signal: "SIGTERM" | "SIGKILL" | 0): boolean => {
|
|
35
|
+
try {
|
|
36
|
+
return process.kill(-group, signal);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (error instanceof Error && "code" in error && error.code === "ESRCH") return false;
|
|
39
|
+
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const isGone = async (group: number, graceMs: number): Promise<boolean> => {
|
|
45
|
+
const deadline = Date.now() + graceMs;
|
|
46
|
+
|
|
47
|
+
while (signalGroup(group, 0) && Date.now() < deadline) await Bun.sleep(POLL_MS);
|
|
48
|
+
|
|
49
|
+
return !signalGroup(group, 0);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Ends every process the command started: SIGTERM first, so git removes its lock files, then SIGKILL for what ignores it. */
|
|
53
|
+
const stopGroup = async (group: number, graceMs: number): Promise<void> => {
|
|
54
|
+
signalGroup(group, "SIGTERM");
|
|
55
|
+
|
|
56
|
+
if (await isGone(group, graceMs)) return;
|
|
57
|
+
|
|
58
|
+
signalGroup(group, "SIGKILL");
|
|
59
|
+
await isGone(group, graceMs);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Runs a command and returns what it printed. The call ends at `timeoutMs` whatever the child does: tinyexec's own
|
|
64
|
+
* `timeout` still waits for the child to exit and close its pipes, which a child that ignores the stop signal, a
|
|
65
|
+
* grandchild that holds a pipe, or an exit the runtime never reports does not allow. The command runs as its own
|
|
66
|
+
* process group, and a timeout ends the whole group, so the helpers git and ssh start do not outlive it. A timeout
|
|
67
|
+
* throws `CommandTimedOut`, never a result.
|
|
68
|
+
*/
|
|
69
|
+
export const exec = (command: string, args: readonly string[], { timeoutMs, graceMs = STOP_GRACE_MS, ...options }: ExecOptions): Promise<Output> => {
|
|
70
|
+
const child = x(command, [...args], { ...options, persist: true });
|
|
71
|
+
|
|
72
|
+
return pTimeout(child, {
|
|
73
|
+
milliseconds: timeoutMs,
|
|
74
|
+
fallback: async () => {
|
|
75
|
+
if (child.pid !== undefined) await stopGroup(child.pid, graceMs);
|
|
76
|
+
|
|
77
|
+
throw new CommandTimedOut(command, args, timeoutMs);
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
};
|