@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
package/src/loop/pass.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import type { WorkSource } from "../adapter-api.ts";
|
|
3
|
+
import type { Config } from "../config/load.ts";
|
|
4
|
+
import type { PastChange } from "../decide/labels.ts";
|
|
5
|
+
import type { State } from "../state/open.ts";
|
|
6
|
+
import { checkNameOf, fileChecks, filedFrom } from "./check-item.ts";
|
|
7
|
+
import { type CiLogs, logNames } from "./ci.ts";
|
|
8
|
+
import { type Proposer, groupNotes } from "./groups.ts";
|
|
9
|
+
import { parseMemory, renderMemory, withoutCovered } from "./memory.ts";
|
|
10
|
+
import { breakNotes, ownerNotes, planChangingNotes, type RawComment, saveNotes } from "./notes.ts";
|
|
11
|
+
import { type RepeatGroup, type ReviewNote, noteKey } from "./records.ts";
|
|
12
|
+
|
|
13
|
+
export type LoopPass = {
|
|
14
|
+
readonly state: State;
|
|
15
|
+
readonly config: Config;
|
|
16
|
+
readonly workSource: Pick<WorkSource, "fileFollowUp" | "comment">;
|
|
17
|
+
readonly now: () => number;
|
|
18
|
+
/** The owner's review comments and bodies since a time. */
|
|
19
|
+
readonly comments: (since: number) => Promise<readonly RawComment[]>;
|
|
20
|
+
/** The merged changes on the base, oldest first. */
|
|
21
|
+
readonly history: () => Promise<readonly PastChange[]>;
|
|
22
|
+
readonly hunkOf: (sha: string, file: string | undefined) => Promise<string>;
|
|
23
|
+
readonly propose: Proposer;
|
|
24
|
+
readonly trusts: (models: readonly string[]) => boolean;
|
|
25
|
+
readonly ciLogs: CiLogs;
|
|
26
|
+
/** The repo's `.hum/memory.md`, when it has one. */
|
|
27
|
+
readonly memoryPath?: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const EPOCH = 0;
|
|
31
|
+
|
|
32
|
+
/** Review comments are read from the newest one already saved; the saved key makes any overlap add nothing. */
|
|
33
|
+
const sinceOfLastComment = (state: State): number => Math.max(EPOCH, ...state.notes.list().filter(({ source }) => source === "review-comment").map(({ at }) => at));
|
|
34
|
+
|
|
35
|
+
const unappliedCard = (state: State) => (extension: string, signals: readonly string[]) => {
|
|
36
|
+
state.faults.raise({
|
|
37
|
+
code: "signal-not-applied",
|
|
38
|
+
item: extension,
|
|
39
|
+
cause: `Hunks in ${extension} files are not parsed, so ${signals.join(" and ")} did not run on them.`,
|
|
40
|
+
fix: "The other signals still decide; add the language to the parsed languages to apply these.",
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Step 1: the owner's notes from each source, saved once. Returns the ones this pass added. */
|
|
45
|
+
export const collectNotes = async ({ state, config, comments, history, hunkOf }: LoopPass): Promise<ReviewNote[]> =>
|
|
46
|
+
saveNotes(state, [
|
|
47
|
+
...ownerNotes(await comments(sinceOfLastComment(state)), config.hum.ownerLogins),
|
|
48
|
+
...planChangingNotes(state.answers.list()),
|
|
49
|
+
...(await breakNotes(await history(), config.hum.changeLabels, hunkOf)),
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
/** Step 6: a landed check item whose name the next CI run's log shows. */
|
|
53
|
+
export const confirmChecks = async ({ state, ciLogs, now }: Pick<LoopPass, "state" | "ciLogs" | "now">): Promise<void> => {
|
|
54
|
+
for (const group of state.repeats.list()) {
|
|
55
|
+
const merge = group.item === undefined ? undefined : state.merges.get(group.item);
|
|
56
|
+
|
|
57
|
+
if (group.checked || !merge) continue;
|
|
58
|
+
|
|
59
|
+
const logs = await ciLogs(merge.at);
|
|
60
|
+
|
|
61
|
+
if (logs.some((log) => logNames(log, group.key))) state.repeats.put(group.key, { ...group, checked: { at: now(), check: checkNameOf(group.key) } });
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const missesOf = (state: State, group: RepeatGroup): ReviewNote[] =>
|
|
66
|
+
group.checked === undefined ? [] : group.notes.flatMap((key) => state.notes.get(key) ?? []).filter((note) => note.at > (group.checked?.at ?? 0) && !group.misses.includes(noteKey(note)));
|
|
67
|
+
|
|
68
|
+
/** Step 7: memory.md drops the entries a check now covers, and a note that came after the check ran reopens its item as a miss. */
|
|
69
|
+
export const closeLoop = async ({ state, workSource, memoryPath }: Pick<LoopPass, "state" | "workSource" | "memoryPath">): Promise<void> => {
|
|
70
|
+
const covered = new Set(state.repeats.list().flatMap((group) => (group.checked ? group.notes : [])));
|
|
71
|
+
|
|
72
|
+
if (memoryPath !== undefined && covered.size > 0 && existsSync(memoryPath)) {
|
|
73
|
+
const held = parseMemory(readFileSync(memoryPath, "utf8"));
|
|
74
|
+
const kept = withoutCovered(held, covered);
|
|
75
|
+
|
|
76
|
+
if (kept.length < held.length) writeFileSync(memoryPath, renderMemory(kept));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for (const group of state.repeats.list()) {
|
|
80
|
+
const missed = missesOf(state, group);
|
|
81
|
+
|
|
82
|
+
if (group.item === undefined || missed.length === 0) continue;
|
|
83
|
+
|
|
84
|
+
for (const note of missed)
|
|
85
|
+
await workSource.fileFollowUp(filedFrom(group.key), { title: `Check missed ${note.sourceId}: ${group.item}`, body: `Kind: fix\n\nThe check for ${group.item} ran in CI and a later review note still repeated its rule.\n\n> ${note.text}\n\n\`\`\`diff\n${note.hunk}\n\`\`\`\n\nThe hunk is a failing case the check must catch.` });
|
|
86
|
+
|
|
87
|
+
state.repeats.put(group.key, { ...group, misses: [...group.misses, ...missed.map(noteKey)] });
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** One pass of the loop: collect, find repeats, file checks, confirm CI ran them, close the loop. */
|
|
92
|
+
export const runLoopPass = async (pass: LoopPass): Promise<void> => {
|
|
93
|
+
const { state, config, workSource, propose, trusts } = pass;
|
|
94
|
+
|
|
95
|
+
await groupNotes({ state, propose, trusts, onUnapplied: unappliedCard(state) }, await collectNotes(pass));
|
|
96
|
+
await fileChecks({ state, workSource, repeatAt: config.hum.repeatAt, checkKind: config.hum.checkKind });
|
|
97
|
+
await confirmChecks(pass);
|
|
98
|
+
await closeLoop(pass);
|
|
99
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import type { Git } from "../adapters/git.ts";
|
|
5
|
+
import { runChecks } from "../worker/checks.ts";
|
|
6
|
+
import type { ReviewNote } from "./records.ts";
|
|
7
|
+
|
|
8
|
+
export type ProofOptions = {
|
|
9
|
+
/** The repo the commits are in. */
|
|
10
|
+
readonly git: Git;
|
|
11
|
+
/** The check item's commit: the base with the new check on it. */
|
|
12
|
+
readonly head: string;
|
|
13
|
+
readonly base: string;
|
|
14
|
+
/** The notes whose commits the check must fail on. */
|
|
15
|
+
readonly notes: readonly ReviewNote[];
|
|
16
|
+
/** The repo's checks, which include the new one. */
|
|
17
|
+
readonly checks: readonly string[];
|
|
18
|
+
readonly timeoutMs: number;
|
|
19
|
+
readonly now: () => number;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type ProofResult = {
|
|
23
|
+
readonly passed: boolean;
|
|
24
|
+
readonly reason?: string;
|
|
25
|
+
/** Worktrees made: one per note commit and one for the head. */
|
|
26
|
+
readonly worktrees: number;
|
|
27
|
+
readonly ms: number;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const isCommit = async (git: Git, sha: string): Promise<boolean> => (await git.run(["cat-file", "-e", `${sha}^{commit}`])).code === 0;
|
|
31
|
+
|
|
32
|
+
/** The files the check item adds or changes against the base. */
|
|
33
|
+
const checkFiles = async (git: Git, base: string, head: string): Promise<string[]> =>
|
|
34
|
+
(await git.raw(["diff", "--name-only", "--diff-filter=AM", `${base}...${head}`])).split("\n").filter(Boolean);
|
|
35
|
+
|
|
36
|
+
/** Runs `body` in a detached worktree at `sha`, removed afterwards whatever happens. */
|
|
37
|
+
export const inWorktreeAt = async <T>(git: Git, sha: string, body: (folder: string) => Promise<T>): Promise<T> => {
|
|
38
|
+
const folder = mkdtempSync(join(tmpdir(), "hum-proof-"));
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
await git.raw(["worktree", "add", "--detach", "--quiet", folder, sha]);
|
|
42
|
+
|
|
43
|
+
return await body(folder);
|
|
44
|
+
} finally {
|
|
45
|
+
await git.run(["worktree", "remove", "--force", folder]);
|
|
46
|
+
rmSync(folder, { recursive: true, force: true });
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A check is real when it fails on the commit each note pointed at and passes on the base with the check on it. Each
|
|
52
|
+
* note's commit is checked out with the check's files laid over it. A note that points at no commit cannot be proved
|
|
53
|
+
* and counts against the check.
|
|
54
|
+
*/
|
|
55
|
+
export const proveCheck = async ({ git, head, base, notes, checks, timeoutMs, now }: ProofOptions): Promise<ProofResult> => {
|
|
56
|
+
const started = now();
|
|
57
|
+
let worktrees = 0;
|
|
58
|
+
|
|
59
|
+
const done = (reason?: string): ProofResult => ({ passed: reason === undefined, reason, worktrees, ms: now() - started });
|
|
60
|
+
|
|
61
|
+
const files = await checkFiles(git, base, head);
|
|
62
|
+
const commits = [...new Set(notes.map(({ change }) => change))];
|
|
63
|
+
const provable = (await Promise.all(commits.map(async (sha) => ((await isCommit(git, sha)) ? [sha] : [])))).flat();
|
|
64
|
+
|
|
65
|
+
if (provable.length === 0) return done("no note points at a commit the check can run on");
|
|
66
|
+
|
|
67
|
+
for (const sha of provable) {
|
|
68
|
+
const failure = await inWorktreeAt(git, sha, async (folder) => {
|
|
69
|
+
worktrees++;
|
|
70
|
+
await git.raw(["-C", folder, "checkout", head, "--", ...files]);
|
|
71
|
+
|
|
72
|
+
return runChecks(checks, folder, { timeoutMs });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
if (failure === undefined) return done(`the check passes on ${sha}, the commit a note points at`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const failure = await inWorktreeAt(git, head, async (folder) => {
|
|
79
|
+
worktrees++;
|
|
80
|
+
|
|
81
|
+
return runChecks(checks, folder, { timeoutMs });
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return failure === undefined ? done() : done(`the checks fail on the check item's own commit: ${failure.slice(0, 300)}`);
|
|
85
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Git } from "../adapters/git.ts";
|
|
2
|
+
import type { Config } from "../config/load.ts";
|
|
3
|
+
import type { State } from "../state/open.ts";
|
|
4
|
+
import { SECOND_MS } from "../time.ts";
|
|
5
|
+
import { proveCheck } from "./proof.ts";
|
|
6
|
+
|
|
7
|
+
export type ProveItem = {
|
|
8
|
+
readonly state: State;
|
|
9
|
+
readonly config: Config;
|
|
10
|
+
readonly git: Git;
|
|
11
|
+
readonly item: string;
|
|
12
|
+
readonly base: string;
|
|
13
|
+
readonly checks: readonly string[];
|
|
14
|
+
readonly now: () => number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Proves the check a check item made, against the notes of the repeat group it was filed for, and saves what the
|
|
19
|
+
* proof cost. `undefined` when the item belongs to no group, so there is no note to prove it against.
|
|
20
|
+
*/
|
|
21
|
+
export const proveCheckItem = async ({ state, config, git, item, base, checks, now }: ProveItem) => {
|
|
22
|
+
const group = state.repeats.list().find((found) => found.item === item);
|
|
23
|
+
|
|
24
|
+
if (!group) return undefined;
|
|
25
|
+
|
|
26
|
+
const notes = group.notes.flatMap((key) => state.notes.get(key) ?? []);
|
|
27
|
+
const head = (await git.raw(["rev-parse", "HEAD"])).trim();
|
|
28
|
+
const proof = await proveCheck({ git, head, base: `origin/${base}`, notes, checks, timeoutMs: config.hum.proofTimeoutSeconds * SECOND_MS, now });
|
|
29
|
+
|
|
30
|
+
state.proofs.put(item, { item, at: now(), passed: proof.passed, reason: proof.reason, worktrees: proof.worktrees, ms: proof.ms });
|
|
31
|
+
|
|
32
|
+
return proof;
|
|
33
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
const name = z.string().min(1);
|
|
4
|
+
const time = z.int().nonnegative();
|
|
5
|
+
|
|
6
|
+
/** Where a review note comes from. */
|
|
7
|
+
export const NOTE_SOURCES = ["review-comment", "review-body", "plan-answer", "revert", "fix"] as const;
|
|
8
|
+
|
|
9
|
+
export type NoteSource = (typeof NOTE_SOURCES)[number];
|
|
10
|
+
|
|
11
|
+
/** One correction the owner made to hum's work, saved before anything acts on it and keyed by its source and source id. */
|
|
12
|
+
export const ReviewNote = z.strictObject({
|
|
13
|
+
source: z.enum(NOTE_SOURCES),
|
|
14
|
+
sourceId: name,
|
|
15
|
+
/** What the note is about: the commit a comment sits on, the change a revert or fix names, or the item a plan answer corrected. */
|
|
16
|
+
change: name,
|
|
17
|
+
/** The item the note is known to belong to; a commit is matched to its item when the measure counts. */
|
|
18
|
+
item: name.optional(),
|
|
19
|
+
file: name.optional(),
|
|
20
|
+
line: z.int().positive().optional(),
|
|
21
|
+
/** The diff hunk the note points at; empty when the source has none. */
|
|
22
|
+
hunk: z.string(),
|
|
23
|
+
text: name,
|
|
24
|
+
at: time,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export type ReviewNote = z.infer<typeof ReviewNote>;
|
|
28
|
+
|
|
29
|
+
/** A note's key in the saved records. */
|
|
30
|
+
export const noteKey = ({ source, sourceId }: Pick<ReviewNote, "source" | "sourceId">): string => `${source}:${sourceId}`;
|
|
31
|
+
|
|
32
|
+
/** Notes that state one rule. The key is its first note's, so a later note joins instead of starting another group. */
|
|
33
|
+
export const RepeatGroup = z.strictObject({
|
|
34
|
+
key: name,
|
|
35
|
+
notes: z.array(name),
|
|
36
|
+
/** The check item filed for the group. */
|
|
37
|
+
item: name.optional(),
|
|
38
|
+
/** Notes already added to the item's thread. */
|
|
39
|
+
commented: z.array(name),
|
|
40
|
+
/** The check landed and CI ran it. */
|
|
41
|
+
checked: z.strictObject({ at: time, check: name }).optional(),
|
|
42
|
+
/** Notes that came after the check ran in CI. */
|
|
43
|
+
misses: z.array(name),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export type RepeatGroup = z.infer<typeof RepeatGroup>;
|
|
47
|
+
|
|
48
|
+
/** Two notes only a model found alike, waiting for its precision to reach the owner's bar. */
|
|
49
|
+
export const PossibleRepeat = z.strictObject({ a: name, b: name, models: z.array(name), at: time });
|
|
50
|
+
|
|
51
|
+
export type PossibleRepeat = z.infer<typeof PossibleRepeat>;
|
|
52
|
+
|
|
53
|
+
/** A merged change: what the measure counts. */
|
|
54
|
+
export const Merge = z.strictObject({ item: name, sha: name, at: time, paths: z.array(name) });
|
|
55
|
+
|
|
56
|
+
export type Merge = z.infer<typeof Merge>;
|
|
57
|
+
|
|
58
|
+
/** A test of the base that failed after an item's change merged. */
|
|
59
|
+
export const Break = z.strictObject({ item: name, sha: name, at: time, paths: z.array(name), level: name, failure: z.string(), onBreak: z.enum(["follow-up", "block"]), followUp: name });
|
|
60
|
+
|
|
61
|
+
export type Break = z.infer<typeof Break>;
|
|
62
|
+
|
|
63
|
+
/** What proving a check cost: the commits it ran on, and the time it took. */
|
|
64
|
+
export const Proof = z.strictObject({
|
|
65
|
+
item: name,
|
|
66
|
+
at: time,
|
|
67
|
+
passed: z.boolean(),
|
|
68
|
+
/** Why it failed, when it did. */
|
|
69
|
+
reason: z.string().optional(),
|
|
70
|
+
/** Worktrees made: one per note commit and one for the check's own head. */
|
|
71
|
+
worktrees: z.int().nonnegative(),
|
|
72
|
+
ms: z.int().nonnegative(),
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
export type Proof = z.infer<typeof Proof>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { dirname, extname } from "node:path";
|
|
2
|
+
import { Lang, parse } from "@ast-grep/napi";
|
|
3
|
+
import type { ReviewNote } from "./records.ts";
|
|
4
|
+
|
|
5
|
+
/** The languages whose hunks the parser reads. A hunk in any other language leaves the signals that need a parse unapplied. */
|
|
6
|
+
const PARSED_LANGUAGES = new Map<string, Lang>([
|
|
7
|
+
[".ts", Lang.TypeScript],
|
|
8
|
+
[".tsx", Lang.Tsx],
|
|
9
|
+
[".js", Lang.JavaScript],
|
|
10
|
+
[".jsx", Lang.JavaScript],
|
|
11
|
+
[".mjs", Lang.JavaScript],
|
|
12
|
+
[".cjs", Lang.JavaScript],
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
const IDENTIFIER_KINDS = ["identifier", "property_identifier", "type_identifier"] as const;
|
|
16
|
+
|
|
17
|
+
const DECLARATION_KINDS = ["function_declaration", "class_declaration", "interface_declaration", "type_alias_declaration", "enum_declaration", "method_definition", "variable_declarator", "public_field_definition", "property_signature"] as const;
|
|
18
|
+
|
|
19
|
+
const WORD = /[A-Za-z_$][\w$]*/g;
|
|
20
|
+
const CODE_SPAN = /`([^`\n]+)`/g;
|
|
21
|
+
const SUGGESTION_BLOCK = /```suggestion\n([\s\S]*?)```/g;
|
|
22
|
+
const STRING_LITERAL = /(["'`])(?:\\.|(?!\1).)*\1/g;
|
|
23
|
+
const NUMBER_LITERAL = /\b\d+(?:\.\d+)?\b/g;
|
|
24
|
+
const HUNK_HEADER = /^(@@|diff |index |--- |\+\+\+ )/;
|
|
25
|
+
const CHANGED_LINE = /^[+-]/;
|
|
26
|
+
const KEYWORDS = new Set(["const", "let", "var", "function", "return", "if", "else", "new", "await", "async", "export", "import", "from", "type", "interface", "class", "extends", "this", "true", "false", "null", "undefined", "typeof", "for", "of", "in"]);
|
|
27
|
+
|
|
28
|
+
/** A one-character word names nothing, so two notes quoting it say nothing alike. */
|
|
29
|
+
const MIN_WORD_LENGTH = 3;
|
|
30
|
+
|
|
31
|
+
const squash = (text: string): string => text.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").replace(/\s+/g, " ").trim();
|
|
32
|
+
|
|
33
|
+
const bodyLines = (hunk: string): string[] => hunk.split("\n").filter((line) => !HUNK_HEADER.test(line));
|
|
34
|
+
|
|
35
|
+
const codeOf = (hunk: string): string => bodyLines(hunk).map((line) => line.slice(1)).join("\n");
|
|
36
|
+
|
|
37
|
+
const changedCodeOf = (hunk: string): string => bodyLines(hunk).flatMap((line) => (CHANGED_LINE.test(line) ? [line.slice(1)] : [])).join("\n");
|
|
38
|
+
|
|
39
|
+
const wordsOf = (text: string): Set<string> => new Set((text.match(WORD) ?? []).filter((word) => word.length >= MIN_WORD_LENGTH));
|
|
40
|
+
|
|
41
|
+
const matchesOf = (pattern: RegExp, text: string): Set<string> => new Set([...text.matchAll(pattern)].flatMap(([, found = ""]) => squash(found) || []));
|
|
42
|
+
|
|
43
|
+
const codePatternOf = (hunk: string): string =>
|
|
44
|
+
changedCodeOf(hunk)
|
|
45
|
+
.replace(STRING_LITERAL, "S")
|
|
46
|
+
.replace(NUMBER_LITERAL, "N")
|
|
47
|
+
.replace(WORD, (word) => (KEYWORDS.has(word) ? word : "I"))
|
|
48
|
+
.replace(/\s+/g, "");
|
|
49
|
+
|
|
50
|
+
/** The glob that holds a file's folder and kind: its siblings of the same extension. */
|
|
51
|
+
export const globOf = (file: string): string => `${dirname(file)}/*${extname(file)}`;
|
|
52
|
+
|
|
53
|
+
const parsedOf = (note: ReviewNote): { identifiers: Set<string>; declared: Set<string> } | undefined => {
|
|
54
|
+
if (note.hunk === "") return { identifiers: new Set(), declared: new Set() };
|
|
55
|
+
|
|
56
|
+
const lang = PARSED_LANGUAGES.get(extname(note.file ?? ""));
|
|
57
|
+
|
|
58
|
+
if (lang === undefined) return undefined;
|
|
59
|
+
|
|
60
|
+
const identifiersIn = (code: string) => parse(lang, code).root().findAll({ rule: { any: IDENTIFIER_KINDS.map((kind) => ({ kind })) } }).map((node) => node.text());
|
|
61
|
+
const declaredIn = (code: string) => parse(lang, code).root().findAll({ rule: { any: DECLARATION_KINDS.map((kind) => ({ kind })) } }).flatMap((node) => node.field("name")?.text() ?? []);
|
|
62
|
+
|
|
63
|
+
return { identifiers: new Set(identifiersIn(codeOf(note.hunk))), declared: new Set(declaredIn(changedCodeOf(note.hunk))) };
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** What the signals compare, read once from a note. */
|
|
67
|
+
export type Prepared = {
|
|
68
|
+
readonly note: ReviewNote;
|
|
69
|
+
readonly text: string;
|
|
70
|
+
readonly suggestions: Set<string>;
|
|
71
|
+
readonly spans: Set<string>;
|
|
72
|
+
/** Identifiers the note's text names that its own hunk holds. `undefined` when the hunk cannot be parsed. */
|
|
73
|
+
readonly named: Set<string> | undefined;
|
|
74
|
+
readonly declared: Set<string> | undefined;
|
|
75
|
+
readonly glob: string | undefined;
|
|
76
|
+
readonly pattern: string;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const prepare = (note: ReviewNote): Prepared => {
|
|
80
|
+
const parsed = parsedOf(note);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
note,
|
|
84
|
+
text: squash(note.text),
|
|
85
|
+
suggestions: matchesOf(SUGGESTION_BLOCK, note.text),
|
|
86
|
+
spans: new Set([...matchesOf(CODE_SPAN, note.text)].filter((span) => span.length >= MIN_WORD_LENGTH)),
|
|
87
|
+
named: parsed && new Set([...wordsOf(note.text)].filter((word) => parsed.identifiers.has(word))),
|
|
88
|
+
declared: parsed?.declared,
|
|
89
|
+
glob: note.file === undefined ? undefined : globOf(note.file),
|
|
90
|
+
pattern: codePatternOf(note.hunk),
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const shares = <T>(a: Set<T> | undefined, b: Set<T> | undefined): boolean => a !== undefined && b !== undefined && [...a].some((found) => b.has(found));
|
|
95
|
+
|
|
96
|
+
/** Two notes on one change are about one hunk; only notes across changes repeat. */
|
|
97
|
+
const isAnotherChange = (a: Prepared, b: Prepared): boolean => a.note.change !== b.note.change;
|
|
98
|
+
|
|
99
|
+
const BREAK_SOURCES: readonly ReviewNote["source"][] = ["revert", "fix"];
|
|
100
|
+
|
|
101
|
+
type Signal = {
|
|
102
|
+
readonly name: string;
|
|
103
|
+
/** The signal reads a parsed hunk: it is left unapplied for a note whose hunk is not in a parsed language. */
|
|
104
|
+
readonly parsed: boolean;
|
|
105
|
+
readonly pairs: (a: Prepared, b: Prepared) => boolean;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** The deterministic signals, checked in order. */
|
|
109
|
+
export const SIGNALS: readonly Signal[] = [
|
|
110
|
+
{ name: "same-text", parsed: false, pairs: (a, b) => a.text === b.text },
|
|
111
|
+
{ name: "same-code", parsed: false, pairs: (a, b) => shares(a.suggestions, b.suggestions) || shares(a.spans, b.spans) },
|
|
112
|
+
{ name: "shared-identifier", parsed: true, pairs: (a, b) => isAnotherChange(a, b) && shares(a.named, b.named) },
|
|
113
|
+
{ name: "same-glob-symbol", parsed: true, pairs: (a, b) => isAnotherChange(a, b) && a.glob !== undefined && a.glob === b.glob && shares(a.declared, b.declared) },
|
|
114
|
+
{
|
|
115
|
+
name: "same-code-pattern",
|
|
116
|
+
parsed: false,
|
|
117
|
+
pairs: (a, b) => BREAK_SOURCES.includes(a.note.source) && BREAK_SOURCES.includes(b.note.source) && a.pattern !== "" && a.pattern === b.pattern,
|
|
118
|
+
},
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
/** The first signal that pairs two notes, and the signals a note's language left unapplied. */
|
|
122
|
+
export type Pairing = { readonly signal?: string; readonly unapplied: readonly string[] };
|
|
123
|
+
|
|
124
|
+
export const pairing = (a: Prepared, b: Prepared): Pairing => {
|
|
125
|
+
const canRead = (signal: Signal): boolean => !signal.parsed || (a.named !== undefined && b.named !== undefined);
|
|
126
|
+
const unapplied = SIGNALS.flatMap((signal) => (canRead(signal) ? [] : [signal.name]));
|
|
127
|
+
const found = SIGNALS.find((signal) => canRead(signal) && signal.pairs(a, b));
|
|
128
|
+
|
|
129
|
+
return { signal: found?.name, unapplied };
|
|
130
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { State } from "../state/open.ts";
|
|
2
|
+
import type { Counted } from "./measure.ts";
|
|
3
|
+
|
|
4
|
+
/** Spent records of work outside any item carry this prefix. */
|
|
5
|
+
/** The prefix of the op key every question to the owner is written under. */
|
|
6
|
+
const QUESTION_OP = "question:";
|
|
7
|
+
|
|
8
|
+
export const LOOP_SPEND_PREFIX = "loop:";
|
|
9
|
+
|
|
10
|
+
/** Reads what each item cost from what the engine saved: notes, question events and the item's token total. */
|
|
11
|
+
export const measureInputsOf = (state: State) => {
|
|
12
|
+
const merges = state.merges.list();
|
|
13
|
+
const notes = state.notes.list();
|
|
14
|
+
const spent = state.spent.list();
|
|
15
|
+
const asked = state.ops.list().filter(({ op, state: done }) => op.startsWith(QUESTION_OP) && done === "done");
|
|
16
|
+
|
|
17
|
+
const costOf = (item: string, shas: readonly string[]): Counted => ({
|
|
18
|
+
notes: notes.filter((note) => note.item === item || shas.includes(note.change)).length,
|
|
19
|
+
questions: asked.filter((found) => found.itemId === item).length,
|
|
20
|
+
tokens: spent.find((found) => found.item === item)?.total ?? 0,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
merged: merges.map(({ item, sha, at, paths }) => ({ item, at, paths, ...costOf(item, [sha]) })),
|
|
25
|
+
abandoned: state.parked.list().flatMap(({ item, why, since }) => (why === "stuck" && !merges.some((merge) => merge.item === item) ? [{ item, since, paths: [], ...costOf(item, []) }] : [])),
|
|
26
|
+
loopTokens: spent.filter(({ item }) => item.startsWith(LOOP_SPEND_PREFIX)).reduce((total, { total: tokens }) => total + tokens, 0),
|
|
27
|
+
};
|
|
28
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** What an item cost the owner: review notes, questions, and model tokens. */
|
|
2
|
+
export type Counted = { readonly notes: number; readonly questions: number; readonly tokens: number };
|
|
3
|
+
|
|
4
|
+
export type MergedChange = Counted & { readonly item: string; readonly at: number; readonly paths: readonly string[] };
|
|
5
|
+
|
|
6
|
+
/** An item that never merged. Its cost counts all the same, so dropping work never lowers a number. */
|
|
7
|
+
export type AbandonedItem = Counted & { readonly item: string; readonly since: number; readonly paths: readonly string[] };
|
|
8
|
+
|
|
9
|
+
export type PerMerge = { readonly merges: number; readonly abandoned: number; readonly notes: number; readonly questions: number; readonly tokens: number };
|
|
10
|
+
|
|
11
|
+
export type MeasureInput = {
|
|
12
|
+
readonly merged: readonly MergedChange[];
|
|
13
|
+
readonly abandoned: readonly AbandonedItem[];
|
|
14
|
+
/** Tokens spent outside any item, such as comparing review notes. */
|
|
15
|
+
readonly loopTokens: number;
|
|
16
|
+
readonly window: number;
|
|
17
|
+
/** Path globs that each get their own numbers over their own last `window` merged changes. */
|
|
18
|
+
readonly globs: readonly string[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type Measured = PerMerge & {
|
|
22
|
+
readonly window: number;
|
|
23
|
+
readonly byPath: ({ readonly glob: string } & PerMerge)[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const sum = (counted: readonly Counted[], key: keyof Counted): number => counted.reduce((total, found) => total + found[key], 0);
|
|
27
|
+
|
|
28
|
+
const perMerge = (merged: readonly MergedChange[], abandoned: readonly AbandonedItem[], loopTokens: number): PerMerge => {
|
|
29
|
+
const all = [...merged, ...abandoned];
|
|
30
|
+
const merges = merged.length;
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
merges,
|
|
34
|
+
abandoned: abandoned.length,
|
|
35
|
+
notes: sum(all, "notes") / merges,
|
|
36
|
+
questions: sum(all, "questions") / merges,
|
|
37
|
+
tokens: (sum(all, "tokens") + loopTokens) / merges,
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const newestFirst = (merged: readonly MergedChange[], window: number): MergedChange[] => merged.toSorted((a, b) => b.at - a.at).slice(0, window);
|
|
42
|
+
|
|
43
|
+
/** The abandoned items from the oldest merge in the window on. */
|
|
44
|
+
const abandonedSince = (abandoned: readonly AbandonedItem[], inWindow: readonly MergedChange[]): AbandonedItem[] => {
|
|
45
|
+
const start = Math.min(...inWindow.map(({ at }) => at));
|
|
46
|
+
|
|
47
|
+
return abandoned.filter(({ since }) => since >= start);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const touches = (glob: string, paths: readonly string[]): boolean => paths.some((path) => new Bun.Glob(glob).match(path));
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The three numbers per merged change over the last `window` merged changes, and over each path's own last
|
|
54
|
+
* `window`. `undefined` while nothing has merged, when a per-merge number has no meaning.
|
|
55
|
+
*/
|
|
56
|
+
export const measureOf = ({ merged, abandoned, loopTokens, window, globs }: MeasureInput): Measured | undefined => {
|
|
57
|
+
const recent = newestFirst(merged, window);
|
|
58
|
+
|
|
59
|
+
if (recent.length === 0) return undefined;
|
|
60
|
+
|
|
61
|
+
const byPath = globs.flatMap((glob) => {
|
|
62
|
+
const onPath = newestFirst(merged.filter(({ paths }) => touches(glob, paths)), window);
|
|
63
|
+
|
|
64
|
+
return onPath.length === 0 ? [] : [{ glob, ...perMerge(onPath, abandonedSince(abandoned, onPath).filter(({ paths }) => touches(glob, paths)), 0) }];
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return { window, ...perMerge(recent, abandonedSince(abandoned, recent), loopTokens), byPath };
|
|
68
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const CARD_CODES = { workerDown: "worker-down", tokenExpiring: "token-expiring", tokenRejected: "token-rejected" } as const;
|
|
2
|
+
|
|
3
|
+
const WORKER_FIX = "Look at the unit's log for the cause. Starting the unit again lets the worker start again.";
|
|
4
|
+
|
|
5
|
+
const TOKEN_FIX = "Run `claude setup-token`, store it as the CLAUDE_CODE_OAUTH_TOKEN secret and set ciToken.setUp in .hum/hum.json to today.";
|
|
6
|
+
|
|
7
|
+
const ERROR_EXCERPT = 160;
|
|
8
|
+
|
|
9
|
+
const kind =
|
|
10
|
+
<Facts>(code: string, fix: string, cause: (facts: Facts) => string) =>
|
|
11
|
+
(facts: Facts) => ({ code, cause: cause(facts), fix });
|
|
12
|
+
|
|
13
|
+
/** Every card the service layer raises, as a function from the facts it is raised for. */
|
|
14
|
+
export const CARD_KINDS = {
|
|
15
|
+
workerSilent: kind(CARD_CODES.workerDown, WORKER_FIX, ({ seconds, ticks }: { seconds: number; ticks: number }) => `The worker has written no heartbeat for ${seconds} seconds, ${ticks} ticks.`),
|
|
16
|
+
workerGaveUp: kind(
|
|
17
|
+
CARD_CODES.workerDown,
|
|
18
|
+
WORKER_FIX,
|
|
19
|
+
({ crashes, windowMinutes, exitCode }: { crashes: number; windowMinutes: number; exitCode: number }) =>
|
|
20
|
+
`The worker exited ${crashes} times in ${windowMinutes} minutes, the last with code ${exitCode}. Restarts have stopped.`,
|
|
21
|
+
),
|
|
22
|
+
tokenExpiring: kind(CARD_CODES.tokenExpiring, TOKEN_FIX, ({ setUp, validUntil }: { setUp: string; validUntil: string }) => `The CI token made on ${setUp} is valid until ${validUntil}.`),
|
|
23
|
+
tokenRejected: kind(CARD_CODES.tokenRejected, TOKEN_FIX, ({ said }: { said: string }) => `claude was refused with a 401: ${said.slice(0, ERROR_EXCERPT)}`),
|
|
24
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type GitIdentity, gitIn, push, whenGitFailed } from "../adapters/git.ts";
|
|
2
|
+
import { SECOND_MS } from "../time.ts";
|
|
3
|
+
|
|
4
|
+
/** Where the scheduled workflow leaves its last tick. A ref holds no file, so no branch or checkout changes. */
|
|
5
|
+
export const CI_TICK_REF = "refs/hum/ticks/ci";
|
|
6
|
+
|
|
7
|
+
/** The scheduled workflow records its tick as an empty commit dated now, force-pushed to `CI_TICK_REF`. */
|
|
8
|
+
export const recordCiTick = async (repo: string, remote: string, at: number, identity: GitIdentity, timeoutMs: number): Promise<void> => {
|
|
9
|
+
const when = `${Math.floor(at / SECOND_MS)} +0000`;
|
|
10
|
+
const git = gitIn(repo, { identity, timeoutMs, env: { GIT_AUTHOR_DATE: when, GIT_COMMITTER_DATE: when } });
|
|
11
|
+
const tree = (await git.raw(["hash-object", "-w", "-t", "tree", "/dev/null"])).trim();
|
|
12
|
+
const commit = (await git.raw(["commit-tree", tree, "-m", `hum ci tick ${new Date(at).toISOString()}`])).trim();
|
|
13
|
+
|
|
14
|
+
await push(git, ["--force", remote, `${commit}:${CI_TICK_REF}`]);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** The time of the last recorded tick, or undefined when the scheduled workflow has never run or the remote cannot be read. */
|
|
18
|
+
export const readCiTick = async (repo: string, remote: string, timeoutMs: number): Promise<number | undefined> => {
|
|
19
|
+
const git = gitIn(repo, { timeoutMs });
|
|
20
|
+
|
|
21
|
+
const fetched = await git.raw(["fetch", "--quiet", "--no-tags", remote, `+${CI_TICK_REF}:${CI_TICK_REF}`]).then(() => true, whenGitFailed(false));
|
|
22
|
+
|
|
23
|
+
if (!fetched) return undefined;
|
|
24
|
+
|
|
25
|
+
const at = Number((await git.raw(["log", "-1", "--format=%ct", CI_TICK_REF])).trim());
|
|
26
|
+
|
|
27
|
+
return Number.isFinite(at) && at > 0 ? at * SECOND_MS : undefined;
|
|
28
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { build } from "plist";
|
|
2
|
+
import type { UnitBuilder } from "./unit.ts";
|
|
3
|
+
|
|
4
|
+
/** A launchd job that starts at load and again after a failure only: the start script exits 0 when it gives up. */
|
|
5
|
+
export const launchdUnit: UnitBuilder = ({ label, repo, command, path, env, log, restartDelaySeconds }) =>
|
|
6
|
+
build({
|
|
7
|
+
Label: label,
|
|
8
|
+
ProgramArguments: [...command],
|
|
9
|
+
WorkingDirectory: repo,
|
|
10
|
+
EnvironmentVariables: { PATH: path, ...env },
|
|
11
|
+
RunAtLoad: true,
|
|
12
|
+
KeepAlive: { SuccessfulExit: false },
|
|
13
|
+
ThrottleInterval: restartDelaySeconds,
|
|
14
|
+
StandardOutPath: log,
|
|
15
|
+
StandardErrorPath: log,
|
|
16
|
+
});
|