@moo.md/hum 0.3.0 → 0.4.1

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 (67) hide show
  1. package/defaults/hum.json +5 -0
  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 +47 -7
  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 +9 -0
  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 +3 -6
  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 +28 -33
  27. package/src/config/checks.ts +21 -50
  28. package/src/config/load.ts +2 -3
  29. package/src/config/machine.ts +29 -0
  30. package/src/config/refusal.ts +5 -6
  31. package/src/config/schema.ts +23 -7
  32. package/src/config/wake-job.ts +6 -2
  33. package/src/decide/risk.ts +2 -2
  34. package/src/exec.ts +10 -0
  35. package/src/index.ts +1 -1
  36. package/src/loop/records.ts +8 -1
  37. package/src/measure/rate.ts +22 -0
  38. package/src/publish/bundle.ts +128 -0
  39. package/src/publish/handoff.ts +107 -0
  40. package/src/publish/handover.ts +182 -0
  41. package/src/publish/intents.ts +61 -0
  42. package/src/publish/publisher.ts +259 -0
  43. package/src/publish/wake.ts +78 -0
  44. package/src/service/ci-state.ts +99 -0
  45. package/src/service/plugins.ts +137 -0
  46. package/src/service/wake.ts +3 -1
  47. package/src/service/workflows.ts +126 -30
  48. package/src/setup/propose.ts +17 -4
  49. package/src/setup/prove.ts +3 -3
  50. package/src/setup/write.ts +2 -2
  51. package/src/state/items.ts +17 -8
  52. package/src/state/open.ts +20 -6
  53. package/src/state/records.ts +7 -0
  54. package/src/state/runs.ts +5 -0
  55. package/src/state/saved.ts +17 -2
  56. package/src/worker/base-check.ts +41 -13
  57. package/src/worker/checks.ts +3 -2
  58. package/src/worker/evidence.ts +72 -0
  59. package/src/worker/kind.ts +4 -7
  60. package/src/worker/land.ts +173 -81
  61. package/src/worker/merged.ts +20 -8
  62. package/src/worker/open.ts +33 -17
  63. package/src/worker/questions.ts +16 -8
  64. package/src/worker/signed-push.ts +167 -0
  65. package/src/worker/thin-path.ts +245 -133
  66. package/src/worker/worker.ts +50 -9
  67. package/surface.json +185 -56
@@ -52,6 +52,15 @@ export const persistedSnapshot = (actor: AnyActor): Json => Json.parse(JSON.pars
52
52
 
53
53
  const reason = (cause: unknown): string => (cause instanceof Error ? cause.message : String(cause));
54
54
 
55
+ /** The cards an item's saved state raises: the only cards a work job hands publish. */
56
+ export const SNAPSHOT_CARDS = {
57
+ writeFailed: "snapshot-write-failed",
58
+ unreadable: "snapshot-unreadable",
59
+ restoreError: "snapshot-restore-error",
60
+ newerEngine: "snapshot-from-newer-engine",
61
+ machineUnknown: "snapshot-machine-unknown",
62
+ } as const;
63
+
55
64
  const DAMAGED_FIX = "Restore the file from a backup, or delete it to start the item over; the other items go on.";
56
65
 
57
66
  /** Per-item snapshots under `items/`. Every failure becomes one fault card; nothing is thrown for bad data. */
@@ -86,7 +95,7 @@ export class Items {
86
95
  return {
87
96
  ok: false,
88
97
  fault: this.#faults.raise({
89
- code: "snapshot-write-failed",
98
+ code: SNAPSHOT_CARDS.writeFailed,
90
99
  item: itemId,
91
100
  cause: `The state of ${itemId} could not be saved: ${reason(error)}. The last saved state is kept.`,
92
101
  fix: "Free disk space where hum keeps its state; the next change saves it again.",
@@ -94,7 +103,7 @@ export class Items {
94
103
  };
95
104
  }
96
105
 
97
- this.#faults.clear({ code: "snapshot-write-failed", item: itemId });
106
+ this.#faults.clear({ code: SNAPSHOT_CARDS.writeFailed, item: itemId });
98
107
 
99
108
  return { ok: true, value: undefined };
100
109
  }
@@ -131,8 +140,8 @@ export class Items {
131
140
  */
132
141
  async restore(itemId: string, kind: Kind, { kindAt, sources = {}, clock }: RestoreOptions = {}): Promise<Outcome<AnyActor | undefined>> {
133
142
  const file = this.path(itemId);
134
- const unreadable = (why: string) => this.#fault({ itemId, code: "snapshot-unreadable", cause: `The saved state of ${itemId} at ${file} is damaged: ${why}.`, fix: DAMAGED_FIX });
135
- const unrestorable = (why: string) => this.#fault({ itemId, code: "snapshot-restore-error", cause: `The saved state of ${itemId} does not restore: ${why}.`, fix: DAMAGED_FIX });
143
+ const unreadable = (why: string) => this.#fault({ itemId, code: SNAPSHOT_CARDS.unreadable, cause: `The saved state of ${itemId} at ${file} is damaged: ${why}.`, fix: DAMAGED_FIX });
144
+ const unrestorable = (why: string) => this.#fault({ itemId, code: SNAPSHOT_CARDS.restoreError, cause: `The saved state of ${itemId} does not restore: ${why}.`, fix: DAMAGED_FIX });
136
145
  let parsed: Json;
137
146
 
138
147
  try {
@@ -142,7 +151,7 @@ export class Items {
142
151
 
143
152
  if (error instanceof SyntaxError) return unreadable(error.message);
144
153
 
145
- return this.#fault({ itemId, code: "snapshot-unreadable", cause: `The saved state of ${itemId} at ${file} cannot be read: ${reason(error)}.`, fix: "Fix the file's permissions or restore it from a backup; the other items go on." });
154
+ return this.#fault({ itemId, code: SNAPSHOT_CARDS.unreadable, cause: `The saved state of ${itemId} at ${file} cannot be read: ${reason(error)}.`, fix: "Fix the file's permissions or restore it from a backup; the other items go on." });
146
155
  }
147
156
 
148
157
  const header = Header.safeParse(parsed);
@@ -150,7 +159,7 @@ export class Items {
150
159
  if (!header.success) return unreadable("it has no format or engine version");
151
160
 
152
161
  if (header.data.format > this.#format || isFromNewerEngine({ written: header.data.engineVersion, running: this.#engineVersion })) {
153
- return this.#fault({ itemId, code: "snapshot-from-newer-engine", cause: `The saved state of ${itemId} was written by hum ${header.data.engineVersion}, which is newer than the running ${this.#engineVersion}.`, fix: "Update the engine to that version or later: bun add --exact @moo.md/hum@latest." });
162
+ return this.#fault({ itemId, code: SNAPSHOT_CARDS.newerEngine, cause: `The saved state of ${itemId} was written by hum ${header.data.engineVersion}, which is newer than the running ${this.#engineVersion}.`, fix: "Update the engine to that version or later: bun add --exact @moo.md/hum@latest." });
154
163
  }
155
164
 
156
165
  let envelope: ReturnType<typeof Envelope.safeParse>;
@@ -171,12 +180,12 @@ export class Items {
171
180
  const previous = kindAt?.(machineVersion);
172
181
 
173
182
  if (!previous) {
174
- return this.#fault({ itemId, code: "snapshot-machine-unknown", cause: `${itemId} was saved under version ${machineVersion} of its machine, and this config holds no map from it.`, fix: "Add a stateMap for that version to the kind file, or delete the item's saved state." });
183
+ return this.#fault({ itemId, code: SNAPSHOT_CARDS.machineUnknown, cause: `${itemId} was saved under version ${machineVersion} of its machine, and this config holds no map from it.`, fix: "Add a stateMap for that version to the kind file, or delete the item's saved state." });
175
184
  }
176
185
 
177
186
  current = await migrateSnapshot(previous, kind, snapshot, sources);
178
187
  } catch (error) {
179
- return this.#fault({ itemId, code: "snapshot-restore-error", cause: `${itemId} could not move from machine version ${machineVersion} to ${kind.machine.version}: ${reason(error)}.`, fix: "Map the state in the kind's stateMap, or delete the item's saved state." });
188
+ return this.#fault({ itemId, code: SNAPSHOT_CARDS.restoreError, cause: `${itemId} could not move from machine version ${machineVersion} to ${kind.machine.version}: ${reason(error)}.`, fix: "Map the state in the kind's stateMap, or delete the item's saved state." });
180
189
  }
181
190
  }
182
191
 
package/src/state/open.ts CHANGED
@@ -10,7 +10,17 @@ import { Choice } from "../status/format.ts";
10
10
  import { OpKeys } from "./ops.ts";
11
11
  import { Records } from "./records.ts";
12
12
  import { RunRecords } from "./runs.ts";
13
- import { ItemKind, Landing, Learned, Limit, Parked, QueuedQuestion, ReadyList, SavedPlan, SettledAnswer, Slot, Spent, Verdict } from "./saved.ts";
13
+ import { FiledFollowUp, ItemKind, Landing, Learned, Limit, Parked, PullRequestText, QueuedQuestion, ReadyList, SavedPlan, SettledAnswer, Slot, Spent, Verdict } from "./saved.ts";
14
+
15
+ /** Names under the state folder that other modules address by name. */
16
+ export const STATUS_FILE = "status.json";
17
+ export const HEARTBEAT_FILE = "heartbeat.json";
18
+ export const INBOX_DIR = "inbox";
19
+ export const STOPS_DIR = "stops";
20
+ export const SLOTS_DIR = "slots";
21
+
22
+ /** Publish's own git repo, beside the state, never saved with it. */
23
+ export const PUBLISH_DIR = "publish";
14
24
 
15
25
  /** On the Mac and in a clone, state lives in the repo's git common dir, so every worktree shares it. */
16
26
  export const defaultStateDir = async (cwd: string): Promise<string> => {
@@ -30,13 +40,13 @@ export const openState = (dir: string, { now, ...codec }: StateOptions) => {
30
40
  return {
31
41
  dir,
32
42
  /** The one status file for the repo. */
33
- statusPath: join(dir, "status.json"),
43
+ statusPath: join(dir, STATUS_FILE),
34
44
  /** The worker's heartbeat, one file the service unit reads. */
35
- heartbeatPath: join(dir, "heartbeat.json"),
45
+ heartbeatPath: join(dir, HEARTBEAT_FILE),
36
46
  /** The answer hook drops one file here per posted answer; its content is the item id. */
37
- inbox: join(dir, "inbox"),
47
+ inbox: join(dir, INBOX_DIR),
38
48
  /** A background run's Stop hook drops one file here per stop: the run's session id and transcript path. */
39
- stops: join(dir, "stops"),
49
+ stops: join(dir, STOPS_DIR),
40
50
  faults,
41
51
  items: new Items(join(dir, "items"), faults, now, codec),
42
52
  events: new EventLog(join(dir, "events"), now),
@@ -49,7 +59,7 @@ export const openState = (dir: string, { now, ...codec }: StateOptions) => {
49
59
  limit: new Records(join(dir, "limit"), Limit),
50
60
  ready: new Records(join(dir, "ready"), ReadyList),
51
61
  /** The run slots, by name. */
52
- slots: new Records(join(dir, "slots"), Slot),
62
+ slots: new Records(join(dir, SLOTS_DIR), Slot),
53
63
  landings: new Records(join(dir, "landings"), Landing),
54
64
  /** Path rules by glob. */
55
65
  learned: new Records(join(dir, "learned"), Learned),
@@ -57,8 +67,12 @@ export const openState = (dir: string, { now, ...codec }: StateOptions) => {
57
67
  itemKinds: new Records(join(dir, "item-kinds"), ItemKind),
58
68
  plans: new Records(join(dir, "plans"), SavedPlan),
59
69
  verdicts: new Records(join(dir, "verdicts"), Verdict),
70
+ /** The pull request text the latest build or fix run wrote, by item. */
71
+ pullRequests: new Records(join(dir, "pull-requests"), PullRequestText),
60
72
  /** Questions by item, waiting for room under the daily cap or asked within the day. */
61
73
  questions: new Records(join(dir, "questions"), QueuedQuestion),
74
+ /** Follow-up items a pass filed within the day, by the item filed. */
75
+ followUps: new Records(join(dir, "follow-ups"), FiledFollowUp),
62
76
  /** Choices made without the owner, by item and time. */
63
77
  choices: new Records(join(dir, "choices"), Choice),
64
78
  /** Each answer's disposition, by item and ordinal. */
@@ -25,6 +25,13 @@ export class Records<S extends z.core.$ZodType> {
25
25
  atomicWrite(this.#path(key), `${JSON.stringify(z.parse(this.#schema, value))}\n`);
26
26
  }
27
27
 
28
+ /** Copies the record at `key` into another store of its kind, as its schema reads it, where this one has it. */
29
+ copyTo(into: Records<S>, key: string): void {
30
+ const record = this.get(key);
31
+
32
+ if (record !== undefined) atomicWrite(into.#path(key), `${JSON.stringify(record)}\n`);
33
+ }
34
+
28
35
  remove(key: string): void {
29
36
  rmSync(this.#path(key), { force: true });
30
37
  }
package/src/state/runs.ts CHANGED
@@ -71,6 +71,11 @@ export class RunRecords {
71
71
  };
72
72
  }
73
73
 
74
+ /** Saves a record another process kept, as it is. */
75
+ keep(record: RunRecord): void {
76
+ atomicWrite(this.#path(record), `${JSON.stringify(RunRecord.parse(record))}\n`);
77
+ }
78
+
74
79
  /** Rewrites a begun run's record. Nothing removes a record, so one gone since `begin` saved it throws. */
75
80
  #rewrite(key: RunKey, change: { readonly sessionId?: string; readonly endedAt?: number; readonly memoryUsed?: string[] }): RunRecord {
76
81
  const record = this.get(key);
@@ -31,8 +31,10 @@ export const Landing = z.object({
31
31
  change: Change,
32
32
  /** The commit the change ends at, before hum marks the item done: what a review reads. */
33
33
  head: name,
34
- /** The owner approved this change in its review state, before it was saved. */
35
- ownerApproved: z.boolean(),
34
+ /** The passes in a row whose push to the base another push beat. */
35
+ lostRaces: z.int().nonnegative().default(0),
36
+ /** The change's patch id against the base: the same change rebased keeps its landing. */
37
+ patch: z.string().optional(),
36
38
  /** The alignment call's grades, or why it abstained; made once per change. */
37
39
  alignment: z.union([z.object({ graded: AlignmentAnswer }), z.object({ abstained: name })]).optional(),
38
40
  });
@@ -61,6 +63,16 @@ export const SavedPlan = z.object({ item: name, plan: PlanFile, hash: name, mode
61
63
 
62
64
  export type SavedPlan = z.infer<typeof SavedPlan>;
63
65
 
66
+ /** One picture or video of the change at work, kept at `url`, or why the one the run named was left out. */
67
+ export const Evidence = z.union([z.object({ kind: z.enum(["image", "video"]), url: z.url() }), z.object({ left: name })]);
68
+
69
+ export type Evidence = z.infer<typeof Evidence>;
70
+
71
+ /** The pull request a build or fix run wrote for its item's whole change, under the repo's own rules for one; the latest run's stands. */
72
+ export const PullRequestText = z.object({ item: name, title: name, body: name, evidence: Evidence.optional() });
73
+
74
+ export type PullRequestText = z.infer<typeof PullRequestText>;
75
+
64
76
  /** A fresh review run's verdict on the change that ends at `head`. */
65
77
  export const Verdict = z.object({ item: name, head: name, event: name, reason: z.string(), cites: z.array(Cite), models });
66
78
 
@@ -82,6 +94,9 @@ export const QueuedQuestion = z.object({
82
94
 
83
95
  export type QueuedQuestion = z.infer<typeof QueuedQuestion>;
84
96
 
97
+ /** A follow-up item a pass filed, which counts against the daily question cap like a question asked. */
98
+ export const FiledFollowUp = z.object({ item: name, from: name, at: time });
99
+
85
100
  const settledFields = {
86
101
  item: name,
87
102
  /** The answer's number among the thread's answers and replies from people. */
@@ -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
  };