@milkenjoyer/toolchain 0.15.0 → 0.17.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/README.md CHANGED
@@ -265,3 +265,7 @@ Rule 16 applies once `specs.json` has an `arithmetic` key. Each `arithmetic` glo
265
265
  - `domain-no-throw`: Domain.Rejection/Schema.TaggedError.
266
266
 
267
267
  Source checks skip tests/proofs. Runtime story coverage is tracked in #325.
268
+
269
+ # Boat start budgets
270
+
271
+ See [Boat start admission](STARTS.md) for `@milkenjoyer/toolchain/starts` in 0.17.0.
package/STARTS.md ADDED
@@ -0,0 +1,17 @@
1
+ # Boat start admission
2
+
3
+ Pin `@milkenjoyer/toolchain` to `0.17.0`. Import `admitStart`, `BoatStarts`, `Merges` and `Repo` from `@milkenjoyer/toolchain/starts`. Wrap each create, fork or resume in `admitStart({ repo, jobs: 1, priority: "ordinary" }, start, { deadline })`. Priority is required and accepts `"ordinary"` or `"p0"`. Red main is p0. `Repo` validates an `owner/name`. `deadline` is an absolute epoch time in milliseconds. `start` is a lazy Effect that runs once after admission.
4
+
5
+ Provide `BoatStarts.layer(key, { fetch? })` and `Merges.layer(githubToken, { fetch? })` with redacted credentials. Boat reads the key's default billing wallet. GitHub must see all four repos. Each optional `fetch` supplies that SDK's transport. The pure `decide(request, counts, now)` takes the current epoch time explicitly.
6
+
7
+ Each request counts as `jobs` in every live minute, hour and day window. A paid or unavailable next-start quote waits. Below 50% account use, every priority passes, subject to the account ceiling. From 50% to 80%, ordinary requests must fit the repo's share of an 80% pool. From 80% to 95%, only p0 passes. No request may exceed its account ceiling, rounded down to 80% for ordinary or 95% for p0, including the requested jobs.
8
+
9
+ `startRepos` contains agent-kit, siglata/siglata, optionhouse-xyz/optionhouse and birbprophet/vibebattler. Each repo's share is `10% + 60% × weight / total weight`. Weight is `max(merges in 24h, 4 × merges in 6h, merges in 7 days / 7)` to main. All zero weights give each repo 25%. Each repo budget is `floor(share × 80% × window limit)`, computed with exact arithmetic. Merge and repo history are read only when an ordinary request faces at least 50% use in a window.
10
+
11
+ `StartHeld.line` names the refusal. `Unread` means a read failed, history is incomplete or input is invalid. Read failures and incomplete histories retry until the deadline. Invalid plans, repos, jobs and timestamps hold immediately. `Paid` waits for a free quote. `Account` means the request exceeds its account ceiling and waits on minute or hour, or holds on day. `Budget` means the repo exceeds its live share and waits for the kth oldest start to leave the window. A request larger than its repo budget holds. Every wait polls fresh readings until the deadline, then fails `StartHeld` with the waiting line.
12
+
13
+ Repo CI history counts a Boat job without `runStartedAt` as starting now unless Boat reports it settled, including states the SDK does not list, such as `paused`. A start stamped after Boat answers counts at the answer's time. That history omits direct sandbox starts. Admission reserves nothing, so concurrent callers can race.
14
+
15
+ Import `boatJobFindings` from `@milkenjoyer/toolchain/starts`. Add its findings to the existing workflow policy, with every workflow as a `[path, text]` pair. It bounds each closure to `BOAT_JOBS_PER_RUN = 2`, including literal matrices, repeated local reusable calls and `workflow_run` descendants. Each descendant counts once per distinct activity type, or three times when `types` is absent. Dynamic and unresolved graphs produce findings. Keep runner, timeout and concurrency checks.
16
+
17
+ PR and main workflow triggers stay unchanged until the request controller ships. Workflows start without admission, bounded only by `BOAT_JOBS_PER_RUN`. Product repos adopt the published pin instead of copying files. Boat reports no start spend, so nothing here proves zero paid starts.
@@ -0,0 +1,113 @@
1
+ import * as Schema from "effect/Schema";
2
+ import * as Effect from "effect/Effect";
3
+ import * as Layer from "effect/Layer";
4
+ import * as Context from "effect/Context";
5
+ import * as Redacted from "effect/Redacted";
6
+ export declare const Repo: Schema.brand<Schema.String, "Repo">;
7
+ export type Repo = typeof Repo.Type;
8
+ type Window = "minute" | "hour" | "day";
9
+ type PerWindow<A> = { readonly [W in Window]: A; };
10
+ type Priority = "ordinary" | "p0";
11
+ type MergeCounts = {
12
+ readonly repo: Repo;
13
+ readonly h6: number;
14
+ readonly h24: number;
15
+ readonly d7: number;
16
+ };
17
+ type Plan = {
18
+ readonly limits: PerWindow<number>;
19
+ readonly used: PerWindow<number>;
20
+ readonly nextStartDollars: number | undefined;
21
+ };
22
+ type RepoStarts = {
23
+ readonly repo: Repo;
24
+ readonly starts: ReadonlyArray<number>;
25
+ readonly complete: boolean;
26
+ };
27
+ type Counts = {
28
+ readonly now: number;
29
+ readonly plan: Plan;
30
+ readonly repo?: RepoStarts | undefined;
31
+ readonly merges?: ReadonlyArray<MergeCounts> | undefined;
32
+ };
33
+ type StartRequest = {
34
+ readonly repo: Repo;
35
+ readonly jobs: number;
36
+ readonly priority: Priority;
37
+ };
38
+ declare const Line: Schema.Union<readonly [Schema.TaggedStruct<"Unread", {
39
+ readonly reason: Schema.String;
40
+ }>, Schema.TaggedStruct<"Paid", {
41
+ readonly nextStartDollars: Schema.UndefinedOr<Schema.Number>;
42
+ }>, Schema.TaggedStruct<"Account", {
43
+ readonly window: Schema.Literals<readonly ["minute", "hour", "day"]>;
44
+ readonly used: Schema.Number;
45
+ readonly line: Schema.Number;
46
+ }>, Schema.TaggedStruct<"Budget", {
47
+ readonly repo: Schema.brand<Schema.String, "Repo">;
48
+ readonly window: Schema.Literals<readonly ["minute", "hour", "day"]>;
49
+ readonly used: Schema.Number;
50
+ readonly budget: Schema.Number;
51
+ }>]>;
52
+ type Line = typeof Line.Type;
53
+ type Admission = {
54
+ readonly _tag: "Admit";
55
+ } | {
56
+ readonly _tag: "Wait";
57
+ readonly until: number;
58
+ readonly line: Line;
59
+ } | {
60
+ readonly _tag: "Hold";
61
+ readonly line: Line;
62
+ };
63
+ export declare const startRepos: ReadonlyArray<Repo>;
64
+ export declare const decide: (request: StartRequest, counts: Counts | undefined, now: number) => Admission;
65
+ declare const StartReadError_base: Schema.Class<StartReadError, Schema.TaggedStruct<"StartReadError", {
66
+ readonly reason: Schema.String;
67
+ }>, import("effect/Cause").YieldableError & {
68
+ readonly brand: unique symbol;
69
+ }>;
70
+ export declare class StartReadError extends StartReadError_base {}
71
+ declare const BoatStarts_base: Context.ServiceClass<BoatStarts, "toolchain/BoatStarts", {
72
+ readonly plan: Effect.Effect<Plan, StartReadError>;
73
+ readonly repoStarts: (repo: Repo) => Effect.Effect<RepoStarts, StartReadError>;
74
+ }>;
75
+ export declare class BoatStarts extends BoatStarts_base {
76
+ static readonly layer: (key: Redacted.Redacted<string>, options?: {
77
+ readonly fetch?: (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
78
+ }) => Layer.Layer<BoatStarts, never, never>;
79
+ }
80
+ declare const Merges_base: Context.ServiceClass<Merges, "toolchain/Merges", {
81
+ readonly counts: Effect.Effect<ReadonlyArray<MergeCounts>, StartReadError>;
82
+ }>;
83
+ export declare class Merges extends Merges_base {
84
+ static readonly layer: (token: Redacted.Redacted<string>, options?: {
85
+ readonly fetch?: (url: string | URL | Request, init?: RequestInit) => Promise<Response>;
86
+ }) => Layer.Layer<Merges, never, never>;
87
+ }
88
+ export declare const BOAT_JOBS_PER_RUN = 2;
89
+ type WorkflowFile = readonly [path: string, text: string];
90
+ export declare const boatJobFindings: (files: ReadonlyArray<WorkflowFile>) => ReadonlyArray<string>;
91
+ declare const StartHeld_base: Schema.Class<StartHeld, Schema.TaggedStruct<"StartHeld", {
92
+ readonly line: Schema.Union<readonly [Schema.TaggedStruct<"Unread", {
93
+ readonly reason: Schema.String;
94
+ }>, Schema.TaggedStruct<"Paid", {
95
+ readonly nextStartDollars: Schema.UndefinedOr<Schema.Number>;
96
+ }>, Schema.TaggedStruct<"Account", {
97
+ readonly window: Schema.Literals<readonly ["minute", "hour", "day"]>;
98
+ readonly used: Schema.Number;
99
+ readonly line: Schema.Number;
100
+ }>, Schema.TaggedStruct<"Budget", {
101
+ readonly repo: Schema.brand<Schema.String, "Repo">;
102
+ readonly window: Schema.Literals<readonly ["minute", "hour", "day"]>;
103
+ readonly used: Schema.Number;
104
+ readonly budget: Schema.Number;
105
+ }>]>;
106
+ }>, import("effect/Cause").YieldableError & {
107
+ readonly brand: unique symbol;
108
+ }>;
109
+ export declare class StartHeld extends StartHeld_base {}
110
+ export declare const admitStart: <A, E, R>(request: StartRequest, start: Effect.Effect<A, E, R>, args_2: {
111
+ readonly deadline: number;
112
+ }) => Effect.Effect<A, E | StartHeld, R | BoatStarts | Merges>;
113
+ export type { Admission, Counts, Line, MergeCounts, PerWindow, Plan, Priority, RepoStarts, StartRequest, Window };
@@ -0,0 +1,458 @@
1
+ import * as Schema from "effect/Schema";
2
+ import * as Effect from "effect/Effect";
3
+ import * as Layer from "effect/Layer";
4
+ import * as Clock from "effect/Clock";
5
+ import * as Context from "effect/Context";
6
+ import { Octokit } from "@octokit/rest";
7
+ import * as Redacted from "effect/Redacted";
8
+ import { BoatApi, CiJobState, Configuration } from "@boatdev/sdk";
9
+ import { parse } from "yaml";
10
+ // src/starts/budget.ts
11
+ const Repo = Schema.String.check(Schema.isPattern(/^[\w.-]+\/[\w.-]+$/u)).pipe(Schema.brand("Repo"));
12
+ const Window = Schema.Literals([
13
+ "minute",
14
+ "hour",
15
+ "day"
16
+ ]);
17
+ const Line = Schema.Union([
18
+ Schema.TaggedStruct("Unread", { reason: Schema.String }),
19
+ Schema.TaggedStruct("Paid", { nextStartDollars: Schema.UndefinedOr(Schema.Number) }),
20
+ Schema.TaggedStruct("Account", {
21
+ window: Window,
22
+ used: Schema.Number,
23
+ line: Schema.Number
24
+ }),
25
+ Schema.TaggedStruct("Budget", {
26
+ repo: Repo,
27
+ window: Window,
28
+ used: Schema.Number,
29
+ budget: Schema.Number
30
+ })
31
+ ]);
32
+ const windows$1 = [
33
+ "minute",
34
+ "hour",
35
+ "day"
36
+ ];
37
+ const windowMillis = {
38
+ minute: 6e4,
39
+ hour: 36e5,
40
+ day: 864e5
41
+ };
42
+ const pollMillis = {
43
+ minute: 15e3,
44
+ hour: 6e4,
45
+ day: 3e5
46
+ };
47
+ const startRepos = [
48
+ Repo.make("birbprophet/agent-kit"),
49
+ Repo.make("siglata/siglata"),
50
+ Repo.make("optionhouse-xyz/optionhouse"),
51
+ Repo.make("birbprophet/vibebattler")
52
+ ];
53
+ const count = (value) => Number.isSafeInteger(value) && value >= 0;
54
+ const ceiling = (request, plan, window) => Number(BigInt(plan.limits[window]) * (request.priority === "p0" ? 95n : 80n) / 100n);
55
+ const sharedWindows = (request, plan) => request.priority === "p0" || plan.nextStartDollars !== 0 || !windows$1.every((window) => count(plan.limits[window]) && count(plan.used[window])) || windows$1.some((window) => plan.used[window] + request.jobs > ceiling(request, plan, window)) ? [] : windows$1.filter((window) => 2 * plan.used[window] >= plan.limits[window]);
56
+ const liveShare = (repo, merges) => {
57
+ if (merges === void 0 || merges.length !== startRepos.length || !startRepos.every((repo) => merges.filter((row) => row.repo === repo).length === 1) || !merges.every((row) => [
58
+ row.h6,
59
+ row.h24,
60
+ row.d7
61
+ ].every(count))) return void 0;
62
+ const weights = merges.map((row) => ({
63
+ repo: row.repo,
64
+ weight: [
65
+ 7n * BigInt(row.h24),
66
+ 28n * BigInt(row.h6),
67
+ BigInt(row.d7)
68
+ ].reduce((largest, value) => value > largest ? value : largest, 0n)
69
+ }));
70
+ const total = weights.reduce((sum, row) => sum + row.weight, 0n);
71
+ const weight = weights.find((row) => row.repo === repo)?.weight;
72
+ return weight === void 0 ? void 0 : total === 0n ? {
73
+ numerator: 1n,
74
+ denominator: 4n
75
+ } : {
76
+ numerator: total + 6n * weight,
77
+ denominator: 10n * total
78
+ };
79
+ };
80
+ const unread = (reason) => ({
81
+ _tag: "Hold",
82
+ line: {
83
+ _tag: "Unread",
84
+ reason
85
+ }
86
+ });
87
+ const decide = (request, counts, now) => {
88
+ if (!startRepos.includes(request.repo)) return unread("repo has no start budget");
89
+ if (!count(request.jobs) || request.jobs === 0) return unread("jobs must be a positive safe integer");
90
+ if (!Number.isFinite(now)) return unread("start time unread");
91
+ if (counts === void 0) return {
92
+ _tag: "Wait",
93
+ until: now + pollMillis.minute,
94
+ line: {
95
+ _tag: "Unread",
96
+ reason: "start plan unread"
97
+ }
98
+ };
99
+ if (!Number.isFinite(counts.now) || counts.now > now || !windows$1.every((window) => count(counts.plan.limits[window]) && count(counts.plan.used[window]))) return unread("start plan unread");
100
+ const { plan } = counts;
101
+ if (plan.nextStartDollars !== 0) return {
102
+ _tag: "Wait",
103
+ until: now + pollMillis.minute,
104
+ line: {
105
+ _tag: "Paid",
106
+ nextStartDollars: plan.nextStartDollars
107
+ }
108
+ };
109
+ for (const window of windows$1) {
110
+ const used = plan.used[window];
111
+ const line = ceiling(request, plan, window);
112
+ if (used + request.jobs > line) return window === "day" ? {
113
+ _tag: "Hold",
114
+ line: {
115
+ _tag: "Account",
116
+ window,
117
+ used,
118
+ line
119
+ }
120
+ } : {
121
+ _tag: "Wait",
122
+ until: now + pollMillis[window],
123
+ line: {
124
+ _tag: "Account",
125
+ window,
126
+ used,
127
+ line
128
+ }
129
+ };
130
+ }
131
+ const shared = sharedWindows(request, plan);
132
+ if (shared.length === 0) return { _tag: "Admit" };
133
+ if (counts.repo === void 0 || !counts.repo.complete) return {
134
+ _tag: "Wait",
135
+ until: now + pollMillis.minute,
136
+ line: {
137
+ _tag: "Unread",
138
+ reason: "repo start history incomplete"
139
+ }
140
+ };
141
+ if (counts.repo.repo !== request.repo || !counts.repo.starts.every((at) => Number.isFinite(at) && at <= counts.now)) return unread("repo start history incomplete");
142
+ const share = liveShare(request.repo, counts.merges);
143
+ if (share === void 0) return {
144
+ _tag: "Wait",
145
+ until: now + pollMillis.minute,
146
+ line: {
147
+ _tag: "Unread",
148
+ reason: "merge history incomplete"
149
+ }
150
+ };
151
+ for (const window of shared) {
152
+ const starts = counts.repo.starts.filter((at) => at > now - windowMillis[window]).toSorted((left, right) => left - right);
153
+ const used = starts.length;
154
+ const budget = Number(share.numerator * 4n * BigInt(plan.limits[window]) / (share.denominator * 5n));
155
+ if (used + request.jobs <= budget) continue;
156
+ const line = {
157
+ _tag: "Budget",
158
+ repo: request.repo,
159
+ window,
160
+ used,
161
+ budget
162
+ };
163
+ const expires = starts[used + request.jobs - budget - 1];
164
+ return request.jobs > budget || expires === void 0 ? {
165
+ _tag: "Hold",
166
+ line
167
+ } : {
168
+ _tag: "Wait",
169
+ until: expires + windowMillis[window],
170
+ line
171
+ };
172
+ }
173
+ return { _tag: "Admit" };
174
+ };
175
+ // src/starts/boat.ts
176
+ var StartReadError = class extends Schema.TaggedError()("StartReadError", { reason: Schema.String }) {};
177
+ const Count$1 = Schema.Number.check(Schema.makeFilter((value) => Number.isSafeInteger(value) && value >= 0));
178
+ const Usage = Schema.Struct({
179
+ limit: Count$1,
180
+ used: Count$1
181
+ });
182
+ const Limits = Schema.Struct({
183
+ ok: Schema.Literal(true),
184
+ starts: Schema.Struct({
185
+ minute: Usage,
186
+ hour: Usage,
187
+ day: Usage
188
+ }),
189
+ nextStartDollars: Schema.optional(Schema.NullOr(Schema.Finite))
190
+ });
191
+ const Ci = Schema.Struct({
192
+ ok: Schema.Literal(true),
193
+ jobs: Schema.Array(Schema.Struct({
194
+ repo: Schema.String,
195
+ label: Schema.String,
196
+ state: Schema.String.check(Schema.isMinLength(1)),
197
+ queuedAt: Schema.Date,
198
+ runStartedAt: Schema.optional(Schema.NullOr(Schema.Date))
199
+ }))
200
+ });
201
+ const settled = /* @__PURE__ */ new Set([
202
+ CiJobState.Success,
203
+ CiJobState.Failure,
204
+ CiJobState.Cancelled,
205
+ CiJobState.Skipped,
206
+ CiJobState.Failed,
207
+ CiJobState.Refused
208
+ ]);
209
+ var BoatStarts = class BoatStarts extends Context.Service()("toolchain/BoatStarts") {
210
+ static layer = (key, options = {}) => Layer.sync(BoatStarts, () => {
211
+ const api = new BoatApi(new Configuration({
212
+ accessToken: Redacted.value(key),
213
+ ...options.fetch === void 0 ? {} : { fetchApi: options.fetch }
214
+ }));
215
+ const plan = Effect.gen(function* () {
216
+ const response = yield* Effect.tryPromise({
217
+ try: () => api.limits(),
218
+ catch: () => new StartReadError({ reason: "Boat limits request failed" })
219
+ });
220
+ const limits = yield* Schema.decodeUnknownEffect(Limits)(response).pipe(Effect.mapError(() => new StartReadError({ reason: "Boat limits unread" })));
221
+ return {
222
+ limits: {
223
+ minute: limits.starts.minute.limit,
224
+ hour: limits.starts.hour.limit,
225
+ day: limits.starts.day.limit
226
+ },
227
+ used: {
228
+ minute: limits.starts.minute.used,
229
+ hour: limits.starts.hour.used,
230
+ day: limits.starts.day.used
231
+ },
232
+ nextStartDollars: limits.nextStartDollars ?? void 0
233
+ };
234
+ });
235
+ const repoStarts = Effect.fn("BoatStarts.repoStarts")(function* (repo) {
236
+ const response = yield* Effect.tryPromise({
237
+ try: () => api.getCiOverview({
238
+ repo,
239
+ limit: 500
240
+ }),
241
+ catch: () => new StartReadError({ reason: "Boat CI request failed" })
242
+ });
243
+ const now = yield* Clock.currentTimeMillis;
244
+ const { jobs } = yield* Schema.decodeUnknownEffect(Ci)(response).pipe(Effect.mapError(() => new StartReadError({ reason: "Boat CI history unread" })));
245
+ if (jobs.some((job) => job.repo !== repo)) return yield* new StartReadError({ reason: "Boat CI history differs from request" });
246
+ const oldest = Math.min(...jobs.map((job) => Math.max(job.queuedAt.getTime(), job.runStartedAt?.getTime() ?? 0)));
247
+ return {
248
+ repo,
249
+ starts: jobs.flatMap((job) => {
250
+ const at = job.runStartedAt?.getTime() ?? (settled.has(job.state) ? void 0 : now);
251
+ return job.label.split("/")[0]?.startsWith("boat") === true && at !== void 0 && at > now - windowMillis.day ? [Math.min(at, now)] : [];
252
+ }),
253
+ complete: jobs.length < 500 || oldest <= now - windowMillis.day
254
+ };
255
+ });
256
+ return BoatStarts.of({
257
+ plan,
258
+ repoStarts
259
+ });
260
+ });
261
+ };
262
+ // src/starts/merges.ts
263
+ const Count = Schema.Struct({ issueCount: Schema.Number.check(Schema.makeFilter((value) => Number.isSafeInteger(value) && value >= 0)) });
264
+ const windows = {
265
+ h6: 216e5,
266
+ h24: 864e5,
267
+ d7: 6048e5
268
+ };
269
+ var Merges = class Merges extends Context.Service()("toolchain/Merges") {
270
+ static layer = (token, options = {}) => Layer.sync(Merges, () => {
271
+ const api = new Octokit({
272
+ auth: Redacted.value(token),
273
+ ...options.fetch === void 0 ? {} : { request: { fetch: options.fetch } }
274
+ });
275
+ const counts = Effect.gen(function* () {
276
+ const now = yield* Clock.currentTimeMillis;
277
+ const query = `query { ${startRepos.flatMap((repo, index) => {
278
+ const [owner, name] = repo.split("/");
279
+ return [`repo${index}: repository(owner: "${owner}", name: "${name}") { nameWithOwner }`, ...Object.entries(windows).map(([window, millis]) => {
280
+ return `${window}_${index}: search(type: ISSUE, first: 0, query: "repo:${repo} is:pr is:merged base:main merged:>=${new Date(now - millis).toISOString().replace(/\.\d{3}Z$/u, "Z")}") { issueCount }`;
281
+ })];
282
+ }).join(" ")} }`;
283
+ const response = yield* Effect.tryPromise({
284
+ try: () => api.graphql(query),
285
+ catch: () => new StartReadError({ reason: "GitHub merges request failed" })
286
+ });
287
+ return yield* Schema.decodeUnknownEffect(Schema.Record(Schema.String, Schema.Unknown))(response).pipe(Effect.flatMap((data) => Effect.forEach(startRepos, (repo, index) => Schema.decodeUnknownEffect(Schema.Struct({
288
+ repository: Schema.Struct({ nameWithOwner: Schema.Literal(repo) }),
289
+ h6: Count,
290
+ h24: Count,
291
+ d7: Count
292
+ }))({
293
+ repository: data[`repo${index}`],
294
+ h6: data[`h6_${index}`],
295
+ h24: data[`h24_${index}`],
296
+ d7: data[`d7_${index}`]
297
+ }).pipe(Effect.map((row) => ({
298
+ repo,
299
+ h6: row.h6.issueCount,
300
+ h24: row.h24.issueCount,
301
+ d7: row.d7.issueCount
302
+ }))), { concurrency: 1 })), Effect.mapError(() => new StartReadError({ reason: "GitHub merges unread" })));
303
+ });
304
+ return Merges.of({ counts });
305
+ });
306
+ };
307
+ // src/starts/policy.ts
308
+ const BOAT_JOBS_PER_RUN = 2;
309
+ const Matrix = Schema.Record(Schema.String, Schema.Array(Schema.Unknown));
310
+ const Workflow = Schema.Struct({
311
+ name: Schema.optionalKey(Schema.String),
312
+ on: Schema.optionalKey(Schema.Union([
313
+ Schema.String,
314
+ Schema.Array(Schema.String),
315
+ Schema.Record(Schema.String, Schema.Unknown)
316
+ ])),
317
+ jobs: Schema.Record(Schema.String, Schema.Struct({
318
+ "runs-on": Schema.optionalKey(Schema.Union([Schema.String, Schema.Array(Schema.String)])),
319
+ uses: Schema.optionalKey(Schema.String),
320
+ strategy: Schema.optionalKey(Schema.Struct({ matrix: Schema.optionalKey(Schema.Union([Schema.String, Matrix])) }))
321
+ }))
322
+ });
323
+ const Descendant = Schema.Struct({ workflow_run: Schema.Struct({
324
+ workflows: Schema.Union([Schema.String, Schema.Array(Schema.String)]),
325
+ types: Schema.optionalKey(Schema.Union([Schema.String, Schema.Array(Schema.String)]))
326
+ }) });
327
+ const dynamic = (value) => {
328
+ try {
329
+ return JSON.stringify(value).includes("${{");
330
+ } catch {
331
+ return true;
332
+ }
333
+ };
334
+ const boatJobFindings = (files) => {
335
+ const findings = [];
336
+ const workflows = [];
337
+ for (const [path, text] of files) try {
338
+ workflows.push({
339
+ path,
340
+ workflow: Schema.decodeUnknownSync(Workflow)(parse(text))
341
+ });
342
+ } catch {
343
+ findings.push(`${path}: workflow unread`);
344
+ }
345
+ const after = (entry) => {
346
+ const events = entry.workflow.on;
347
+ if (!Schema.is(Descendant)(events)) {
348
+ if (typeof events === "object" && "workflow_run" in events) findings.push(`${entry.path}: workflow_run unread`);
349
+ return {
350
+ names: [],
351
+ copies: 0
352
+ };
353
+ }
354
+ const names = events.workflow_run.workflows;
355
+ const selectors = typeof names === "string" ? [names] : names;
356
+ if (selectors.some((name) => /[*!?[\]]/u.test(name))) findings.push(`${entry.path}: workflow_run pattern unresolved`);
357
+ const types = events.workflow_run.types;
358
+ if (types !== void 0 && dynamic(types)) findings.push(`${entry.path}: dynamic workflow_run types`);
359
+ return {
360
+ names: selectors,
361
+ copies: types === void 0 ? 3 : new Set(typeof types === "string" ? [types] : types).size
362
+ };
363
+ };
364
+ const count = (entry, parents, descendants) => {
365
+ if (parents.includes(entry.path)) {
366
+ findings.push(`${entry.path}: workflow cycle`);
367
+ return 0;
368
+ }
369
+ const stack = [...parents, entry.path];
370
+ let total = 0;
371
+ for (const [name, job] of Object.entries(entry.workflow.jobs)) {
372
+ const label = `${entry.path} ${name}`;
373
+ const matrix = job.strategy?.matrix;
374
+ let copies = 1;
375
+ if (matrix !== void 0) {
376
+ if (typeof matrix === "string" || dynamic(matrix)) findings.push(`${label}: dynamic matrix`);
377
+ else {
378
+ const axes = Object.entries(matrix).filter(([axis]) => axis !== "include" && axis !== "exclude");
379
+ copies = (axes.length === 0 ? 0 : axes.reduce((product, [, values]) => product * values.length, 1)) + (matrix.include?.length ?? 0);
380
+ }
381
+ }
382
+ if (job.uses !== void 0) {
383
+ if (!/^\.\/\.github\/workflows\/[^/]+\.ya?ml$/u.test(job.uses)) {
384
+ findings.push(`${label}: remote or unresolved reusable workflow`);
385
+ continue;
386
+ }
387
+ const target = job.uses.slice(2);
388
+ const called = workflows.find(({ path }) => path === target || `.github/workflows/${path}` === target);
389
+ if (called === void 0) findings.push(`${label}: reusable workflow ${target} unread`);
390
+ else total += copies * count(called, stack, false);
391
+ } else if (job["runs-on"] === void 0 || dynamic(job["runs-on"])) findings.push(`${label}: dynamic or unread runs-on`);
392
+ else {
393
+ const runners = job["runs-on"];
394
+ if ((typeof runners === "string" ? [runners] : runners).some((runner) => runner.split("/")[0]?.startsWith("boat") === true)) total += copies;
395
+ }
396
+ }
397
+ if (descendants) for (const child of workflows) {
398
+ const { names, copies } = after(child);
399
+ if (dynamic(names)) findings.push(`${child.path}: dynamic workflow_run`);
400
+ const file = entry.path.split("/").at(-1) ?? entry.path;
401
+ if ((entry.workflow.name === void 0 ? [
402
+ entry.path,
403
+ file,
404
+ `.github/workflows/${file}`
405
+ ] : [entry.workflow.name]).some((name) => names.includes(name))) total += copies * count(child, stack, true);
406
+ }
407
+ return total;
408
+ };
409
+ for (const entry of workflows) {
410
+ const total = count(entry, [], true);
411
+ if (total > 2) findings.push(`${entry.path}: closure starts ${total} Boat jobs, limit 2`);
412
+ }
413
+ return [...new Set(findings)];
414
+ };
415
+ // src/starts.ts
416
+ var StartHeld = class extends Schema.TaggedError()("StartHeld", { line: Line }) {};
417
+ const admitStart = Effect.fn("Starts.admitStart")(function* (request, start, { deadline }) {
418
+ const boat = yield* BoatStarts;
419
+ const merges = yield* Merges;
420
+ let last = {
421
+ _tag: "Unread",
422
+ reason: "start deadline reached"
423
+ };
424
+ for (;;) {
425
+ const remaining = deadline - (yield* Clock.currentTimeMillis);
426
+ if (!Number.isFinite(remaining) || remaining <= 0) return yield* new StartHeld({ line: last });
427
+ const counts = yield* Effect.gen(function* () {
428
+ const plan = yield* boat.plan;
429
+ if (sharedWindows(request, plan).length === 0) return {
430
+ now: yield* Clock.currentTimeMillis,
431
+ plan
432
+ };
433
+ const repo = yield* boat.repoStarts(request.repo).pipe(Effect.catchTag("StartReadError", () => Effect.undefined));
434
+ const history = yield* merges.counts.pipe(Effect.catchTag("StartReadError", () => Effect.undefined));
435
+ return {
436
+ now: yield* Clock.currentTimeMillis,
437
+ plan,
438
+ repo,
439
+ merges: history
440
+ };
441
+ }).pipe(Effect.catchTag("StartReadError", () => Effect.undefined), Effect.timeoutOrElse({
442
+ duration: remaining,
443
+ orElse: () => Effect.fail(new StartHeld({ line: last }))
444
+ }));
445
+ const now = yield* Clock.currentTimeMillis;
446
+ const decision = decide(request, counts, now);
447
+ if (decision._tag === "Hold") return yield* new StartHeld({ line: decision.line });
448
+ if (!Number.isFinite(deadline) || now >= deadline) return yield* new StartHeld({ line: decision._tag === "Wait" ? decision.line : {
449
+ _tag: "Unread",
450
+ reason: "start deadline reached"
451
+ } });
452
+ if (decision._tag === "Admit") return yield* start;
453
+ last = decision.line;
454
+ const window = decision.line._tag === "Account" || decision.line._tag === "Budget" ? decision.line.window : "minute";
455
+ yield* Effect.sleep(Math.max(0, Math.min(decision.until, now + pollMillis[window], deadline) - now));
456
+ }
457
+ });
458
+ export { BOAT_JOBS_PER_RUN, BoatStarts, Merges, Repo, StartHeld, StartReadError, admitStart, boatJobFindings, decide, startRepos };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@milkenjoyer/toolchain",
3
- "version": "0.15.0",
3
+ "version": "0.17.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/birbprophet/agent-kit.git",
@@ -15,6 +15,7 @@
15
15
  },
16
16
  "files": [
17
17
  "dist",
18
+ "STARTS.md",
18
19
  "licenses",
19
20
  "node-version",
20
21
  "renovate.json",
@@ -81,6 +82,10 @@
81
82
  "types": "./dist/rules.d.mts",
82
83
  "default": "./dist/rules.mjs"
83
84
  },
85
+ "./starts": {
86
+ "types": "./dist/starts.d.mts",
87
+ "default": "./dist/starts.mjs"
88
+ },
84
89
  "./versions": {
85
90
  "types": "./dist/versions.d.mts",
86
91
  "default": "./dist/versions.mjs"
@@ -102,6 +107,7 @@
102
107
  "prepack": "vp pack"
103
108
  },
104
109
  "dependencies": {
110
+ "@boatdev/sdk": "1.13.0",
105
111
  "@effect/platform-node": "4.1.2",
106
112
  "@effect/tsgo": "0.67.3",
107
113
  "@foldkit/oxlint-plugin": "0.15.4",
@@ -112,6 +118,7 @@
112
118
  "oxc-parser": "0.157.0",
113
119
  "semver": "7.8.6",
114
120
  "ultracite": "8.1.1",
121
+ "yaml": "2.9.1",
115
122
  "z3-solver": "5.3.3"
116
123
  },
117
124
  "devDependencies": {
@@ -0,0 +1,140 @@
1
+ import { BoatApi, CiJobState, Configuration } from "@boatdev/sdk";
2
+ import * as Clock from "effect/Clock";
3
+ import * as Context from "effect/Context";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Layer from "effect/Layer";
6
+ import * as Redacted from "effect/Redacted";
7
+ import * as Schema from "effect/Schema";
8
+
9
+ import {
10
+ type Plan,
11
+ type Repo,
12
+ type RepoStarts,
13
+ windowMillis,
14
+ } from "./budget.ts";
15
+
16
+ export class StartReadError extends Schema.TaggedError<
17
+ StartReadError,
18
+ { readonly brand: unique symbol }
19
+ >()("StartReadError", { reason: Schema.String }) {}
20
+
21
+ const Count = Schema.Number.check(
22
+ Schema.makeFilter((value) => Number.isSafeInteger(value) && value >= 0)
23
+ );
24
+ const Usage = Schema.Struct({ limit: Count, used: Count });
25
+ const Limits = Schema.Struct({
26
+ ok: Schema.Literal(true),
27
+ starts: Schema.Struct({ minute: Usage, hour: Usage, day: Usage }),
28
+ nextStartDollars: Schema.optional(Schema.NullOr(Schema.Finite)),
29
+ });
30
+ const Ci = Schema.Struct({
31
+ ok: Schema.Literal(true),
32
+ jobs: Schema.Array(
33
+ Schema.Struct({
34
+ repo: Schema.String,
35
+ label: Schema.String,
36
+ state: Schema.String.check(Schema.isMinLength(1)),
37
+ queuedAt: Schema.Date,
38
+ runStartedAt: Schema.optional(Schema.NullOr(Schema.Date)),
39
+ })
40
+ ),
41
+ });
42
+ const settled = new Set<string>([
43
+ CiJobState.Success,
44
+ CiJobState.Failure,
45
+ CiJobState.Cancelled,
46
+ CiJobState.Skipped,
47
+ CiJobState.Failed,
48
+ CiJobState.Refused,
49
+ ]);
50
+
51
+ export class BoatStarts extends Context.Service<
52
+ BoatStarts,
53
+ {
54
+ readonly plan: Effect.Effect<Plan, StartReadError>;
55
+ readonly repoStarts: (
56
+ repo: Repo
57
+ ) => Effect.Effect<RepoStarts, StartReadError>;
58
+ }
59
+ >()("toolchain/BoatStarts") {
60
+ static readonly layer = (
61
+ key: Redacted.Redacted<string>,
62
+ options: {
63
+ readonly fetch?: (
64
+ url: string | URL | Request,
65
+ init?: RequestInit
66
+ ) => Promise<Response>;
67
+ } = {}
68
+ ) =>
69
+ Layer.sync(BoatStarts, () => {
70
+ const api = new BoatApi(
71
+ new Configuration({
72
+ accessToken: Redacted.value(key),
73
+ ...(options.fetch === undefined ? {} : { fetchApi: options.fetch }),
74
+ })
75
+ );
76
+ const plan = Effect.gen(function* () {
77
+ const response = yield* Effect.tryPromise({
78
+ try: () => api.limits(),
79
+ catch: () =>
80
+ new StartReadError({ reason: "Boat limits request failed" }),
81
+ });
82
+ const limits = yield* Schema.decodeUnknownEffect(Limits)(response).pipe(
83
+ Effect.mapError(
84
+ () => new StartReadError({ reason: "Boat limits unread" })
85
+ )
86
+ );
87
+ return {
88
+ limits: {
89
+ minute: limits.starts.minute.limit,
90
+ hour: limits.starts.hour.limit,
91
+ day: limits.starts.day.limit,
92
+ },
93
+ used: {
94
+ minute: limits.starts.minute.used,
95
+ hour: limits.starts.hour.used,
96
+ day: limits.starts.day.used,
97
+ },
98
+ nextStartDollars: limits.nextStartDollars ?? undefined,
99
+ };
100
+ });
101
+ const repoStarts = Effect.fn("BoatStarts.repoStarts")(function* (
102
+ repo: Repo
103
+ ) {
104
+ const response = yield* Effect.tryPromise({
105
+ try: () => api.getCiOverview({ repo, limit: 500 }),
106
+ catch: () => new StartReadError({ reason: "Boat CI request failed" }),
107
+ });
108
+ const now = yield* Clock.currentTimeMillis;
109
+ const { jobs } = yield* Schema.decodeUnknownEffect(Ci)(response).pipe(
110
+ Effect.mapError(
111
+ () => new StartReadError({ reason: "Boat CI history unread" })
112
+ )
113
+ );
114
+ if (jobs.some((job) => job.repo !== repo))
115
+ return yield* new StartReadError({
116
+ reason: "Boat CI history differs from request",
117
+ });
118
+ const oldest = Math.min(
119
+ ...jobs.map((job) =>
120
+ Math.max(job.queuedAt.getTime(), job.runStartedAt?.getTime() ?? 0)
121
+ )
122
+ );
123
+ return {
124
+ repo,
125
+ starts: jobs.flatMap((job) => {
126
+ const at =
127
+ job.runStartedAt?.getTime() ??
128
+ (settled.has(job.state) ? undefined : now);
129
+ return job.label.split("/")[0]?.startsWith("boat") === true &&
130
+ at !== undefined &&
131
+ at > now - windowMillis.day
132
+ ? [Math.min(at, now)]
133
+ : [];
134
+ }),
135
+ complete: jobs.length < 500 || oldest <= now - windowMillis.day,
136
+ };
137
+ });
138
+ return BoatStarts.of({ plan, repoStarts });
139
+ });
140
+ }
@@ -0,0 +1,224 @@
1
+ import * as Schema from "effect/Schema";
2
+
3
+ export const Repo = Schema.String.check(
4
+ Schema.isPattern(/^[\w.-]+\/[\w.-]+$/u)
5
+ ).pipe(Schema.brand("Repo"));
6
+ export type Repo = typeof Repo.Type;
7
+ export type Window = "minute" | "hour" | "day";
8
+ export type PerWindow<A> = { readonly [W in Window]: A };
9
+ export type Priority = "ordinary" | "p0";
10
+ export type MergeCounts = {
11
+ readonly repo: Repo;
12
+ readonly h6: number;
13
+ readonly h24: number;
14
+ readonly d7: number;
15
+ };
16
+ export type Plan = {
17
+ readonly limits: PerWindow<number>;
18
+ readonly used: PerWindow<number>;
19
+ readonly nextStartDollars: number | undefined;
20
+ };
21
+ export type RepoStarts = {
22
+ readonly repo: Repo;
23
+ readonly starts: ReadonlyArray<number>;
24
+ readonly complete: boolean;
25
+ };
26
+ export type Counts = {
27
+ readonly now: number;
28
+ readonly plan: Plan;
29
+ readonly repo?: RepoStarts | undefined;
30
+ readonly merges?: ReadonlyArray<MergeCounts> | undefined;
31
+ };
32
+ export type StartRequest = {
33
+ readonly repo: Repo;
34
+ readonly jobs: number;
35
+ readonly priority: Priority;
36
+ };
37
+
38
+ const Window = Schema.Literals(["minute", "hour", "day"]);
39
+ export const Line = Schema.Union([
40
+ Schema.TaggedStruct("Unread", { reason: Schema.String }),
41
+ Schema.TaggedStruct("Paid", {
42
+ nextStartDollars: Schema.UndefinedOr(Schema.Number),
43
+ }),
44
+ Schema.TaggedStruct("Account", {
45
+ window: Window,
46
+ used: Schema.Number,
47
+ line: Schema.Number,
48
+ }),
49
+ Schema.TaggedStruct("Budget", {
50
+ repo: Repo,
51
+ window: Window,
52
+ used: Schema.Number,
53
+ budget: Schema.Number,
54
+ }),
55
+ ]);
56
+ export type Line = typeof Line.Type;
57
+ export type Admission =
58
+ | { readonly _tag: "Admit" }
59
+ | { readonly _tag: "Wait"; readonly until: number; readonly line: Line }
60
+ | { readonly _tag: "Hold"; readonly line: Line };
61
+
62
+ const windows: ReadonlyArray<Window> = ["minute", "hour", "day"];
63
+ export const windowMillis: PerWindow<number> = {
64
+ minute: 60_000,
65
+ hour: 3_600_000,
66
+ day: 86_400_000,
67
+ };
68
+ export const pollMillis: PerWindow<number> = {
69
+ minute: 15_000,
70
+ hour: 60_000,
71
+ day: 300_000,
72
+ };
73
+
74
+ export const startRepos: ReadonlyArray<Repo> = [
75
+ Repo.make("birbprophet/agent-kit"),
76
+ Repo.make("siglata/siglata"),
77
+ Repo.make("optionhouse-xyz/optionhouse"),
78
+ Repo.make("birbprophet/vibebattler"),
79
+ ];
80
+
81
+ const count = (value: number) => Number.isSafeInteger(value) && value >= 0;
82
+ const ceiling = (request: StartRequest, plan: Plan, window: Window) =>
83
+ Number(
84
+ (BigInt(plan.limits[window]) * (request.priority === "p0" ? 95n : 80n)) /
85
+ 100n
86
+ );
87
+
88
+ export const sharedWindows = (
89
+ request: StartRequest,
90
+ plan: Plan
91
+ ): ReadonlyArray<Window> =>
92
+ request.priority === "p0" ||
93
+ plan.nextStartDollars !== 0 ||
94
+ !windows.every(
95
+ (window) => count(plan.limits[window]) && count(plan.used[window])
96
+ ) ||
97
+ windows.some(
98
+ (window) =>
99
+ plan.used[window] + request.jobs > ceiling(request, plan, window)
100
+ )
101
+ ? []
102
+ : windows.filter((window) => 2 * plan.used[window] >= plan.limits[window]);
103
+
104
+ const liveShare = (
105
+ repo: Repo,
106
+ merges: Counts["merges"]
107
+ ): { readonly numerator: bigint; readonly denominator: bigint } | undefined => {
108
+ if (
109
+ merges === undefined ||
110
+ merges.length !== startRepos.length ||
111
+ !startRepos.every(
112
+ (repo) => merges.filter((row) => row.repo === repo).length === 1
113
+ ) ||
114
+ !merges.every((row) => [row.h6, row.h24, row.d7].every(count))
115
+ )
116
+ return undefined;
117
+ const weights = merges.map((row) => ({
118
+ repo: row.repo,
119
+ weight: [7n * BigInt(row.h24), 28n * BigInt(row.h6), BigInt(row.d7)].reduce(
120
+ (largest, value) => (value > largest ? value : largest),
121
+ 0n
122
+ ),
123
+ }));
124
+ const total = weights.reduce((sum, row) => sum + row.weight, 0n);
125
+ const weight = weights.find((row) => row.repo === repo)?.weight;
126
+ return weight === undefined
127
+ ? undefined
128
+ : total === 0n
129
+ ? { numerator: 1n, denominator: 4n }
130
+ : { numerator: total + 6n * weight, denominator: 10n * total };
131
+ };
132
+ const unread = (reason: string): Admission => ({
133
+ _tag: "Hold",
134
+ line: { _tag: "Unread", reason },
135
+ });
136
+
137
+ export const decide = (
138
+ request: StartRequest,
139
+ counts: Counts | undefined,
140
+ now: number
141
+ ): Admission => {
142
+ if (!startRepos.includes(request.repo))
143
+ return unread("repo has no start budget");
144
+ if (!count(request.jobs) || request.jobs === 0)
145
+ return unread("jobs must be a positive safe integer");
146
+ if (!Number.isFinite(now)) return unread("start time unread");
147
+ if (counts === undefined)
148
+ return {
149
+ _tag: "Wait",
150
+ until: now + pollMillis.minute,
151
+ line: { _tag: "Unread", reason: "start plan unread" },
152
+ };
153
+ if (
154
+ !Number.isFinite(counts.now) ||
155
+ counts.now > now ||
156
+ !windows.every(
157
+ (window) =>
158
+ count(counts.plan.limits[window]) && count(counts.plan.used[window])
159
+ )
160
+ )
161
+ return unread("start plan unread");
162
+ const { plan } = counts;
163
+ if (plan.nextStartDollars !== 0)
164
+ return {
165
+ _tag: "Wait",
166
+ until: now + pollMillis.minute,
167
+ line: { _tag: "Paid", nextStartDollars: plan.nextStartDollars },
168
+ };
169
+ for (const window of windows) {
170
+ const used = plan.used[window];
171
+ const line = ceiling(request, plan, window);
172
+ if (used + request.jobs > line)
173
+ return window === "day"
174
+ ? { _tag: "Hold", line: { _tag: "Account", window, used, line } }
175
+ : {
176
+ _tag: "Wait",
177
+ until: now + pollMillis[window],
178
+ line: { _tag: "Account", window, used, line },
179
+ };
180
+ }
181
+ const shared = sharedWindows(request, plan);
182
+ if (shared.length === 0) return { _tag: "Admit" };
183
+ if (counts.repo === undefined || !counts.repo.complete)
184
+ return {
185
+ _tag: "Wait",
186
+ until: now + pollMillis.minute,
187
+ line: { _tag: "Unread", reason: "repo start history incomplete" },
188
+ };
189
+ if (
190
+ counts.repo.repo !== request.repo ||
191
+ !counts.repo.starts.every((at) => Number.isFinite(at) && at <= counts.now)
192
+ )
193
+ return unread("repo start history incomplete");
194
+ const share = liveShare(request.repo, counts.merges);
195
+ if (share === undefined)
196
+ return {
197
+ _tag: "Wait",
198
+ until: now + pollMillis.minute,
199
+ line: { _tag: "Unread", reason: "merge history incomplete" },
200
+ };
201
+ for (const window of shared) {
202
+ const starts = counts.repo.starts
203
+ .filter((at) => at > now - windowMillis[window])
204
+ .toSorted((left, right) => left - right);
205
+ const used = starts.length;
206
+ const budget = Number(
207
+ (share.numerator * 4n * BigInt(plan.limits[window])) /
208
+ (share.denominator * 5n)
209
+ );
210
+ if (used + request.jobs <= budget) continue;
211
+ const line: Line = {
212
+ _tag: "Budget",
213
+ repo: request.repo,
214
+ window,
215
+ used,
216
+ budget,
217
+ };
218
+ const expires = starts[used + request.jobs - budget - 1];
219
+ return request.jobs > budget || expires === undefined
220
+ ? { _tag: "Hold", line }
221
+ : { _tag: "Wait", until: expires + windowMillis[window], line };
222
+ }
223
+ return { _tag: "Admit" };
224
+ };
@@ -0,0 +1,99 @@
1
+ import { Octokit } from "@octokit/rest";
2
+ import * as Clock from "effect/Clock";
3
+ import * as Context from "effect/Context";
4
+ import * as Effect from "effect/Effect";
5
+ import * as Layer from "effect/Layer";
6
+ import * as Redacted from "effect/Redacted";
7
+ import * as Schema from "effect/Schema";
8
+
9
+ import { StartReadError } from "./boat.ts";
10
+ import { type MergeCounts, startRepos } from "./budget.ts";
11
+
12
+ const Count = Schema.Struct({
13
+ issueCount: Schema.Number.check(
14
+ Schema.makeFilter((value) => Number.isSafeInteger(value) && value >= 0)
15
+ ),
16
+ });
17
+ const windows = { h6: 21_600_000, h24: 86_400_000, d7: 604_800_000 };
18
+
19
+ export class Merges extends Context.Service<
20
+ Merges,
21
+ { readonly counts: Effect.Effect<ReadonlyArray<MergeCounts>, StartReadError> }
22
+ >()("toolchain/Merges") {
23
+ static readonly layer = (
24
+ token: Redacted.Redacted<string>,
25
+ options: {
26
+ readonly fetch?: (
27
+ url: string | URL | Request,
28
+ init?: RequestInit
29
+ ) => Promise<Response>;
30
+ } = {}
31
+ ) =>
32
+ Layer.sync(Merges, () => {
33
+ const api = new Octokit({
34
+ auth: Redacted.value(token),
35
+ ...(options.fetch === undefined
36
+ ? {}
37
+ : { request: { fetch: options.fetch } }),
38
+ });
39
+ const counts = Effect.gen(function* () {
40
+ const now = yield* Clock.currentTimeMillis;
41
+ const query = `query { ${startRepos
42
+ .flatMap((repo, index) => {
43
+ const [owner, name] = repo.split("/");
44
+ return [
45
+ `repo${index}: repository(owner: "${owner}", name: "${name}") { nameWithOwner }`,
46
+ ...Object.entries(windows).map(([window, millis]) => {
47
+ const since = new Date(now - millis)
48
+ .toISOString()
49
+ .replace(/\.\d{3}Z$/u, "Z");
50
+ return `${window}_${index}: search(type: ISSUE, first: 0, query: "repo:${repo} is:pr is:merged base:main merged:>=${since}") { issueCount }`;
51
+ }),
52
+ ];
53
+ })
54
+ .join(" ")} }`;
55
+ const response = yield* Effect.tryPromise({
56
+ try: () => api.graphql<unknown>(query),
57
+ catch: () =>
58
+ new StartReadError({ reason: "GitHub merges request failed" }),
59
+ });
60
+ return yield* Schema.decodeUnknownEffect(
61
+ Schema.Record(Schema.String, Schema.Unknown)
62
+ )(response).pipe(
63
+ Effect.flatMap((data) =>
64
+ Effect.forEach(
65
+ startRepos,
66
+ (repo, index) =>
67
+ Schema.decodeUnknownEffect(
68
+ Schema.Struct({
69
+ repository: Schema.Struct({
70
+ nameWithOwner: Schema.Literal(repo),
71
+ }),
72
+ h6: Count,
73
+ h24: Count,
74
+ d7: Count,
75
+ })
76
+ )({
77
+ repository: data[`repo${index}`],
78
+ h6: data[`h6_${index}`],
79
+ h24: data[`h24_${index}`],
80
+ d7: data[`d7_${index}`],
81
+ }).pipe(
82
+ Effect.map((row) => ({
83
+ repo,
84
+ h6: row.h6.issueCount,
85
+ h24: row.h24.issueCount,
86
+ d7: row.d7.issueCount,
87
+ }))
88
+ ),
89
+ { concurrency: 1 }
90
+ )
91
+ ),
92
+ Effect.mapError(
93
+ () => new StartReadError({ reason: "GitHub merges unread" })
94
+ )
95
+ );
96
+ });
97
+ return Merges.of({ counts });
98
+ });
99
+ }
@@ -0,0 +1,171 @@
1
+ import * as Schema from "effect/Schema";
2
+ import { parse } from "yaml";
3
+
4
+ export const BOAT_JOBS_PER_RUN = 2;
5
+
6
+ const Matrix = Schema.Record(Schema.String, Schema.Array(Schema.Unknown));
7
+ const Workflow = Schema.Struct({
8
+ name: Schema.optionalKey(Schema.String),
9
+ on: Schema.optionalKey(
10
+ Schema.Union([
11
+ Schema.String,
12
+ Schema.Array(Schema.String),
13
+ Schema.Record(Schema.String, Schema.Unknown),
14
+ ])
15
+ ),
16
+ jobs: Schema.Record(
17
+ Schema.String,
18
+ Schema.Struct({
19
+ "runs-on": Schema.optionalKey(
20
+ Schema.Union([Schema.String, Schema.Array(Schema.String)])
21
+ ),
22
+ uses: Schema.optionalKey(Schema.String),
23
+ strategy: Schema.optionalKey(
24
+ Schema.Struct({
25
+ matrix: Schema.optionalKey(Schema.Union([Schema.String, Matrix])),
26
+ })
27
+ ),
28
+ })
29
+ ),
30
+ });
31
+ const Descendant = Schema.Struct({
32
+ workflow_run: Schema.Struct({
33
+ workflows: Schema.Union([Schema.String, Schema.Array(Schema.String)]),
34
+ types: Schema.optionalKey(
35
+ Schema.Union([Schema.String, Schema.Array(Schema.String)])
36
+ ),
37
+ }),
38
+ });
39
+ type WorkflowFile = readonly [path: string, text: string];
40
+ type Parsed = {
41
+ readonly path: string;
42
+ readonly workflow: typeof Workflow.Type;
43
+ };
44
+
45
+ const dynamic = (value: unknown): boolean => {
46
+ try {
47
+ return JSON.stringify(value).includes("${{");
48
+ } catch {
49
+ return true;
50
+ }
51
+ };
52
+
53
+ export const boatJobFindings = (
54
+ files: ReadonlyArray<WorkflowFile>
55
+ ): ReadonlyArray<string> => {
56
+ const findings: string[] = [];
57
+ const workflows: Parsed[] = [];
58
+ for (const [path, text] of files) {
59
+ try {
60
+ workflows.push({
61
+ path,
62
+ workflow: Schema.decodeUnknownSync(Workflow)(parse(text)),
63
+ });
64
+ } catch {
65
+ findings.push(`${path}: workflow unread`);
66
+ }
67
+ }
68
+ const after = (
69
+ entry: Parsed
70
+ ): { readonly names: ReadonlyArray<string>; readonly copies: number } => {
71
+ const events = entry.workflow.on;
72
+ if (!Schema.is(Descendant)(events)) {
73
+ if (typeof events === "object" && "workflow_run" in events)
74
+ findings.push(`${entry.path}: workflow_run unread`);
75
+ return { names: [], copies: 0 };
76
+ }
77
+ const names = events.workflow_run.workflows;
78
+ const selectors = typeof names === "string" ? [names] : names;
79
+ if (selectors.some((name) => /[*!?[\]]/u.test(name)))
80
+ findings.push(`${entry.path}: workflow_run pattern unresolved`);
81
+ const types = events.workflow_run.types;
82
+ if (types !== undefined && dynamic(types))
83
+ findings.push(`${entry.path}: dynamic workflow_run types`);
84
+ return {
85
+ names: selectors,
86
+ copies:
87
+ types === undefined
88
+ ? 3
89
+ : new Set(typeof types === "string" ? [types] : types).size,
90
+ };
91
+ };
92
+ const count = (
93
+ entry: Parsed,
94
+ parents: ReadonlyArray<string>,
95
+ descendants: boolean
96
+ ): number => {
97
+ if (parents.includes(entry.path)) {
98
+ findings.push(`${entry.path}: workflow cycle`);
99
+ return 0;
100
+ }
101
+ const stack = [...parents, entry.path];
102
+ let total = 0;
103
+ for (const [name, job] of Object.entries(entry.workflow.jobs)) {
104
+ const label = `${entry.path} ${name}`;
105
+ const matrix = job.strategy?.matrix;
106
+ let copies = 1;
107
+ if (matrix !== undefined) {
108
+ if (typeof matrix === "string" || dynamic(matrix)) {
109
+ findings.push(`${label}: dynamic matrix`);
110
+ } else {
111
+ const axes = Object.entries(matrix).filter(
112
+ ([axis]) => axis !== "include" && axis !== "exclude"
113
+ );
114
+ copies =
115
+ (axes.length === 0
116
+ ? 0
117
+ : axes.reduce(
118
+ (product, [, values]) => product * values.length,
119
+ 1
120
+ )) + (matrix.include?.length ?? 0);
121
+ }
122
+ }
123
+ if (job.uses !== undefined) {
124
+ if (!/^\.\/\.github\/workflows\/[^/]+\.ya?ml$/u.test(job.uses)) {
125
+ findings.push(`${label}: remote or unresolved reusable workflow`);
126
+ continue;
127
+ }
128
+ const target = job.uses.slice(2);
129
+ const called = workflows.find(
130
+ ({ path }) =>
131
+ path === target || `.github/workflows/${path}` === target
132
+ );
133
+ if (called === undefined)
134
+ findings.push(`${label}: reusable workflow ${target} unread`);
135
+ else total += copies * count(called, stack, false);
136
+ } else if (job["runs-on"] === undefined || dynamic(job["runs-on"])) {
137
+ findings.push(`${label}: dynamic or unread runs-on`);
138
+ } else {
139
+ const runners = job["runs-on"];
140
+ if (
141
+ (typeof runners === "string" ? [runners] : runners).some(
142
+ (runner) => runner.split("/")[0]?.startsWith("boat") === true
143
+ )
144
+ )
145
+ total += copies;
146
+ }
147
+ }
148
+ if (descendants)
149
+ for (const child of workflows) {
150
+ const { names, copies } = after(child);
151
+ if (dynamic(names))
152
+ findings.push(`${child.path}: dynamic workflow_run`);
153
+ const file = entry.path.split("/").at(-1) ?? entry.path;
154
+ const rootNames =
155
+ entry.workflow.name === undefined
156
+ ? [entry.path, file, `.github/workflows/${file}`]
157
+ : [entry.workflow.name];
158
+ if (rootNames.some((name) => names.includes(name)))
159
+ total += copies * count(child, stack, true);
160
+ }
161
+ return total;
162
+ };
163
+ for (const entry of workflows) {
164
+ const total = count(entry, [], true);
165
+ if (total > BOAT_JOBS_PER_RUN)
166
+ findings.push(
167
+ `${entry.path}: closure starts ${total} Boat jobs, limit ${BOAT_JOBS_PER_RUN}`
168
+ );
169
+ }
170
+ return [...new Set(findings)];
171
+ };
package/src/starts.ts ADDED
@@ -0,0 +1,101 @@
1
+ import * as Clock from "effect/Clock";
2
+ import * as Effect from "effect/Effect";
3
+ import * as Schema from "effect/Schema";
4
+
5
+ import { BoatStarts } from "./starts/boat.ts";
6
+ import {
7
+ decide,
8
+ Line,
9
+ pollMillis,
10
+ sharedWindows,
11
+ type StartRequest,
12
+ } from "./starts/budget.ts";
13
+ import { Merges } from "./starts/merges.ts";
14
+
15
+ export { BoatStarts, StartReadError } from "./starts/boat.ts";
16
+ export { decide, Repo, startRepos } from "./starts/budget.ts";
17
+ export { Merges } from "./starts/merges.ts";
18
+ export type {
19
+ Admission,
20
+ Counts,
21
+ MergeCounts,
22
+ PerWindow,
23
+ Plan,
24
+ Priority,
25
+ RepoStarts,
26
+ StartRequest,
27
+ Window,
28
+ } from "./starts/budget.ts";
29
+ export { boatJobFindings, BOAT_JOBS_PER_RUN } from "./starts/policy.ts";
30
+ export type { Line } from "./starts/budget.ts";
31
+
32
+ export class StartHeld extends Schema.TaggedError<
33
+ StartHeld,
34
+ { readonly brand: unique symbol }
35
+ >()("StartHeld", {
36
+ line: Line,
37
+ }) {}
38
+
39
+ export const admitStart = Effect.fn("Starts.admitStart")(function* <A, E, R>(
40
+ request: StartRequest,
41
+ start: Effect.Effect<A, E, R>,
42
+ { deadline }: { readonly deadline: number }
43
+ ) {
44
+ const boat = yield* BoatStarts;
45
+ const merges = yield* Merges;
46
+ let last: Line = {
47
+ _tag: "Unread",
48
+ reason: "start deadline reached",
49
+ };
50
+ for (;;) {
51
+ const remaining = deadline - (yield* Clock.currentTimeMillis);
52
+ if (!Number.isFinite(remaining) || remaining <= 0)
53
+ return yield* new StartHeld({ line: last });
54
+ const counts = yield* Effect.gen(function* () {
55
+ const plan = yield* boat.plan;
56
+ if (sharedWindows(request, plan).length === 0)
57
+ return { now: yield* Clock.currentTimeMillis, plan };
58
+ const repo = yield* boat
59
+ .repoStarts(request.repo)
60
+ .pipe(Effect.catchTag("StartReadError", () => Effect.undefined));
61
+ const history = yield* merges.counts.pipe(
62
+ Effect.catchTag("StartReadError", () => Effect.undefined)
63
+ );
64
+ return {
65
+ now: yield* Clock.currentTimeMillis,
66
+ plan,
67
+ repo,
68
+ merges: history,
69
+ };
70
+ }).pipe(
71
+ Effect.catchTag("StartReadError", () => Effect.undefined),
72
+ Effect.timeoutOrElse({
73
+ duration: remaining,
74
+ orElse: () => Effect.fail(new StartHeld({ line: last })),
75
+ })
76
+ );
77
+ const now = yield* Clock.currentTimeMillis;
78
+ const decision = decide(request, counts, now);
79
+ if (decision._tag === "Hold")
80
+ return yield* new StartHeld({ line: decision.line });
81
+ if (!Number.isFinite(deadline) || now >= deadline)
82
+ return yield* new StartHeld({
83
+ line:
84
+ decision._tag === "Wait"
85
+ ? decision.line
86
+ : { _tag: "Unread", reason: "start deadline reached" },
87
+ });
88
+ if (decision._tag === "Admit") return yield* start;
89
+ last = decision.line;
90
+ const window =
91
+ decision.line._tag === "Account" || decision.line._tag === "Budget"
92
+ ? decision.line.window
93
+ : "minute";
94
+ yield* Effect.sleep(
95
+ Math.max(
96
+ 0,
97
+ Math.min(decision.until, now + pollMillis[window], deadline) - now
98
+ )
99
+ );
100
+ }
101
+ });