@moo.md/hum 0.0.0 → 0.2.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/package.json +2 -2
- package/schema/hum.schema.json +2 -2
- package/scripts/eval.ts +88 -50
- package/src/adapter-api.ts +46 -14
- package/src/adapters/admit.ts +2 -2
- package/src/adapters/argument.ts +1 -1
- package/src/adapters/claim.ts +18 -15
- package/src/adapters/claude-bg.ts +72 -32
- package/src/adapters/claude-p.ts +26 -25
- package/src/adapters/contract.ts +22 -10
- package/src/adapters/each-call.ts +9 -0
- package/src/adapters/fake.ts +6 -6
- package/src/adapters/git.ts +12 -19
- package/src/adapters/github-client.ts +102 -0
- package/src/adapters/github.ts +50 -136
- package/src/adapters/guard.ts +20 -9
- package/src/adapters/history.ts +12 -15
- package/src/adapters/item-file.ts +1 -1
- package/src/adapters/markdown.ts +49 -42
- package/src/adapters/marked.ts +15 -13
- package/src/adapters/model-call.ts +4 -4
- package/src/adapters/once.ts +5 -5
- package/src/adapters/owner-commit.ts +13 -11
- package/src/adapters/pull-requests.ts +11 -13
- package/src/adapters/registry.ts +7 -7
- package/src/adapters/roundup.ts +31 -37
- package/src/adapters/serial.ts +4 -5
- package/src/adapters/transcript.ts +13 -8
- package/src/cli/main.ts +1 -0
- package/src/cli/ops.ts +32 -14
- package/src/cli/service.ts +14 -8
- package/src/cli/setup.ts +8 -3
- package/src/cli/shared-watch.ts +3 -3
- package/src/config/checks.ts +14 -18
- package/src/config/machine.ts +5 -7
- package/src/config/refusal.ts +7 -0
- package/src/config/schema.ts +9 -9
- package/src/config/settings.ts +17 -2
- package/src/config/state-map.ts +5 -3
- package/src/decide/alignment.ts +4 -4
- package/src/decide/answer.ts +24 -22
- package/src/decide/kind.ts +1 -6
- package/src/decide/labels.ts +9 -9
- package/src/decide/merge.ts +6 -7
- package/src/decide/prose.ts +4 -4
- package/src/decide/question.ts +23 -20
- package/src/decide/review.ts +4 -2
- package/src/decide/risk.ts +25 -19
- package/src/decide/taste.ts +3 -3
- package/src/exec.ts +10 -4
- package/src/index.ts +14 -13
- package/src/loop/check-item.ts +3 -3
- package/src/loop/ci.ts +1 -1
- package/src/loop/github.ts +2 -2
- package/src/loop/groups.ts +13 -6
- package/src/loop/memory.ts +40 -28
- package/src/loop/notes.ts +5 -5
- package/src/loop/pair.ts +1 -1
- package/src/loop/pass.ts +18 -12
- package/src/loop/proof.ts +9 -7
- package/src/loop/prove-item.ts +3 -3
- package/src/loop/records.ts +12 -13
- package/src/loop/signals.ts +6 -6
- package/src/measure/inputs.ts +1 -1
- package/src/measure/measure.ts +4 -4
- package/src/measure/rate.ts +2 -0
- package/src/service/ci-tick.ts +2 -2
- package/src/service/supervise.ts +3 -2
- package/src/service/token.ts +3 -3
- package/src/service/wake.ts +2 -2
- package/src/service/workflows.ts +2 -2
- package/src/setup/propose.ts +4 -4
- package/src/setup/prove.ts +20 -14
- package/src/setup/scan.ts +31 -24
- package/src/setup/shipped.ts +8 -2
- package/src/setup/tables.ts +4 -3
- package/src/setup/waits.ts +9 -11
- package/src/setup/write.ts +17 -10
- package/src/slots/pool.ts +32 -24
- package/src/state/events.ts +1 -1
- package/src/state/faults.ts +13 -5
- package/src/state/format.ts +2 -2
- package/src/state/items.ts +20 -15
- package/src/state/open.ts +2 -2
- package/src/state/ops.ts +16 -10
- package/src/state/records.ts +5 -5
- package/src/state/runs.ts +33 -26
- package/src/state/saved.ts +44 -32
- package/src/status/file.ts +20 -1
- package/src/status/format.ts +47 -57
- package/src/status/sections.ts +2 -2
- package/src/worker/alignment-report.ts +4 -5
- package/src/worker/answers.ts +26 -24
- package/src/worker/base-check.ts +4 -5
- package/src/worker/checks.ts +25 -11
- package/src/worker/kind.ts +23 -19
- package/src/worker/land.ts +115 -126
- package/src/worker/merged.ts +9 -7
- package/src/worker/open.ts +15 -13
- package/src/worker/prose-run.ts +3 -3
- package/src/worker/questions.ts +15 -12
- package/src/worker/scheduler.ts +4 -4
- package/src/worker/signature.ts +3 -3
- package/src/worker/tally.ts +0 -2
- package/src/worker/taste.ts +6 -15
- package/src/worker/thin-path.ts +150 -123
- package/src/worker/thread.ts +25 -0
- package/src/worker/worker.ts +125 -73
- package/surface.json +633 -2198
- package/src/slots/scan.ts +0 -26
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync, statSync } from "node:fs";
|
|
1
|
+
import { mkdirSync, readFileSync, rmSync, statSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { stripVTControlCharacters } from "node:util";
|
|
@@ -9,8 +9,9 @@ import { listRecords, orMissing } from "../state/fs.ts";
|
|
|
9
9
|
import type { State } from "../state/open.ts";
|
|
10
10
|
import { CARD_EXCERPT, type Card } from "../status/format.ts";
|
|
11
11
|
import { SECOND_MS } from "../time.ts";
|
|
12
|
-
import { type Finished, type RunOutcome, type RunRequest, type Runner, type RunnerOptions, START_ANSWER, START_PROMPT, START_SCHEMA, startRunner,
|
|
12
|
+
import { type Finished, type RunOutcome, type RunRequest, type Runner, type RunnerOptions, START_ANSWER, START_PROMPT, START_SCHEMA, startRunner, hasSucceeded } from "./claude-p.ts";
|
|
13
13
|
import { type LimitEnded, limitOf, savedLimitUntil, settleLimit } from "./limit.ts";
|
|
14
|
+
import { parseLooseJson } from "./marked.ts";
|
|
14
15
|
import { billedTokens, cacheReadTokens, modelsOf, outputTokens, type RunTranscript, readTranscript } from "./transcript.ts";
|
|
15
16
|
|
|
16
17
|
/** The plugin a background run turns off, so no run draws the band or polls. */
|
|
@@ -24,12 +25,21 @@ const SHORT_ID = /backgrounded\W+([0-9a-f]{8})\b/u;
|
|
|
24
25
|
|
|
25
26
|
const AGENT_VIEW_KIND = "background";
|
|
26
27
|
|
|
28
|
+
const AGENTS_UNREAD = "agents-unread";
|
|
29
|
+
|
|
27
30
|
const SINGLE_QUOTE = /'/g;
|
|
28
31
|
|
|
29
32
|
const shellQuote = (text: string): string => `'${text.replace(SINGLE_QUOTE, `'\\''`)}'`;
|
|
30
33
|
|
|
31
|
-
/**
|
|
32
|
-
|
|
34
|
+
/**
|
|
35
|
+
* A hook command that saves the hook's input, which names the session and its transcript, as a file of its own. It
|
|
36
|
+
* writes a temp file the folder's reader skips and renames it in, so a reader never meets half a report.
|
|
37
|
+
*/
|
|
38
|
+
const dropInto = (folder: string): string => {
|
|
39
|
+
const at = shellQuote(folder);
|
|
40
|
+
|
|
41
|
+
return `cat > ${at}/$$.part && mv ${at}/$$.part ${at}/$$.json`;
|
|
42
|
+
};
|
|
33
43
|
|
|
34
44
|
/** The settings every background run starts with: hum's plugin off, the deny rules and auto mode's rules, and a hook that reports the run. */
|
|
35
45
|
export const backgroundSettings = ({ deniedRunCommands, runAutoMode }: Pick<HumConfig, "deniedRunCommands" | "runAutoMode">, stopsFolder: string, env: Readonly<Record<string, string>> = {}) => ({
|
|
@@ -169,28 +179,44 @@ export type BackgroundOptions = Pick<RunnerOptions, "binary" | "env" | "now"> &
|
|
|
169
179
|
* transcript, enforces the token cap by `claude stop`, and stops and removes the run once its outcome is taken.
|
|
170
180
|
* A cheap model call, a run with no name, and a run the background service cannot take go to `fallback`.
|
|
171
181
|
*/
|
|
172
|
-
export const backgroundRunner = ({ binary = "claude", env, state, config, fallback, now
|
|
182
|
+
export const backgroundRunner = ({ binary = "claude", env, state, config, fallback, now }: BackgroundOptions): Runner => {
|
|
173
183
|
const pollMs = config.backgroundPollSeconds * SECOND_MS;
|
|
174
|
-
const jobsFolder = join(
|
|
184
|
+
const jobsFolder = join(env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude"), JOBS_FOLDER);
|
|
175
185
|
|
|
176
186
|
const claude = (args: readonly string[], options: { stdin?: string; cwd?: string } = {}) =>
|
|
177
|
-
exec(binary, args, { stdin: options.stdin, nodePath: false, timeoutMs: commandTimeoutMs(config), nodeOptions: { cwd: options.cwd, env
|
|
187
|
+
exec(binary, args, { stdin: options.stdin, nodePath: false, timeoutMs: commandTimeoutMs(config), nodeOptions: { cwd: options.cwd, env } });
|
|
178
188
|
|
|
189
|
+
/** The background runs Claude Code lists. A list hum cannot read throws, with one card: read as empty, it would start a run that is already going. */
|
|
179
190
|
const agents = async (): Promise<Agent[]> => {
|
|
180
191
|
const { stdout } = await claude(["agents", "--json"]);
|
|
181
|
-
const
|
|
192
|
+
const found = parseLooseJson(z.array(Listed), stdout);
|
|
193
|
+
|
|
194
|
+
if (found === undefined) {
|
|
195
|
+
const cause = `claude agents --json replied in a shape hum does not read: ${stdout.slice(0, CARD_EXCERPT)}`;
|
|
196
|
+
|
|
197
|
+
state.faults.raise({ code: AGENTS_UNREAD, cause, fix: "No background run starts until hum reads the list again; update hum to a version that reads this Claude Code's list." });
|
|
198
|
+
|
|
199
|
+
throw new Error(cause);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
state.faults.clear({ code: AGENTS_UNREAD });
|
|
182
203
|
|
|
183
|
-
return
|
|
204
|
+
return found.flatMap(({ kind, id, name, sessionId, state }) => (kind === AGENT_VIEW_KIND && id !== undefined ? [{ id, name, sessionId, state }] : []));
|
|
184
205
|
};
|
|
185
206
|
|
|
186
|
-
const reportsOf = (sessionId: string): { at: number; event: string; transcriptPath: string | undefined }[] =>
|
|
207
|
+
const reportsOf = (sessionId: string): { file: string; at: number; event: string; transcriptPath: string | undefined }[] =>
|
|
187
208
|
listRecords(state.stops).flatMap((file) => {
|
|
188
209
|
const reported = orMissing(() => Reported.safeParse(JSON.parse(readFileSync(file, "utf8"))), undefined);
|
|
189
210
|
|
|
190
|
-
return reported?.success && reported.data.session_id === sessionId ? [{ at: statSync(file).mtimeMs, event: reported.data.hook_event_name, transcriptPath: reported.data.transcript_path }] : [];
|
|
211
|
+
return reported?.success && reported.data.session_id === sessionId ? [{ file, at: statSync(file).mtimeMs, event: reported.data.hook_event_name, transcriptPath: reported.data.transcript_path }] : [];
|
|
191
212
|
});
|
|
192
213
|
|
|
193
|
-
|
|
214
|
+
/** Drops the session's hook reports, so the stops folder holds only runs still going. */
|
|
215
|
+
const forget = (sessionId: string): void => {
|
|
216
|
+
for (const { file } of reportsOf(sessionId)) rmSync(file, { force: true });
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const jobTokens = (id: string): number => orMissing(() => JobFile.parse(JSON.parse(readFileSync(join(jobsFolder, id, JOB_STATE_FILE), "utf8"))).tokens ?? 0, 0);
|
|
194
220
|
|
|
195
221
|
const readRun = async (id: string, sessionId: string): Promise<Reading> => {
|
|
196
222
|
// The job is read before the transcript: whatever put it in its state is already written.
|
|
@@ -232,9 +258,9 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
|
|
|
232
258
|
};
|
|
233
259
|
|
|
234
260
|
const listed = async (id: string, signal: AbortSignal | undefined): Promise<Agent | undefined> => {
|
|
235
|
-
const until =
|
|
261
|
+
const until = now() + config.backgroundLaunchSeconds * SECOND_MS;
|
|
236
262
|
|
|
237
|
-
while (
|
|
263
|
+
while (now() < until && !signal?.aborted) {
|
|
238
264
|
const found = (await agents()).find((agent) => agent.id === id);
|
|
239
265
|
|
|
240
266
|
if (found) return found;
|
|
@@ -245,6 +271,22 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
|
|
|
245
271
|
return undefined;
|
|
246
272
|
};
|
|
247
273
|
|
|
274
|
+
/** The outcome a reading ends the run with, or `waiting` while it goes on. */
|
|
275
|
+
const endOf = (progress: Progress, sessionId: string, reading: Reading): RunOutcome | "waiting" => {
|
|
276
|
+
switch (progress.kind) {
|
|
277
|
+
case "waiting":
|
|
278
|
+
return "waiting";
|
|
279
|
+
case "over-cap":
|
|
280
|
+
return progress;
|
|
281
|
+
case "limit":
|
|
282
|
+
return settleLimit(state, progress.ended);
|
|
283
|
+
case "done":
|
|
284
|
+
return finishedOf(sessionId, reading, succeededWith(progress.structured));
|
|
285
|
+
case "failed":
|
|
286
|
+
return finishedOf(sessionId, reading, failedWith(progress.why));
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
248
290
|
const watch = async (request: RunRequest, id: string, sessionId: string): Promise<RunOutcome> => {
|
|
249
291
|
let announced = false;
|
|
250
292
|
|
|
@@ -258,15 +300,9 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
|
|
|
258
300
|
request.onFirstToken?.();
|
|
259
301
|
}
|
|
260
302
|
|
|
261
|
-
const
|
|
303
|
+
const ended = endOf(progressOf(reading, request.tokenCap), sessionId, reading);
|
|
262
304
|
|
|
263
|
-
if (
|
|
264
|
-
|
|
265
|
-
if (progress.kind === "limit") return settleLimit(state, progress.ended);
|
|
266
|
-
|
|
267
|
-
if (progress.kind === "done") return finishedOf(sessionId, reading, succeededWith(progress.structured));
|
|
268
|
-
|
|
269
|
-
if (progress.kind === "failed") return finishedOf(sessionId, reading, failedWith(progress.why));
|
|
305
|
+
if (ended !== "waiting") return ended;
|
|
270
306
|
|
|
271
307
|
await Bun.sleep(pollMs);
|
|
272
308
|
}
|
|
@@ -290,7 +326,7 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
|
|
|
290
326
|
|
|
291
327
|
if (session === undefined) return request.signal?.aborted ? { kind: "stopped" } : fellBack(request, "the run never appeared in claude agents");
|
|
292
328
|
|
|
293
|
-
state.faults.clear(FALLBACK_CODE);
|
|
329
|
+
state.faults.clear({ code: FALLBACK_CODE });
|
|
294
330
|
|
|
295
331
|
request.onSession?.(session.sessionId);
|
|
296
332
|
|
|
@@ -298,6 +334,7 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
|
|
|
298
334
|
return await watch(request, id, session.sessionId);
|
|
299
335
|
} finally {
|
|
300
336
|
await end(id);
|
|
337
|
+
forget(session.sessionId);
|
|
301
338
|
}
|
|
302
339
|
};
|
|
303
340
|
|
|
@@ -320,7 +357,7 @@ export type BackgroundStartOptions = Omit<RunnerOptions, "state"> &
|
|
|
320
357
|
readonly repo: string;
|
|
321
358
|
};
|
|
322
359
|
|
|
323
|
-
const isOk = (outcome: RunOutcome): boolean => outcome.kind === "finished" &&
|
|
360
|
+
const isOk = (outcome: RunOutcome): boolean => outcome.kind === "finished" && hasSucceeded(outcome) && START_ANSWER.safeParse(outcome.structured).success;
|
|
324
361
|
|
|
325
362
|
const startCheckFault = (repo: string, why: string) => ({
|
|
326
363
|
code: BACKGROUND_CHECK_CODE,
|
|
@@ -336,9 +373,11 @@ const refusalCard = ({ faults }: Pick<BackgroundOptions, "state">["state"]): Car
|
|
|
336
373
|
return card;
|
|
337
374
|
};
|
|
338
375
|
|
|
339
|
-
const whyNot = (outcome: RunOutcome): string => {
|
|
376
|
+
const whyNot = (outcome: RunOutcome, timeoutMs: number): string => {
|
|
340
377
|
if (outcome.kind === "finished") return outcome.text.slice(0, CARD_EXCERPT);
|
|
341
378
|
|
|
379
|
+
if (outcome.kind === "stopped") return `no result within ${timeoutMs / SECOND_MS} seconds`;
|
|
380
|
+
|
|
342
381
|
return outcome.kind === "held" ? outcome.card.cause : outcome.kind;
|
|
343
382
|
};
|
|
344
383
|
|
|
@@ -349,26 +388,27 @@ const whyNot = (outcome: RunOutcome): string => {
|
|
|
349
388
|
* missing from agent view.
|
|
350
389
|
*/
|
|
351
390
|
export const startBackgroundRunner = async (options: BackgroundStartOptions): Promise<StartedBackground> => {
|
|
352
|
-
const { config, repo, state, binary, env } = options;
|
|
391
|
+
const { config, repo, state, binary, env, now } = options;
|
|
353
392
|
const started = await startRunner(options);
|
|
354
393
|
|
|
355
394
|
if (started.check.kind !== "finished") return started;
|
|
356
395
|
|
|
357
396
|
// The check's own fall-back holds with the card that says why the service refused.
|
|
358
397
|
const refuse: Runner = { run: async () => ({ kind: "held", card: refusalCard(state) }), limitedUntil: started.runner.limitedUntil };
|
|
359
|
-
const checker = backgroundRunner({ binary, env, state, config, fallback: refuse });
|
|
360
|
-
const
|
|
398
|
+
const checker = backgroundRunner({ binary, env, state, config, fallback: refuse, now });
|
|
399
|
+
const timeoutMs = commandTimeoutMs(config);
|
|
400
|
+
const check = await checker.run({ name: START_NAME, prompt: START_PROMPT, cwd: repo, model: options.model, effort: "low", schema: START_SCHEMA, signal: AbortSignal.timeout(timeoutMs) });
|
|
361
401
|
|
|
362
402
|
if (isOk(check)) {
|
|
363
|
-
state.faults.clear(BACKGROUND_CHECK_CODE);
|
|
403
|
+
state.faults.clear({ code: BACKGROUND_CHECK_CODE });
|
|
364
404
|
|
|
365
|
-
return { check, runner: backgroundRunner({ binary, env, state, config, fallback: started.runner }) };
|
|
405
|
+
return { check, runner: backgroundRunner({ binary, env, state, config, fallback: started.runner, now }) };
|
|
366
406
|
}
|
|
367
407
|
|
|
368
408
|
// The start check's card says why for every run, so the card for the check's own fall-back would only repeat it.
|
|
369
|
-
const why = whyNot(check);
|
|
409
|
+
const why = whyNot(check, timeoutMs);
|
|
370
410
|
|
|
371
|
-
state.faults.clear(FALLBACK_CODE);
|
|
411
|
+
state.faults.clear({ code: FALLBACK_CODE });
|
|
372
412
|
state.faults.raise(startCheckFault(repo, why));
|
|
373
413
|
|
|
374
414
|
return { check, runner: started.runner };
|
package/src/adapters/claude-p.ts
CHANGED
|
@@ -8,14 +8,14 @@ import semver from "semver";
|
|
|
8
8
|
import { x } from "tinyexec";
|
|
9
9
|
import { z } from "zod";
|
|
10
10
|
import type { EFFORTS } from "../config/schema.ts";
|
|
11
|
-
import { CommandTimedOut, exec, SHIPPED_COMMAND_TIMEOUT_MS } from "../exec.ts";
|
|
12
|
-
import {
|
|
11
|
+
import { CommandTimedOut, type Env, exec, SHIPPED_COMMAND_TIMEOUT_MS } from "../exec.ts";
|
|
12
|
+
import { raiseRejectedToken, UNAUTHORIZED } from "../service/token.ts";
|
|
13
13
|
import { atomicWrite, isMissing } from "../state/fs.ts";
|
|
14
14
|
import type { State } from "../state/open.ts";
|
|
15
15
|
import { SECOND_MS } from "../time.ts";
|
|
16
16
|
import { CARD_EXCERPT, type Card } from "../status/format.ts";
|
|
17
17
|
import { LIMIT_TEXT, LIMIT_UNPARSED, type LimitEnded, limitOf, savedLimitUntil, settleLimit } from "./limit.ts";
|
|
18
|
-
import { billed } from "./transcript.ts";
|
|
18
|
+
import { billed, Counts } from "./transcript.ts";
|
|
19
19
|
|
|
20
20
|
export type Effort = (typeof EFFORTS)[number];
|
|
21
21
|
|
|
@@ -33,7 +33,7 @@ export const RUN_SETTINGS = JSON.stringify({
|
|
|
33
33
|
* no settings source (plugins, MCP servers, memory files) and no built-in tool: a question needs neither, and each
|
|
34
34
|
* one bills on every call.
|
|
35
35
|
*/
|
|
36
|
-
|
|
36
|
+
const CALLS = {
|
|
37
37
|
run: { settings: RUN_SETTINGS, flags: [] },
|
|
38
38
|
judgment: {
|
|
39
39
|
settings: JSON.stringify({ permissions: { defaultMode: "bypassPermissions" }, disableAllHooks: true, outputStyle: "default" }),
|
|
@@ -41,7 +41,7 @@ export const CALLS = {
|
|
|
41
41
|
},
|
|
42
42
|
} as const satisfies Record<string, { settings: string; flags: readonly string[] }>;
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
type Call = keyof typeof CALLS;
|
|
45
45
|
|
|
46
46
|
const REQUIRED_PLUGIN = "hope";
|
|
47
47
|
|
|
@@ -88,11 +88,7 @@ export const runArgs = ({ model, effort, schema, sessionId, call = "run", name }
|
|
|
88
88
|
...(name === undefined ? [] : ["-n", name]),
|
|
89
89
|
];
|
|
90
90
|
|
|
91
|
-
const Usage = z.looseObject(
|
|
92
|
-
input_tokens: z.number().default(0),
|
|
93
|
-
output_tokens: z.number().default(0),
|
|
94
|
-
cache_creation_input_tokens: z.number().default(0),
|
|
95
|
-
});
|
|
91
|
+
const Usage = z.looseObject(Counts);
|
|
96
92
|
|
|
97
93
|
const ModelUsage = z.looseObject({
|
|
98
94
|
inputTokens: z.number().default(0),
|
|
@@ -229,7 +225,7 @@ export type RunOutcome =
|
|
|
229
225
|
/** What one process ended as, before the runner acts on a limit it could not read. */
|
|
230
226
|
type Ended = RunOutcome | LimitEnded;
|
|
231
227
|
|
|
232
|
-
export const
|
|
228
|
+
export const hasSucceeded = (outcome: Finished): boolean => outcome.subtype === "success" && !outcome.isError;
|
|
233
229
|
|
|
234
230
|
const LOCK_TEXT = /refresh[^\n]*lock|lock[^\n]*refresh/i;
|
|
235
231
|
|
|
@@ -320,11 +316,11 @@ export type RunnerOptions = {
|
|
|
320
316
|
/** The model the start check runs on. */
|
|
321
317
|
readonly model: string;
|
|
322
318
|
readonly binary?: string;
|
|
323
|
-
/** The limit of `claude --version`, which the version floor reads; the owner's `commandTimeoutSeconds` in milliseconds. */
|
|
319
|
+
/** The limit of `claude --version`, which the version floor reads, and of the start check; the owner's `commandTimeoutSeconds` in milliseconds. */
|
|
324
320
|
readonly timeoutMs?: number;
|
|
325
|
-
/**
|
|
326
|
-
readonly env
|
|
327
|
-
readonly now
|
|
321
|
+
/** The environment `claude` runs with, the process's own from the root. An API key in it is passed on untouched. */
|
|
322
|
+
readonly env: Env;
|
|
323
|
+
readonly now: () => number;
|
|
328
324
|
/** The first wait before a run that hit the refresh-lock error runs again; later waits grow from it. */
|
|
329
325
|
readonly lockBackoffMs?: number;
|
|
330
326
|
};
|
|
@@ -368,7 +364,7 @@ const recordCredential = (state: RunnerOptions["state"], credential: string): vo
|
|
|
368
364
|
return;
|
|
369
365
|
}
|
|
370
366
|
|
|
371
|
-
if (recorded === credential) return state.faults.clear("credential-changed");
|
|
367
|
+
if (recorded === credential) return state.faults.clear({ code: "credential-changed" });
|
|
372
368
|
|
|
373
369
|
state.faults.raise({
|
|
374
370
|
code: "credential-changed",
|
|
@@ -377,6 +373,8 @@ const recordCredential = (state: RunnerOptions["state"], credential: string): vo
|
|
|
377
373
|
});
|
|
378
374
|
};
|
|
379
375
|
|
|
376
|
+
const START_TIMED_OUT = "start-check-timed-out";
|
|
377
|
+
|
|
380
378
|
export const START_PROMPT = 'Reply with the JSON {"ok":true}.';
|
|
381
379
|
|
|
382
380
|
export const START_SCHEMA = {
|
|
@@ -400,8 +398,7 @@ type Started = { readonly check: RunOutcome; readonly runner: Runner };
|
|
|
400
398
|
* No run starts until the version floor and the start check pass; until then every run is held with one card.
|
|
401
399
|
*/
|
|
402
400
|
const startChecked = async (options: RunnerOptions): Promise<Started> => {
|
|
403
|
-
const { state, binary = "claude",
|
|
404
|
-
const env = { ...process.env, ...options.env };
|
|
401
|
+
const { state, binary = "claude", env, now, lockBackoffMs = LOCK_BACKOFF_MS, timeoutMs = SHIPPED_COMMAND_TIMEOUT_MS } = options;
|
|
405
402
|
const limitedUntil = () => savedLimitUntil(state);
|
|
406
403
|
|
|
407
404
|
const hold = (code: string, cause: string, fix: string): Extract<RunOutcome, { kind: "held" }> => ({
|
|
@@ -505,9 +502,9 @@ const startChecked = async (options: RunnerOptions): Promise<Started> => {
|
|
|
505
502
|
|
|
506
503
|
if (outcome.kind !== "finished") return outcome;
|
|
507
504
|
|
|
508
|
-
if (isUnauthorized(outcome)) return { kind: "held", card:
|
|
505
|
+
if (isUnauthorized(outcome)) return { kind: "held", card: raiseRejectedToken(state.faults, outcome.text) };
|
|
509
506
|
|
|
510
|
-
state.faults.clear("token-rejected");
|
|
507
|
+
state.faults.clear({ code: "token-rejected" });
|
|
511
508
|
|
|
512
509
|
return outcome;
|
|
513
510
|
};
|
|
@@ -535,8 +532,11 @@ const startChecked = async (options: RunnerOptions): Promise<Started> => {
|
|
|
535
532
|
model: options.model,
|
|
536
533
|
effort: "low",
|
|
537
534
|
schema: START_SCHEMA,
|
|
535
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
538
536
|
});
|
|
539
537
|
|
|
538
|
+
if (check.kind === "stopped") return held(hold(START_TIMED_OUT, `The start check got no answer within ${timeoutMs / SECOND_MS} seconds and was stopped.`, "hum makes the check again on the next run; if this card stays, check that claude -p answers in a terminal."));
|
|
539
|
+
|
|
540
540
|
if (check.kind === "held") return held(check);
|
|
541
541
|
|
|
542
542
|
if (check.kind === "limited") return { check, runner: { run: async () => check, limitedUntil } };
|
|
@@ -545,7 +545,7 @@ const startChecked = async (options: RunnerOptions): Promise<Started> => {
|
|
|
545
545
|
|
|
546
546
|
const hasPlugin = check.plugins.includes(REQUIRED_PLUGIN);
|
|
547
547
|
|
|
548
|
-
if (!
|
|
548
|
+
if (!hasSucceeded(check) || !hasPlugin) {
|
|
549
549
|
return held(
|
|
550
550
|
hold(
|
|
551
551
|
"start-check-failed",
|
|
@@ -557,20 +557,21 @@ const startChecked = async (options: RunnerOptions): Promise<Started> => {
|
|
|
557
557
|
|
|
558
558
|
recordCredential(state, credentialOf(check.keySource, env));
|
|
559
559
|
|
|
560
|
-
state.faults.clear(LIMIT_UNPARSED);
|
|
560
|
+
state.faults.clear({ code: LIMIT_UNPARSED });
|
|
561
|
+
state.faults.clear({ code: START_TIMED_OUT });
|
|
561
562
|
|
|
562
563
|
return { check, runner: { run, limitedUntil } };
|
|
563
564
|
};
|
|
564
565
|
|
|
565
|
-
/** A start check that a usage limit refused says nothing about the install: it is made again
|
|
566
|
-
const isUnsettled = ({ check }: Started): boolean => check.kind === "limited" || (check.kind === "held" && check.card.code === LIMIT_UNPARSED);
|
|
566
|
+
/** A start check that a usage limit refused or that got no answer says nothing about the install: it is made again on the next run. */
|
|
567
|
+
const isUnsettled = ({ check }: Started): boolean => check.kind === "limited" || (check.kind === "held" && (check.card.code === LIMIT_UNPARSED || check.card.code === START_TIMED_OUT));
|
|
567
568
|
|
|
568
569
|
/**
|
|
569
570
|
* A runner. Its start check is made again on the next run for as long as a usage limit refuses it, and a saved
|
|
570
571
|
* limit that still stands defers the check, so no call is made before the limit ends and no restart is needed.
|
|
571
572
|
*/
|
|
572
573
|
export const startRunner = async (options: RunnerOptions): Promise<Started> => {
|
|
573
|
-
const { state, now
|
|
574
|
+
const { state, now } = options;
|
|
574
575
|
const started = pMemoize(() => startChecked(options));
|
|
575
576
|
|
|
576
577
|
const settle = async (): Promise<Started> => {
|
package/src/adapters/contract.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
|
-
import { type Adapter, type Answers, ClaimResult, Item, ItemRef, type Questions, type Release, Watch, type WorkSource } from "../adapter-api.ts";
|
|
2
|
+
import { type Adapter, type Answers, ClaimResult, Failed, type Fallible, Holder, Item, ItemId, ItemRef, type Questions, type Release, Watch, type WorkSource } from "../adapter-api.ts";
|
|
3
3
|
import type { ClaimMode } from "./claim.ts";
|
|
4
|
+
import { eachCall } from "./each-call.ts";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* What the contract suite needs from one provider. Reads hit the provider; writes go to a scratch
|
|
@@ -12,7 +13,7 @@ export type ContractTarget = {
|
|
|
12
13
|
/** A second worker on the same scratch target, for races. Defaults to `adapter`. */
|
|
13
14
|
readonly other?: () => Promise<Adapter>;
|
|
14
15
|
/** Puts a ready item on the scratch target and returns its id. */
|
|
15
|
-
readonly seed: (title: string) => Promise<
|
|
16
|
+
readonly seed: (title: string) => Promise<ItemId>;
|
|
16
17
|
/** Moves the provider's clock forward, so a claim expires. */
|
|
17
18
|
readonly advance: (ms: number) => Promise<void>;
|
|
18
19
|
/** Requests so far that spent rate limit. */
|
|
@@ -31,14 +32,25 @@ const need = <T>(value: T | undefined, what: string): T => {
|
|
|
31
32
|
return value;
|
|
32
33
|
};
|
|
33
34
|
|
|
35
|
+
/** `integration` with a `Failed` reply turned into a failed check, in the adapter's own words. */
|
|
36
|
+
const answering = <I extends object>(integration: Fallible<I>): I =>
|
|
37
|
+
eachCall<Fallible<I>, I>(integration, (call, name) => async (...args) => {
|
|
38
|
+
const reply = await call(...args);
|
|
39
|
+
const failed = Failed.safeParse(reply);
|
|
40
|
+
|
|
41
|
+
assert.ok(!failed.success, `${name} answered failed ${failed.data?.failed}: ${failed.data?.why}`);
|
|
42
|
+
|
|
43
|
+
return reply;
|
|
44
|
+
});
|
|
45
|
+
|
|
34
46
|
const workSourceChecks = (target: ContractTarget, source: WorkSource): Check[] => {
|
|
35
47
|
const second = async () => (target.other ? target.other() : target.adapter);
|
|
36
|
-
const readItem = async (id:
|
|
48
|
+
const readItem = async (id: ItemId) => Item.parse(await source.read(id));
|
|
37
49
|
const ready = async () => ItemRef.array().parse(await source.listReady());
|
|
38
|
-
const isListed = async (id:
|
|
50
|
+
const isListed = async (id: ItemId) => (await ready()).some((ref) => ref.id === id);
|
|
39
51
|
|
|
40
|
-
const claimOf = async (who: Adapter, id:
|
|
41
|
-
ClaimResult.parse(await need(who.workSource, "no work source")[mode](id, holder, TTL));
|
|
52
|
+
const claimOf = async (who: Adapter, id: ItemId, holder: string, mode: ClaimMode = "claim") =>
|
|
53
|
+
ClaimResult.parse(await answering<WorkSource>(need(who.workSource, "no work source"))[mode](id, Holder.parse(holder), TTL));
|
|
42
54
|
|
|
43
55
|
return [
|
|
44
56
|
{
|
|
@@ -199,13 +211,13 @@ const releaseChecks = (target: ContractTarget, release: Release): Check[] => [
|
|
|
199
211
|
/** The checks every adapter runs for the integrations it provides. */
|
|
200
212
|
export const contractChecks = (target: ContractTarget): Check[] => {
|
|
201
213
|
const { questions, answers, release } = target.adapter;
|
|
202
|
-
const source = need(target.adapter.workSource, `${target.name} provides no work source`);
|
|
214
|
+
const source = answering<WorkSource>(need(target.adapter.workSource, `${target.name} provides no work source`));
|
|
203
215
|
|
|
204
216
|
return [
|
|
205
217
|
...workSourceChecks(target, source),
|
|
206
|
-
...(questions ? questionChecks(target, source, questions) : []),
|
|
207
|
-
...(answers ? answerChecks(target, source, answers) : []),
|
|
208
|
-
...(release ? releaseChecks(target, release) : []),
|
|
218
|
+
...(questions ? questionChecks(target, source, answering<Questions>(questions)) : []),
|
|
219
|
+
...(answers ? answerChecks(target, source, answering<Answers>(answers)) : []),
|
|
220
|
+
...(release ? releaseChecks(target, answering<Release>(release)) : []),
|
|
209
221
|
];
|
|
210
222
|
};
|
|
211
223
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type Wrap = <A extends unknown[], R>(call: (...args: A) => Promise<R>, name: string) => (...args: A) => Promise<R>;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `role` with each of its calls passed through `wrap`, and its other members kept.
|
|
5
|
+
* SAFETY: every member of an integration role that is a function is an async method, and `wrap` keeps its
|
|
6
|
+
* parameters; the caller names the role type the wrapped calls answer as.
|
|
7
|
+
*/
|
|
8
|
+
export const eachCall = <R extends object, W extends object>(role: R, wrap: Wrap): W =>
|
|
9
|
+
Object.fromEntries(Object.entries(role).map(([name, member]) => [name, member instanceof Function ? wrap(async (...args: unknown[]) => member.apply(role, args), name) : member])) as W;
|
package/src/adapters/fake.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { ADAPTER_API_VERSION, type Claim, type Integrations, ItemId, type ItemRef, PermanentError, type ThreadEntry, type Watch } from "../adapter-api.ts";
|
|
2
2
|
import { claimer, isLive } from "./claim.ts";
|
|
3
3
|
import { questionText } from "./marked.ts";
|
|
4
4
|
|
|
5
|
-
type Stored = { id:
|
|
5
|
+
type Stored = { id: ItemId; title: string; body: string; ready: boolean; thread: ThreadEntry[]; claim?: Claim; from?: string };
|
|
6
6
|
|
|
7
7
|
/** A provider failure a retry can outlast, like a GitHub 502. */
|
|
8
8
|
export class ProviderError extends Error {
|
|
@@ -48,8 +48,8 @@ export class FakeProvider {
|
|
|
48
48
|
this.#emptyList = value;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
seed(title: string, { ready = true }: { ready?: boolean } = {}):
|
|
52
|
-
const id = `item-${this.#items.size + 1}
|
|
51
|
+
seed(title: string, { ready = true }: { ready?: boolean } = {}): ItemId {
|
|
52
|
+
const id = ItemId.parse(`item-${this.#items.size + 1}`);
|
|
53
53
|
|
|
54
54
|
this.#items.set(id, { id, title, body: `${title} body`, ready, thread: [] });
|
|
55
55
|
this.#version++;
|
|
@@ -107,12 +107,12 @@ export class FakeProvider {
|
|
|
107
107
|
this.now,
|
|
108
108
|
);
|
|
109
109
|
|
|
110
|
-
adapter():
|
|
110
|
+
adapter(): Integrations {
|
|
111
111
|
const asRef = ({ id, title }: Stored): ItemRef => ({ id, title });
|
|
112
112
|
const spend = <T>(body: () => T) => this.#call(true, body);
|
|
113
113
|
|
|
114
114
|
return {
|
|
115
|
-
apiVersion:
|
|
115
|
+
apiVersion: ADAPTER_API_VERSION,
|
|
116
116
|
workSource: {
|
|
117
117
|
listReady: async () =>
|
|
118
118
|
spend(() => this.listReplacement?.() ?? (this.#emptyList ? [] : [...this.#items.values()].flatMap((item) => (this.#isReady(item) ? [asRef(item)] : [])))),
|
package/src/adapters/git.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { PermanentError } from "../adapter-api.ts";
|
|
2
1
|
import { exec } from "../exec.ts";
|
|
3
2
|
|
|
4
3
|
/** No hook runs: the hooks path points nowhere, whatever the clone's own config says. */
|
|
5
4
|
const HOOKS_OFF = "core.hooksPath=/dev/null";
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
/** The words git uses when another push moved the ref first; git 2.50 and later say `incorrect old value provided` when two pushes race at the update. */
|
|
7
|
+
const CONFLICT = /non-fast-forward|fetch first|already exists|stale info|failed to update ref|cannot lock ref|incorrect old value/i;
|
|
8
8
|
const REFUSED = /remote rejected|declined|protected|GH006|denied|not permitted/i;
|
|
9
9
|
const ABSENT = /does not exist|not a valid object name/i;
|
|
10
10
|
|
|
@@ -14,7 +14,7 @@ export const PATCH = ["--no-ext-diff", "--no-color"] as const;
|
|
|
14
14
|
const ANCESTOR = 0;
|
|
15
15
|
const NOT_ANCESTOR = 1;
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
type GitResult = { readonly code: number; readonly stdout: string; readonly stderr: string };
|
|
18
18
|
|
|
19
19
|
export type Git = {
|
|
20
20
|
/** Runs git; a non-zero exit is a result, not a throw, but a run past its time limit throws `CommandTimedOut`. `input` is its stdin, which is closed either way. */
|
|
@@ -23,7 +23,7 @@ export type Git = {
|
|
|
23
23
|
readonly raw: (args: readonly string[], input?: string) => Promise<string>;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
class GitFailed extends Error {
|
|
27
27
|
override name = "GitFailed";
|
|
28
28
|
|
|
29
29
|
constructor(
|
|
@@ -43,12 +43,8 @@ export const whenGitFailed =
|
|
|
43
43
|
throw error;
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
/**
|
|
47
|
-
export
|
|
48
|
-
override name = "PushRefused";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type PushOutcome = "pushed" | "conflict";
|
|
46
|
+
/** `conflict`: another writer moved the ref first, and a retry can win. `refused`: the remote said no, in `said`, and no retry helps. */
|
|
47
|
+
export type PushOutcome = { readonly kind: "pushed" } | { readonly kind: "conflict" } | { readonly kind: "refused"; readonly said: string };
|
|
52
48
|
|
|
53
49
|
/** The author and committer of the commits hum makes. */
|
|
54
50
|
export type GitIdentity = { readonly name: string; readonly email: string };
|
|
@@ -94,7 +90,7 @@ export const gitIn = (cwd: string, { identity, env = {}, timeoutMs }: GitOptions
|
|
|
94
90
|
const run = async (args: readonly string[], input = ""): Promise<GitResult> => {
|
|
95
91
|
const { exitCode, stdout, stderr } = await exec("git", ["-c", HOOKS_OFF, ...args], {
|
|
96
92
|
timeoutMs,
|
|
97
|
-
nodeOptions: { cwd, env: { ...
|
|
93
|
+
nodeOptions: { cwd, env: { ...(identity && identityEnv(identity)), ...env } },
|
|
98
94
|
stdin: input,
|
|
99
95
|
});
|
|
100
96
|
|
|
@@ -115,26 +111,23 @@ export const gitIn = (cwd: string, { identity, env = {}, timeoutMs }: GitOptions
|
|
|
115
111
|
/** The arguments for `git push` with hooks off, for a command line handed straight to git. */
|
|
116
112
|
export const pushArgs = (rest: readonly string[]): string[] => ["-c", HOOKS_OFF, "push", "--no-verify", ...rest];
|
|
117
113
|
|
|
118
|
-
/**
|
|
119
|
-
* Pushes. `conflict` means another writer moved the ref first and a retry can win.
|
|
120
|
-
* A refusal throws `PushRefused` with the remote's own words; any other failure throws `GitFailed`.
|
|
121
|
-
*/
|
|
114
|
+
/** Pushes. A refusal is an outcome that carries the remote's own words; any other failure throws `GitFailed`. */
|
|
122
115
|
export const push = async (git: Git, rest: readonly string[]): Promise<PushOutcome> => {
|
|
123
116
|
const args = ["push", "--no-verify", ...rest];
|
|
124
117
|
const result = await git.run(args);
|
|
125
118
|
const said = `${result.stderr}${result.stdout}`.trim();
|
|
126
119
|
|
|
127
|
-
if (result.code === 0) return "pushed";
|
|
120
|
+
if (result.code === 0) return { kind: "pushed" };
|
|
128
121
|
|
|
129
|
-
if (CONFLICT.test(said)) return "conflict";
|
|
122
|
+
if (CONFLICT.test(said)) return { kind: "conflict" };
|
|
130
123
|
|
|
131
|
-
if (REFUSED.test(said))
|
|
124
|
+
if (REFUSED.test(said)) return { kind: "refused", said: `git push refused: ${said}` };
|
|
132
125
|
|
|
133
126
|
throw new GitFailed(args, result);
|
|
134
127
|
};
|
|
135
128
|
|
|
136
129
|
/** Whether `ancestor` is reachable from `descendant`. git answers by exit code alone. */
|
|
137
|
-
export const isAncestor = async (git: Git, ancestor: string
|
|
130
|
+
export const isAncestor = async (git: Git, { ancestor, descendant }: { readonly ancestor: string; readonly descendant: string }): Promise<boolean> => {
|
|
138
131
|
const args = ["merge-base", "--is-ancestor", ancestor, descendant];
|
|
139
132
|
const result = await git.run(args);
|
|
140
133
|
|