@moo.md/hum 0.1.0 → 0.3.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 (112) hide show
  1. package/defaults/hum.json +9 -2
  2. package/package.json +2 -2
  3. package/schema/hum.schema.json +208 -7
  4. package/scripts/eval.ts +88 -50
  5. package/src/adapter-api.ts +46 -14
  6. package/src/adapters/admit.ts +2 -2
  7. package/src/adapters/argument.ts +1 -1
  8. package/src/adapters/claim.ts +18 -15
  9. package/src/adapters/claude-bg.ts +50 -27
  10. package/src/adapters/claude-p.ts +18 -23
  11. package/src/adapters/contract.ts +22 -10
  12. package/src/adapters/each-call.ts +9 -0
  13. package/src/adapters/fake.ts +6 -6
  14. package/src/adapters/git.ts +10 -18
  15. package/src/adapters/github-client.ts +102 -0
  16. package/src/adapters/github.ts +61 -146
  17. package/src/adapters/guard.ts +20 -9
  18. package/src/adapters/history.ts +12 -15
  19. package/src/adapters/item-file.ts +1 -1
  20. package/src/adapters/markdown.ts +49 -42
  21. package/src/adapters/marked.ts +6 -12
  22. package/src/adapters/model-call.ts +4 -4
  23. package/src/adapters/once.ts +5 -5
  24. package/src/adapters/owner-commit.ts +13 -11
  25. package/src/adapters/pull-requests.ts +11 -13
  26. package/src/adapters/registry.ts +9 -8
  27. package/src/adapters/roundup.ts +27 -35
  28. package/src/adapters/serial.ts +4 -5
  29. package/src/adapters/transcript.ts +13 -8
  30. package/src/cli/main.ts +1 -0
  31. package/src/cli/ops.ts +32 -14
  32. package/src/cli/service.ts +14 -8
  33. package/src/cli/setup.ts +22 -7
  34. package/src/cli/shared-watch.ts +3 -3
  35. package/src/config/checks.ts +27 -18
  36. package/src/config/machine.ts +5 -7
  37. package/src/config/migrate.ts +5 -2
  38. package/src/config/refusal.ts +15 -2
  39. package/src/config/schema.ts +74 -12
  40. package/src/config/settings.ts +17 -2
  41. package/src/config/state-map.ts +5 -3
  42. package/src/config/wake-job.ts +74 -0
  43. package/src/decide/alignment.ts +4 -4
  44. package/src/decide/answer.ts +24 -22
  45. package/src/decide/kind.ts +1 -6
  46. package/src/decide/labels.ts +9 -9
  47. package/src/decide/merge.ts +6 -7
  48. package/src/decide/prose.ts +4 -4
  49. package/src/decide/question.ts +23 -20
  50. package/src/decide/review.ts +4 -2
  51. package/src/decide/risk.ts +25 -19
  52. package/src/decide/taste.ts +3 -3
  53. package/src/exec.ts +4 -3
  54. package/src/index.ts +14 -13
  55. package/src/loop/check-item.ts +3 -3
  56. package/src/loop/ci.ts +1 -1
  57. package/src/loop/github.ts +2 -2
  58. package/src/loop/groups.ts +13 -6
  59. package/src/loop/memory.ts +40 -28
  60. package/src/loop/notes.ts +5 -5
  61. package/src/loop/pair.ts +1 -1
  62. package/src/loop/pass.ts +18 -12
  63. package/src/loop/proof.ts +9 -7
  64. package/src/loop/prove-item.ts +1 -1
  65. package/src/loop/records.ts +12 -13
  66. package/src/loop/signals.ts +6 -6
  67. package/src/measure/inputs.ts +1 -1
  68. package/src/measure/measure.ts +4 -4
  69. package/src/measure/rate.ts +2 -0
  70. package/src/service/ci-tick.ts +2 -2
  71. package/src/service/supervise.ts +3 -2
  72. package/src/service/token.ts +3 -3
  73. package/src/service/wake.ts +2 -2
  74. package/src/service/workflows.ts +165 -48
  75. package/src/setup/propose.ts +78 -7
  76. package/src/setup/prove.ts +18 -12
  77. package/src/setup/scan.ts +91 -28
  78. package/src/setup/shipped.ts +8 -2
  79. package/src/setup/tables.ts +22 -3
  80. package/src/setup/waits.ts +9 -11
  81. package/src/setup/write.ts +67 -27
  82. package/src/slots/pool.ts +32 -24
  83. package/src/state/events.ts +1 -1
  84. package/src/state/faults.ts +13 -5
  85. package/src/state/format.ts +2 -2
  86. package/src/state/items.ts +9 -10
  87. package/src/state/open.ts +2 -2
  88. package/src/state/ops.ts +16 -10
  89. package/src/state/records.ts +5 -5
  90. package/src/state/runs.ts +33 -26
  91. package/src/state/saved.ts +44 -32
  92. package/src/status/file.ts +20 -1
  93. package/src/status/format.ts +47 -57
  94. package/src/status/sections.ts +2 -2
  95. package/src/worker/alignment-report.ts +4 -5
  96. package/src/worker/answers.ts +26 -24
  97. package/src/worker/base-check.ts +2 -2
  98. package/src/worker/kind.ts +23 -19
  99. package/src/worker/land.ts +114 -126
  100. package/src/worker/merged.ts +9 -7
  101. package/src/worker/open.ts +14 -12
  102. package/src/worker/prose-run.ts +3 -3
  103. package/src/worker/questions.ts +15 -12
  104. package/src/worker/scheduler.ts +4 -4
  105. package/src/worker/signature.ts +3 -3
  106. package/src/worker/tally.ts +0 -2
  107. package/src/worker/taste.ts +6 -15
  108. package/src/worker/thin-path.ts +148 -121
  109. package/src/worker/thread.ts +25 -0
  110. package/src/worker/worker.ts +77 -47
  111. package/surface.json +1091 -2251
  112. package/src/slots/scan.ts +0 -26
@@ -1,12 +1,13 @@
1
1
  import { z } from "zod";
2
+ import type { Env } from "./exec.ts";
2
3
  import type { FaultInput } from "./state/faults.ts";
3
4
 
4
5
  export type { FaultInput };
5
6
 
6
7
  /** Bumped on every change to the API an adapter implements; a breaking change bumps the engine's major version. */
7
- export const ADAPTER_API_VERSION = 1;
8
+ export const ADAPTER_API_VERSION = 2;
8
9
 
9
- /** Failure that retrying cannot fix: a reply of the wrong shape, a refused push. The engine surfaces it at once. */
10
+ /** Failure that retrying cannot fix: a reply of the wrong shape, a refused push. The engine surfaces it at once, as it does a `failed: "now"` answer. */
10
11
  export class PermanentError extends Error {
11
12
  override name: string = "PermanentError";
12
13
  }
@@ -21,10 +22,19 @@ export const ItemId = z
21
22
  .string()
22
23
  .max(ITEM_ID_LENGTH, ITEM_ID_MESSAGE)
23
24
  .regex(/^[A-Za-z0-9][A-Za-z0-9._-]*$/, ITEM_ID_MESSAGE)
24
- .refine((id) => !id.includes("..") && !id.endsWith(".lock"), "an item id has no '..' and does not end in '.lock'");
25
+ .refine((id) => !id.includes("..") && !id.endsWith(".lock"), "an item id has no '..' and does not end in '.lock'")
26
+ .brand<"ItemId">();
25
27
 
26
28
  export type ItemId = z.infer<typeof ItemId>;
27
29
 
30
+ /** Who holds a claim: a plain name, never an item id. */
31
+ export const Holder = z
32
+ .string()
33
+ .regex(/^[A-Za-z0-9][\w.@:-]{0,99}$/, "a holder is a plain name: letters, digits and . _ @ : -")
34
+ .brand<"Holder">();
35
+
36
+ export type Holder = z.infer<typeof Holder>;
37
+
28
38
  const text = z.string().min(1);
29
39
  const time = z.int().nonnegative();
30
40
 
@@ -60,7 +70,7 @@ export const Item = ItemRef.extend({
60
70
 
61
71
  export type Item = z.infer<typeof Item>;
62
72
 
63
- export const Claim = z.strictObject({ item: ItemId, holder: text, expiresAt: time });
73
+ export const Claim = z.strictObject({ item: ItemId, holder: Holder, expiresAt: time });
64
74
 
65
75
  export type Claim = z.infer<typeof Claim>;
66
76
 
@@ -84,39 +94,59 @@ export type FollowUp = { readonly title: string; readonly body: string };
84
94
  /** The work source: the queue, the claims and the item threads. */
85
95
  export interface WorkSource {
86
96
  listReady(): Promise<ItemRef[]>;
87
- read(id: string): Promise<Item>;
97
+ read(id: ItemId): Promise<Item>;
88
98
  /** An atomic take. An unclaimed or expired item goes to the first caller; the same holder renews. */
89
- claim(id: string, holder: string, ttlMs: number): Promise<ClaimResult>;
99
+ claim(id: ItemId, holder: Holder, ttlMs: number): Promise<ClaimResult>;
90
100
  /** Extends the holder's own claim; `won: false` when another worker holds it now. */
91
- renew(id: string, holder: string, ttlMs: number): Promise<ClaimResult>;
92
- comment(id: string, text: string): Promise<void>;
101
+ renew(id: ItemId, holder: Holder, ttlMs: number): Promise<ClaimResult>;
102
+ comment(id: ItemId, text: string): Promise<void>;
93
103
  /** Files a ready item. Filing the same `title` from the same `from` twice returns the first item. */
94
- fileFollowUp(from: string, followUp: FollowUp): Promise<ItemRef>;
104
+ fileFollowUp(from: ItemId, followUp: FollowUp): Promise<ItemRef>;
95
105
  }
96
106
 
97
107
  /** Where a run's questions and the choices made without the owner land. */
98
108
  export interface Questions {
99
- ask(id: string, question: Question): Promise<void>;
109
+ ask(id: ItemId, question: Question): Promise<void>;
100
110
  /** Records a choice made without the owner. */
101
- decide(id: string, text: string): Promise<void>;
111
+ decide(id: ItemId, text: string): Promise<void>;
102
112
  }
103
113
 
104
114
  /** Where the owner's answers arrive. */
105
115
  export interface Answers {
106
- answer(id: string, answer: Answer): Promise<void>;
116
+ answer(id: ItemId, answer: Answer): Promise<void>;
107
117
  /** The one cheap read: has anything changed since `since`. An idle call costs no rate limit. */
108
118
  watch(since?: string): Promise<Watch>;
109
119
  }
110
120
 
111
121
  export interface Release {
112
122
  /** Puts the item's artifact where reviewers see it. */
113
- publish(id: string, ref: string): Promise<void>;
123
+ publish(id: ItemId, ref: string): Promise<void>;
114
124
  /** Ships a published artifact. */
115
- release(id: string, ref: string): Promise<void>;
125
+ release(id: ItemId, ref: string): Promise<void>;
116
126
  }
117
127
 
128
+ /**
129
+ * A call an adapter could not complete, answered in place of its value. `retry`: a later attempt may work, and the
130
+ * engine tries again within the provider timeout. `now`: no attempt will, and the engine raises one card and stops.
131
+ */
132
+ export const Failed = z.strictObject({ failed: z.enum(["retry", "now"]), why: text });
133
+
134
+ export type Failed = z.infer<typeof Failed>;
135
+
136
+ /** An integration as an adapter implements it: every call may answer `Failed` in place of its value. */
137
+ export type Fallible<I> = { readonly [K in keyof I]: I[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => Promise<R | Failed> : I[K] };
138
+
118
139
  /** An adapter declares the API version it implements and the integrations it provides. */
119
140
  export type Adapter = {
141
+ readonly apiVersion: number;
142
+ readonly workSource?: Fallible<WorkSource>;
143
+ readonly questions?: Fallible<Questions>;
144
+ readonly answers?: Fallible<Answers>;
145
+ readonly release?: Fallible<Release>;
146
+ };
147
+
148
+ /** The integrations with plain replies: the engine calls them so behind the guard, which retries or throws on `Failed`, and an adapter that never answers `Failed` may implement them so. */
149
+ export type Integrations = {
120
150
  readonly apiVersion: number;
121
151
  readonly workSource?: WorkSource;
122
152
  readonly questions?: Questions;
@@ -128,6 +158,8 @@ export type AdapterContext = {
128
158
  /** Raise a fault card, scoped to one item or provider. */
129
159
  readonly report: (fault: FaultInput) => void;
130
160
  readonly now: () => number;
161
+ /** The process environment, for the tokens and addresses an adapter reads. */
162
+ readonly env: Env;
131
163
  };
132
164
 
133
165
  /** The default export of an owner's `adapters/<name>.ts`. */
@@ -1,6 +1,6 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
2
  import { extname, relative } from "node:path";
3
- import type { Adapter } from "../adapter-api.ts";
3
+ import type { Adapter, Integrations } from "../adapter-api.ts";
4
4
  import type { Faults } from "../state/faults.ts";
5
5
  import type { Card } from "../status/format.ts";
6
6
  import { type GuardOptions, guarded } from "./guard.ts";
@@ -47,7 +47,7 @@ export const engineImporters = (files: readonly string[]): { file: string; modul
47
47
  export const sourceFiles = (dir: string): string[] =>
48
48
  existsSync(dir) ? [...SOURCE_GLOB.scanSync({ cwd: dir, absolute: true })].filter(isOutsideDependencies).sort() : [];
49
49
 
50
- export type Admission = { readonly ok: true; readonly adapter: Adapter } | { readonly ok: false; readonly cards: Card[] };
50
+ export type Admission = { readonly ok: true; readonly adapter: Integrations } | { readonly ok: false; readonly cards: Card[] };
51
51
 
52
52
  export type AdmitInput = {
53
53
  readonly name: string;
@@ -2,7 +2,7 @@ import { z } from "zod";
2
2
  import { PermanentError } from "../adapter-api.ts";
3
3
 
4
4
  /** A caller's argument, parsed. One that fails is the caller's mistake, which no retry fixes. */
5
- export const parseArgument = <Out, In>(schema: z.ZodType<Out, In>, value: In): Out => {
5
+ export const parseArgument = <S extends z.ZodType>(schema: S, value: z.input<S>): z.output<S> => {
6
6
  const parsed = schema.safeParse(value);
7
7
 
8
8
  if (!parsed.success) throw new PermanentError(z.prettifyError(parsed.error));
@@ -1,23 +1,21 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { z } from "zod";
3
- import { type Claim, type ClaimResult, PermanentError } from "../adapter-api.ts";
3
+ import { type Claim, type ClaimResult, Holder, type ItemId, PermanentError } from "../adapter-api.ts";
4
4
  import { parseArgument } from "./argument.ts";
5
5
 
6
- const Holder = z.string().regex(/^[A-Za-z0-9][\w.@:-]{0,99}$/, "a holder is a plain name: letters, digits and . _ @ : -");
7
-
8
6
  const Ttl = z.int().positive("a claim lasts a positive whole number of milliseconds");
9
7
 
10
8
  const CLAIM_SUBJECT = /^hum-claim (\S+) (\d+)$/;
11
9
 
12
10
  export type ClaimMode = "claim" | "renew";
13
11
 
14
- /** A provider's claims. `S` is the standing claim with whatever the provider needs to write over it. */
15
- export type ClaimStore<S extends { readonly claim: Claim }> = {
16
- readonly standing: (id: string) => Promise<S | undefined>;
12
+ /** A provider's claims. `S` is the standing claim with whatever the provider needs to write over it; `F` is what its write answers when it fails. */
13
+ export type ClaimStore<S extends { readonly claim: Claim }, F = never> = {
14
+ readonly standing: (id: ItemId) => Promise<S | undefined>;
17
15
  /** Asked only before a first claim: a standing one already proves the item was taken while ready. */
18
- readonly isReady: (id: string) => Promise<boolean>;
19
- /** Writes `claim` over `over`; false when another writer moved first. */
20
- readonly write: (claim: Claim, over: S | undefined) => Promise<boolean>;
16
+ readonly isReady: (id: ItemId) => Promise<boolean>;
17
+ /** Writes `claim` over `over`; false when another writer moved first, `F` when the write failed. */
18
+ readonly write: (claim: Claim, over: S | undefined) => Promise<boolean | NoInfer<F>>;
21
19
  };
22
20
 
23
21
  export type ClaimDecision =
@@ -34,7 +32,7 @@ const isHeldByAnother = (standing: Claim, holder: string, now: number): boolean
34
32
  * Who may write a claim, given the one standing. An unclaimed or expired item goes to whoever writes first;
35
33
  * the holder claiming again renews; a renewal by anyone else loses to the standing claim.
36
34
  */
37
- export const decideClaim = (mode: ClaimMode, holder: string, standing: Claim | undefined, now: number): ClaimDecision => {
35
+ const decideClaim = (mode: ClaimMode, holder: string, standing: Claim | undefined, now: number): ClaimDecision => {
38
36
  if (standing && isHeldByAnother(standing, holder, now)) return { kind: "held", claim: standing };
39
37
 
40
38
  if (mode === "renew" && standing?.holder !== holder) return standing ? { kind: "held", claim: standing } : { kind: "unheld" };
@@ -44,8 +42,8 @@ export const decideClaim = (mode: ClaimMode, holder: string, standing: Claim | u
44
42
 
45
43
  /** `claim` and `renew` over one provider's store: an atomic take whose race the provider's write settles. */
46
44
  export const claimer =
47
- <S extends { readonly claim: Claim }>(store: ClaimStore<S>, now: () => number) =>
48
- async (mode: ClaimMode, item: string, holder: string, ttlMs: number): Promise<ClaimResult> => {
45
+ <S extends { readonly claim: Claim }, F = never>(store: ClaimStore<S, F>, now: () => number) =>
46
+ async (mode: ClaimMode, item: ItemId, holder: Holder, ttlMs: number): Promise<ClaimResult | F> => {
49
47
  const who = parseArgument(Holder, holder);
50
48
  const ttl = parseArgument(Ttl, ttlMs);
51
49
  const standing = await store.standing(item);
@@ -61,7 +59,11 @@ export const claimer =
61
59
 
62
60
  const claim: Claim = { item, holder: who, expiresAt: now() + ttl };
63
61
 
64
- if (await store.write(claim, standing)) return { won: true, claim };
62
+ const written = await store.write(claim, standing);
63
+
64
+ if (written === true) return { won: true, claim };
65
+
66
+ if (written !== false) return written;
65
67
 
66
68
  const winner = await store.standing(item);
67
69
 
@@ -85,9 +87,10 @@ export const claimCommitMessage = (claim: Claim): string => {
85
87
  };
86
88
 
87
89
  /** The claim a commit message holds, or undefined for any other commit. */
88
- export const claimOfCommit = (item: string, message: string): Claim | undefined => {
90
+ export const claimOfCommit = (item: ItemId, message: string): Claim | undefined => {
89
91
  const [subject = ""] = message.split("\n");
90
92
  const match = CLAIM_SUBJECT.exec(subject);
93
+ const holder = Holder.safeParse(match?.[1]);
91
94
 
92
- return match?.[1] && match[2] ? { item, holder: match[1], expiresAt: Number(match[2]) } : undefined;
95
+ return holder.success && match?.[2] ? { item, holder: holder.data, expiresAt: Number(match[2]) } : undefined;
93
96
  };
@@ -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,7 +9,7 @@ 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, succeeded } from "./claude-p.ts";
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
14
  import { parseLooseJson } from "./marked.ts";
15
15
  import { billedTokens, cacheReadTokens, modelsOf, outputTokens, type RunTranscript, readTranscript } from "./transcript.ts";
@@ -31,8 +31,15 @@ const SINGLE_QUOTE = /'/g;
31
31
 
32
32
  const shellQuote = (text: string): string => `'${text.replace(SINGLE_QUOTE, `'\\''`)}'`;
33
33
 
34
- /** A hook command that saves the hook's input, which names the session and its transcript, as a file of its own. */
35
- const dropInto = (folder: string): string => `cat > ${shellQuote(folder)}/$$.json`;
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
+ };
36
43
 
37
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. */
38
45
  export const backgroundSettings = ({ deniedRunCommands, runAutoMode }: Pick<HumConfig, "deniedRunCommands" | "runAutoMode">, stopsFolder: string, env: Readonly<Record<string, string>> = {}) => ({
@@ -172,12 +179,12 @@ export type BackgroundOptions = Pick<RunnerOptions, "binary" | "env" | "now"> &
172
179
  * transcript, enforces the token cap by `claude stop`, and stops and removes the run once its outcome is taken.
173
180
  * A cheap model call, a run with no name, and a run the background service cannot take go to `fallback`.
174
181
  */
175
- export const backgroundRunner = ({ binary = "claude", env, state, config, fallback, now = Date.now }: BackgroundOptions): Runner => {
182
+ export const backgroundRunner = ({ binary = "claude", env, state, config, fallback, now }: BackgroundOptions): Runner => {
176
183
  const pollMs = config.backgroundPollSeconds * SECOND_MS;
177
- const jobsFolder = join({ ...process.env, ...env }.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude"), JOBS_FOLDER);
184
+ const jobsFolder = join(env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude"), JOBS_FOLDER);
178
185
 
179
186
  const claude = (args: readonly string[], options: { stdin?: string; cwd?: string } = {}) =>
180
- exec(binary, args, { stdin: options.stdin, nodePath: false, timeoutMs: commandTimeoutMs(config), nodeOptions: { cwd: options.cwd, env: { ...process.env, ...env } } });
187
+ exec(binary, args, { stdin: options.stdin, nodePath: false, timeoutMs: commandTimeoutMs(config), nodeOptions: { cwd: options.cwd, env } });
181
188
 
182
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. */
183
190
  const agents = async (): Promise<Agent[]> => {
@@ -192,18 +199,23 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
192
199
  throw new Error(cause);
193
200
  }
194
201
 
195
- state.faults.clear(AGENTS_UNREAD);
202
+ state.faults.clear({ code: AGENTS_UNREAD });
196
203
 
197
204
  return found.flatMap(({ kind, id, name, sessionId, state }) => (kind === AGENT_VIEW_KIND && id !== undefined ? [{ id, name, sessionId, state }] : []));
198
205
  };
199
206
 
200
- 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 }[] =>
201
208
  listRecords(state.stops).flatMap((file) => {
202
209
  const reported = orMissing(() => Reported.safeParse(JSON.parse(readFileSync(file, "utf8"))), undefined);
203
210
 
204
- 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 }] : [];
205
212
  });
206
213
 
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
+
207
219
  const jobTokens = (id: string): number => orMissing(() => JobFile.parse(JSON.parse(readFileSync(join(jobsFolder, id, JOB_STATE_FILE), "utf8"))).tokens ?? 0, 0);
208
220
 
209
221
  const readRun = async (id: string, sessionId: string): Promise<Reading> => {
@@ -246,9 +258,9 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
246
258
  };
247
259
 
248
260
  const listed = async (id: string, signal: AbortSignal | undefined): Promise<Agent | undefined> => {
249
- const until = Date.now() + config.backgroundLaunchSeconds * SECOND_MS;
261
+ const until = now() + config.backgroundLaunchSeconds * SECOND_MS;
250
262
 
251
- while (Date.now() < until && !signal?.aborted) {
263
+ while (now() < until && !signal?.aborted) {
252
264
  const found = (await agents()).find((agent) => agent.id === id);
253
265
 
254
266
  if (found) return found;
@@ -259,6 +271,22 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
259
271
  return undefined;
260
272
  };
261
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
+
262
290
  const watch = async (request: RunRequest, id: string, sessionId: string): Promise<RunOutcome> => {
263
291
  let announced = false;
264
292
 
@@ -272,15 +300,9 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
272
300
  request.onFirstToken?.();
273
301
  }
274
302
 
275
- const progress = progressOf(reading, request.tokenCap);
276
-
277
- if (progress.kind === "over-cap") return progress;
278
-
279
- if (progress.kind === "limit") return settleLimit(state, progress.ended);
280
-
281
- if (progress.kind === "done") return finishedOf(sessionId, reading, succeededWith(progress.structured));
303
+ const ended = endOf(progressOf(reading, request.tokenCap), sessionId, reading);
282
304
 
283
- if (progress.kind === "failed") return finishedOf(sessionId, reading, failedWith(progress.why));
305
+ if (ended !== "waiting") return ended;
284
306
 
285
307
  await Bun.sleep(pollMs);
286
308
  }
@@ -304,7 +326,7 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
304
326
 
305
327
  if (session === undefined) return request.signal?.aborted ? { kind: "stopped" } : fellBack(request, "the run never appeared in claude agents");
306
328
 
307
- state.faults.clear(FALLBACK_CODE);
329
+ state.faults.clear({ code: FALLBACK_CODE });
308
330
 
309
331
  request.onSession?.(session.sessionId);
310
332
 
@@ -312,6 +334,7 @@ export const backgroundRunner = ({ binary = "claude", env, state, config, fallba
312
334
  return await watch(request, id, session.sessionId);
313
335
  } finally {
314
336
  await end(id);
337
+ forget(session.sessionId);
315
338
  }
316
339
  };
317
340
 
@@ -334,7 +357,7 @@ export type BackgroundStartOptions = Omit<RunnerOptions, "state"> &
334
357
  readonly repo: string;
335
358
  };
336
359
 
337
- const isOk = (outcome: RunOutcome): boolean => outcome.kind === "finished" && succeeded(outcome) && START_ANSWER.safeParse(outcome.structured).success;
360
+ const isOk = (outcome: RunOutcome): boolean => outcome.kind === "finished" && hasSucceeded(outcome) && START_ANSWER.safeParse(outcome.structured).success;
338
361
 
339
362
  const startCheckFault = (repo: string, why: string) => ({
340
363
  code: BACKGROUND_CHECK_CODE,
@@ -365,27 +388,27 @@ const whyNot = (outcome: RunOutcome, timeoutMs: number): string => {
365
388
  * missing from agent view.
366
389
  */
367
390
  export const startBackgroundRunner = async (options: BackgroundStartOptions): Promise<StartedBackground> => {
368
- const { config, repo, state, binary, env } = options;
391
+ const { config, repo, state, binary, env, now } = options;
369
392
  const started = await startRunner(options);
370
393
 
371
394
  if (started.check.kind !== "finished") return started;
372
395
 
373
396
  // The check's own fall-back holds with the card that says why the service refused.
374
397
  const refuse: Runner = { run: async () => ({ kind: "held", card: refusalCard(state) }), limitedUntil: started.runner.limitedUntil };
375
- const checker = backgroundRunner({ binary, env, state, config, fallback: refuse });
398
+ const checker = backgroundRunner({ binary, env, state, config, fallback: refuse, now });
376
399
  const timeoutMs = commandTimeoutMs(config);
377
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) });
378
401
 
379
402
  if (isOk(check)) {
380
- state.faults.clear(BACKGROUND_CHECK_CODE);
403
+ state.faults.clear({ code: BACKGROUND_CHECK_CODE });
381
404
 
382
- return { check, runner: backgroundRunner({ binary, env, state, config, fallback: started.runner }) };
405
+ return { check, runner: backgroundRunner({ binary, env, state, config, fallback: started.runner, now }) };
383
406
  }
384
407
 
385
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.
386
409
  const why = whyNot(check, timeoutMs);
387
410
 
388
- state.faults.clear(FALLBACK_CODE);
411
+ state.faults.clear({ code: FALLBACK_CODE });
389
412
  state.faults.raise(startCheckFault(repo, why));
390
413
 
391
414
  return { check, runner: started.runner };
@@ -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 { rejectedToken, UNAUTHORIZED } from "../service/token.ts";
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
- export const CALLS = {
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
- export type Call = keyof typeof CALLS;
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 succeeded = (outcome: Finished): boolean => outcome.subtype === "success" && !outcome.isError;
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
 
@@ -322,9 +318,9 @@ export type RunnerOptions = {
322
318
  readonly binary?: string;
323
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
- /** Added to the process environment. An API key in the environment is passed on untouched. */
326
- readonly env?: Readonly<Record<string, string>>;
327
- readonly now?: () => number;
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",
@@ -402,8 +398,7 @@ type Started = { readonly check: RunOutcome; readonly runner: Runner };
402
398
  * No run starts until the version floor and the start check pass; until then every run is held with one card.
403
399
  */
404
400
  const startChecked = async (options: RunnerOptions): Promise<Started> => {
405
- const { state, binary = "claude", now = Date.now, lockBackoffMs = LOCK_BACKOFF_MS, timeoutMs = SHIPPED_COMMAND_TIMEOUT_MS } = options;
406
- const env = { ...process.env, ...options.env };
401
+ const { state, binary = "claude", env, now, lockBackoffMs = LOCK_BACKOFF_MS, timeoutMs = SHIPPED_COMMAND_TIMEOUT_MS } = options;
407
402
  const limitedUntil = () => savedLimitUntil(state);
408
403
 
409
404
  const hold = (code: string, cause: string, fix: string): Extract<RunOutcome, { kind: "held" }> => ({
@@ -507,9 +502,9 @@ const startChecked = async (options: RunnerOptions): Promise<Started> => {
507
502
 
508
503
  if (outcome.kind !== "finished") return outcome;
509
504
 
510
- if (isUnauthorized(outcome)) return { kind: "held", card: rejectedToken(state.faults, outcome.text) };
505
+ if (isUnauthorized(outcome)) return { kind: "held", card: raiseRejectedToken(state.faults, outcome.text) };
511
506
 
512
- state.faults.clear("token-rejected");
507
+ state.faults.clear({ code: "token-rejected" });
513
508
 
514
509
  return outcome;
515
510
  };
@@ -550,7 +545,7 @@ const startChecked = async (options: RunnerOptions): Promise<Started> => {
550
545
 
551
546
  const hasPlugin = check.plugins.includes(REQUIRED_PLUGIN);
552
547
 
553
- if (!succeeded(check) || !hasPlugin) {
548
+ if (!hasSucceeded(check) || !hasPlugin) {
554
549
  return held(
555
550
  hold(
556
551
  "start-check-failed",
@@ -562,8 +557,8 @@ const startChecked = async (options: RunnerOptions): Promise<Started> => {
562
557
 
563
558
  recordCredential(state, credentialOf(check.keySource, env));
564
559
 
565
- state.faults.clear(LIMIT_UNPARSED);
566
- state.faults.clear(START_TIMED_OUT);
560
+ state.faults.clear({ code: LIMIT_UNPARSED });
561
+ state.faults.clear({ code: START_TIMED_OUT });
567
562
 
568
563
  return { check, runner: { run, limitedUntil } };
569
564
  };
@@ -576,7 +571,7 @@ const isUnsettled = ({ check }: Started): boolean => check.kind === "limited" ||
576
571
  * limit that still stands defers the check, so no call is made before the limit ends and no restart is needed.
577
572
  */
578
573
  export const startRunner = async (options: RunnerOptions): Promise<Started> => {
579
- const { state, now = Date.now } = options;
574
+ const { state, now } = options;
580
575
  const started = pMemoize(() => startChecked(options));
581
576
 
582
577
  const settle = async (): Promise<Started> => {
@@ -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<string>;
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: string) => Item.parse(await source.read(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: string) => (await ready()).some((ref) => ref.id === id);
50
+ const isListed = async (id: ItemId) => (await ready()).some((ref) => ref.id === id);
39
51
 
40
- const claimOf = async (who: Adapter, id: string, holder: string, mode: ClaimMode = "claim") =>
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;