@moo.md/hum 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/defaults/hum.json +321 -0
  2. package/defaults/kinds/check.json +183 -0
  3. package/defaults/kinds/docs.json +151 -0
  4. package/defaults/kinds/feature.json +202 -0
  5. package/defaults/kinds/fix.json +183 -0
  6. package/defaults/kinds/hotfix.json +164 -0
  7. package/defaults/roles/build.md +3 -0
  8. package/defaults/roles/fix.md +1 -0
  9. package/defaults/roles/plan.md +3 -0
  10. package/defaults/roles/review.md +3 -0
  11. package/evals/alignment/RESULTS.md +8 -0
  12. package/evals/alignment/cases.jsonl +128 -0
  13. package/evals/alignment/sample.jsonl +20 -0
  14. package/evals/kind/RESULTS.md +5 -0
  15. package/evals/kind/cases.jsonl +24 -0
  16. package/evals/plan-outcome/RESULTS.md +4 -0
  17. package/evals/plan-outcome/cases.jsonl +12 -0
  18. package/evals/repeat-pair/RESULTS.md +4 -0
  19. package/evals/repeat-pair/cases.jsonl +51 -0
  20. package/evals/reply-option/RESULTS.md +3 -0
  21. package/evals/reply-option/cases.jsonl +45 -0
  22. package/evals/review-verdict/RESULTS.md +4 -0
  23. package/evals/review-verdict/cases.jsonl +12 -0
  24. package/package.json +88 -4
  25. package/schema/hum.schema.json +1355 -0
  26. package/scripts/eval.ts +244 -0
  27. package/src/adapter-api.ts +134 -0
  28. package/src/adapters/admit.ts +111 -0
  29. package/src/adapters/argument.ts +11 -0
  30. package/src/adapters/claim.ts +93 -0
  31. package/src/adapters/claude-bg.ts +375 -0
  32. package/src/adapters/claude-p.ts +593 -0
  33. package/src/adapters/contract.ts +231 -0
  34. package/src/adapters/fake.ts +157 -0
  35. package/src/adapters/git.ts +155 -0
  36. package/src/adapters/github.ts +457 -0
  37. package/src/adapters/guard.ts +152 -0
  38. package/src/adapters/history.ts +215 -0
  39. package/src/adapters/item-file.ts +102 -0
  40. package/src/adapters/limit.ts +67 -0
  41. package/src/adapters/markdown.ts +365 -0
  42. package/src/adapters/marked.ts +119 -0
  43. package/src/adapters/model-call.ts +75 -0
  44. package/src/adapters/once.ts +45 -0
  45. package/src/adapters/owner-commit.ts +60 -0
  46. package/src/adapters/pull-requests.ts +253 -0
  47. package/src/adapters/registry.ts +156 -0
  48. package/src/adapters/roundup.ts +291 -0
  49. package/src/adapters/serial.ts +22 -0
  50. package/src/adapters/transcript.ts +99 -0
  51. package/src/cli/commands.ts +46 -0
  52. package/src/cli/main.ts +8 -0
  53. package/src/cli/ops.ts +224 -0
  54. package/src/cli/service.ts +156 -0
  55. package/src/cli/setup.ts +141 -0
  56. package/src/cli/shared-watch.ts +127 -0
  57. package/src/config/change.ts +54 -0
  58. package/src/config/checks.ts +217 -0
  59. package/src/config/git-ref-name.d.ts +3 -0
  60. package/src/config/hash.ts +6 -0
  61. package/src/config/json.ts +6 -0
  62. package/src/config/load.ts +88 -0
  63. package/src/config/machine.ts +20 -0
  64. package/src/config/migrate.ts +61 -0
  65. package/src/config/refusal.ts +58 -0
  66. package/src/config/schema.ts +465 -0
  67. package/src/config/settings.ts +6 -0
  68. package/src/config/state-map.ts +76 -0
  69. package/src/decide/alignment.ts +79 -0
  70. package/src/decide/answer.ts +131 -0
  71. package/src/decide/kind.ts +39 -0
  72. package/src/decide/labels.ts +137 -0
  73. package/src/decide/merge.ts +102 -0
  74. package/src/decide/prose.ts +109 -0
  75. package/src/decide/question.ts +159 -0
  76. package/src/decide/review.ts +10 -0
  77. package/src/decide/risk.ts +216 -0
  78. package/src/decide/taste.ts +37 -0
  79. package/src/exec.ts +80 -0
  80. package/src/index.ts +200 -0
  81. package/src/loop/breaks.ts +8 -0
  82. package/src/loop/check-item.ts +76 -0
  83. package/src/loop/ci.ts +8 -0
  84. package/src/loop/github.ts +81 -0
  85. package/src/loop/groups.ts +108 -0
  86. package/src/loop/memory.ts +70 -0
  87. package/src/loop/notes.ts +76 -0
  88. package/src/loop/pair.ts +39 -0
  89. package/src/loop/pass.ts +99 -0
  90. package/src/loop/proof.ts +85 -0
  91. package/src/loop/prove-item.ts +33 -0
  92. package/src/loop/records.ts +75 -0
  93. package/src/loop/signals.ts +130 -0
  94. package/src/measure/inputs.ts +28 -0
  95. package/src/measure/measure.ts +68 -0
  96. package/src/service/cards.ts +24 -0
  97. package/src/service/ci-tick.ts +28 -0
  98. package/src/service/launchd.ts +16 -0
  99. package/src/service/supervise.ts +145 -0
  100. package/src/service/systemd.ts +44 -0
  101. package/src/service/token.ts +21 -0
  102. package/src/service/unit.ts +29 -0
  103. package/src/service/wake.ts +30 -0
  104. package/src/service/workflows.ts +97 -0
  105. package/src/setup/propose.ts +120 -0
  106. package/src/setup/prove.ts +216 -0
  107. package/src/setup/scan.ts +186 -0
  108. package/src/setup/scratch.ts +81 -0
  109. package/src/setup/shipped.ts +15 -0
  110. package/src/setup/tables.ts +101 -0
  111. package/src/setup/waits.ts +58 -0
  112. package/src/setup/write.ts +161 -0
  113. package/src/slots/env.ts +42 -0
  114. package/src/slots/pool.ts +329 -0
  115. package/src/slots/scan.ts +26 -0
  116. package/src/slots/worktree.ts +17 -0
  117. package/src/state/events.ts +52 -0
  118. package/src/state/faults.ts +73 -0
  119. package/src/state/format.ts +50 -0
  120. package/src/state/fs.ts +100 -0
  121. package/src/state/items.ts +189 -0
  122. package/src/state/open.ts +80 -0
  123. package/src/state/ops.ts +76 -0
  124. package/src/state/records.ts +35 -0
  125. package/src/state/runs.ts +99 -0
  126. package/src/state/saved.ts +119 -0
  127. package/src/status/file.ts +49 -0
  128. package/src/status/format.ts +132 -0
  129. package/src/status/sections.ts +14 -0
  130. package/src/status/version.ts +2 -0
  131. package/src/time.ts +7 -0
  132. package/src/worker/alignment-report.ts +18 -0
  133. package/src/worker/answers.ts +153 -0
  134. package/src/worker/base-check.ts +46 -0
  135. package/src/worker/checks.ts +26 -0
  136. package/src/worker/kind.ts +68 -0
  137. package/src/worker/land.ts +508 -0
  138. package/src/worker/lifecycle.ts +97 -0
  139. package/src/worker/merged.ts +29 -0
  140. package/src/worker/open.ts +100 -0
  141. package/src/worker/prose-run.ts +56 -0
  142. package/src/worker/questions.ts +89 -0
  143. package/src/worker/scheduler.ts +135 -0
  144. package/src/worker/signature.ts +41 -0
  145. package/src/worker/tally.ts +25 -0
  146. package/src/worker/taste.ts +38 -0
  147. package/src/worker/thin-path.ts +886 -0
  148. package/src/worker/worker.ts +728 -0
  149. package/surface.json +8787 -0
  150. package/README.md +0 -3
package/src/index.ts ADDED
@@ -0,0 +1,200 @@
1
+ export { type Command, commands } from "./cli/commands.ts";
2
+
3
+ export { type CheckEnv, pickTier } from "./config/checks.ts";
4
+
5
+ export {
6
+ applyConfigChange,
7
+ type ConfigChange,
8
+ type ConfigResult,
9
+ isHumBranch,
10
+ } from "./config/change.ts";
11
+
12
+ export { hashJson } from "./config/hash.ts";
13
+
14
+ export { Json } from "./config/json.ts";
15
+
16
+ export {
17
+ type Config,
18
+ listHopeSkills,
19
+ loadConfig,
20
+ type LoadOptions,
21
+ type RawConfig,
22
+ readConfigDir,
23
+ } from "./config/load.ts";
24
+
25
+ export { kindMachine } from "./config/machine.ts";
26
+
27
+ export { MIGRATIONS, type Migrations, migrateConfig } from "./config/migrate.ts";
28
+
29
+ export { describeProblem, type Problem, type Refusal, type Result } from "./config/refusal.ts";
30
+
31
+ export {
32
+ FIXED_PARTS,
33
+ HumConfig,
34
+ Kind,
35
+ MachineJSON,
36
+ OWNER_CHANGEABLE,
37
+ OWNER_WAIT_TAG,
38
+ READABLE_SCHEMA_VERSIONS,
39
+ SCHEMA_VERSION,
40
+ STUCK_TAG,
41
+ WAIT_TAG,
42
+ } from "./config/schema.ts";
43
+
44
+ export { migrateSnapshot, type SavedItem, unmappedItems } from "./config/state-map.ts";
45
+
46
+ export { type Codec, Items, persistedSnapshot, type RestoreOptions } from "./state/items.ts";
47
+
48
+ export { EventLog, type NewEvent, SavedEvent } from "./state/events.ts";
49
+
50
+ export { Faults, type FaultInput, type Outcome } from "./state/faults.ts";
51
+
52
+ export {
53
+ ENGINE_VERSION,
54
+ ENVELOPE_FORMAT,
55
+ ENVELOPE_MIGRATIONS,
56
+ Envelope,
57
+ type EnvelopeMigrations,
58
+ } from "./state/format.ts";
59
+
60
+ export { OpKey, OpKeys, type OpProvider } from "./state/ops.ts";
61
+
62
+ export { defaultStateDir, openState, type State, type StateOptions } from "./state/open.ts";
63
+
64
+ export { type BegunRun, RunKey, RunRecord, RunRecords } from "./state/runs.ts";
65
+
66
+ export { readStatus, type StatusRead, writeStatus } from "./status/file.ts";
67
+
68
+ export { Action, Card, CardAction, Choice, emptyStatus, InFlight, Measure, Merged, Question, Status, STATUS_FORMAT } from "./status/format.ts";
69
+
70
+ export * from "./adapter-api.ts";
71
+
72
+ export { type Admission, admit, type AdmitInput, engineImporters, sourceFiles } from "./adapters/admit.ts";
73
+
74
+ export { type Check, contractChecks, type ContractResult, type ContractTarget, runContract } from "./adapters/contract.ts";
75
+
76
+ export { FakeProvider, ProviderError } from "./adapters/fake.ts";
77
+
78
+ export { pushArgs, PushRefused } from "./adapters/git.ts";
79
+
80
+ export { type GuardOptions, guarded } from "./adapters/guard.ts";
81
+
82
+ export { type GitHubClient, gitHubClient, githubAdapter, type GitHubOptions, repoFromRemote } from "./adapters/github.ts";
83
+
84
+ export { Marker, marked, markerOf, parseComment } from "./adapters/marked.ts";
85
+
86
+ export { markdownAdapter, type MarkdownOptions } from "./adapters/markdown.ts";
87
+
88
+ export { markDone, parseItemFile } from "./adapters/item-file.ts";
89
+
90
+ export { refuseOtherVersion, resolveAdapters, type Slot, SLOTS } from "./adapters/registry.ts";
91
+
92
+ export { type OpName } from "./cli/commands.ts";
93
+
94
+ export { type Io, runCli, runOp } from "./cli/ops.ts";
95
+
96
+ export { limitOf, type LimitEnded, parseLimit, settleLimit } from "./adapters/limit.ts";
97
+
98
+ export { BACKGROUND_CHECK_CODE, FALLBACK_CODE, type BackgroundOptions, type BackgroundStartOptions, backgroundRunner, backgroundSettings, type StartedBackground, startBackgroundRunner } from "./adapters/claude-bg.ts";
99
+
100
+ export {
101
+ CLAUDE_FLOOR,
102
+ type Effort,
103
+ type Finished,
104
+ RUN_SETTINGS,
105
+ type RunOutcome,
106
+ type RunRequest,
107
+ type RunSpec,
108
+ type Runner,
109
+ type RunnerOptions,
110
+ runArgs,
111
+ startRunner,
112
+ succeeded,
113
+ } from "./adapters/claude-p.ts";
114
+
115
+ export {
116
+ PLAN_MARK,
117
+ RUN_SCHEMA,
118
+ runBranch,
119
+ runThinPath,
120
+ type Checkout,
121
+ type Source,
122
+ type ThinPathOptions,
123
+ type ThinResult,
124
+ } from "./worker/thin-path.ts";
125
+
126
+ export { type Change, type Histories, LABELS, type Label, type Moment, type PathHistory, type Placement, type Policy, placeChange, type Step, usualSize } from "./decide/risk.ts";
127
+
128
+ export { type Approvals, isCleared, type Landing, landingOf, needsReviewRun, OWNER_APPROVAL, type Route, routeOf, type Unclear, waitReason } from "./decide/merge.ts";
129
+
130
+ export { reviewInput } from "./decide/review.ts";
131
+
132
+ export { citesDiff, DATA_RULE, fenced, EventPick, isTrusted, PlanFile, planHash, PROSE_STATES, type ProsePurpose, readPick } from "./decide/prose.ts";
133
+
134
+ export { declaredKind, kindQuestion, leavesDocs, UNSURE } from "./decide/kind.ts";
135
+
136
+ export { AlignmentAnswer, alignedPlacement, alignmentQuestion, GRADES, raisesLevel } from "./decide/alignment.ts";
137
+
138
+ export { configuredModelCall, type Judgment, type ModelCall, modelCallOver, type Question as ModelQuestion } from "./adapters/model-call.ts";
139
+
140
+ export { decideProse, type ProseDecision } from "./worker/prose-run.ts";
141
+
142
+ export { decideKind } from "./worker/kind.ts";
143
+
144
+ export { labelChanges, type LabelRules, matchedSample, type PastChange } from "./decide/labels.ts";
145
+
146
+ export { changeOf, historiesOf, readHistory } from "./adapters/history.ts";
147
+
148
+ export { type CommitLanded, landOwnerCommit, type OwnerCommit } from "./adapters/owner-commit.ts";
149
+
150
+ export { type BranchRules, gitHubPullRequests, methodFor, type Pull, type PullRequests } from "./adapters/pull-requests.ts";
151
+
152
+ export { land, type LandContext, prepareLanding, ruleGlobFor } from "./worker/land.ts";
153
+
154
+ export { roundupAdapter, type RoundupOptions, stdioCall, type ToolCall } from "./adapters/roundup.ts";
155
+
156
+ export { type CallScope, onceCalls } from "./adapters/once.ts";
157
+
158
+ export { serialized } from "./adapters/serial.ts";
159
+
160
+ export { Records } from "./state/records.ts";
161
+
162
+ export { Limit, Parked, ReadyList, Spent } from "./state/saved.ts";
163
+
164
+ export type { SavedSnapshot } from "./state/items.ts";
165
+
166
+ export { Heartbeat } from "./status/format.ts";
167
+
168
+ export { readHeartbeat, writeHeartbeat } from "./status/file.ts";
169
+
170
+ export { type Candidate, type Footprint, footprintOf, overlaps, pick, type Picked, PrefixGate } from "./worker/scheduler.ts";
171
+
172
+ export { installedVersion, type Signature, signatureOf } from "./worker/signature.ts";
173
+
174
+ export { openWorker, type OpenWorkerOptions, npmLatest } from "./worker/open.ts";
175
+
176
+ export { Worker, type WorkerOptions } from "./worker/worker.ts";
177
+
178
+ export type { Exit, TickResult } from "./worker/lifecycle.ts";
179
+
180
+ export { claimTtlMs, park, rememberKinds } from "./worker/thin-path.ts";
181
+
182
+ export { CI_TICK_REF, readCiTick, recordCiTick } from "./service/ci-tick.ts";
183
+
184
+ export { type Child, EXIT_UPDATED, supervise, type Supervision } from "./service/supervise.ts";
185
+
186
+ export { checkTokenAge, TOKEN_LIFE_DAYS } from "./service/token.ts";
187
+
188
+ export { renderUnit, type UnitKind, type UnitVars } from "./service/unit.ts";
189
+
190
+ export { reportCards, wakeUntilQuiet } from "./service/wake.ts";
191
+
192
+ export { ciWorkflows, type WorkflowFile, type WorkflowSettings } from "./service/workflows.ts";
193
+
194
+ export { writeCards } from "./status/file.ts";
195
+
196
+ export { SLOT_ENV, slotEnv } from "./slots/env.ts";
197
+
198
+ export { type PoolOptions, SLOT_KEPT_CODE, SLOT_MAINTENANCE_CODE, SLOT_OUT_CODE, SlotPool } from "./slots/pool.ts";
199
+
200
+ export { proposeRecipe } from "./slots/scan.ts";
@@ -0,0 +1,8 @@
1
+ import type { State } from "../state/open.ts";
2
+
3
+ /** Paths blocked by a break whose level blocks and whose follow-up has not merged. */
4
+ export const blockedPaths = (state: State): string[] =>
5
+ state.breaks.list().flatMap(({ onBreak, followUp, paths }) => (onBreak === "block" && !state.merges.get(followUp) ? paths : []));
6
+
7
+ /** The commits whose merge broke the base: the fix limit counts them beside the breaks git labels. */
8
+ export const brokenCommits = (state: State): ReadonlySet<string> => new Set(state.breaks.list().map(({ sha }) => sha));
@@ -0,0 +1,76 @@
1
+ import { createHash } from "node:crypto";
2
+ import type { WorkSource } from "../adapter-api.ts";
3
+ import type { State } from "../state/open.ts";
4
+ import { noteKey, type RepeatGroup, type ReviewNote } from "./records.ts";
5
+
6
+ const SHORT_HASH = 10;
7
+ const TITLE_CELLS = 60;
8
+
9
+ const hashOf = (groupKey: string): string => createHash("sha1").update(groupKey).digest("hex").slice(0, SHORT_HASH);
10
+
11
+ /** The name the check item is told to give its check, so CI's log can name it. */
12
+ export const checkNameOf = (groupKey: string): string => `hum-check-${hashOf(groupKey)}`;
13
+
14
+ /** The id a group files its items from; filing the same title from it twice returns the first item. */
15
+ export const filedFrom = (groupKey: string): string => `rn-${hashOf(groupKey)}`;
16
+
17
+ const oneLine = (text: string): string => text.replace(/\s+/g, " ").trim();
18
+
19
+ const where = ({ file, line }: ReviewNote): string => (file === undefined ? "" : ` on ${file}${line === undefined ? "" : `:${line}`}`);
20
+
21
+ const quoted = (text: string): string => text.split("\n").map((line) => `> ${line}`).join("\n");
22
+
23
+ const noteBlock = (note: ReviewNote): string =>
24
+ [`- ${note.source} ${note.sourceId}${where(note)}, change ${note.change}`, quoted(note.text), ...(note.hunk === "" ? [] : ["", "```diff", note.hunk.trimEnd(), "```"])].join("\n");
25
+
26
+ const titleOf = (note: ReviewNote): string => `Check: ${oneLine(note.text).slice(0, TITLE_CELLS)}`;
27
+
28
+ /** The check item for a group: the kind, the name its check takes, and every note with its link and hunk. */
29
+ const checkItemOf = (first: ReviewNote, notes: readonly ReviewNote[], checkKind: string, checkName: string) => ({
30
+ title: titleOf(first),
31
+ body: [
32
+ `Kind: ${checkKind}`,
33
+ "",
34
+ `The owner made these review notes about one rule. Turn them into one rule with hope:anchor, widen it where the notes show a wider class, and add the cheapest check that decides it: a type, then a rule the repo's linter has, then a custom lint rule, then a test. Name the check \`${checkName}\`. It must fail on each note's commit and pass on the base.`,
35
+ "",
36
+ notes.map(noteBlock).join("\n\n"),
37
+ ].join("\n"),
38
+ });
39
+
40
+ export type FilingOptions = {
41
+ readonly state: State;
42
+ readonly workSource: Pick<WorkSource, "fileFollowUp" | "comment">;
43
+ /** Notes stating one rule at which a check item is filed. */
44
+ readonly repeatAt: number;
45
+ readonly checkKind: string;
46
+ };
47
+
48
+ const notesOf = (state: State, group: RepeatGroup): ReviewNote[] =>
49
+ group.notes.flatMap((key) => state.notes.get(key) ?? []).toSorted((a, b) => a.at - b.at);
50
+
51
+ /**
52
+ * Files one check item for each group that reached `repeatAt` notes and has none, and adds every later note to its
53
+ * item's thread. The item's id comes from the group, so a rerun files nothing.
54
+ */
55
+ export const fileChecks = async ({ state, workSource, repeatAt, checkKind }: FilingOptions): Promise<void> => {
56
+ for (const group of state.repeats.list()) {
57
+ const notes = notesOf(state, group);
58
+
59
+ const [first] = notes;
60
+
61
+ if (first === undefined || notes.length < repeatAt) continue;
62
+
63
+ if (group.item === undefined) {
64
+ const filed = await workSource.fileFollowUp(filedFrom(group.key), checkItemOf(first, notes, checkKind, checkNameOf(group.key)));
65
+
66
+ state.repeats.put(group.key, { ...group, item: filed.id, commented: group.notes });
67
+ continue;
68
+ }
69
+
70
+ const unposted = notes.filter((found) => !group.commented.includes(noteKey(found)));
71
+
72
+ for (const note of unposted) await workSource.comment(group.item, noteBlock(note));
73
+
74
+ if (unposted.length > 0) state.repeats.put(group.key, { ...group, commented: group.notes });
75
+ }
76
+ };
package/src/loop/ci.ts ADDED
@@ -0,0 +1,8 @@
1
+ import type { RepeatGroup } from "./records.ts";
2
+ import { checkNameOf } from "./check-item.ts";
3
+
4
+ /** The text of CI runs that started after a time; the integration reads them from the CI provider. */
5
+ export type CiLogs = (since: number) => Promise<readonly string[]>;
6
+
7
+ /** Whether a CI log names the check: the next run after the check landed proves it ran. */
8
+ export const logNames = (log: string, groupKey: RepeatGroup["key"]): boolean => log.includes(checkNameOf(groupKey));
@@ -0,0 +1,81 @@
1
+ import pMemoize from "p-memoize";
2
+ import { z } from "zod";
3
+ import type { RawComment } from "./notes.ts";
4
+
5
+ /** What the reads need of a GitHub client: one REST call. */
6
+ export type Requester = { readonly request: (route: string, params?: Readonly<Record<string, string | number>>) => Promise<{ readonly data: unknown }> };
7
+
8
+ const PAGE_SIZE = 100;
9
+ const BOT = "Bot";
10
+
11
+ const ReviewComment = z.object({
12
+ id: z.number(),
13
+ user: z.object({ login: z.string(), type: z.string() }).nullable(),
14
+ body: z.string(),
15
+ commit_id: z.string(),
16
+ path: z.string().optional(),
17
+ line: z.number().nullable().optional(),
18
+ diff_hunk: z.string().optional(),
19
+ created_at: z.string(),
20
+ });
21
+
22
+ const Jobs = z.object({ jobs: z.array(z.object({ id: z.number() })) });
23
+ const Runs = z.object({ workflow_runs: z.array(z.object({ id: z.number() })) });
24
+
25
+ const pagesOf = async <T>(read: (page: number) => Promise<T[]>): Promise<T[]> => {
26
+ const all: T[] = [];
27
+
28
+ for (let page = 1; ; page++) {
29
+ const found = await read(page);
30
+
31
+ all.push(...found);
32
+
33
+ if (found.length < PAGE_SIZE) return all;
34
+ }
35
+ };
36
+
37
+ /** Every pull request review comment on the repo since a time, as raw comments. */
38
+ export const reviewComments = async (client: Requester, repo: string, since: number): Promise<RawComment[]> =>
39
+ (
40
+ await pagesOf(async (page) =>
41
+ z.array(ReviewComment).parse((await client.request(`GET /repos/${repo}/pulls/comments`, { per_page: PAGE_SIZE, page, since: new Date(since).toISOString(), sort: "created", direction: "asc" })).data),
42
+ )
43
+ ).map((found) => ({
44
+ kind: "comment",
45
+ id: String(found.id),
46
+ author: found.user?.login ?? "",
47
+ isBot: found.user?.type === BOT,
48
+ body: found.body,
49
+ commit: found.commit_id,
50
+ file: found.path,
51
+ line: found.line ?? undefined,
52
+ hunk: found.diff_hunk,
53
+ at: Date.parse(found.created_at),
54
+ }));
55
+
56
+ /** The logs of the newest runs on the base branch that started after a time, one text per job. */
57
+ export const ciLogsOf =
58
+ (client: Requester, repo: string, base: string, runsRead: number) =>
59
+ async (since: number): Promise<string[]> => {
60
+ const { workflow_runs: runs } = Runs.parse((await client.request(`GET /repos/${repo}/actions/runs`, { branch: base, per_page: runsRead, created: `>=${new Date(since).toISOString()}` })).data);
61
+
62
+ return (
63
+ await Promise.all(
64
+ runs.map(async ({ id }) => {
65
+ const { jobs } = Jobs.parse((await client.request(`GET /repos/${repo}/actions/runs/${id}/jobs`)).data);
66
+
67
+ return Promise.all(jobs.map(async (job) => String((await client.request(`GET /repos/${repo}/actions/jobs/${job.id}/logs`)).data)));
68
+ }),
69
+ )
70
+ ).flat(2);
71
+ };
72
+
73
+ /** The reads of a repo on GitHub, connected on first use with the owner's own login. */
74
+ export const githubLoopReads = (repo: string, base: string, runsRead: number, connect: () => Promise<Requester>) => {
75
+ const connected = pMemoize(connect);
76
+
77
+ return {
78
+ comments: async (since: number) => reviewComments(await connected(), repo, since),
79
+ ciLogs: async (since: number) => ciLogsOf(await connected(), repo, base, runsRead)(since),
80
+ };
81
+ };
@@ -0,0 +1,108 @@
1
+ import { createHash } from "node:crypto";
2
+ import { extname } from "node:path";
3
+ import type { State } from "../state/open.ts";
4
+ import { noteKey, type RepeatGroup, type ReviewNote } from "./records.ts";
5
+ import { type Pairing, pairing, prepare } from "./signals.ts";
6
+
7
+ /** A model's answer to whether two notes state one rule; `undefined` when it abstained. */
8
+ export type Proposal = { readonly same: boolean; readonly models: readonly string[] } | undefined;
9
+
10
+ export type Proposer = (a: ReviewNote, b: ReviewNote) => Promise<Proposal>;
11
+
12
+ export type GroupingOptions = {
13
+ readonly state: State;
14
+ /** Row 14's model call: asked only about notes no signal pairs. */
15
+ readonly propose: Proposer;
16
+ /** Whether a model's proposal files a check alone: its precision on labelled pairs reached the owner's bar. */
17
+ readonly trusts: (models: readonly string[]) => boolean;
18
+ /** Told when a note's language left a signal unapplied, so it is never silent. */
19
+ readonly onUnapplied: (extension: string, signals: readonly string[]) => void;
20
+ };
21
+
22
+ const noteOf = (state: State, key: string): ReviewNote => {
23
+ const note = state.notes.get(key);
24
+
25
+ if (!note) throw new Error(`group member ${key} has no saved note`);
26
+
27
+ return note;
28
+ };
29
+
30
+ /** The first group a deterministic signal puts the note in, with every group it pairs with. */
31
+ const pairedGroups = (state: State, note: ReviewNote, groups: readonly RepeatGroup[], onUnapplied: GroupingOptions["onUnapplied"]): RepeatGroup[] => {
32
+ const target = prepare(note);
33
+
34
+ const report = ({ unapplied }: Pairing): void => {
35
+ if (unapplied.length > 0) onUnapplied(extname(note.file ?? "") || "(none)", unapplied);
36
+ };
37
+
38
+ return groups.filter((group) =>
39
+ group.notes.some((key) => {
40
+ const found = pairing(prepare(noteOf(state, key)), target);
41
+
42
+ report(found);
43
+
44
+ return found.signal !== undefined;
45
+ }),
46
+ );
47
+ };
48
+
49
+ const byAge = (state: State) => (a: RepeatGroup, b: RepeatGroup): number => noteOf(state, a.key).at - noteOf(state, b.key).at || (a.key < b.key ? -1 : 1);
50
+
51
+ const join = (state: State, group: RepeatGroup, key: string): RepeatGroup => {
52
+ const joined = { ...group, notes: group.notes.includes(key) ? group.notes : [...group.notes, key] };
53
+
54
+ state.repeats.put(group.key, joined);
55
+
56
+ return joined;
57
+ };
58
+
59
+ /** Joins the groups the note pairs with into the oldest one. */
60
+ const mergeInto = (state: State, groups: readonly RepeatGroup[], key: string): void => {
61
+ const [oldest, ...rest] = groups.toSorted(byAge(state));
62
+
63
+ if (!oldest) return;
64
+
65
+ const merged = { ...oldest, notes: [...new Set([oldest, ...rest].flatMap(({ notes }) => notes))], commented: [...new Set([oldest, ...rest].flatMap(({ commented }) => commented))] };
66
+
67
+ for (const other of rest) state.repeats.remove(other.key);
68
+
69
+ join(state, merged, key);
70
+ };
71
+
72
+ const keyOfPair = (a: string, b: string): string => createHash("sha1").update([a, b].sort().join("|")).digest("hex").slice(0, 16);
73
+
74
+ /**
75
+ * Puts each new note in the group whose notes it repeats. Signals decide first, in order. A note no signal pairs
76
+ * goes to the model with one note of each group; a pair it calls alike joins the group only once the model is trusted,
77
+ * and waits as a possible repeat before that. A note with no group starts its own.
78
+ */
79
+ export const groupNotes = async ({ state, propose, trusts, onUnapplied }: GroupingOptions, fresh: readonly ReviewNote[]): Promise<void> => {
80
+ for (const note of fresh.toSorted((a, b) => a.at - b.at)) {
81
+ const key = noteKey(note);
82
+ const groups = state.repeats.list();
83
+ const found = pairedGroups(state, note, groups, onUnapplied);
84
+
85
+ if (found.length > 0) {
86
+ mergeInto(state, found, key);
87
+ continue;
88
+ }
89
+
90
+ let placed = false;
91
+
92
+ for (const group of groups.toSorted(byAge(state))) {
93
+ const proposal = await propose(noteOf(state, group.key), note);
94
+
95
+ if (proposal?.same !== true) continue;
96
+
97
+ if (trusts(proposal.models)) {
98
+ join(state, group, key);
99
+ placed = true;
100
+ break;
101
+ }
102
+
103
+ state.possibleRepeats.put(keyOfPair(group.key, key), { a: group.key, b: key, models: [...proposal.models], at: note.at });
104
+ }
105
+
106
+ if (!placed) state.repeats.put(key, { key, notes: [key], commented: [], misses: [] });
107
+ }
108
+ };
@@ -0,0 +1,70 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { z } from "zod";
3
+ import type { Finished } from "../adapters/claude-p.ts";
4
+
5
+ export type MemoryEntry = { readonly rule: string; readonly source: string };
6
+
7
+ /** One entry per line: `- <rule> (source: <where the owner said it>)`. */
8
+ const ENTRY_LINE = /^- (.+) \(source: (.+)\)$/;
9
+
10
+ export class MemoryFormatError extends Error {
11
+ override name = "MemoryFormatError";
12
+ }
13
+
14
+ export class MemoryFull extends Error {
15
+ override name = "MemoryFull";
16
+ }
17
+
18
+ const squash = (rule: string): string => rule.toLowerCase().replace(/\s+/g, " ").trim();
19
+
20
+ /** The entries of a memory file. A list line with no source is refused: every entry names where it came from. */
21
+ export const parseMemory = (text: string): MemoryEntry[] =>
22
+ text.split("\n").flatMap((line, at): MemoryEntry[] => {
23
+ if (!line.startsWith("- ")) return [];
24
+
25
+ const [, rule, source] = ENTRY_LINE.exec(line) ?? [];
26
+
27
+ if (rule === undefined || source === undefined) throw new MemoryFormatError(`memory.md line ${at + 1} has no "(source: …)": ${line.slice(0, 80)}`);
28
+
29
+ return [{ rule, source }];
30
+ });
31
+
32
+ export const renderMemory = (entries: readonly MemoryEntry[]): string => entries.map(({ rule, source }) => `- ${rule} (source: ${source})\n`).join("");
33
+
34
+ /** A new entry on a rule already held replaces it, so one rule has one entry; past the byte cap the entry is refused. */
35
+ export const addEntry = (entries: readonly MemoryEntry[], entry: MemoryEntry, maxBytes: number): MemoryEntry[] => {
36
+ const next = [...entries.filter(({ rule }) => squash(rule) !== squash(entry.rule)), entry];
37
+
38
+ if (Buffer.byteLength(renderMemory(next)) > maxBytes) throw new MemoryFull(`memory.md would pass ${maxBytes} bytes; remove an entry first`);
39
+
40
+ return next;
41
+ };
42
+
43
+ /** The entries left once a check covers the rules whose sources are `covered`. */
44
+ export const withoutCovered = (entries: readonly MemoryEntry[], covered: ReadonlySet<string>): MemoryEntry[] => entries.filter(({ source }) => !covered.has(source));
45
+
46
+ /** Entries no run used within the last `limit` runs: `lastUsed` is the run number of an entry's last use, absent when none used it. */
47
+ export const unusedEntries = (entries: readonly MemoryEntry[], lastUsed: ReadonlyMap<string, number>, runsSoFar: number, limit: number): MemoryEntry[] =>
48
+ entries.filter(({ rule }) => runsSoFar - (lastUsed.get(rule) ?? 0) >= limit);
49
+
50
+ /** The entries of the file at `path`; none when it is absent. */
51
+ export const readMemory = (path: string): MemoryEntry[] => (existsSync(path) ? parseMemory(readFileSync(path, "utf8")) : []);
52
+
53
+ /** What a run reads of memory.md: its rules, numbered, and the ask to name the ones it applied. */
54
+ export const memorySection = (entries: readonly MemoryEntry[]): string =>
55
+ entries.length === 0 ? "" : `\n\n## Standing rules\n${entries.map(({ rule }, at) => `${at + 1}. ${rule}`).join("\n")}\nIn "memoryUsed", give the number of each rule you applied.`;
56
+
57
+ const Applied = z.object({ memoryUsed: z.array(z.int().positive()) });
58
+
59
+ /** The rules a run says it applied, from the numbers in its answer; numbers beyond the list name nothing. */
60
+ export const rulesUsed = ({ structured }: Pick<Finished, "structured">, entries: readonly MemoryEntry[]): string[] =>
61
+ (Applied.safeParse(structured).data?.memoryUsed ?? []).flatMap((number) => entries[number - 1]?.rule ?? []);
62
+
63
+ export type MemoryUse = { readonly runsSoFar: number; readonly lastUsed: ReadonlyMap<string, number> };
64
+
65
+ /** From the runs that were shown memory.md, oldest first: how many there were, and the run each rule was last used in. */
66
+ export const useOf = (runs: readonly { readonly memoryUsed?: readonly string[] }[]): MemoryUse => {
67
+ const shown = runs.flatMap(({ memoryUsed }) => (memoryUsed === undefined ? [] : [memoryUsed]));
68
+
69
+ return { runsSoFar: shown.length, lastUsed: new Map(shown.flatMap((rules, at) => rules.map((rule): [string, number] => [rule, at + 1]))) };
70
+ };
@@ -0,0 +1,76 @@
1
+ import type { PastChange } from "../decide/labels.ts";
2
+ import { breakOf, type LabelRules } from "../decide/labels.ts";
3
+ import type { SettledAnswer } from "../state/saved.ts";
4
+ import type { State } from "../state/open.ts";
5
+ import { type NoteSource, noteKey, type ReviewNote } from "./records.ts";
6
+
7
+ /** What an integration reads from the review surface, before the owner's own are told from the rest. */
8
+ export type RawComment = {
9
+ readonly kind: "comment" | "review";
10
+ readonly id: string;
11
+ readonly author: string;
12
+ readonly isBot: boolean;
13
+ readonly body: string;
14
+ /** The commit the comment sits on. */
15
+ readonly commit: string;
16
+ readonly file?: string;
17
+ readonly line?: number;
18
+ readonly hunk?: string;
19
+ readonly at: number;
20
+ };
21
+
22
+ const SOURCE_OF_KIND: Readonly<Record<RawComment["kind"], NoteSource>> = { comment: "review-comment", review: "review-body" };
23
+
24
+ /** The owner's comments and review bodies; a bot's, another person's and an empty body are not review notes. */
25
+ export const ownerNotes = (raw: readonly RawComment[], owners: readonly string[]): ReviewNote[] =>
26
+ raw.flatMap((found): ReviewNote[] =>
27
+ owners.includes(found.author) && !found.isBot && found.body.trim() !== ""
28
+ ? [{ source: SOURCE_OF_KIND[found.kind], sourceId: found.id, change: found.commit, file: found.file, line: found.line, hunk: found.hunk ?? "", text: found.body.trim(), at: found.at }]
29
+ : [],
30
+ );
31
+
32
+ /** The option that approves a plan; every other answer changed it. */
33
+ export const APPROVAL_OPTION = "approve";
34
+
35
+ /** An answer that changed the plan: one applied, and not an approval. */
36
+ export const planChangingNotes = (answers: readonly SettledAnswer[]): ReviewNote[] =>
37
+ answers
38
+ .flatMap(({ item, ordinal, text, at, disposition, option }): ReviewNote[] =>
39
+ disposition === "applied" && option !== APPROVAL_OPTION ? [{ source: "plan-answer", sourceId: `${item}:${ordinal}`, change: item, item, hunk: "", text, at }] : [],
40
+ );
41
+
42
+ /** The revert or fix commit behind each broken change, with the hunk it made. */
43
+ export const breakNotes = (changes: readonly PastChange[], rules: LabelRules, hunkOf: (sha: string, file: string | undefined) => Promise<string>): Promise<ReviewNote[]> =>
44
+ Promise.all(
45
+ changes.flatMap((change, at) => {
46
+ const found = breakOf(changes, at, rules);
47
+
48
+ if (!found) return [];
49
+
50
+ const file = found.by.files.find((path) => change.files.includes(path));
51
+
52
+ return [
53
+ hunkOf(found.by.sha, file).then(
54
+ (hunk): ReviewNote => ({
55
+ source: found.label === "reverted" ? "revert" : "fix",
56
+ sourceId: found.by.sha,
57
+ change: change.sha,
58
+ file,
59
+ hunk,
60
+ text: `${found.by.subject}\n${found.by.body}`.trim(),
61
+ at: found.by.committedAt ?? at,
62
+ }),
63
+ ),
64
+ ];
65
+ }),
66
+ );
67
+
68
+ /** Saves each note once, keyed by its source and source id; the ones this call added are returned. */
69
+ export const saveNotes = (state: State, notes: readonly ReviewNote[]): ReviewNote[] =>
70
+ notes.filter((note) => {
71
+ if (state.notes.get(noteKey(note))) return false;
72
+
73
+ state.notes.put(noteKey(note), note);
74
+
75
+ return true;
76
+ });
@@ -0,0 +1,39 @@
1
+ import { z } from "zod";
2
+ import type { Question } from "../adapters/model-call.ts";
3
+ import type { ModelCall } from "../adapters/model-call.ts";
4
+ import type { HumConfig } from "../config/schema.ts";
5
+ import { DATA_RULE, fenced } from "../decide/prose.ts";
6
+ import { LOOP_SPEND_PREFIX } from "../measure/inputs.ts";
7
+ import type { Proposer } from "./groups.ts";
8
+ import { noteKey, type ReviewNote } from "./records.ts";
9
+
10
+ const SamePair = z.strictObject({ same: z.boolean() });
11
+
12
+ const described = (label: string, { text, file }: ReviewNote): string => fenced(label, file === undefined ? text : `${file}\n${text}`);
13
+
14
+ /** The cheap model call that says whether two review notes state one rule. */
15
+ export const pairQuestion = (a: ReviewNote, b: ReviewNote): Question<z.infer<typeof SamePair>> => ({
16
+ boundary: "repeat-pair",
17
+ prompt: [
18
+ "Two review notes follow. Say whether they ask for the same thing: the same behaviour to do or to avoid, whatever the wording. One may be a terse complaint and the other a formal rule. Notes about the same area that ask for different things are not the same.",
19
+ DATA_RULE,
20
+ described("note-a", a),
21
+ described("note-b", b),
22
+ 'Answer with JSON {"same": true} or {"same": false}.',
23
+ ].join("\n\n"),
24
+ answer: SamePair,
25
+ item: `${LOOP_SPEND_PREFIX}${noteKey(b)}`,
26
+ });
27
+
28
+ /** Row 14's model call as a proposer; an abstention is no proposal. */
29
+ export const modelProposer =
30
+ (call: ModelCall): Proposer =>
31
+ async (a, b) => {
32
+ const judged = await call(pairQuestion(a, b));
33
+
34
+ return judged.kind === "picked" ? { same: judged.value.same, models: judged.models } : undefined;
35
+ };
36
+
37
+ /** Whether every model id behind a proposal reached the owner's precision bar on labelled pairs. */
38
+ export const trustsRepeats = (models: readonly string[], { measuredModels, repeatPrecision }: Pick<HumConfig, "measuredModels" | "repeatPrecision">): boolean =>
39
+ models.length > 0 && models.every((model) => (measuredModels["repeat-pair"]?.[model] ?? 0) >= repeatPrecision);