@moo.md/hum 0.0.0-stage → 0.1.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 +392 -0
- package/src/adapters/claude-p.ts +599 -0
- package/src/adapters/contract.ts +231 -0
- package/src/adapters/fake.ts +157 -0
- package/src/adapters/git.ts +156 -0
- package/src/adapters/github.ts +479 -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 +127 -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 +293 -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 +85 -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 +195 -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 +45 -0
- package/src/worker/checks.ts +40 -0
- package/src/worker/kind.ts +68 -0
- package/src/worker/land.ts +509 -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 +750 -0
- package/surface.json +8787 -0
- package/README.md +0 -3
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { cpSync, existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { type Git, gitIn } from "../adapters/git.ts";
|
|
5
|
+
import { newItemFile } from "../adapters/item-file.ts";
|
|
6
|
+
import type { HumConfig } from "../config/schema.ts";
|
|
7
|
+
import { commandTimeoutMs } from "../exec.ts";
|
|
8
|
+
import { atomicWrite } from "../state/fs.ts";
|
|
9
|
+
|
|
10
|
+
type ScratchSettings = Pick<HumConfig, "commitIdentity" | "itemsDir" | "commandTimeoutSeconds">;
|
|
11
|
+
|
|
12
|
+
/** What the owner's checkout contributes to a scratch copy beyond its commits: setup's files, which may not be committed yet. */
|
|
13
|
+
const SETUP_FILES = [".hum", ".github/workflows"] as const;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A throwaway copy of the owner's repo with its own bare remote, so setup can run an item or a contract suite without
|
|
17
|
+
* touching the owner's remote. The copy holds the owner's commits and their uncommitted `.hum/` and workflows.
|
|
18
|
+
*/
|
|
19
|
+
export class Scratch {
|
|
20
|
+
readonly root = mkdtempSync(join(tmpdir(), "hum-prove-"));
|
|
21
|
+
readonly remote = join(this.root, "remote.git");
|
|
22
|
+
readonly owner = join(this.root, "owner");
|
|
23
|
+
readonly #base: string;
|
|
24
|
+
readonly #settings: ScratchSettings;
|
|
25
|
+
#workers = 0;
|
|
26
|
+
|
|
27
|
+
private constructor(base: string, settings: ScratchSettings) {
|
|
28
|
+
this.#base = base;
|
|
29
|
+
this.#settings = settings;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static async from(repo: string, base: string, settings: ScratchSettings): Promise<Scratch> {
|
|
33
|
+
const scratch = new Scratch(base, settings);
|
|
34
|
+
const git = scratch.#git(scratch.root);
|
|
35
|
+
|
|
36
|
+
await git.raw(["clone", "-q", "--bare", repo, scratch.remote]);
|
|
37
|
+
await git.raw(["clone", "-q", scratch.remote, scratch.owner]);
|
|
38
|
+
|
|
39
|
+
for (const file of SETUP_FILES) {
|
|
40
|
+
if (existsSync(join(repo, file))) cpSync(join(repo, file), join(scratch.owner, file), { recursive: true });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await scratch.publish("scratch: the owner's setup files");
|
|
44
|
+
|
|
45
|
+
return scratch;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#git(folder: string): Git {
|
|
49
|
+
return gitIn(folder, { identity: this.#settings.commitIdentity, timeoutMs: commandTimeoutMs(this.#settings) });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async publish(message: string): Promise<void> {
|
|
53
|
+
const git = this.#git(this.owner);
|
|
54
|
+
|
|
55
|
+
await git.raw(["add", "-A"]);
|
|
56
|
+
await git.raw(["commit", "-q", "--allow-empty", "-m", message]);
|
|
57
|
+
await git.raw(["pull", "-q", "--rebase", "origin", this.#base]);
|
|
58
|
+
await git.raw(["push", "-q", "origin", `HEAD:refs/heads/${this.#base}`]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A ready item on the scratch remote's base; returns its id. */
|
|
62
|
+
async seed(id: string, title: string, body: string): Promise<string> {
|
|
63
|
+
atomicWrite(join(this.owner, this.#settings.itemsDir, `${id}.md`), newItemFile(title, body));
|
|
64
|
+
await this.publish(`scratch: item ${id}`);
|
|
65
|
+
|
|
66
|
+
return id;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Another checkout of the scratch remote, for a second worker. */
|
|
70
|
+
async worker(): Promise<string> {
|
|
71
|
+
const folder = join(this.root, `worker-${++this.#workers}`);
|
|
72
|
+
|
|
73
|
+
await this.#git(this.root).raw(["clone", "-q", this.remote, folder]);
|
|
74
|
+
|
|
75
|
+
return folder;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
dispose(): void {
|
|
79
|
+
rmSync(this.root, { recursive: true, force: true });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { type RawConfig, readConfigDir } from "../config/load.ts";
|
|
3
|
+
import { DefaultsConfig, Kind } from "../config/schema.ts";
|
|
4
|
+
|
|
5
|
+
/** Where the shipped defaults sit, in the package or in the owner's `node_modules`. */
|
|
6
|
+
export const DEFAULTS_DIR = join(import.meta.dir, "..", "..", "defaults");
|
|
7
|
+
|
|
8
|
+
export const shippedRaw = (): RawConfig => readConfigDir(DEFAULTS_DIR);
|
|
9
|
+
|
|
10
|
+
/** The config hum ships with, parsed: its settings and its kinds. */
|
|
11
|
+
export const shippedConfig = () => {
|
|
12
|
+
const raw = shippedRaw();
|
|
13
|
+
|
|
14
|
+
return { hum: DefaultsConfig.parse(raw.hum), kinds: Object.fromEntries(Object.entries(raw.kinds).map(([name, file]) => [name, Kind.parse(file)])) };
|
|
15
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/** The package manager each lockfile says. */
|
|
2
|
+
export const LOCKFILES = {
|
|
3
|
+
"bun.lock": "bun",
|
|
4
|
+
"bun.lockb": "bun",
|
|
5
|
+
"pnpm-lock.yaml": "pnpm",
|
|
6
|
+
"yarn.lock": "yarn",
|
|
7
|
+
"package-lock.json": "npm",
|
|
8
|
+
"uv.lock": "uv",
|
|
9
|
+
"poetry.lock": "poetry",
|
|
10
|
+
"Cargo.lock": "cargo",
|
|
11
|
+
"go.sum": "go",
|
|
12
|
+
} as const satisfies Record<string, string>;
|
|
13
|
+
|
|
14
|
+
export type PackageManager = (typeof LOCKFILES)[keyof typeof LOCKFILES];
|
|
15
|
+
|
|
16
|
+
/** The command that installs from the lockfile alone, per package manager. */
|
|
17
|
+
export const INSTALL_COMMANDS = {
|
|
18
|
+
bun: "bun install --frozen-lockfile",
|
|
19
|
+
pnpm: "pnpm install --frozen-lockfile",
|
|
20
|
+
yarn: "yarn install --immutable",
|
|
21
|
+
npm: "npm ci",
|
|
22
|
+
uv: "uv sync --frozen",
|
|
23
|
+
poetry: "poetry install --sync",
|
|
24
|
+
cargo: "cargo fetch --locked",
|
|
25
|
+
go: "go mod download",
|
|
26
|
+
} as const satisfies Record<PackageManager, string>;
|
|
27
|
+
|
|
28
|
+
/** The command that runs a package script, per package manager that has scripts. */
|
|
29
|
+
export const RUN_SCRIPT: Readonly<Record<PackageManager, string | undefined>> = { bun: "bun run", pnpm: "pnpm run", yarn: "yarn", npm: "npm run", uv: undefined, poetry: undefined, cargo: undefined, go: undefined };
|
|
30
|
+
|
|
31
|
+
/** Package scripts that check a change, in the order a repo's own run gives them. */
|
|
32
|
+
export const CHECK_SCRIPTS = ["check", "typecheck", "lint", "test"] as const;
|
|
33
|
+
|
|
34
|
+
/** Package scripts, justfile and Makefile targets that set up or seed an environment. */
|
|
35
|
+
export const SETUP_TARGETS = ["setup", "bootstrap", "seed", "dev:setup", "db:seed", "db:setup"] as const;
|
|
36
|
+
|
|
37
|
+
/** A CI command line that checks the change. */
|
|
38
|
+
export const CHECK_COMMAND = /\b(test|tests|lint|typecheck|type-check|tsc|check|vitest|jest|pytest|clippy|eslint|oxlint|biome|ruff|mypy|vet)\b/;
|
|
39
|
+
|
|
40
|
+
/** A CI command line that is not a check, whatever words it holds: installs, publishing, shell plumbing. */
|
|
41
|
+
export const NOT_A_CHECK = /^(echo|export|cd|mkdir|cp|mv|rm|curl|wget|sudo|gh|git)\b|\b(install|publish|deploy|release|upload|setup)\b/;
|
|
42
|
+
|
|
43
|
+
/** The files that hold a repo's tool versions. */
|
|
44
|
+
export const TOOL_VERSION_FILES = [".tool-versions", ".nvmrc", ".node-version", ".bun-version", ".python-version", "mise.toml", ".mise.toml", "rust-toolchain.toml", ".go-version"] as const;
|
|
45
|
+
|
|
46
|
+
export const ENV_EXAMPLE_FILES = [".env.example", ".env.sample", ".env.template"] as const;
|
|
47
|
+
|
|
48
|
+
/** The owner's own environment tooling: a recipe calls it and rebuilds nothing. */
|
|
49
|
+
export const ENV_TOOLING_FILES = [".envrc", "mise.toml", ".mise.toml", "devbox.json", "flake.nix", ".devcontainer/devcontainer.json", "Tiltfile"] as const;
|
|
50
|
+
|
|
51
|
+
export const COMPOSE_FILES = ["compose.yaml", "compose.yml", "docker-compose.yaml", "docker-compose.yml"] as const;
|
|
52
|
+
|
|
53
|
+
export const CODEOWNERS_FILES = [".github/CODEOWNERS", "CODEOWNERS", "docs/CODEOWNERS"] as const;
|
|
54
|
+
|
|
55
|
+
export const WORKFLOW_FOLDER = ".github/workflows";
|
|
56
|
+
|
|
57
|
+
export const ISSUE_TEMPLATE_FOLDER = ".github/ISSUE_TEMPLATE";
|
|
58
|
+
|
|
59
|
+
/** Paths that hold data or secrets, as globs; setup proposes the ones a repo's tracked files match. */
|
|
60
|
+
export const DATA_AND_SECRET_GLOBS = [
|
|
61
|
+
"**/migrations/**",
|
|
62
|
+
"**/*.sql",
|
|
63
|
+
"**/.env",
|
|
64
|
+
"**/.env.*",
|
|
65
|
+
"**/*.pem",
|
|
66
|
+
"**/*.key",
|
|
67
|
+
"**/secrets/**",
|
|
68
|
+
"**/credentials*",
|
|
69
|
+
"**/*.tfstate",
|
|
70
|
+
"**/schema.prisma",
|
|
71
|
+
] as const;
|
|
72
|
+
|
|
73
|
+
/** A file that shows the shape of a secret without holding one. */
|
|
74
|
+
export const SECRET_TEMPLATE = /\.(example|sample|template)$/;
|
|
75
|
+
|
|
76
|
+
/** How careful a top-level folder is, as a position among the risk levels: 0 is the lowest. */
|
|
77
|
+
export const FOLDER_RANKS = [
|
|
78
|
+
{ rank: 0, folder: /^(docs?|documentation|examples?|samples?)$/ },
|
|
79
|
+
{ rank: 2, folder: /^(infra|infrastructure|deploy|deployment|terraform|k8s|helm|ops|\.github|\.circleci)$/ },
|
|
80
|
+
] as const;
|
|
81
|
+
|
|
82
|
+
export const DEFAULT_FOLDER_RANK = 1;
|
|
83
|
+
|
|
84
|
+
/** The risk level position of a root-level markdown file, and of any other root file. */
|
|
85
|
+
export const ROOT_MARKDOWN_RANK = 0;
|
|
86
|
+
|
|
87
|
+
/** The words a repo's labels use for a kind of work, per kind name. A label the table lacks goes to the kind question. */
|
|
88
|
+
export const KIND_LABEL_WORDS = {
|
|
89
|
+
fix: ["bug", "bugfix", "defect", "regression", "fix", "type: bug", "kind/bug"],
|
|
90
|
+
feature: ["feature", "enhancement", "feat", "improvement", "type: feature", "kind/feature"],
|
|
91
|
+
docs: ["docs", "documentation", "doc", "type: docs", "kind/documentation"],
|
|
92
|
+
hotfix: ["hotfix", "urgent", "incident", "p0", "critical"],
|
|
93
|
+
} as const satisfies Record<string, readonly string[]>;
|
|
94
|
+
|
|
95
|
+
/** Hours a wait gets before it ends stuck, by who it waits on. */
|
|
96
|
+
export const WAIT_HOURS = { owner: 72, other: 24 } as const;
|
|
97
|
+
|
|
98
|
+
export const HOUR_MS = 3_600_000;
|
|
99
|
+
|
|
100
|
+
/** Tracked files setup reads the repo's shape from, at most. */
|
|
101
|
+
export const TRACKED_FILE_LIMIT = 50_000;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { OWNER_WAIT_TAG, STUCK_TAG, WAIT_TAG, type Kind, type StateNodeJSON } from "../config/schema.ts";
|
|
2
|
+
import { HOUR_MS, WAIT_HOURS } from "./tables.ts";
|
|
3
|
+
|
|
4
|
+
/** A state a kind's machine waits in, with the hours its timeout proposes. */
|
|
5
|
+
export type Wait = { readonly kind: string; readonly state: string; readonly waitsOn: "owner" | "other"; readonly hours: number };
|
|
6
|
+
|
|
7
|
+
/** The answer for one wait: the hours after which the item ends stuck. */
|
|
8
|
+
export type WaitAnswers = Readonly<Record<string, number>>;
|
|
9
|
+
|
|
10
|
+
/** The key a wait's answer sits under. */
|
|
11
|
+
export const waitKey = ({ kind, state }: Pick<Wait, "kind" | "state">): string => `${kind}/${state}`;
|
|
12
|
+
|
|
13
|
+
const isOwnerWait = (tags: readonly string[] | undefined): boolean => tags?.includes(OWNER_WAIT_TAG) ?? false;
|
|
14
|
+
|
|
15
|
+
const isWait = (tags: readonly string[] | undefined): boolean => isOwnerWait(tags) || (tags?.includes(WAIT_TAG) ?? false);
|
|
16
|
+
|
|
17
|
+
/** Every waiting state of the kinds, each with a proposed timeout; setup asks the owner one question per wait. */
|
|
18
|
+
export const waitsOf = (kinds: Readonly<Record<string, Kind>>): Wait[] =>
|
|
19
|
+
Object.entries(kinds).flatMap(([kind, config]) =>
|
|
20
|
+
Object.entries(config.machine.states).flatMap(([state, node]): Wait[] => {
|
|
21
|
+
if (!isWait(node.tags)) return [];
|
|
22
|
+
|
|
23
|
+
const waitsOn = isOwnerWait(node.tags) ? "owner" : "other";
|
|
24
|
+
|
|
25
|
+
return [{ kind, state, waitsOn, hours: WAIT_HOURS[waitsOn] }];
|
|
26
|
+
}),
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
/** The state a timed-out wait goes to: the kind's first stuck state. */
|
|
30
|
+
const stuckStateOf = (kind: string, config: Kind): string => {
|
|
31
|
+
const stuck = Object.entries(config.machine.states).find(([, node]) => node.tags?.includes(STUCK_TAG));
|
|
32
|
+
|
|
33
|
+
if (!stuck) throw new Error(`kind "${kind}" has no state tagged "${STUCK_TAG}" for a timed-out wait to go to`);
|
|
34
|
+
|
|
35
|
+
return stuck[0];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const hasTimeout = (node: StateNodeJSON): boolean => node.timeout !== undefined || Object.keys(node.after ?? {}).length > 0;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The kinds with a timeout on every wait: the hours answered, else the proposed hours. A timed-out wait ends stuck.
|
|
42
|
+
* A wait that already has a timeout keeps it.
|
|
43
|
+
*/
|
|
44
|
+
export const withTimeouts = (kinds: Readonly<Record<string, Kind>>, answers: WaitAnswers): Record<string, Kind> =>
|
|
45
|
+
Object.fromEntries(
|
|
46
|
+
Object.entries(kinds).map(([kind, config]) => {
|
|
47
|
+
const stuck = stuckStateOf(kind, config);
|
|
48
|
+
const hoursOf = new Map(waitsOf({ [kind]: config }).map((wait) => [wait.state, answers[waitKey(wait)] ?? wait.hours]));
|
|
49
|
+
|
|
50
|
+
const states = Object.entries(config.machine.states).map(([state, node]): [string, StateNodeJSON] => {
|
|
51
|
+
const hours = hoursOf.get(state);
|
|
52
|
+
|
|
53
|
+
return [state, hours === undefined || hasTimeout(node) ? node : { ...node, after: { [String(hours * HOUR_MS)]: [{ target: stuck }] } }];
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return [kind, { ...config, machine: { ...config.machine, states: Object.fromEntries(states) } }];
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import type { GitIdentityRead } from "../adapters/git.ts";
|
|
6
|
+
import { type Config, loadConfig, type RawConfig, readConfigDir } from "../config/load.ts";
|
|
7
|
+
import { hashJson } from "../config/hash.ts";
|
|
8
|
+
import { Json } from "../config/json.ts";
|
|
9
|
+
import { migrateConfig } from "../config/migrate.ts";
|
|
10
|
+
import type { Refusal, Result } from "../config/refusal.ts";
|
|
11
|
+
import { Kind, READABLE_SCHEMA_VERSIONS, SCHEMA_VERSION } from "../config/schema.ts";
|
|
12
|
+
import { ciWorkflows } from "../service/workflows.ts";
|
|
13
|
+
import { renderUnit, type UnitKind } from "../service/unit.ts";
|
|
14
|
+
import { atomicWrite } from "../state/fs.ts";
|
|
15
|
+
import { shippedRaw } from "./shipped.ts";
|
|
16
|
+
import { withTimeouts } from "./waits.ts";
|
|
17
|
+
|
|
18
|
+
const WORKFLOW_FOLDER = join(".github", "workflows");
|
|
19
|
+
|
|
20
|
+
const HumObject = z.record(z.string(), Json);
|
|
21
|
+
|
|
22
|
+
/** The answers come from a model's JSON, so no answer may name the identity every commit is credited to: it comes from the owner's git config or their own hum.json. */
|
|
23
|
+
const AnsweredSettings = HumObject.refine((hum) => !("commitIdentity" in hum), "commitIdentity is never an answer: setup reads it from git config");
|
|
24
|
+
|
|
25
|
+
/** What the owner decided: the `hum.json` keys they set, the hours of each wait, and the schema the writer targets. */
|
|
26
|
+
export const SetupAnswers = z.strictObject({
|
|
27
|
+
hum: AnsweredSettings.default({}),
|
|
28
|
+
waitHours: z.record(z.string(), z.number().positive()).default({}),
|
|
29
|
+
/** The schema version the writing skill produces; a newer one than this engine reads is refused. */
|
|
30
|
+
schemaVersion: z.int().optional(),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export type SetupAnswers = z.infer<typeof SetupAnswers>;
|
|
34
|
+
|
|
35
|
+
export type SetupEnv = {
|
|
36
|
+
readonly hopeSkills: ReadonlySet<string>;
|
|
37
|
+
/** Where a Mac or Linux service unit is written; default the user's home. */
|
|
38
|
+
readonly home?: string;
|
|
39
|
+
readonly platform?: NodeJS.Platform;
|
|
40
|
+
/** The program and PATH a unit starts `hum serve` with. */
|
|
41
|
+
readonly runtime?: { readonly bun: string; readonly path: string };
|
|
42
|
+
/** The owner's name and email as git config holds them: the identity hum.json takes unless it names one already. */
|
|
43
|
+
readonly gitIdentity: GitIdentityRead;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type SetupResult = { readonly ok: true; readonly written: readonly string[]; readonly config: Config } | { readonly ok: false; readonly refusal: Refusal };
|
|
47
|
+
|
|
48
|
+
const UPDATE_ENGINE = "Update the engine: bun add --exact @moo.md/hum@latest.";
|
|
49
|
+
|
|
50
|
+
/** Refuses a schema the installed engine does not read, whether the existing file has it or the writer would produce it. */
|
|
51
|
+
const schemaRefusal = (existing: Json | undefined, target: number): Refusal | undefined => {
|
|
52
|
+
if (!READABLE_SCHEMA_VERSIONS.includes(target)) {
|
|
53
|
+
return {
|
|
54
|
+
code: "unknown-schema-version",
|
|
55
|
+
found: target,
|
|
56
|
+
readable: READABLE_SCHEMA_VERSIONS,
|
|
57
|
+
message: `Setup would write hum.json schemaVersion ${target}, and this engine reads ${READABLE_SCHEMA_VERSIONS.join(", ")}. ${UPDATE_ENGINE} Nothing was written.`,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const migrated = existing === undefined ? undefined : migrateConfig(existing);
|
|
62
|
+
|
|
63
|
+
return migrated && !migrated.ok ? { ...migrated.refusal, message: `${migrated.refusal.message} Nothing was written.` } : undefined;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const noGitIdentity = (missing: readonly string[]): Refusal => ({
|
|
67
|
+
code: "no-git-identity",
|
|
68
|
+
missing,
|
|
69
|
+
message: `hum commits as you, and git config holds no ${missing.join(" or ")}. Set ${missing.length === 1 ? "it" : "each"}, then run setup again: ${missing.map((key) => `git config --global ${key} "<yours>"`).join("; ")}. Nothing was written.`,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/** The identity hum.json already holds, else the one git config holds, else the refusal that says what to set. */
|
|
73
|
+
const identityOf = (hum: Readonly<Record<string, Json>>, git: GitIdentityRead): Result<Json> => {
|
|
74
|
+
if (hum.commitIdentity !== undefined) return { ok: true, value: hum.commitIdentity };
|
|
75
|
+
|
|
76
|
+
return git.found ? { ok: true, value: asJson(git.identity) } : { ok: false, refusal: noGitIdentity(git.missing) };
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const readExisting = (repo: string): RawConfig | undefined => (existsSync(join(repo, ".hum", "hum.json")) ? readConfigDir(join(repo, ".hum")) : undefined);
|
|
80
|
+
|
|
81
|
+
type FileToWrite = { readonly path: string; readonly text: string };
|
|
82
|
+
|
|
83
|
+
const unitPath = (kind: UnitKind, label: string, home: string): string =>
|
|
84
|
+
kind === "launchd" ? join(home, "Library", "LaunchAgents", `${label}.plist`) : join(home, ".config", "systemd", "user", `${label}.service`);
|
|
85
|
+
|
|
86
|
+
const jsonText = (value: Json): string => `${JSON.stringify(value, null, "\t")}\n`;
|
|
87
|
+
|
|
88
|
+
/** The keys of `hum.json` the owner's file holds: the structure every config needs, and each setting that is not the shipped default. */
|
|
89
|
+
const STRUCTURE = ["schemaVersion", "levels", "paths", "roles", "events", "kinds", "integrations"] as const;
|
|
90
|
+
|
|
91
|
+
const STRUCTURAL = new Set<string>(STRUCTURE);
|
|
92
|
+
|
|
93
|
+
const ownerKeys = (hum: Readonly<Record<string, Json>>, shipped: Readonly<Record<string, Json>>): Record<string, Json> =>
|
|
94
|
+
Object.fromEntries(Object.entries(hum).filter(([key, value]) => STRUCTURAL.has(key) || hashJson(value) !== hashJson(shipped[key] ?? null)));
|
|
95
|
+
|
|
96
|
+
const asJson = (value: Kind | Readonly<Record<string, Json>>): Json => Json.parse(JSON.parse(JSON.stringify(value)));
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Writes `.hum/` and the service files from the owner's answers, or writes nothing: the config must pass every check
|
|
100
|
+
* with every wait timed out, and must be a schema the installed engine reads (and the one already in the repo).
|
|
101
|
+
*/
|
|
102
|
+
export const writeSetup = (repo: string, answers: SetupAnswers, env: SetupEnv): SetupResult => {
|
|
103
|
+
const existing = readExisting(repo);
|
|
104
|
+
const refused = schemaRefusal(existing?.hum, answers.schemaVersion ?? SCHEMA_VERSION);
|
|
105
|
+
|
|
106
|
+
if (refused) return { ok: false, refusal: refused };
|
|
107
|
+
|
|
108
|
+
const shipped = shippedRaw();
|
|
109
|
+
const base = existing ?? shipped;
|
|
110
|
+
const shippedHum = HumObject.parse(shipped.hum);
|
|
111
|
+
const given = { ...HumObject.parse(base.hum), ...answers.hum };
|
|
112
|
+
const identity = identityOf(given, env.gitIdentity);
|
|
113
|
+
|
|
114
|
+
if (!identity.ok) return identity;
|
|
115
|
+
|
|
116
|
+
const hum = { ...given, commitIdentity: identity.value };
|
|
117
|
+
const kinds = Object.fromEntries(Object.entries(withTimeouts(parseKinds(base), answers.waitHours)).map(([name, kind]) => [name, asJson(kind)]));
|
|
118
|
+
const roles = { ...shipped.roles, ...existing?.roles };
|
|
119
|
+
const loaded = loadConfig({ hum: asJson(hum), kinds, roles }, { env: { hopeSkills: env.hopeSkills } });
|
|
120
|
+
|
|
121
|
+
if (!loaded.ok) return { ok: false, refusal: loaded.refusal };
|
|
122
|
+
|
|
123
|
+
const files = [
|
|
124
|
+
{ path: join(repo, ".hum", "hum.json"), text: jsonText(ownerKeys(hum, shippedHum)) },
|
|
125
|
+
...Object.entries(kinds).map(([name, kind]) => ({ path: join(repo, ".hum", "kinds", `${name}.json`), text: jsonText(kind) })),
|
|
126
|
+
...Object.entries(roles).map(([file, text]) => ({ path: join(repo, ".hum", file), text })),
|
|
127
|
+
...(existsSync(join(repo, ".hum", "memory.md")) ? [] : [{ path: join(repo, ".hum", "memory.md"), text: "" }]),
|
|
128
|
+
...(existsSync(join(repo, loaded.value.hum.itemsDir, ".gitkeep")) ? [] : [{ path: join(repo, loaded.value.hum.itemsDir, ".gitkeep"), text: "" }]),
|
|
129
|
+
...(loaded.value.hum.runPlaces.includes("ci") ? ciWorkflows(loaded.value.hum).map(({ file, text }) => ({ path: join(repo, WORKFLOW_FOLDER, file), text })) : []),
|
|
130
|
+
...(loaded.value.hum.runPlaces.includes("mac") ? [serviceFile(repo, loaded.value, env)] : []),
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
for (const { path, text } of files) atomicWrite(path, text);
|
|
134
|
+
|
|
135
|
+
return { ok: true, written: files.map(({ path }) => path), config: loaded.value };
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const parseKinds = (raw: RawConfig): Record<string, Kind> => Object.fromEntries(Object.entries(raw.kinds).map(([name, file]) => [name, Kind.parse(file)]));
|
|
139
|
+
|
|
140
|
+
const serviceFile = (repo: string, config: Config, env: SetupEnv): FileToWrite => {
|
|
141
|
+
const kind: UnitKind = (env.platform ?? process.platform) === "darwin" ? "launchd" : "systemd";
|
|
142
|
+
const home = env.home ?? homedir();
|
|
143
|
+
const bun = env.runtime?.bun ?? process.execPath;
|
|
144
|
+
const label = config.hum.serviceLabel;
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
path: unitPath(kind, label, home),
|
|
148
|
+
text: renderUnit(kind, {
|
|
149
|
+
label,
|
|
150
|
+
repo,
|
|
151
|
+
command: [bun, join(repo, "node_modules", ".bin", "hum"), "serve"],
|
|
152
|
+
path: env.runtime?.path ?? [dirname(bun), dirname(Bun.which("git") ?? "/usr/bin/git"), dirname(Bun.which("claude") ?? "/usr/local/bin/claude"), "/usr/bin", "/bin"].join(":"),
|
|
153
|
+
env: {},
|
|
154
|
+
log: join(home, kind === "launchd" ? join("Library", "Logs") : join(".local", "state"), `${label}.log`),
|
|
155
|
+
restartDelaySeconds: config.hum.restartDelaySeconds,
|
|
156
|
+
}),
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** The answers file the skill writes: `{ "hum": {...}, "waitHours": {...}, "schemaVersion": 1 }`. */
|
|
161
|
+
export const readAnswers = (file: string): SetupAnswers => SetupAnswers.parse(JSON.parse(readFileSync(file, "utf8")));
|
package/src/slots/env.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { basename, join, resolve } from "node:path";
|
|
2
|
+
import type { HumConfig } from "../config/schema.ts";
|
|
3
|
+
|
|
4
|
+
/** The variables a slot's recipe commands, repo checks and runs read. The recipe is written against these names. */
|
|
5
|
+
export const SLOT_ENV = {
|
|
6
|
+
slot: "HUM_SLOT",
|
|
7
|
+
folder: "HUM_SLOT_FOLDER",
|
|
8
|
+
portBase: "HUM_SLOT_PORT_BASE",
|
|
9
|
+
database: "HUM_SLOT_DATABASE",
|
|
10
|
+
composeProject: "COMPOSE_PROJECT_NAME",
|
|
11
|
+
} as const;
|
|
12
|
+
|
|
13
|
+
export type SlotIdentity = { readonly name: string; readonly index: number; readonly folder: string };
|
|
14
|
+
|
|
15
|
+
type Settings = Pick<HumConfig["runSlots"], "services" | "ports" | "projectPrefix">;
|
|
16
|
+
|
|
17
|
+
const NOT_NAME_CHARACTER = /[^A-Za-z0-9]+/g;
|
|
18
|
+
|
|
19
|
+
/** The name of the slot at this position among the slots. */
|
|
20
|
+
export const slotName = (index: number): string => `slot-${index + 1}`;
|
|
21
|
+
|
|
22
|
+
/** Where a slot's worktree is: in the pool folder, named for the checkout it belongs to and the slot. */
|
|
23
|
+
export const slotFolder = (repo: string, pool: string, name: string): string => join(resolve(repo, pool), `${basename(repo)}-${name}`);
|
|
24
|
+
|
|
25
|
+
const isShared = ({ services }: Settings): boolean => services === "shared";
|
|
26
|
+
|
|
27
|
+
/** The port a slot's services start from: the start of its own block, or the one block every slot shares. */
|
|
28
|
+
const portBaseOf = (settings: Settings, index: number): number => settings.ports.base + (isShared(settings) ? 0 : index * settings.ports.stride);
|
|
29
|
+
|
|
30
|
+
/** One compose project for the slot, or for all slots when they share one set of services. */
|
|
31
|
+
const projectOf = (settings: Settings, name: string): string => `${settings.projectPrefix}-${isShared(settings) ? "shared" : name}`;
|
|
32
|
+
|
|
33
|
+
/** The database only this slot's runs reach on a shared set of services. */
|
|
34
|
+
const databaseOf = ({ projectPrefix }: Settings, name: string): string => `${projectPrefix}_${name}`.replace(NOT_NAME_CHARACTER, "_");
|
|
35
|
+
|
|
36
|
+
export const slotEnv = (settings: Settings, { name, index, folder }: SlotIdentity) => ({
|
|
37
|
+
[SLOT_ENV.slot]: name,
|
|
38
|
+
[SLOT_ENV.folder]: folder,
|
|
39
|
+
[SLOT_ENV.portBase]: String(portBaseOf(settings, index)),
|
|
40
|
+
[SLOT_ENV.database]: databaseOf(settings, name),
|
|
41
|
+
[SLOT_ENV.composeProject]: projectOf(settings, name),
|
|
42
|
+
});
|