@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,231 @@
|
|
|
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";
|
|
3
|
+
import type { ClaimMode } from "./claim.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What the contract suite needs from one provider. Reads hit the provider; writes go to a scratch
|
|
7
|
+
* target the provider owns: a temp repo, a scratch Project, an in-memory fake.
|
|
8
|
+
*/
|
|
9
|
+
export type ContractTarget = {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly adapter: Adapter;
|
|
12
|
+
/** A second worker on the same scratch target, for races. Defaults to `adapter`. */
|
|
13
|
+
readonly other?: () => Promise<Adapter>;
|
|
14
|
+
/** Puts a ready item on the scratch target and returns its id. */
|
|
15
|
+
readonly seed: (title: string) => Promise<string>;
|
|
16
|
+
/** Moves the provider's clock forward, so a claim expires. */
|
|
17
|
+
readonly advance: (ms: number) => Promise<void>;
|
|
18
|
+
/** Requests so far that spent rate limit. */
|
|
19
|
+
readonly spent: () => number;
|
|
20
|
+
/** Polls `until` while the provider catches up with a write; a provider whose reads lag its writes sets it. It throws if `until` never holds. */
|
|
21
|
+
readonly settle?: (until: () => Promise<boolean>) => Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type Check = { readonly name: string; readonly run: () => Promise<void> };
|
|
25
|
+
|
|
26
|
+
const TTL = 60_000;
|
|
27
|
+
|
|
28
|
+
const need = <T>(value: T | undefined, what: string): T => {
|
|
29
|
+
assert.ok(value !== undefined, what);
|
|
30
|
+
|
|
31
|
+
return value;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const workSourceChecks = (target: ContractTarget, source: WorkSource): Check[] => {
|
|
35
|
+
const second = async () => (target.other ? target.other() : target.adapter);
|
|
36
|
+
const readItem = async (id: string) => Item.parse(await source.read(id));
|
|
37
|
+
const ready = async () => ItemRef.array().parse(await source.listReady());
|
|
38
|
+
const isListed = async (id: string) => (await ready()).some((ref) => ref.id === id);
|
|
39
|
+
|
|
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));
|
|
42
|
+
|
|
43
|
+
return [
|
|
44
|
+
{
|
|
45
|
+
name: "work source: a seeded item is listed ready and reads back",
|
|
46
|
+
run: async () => {
|
|
47
|
+
const id = await target.seed("seeded one");
|
|
48
|
+
|
|
49
|
+
assert.ok((await ready()).some((ref) => ref.id === id && ref.title === "seeded one"));
|
|
50
|
+
|
|
51
|
+
const item = await readItem(id);
|
|
52
|
+
|
|
53
|
+
assert.equal(item.title, "seeded one");
|
|
54
|
+
assert.equal(item.ready, true);
|
|
55
|
+
assert.deepEqual(item.thread, []);
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "work source: a comment lands on the thread",
|
|
60
|
+
run: async () => {
|
|
61
|
+
const id = await target.seed("commented");
|
|
62
|
+
|
|
63
|
+
await source.comment(id, "first words");
|
|
64
|
+
|
|
65
|
+
const { thread } = await readItem(id);
|
|
66
|
+
|
|
67
|
+
assert.equal(thread.length, 1);
|
|
68
|
+
assert.equal(thread[0]?.kind, "comment");
|
|
69
|
+
assert.equal(thread[0]?.text, "first words");
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: "work source: two claims race and one wins",
|
|
74
|
+
run: async () => {
|
|
75
|
+
const id = await target.seed("raced");
|
|
76
|
+
const results = await Promise.all([claimOf(target.adapter, id, "worker-a"), claimOf(await second(), id, "worker-b")]);
|
|
77
|
+
const winners = results.filter((result) => result.won);
|
|
78
|
+
|
|
79
|
+
assert.equal(winners.length, 1, "exactly one claim wins");
|
|
80
|
+
|
|
81
|
+
for (const result of results) assert.equal(result.claim.holder, winners[0]?.claim.holder, "both calls name the winner");
|
|
82
|
+
|
|
83
|
+
assert.ok(!(await isListed(id)), "a claimed item is no longer listed");
|
|
84
|
+
assert.equal((await readItem(id)).ready, false);
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
name: "work source: the holder renews, another worker cannot",
|
|
89
|
+
run: async () => {
|
|
90
|
+
const id = await target.seed("renewed");
|
|
91
|
+
|
|
92
|
+
assert.equal((await claimOf(target.adapter, id, "worker-a")).won, true);
|
|
93
|
+
assert.equal((await claimOf(target.adapter, id, "worker-a")).won, true, "the holder claiming again renews");
|
|
94
|
+
assert.equal((await claimOf(target.adapter, id, "worker-a", "renew")).won, true);
|
|
95
|
+
assert.equal((await claimOf(target.adapter, id, "worker-b", "renew")).won, false);
|
|
96
|
+
assert.equal((await claimOf(target.adapter, id, "worker-b")).won, false);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: "work source: an expired claim is taken once",
|
|
101
|
+
run: async () => {
|
|
102
|
+
const id = await target.seed("expired");
|
|
103
|
+
const b = await second();
|
|
104
|
+
|
|
105
|
+
assert.equal((await claimOf(target.adapter, id, "worker-a")).won, true);
|
|
106
|
+
await target.advance(TTL + 1);
|
|
107
|
+
assert.ok(await isListed(id), "an expired claim lists the item again");
|
|
108
|
+
|
|
109
|
+
const results = await Promise.all([claimOf(target.adapter, id, "worker-b"), claimOf(b, id, "worker-c")]);
|
|
110
|
+
|
|
111
|
+
assert.equal(results.filter((result) => result.won).length, 1, "the expired claim is taken once");
|
|
112
|
+
assert.equal((await claimOf(target.adapter, id, "worker-a", "renew")).won, false, "the old holder lost it");
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "work source: a follow-up is listed ready, and filing it twice files it once",
|
|
117
|
+
run: async () => {
|
|
118
|
+
const from = await target.seed("parent");
|
|
119
|
+
const first = ItemRef.parse(await source.fileFollowUp(from, { title: "Follow up A", body: "Do A." }));
|
|
120
|
+
const again = ItemRef.parse(await source.fileFollowUp(from, { title: "Follow up A", body: "Do A." }));
|
|
121
|
+
|
|
122
|
+
await target.settle?.(() => isListed(first.id));
|
|
123
|
+
|
|
124
|
+
assert.equal(again.id, first.id);
|
|
125
|
+
assert.equal((await ready()).filter((ref) => ref.id === first.id).length, 1);
|
|
126
|
+
assert.equal((await readItem(first.id)).ready, true);
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
];
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const questionChecks = (target: ContractTarget, source: WorkSource, questions: Questions): Check[] => [
|
|
133
|
+
{
|
|
134
|
+
name: "questions: a question and a decision land on the thread",
|
|
135
|
+
run: async () => {
|
|
136
|
+
const id = await target.seed("asked");
|
|
137
|
+
|
|
138
|
+
await questions.ask(id, { text: "Which way?", options: ["left", "right"] });
|
|
139
|
+
await questions.decide(id, "went left");
|
|
140
|
+
|
|
141
|
+
assert.deepEqual(
|
|
142
|
+
Item.parse(await source.read(id)).thread.map((entry) => entry.kind),
|
|
143
|
+
["question", "decision"],
|
|
144
|
+
);
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
];
|
|
148
|
+
|
|
149
|
+
const answerChecks = (target: ContractTarget, source: WorkSource, answers: Answers): Check[] => [
|
|
150
|
+
{
|
|
151
|
+
name: "answers: an answer lands on the thread with its option",
|
|
152
|
+
run: async () => {
|
|
153
|
+
const id = await target.seed("answered");
|
|
154
|
+
|
|
155
|
+
await answers.answer(id, { text: "go ahead", option: "approve" });
|
|
156
|
+
|
|
157
|
+
const [entry] = Item.parse(await source.read(id)).thread;
|
|
158
|
+
|
|
159
|
+
assert.equal(entry?.kind, "answer");
|
|
160
|
+
assert.equal(entry?.text, "go ahead");
|
|
161
|
+
assert.equal(entry?.option, "approve");
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
name: "answers: an idle watch costs nothing and a write flips it",
|
|
166
|
+
run: async () => {
|
|
167
|
+
const id = await target.seed("watched");
|
|
168
|
+
const first = Watch.parse(await answers.watch());
|
|
169
|
+
const spent = target.spent();
|
|
170
|
+
const idle = Watch.parse(await answers.watch(first.token));
|
|
171
|
+
|
|
172
|
+
assert.equal(idle.changed, false, "nothing wrote, so nothing changed");
|
|
173
|
+
assert.equal(idle.token, first.token);
|
|
174
|
+
assert.equal(target.spent(), spent, "an idle watch spends no rate limit");
|
|
175
|
+
|
|
176
|
+
await answers.answer(id, { text: "flip" });
|
|
177
|
+
await target.settle?.(async () => (await answers.watch(first.token)).changed);
|
|
178
|
+
|
|
179
|
+
const flipped = Watch.parse(await answers.watch(first.token));
|
|
180
|
+
|
|
181
|
+
assert.equal(flipped.changed, true);
|
|
182
|
+
assert.notEqual(flipped.token, first.token);
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
];
|
|
186
|
+
|
|
187
|
+
const releaseChecks = (target: ContractTarget, release: Release): Check[] => [
|
|
188
|
+
{
|
|
189
|
+
name: "release: publish and release accept an item",
|
|
190
|
+
run: async () => {
|
|
191
|
+
const id = await target.seed("shipped");
|
|
192
|
+
|
|
193
|
+
await release.publish(id, "branch-x");
|
|
194
|
+
await release.release(id, "branch-x");
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
];
|
|
198
|
+
|
|
199
|
+
/** The checks every adapter runs for the integrations it provides. */
|
|
200
|
+
export const contractChecks = (target: ContractTarget): Check[] => {
|
|
201
|
+
const { questions, answers, release } = target.adapter;
|
|
202
|
+
const source = need(target.adapter.workSource, `${target.name} provides no work source`);
|
|
203
|
+
|
|
204
|
+
return [
|
|
205
|
+
...workSourceChecks(target, source),
|
|
206
|
+
...(questions ? questionChecks(target, source, questions) : []),
|
|
207
|
+
...(answers ? answerChecks(target, source, answers) : []),
|
|
208
|
+
...(release ? releaseChecks(target, release) : []),
|
|
209
|
+
];
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
export type ContractResult = { readonly name: string; readonly failure?: string };
|
|
213
|
+
|
|
214
|
+
const runCheck = async ({ name, run }: Check): Promise<ContractResult> => {
|
|
215
|
+
try {
|
|
216
|
+
await run();
|
|
217
|
+
|
|
218
|
+
return { name };
|
|
219
|
+
} catch (error) {
|
|
220
|
+
return { name, failure: error instanceof Error ? error.message : String(error) };
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/** Runs every check one after another and returns each result; nothing stops at the first failure. */
|
|
225
|
+
export const runContract = async (target: ContractTarget): Promise<ContractResult[]> => {
|
|
226
|
+
const results: ContractResult[] = [];
|
|
227
|
+
|
|
228
|
+
for (const check of contractChecks(target)) results.push(await runCheck(check));
|
|
229
|
+
|
|
230
|
+
return results;
|
|
231
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { type Adapter, type Claim, type ItemRef, PermanentError, type ThreadEntry, type Watch } from "../adapter-api.ts";
|
|
2
|
+
import { claimer, isLive } from "./claim.ts";
|
|
3
|
+
import { questionText } from "./marked.ts";
|
|
4
|
+
|
|
5
|
+
type Stored = { id: string; title: string; body: string; ready: boolean; thread: ThreadEntry[]; claim?: Claim; from?: string };
|
|
6
|
+
|
|
7
|
+
/** A provider failure a retry can outlast, like a GitHub 502. */
|
|
8
|
+
export class ProviderError extends Error {
|
|
9
|
+
override readonly name = "ProviderError";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const START_CLOCK = 1_000_000;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* One in-memory provider behind all four integrations. Its controls stand in for the real provider's clock,
|
|
16
|
+
* its failures and its rate limit, so machine tests and fault tests run without a network.
|
|
17
|
+
*/
|
|
18
|
+
export class FakeProvider {
|
|
19
|
+
#items = new Map<string, Stored>();
|
|
20
|
+
#version = 0;
|
|
21
|
+
#clock = START_CLOCK;
|
|
22
|
+
#failures = 0;
|
|
23
|
+
#spent = 0;
|
|
24
|
+
#emptyList = false;
|
|
25
|
+
/** Every `publish` and `release`, in order. */
|
|
26
|
+
readonly shipped: string[] = [];
|
|
27
|
+
/** Replaces `listReady`'s answer, as when a provider changes its API and replies off the shape the engine knows. */
|
|
28
|
+
listReplacement: (() => ItemRef[]) | undefined;
|
|
29
|
+
|
|
30
|
+
now = (): number => this.#clock;
|
|
31
|
+
|
|
32
|
+
advance(ms: number): void {
|
|
33
|
+
this.#clock += ms;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Requests that spent rate limit. `watch` spends none. */
|
|
37
|
+
spent(): number {
|
|
38
|
+
return this.#spent;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The next `count` calls throw a `ProviderError`. */
|
|
42
|
+
failNext(count: number): void {
|
|
43
|
+
this.#failures = count;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `listReady` answers `[]` whatever it holds. */
|
|
47
|
+
set emptyList(value: boolean) {
|
|
48
|
+
this.#emptyList = value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
seed(title: string, { ready = true }: { ready?: boolean } = {}): string {
|
|
52
|
+
const id = `item-${this.#items.size + 1}`;
|
|
53
|
+
|
|
54
|
+
this.#items.set(id, { id, title, body: `${title} body`, ready, thread: [] });
|
|
55
|
+
this.#version++;
|
|
56
|
+
|
|
57
|
+
return id;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#call<T>(spend: boolean, body: () => T): T {
|
|
61
|
+
if (this.#failures > 0) {
|
|
62
|
+
this.#failures--;
|
|
63
|
+
throw new ProviderError("502 Bad Gateway");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (spend) this.#spent++;
|
|
67
|
+
|
|
68
|
+
return body();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
#get(id: string): Stored {
|
|
72
|
+
const found = this.#items.get(id);
|
|
73
|
+
|
|
74
|
+
if (!found) throw new PermanentError(`no item "${id}"`);
|
|
75
|
+
|
|
76
|
+
return found;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#isReady(item: Stored): boolean {
|
|
80
|
+
return item.ready && !isLive(item.claim, this.#clock);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#push(id: string, entry: Omit<ThreadEntry, "at">): void {
|
|
84
|
+
this.#get(id).thread.push({ ...entry, at: this.#clock });
|
|
85
|
+
this.#version++;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
#take = claimer(
|
|
89
|
+
{
|
|
90
|
+
standing: async (id) => {
|
|
91
|
+
const { claim } = this.#get(id);
|
|
92
|
+
|
|
93
|
+
return claim && { claim };
|
|
94
|
+
},
|
|
95
|
+
isReady: async (id) => this.#get(id).ready,
|
|
96
|
+
write: async (claim, over) => {
|
|
97
|
+
const item = this.#get(claim.item);
|
|
98
|
+
|
|
99
|
+
if (item.claim !== over?.claim) return false;
|
|
100
|
+
|
|
101
|
+
item.claim = claim;
|
|
102
|
+
this.#version++;
|
|
103
|
+
|
|
104
|
+
return true;
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
this.now,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
adapter(): Adapter {
|
|
111
|
+
const asRef = ({ id, title }: Stored): ItemRef => ({ id, title });
|
|
112
|
+
const spend = <T>(body: () => T) => this.#call(true, body);
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
apiVersion: 1,
|
|
116
|
+
workSource: {
|
|
117
|
+
listReady: async () =>
|
|
118
|
+
spend(() => this.listReplacement?.() ?? (this.#emptyList ? [] : [...this.#items.values()].flatMap((item) => (this.#isReady(item) ? [asRef(item)] : [])))),
|
|
119
|
+
read: async (id) =>
|
|
120
|
+
spend(() => {
|
|
121
|
+
const item = this.#get(id);
|
|
122
|
+
|
|
123
|
+
return { id: item.id, title: item.title, body: item.body, ready: this.#isReady(item), thread: [...item.thread] };
|
|
124
|
+
}),
|
|
125
|
+
claim: async (id, holder, ttlMs) => spend(() => this.#take("claim", id, holder, ttlMs)),
|
|
126
|
+
renew: async (id, holder, ttlMs) => spend(() => this.#take("renew", id, holder, ttlMs)),
|
|
127
|
+
comment: async (id, text) => spend(() => this.#push(id, { kind: "comment", text })),
|
|
128
|
+
fileFollowUp: async (from, { title, body }) =>
|
|
129
|
+
spend(() => {
|
|
130
|
+
this.#get(from);
|
|
131
|
+
|
|
132
|
+
const known = [...this.#items.values()].find((item) => item.from === from && item.title === title);
|
|
133
|
+
|
|
134
|
+
if (known) return asRef(known);
|
|
135
|
+
|
|
136
|
+
const id = this.seed(title);
|
|
137
|
+
|
|
138
|
+
Object.assign(this.#get(id), { body, from });
|
|
139
|
+
|
|
140
|
+
return { id, title };
|
|
141
|
+
}),
|
|
142
|
+
},
|
|
143
|
+
questions: {
|
|
144
|
+
ask: async (id, question) => spend(() => this.#push(id, { kind: "question", text: questionText(question) })),
|
|
145
|
+
decide: async (id, text) => spend(() => this.#push(id, { kind: "decision", text })),
|
|
146
|
+
},
|
|
147
|
+
answers: {
|
|
148
|
+
answer: async (id, { text, option }) => spend(() => this.#push(id, option === undefined ? { kind: "answer", text } : { kind: "answer", text, option })),
|
|
149
|
+
watch: async (since): Promise<Watch> => this.#call(false, () => ({ token: String(this.#version), changed: String(this.#version) !== since })),
|
|
150
|
+
},
|
|
151
|
+
release: {
|
|
152
|
+
publish: async (id, ref) => spend(() => void this.shipped.push(`publish ${id} ${ref}`)),
|
|
153
|
+
release: async (id, ref) => spend(() => void this.shipped.push(`release ${id} ${ref}`)),
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { PermanentError } from "../adapter-api.ts";
|
|
2
|
+
import { exec } from "../exec.ts";
|
|
3
|
+
|
|
4
|
+
/** No hook runs: the hooks path points nowhere, whatever the clone's own config says. */
|
|
5
|
+
const HOOKS_OFF = "core.hooksPath=/dev/null";
|
|
6
|
+
|
|
7
|
+
const CONFLICT = /non-fast-forward|fetch first|already exists|stale info|failed to update ref|cannot lock ref/i;
|
|
8
|
+
const REFUSED = /remote rejected|declined|protected|GH006|denied|not permitted/i;
|
|
9
|
+
const ABSENT = /does not exist|not a valid object name/i;
|
|
10
|
+
|
|
11
|
+
/** Patch text as git writes it, whatever external diff tool or colour the environment sets: `GIT_EXTERNAL_DIFF` replaces it otherwise. */
|
|
12
|
+
export const PATCH = ["--no-ext-diff", "--no-color"] as const;
|
|
13
|
+
|
|
14
|
+
const ANCESTOR = 0;
|
|
15
|
+
const NOT_ANCESTOR = 1;
|
|
16
|
+
|
|
17
|
+
export type GitResult = { readonly code: number; readonly stdout: string; readonly stderr: string };
|
|
18
|
+
|
|
19
|
+
export type Git = {
|
|
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. */
|
|
21
|
+
readonly run: (args: readonly string[], input?: string) => Promise<GitResult>;
|
|
22
|
+
/** Runs git and returns what it printed; a non-zero exit throws `GitFailed` with git's own words. */
|
|
23
|
+
readonly raw: (args: readonly string[], input?: string) => Promise<string>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export class GitFailed extends Error {
|
|
27
|
+
override name = "GitFailed";
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
readonly args: readonly string[],
|
|
31
|
+
readonly result: GitResult,
|
|
32
|
+
) {
|
|
33
|
+
super(`git ${args.join(" ")} exited ${result.code}: ${(result.stderr || result.stdout).trim()}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** For `.catch`: git failing with its own words is the fallback; a call that ran out of time stays an error. */
|
|
38
|
+
export const whenGitFailed =
|
|
39
|
+
<T>(fallback: T) =>
|
|
40
|
+
(error: Error): T => {
|
|
41
|
+
if (error instanceof GitFailed) return fallback;
|
|
42
|
+
|
|
43
|
+
throw error;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** The remote refused the push: a protected branch, a hook, a permission. Retrying cannot help. */
|
|
47
|
+
export class PushRefused extends PermanentError {
|
|
48
|
+
override name = "PushRefused";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type PushOutcome = "pushed" | "conflict";
|
|
52
|
+
|
|
53
|
+
/** The author and committer of the commits hum makes. */
|
|
54
|
+
export type GitIdentity = { readonly name: string; readonly email: string };
|
|
55
|
+
|
|
56
|
+
export type GitOptions = {
|
|
57
|
+
/** The owner's `commandTimeoutSeconds` in milliseconds; a call that runs longer is stopped and throws `CommandTimedOut`. */
|
|
58
|
+
readonly timeoutMs: number;
|
|
59
|
+
/** The owner's `commitIdentity` setting. Without it git commits as its own config says, so every call that commits passes it. */
|
|
60
|
+
readonly identity?: GitIdentity;
|
|
61
|
+
/** More variables, such as a scratch index or a commit date. */
|
|
62
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/** The variables that make git credit a commit to `identity`, over any git config the clone or the machine holds. */
|
|
66
|
+
export const identityEnv = ({ name, email }: GitIdentity) => ({
|
|
67
|
+
GIT_AUTHOR_NAME: name,
|
|
68
|
+
GIT_AUTHOR_EMAIL: email,
|
|
69
|
+
GIT_COMMITTER_NAME: name,
|
|
70
|
+
GIT_COMMITTER_EMAIL: email,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const NAME_KEY = "user.name";
|
|
74
|
+
const EMAIL_KEY = "user.email";
|
|
75
|
+
|
|
76
|
+
export type GitIdentityRead =
|
|
77
|
+
| { readonly found: true; readonly identity: GitIdentity }
|
|
78
|
+
/** `missing` names each git config key that holds nothing. */
|
|
79
|
+
| { readonly found: false; readonly missing: readonly string[] };
|
|
80
|
+
|
|
81
|
+
const configValue = async (git: Git, key: string): Promise<string> => (await git.run(["config", "--get", key])).stdout.trim();
|
|
82
|
+
|
|
83
|
+
/** The owner's name and email from git config, the repo's own over the user's. Read with a `git` that has no identity of its own. */
|
|
84
|
+
export const readGitIdentity = async (git: Git): Promise<GitIdentityRead> => {
|
|
85
|
+
const name = await configValue(git, NAME_KEY);
|
|
86
|
+
const email = await configValue(git, EMAIL_KEY);
|
|
87
|
+
const missing = [...(name === "" ? [NAME_KEY] : []), ...(email === "" ? [EMAIL_KEY] : [])];
|
|
88
|
+
|
|
89
|
+
return missing.length === 0 ? { found: true, identity: { name, email } } : { found: false, missing };
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/** git in `cwd` with hooks off. */
|
|
93
|
+
export const gitIn = (cwd: string, { identity, env = {}, timeoutMs }: GitOptions): Git => {
|
|
94
|
+
const run = async (args: readonly string[], input = ""): Promise<GitResult> => {
|
|
95
|
+
const { exitCode, stdout, stderr } = await exec("git", ["-c", HOOKS_OFF, ...args], {
|
|
96
|
+
timeoutMs,
|
|
97
|
+
nodeOptions: { cwd, env: { ...process.env, ...(identity && identityEnv(identity)), ...env } },
|
|
98
|
+
stdin: input,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return { code: exitCode ?? -1, stdout, stderr };
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const raw = async (args: readonly string[], input?: string): Promise<string> => {
|
|
105
|
+
const result = await run(args, input);
|
|
106
|
+
|
|
107
|
+
if (result.code !== 0) throw new GitFailed(args, result);
|
|
108
|
+
|
|
109
|
+
return result.stdout;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
return { run, raw };
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** The arguments for `git push` with hooks off, for a command line handed straight to git. */
|
|
116
|
+
export const pushArgs = (rest: readonly string[]): string[] => ["-c", HOOKS_OFF, "push", "--no-verify", ...rest];
|
|
117
|
+
|
|
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
|
+
*/
|
|
122
|
+
export const push = async (git: Git, rest: readonly string[]): Promise<PushOutcome> => {
|
|
123
|
+
const args = ["push", "--no-verify", ...rest];
|
|
124
|
+
const result = await git.run(args);
|
|
125
|
+
const said = `${result.stderr}${result.stdout}`.trim();
|
|
126
|
+
|
|
127
|
+
if (result.code === 0) return "pushed";
|
|
128
|
+
|
|
129
|
+
if (CONFLICT.test(said)) return "conflict";
|
|
130
|
+
|
|
131
|
+
if (REFUSED.test(said)) throw new PushRefused(`git push refused: ${said}`);
|
|
132
|
+
|
|
133
|
+
throw new GitFailed(args, result);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** Whether `ancestor` is reachable from `descendant`. git answers by exit code alone. */
|
|
137
|
+
export const isAncestor = async (git: Git, ancestor: string, descendant: string): Promise<boolean> => {
|
|
138
|
+
const args = ["merge-base", "--is-ancestor", ancestor, descendant];
|
|
139
|
+
const result = await git.run(args);
|
|
140
|
+
|
|
141
|
+
if (result.code === ANCESTOR || result.code === NOT_ANCESTOR) return result.code === ANCESTOR;
|
|
142
|
+
|
|
143
|
+
throw new GitFailed(args, result);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** What git printed for one object, or undefined when the object does not exist. */
|
|
147
|
+
export const readOrAbsent = async (git: Git, args: readonly string[]): Promise<string | undefined> => {
|
|
148
|
+
const result = await git.run(args);
|
|
149
|
+
|
|
150
|
+
if (result.code === 0) return result.stdout;
|
|
151
|
+
|
|
152
|
+
if (ABSENT.test(result.stderr)) return undefined;
|
|
153
|
+
|
|
154
|
+
throw new GitFailed(args, result);
|
|
155
|
+
};
|