@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.
- package/defaults/hum.json +321 -0
- package/defaults/kinds/check.json +183 -0
- package/defaults/kinds/docs.json +151 -0
- package/defaults/kinds/feature.json +202 -0
- package/defaults/kinds/fix.json +183 -0
- package/defaults/kinds/hotfix.json +164 -0
- package/defaults/roles/build.md +3 -0
- package/defaults/roles/fix.md +1 -0
- package/defaults/roles/plan.md +3 -0
- package/defaults/roles/review.md +3 -0
- package/evals/alignment/RESULTS.md +8 -0
- package/evals/alignment/cases.jsonl +128 -0
- package/evals/alignment/sample.jsonl +20 -0
- package/evals/kind/RESULTS.md +5 -0
- package/evals/kind/cases.jsonl +24 -0
- package/evals/plan-outcome/RESULTS.md +4 -0
- package/evals/plan-outcome/cases.jsonl +12 -0
- package/evals/repeat-pair/RESULTS.md +4 -0
- package/evals/repeat-pair/cases.jsonl +51 -0
- package/evals/reply-option/RESULTS.md +3 -0
- package/evals/reply-option/cases.jsonl +45 -0
- package/evals/review-verdict/RESULTS.md +4 -0
- package/evals/review-verdict/cases.jsonl +12 -0
- package/package.json +88 -4
- package/schema/hum.schema.json +1355 -0
- package/scripts/eval.ts +244 -0
- package/src/adapter-api.ts +134 -0
- package/src/adapters/admit.ts +111 -0
- package/src/adapters/argument.ts +11 -0
- package/src/adapters/claim.ts +93 -0
- package/src/adapters/claude-bg.ts +375 -0
- package/src/adapters/claude-p.ts +593 -0
- package/src/adapters/contract.ts +231 -0
- package/src/adapters/fake.ts +157 -0
- package/src/adapters/git.ts +155 -0
- package/src/adapters/github.ts +457 -0
- package/src/adapters/guard.ts +152 -0
- package/src/adapters/history.ts +215 -0
- package/src/adapters/item-file.ts +102 -0
- package/src/adapters/limit.ts +67 -0
- package/src/adapters/markdown.ts +365 -0
- package/src/adapters/marked.ts +119 -0
- package/src/adapters/model-call.ts +75 -0
- package/src/adapters/once.ts +45 -0
- package/src/adapters/owner-commit.ts +60 -0
- package/src/adapters/pull-requests.ts +253 -0
- package/src/adapters/registry.ts +156 -0
- package/src/adapters/roundup.ts +291 -0
- package/src/adapters/serial.ts +22 -0
- package/src/adapters/transcript.ts +99 -0
- package/src/cli/commands.ts +46 -0
- package/src/cli/main.ts +8 -0
- package/src/cli/ops.ts +224 -0
- package/src/cli/service.ts +156 -0
- package/src/cli/setup.ts +141 -0
- package/src/cli/shared-watch.ts +127 -0
- package/src/config/change.ts +54 -0
- package/src/config/checks.ts +217 -0
- package/src/config/git-ref-name.d.ts +3 -0
- package/src/config/hash.ts +6 -0
- package/src/config/json.ts +6 -0
- package/src/config/load.ts +88 -0
- package/src/config/machine.ts +20 -0
- package/src/config/migrate.ts +61 -0
- package/src/config/refusal.ts +58 -0
- package/src/config/schema.ts +465 -0
- package/src/config/settings.ts +6 -0
- package/src/config/state-map.ts +76 -0
- package/src/decide/alignment.ts +79 -0
- package/src/decide/answer.ts +131 -0
- package/src/decide/kind.ts +39 -0
- package/src/decide/labels.ts +137 -0
- package/src/decide/merge.ts +102 -0
- package/src/decide/prose.ts +109 -0
- package/src/decide/question.ts +159 -0
- package/src/decide/review.ts +10 -0
- package/src/decide/risk.ts +216 -0
- package/src/decide/taste.ts +37 -0
- package/src/exec.ts +80 -0
- package/src/index.ts +200 -0
- package/src/loop/breaks.ts +8 -0
- package/src/loop/check-item.ts +76 -0
- package/src/loop/ci.ts +8 -0
- package/src/loop/github.ts +81 -0
- package/src/loop/groups.ts +108 -0
- package/src/loop/memory.ts +70 -0
- package/src/loop/notes.ts +76 -0
- package/src/loop/pair.ts +39 -0
- package/src/loop/pass.ts +99 -0
- package/src/loop/proof.ts +85 -0
- package/src/loop/prove-item.ts +33 -0
- package/src/loop/records.ts +75 -0
- package/src/loop/signals.ts +130 -0
- package/src/measure/inputs.ts +28 -0
- package/src/measure/measure.ts +68 -0
- package/src/service/cards.ts +24 -0
- package/src/service/ci-tick.ts +28 -0
- package/src/service/launchd.ts +16 -0
- package/src/service/supervise.ts +145 -0
- package/src/service/systemd.ts +44 -0
- package/src/service/token.ts +21 -0
- package/src/service/unit.ts +29 -0
- package/src/service/wake.ts +30 -0
- package/src/service/workflows.ts +97 -0
- package/src/setup/propose.ts +120 -0
- package/src/setup/prove.ts +216 -0
- package/src/setup/scan.ts +186 -0
- package/src/setup/scratch.ts +81 -0
- package/src/setup/shipped.ts +15 -0
- package/src/setup/tables.ts +101 -0
- package/src/setup/waits.ts +58 -0
- package/src/setup/write.ts +161 -0
- package/src/slots/env.ts +42 -0
- package/src/slots/pool.ts +329 -0
- package/src/slots/scan.ts +26 -0
- package/src/slots/worktree.ts +17 -0
- package/src/state/events.ts +52 -0
- package/src/state/faults.ts +73 -0
- package/src/state/format.ts +50 -0
- package/src/state/fs.ts +100 -0
- package/src/state/items.ts +189 -0
- package/src/state/open.ts +80 -0
- package/src/state/ops.ts +76 -0
- package/src/state/records.ts +35 -0
- package/src/state/runs.ts +99 -0
- package/src/state/saved.ts +119 -0
- package/src/status/file.ts +49 -0
- package/src/status/format.ts +132 -0
- package/src/status/sections.ts +14 -0
- package/src/status/version.ts +2 -0
- package/src/time.ts +7 -0
- package/src/worker/alignment-report.ts +18 -0
- package/src/worker/answers.ts +153 -0
- package/src/worker/base-check.ts +46 -0
- package/src/worker/checks.ts +26 -0
- package/src/worker/kind.ts +68 -0
- package/src/worker/land.ts +508 -0
- package/src/worker/lifecycle.ts +97 -0
- package/src/worker/merged.ts +29 -0
- package/src/worker/open.ts +100 -0
- package/src/worker/prose-run.ts +56 -0
- package/src/worker/questions.ts +89 -0
- package/src/worker/scheduler.ts +135 -0
- package/src/worker/signature.ts +41 -0
- package/src/worker/tally.ts +25 -0
- package/src/worker/taste.ts +38 -0
- package/src/worker/thin-path.ts +886 -0
- package/src/worker/worker.ts +728 -0
- package/surface.json +8787 -0
- package/README.md +0 -3
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import parseDiff from "parse-diff";
|
|
3
|
+
import PQueue from "p-queue";
|
|
4
|
+
import pkg from "../../package.json" with { type: "json" };
|
|
5
|
+
import type { HumConfig } from "../config/schema.ts";
|
|
6
|
+
import { labelChanges, type PastChange } from "../decide/labels.ts";
|
|
7
|
+
import type { Change, Histories, Label } from "../decide/risk.ts";
|
|
8
|
+
import { type Git, PATCH } from "./git.ts";
|
|
9
|
+
|
|
10
|
+
const FIELD = "\u0000";
|
|
11
|
+
const RECORD = "\u0001";
|
|
12
|
+
const LOG_FORMAT = "--format=%x01%H%x00%s%x00%ct%x00%b%x00";
|
|
13
|
+
const NUMSTAT_LINE = /^(\d+|-)\t(\d+|-)\t(.+)$/;
|
|
14
|
+
const BLAME_HEADER = /^([0-9a-f]{40}) \d+ \d+/;
|
|
15
|
+
const BLAMES_AT_ONCE = 8;
|
|
16
|
+
const MANIFEST = "package.json";
|
|
17
|
+
|
|
18
|
+
/** Each merged change on the first-parent line of `base`, oldest first, with its files and size; `blamed` is left empty. */
|
|
19
|
+
const readLog = async (
|
|
20
|
+
git: Git,
|
|
21
|
+
base: string,
|
|
22
|
+
depth: number,
|
|
23
|
+
): Promise<PastChange[]> => {
|
|
24
|
+
const out = await git.raw([
|
|
25
|
+
"log",
|
|
26
|
+
"--first-parent",
|
|
27
|
+
"--diff-merges=first-parent",
|
|
28
|
+
"--numstat",
|
|
29
|
+
"--no-renames",
|
|
30
|
+
LOG_FORMAT,
|
|
31
|
+
"-n",
|
|
32
|
+
String(depth),
|
|
33
|
+
base,
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
return out
|
|
37
|
+
.split(RECORD)
|
|
38
|
+
.filter(Boolean)
|
|
39
|
+
.map((record): PastChange => {
|
|
40
|
+
const [sha = "", subject = "", committed = "", body = "", stats = ""] = record.split(FIELD);
|
|
41
|
+
|
|
42
|
+
const counted = stats.split("\n").flatMap((line) => {
|
|
43
|
+
const [, added, deleted, file] = NUMSTAT_LINE.exec(line) ?? [];
|
|
44
|
+
|
|
45
|
+
return file === undefined
|
|
46
|
+
? []
|
|
47
|
+
: [{ file, lines: (Number(added) || 0) + (Number(deleted) || 0) }];
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
sha,
|
|
52
|
+
subject,
|
|
53
|
+
committedAt: Number(committed) * 1000,
|
|
54
|
+
body: body.trim(),
|
|
55
|
+
files: counted.map(({ file }) => file),
|
|
56
|
+
size: counted.reduce((sum, { lines }) => sum + lines, 0),
|
|
57
|
+
blamed: [],
|
|
58
|
+
};
|
|
59
|
+
})
|
|
60
|
+
.reverse();
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** Line ranges, in the parent's numbering, that a change deleted or rewrote, by the file's old path. */
|
|
64
|
+
const rewrittenRanges = (
|
|
65
|
+
diff: string,
|
|
66
|
+
): readonly { file: string; from: number; to: number }[] =>
|
|
67
|
+
parseDiff(diff).flatMap((file) =>
|
|
68
|
+
file.chunks.flatMap((chunk) => {
|
|
69
|
+
const deleted = chunk.changes.flatMap((change) =>
|
|
70
|
+
change.type === "del" ? [change.ln] : [],
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const first = deleted[0];
|
|
74
|
+
const last = deleted.at(-1);
|
|
75
|
+
|
|
76
|
+
return file.from && first !== undefined && last !== undefined
|
|
77
|
+
? [{ file: file.from, from: first, to: last }]
|
|
78
|
+
: [];
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
/** The changes that wrote the lines `sha` deleted or rewrote: git blame at its parent. */
|
|
83
|
+
const blame = async (git: Git, sha: string): Promise<readonly string[]> => {
|
|
84
|
+
const diff = await git.raw([
|
|
85
|
+
"show",
|
|
86
|
+
...PATCH,
|
|
87
|
+
"--diff-merges=first-parent",
|
|
88
|
+
"-U0",
|
|
89
|
+
"--format=",
|
|
90
|
+
sha,
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
const found = await Promise.all(
|
|
94
|
+
rewrittenRanges(diff).map(async ({ file, from, to }) => {
|
|
95
|
+
const result = await git.run([
|
|
96
|
+
"blame",
|
|
97
|
+
"--porcelain",
|
|
98
|
+
"-L",
|
|
99
|
+
`${from},${to}`,
|
|
100
|
+
`${sha}^`,
|
|
101
|
+
"--",
|
|
102
|
+
file,
|
|
103
|
+
]);
|
|
104
|
+
|
|
105
|
+
return result.code === 0
|
|
106
|
+
? result.stdout
|
|
107
|
+
.split("\n")
|
|
108
|
+
.flatMap((line) => BLAME_HEADER.exec(line)?.[1] ?? [])
|
|
109
|
+
: [];
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return [...new Set(found.flat())];
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** The merged changes on `base`, oldest first; each fix commit carries the changes whose lines it changed. */
|
|
117
|
+
export const readHistory = async (
|
|
118
|
+
git: Git,
|
|
119
|
+
base: string,
|
|
120
|
+
{
|
|
121
|
+
historyDepth,
|
|
122
|
+
changeLabels,
|
|
123
|
+
}: Pick<HumConfig, "historyDepth" | "changeLabels">,
|
|
124
|
+
): Promise<PastChange[]> => {
|
|
125
|
+
const changes = await readLog(git, base, historyDepth);
|
|
126
|
+
|
|
127
|
+
const rewrites = (change: PastChange): boolean =>
|
|
128
|
+
[changeLabels.fixSubject, changeLabels.revertSubject].some((subject) => new RegExp(subject).test(change.subject));
|
|
129
|
+
|
|
130
|
+
const queue = new PQueue({ concurrency: BLAMES_AT_ONCE });
|
|
131
|
+
|
|
132
|
+
return Promise.all(
|
|
133
|
+
changes.map(async (change) =>
|
|
134
|
+
rewrites(change)
|
|
135
|
+
? {
|
|
136
|
+
...change,
|
|
137
|
+
blamed: (await queue.add(() => blame(git, change.sha))) ?? [],
|
|
138
|
+
}
|
|
139
|
+
: change,
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** Each path rule's merged changes, newest first: their sizes and labels. */
|
|
145
|
+
export const historiesOf = (
|
|
146
|
+
changes: readonly PastChange[],
|
|
147
|
+
{ paths, changeLabels }: Pick<HumConfig, "paths" | "changeLabels">,
|
|
148
|
+
broken: ReadonlySet<string> = new Set(),
|
|
149
|
+
): Histories => {
|
|
150
|
+
const labels = labelChanges(changes, changeLabels, broken);
|
|
151
|
+
const newestFirst = changes.toReversed();
|
|
152
|
+
|
|
153
|
+
return new Map(
|
|
154
|
+
paths.map(({ glob }) => {
|
|
155
|
+
const onPath = newestFirst.filter((change) =>
|
|
156
|
+
change.files.some((file) => new Bun.Glob(glob).match(file)),
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
return [
|
|
160
|
+
glob,
|
|
161
|
+
{
|
|
162
|
+
sizes: onPath.map(({ size }) => size),
|
|
163
|
+
labels: onPath.map(({ sha }): Label => labels.get(sha) ?? "clean"),
|
|
164
|
+
},
|
|
165
|
+
];
|
|
166
|
+
}),
|
|
167
|
+
);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const matchesAny = (sources: readonly string[], line: string): boolean =>
|
|
171
|
+
sources.some((source) => new RegExp(source).test(line));
|
|
172
|
+
|
|
173
|
+
/** What a diff tells the risk level: the paths, the size, a loosened check and a change to the engine's pin. */
|
|
174
|
+
export const changeOf = (
|
|
175
|
+
diff: string,
|
|
176
|
+
{ loosenedCheck, lockfiles }: Pick<HumConfig, "loosenedCheck" | "lockfiles">,
|
|
177
|
+
): Change => {
|
|
178
|
+
const files = parseDiff(diff);
|
|
179
|
+
|
|
180
|
+
const lines = files.flatMap((file) =>
|
|
181
|
+
file.chunks.flatMap((chunk) =>
|
|
182
|
+
chunk.changes.map((change) => ({
|
|
183
|
+
file,
|
|
184
|
+
change,
|
|
185
|
+
text: change.content.slice(1),
|
|
186
|
+
})),
|
|
187
|
+
),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const holdsPin = (file: parseDiff.File): boolean =>
|
|
191
|
+
[MANIFEST, ...lockfiles].includes(basename(file.to ?? file.from ?? ""));
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
paths: [
|
|
195
|
+
...new Set(
|
|
196
|
+
files.flatMap((file) =>
|
|
197
|
+
[file.from, file.to].filter(
|
|
198
|
+
(path): path is string =>
|
|
199
|
+
path !== undefined && path !== "/dev/null",
|
|
200
|
+
),
|
|
201
|
+
),
|
|
202
|
+
),
|
|
203
|
+
],
|
|
204
|
+
size: files.reduce((sum, file) => sum + file.additions + file.deletions, 0),
|
|
205
|
+
loosened: lines.some(
|
|
206
|
+
({ change, text }) =>
|
|
207
|
+
(change.type === "add" && matchesAny(loosenedCheck.added, text)) ||
|
|
208
|
+
(change.type === "del" && matchesAny(loosenedCheck.removed, text)),
|
|
209
|
+
),
|
|
210
|
+
pinChanged: lines.some(
|
|
211
|
+
({ file, change, text }) =>
|
|
212
|
+
change.type !== "normal" && holdsPin(file) && text.includes(pkg.name),
|
|
213
|
+
),
|
|
214
|
+
};
|
|
215
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { RootContent } from "mdast";
|
|
2
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
3
|
+
import { frontmatterFromMarkdown } from "mdast-util-frontmatter";
|
|
4
|
+
import { frontmatter } from "micromark-extension-frontmatter";
|
|
5
|
+
import { parse, parseDocument, stringify } from "yaml";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { PermanentError, THREAD_KINDS, type ThreadEntry } from "../adapter-api.ts";
|
|
8
|
+
import { isMarkerLine, markerLine, splitMarker } from "./marked.ts";
|
|
9
|
+
|
|
10
|
+
/** The front matter `state` of an item hum may take. */
|
|
11
|
+
export const READY_STATE = "ready";
|
|
12
|
+
|
|
13
|
+
/** The front matter `state` of a merged item. */
|
|
14
|
+
const DONE_STATE = "done";
|
|
15
|
+
|
|
16
|
+
/** Titles are never folded over several lines, so the owner reads the line they wrote. */
|
|
17
|
+
const YAML_OPTIONS = { lineWidth: 0 };
|
|
18
|
+
|
|
19
|
+
const FRONT_MATTER_FENCE = "---";
|
|
20
|
+
|
|
21
|
+
const FrontMatter = z.looseObject({ title: z.string().min(1), state: z.string().min(1), kind: z.string().min(1).optional() });
|
|
22
|
+
|
|
23
|
+
const EntryMarker = z.strictObject({ kind: z.enum(THREAD_KINDS), at: z.int().nonnegative(), option: z.string().min(1).optional() });
|
|
24
|
+
|
|
25
|
+
const Span = z.object({ start: z.object({ offset: z.int() }), end: z.object({ offset: z.int() }) });
|
|
26
|
+
|
|
27
|
+
/** An item file: YAML front matter, a body, then thread entries, each opened by a hidden `<!-- hum {…} -->` line. */
|
|
28
|
+
export type ItemFile = { readonly title: string; readonly state: string; readonly kind: string | undefined; readonly body: string; readonly thread: ThreadEntry[] };
|
|
29
|
+
|
|
30
|
+
type EntryOpening = { readonly json: string; readonly start: number; readonly end: number };
|
|
31
|
+
|
|
32
|
+
const parseMarkdown = (source: string) => fromMarkdown(source, { extensions: [frontmatter()], mdastExtensions: [frontmatterFromMarkdown()] });
|
|
33
|
+
|
|
34
|
+
const spanOf = (node: RootContent) => {
|
|
35
|
+
const { start, end } = Span.parse(node.position);
|
|
36
|
+
|
|
37
|
+
return { start: start.offset, end: end.offset };
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** The marker JSON of a node that opens a thread entry; a hidden line inside a code block opens none. */
|
|
41
|
+
const entryJson = (node: RootContent): string | undefined => (node.type === "html" && isMarkerLine(node.value) ? splitMarker(node.value)?.json : undefined);
|
|
42
|
+
|
|
43
|
+
const entryOpenings = (nodes: readonly RootContent[]): EntryOpening[] =>
|
|
44
|
+
nodes.flatMap((node) => {
|
|
45
|
+
const json = entryJson(node);
|
|
46
|
+
|
|
47
|
+
return json === undefined ? [] : [{ json, ...spanOf(node) }];
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const parseEntry = (source: string, opening: EntryOpening, next: EntryOpening | undefined): ThreadEntry => {
|
|
51
|
+
const fields = EntryMarker.parse(JSON.parse(opening.json));
|
|
52
|
+
const text = source.slice(opening.end, next?.start ?? source.length).trim();
|
|
53
|
+
|
|
54
|
+
if (text === "") throw new Error(`thread entry at ${fields.at} has no text`);
|
|
55
|
+
|
|
56
|
+
return { ...fields, text };
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const parseItemFile = (source: string): ItemFile => {
|
|
60
|
+
const nodes = parseMarkdown(source).children;
|
|
61
|
+
const [head] = nodes;
|
|
62
|
+
|
|
63
|
+
if (head?.type !== "yaml") throw new Error("front matter missing: the file must open with a '---' block");
|
|
64
|
+
|
|
65
|
+
const front = FrontMatter.parse(parse(head.value));
|
|
66
|
+
const openings = entryOpenings(nodes);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
title: front.title,
|
|
70
|
+
state: front.state,
|
|
71
|
+
kind: front.kind,
|
|
72
|
+
body: source.slice(spanOf(head).end, openings[0]?.start ?? source.length).trim(),
|
|
73
|
+
thread: openings.map((opening, index) => parseEntry(source, opening, openings[index + 1])),
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const frontMatterBlock = (yaml: string): string => [FRONT_MATTER_FENCE, yaml.trimEnd(), FRONT_MATTER_FENCE].join("\n");
|
|
78
|
+
|
|
79
|
+
/** A new ready item file. */
|
|
80
|
+
export const newItemFile = (title: string, body: string): string => [frontMatterBlock(stringify({ title, state: READY_STATE }, YAML_OPTIONS)), "", body.trim(), ""].join("\n");
|
|
81
|
+
|
|
82
|
+
/** The file with `state: done` in its front matter, so the merged change itself takes the item off the ready list. */
|
|
83
|
+
export const markDone = (source: string): string => {
|
|
84
|
+
const [head] = parseMarkdown(source).children;
|
|
85
|
+
|
|
86
|
+
if (head?.type !== "yaml") throw new Error("front matter missing: the file must open with a '---' block");
|
|
87
|
+
|
|
88
|
+
const { start, end } = spanOf(head);
|
|
89
|
+
const front = parseDocument(head.value);
|
|
90
|
+
|
|
91
|
+
FrontMatter.parse(front.toJS());
|
|
92
|
+
front.set("state", DONE_STATE);
|
|
93
|
+
|
|
94
|
+
return [source.slice(0, start), frontMatterBlock(front.toString(YAML_OPTIONS)), source.slice(end)].join("");
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** The file with one more thread entry. Text that would open an entry of its own is refused. */
|
|
98
|
+
export const appendEntry = (source: string, entry: Omit<ThreadEntry, "text">, text: string): string => {
|
|
99
|
+
if (entryOpenings(parseMarkdown(text).children).length > 0) throw new PermanentError("an entry's text may not hold a line hum reads as the start of an entry");
|
|
100
|
+
|
|
101
|
+
return [source.trimEnd(), "", markerLine(EntryMarker, entry), text.trim(), ""].join("\n");
|
|
102
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { State } from "../state/open.ts";
|
|
2
|
+
import { SECOND_MS } from "../time.ts";
|
|
3
|
+
import { CARD_EXCERPT, type Card } from "../status/format.ts";
|
|
4
|
+
|
|
5
|
+
const LIMIT_KEY = "runner";
|
|
6
|
+
|
|
7
|
+
export const LIMIT_UNPARSED = "limit-unparsed";
|
|
8
|
+
|
|
9
|
+
/** Epoch ms until which the saved usage limit holds every run, or 0. */
|
|
10
|
+
export const savedLimitUntil = (state: Pick<State, "limit">): number => state.limit.get(LIMIT_KEY)?.until ?? 0;
|
|
11
|
+
|
|
12
|
+
export const LIMIT_TEXT = /usage limit|hit your (?:\w+ )?limit|limit reached/i;
|
|
13
|
+
|
|
14
|
+
const LIMIT_EPOCH = /limit reached\|(\d{9,13})/i;
|
|
15
|
+
|
|
16
|
+
const LIMIT_ISO = /resets?(?: at)? (\d{4}-\d\d-\d\dT[\d:.]+(?:Z|[+-]\d{2}:\d{2}))/i;
|
|
17
|
+
|
|
18
|
+
/** Epoch values below this are seconds, at or above it milliseconds. */
|
|
19
|
+
const FIRST_MS_EPOCH = 1e12;
|
|
20
|
+
|
|
21
|
+
export const epochMs = (value: number): number => (value < FIRST_MS_EPOCH ? value * SECOND_MS : value);
|
|
22
|
+
|
|
23
|
+
const isoMs = (text: string | undefined): number | undefined => {
|
|
24
|
+
const parsed = text === undefined ? Number.NaN : Date.parse(text);
|
|
25
|
+
|
|
26
|
+
return Number.isNaN(parsed) ? undefined : parsed;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The reset time in a limit message, as epoch ms. Undefined when the message holds none this parser knows. */
|
|
30
|
+
export const parseLimit = (text: string): number | undefined => {
|
|
31
|
+
const epoch = LIMIT_EPOCH.exec(text)?.[1];
|
|
32
|
+
|
|
33
|
+
return epoch === undefined ? isoMs(LIMIT_ISO.exec(text)?.[1]) : epochMs(Number(epoch));
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** A usage limit a run ended on: with its reset time, or with a message no parser here reads. */
|
|
37
|
+
export type LimitEnded = { readonly kind: "limited"; readonly until: number } | { readonly kind: "limit-unparsed"; readonly text: string };
|
|
38
|
+
|
|
39
|
+
/** The limit a refusal names: its structured reset time when it has one, else the time in its text. */
|
|
40
|
+
export const limitOf = (text: string, resetsAt: number | undefined): LimitEnded => {
|
|
41
|
+
const until = resetsAt ?? parseLimit(text);
|
|
42
|
+
|
|
43
|
+
return until === undefined ? { kind: "limit-unparsed", text } : { kind: "limited", until };
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type Held = { readonly kind: "held"; readonly card: Card };
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A run that ended on a usage limit, taken by the engine: the reset time is saved so no call is made before it ends,
|
|
50
|
+
* and a message with no readable reset time holds every run with one card.
|
|
51
|
+
*/
|
|
52
|
+
export const settleLimit = (state: Pick<State, "limit" | "faults">, ended: LimitEnded): { readonly kind: "limited"; readonly until: number } | Held => {
|
|
53
|
+
if (ended.kind === "limit-unparsed") {
|
|
54
|
+
return {
|
|
55
|
+
kind: "held",
|
|
56
|
+
card: state.faults.raise({
|
|
57
|
+
code: LIMIT_UNPARSED,
|
|
58
|
+
cause: `claude refused with a usage limit and no reset time hum can read: ${ended.text.slice(0, CARD_EXCERPT)}`,
|
|
59
|
+
fix: "Runs stay held. Tell hum's maintainer the message, or restart the worker once the limit has reset.",
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
state.limit.put(LIMIT_KEY, { until: Math.max(savedLimitUntil(state), ended.until) });
|
|
65
|
+
|
|
66
|
+
return ended;
|
|
67
|
+
};
|