@moo.md/hum 0.4.1 → 0.6.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 (48) hide show
  1. package/defaults/hum.json +39 -12
  2. package/npm-shrinkwrap.json +2 -2
  3. package/package.json +2 -1
  4. package/release.json +1 -1
  5. package/schema/hum.schema.json +63 -17
  6. package/scripts/eval.ts +58 -27
  7. package/src/adapters/claude-p.ts +7 -0
  8. package/src/adapters/model-call.ts +8 -4
  9. package/src/adapters/pull-requests.ts +11 -7
  10. package/src/cli/setup.ts +13 -18
  11. package/src/config/json.ts +3 -0
  12. package/src/config/migrate.ts +11 -0
  13. package/src/config/schema.ts +21 -6
  14. package/src/decide/alignment.ts +1 -1
  15. package/src/decide/merge.ts +21 -1
  16. package/src/decide/prose.ts +12 -2
  17. package/src/decide/risk.ts +1 -0
  18. package/src/loop/breaks.ts +17 -0
  19. package/src/loop/notes.ts +2 -2
  20. package/src/loop/pair.ts +2 -2
  21. package/src/loop/pass.ts +6 -3
  22. package/src/loop/records.ts +24 -1
  23. package/src/loop/signals.ts +2 -2
  24. package/src/measure/cases.ts +102 -0
  25. package/src/measure/inputs.ts +15 -1
  26. package/src/measure/judges.ts +52 -0
  27. package/src/measure/measure.ts +22 -3
  28. package/src/measure/rate.ts +9 -6
  29. package/src/measure/remeasure.ts +31 -0
  30. package/src/publish/handoff.ts +1 -1
  31. package/src/service/workflows.ts +4 -2
  32. package/src/setup/write.ts +2 -4
  33. package/src/state/events.ts +201 -19
  34. package/src/state/format.ts +9 -2
  35. package/src/state/item-log.ts +133 -0
  36. package/src/state/items.ts +6 -5
  37. package/src/state/open.ts +7 -2
  38. package/src/state/runs.ts +5 -0
  39. package/src/state/saved.ts +7 -2
  40. package/src/status/format.ts +12 -3
  41. package/src/worker/land.ts +36 -15
  42. package/src/worker/merged.ts +18 -8
  43. package/src/worker/open.ts +5 -1
  44. package/src/worker/prose-run.ts +6 -2
  45. package/src/worker/signature.ts +11 -5
  46. package/src/worker/thin-path.ts +69 -53
  47. package/src/worker/worker.ts +82 -12
  48. package/surface.json +969 -151
@@ -33,7 +33,10 @@ import { pickTier } from "../config/checks.ts";
33
33
  import { footprintOf } from "./scheduler.ts";
34
34
  import { alignedPlacement, alignmentQuestion, doesGateAlignment } from "../decide/alignment.ts";
35
35
  import {
36
+ doesApprove,
36
37
  isCleared,
38
+ reachesDoneBy,
39
+ REVIEW_APPROVAL,
37
40
  type Landing,
38
41
  landingOf,
39
42
  doesNeedReviewRun,
@@ -54,7 +57,8 @@ import type { State } from "../state/open.ts";
54
57
  import type { Landing as SavedLanding, Verdict } from "../state/saved.ts";
55
58
  import { PLAIN_QUESTIONS } from "../decide/question.ts";
56
59
  import { checksTimeoutMs, runChecks } from "./checks.ts";
57
- import { recordMerged } from "./merged.ts";
60
+ import { mergedThrough, type OnBase, recordMerged } from "./merged.ts";
61
+ import { keepLanding, keepPick } from "../state/item-log.ts";
58
62
  import { blockedPaths, brokenCommits } from "../loop/breaks.ts";
59
63
  import { queueQuestion, releaseQuestions } from "./questions.ts";
60
64
  import { pickedAnswer } from "./thread.ts";
@@ -107,9 +111,6 @@ export type LandContext = {
107
111
  /** What a review run at landing came to: its verdict, or the result that stopped it, such as a usage limit. */
108
112
  export type ReviewOutcome = { readonly verdict: Verdict } | { readonly halted: ThinResult };
109
113
 
110
- /** The review verdict that lets a change merge. */
111
- const REVIEW_APPROVAL = "approve";
112
-
113
114
  /** The base moved after the work side checked the change on it: pushing now could land code no check ran on. */
114
115
  type Moved = { readonly kind: "moved" };
115
116
 
@@ -290,9 +291,13 @@ const markItemDone = async ({ git, clone, dir, id }: LandContext): Promise<void>
290
291
  /** The patch id of the change against the base: a rebase keeps it. */
291
292
  const patchOf = async (ctx: Pick<LandContext, "git" | "base" | "dir" | "id">): Promise<string> => (await ctx.git.raw(["patch-id", "--stable"], await changeDiff(ctx))).split(" ")[0] ?? "";
292
293
 
294
+ /** The run branch's commits past the base, oldest first. */
295
+ const branchCommits = async ({ git, base }: Pick<LandContext, "git" | "base">): Promise<string[]> =>
296
+ (await git.raw(["rev-list", "--reverse", "--first-parent", `origin/${base}..HEAD`])).split("\n").filter(Boolean);
297
+
293
298
  /**
294
299
  * Marks the item done on the run branch where it holds the item's file, pushes the branch and saves what its diff
295
- * showed, so a retry lands it without a run. The saved head is the change before hum's done mark; a push GitHub signed
300
+ * showed and the commits it pushed, so a retry lands it without a run. The saved head is the change before hum's done mark; a push GitHub signed
296
301
  * rebuilt the change as its own commit, which is then the head, and a verdict on the old commit holds for its tree.
297
302
  * The same change saved again, as after a rebase, keeps when it was first saved, so the owner's answers since then
298
303
  * still count; a new change starts the count afresh.
@@ -315,7 +320,7 @@ export const prepareLanding = async (ctx: LandContext): Promise<PushOutcome> =>
315
320
 
316
321
  if (head !== built && verdict?.head === built) state.verdicts.put(id, { ...verdict, head });
317
322
 
318
- state.landings.put(id, { item: id, branch, since: kept?.since ?? now(), change, head, lostRaces: kept?.lostRaces ?? 0, alignment: kept?.alignment, patch });
323
+ state.landings.put(id, { item: id, branch, since: kept?.since ?? now(), change, head, lostRaces: kept?.lostRaces ?? 0, alignment: kept?.alignment, patch, commits: await branchCommits(ctx) });
319
324
 
320
325
  return pushed;
321
326
  };
@@ -364,15 +369,27 @@ const endedByChange = async ({ git, dir, id }: LandContext): Promise<boolean> =>
364
369
  * The change is on the base: its item ends at the work source unless the change ended it, then the merge is recorded.
365
370
  * A failed end leaves the landing saved, so the next pass ends it again.
366
371
  */
367
- const finish = async (ctx: LandContext): Promise<ThinResult> => {
372
+ const finish = async (ctx: LandContext, onBase: OnBase): Promise<ThinResult> => {
368
373
  const { state, git, id, item, workSource, now } = ctx;
369
374
 
370
375
  if (!(await endedByChange(ctx))) await workSource.finish(item.id);
371
- await recordMerged(state, { git, remote: "origin" }, { item: id, sha: (await git.raw(["rev-parse", "HEAD"])).trim(), at: now() });
376
+ await recordMerged(state, { git, remote: "origin" }, { item: id, at: now(), ...onBase });
372
377
 
373
378
  return { outcome: "merged", item: id };
374
379
  };
375
380
 
381
+ /** A change the run branch's own push put on the base: the base stands at HEAD, and its commits are the ones the landing saved. */
382
+ const pushedOnBase = async ({ git, state, id }: LandContext): Promise<OnBase> => ({ sha: (await git.raw(["rev-parse", "HEAD"])).trim(), commits: state.landings.get(id)?.commits });
383
+
384
+ /** A change GitHub merged through its pull request, once GitHub names the commit the merge made. */
385
+ const mergedPull = async (ctx: LandContext, pulls: PullRequests): Promise<ThinResult> => {
386
+ const pull = await pulls.find(ctx.branch);
387
+
388
+ if (pull?.state !== "MERGED") throw new Error(`GitHub merged the pull request from ${ctx.branch}, then named it ${pull?.state ?? "missing"}`);
389
+
390
+ return finish(ctx, mergedThrough(ctx.state.landings.get(ctx.id), pull.mergeCommit));
391
+ };
392
+
376
393
  /** Pushes the change to the base once, only as it grows from the base as it stands: a base that moved after the change was checked waits for the next pass to check it again. */
377
394
  const pushToBase = async (ctx: LandContext): Promise<BaseLanding> => {
378
395
  if (await isOnBase(ctx)) return LANDED;
@@ -402,7 +419,7 @@ const throughPullRequest = async (
402
419
  const { id, branch, base, config } = ctx;
403
420
  const pull = (await pulls.find(branch)) ?? (await pulls.open({ head: branch, base, ...pullRequestOf(ctx, autoMerge ? `Level ${route.level.name}: it merges once the required checks pass.` : `Not merged by hum: ${why}.`) }));
404
421
 
405
- if (pull.state === "MERGED") return finish(ctx);
422
+ if (pull.state === "MERGED") return finish(ctx, mergedThrough(ctx.state.landings.get(id), pull.mergeCommit));
406
423
 
407
424
  if (pull.state === "CLOSED")
408
425
  return {
@@ -422,7 +439,7 @@ const throughPullRequest = async (
422
439
  methodFor(config.hum.mergeMethod, rules.mergeMethods),
423
440
  );
424
441
 
425
- if (armed === "merged") return finish(ctx);
442
+ if (armed === "merged") return mergedPull(ctx, pulls);
426
443
 
427
444
  if (armed === "armed" && pull.behind) await pulls.update(pull);
428
445
 
@@ -486,7 +503,7 @@ const landBy = async (
486
503
 
487
504
  if (landed.kind === "moved") return loseRace(ctx, `${ctx.base} moved after the change was checked on it; the next pass checks it there`);
488
505
 
489
- return finish(ctx);
506
+ return finish(ctx, await pushedOnBase(ctx));
490
507
  };
491
508
 
492
509
  /**
@@ -500,7 +517,7 @@ export const land = async (ctx: LandContext): Promise<ThinResult> => {
500
517
  if (!saved) throw new Error(`no landing is saved for ${ctx.id}`);
501
518
 
502
519
  // A change of no files, such as a split item's, whose branch the base already holds has nothing left to land.
503
- if (saved.change.paths.length === 0 && (await isOnBase(ctx))) return finish(ctx);
520
+ if (saved.change.paths.length === 0 && (await isOnBase(ctx))) return finish(ctx, await pushedOnBase(ctx));
504
521
 
505
522
  const first = await placementOf(ctx, saved.change);
506
523
  const answered = answeredLevel(ctx.item, saved.since, levelNames(ctx.config));
@@ -528,14 +545,17 @@ export const land = async (ctx: LandContext): Promise<ThinResult> => {
528
545
 
529
546
  const verdict = reviewed?.verdict;
530
547
  const route = routeWith(verdict);
531
- const approvals = { review: verdict?.event === REVIEW_APPROVAL, owner: pickedAnswer(ctx.item.thread, [OWNER_APPROVAL], saved.since) !== undefined };
548
+ const approvals = { review: doesApprove(ctx.state.verdicts.get(ctx.id), saved.head), owner: pickedAnswer(ctx.item.thread, [OWNER_APPROVAL], saved.since) !== undefined };
549
+ const cleared = isCleared(route, approvals);
532
550
  const why = verdict && verdict.event !== REVIEW_APPROVAL ? `the review run answered ${verdict.event}: ${verdict.reason}` : waitReason(route);
533
551
  const rules = ctx.pulls ? await ctx.pulls.rules(ctx.base) : NO_RULES;
534
552
 
535
- return landBy(ctx, landingOf({ requiresPullRequest: rules.requiresPullRequest, cleared: isCleared(route, approvals) }), route, rules, why);
553
+ keepLanding(ctx.state.events, { itemId: ctx.id, head: saved.head, doneBy: reachesDoneBy(approvals, cleared) });
554
+
555
+ return landBy(ctx, landingOf({ requiresPullRequest: rules.requiresPullRequest, cleared }), route, rules, why);
536
556
  };
537
557
 
538
- /** The alignment call's grades for a saved change, made once and kept with the landing. */
558
+ /** The alignment call's grades for a saved change, made once and kept with the landing and in the item's log. */
539
559
  const alignmentOf = async (ctx: LandContext, saved: SavedLanding, diff: string): Promise<NonNullable<SavedLanding["alignment"]>> => {
540
560
  if (saved.alignment) return saved.alignment;
541
561
 
@@ -543,6 +563,7 @@ const alignmentOf = async (ctx: LandContext, saved: SavedLanding, diff: string):
543
563
  const alignment = judged.kind === "picked" ? { graded: judged.value } : { abstained: judged.why };
544
564
 
545
565
  ctx.state.landings.put(ctx.id, { ...saved, alignment });
566
+ keepPick(ctx.state.events, { itemId: ctx.id, pick: { judge: "alignment", head: saved.head, alignment } });
546
567
 
547
568
  return alignment;
548
569
  };
@@ -2,22 +2,30 @@ import { ItemId, type WorkSource } from "../adapter-api.ts";
2
2
  import type { PullRequests } from "../adapters/pull-requests.ts";
3
3
  import type { Git } from "../adapters/git.ts";
4
4
  import type { State } from "../state/open.ts";
5
+ import type { Landing } from "../state/saved.ts";
5
6
  import { dropEvidence } from "./evidence.ts";
6
7
 
7
8
  /** Where a merged change's evidence ref is taken off: git in a repo with `remote`. */
8
9
  export type EvidenceRemote = { readonly git: Git; readonly remote: string };
9
10
 
11
+ /** Where a merged change is: the base's commit once it is on it, and every commit that held it, absent where hum did not see them. */
12
+ export type OnBase = { readonly sha: string; readonly commits: readonly string[] | undefined };
13
+
14
+ /** A change a pull request merged: the commit the merge made on the base, after the run branch's own. */
15
+ export const mergedThrough = (landing: Landing | undefined, mergeCommit: string): OnBase => ({ sha: mergeCommit, commits: [...(landing?.commits ?? []), mergeCommit] });
16
+
10
17
  /**
11
18
  * An item's change reached the base: its evidence ref is taken off the remote, the merge is kept for the measure, the
12
- * saved landing, plan, verdict and pull request text go, and the merge is logged once per commit.
19
+ * saved landing, plan, verdict and pull request text go, and the merge is logged once per commit in the item's log,
20
+ * which keeps how the item reached done and the picks behind it.
13
21
  */
14
- export const recordMerged = async (state: State, { git, remote }: EvidenceRemote, { item, sha, at }: { readonly item: string; readonly sha: string; readonly at: number }): Promise<void> => {
22
+ export const recordMerged = async (state: State, { git, remote }: EvidenceRemote, { item, at, sha, commits }: OnBase & { readonly item: string; readonly at: number }): Promise<void> => {
15
23
  const landing = state.landings.get(item);
16
24
  const evidence = state.pullRequests.get(item)?.evidence;
17
25
 
18
26
  if (evidence !== undefined && "url" in evidence) await dropEvidence(git, { remote, item });
19
27
 
20
- state.merges.put(item, { item, sha, at, paths: [...(landing?.change.paths ?? [])], baseChecked: false });
28
+ state.merges.put(item, { item, sha, at, paths: [...(landing?.change.paths ?? [])], baseChecked: false, commits: commits && [...commits] });
21
29
  state.landings.remove(item);
22
30
  state.plans.remove(item);
23
31
  state.verdicts.remove(item);
@@ -32,12 +40,14 @@ export const recordMerged = async (state: State, { git, remote }: EvidenceRemote
32
40
  */
33
41
  export const settleMergedPulls = async (state: State, { pulls, workSource, ...evidenceRemote }: { readonly pulls: Pick<PullRequests, "find">; readonly workSource: Pick<WorkSource, "finish"> } & EvidenceRemote, at: number): Promise<void> => {
34
42
  await Promise.all(
35
- state.landings.list().map(async ({ item, branch, head }) => {
36
- if ((await pulls.find(branch))?.state !== "MERGED") return;
43
+ state.landings.list().map(async (landing) => {
44
+ const pull = await pulls.find(landing.branch);
45
+
46
+ if (pull?.state !== "MERGED") return;
37
47
 
38
- await workSource.finish(ItemId.parse(item));
39
- await recordMerged(state, evidenceRemote, { item, sha: head, at });
40
- state.items.remove(item);
48
+ await workSource.finish(ItemId.parse(landing.item));
49
+ await recordMerged(state, evidenceRemote, { item: landing.item, at, ...mergedThrough(landing, pull.mergeCommit) });
50
+ state.items.remove(landing.item);
41
51
  }),
42
52
  );
43
53
  };
@@ -17,6 +17,7 @@ import { loadConfig, readConfigDir } from "../config/load.ts";
17
17
  import type { HumConfig } from "../config/schema.ts";
18
18
  import { commandTimeoutMs, type Env, withoutGitHubTokens } from "../exec.ts";
19
19
  import { defaultStateDir, openState } from "../state/open.ts";
20
+ import { type Measurement, measureOn } from "../measure/remeasure.ts";
20
21
  import { DEFAULT_BASE } from "./thin-path.ts";
21
22
  import { Worker, type WorkerOptions } from "./worker.ts";
22
23
 
@@ -114,5 +115,8 @@ export const workerOptions = async (repo: string, options: OpenWorkerOptions): P
114
115
  const commits = github === undefined ? undefined : gitHubCommits(github, { timeoutMs, env });
115
116
  const loop = github === undefined ? undefined : githubLoopReads(github, rest.base ?? DEFAULT_BASE, config.hum.measureWindow, () => ownerGitHub(timeoutMs, env));
116
117
 
117
- return { ...rest, checks: config.hum.checks, checkout: rest.checkout ?? slots?.checkout, slots, repo, state, config, adapters: { workSource, questions, answers }, runner, remote, pulls, commits, loop, runnerVariables: new Set(Object.entries(withoutGitHubTokens(env)).flatMap(([name, value]) => (value === undefined ? [] : [name]))) };
118
+ /** A measurement writes the owner's `.hum/hum.json`, which a CI checkout throws away. */
119
+ const remeasure = rest.where === "ci" ? undefined : (measurement: Measurement, signal: AbortSignal) => measureOn(repo, measurement, { inherit: false, signal });
120
+
121
+ return { ...rest, remeasure, checks: config.hum.checks, checkout: rest.checkout ?? slots?.checkout, slots, repo, state, config, adapters: { workSource, questions, answers }, runner, remote, pulls, commits, loop, runnerVariables: new Set(Object.entries(withoutGitHubTokens(env)).flatMap(([name, value]) => (value === undefined ? [] : [name]))) };
118
122
  };
@@ -1,6 +1,6 @@
1
1
  import { type AgentDecisionExecutor, createDecisionRequest, getMessageText, renderDecisionAttempts, resolveDecision } from "@statelyai/agent";
2
2
  import { type Finished, hasSucceeded } from "../adapters/claude-p.ts";
3
- import { type EventPick, type JsonSchema, PROSE_STATES, type ProsePurpose, pickSchema, readPick } from "../decide/prose.ts";
3
+ import { type EventPick, type JsonSchema, type ModelAsk, PROSE_STATES, type ProsePurpose, pickSchema, readPick } from "../decide/prose.ts";
4
4
 
5
5
  const RESULT_EXCERPT = 300;
6
6
 
@@ -23,6 +23,9 @@ export type ProseOptions = {
23
23
  const offer = (events: readonly string[]): string =>
24
24
  `Answer with one event: ${events.join(", ")}. Set sure to false when you are not sure; the safer event is then taken. Give your reason in a sentence.`;
25
25
 
26
+ /** What a plan or review run is sent first: its prompt and the events it is offered, and the schema its answer must match. */
27
+ export const proseAsk = (prompt: string, offered: readonly string[]): ModelAsk => ({ prompt: [prompt, offer(offered)].join("\n\n"), schema: pickSchema(offered) });
28
+
26
29
  /**
27
30
  * A plan or review run's pick of the next event, through the agent library's decide executor: the run is offered
28
31
  * only the events the state takes, and code checks the artifact behind its pick. A pick that fails its check is
@@ -34,7 +37,8 @@ export const decideProse = async ({ purpose, prompt, diff, model, run, canTake =
34
37
  const decide: AgentDecisionExecutor = async (request) => {
35
38
  const offered = request.events.map(({ type }) => type);
36
39
  const feedback = renderDecisionAttempts(request).map(getMessageText);
37
- const outcome = await run([prompt, offer(offered), ...feedback].join("\n\n"), pickSchema(offered));
40
+ const ask = proseAsk(prompt, offered);
41
+ const outcome = await run([ask.prompt, ...feedback].join("\n\n"), ask.schema);
38
42
 
39
43
  if (!hasSucceeded(outcome)) return { rejected: { failure: "invalid-payload", reason: `the run ended as ${outcome.subtype}: ${outcome.text.slice(0, RESULT_EXCERPT)}` } };
40
44
 
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
2
2
  import { existsSync, readFileSync } from "node:fs";
3
3
  import { join, relative } from "node:path";
4
4
  import { z } from "zod";
5
+ import { EVALS_DIR } from "../measure/remeasure.ts";
5
6
  import { isMissing } from "../state/fs.ts";
6
7
 
7
8
  const Package = z.looseObject({ version: z.string().min(1) });
@@ -13,10 +14,14 @@ const LOCKFILES = ["bun.lock", "bun.lockb", "package-lock.json", "pnpm-lock.yaml
13
14
 
14
15
  const isInside = (path: string, folder: string): boolean => path === folder || path.startsWith(`${folder}/`);
15
16
 
16
- /** Every file under `folder` of `root`, hidden ones too, as paths from `root` in a stable order, without the items folder: every claim, answer and merge changes it, and it is no config. */
17
- const filesUnder = ({ root, folder, itemsDir }: { readonly root: string; readonly folder: string; readonly itemsDir: string }): string[] =>
17
+ /**
18
+ * Every file under `folder` of `root`, hidden ones too, as paths from `root` in a stable order, without the folders
19
+ * `skipped` names: the items, which every claim, answer and merge changes, and the eval results each measurement
20
+ * adds to, are no config.
21
+ */
22
+ const filesUnder = ({ root, folder, skipped }: { readonly root: string; readonly folder: string; readonly skipped: readonly string[] }): string[] =>
18
23
  existsSync(join(root, folder))
19
- ? [...new Bun.Glob("**").scanSync({ cwd: join(root, folder), dot: true, onlyFiles: true })].flatMap((path) => (isInside(join(folder, path), itemsDir) ? [] : [join(folder, path)])).sort()
24
+ ? [...new Bun.Glob("**").scanSync({ cwd: join(root, folder), dot: true, onlyFiles: true })].flatMap((path) => (skipped.some((skip) => isInside(join(folder, path), skip)) ? [] : [join(folder, path)])).sort()
20
25
  : [];
21
26
 
22
27
  /** The engine version installed in the owner's repo, else `fallback` (the running one). */
@@ -30,10 +35,11 @@ export const installedVersion = (repo: string, fallback: string): string => {
30
35
  }
31
36
  };
32
37
 
33
- /** The installed engine version and one hash of the lockfile, `.hum/` (without its items) and `adapters/`. */
38
+ /** The installed engine version and one hash of the lockfile, `.hum/` (without its items and eval results) and `adapters/`. */
34
39
  export const signatureOf = ({ repo, fallbackVersion, itemsDir }: { readonly repo: string; readonly fallbackVersion: string; readonly itemsDir: string }): Signature => {
35
40
  const hash = createHash("sha256");
36
- const files = [...LOCKFILES.filter((name) => existsSync(join(repo, name))), ...filesUnder({ root: repo, folder: ".hum", itemsDir }), ...filesUnder({ root: repo, folder: "adapters", itemsDir })];
41
+ const skipped = [itemsDir, join(".hum", EVALS_DIR)];
42
+ const files = [...LOCKFILES.filter((name) => existsSync(join(repo, name))), ...filesUnder({ root: repo, folder: ".hum", skipped }), ...filesUnder({ root: repo, folder: "adapters", skipped })];
37
43
 
38
44
  for (const file of files) hash.update(relative(repo, join(repo, file))).update("\0").update(readFileSync(join(repo, file))).update("\0");
39
45
 
@@ -4,7 +4,7 @@ import { join } from "node:path";
4
4
  import { z } from "zod";
5
5
  import { type AnyActor, createActor, createAsyncLogic } from "xstate";
6
6
  import type { Holder, Item, ItemId, Question, Questions, ThreadEntry, WorkSource } from "../adapter-api.ts";
7
- import { type Finished, type Runner, type RunOutcome, hasSucceeded } from "../adapters/claude-p.ts";
7
+ import { type Finished, type Paused, type Runner, hasSucceeded, isPaused } from "../adapters/claude-p.ts";
8
8
  import { configuredModelCall, type ModelCall } from "../adapters/model-call.ts";
9
9
  import { visibleText } from "../adapters/marked.ts";
10
10
  import { type Git, gitIn, PATCH, withConfigPinned } from "../adapters/git.ts";
@@ -15,8 +15,9 @@ import { invokedSources, kindMachine } from "../config/machine.ts";
15
15
  import { type Kind, type Naming, OWNER_WAIT_TAG, type PURPOSES, STUCK_TAG } from "../config/schema.ts";
16
16
  import { commandTimeoutMs } from "../exec.ts";
17
17
  import { doesLeaveDocs } from "../decide/kind.ts";
18
- import { type EventPick, type JsonSchema, jsonSchemaOf, type ProsePurpose, planHash, requestedChanges } from "../decide/prose.ts";
18
+ import { type EventPick, type JsonSchema, jsonSchemaOf, type ProsePurpose, planHash, requestedChanges, SPLIT } from "../decide/prose.ts";
19
19
  import { persistedSnapshot } from "../state/items.ts";
20
+ import { answerOnce, foldItem, keepPick, machineStarted, ownerAnswered } from "../state/item-log.ts";
20
21
  import type { State } from "../state/open.ts";
21
22
  import { HOUR_MS, SECOND_MS } from "../time.ts";
22
23
  import { checksTimeoutMs, runChecks } from "./checks.ts";
@@ -113,7 +114,7 @@ export type ThinResult =
113
114
  | { readonly outcome: "limited"; readonly item: string; readonly until: number }
114
115
  | { readonly outcome: "held"; readonly item: string; readonly why: string };
115
116
 
116
- type Answered = { readonly question: string; readonly answer: string; readonly option: string | undefined; readonly plan: string };
117
+ type Answered = { readonly question: string; readonly answer: string; readonly option: string | undefined; readonly plan: string; readonly at: number };
117
118
 
118
119
  /** The question, the answer to it and the plan, once the owner has answered the latest question. */
119
120
  export const answerTo = (item: Item): Answered | undefined => {
@@ -128,6 +129,7 @@ export const answerTo = (item: Item): Answered | undefined => {
128
129
  answer: answer.option ? `${answer.option}: ${answer.text}` : answer.text,
129
130
  option: answer.option,
130
131
  plan: plan?.text.slice(PLAN_MARK.length).trim() ?? "",
132
+ at: answer.at,
131
133
  };
132
134
  };
133
135
 
@@ -223,6 +225,10 @@ export const gatherItem = async (o: ThinPathOptions, id: ItemId): Promise<Gather
223
225
 
224
226
  if (!saved.ok) return { outcome: "held", item: id, why: saved.fault.cause };
225
227
 
228
+ const log = o.state.events.read(id);
229
+
230
+ if (!log.ok) return { outcome: "held", item: id, why: log.fault.cause };
231
+
226
232
  const at = saved.value === undefined ? undefined : String(saved.value.getSnapshot().value);
227
233
 
228
234
  if (knownKind && hasEndedStuck(knownKind, at)) return park(o, { outcome: "stuck", item: id, detail: "the saved state ended stuck" });
@@ -407,6 +413,7 @@ type Driven = ThinPathOptions & {
407
413
 
408
414
  const drive = async (d: Driven): Promise<ThinResult> => {
409
415
  const { state, runner, config, kind, id, item, branch, clone, g, base } = d;
416
+ const { version } = kind.machine;
410
417
  const maxFixes = config.hum.maxFixRuns;
411
418
  const result = Promise.withResolvers<ThinResult>();
412
419
 
@@ -415,9 +422,11 @@ const drive = async (d: Driven): Promise<ThinResult> => {
415
422
  // The total over all runs is saved; the cap counts what the item spent since the owner last answered.
416
423
  const tally = tallyOf(state, id);
417
424
 
418
- if (d.answered) tally.startAfresh();
425
+ if (d.answered) {
426
+ tally.startAfresh();
427
+ state.events.add(ownerAnswered(id, d.answered.at));
428
+ }
419
429
 
420
- let fixes = 0;
421
430
  let lastFailure = "";
422
431
  let cause = "";
423
432
  let handled = "";
@@ -477,40 +486,49 @@ const drive = async (d: Driven): Promise<ThinResult> => {
477
486
  actor.send({ type: "token-cap" });
478
487
  };
479
488
 
480
- /** One agent run of the item. A limit, a hold or a stop pauses the item; passing the token cap asks the owner where the state can, and ends stuck where it cannot. */
481
- const runAgent = async (purpose: Purpose, text: string, schema: JsonSchema, signal: AbortSignal): Promise<Finished> => {
482
- const attempt = state.runs.unended(id, purpose)?.attempt ?? state.runs.list(id).length;
483
- const key = { itemId: id, run: purpose, attempt };
484
- const begun = state.runs.begin(key, { name: `hum ${id} ${purpose} #${attempt}`, sessionId: d.newSessionId() });
489
+ /**
490
+ * One agent run of the item. A limit, a hold or a stop pauses the item; passing the token cap asks the owner where
491
+ * the state can, and ends stuck where it cannot. A run given what it judges is answered from the item's log when
492
+ * the log keeps an answer to the same prompt on the same change, and that answer spends nothing again.
493
+ */
494
+ const runAgent = async (purpose: Purpose, text: string, schema: JsonSchema, signal: AbortSignal, judged?: Judged): Promise<Finished> => {
495
+ const key = state.runs.keyFor(id, purpose);
485
496
  const held = purpose === "build" || purpose === "fix" ? readMemory(join(clone, ".hum", "memory.md"), config.hum.memoryMaxBytes).entries : [];
486
497
  const prefix = config.hum.roles[purpose]?.file ?? purpose;
487
-
488
- const lease = await d.gate?.enter(prefix, signal);
489
-
490
- const outcome = await runner
491
- .run({
492
- prompt: `${text}${memorySection(held)}`,
493
- cwd: purpose === "review" ? tmpdir() : clone,
494
- env: d.env,
495
- ...(await tierFor(purpose)),
496
- schema,
497
- tokenCap: kind.tokenCap.tokens - tally.sinceAnswer(),
498
- name: begun.record.name,
499
- // A saved id already has a transcript under it; a fresh run mints its own.
500
- sessionId: begun.adopted ? undefined : begun.record.sessionId,
501
- signal,
502
- onSession: (sessionId) => begun.saveSession(sessionId),
503
- onFirstToken: () => lease?.opened(),
504
- })
505
- .finally(() => lease?.release());
498
+ const prompt = `${text}${memorySection(held)}`;
499
+ const tier = await tierFor(purpose);
500
+ const launch = { name: `hum ${id} ${purpose} #${key.attempt}`, sessionId: d.newSessionId() };
501
+
502
+ const { begun, outcome } = await answerOnce(state, { key, launch, machineVersion: version, question: judged && { prompt, ...tier, ...judged } }, async (begun) => {
503
+ const lease = await d.gate?.enter(prefix, signal);
504
+
505
+ const ran = await runner
506
+ .run({
507
+ prompt,
508
+ cwd: purpose === "review" ? tmpdir() : clone,
509
+ env: d.env,
510
+ ...tier,
511
+ schema,
512
+ tokenCap: kind.tokenCap.tokens - tally.sinceAnswer(),
513
+ name: begun.record.name,
514
+ // A saved id already has a transcript under it; a fresh run mints its own.
515
+ sessionId: begun.adopted ? undefined : begun.record.sessionId,
516
+ signal,
517
+ onSession: (sessionId) => begun.saveSession(sessionId),
518
+ onFirstToken: () => lease?.opened(),
519
+ })
520
+ .finally(() => lease?.release());
521
+
522
+ if (!isPaused(ran)) tally.spend(ran.tokens);
523
+
524
+ return ran;
525
+ });
506
526
 
507
527
  begun.end(held.length === 0 ? undefined : outcome.kind === "finished" ? rulesUsed(outcome, held) : []);
508
528
 
509
529
  if (isPaused(outcome)) return pause(pauseOf(id, outcome));
510
530
 
511
531
  if (outcome.kind === "over-cap") {
512
- tally.spend(outcome.tokens);
513
-
514
532
  const why = `The run passed its token cap (${kind.tokenCap.tokens}) at ${tally.sinceAnswer()} tokens.`;
515
533
 
516
534
  if (!actor.getSnapshot().can({ type: "token-cap" })) throw failed(why);
@@ -520,8 +538,6 @@ const drive = async (d: Driven): Promise<ThinResult> => {
520
538
  return new Promise<never>(() => {});
521
539
  }
522
540
 
523
- tally.spend(outcome.tokens);
524
-
525
541
  return outcome;
526
542
  };
527
543
 
@@ -550,7 +566,8 @@ const drive = async (d: Driven): Promise<ThinResult> => {
550
566
  const step = (purpose: "build" | "fix") =>
551
567
  createAsyncLogic({
552
568
  run: async ({ signal }) => {
553
- if (purpose === "fix" && ++fixes > maxFixes) throw failed(`the checks still fail after ${maxFixes} fixes`);
569
+ // Every fix run since the owner last answered counts, over every pass and wake: the item's log holds them all.
570
+ if (purpose === "fix" && foldItem(state.events.list(id)).fixRuns >= maxFixes) throw failed(`the checks still fail after ${maxFixes} fixes`);
554
571
 
555
572
  const before = (await g(["rev-parse", "HEAD"])).trim();
556
573
  const chosen: string[] = [];
@@ -611,8 +628,6 @@ const drive = async (d: Driven): Promise<ThinResult> => {
611
628
  ],
612
629
  ]);
613
630
 
614
- const saveVerdict = (pick: EventPick, models: readonly string[], head: string) => state.verdicts.put(id, verdictOf({ id, head, pick, models }));
615
-
616
631
  /** A plan or review state: its run picks the next event, and a split files the parts and lands the item as done. */
617
632
  const decision = (purpose: ProsePurpose) =>
618
633
  createAsyncLogic({
@@ -625,11 +640,12 @@ const drive = async (d: Driven): Promise<ThinResult> => {
625
640
  prompt: PROSE_PROMPTS[purpose](config, item, diff),
626
641
  diff,
627
642
  model: (await tierFor(purpose)).model,
628
- run: (text, schema) => runAgent(purpose, text, schema, signal),
643
+ run: (text, schema) => runAgent(purpose, text, schema, signal, { head }),
629
644
  canTake: (type) => actor.getSnapshot().can({ type }),
630
645
  });
631
646
 
632
- if (purpose === "review") saveVerdict(pick, models, head);
647
+ if (purpose === "review") keepVerdict(state, { id, head, pick, models });
648
+ else keepPick(state.events, { itemId: id, pick: { judge: purpose, pick, models: [...models] } });
633
649
 
634
650
  PICKED.get(pick.event)?.(pick, models);
635
651
 
@@ -652,6 +668,7 @@ const drive = async (d: Driven): Promise<ThinResult> => {
652
668
  // The wait it was saved in is already handled; only what follows the answer needs a reaction.
653
669
  handled = String(actor.getSnapshot().value);
654
670
  } else {
671
+ state.events.add(machineStarted({ itemId: id, machineVersion: version, afresh: d.restarted }));
655
672
  actor = createActor(kindMachine(kind, sources));
656
673
  }
657
674
 
@@ -749,8 +766,12 @@ type Purpose = (typeof PURPOSES)[number];
749
766
  /** Which runs are placed by the branch's diff so far; the others by the files their item declares. */
750
767
  const PLACED_BY_DIFF = { plan: false, build: false, fix: true, review: true } as const satisfies Record<Purpose, boolean>;
751
768
 
752
- /** The event by which a plan run splits its item into parts. */
753
- const SPLIT = "needs-split";
769
+ /**
770
+ * What a plan or review run reads besides its prompt: the commit its clone stands at, which fixes the whole diff its
771
+ * prompt may cut short. Only these runs are given it: their answer is all they do, while a build or fix run's change
772
+ * lives in a clone that a kill loses.
773
+ */
774
+ type Judged = { readonly head: string };
754
775
 
755
776
  /** What a run reads from its config: the settings and the role files. */
756
777
  export type RunConfig = Pick<Config, "hum" | "roles">;
@@ -776,14 +797,15 @@ export const PROSE_PROMPTS = {
776
797
  review: (config: RunConfig, item: ItemText, diff: string): string => [roleOf(config, "review"), reviewInput(item, diff, config.hum)].join("\n\n"),
777
798
  } as const satisfies Record<ProsePurpose, (config: RunConfig, item: ItemText, diff: string) => string>;
778
799
 
779
- const verdictOf = ({ id, head, pick, models }: { readonly id: string; readonly head: string; readonly pick: EventPick; readonly models: readonly string[] }) => ({ item: id, head, event: pick.event, reason: pick.reason, cites: pick.cites, models: [...models] });
800
+ /** A review's verdict on the change that ends at `head`: saved for landing, and kept in the item's log. */
801
+ const keepVerdict = (state: State, { id, head, pick, models }: { readonly id: string; readonly head: string; readonly pick: EventPick; readonly models: readonly string[] }) => {
802
+ const verdict = { item: id, head, event: pick.event, reason: pick.reason, cites: pick.cites, models: [...models] };
780
803
 
781
- /** A run that ended with no answer and no token to count: a usage limit, a hold, or a stop. */
782
- type Paused = Extract<RunOutcome, { kind: "limited" | "held" | "stopped" }>;
804
+ state.verdicts.put(id, verdict);
805
+ keepPick(state.events, { itemId: id, pick: { judge: "review", head, pick, models: [...models] } });
783
806
 
784
- const PAUSED_KINDS: readonly string[] = ["limited", "held", "stopped"] satisfies readonly Paused["kind"][];
785
-
786
- const isPaused = (outcome: RunOutcome): outcome is Paused => PAUSED_KINDS.includes(outcome.kind);
807
+ return verdict;
808
+ };
787
809
 
788
810
  /** What a paused run comes to for its item. */
789
811
  const pauseOf = (id: string, outcome: Paused): ThinResult => {
@@ -834,13 +856,7 @@ export const reviewRunOf =
834
856
  return new Promise<never>(() => {});
835
857
  };
836
858
 
837
- const decided = decideProse({ purpose: "review", prompt: PROSE_PROMPTS.review(o.config, item, diff), diff, model: o.config.hum.models[row.tier], run }).then(({ pick, models }): ReviewOutcome => {
838
- const verdict = verdictOf({ id: item.id, head, pick, models });
839
-
840
- o.state.verdicts.put(item.id, verdict);
841
-
842
- return { verdict };
843
- });
859
+ const decided = decideProse({ purpose: "review", prompt: PROSE_PROMPTS.review(o.config, item, diff), diff, model: o.config.hum.models[row.tier], run }).then(({ pick, models }): ReviewOutcome => ({ verdict: keepVerdict(o.state, { id: item.id, head, pick, models }) }));
844
860
 
845
861
  return Promise.race([decided, halted]);
846
862
  };