@moo.md/hum 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/defaults/hum.json +14 -2
  2. package/defaults/roles/build.md +1 -1
  3. package/defaults/roles/fix.md +1 -1
  4. package/npm-shrinkwrap.json +3297 -0
  5. package/package.json +6 -2
  6. package/release.json +3 -0
  7. package/schema/hum.schema.json +251 -10
  8. package/scripts/eval.ts +21 -5
  9. package/src/adapter-api.ts +3 -1
  10. package/src/adapters/claude-bg.ts +2 -2
  11. package/src/adapters/claude-p.ts +44 -18
  12. package/src/adapters/contract.ts +12 -0
  13. package/src/adapters/fake.ts +5 -0
  14. package/src/adapters/git.ts +61 -9
  15. package/src/adapters/github-client.ts +15 -0
  16. package/src/adapters/github-commits.ts +84 -0
  17. package/src/adapters/github.ts +36 -4
  18. package/src/adapters/guard.ts +1 -0
  19. package/src/adapters/item-file.ts +1 -1
  20. package/src/adapters/markdown.ts +10 -1
  21. package/src/adapters/pull-requests.ts +8 -8
  22. package/src/adapters/registry.ts +5 -7
  23. package/src/adapters/roundup.ts +3 -0
  24. package/src/bytes.ts +1 -0
  25. package/src/cli/service.ts +176 -29
  26. package/src/cli/setup.ts +16 -7
  27. package/src/config/checks.ts +27 -43
  28. package/src/config/load.ts +2 -3
  29. package/src/config/machine.ts +29 -0
  30. package/src/config/migrate.ts +5 -2
  31. package/src/config/refusal.ts +9 -4
  32. package/src/config/schema.ts +86 -8
  33. package/src/config/wake-job.ts +78 -0
  34. package/src/decide/risk.ts +2 -2
  35. package/src/exec.ts +10 -0
  36. package/src/index.ts +1 -1
  37. package/src/loop/records.ts +8 -1
  38. package/src/measure/rate.ts +14 -0
  39. package/src/publish/bundle.ts +128 -0
  40. package/src/publish/handoff.ts +107 -0
  41. package/src/publish/handover.ts +182 -0
  42. package/src/publish/intents.ts +61 -0
  43. package/src/publish/publisher.ts +259 -0
  44. package/src/publish/wake.ts +78 -0
  45. package/src/service/ci-state.ts +99 -0
  46. package/src/service/plugins.ts +137 -0
  47. package/src/service/wake.ts +3 -1
  48. package/src/service/workflows.ts +261 -50
  49. package/src/setup/propose.ts +87 -3
  50. package/src/setup/prove.ts +3 -3
  51. package/src/setup/scan.ts +61 -5
  52. package/src/setup/tables.ts +18 -0
  53. package/src/setup/write.ts +52 -19
  54. package/src/state/items.ts +17 -8
  55. package/src/state/open.ts +20 -6
  56. package/src/state/records.ts +7 -0
  57. package/src/state/runs.ts +5 -0
  58. package/src/state/saved.ts +17 -2
  59. package/src/worker/base-check.ts +41 -13
  60. package/src/worker/checks.ts +3 -2
  61. package/src/worker/evidence.ts +72 -0
  62. package/src/worker/kind.ts +4 -7
  63. package/src/worker/land.ts +173 -81
  64. package/src/worker/merged.ts +20 -8
  65. package/src/worker/open.ts +33 -17
  66. package/src/worker/questions.ts +16 -8
  67. package/src/worker/signed-push.ts +167 -0
  68. package/src/worker/thin-path.ts +245 -133
  69. package/src/worker/worker.ts +50 -9
  70. package/surface.json +610 -76
@@ -10,7 +10,7 @@ export type BaseCheckOptions = {
10
10
  readonly state: State;
11
11
  readonly config: Config;
12
12
  readonly workSource: Pick<WorkSource, "fileFollowUp">;
13
- /** The owner's checkout; the merged commit is fetched into it. */
13
+ /** A repo whose `origin/<base>` holds the base's history; the merged commit is fetched into it. */
14
14
  readonly git: Git;
15
15
  readonly remote: string;
16
16
  readonly base: string;
@@ -19,27 +19,55 @@ export type BaseCheckOptions = {
19
19
  readonly item: string;
20
20
  };
21
21
 
22
+ /** The repo's checks on the base at some commit, for the merges they cover: their failure, absent when they passed. */
23
+ export type BaseCheck = { readonly items: readonly string[]; readonly failure?: string | undefined };
24
+
22
25
  const FAILURE_EXCERPT = 1500;
23
26
 
27
+ const failureAt = async ({ config, git, checks }: Pick<BaseCheckOptions, "config" | "git" | "checks">, sha: string): Promise<string | undefined> =>
28
+ (await inWorktreeAt(git, sha, (folder) => runChecks(checks, folder, { timeoutMs: checksTimeoutMs(config.hum) })))?.slice(0, FAILURE_EXCERPT);
29
+
24
30
  /**
25
- * After an item merged, the repo's checks run on the base at the merged commit. A failure there is a break: it is
26
- * saved, a follow-up fix item is filed, and the owner's level for the change says whether its paths are blocked until
27
- * that item merges. The fix limit counts the break beside the breaks git shows.
31
+ * Settles the base checks that cover merged items: each merge not yet checked counts as checked, and a failure is a
32
+ * break of each. A break is saved, a follow-up fix item is filed, and the owner's level for the change says whether
33
+ * its paths are blocked until that item merges; the fix limit counts it beside the breaks git shows. A report can only
34
+ * add breaks: an item with no unchecked merge is passed over.
28
35
  */
29
- export const checkBaseAfterMerge = async ({ state, config, workSource, git, remote, base, checks, now, item }: BaseCheckOptions): Promise<void> => {
30
- const merge = state.merges.get(item);
36
+ export const settleBaseCheck = async ({ state, config, workSource, git, base, now }: Omit<BaseCheckOptions, "item" | "remote" | "checks">, { items, failure }: BaseCheck): Promise<void> => {
37
+ for (const item of items) {
38
+ const merge = state.merges.get(item);
39
+
40
+ if (!merge || merge.baseChecked) continue;
41
+
42
+ if (failure !== undefined) {
43
+ const { placement } = await placementOf({ config, state, git, base, now }, { paths: merge.paths, size: 0, loosened: false, pinChanged: false });
44
+ const followUp = await workSource.fileFollowUp(ItemId.parse(item), { title: `Fix the checks that fail on ${base} after ${item} merged`, body: `Kind: fix\n\nThe repo's checks fail on ${base} after ${merge.sha}, the commit that merged ${item}.\n\n\`\`\`\n${failure}\n\`\`\`` });
31
45
 
32
- if (!config.hum.breakCheck || checks.length === 0 || !merge) return;
46
+ state.breaks.put(item, { item, sha: merge.sha, at: now(), paths: merge.paths, level: placement.level.name, failure, onBreak: placement.level.onBreak, followUp: followUp.id });
47
+ }
33
48
 
34
- await git.raw(["fetch", "-q", remote, base]);
49
+ state.merges.put(item, { ...merge, baseChecked: true });
50
+ }
51
+ };
35
52
 
36
- const failure = await inWorktreeAt(git, merge.sha, (folder) => runChecks(checks, folder, { timeoutMs: checksTimeoutMs(config.hum) }));
53
+ /** Whether the owner's config has the base checked after merges: the break check is on and there are checks to run. */
54
+ const checksBase = ({ config, checks }: Pick<BaseCheckOptions, "config" | "checks">): boolean => config.hum.breakCheck && checks.length > 0;
37
55
 
38
- if (failure === undefined) return;
56
+ /** After an item merged, the repo's checks run on the base at the merged commit, and their result is settled. */
57
+ export const checkBaseAfterMerge = async (o: BaseCheckOptions): Promise<void> => {
58
+ const merge = o.state.merges.get(o.item);
39
59
 
40
- const { placement } = await placementOf({ config, state, git, base, now }, { paths: merge.paths, size: 0, loosened: false, pinChanged: false });
41
- const followUp = await workSource.fileFollowUp(ItemId.parse(item), { title: `Fix the checks that fail on ${base} after ${item} merged`, body: `Kind: fix\n\nThe repo's checks fail on ${base} at ${merge.sha}, the commit that merged ${item}.\n\n\`\`\`\n${failure.slice(0, FAILURE_EXCERPT)}\n\`\`\`` });
60
+ if (!checksBase(o) || !merge) return;
42
61
 
43
- state.breaks.put(item, { item, sha: merge.sha, at: now(), paths: merge.paths, level: placement.level.name, failure: failure.slice(0, FAILURE_EXCERPT), onBreak: placement.level.onBreak, followUp: followUp.id });
62
+ await o.git.raw(["fetch", "-q", o.remote, o.base]);
63
+ await settleBaseCheck(o, { items: [o.item], failure: await failureAt(o, merge.sha) });
44
64
  };
45
65
 
66
+ /** The repo's checks at `at`, a commit of the base, for every merge no check has covered yet; undefined when none waits or the break check is off. */
67
+ export const checkMergesAt = async (o: Pick<BaseCheckOptions, "state" | "config" | "git" | "checks">, at: string): Promise<BaseCheck | undefined> => {
68
+ const items = o.state.merges.list().flatMap(({ item, baseChecked }) => (baseChecked ? [] : [item]));
69
+
70
+ if (!checksBase(o) || items.length === 0) return undefined;
71
+
72
+ return { items, failure: await failureAt(o, at) };
73
+ };
@@ -1,5 +1,5 @@
1
1
  import type { HumConfig } from "../config/schema.ts";
2
- import { CommandTimedOut, exec } from "../exec.ts";
2
+ import { CommandTimedOut, exec, withoutGitHubTokens } from "../exec.ts";
3
3
  import { SECOND_MS } from "../time.ts";
4
4
 
5
5
  export type ShellOptions = {
@@ -12,10 +12,11 @@ export type ShellOptions = {
12
12
  /**
13
13
  * One check's failure as its command, exit code and output, or as the limit it ran past; undefined when it passes.
14
14
  * It 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.
15
+ * A run wrote the code a check runs, so a check holds no GitHub token.
15
16
  */
16
17
  const failureOf = async (command: string, cwd: string, { env, timeoutMs }: ShellOptions): Promise<string | undefined> => {
17
18
  try {
18
- const { exitCode, stdout, stderr } = await exec("sh", ["-c", command], { nodePath: false, timeoutMs, nodeOptions: { cwd, env: { ...process.env, ...env }, stdio: ["ignore", "pipe", "pipe"] } });
19
+ const { exitCode, stdout, stderr } = await exec("sh", ["-c", command], { nodePath: false, timeoutMs, nodeOptions: { cwd, env: withoutGitHubTokens({ ...process.env, ...env }), stdio: ["ignore", "pipe", "pipe"] } });
19
20
 
20
21
  return exitCode === 0 ? undefined : `${command} exited ${exitCode}:\n${stdout}${stderr}`;
21
22
  } catch (error) {
@@ -0,0 +1,72 @@
1
+ import { lstatSync } from "node:fs";
2
+ import { basename, extname, join } from "node:path";
3
+ import { type Git, push } from "../adapters/git.ts";
4
+ import type { Evidence } from "../state/saved.ts";
5
+ import { MEGABYTE } from "../bytes.ts";
6
+
7
+ /** The files a pull request body shows, by extension: a picture inline, a video as a link. */
8
+ const KINDS: ReadonlyMap<string, "image" | "video"> = new Map([
9
+ [".png", "image"],
10
+ [".jpg", "image"],
11
+ [".jpeg", "image"],
12
+ [".gif", "image"],
13
+ [".webp", "image"],
14
+ [".mp4", "video"],
15
+ [".mov", "video"],
16
+ [".webm", "video"],
17
+ ]);
18
+
19
+ const MOST_MEGABYTES = 10;
20
+
21
+ /** Where an item's evidence is kept: a ref, never a branch, so no branch rule and no merge reaches it. */
22
+ export const evidenceRef = (item: string): string => `refs/hum/evidence/${item}`;
23
+
24
+ /**
25
+ * Keeps the one picture or video a run saved in `folder` to show its change at work: a commit of that file alone, on
26
+ * the item's evidence ref in this clone for publish to push, and linked on GitHub by that commit. Says why it was left
27
+ * out where it cannot be kept; a name with a folder in it is read from `folder` all the same. Only a plain file is
28
+ * kept: a run could name a link to any file it can read.
29
+ */
30
+ export const keepEvidence = async ({ git, folder, file, item, repo }: { readonly git: Git; readonly folder: string; readonly file: string; readonly item: string; readonly repo: string | undefined }): Promise<Evidence> => {
31
+ const name = basename(file);
32
+ const path = join(folder, name);
33
+ const kind = KINDS.get(extname(name).toLowerCase());
34
+
35
+ if (kind === undefined) return { left: `${name} is no picture or video` };
36
+
37
+ const entry = lstatSync(path, { throwIfNoEntry: false });
38
+
39
+ if (entry === undefined) return { left: `${name} is not in the folder for evidence` };
40
+
41
+ if (!entry.isFile()) return { left: `${name} is no plain file` };
42
+
43
+ if (entry.size > MOST_MEGABYTES * MEGABYTE) return { left: `${name} is over ${MOST_MEGABYTES} MB` };
44
+
45
+ if (repo === undefined) return { left: "the repo is not on GitHub, where a pull request would show it" };
46
+
47
+ const blob = (await git.raw(["hash-object", "-w", path])).trim();
48
+ const tree = (await git.raw(["mktree"], `100644 blob ${blob}\t${name}\n`)).trim();
49
+ const commit = (await git.raw(["commit-tree", tree, "-m", `hum: evidence for ${item}`])).trim();
50
+
51
+ await git.raw(["update-ref", evidenceRef(item), commit]);
52
+
53
+ return { kind, url: `https://github.com/${repo}/blob/${commit}/${encodeURIComponent(name)}?raw=true` };
54
+ };
55
+
56
+ /** Takes the item's evidence ref off `remote`, so no picture outlives the change it showed; a ref already gone is fine. */
57
+ export const dropEvidence = async (git: Git, { remote, item }: { readonly remote: string; readonly item: string }): Promise<void> => {
58
+ const ref = evidenceRef(item);
59
+
60
+ if ((await git.raw(["ls-remote", remote, ref])).trim() === "") return;
61
+
62
+ const dropped = await push(git, [remote, `:${ref}`]);
63
+
64
+ if (dropped.kind !== "pushed") throw new Error(`the evidence ref ${ref} stays on ${remote}: ${dropped.kind === "refused" ? dropped.said : "another push moved it first"}`);
65
+ };
66
+
67
+ /** Evidence as a pull request body shows it: a picture inline, a video as a link, or why the run's file was left out. */
68
+ export const evidenceMarkdown = (evidence: Evidence): string => {
69
+ if ("left" in evidence) return `The run's evidence was left out: ${evidence.left}.`;
70
+
71
+ return evidence.kind === "image" ? `![The change at work](${evidence.url})` : `[A video of the change at work](${evidence.url})`;
72
+ };
@@ -5,7 +5,7 @@ import { declaredKind, kindQuestion, UNSURE } from "../decide/kind.ts";
5
5
  import { PLAIN_QUESTIONS } from "../decide/question.ts";
6
6
  import type { State } from "../state/open.ts";
7
7
  import type { ItemKind } from "../state/saved.ts";
8
- import { queueQuestion, releaseQuestions } from "./questions.ts";
8
+ import { queueQuestion } from "./questions.ts";
9
9
  import { pickedAnswer } from "./thread.ts";
10
10
 
11
11
  export type KindContext = {
@@ -44,14 +44,11 @@ export const pickKind = async (o: Pick<KindContext, "config" | "judge">, item: I
44
44
  return "kind" in picked ? { item: item.id, kind: picked.kind, from: "model" } : { item: item.id, kind: o.config.hum.fallbackKind, from: "fallback", why: picked.why };
45
45
  };
46
46
 
47
- /** Saves the item's kind and, for a fallback, asks the owner once for it; returns the kind's name. */
48
- export const keepKind = async (o: KindContext, item: Item, kind: ItemKind, except?: string): Promise<string> => {
47
+ /** Saves the item's kind and, for a fallback, queues one question to the owner for it; returns the kind's name. */
48
+ export const keepKind = (o: Pick<KindContext, "state" | "config" | "now">, item: Item, kind: ItemKind, except?: string): string => {
49
49
  o.state.itemKinds.put(item.id, kind);
50
50
 
51
- if (kind.from === "fallback") {
52
- queueQuestion(o, item, { text: PLAIN_QUESTIONS.kind, options: kindsBut(o, except), detail: `It runs as ${kind.kind} for now.` }, "kind");
53
- await releaseQuestions(o);
54
- }
51
+ if (kind.from === "fallback") queueQuestion(o, item, { text: PLAIN_QUESTIONS.kind, options: kindsBut(o, except), detail: `It runs as ${kind.kind} for now.` }, "kind");
55
52
 
56
53
  return kind.kind;
57
54
  };