@milkenjoyer/toolchain 0.19.0 → 0.20.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/STARTS.md +5 -13
- package/dist/starts.d.mts +1 -110
- package/dist/starts.mjs +1 -349
- package/package.json +1 -2
- package/src/starts.ts +0 -100
- package/src/starts/boat.ts +0 -144
- package/src/starts/budget.ts +0 -224
- package/src/starts/merges.ts +0 -99
package/STARTS.md
CHANGED
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
# Boat
|
|
1
|
+
# Boat workflow policy
|
|
2
2
|
|
|
3
|
-
Pin `@milkenjoyer/toolchain` to `0.
|
|
3
|
+
Pin `@milkenjoyer/toolchain` to `0.20.0`. Import `boatJobFindings` and `BOAT_JOBS_PER_RUN` from `@milkenjoyer/toolchain/starts`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Pass every workflow as a `[path, text]` pair and add the returned findings to the repository's workflow policy. Each closure has at most two Boat jobs, 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 or unresolved graphs produce findings. Keep runner, timeout and concurrency checks.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
`boatJobFindings` reports Boat jobs that start on `pull_request`, `pull_request_target` or PR-branch push. A `pull_request` trigger with a non-empty `types` list containing only `labeled` is exempt. Agent-requested CI uses that label event on the proven head.
|
|
8
8
|
|
|
9
|
-
|
|
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 or gives it a `finishedAt`. States the SDK does not list, such as `paused`, follow the same rule. A paused job that GitHub cancelled stops counting once Boat records its end. 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
|
-
`boatJobFindings` also reports a workflow whose Boat jobs start on a `pull_request`, `pull_request_target` or PR-branch push. A `pull_request` trigger with a non-empty `types` list containing only `labeled` is exempt. Every other PR trigger stays flagged. A product repo moves PR `ready` to `pull_request` `labeled` with the pin move that brings this rule. Product repos adopt the published pin instead of copying files. Boat reports no start spend, so nothing here proves zero paid starts.
|
|
9
|
+
CI requesting and its monthly spend cap use the Boat SDK directly. Direct probe creates and resumes require an explicit free next-start quote through the SDK. The starts module supplies workflow validation.
|
package/dist/starts.d.mts
CHANGED
|
@@ -1,113 +1,4 @@
|
|
|
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
1
|
export declare const BOAT_JOBS_PER_RUN = 2;
|
|
89
2
|
type WorkflowFile = readonly [path: string, text: string];
|
|
90
3
|
export declare const boatJobFindings: (files: ReadonlyArray<WorkflowFile>) => ReadonlyArray<string>;
|
|
91
|
-
|
|
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 };
|
|
4
|
+
export {};
|
package/dist/starts.mjs
CHANGED
|
@@ -1,310 +1,5 @@
|
|
|
1
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
2
|
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
|
-
finishedAt: Schema.optional(Schema.NullOr(Schema.Date))
|
|
200
|
-
}))
|
|
201
|
-
});
|
|
202
|
-
const settled = /* @__PURE__ */ new Set([
|
|
203
|
-
CiJobState.Success,
|
|
204
|
-
CiJobState.Failure,
|
|
205
|
-
CiJobState.Cancelled,
|
|
206
|
-
CiJobState.Skipped,
|
|
207
|
-
CiJobState.Failed,
|
|
208
|
-
CiJobState.Refused
|
|
209
|
-
]);
|
|
210
|
-
var BoatStarts = class BoatStarts extends Context.Service()("toolchain/BoatStarts") {
|
|
211
|
-
static layer = (key, options = {}) => Layer.sync(BoatStarts, () => {
|
|
212
|
-
const api = new BoatApi(new Configuration({
|
|
213
|
-
accessToken: Redacted.value(key),
|
|
214
|
-
...options.fetch === void 0 ? {} : { fetchApi: options.fetch }
|
|
215
|
-
}));
|
|
216
|
-
const plan = Effect.gen(function* () {
|
|
217
|
-
const response = yield* Effect.tryPromise({
|
|
218
|
-
try: () => api.limits(),
|
|
219
|
-
catch: () => new StartReadError({ reason: "Boat limits request failed" })
|
|
220
|
-
});
|
|
221
|
-
const limits = yield* Schema.decodeUnknownEffect(Limits)(response).pipe(Effect.mapError(() => new StartReadError({ reason: "Boat limits unread" })));
|
|
222
|
-
return {
|
|
223
|
-
limits: {
|
|
224
|
-
minute: limits.starts.minute.limit,
|
|
225
|
-
hour: limits.starts.hour.limit,
|
|
226
|
-
day: limits.starts.day.limit
|
|
227
|
-
},
|
|
228
|
-
used: {
|
|
229
|
-
minute: limits.starts.minute.used,
|
|
230
|
-
hour: limits.starts.hour.used,
|
|
231
|
-
day: limits.starts.day.used
|
|
232
|
-
},
|
|
233
|
-
nextStartDollars: limits.nextStartDollars ?? void 0
|
|
234
|
-
};
|
|
235
|
-
});
|
|
236
|
-
const repoStarts = Effect.fn("BoatStarts.repoStarts")(function* (repo) {
|
|
237
|
-
const response = yield* Effect.tryPromise({
|
|
238
|
-
try: () => api.getCiOverview({
|
|
239
|
-
repo,
|
|
240
|
-
limit: 500
|
|
241
|
-
}),
|
|
242
|
-
catch: () => new StartReadError({ reason: "Boat CI request failed" })
|
|
243
|
-
});
|
|
244
|
-
const now = yield* Clock.currentTimeMillis;
|
|
245
|
-
const { jobs } = yield* Schema.decodeUnknownEffect(Ci)(response).pipe(Effect.mapError(() => new StartReadError({ reason: "Boat CI history unread" })));
|
|
246
|
-
if (jobs.some((job) => job.repo !== repo)) return yield* new StartReadError({ reason: "Boat CI history differs from request" });
|
|
247
|
-
const oldest = Math.min(...jobs.map((job) => Math.max(job.queuedAt.getTime(), job.runStartedAt?.getTime() ?? 0)));
|
|
248
|
-
return {
|
|
249
|
-
repo,
|
|
250
|
-
starts: jobs.flatMap((job) => {
|
|
251
|
-
const at = job.runStartedAt?.getTime() ?? (settled.has(job.state) || job.finishedAt !== void 0 && job.finishedAt !== null ? void 0 : now);
|
|
252
|
-
return job.label.split("/")[0]?.startsWith("boat") === true && at !== void 0 && at > now - windowMillis.day ? [Math.min(at, now)] : [];
|
|
253
|
-
}),
|
|
254
|
-
complete: jobs.length < 500 || oldest <= now - windowMillis.day
|
|
255
|
-
};
|
|
256
|
-
});
|
|
257
|
-
return BoatStarts.of({
|
|
258
|
-
plan,
|
|
259
|
-
repoStarts
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
};
|
|
263
|
-
// src/starts/merges.ts
|
|
264
|
-
const Count = Schema.Struct({ issueCount: Schema.Number.check(Schema.makeFilter((value) => Number.isSafeInteger(value) && value >= 0)) });
|
|
265
|
-
const windows = {
|
|
266
|
-
h6: 216e5,
|
|
267
|
-
h24: 864e5,
|
|
268
|
-
d7: 6048e5
|
|
269
|
-
};
|
|
270
|
-
var Merges = class Merges extends Context.Service()("toolchain/Merges") {
|
|
271
|
-
static layer = (token, options = {}) => Layer.sync(Merges, () => {
|
|
272
|
-
const api = new Octokit({
|
|
273
|
-
auth: Redacted.value(token),
|
|
274
|
-
...options.fetch === void 0 ? {} : { request: { fetch: options.fetch } }
|
|
275
|
-
});
|
|
276
|
-
const counts = Effect.gen(function* () {
|
|
277
|
-
const now = yield* Clock.currentTimeMillis;
|
|
278
|
-
const query = `query { ${startRepos.flatMap((repo, index) => {
|
|
279
|
-
const [owner, name] = repo.split("/");
|
|
280
|
-
return [`repo${index}: repository(owner: "${owner}", name: "${name}") { nameWithOwner }`, ...Object.entries(windows).map(([window, millis]) => {
|
|
281
|
-
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 }`;
|
|
282
|
-
})];
|
|
283
|
-
}).join(" ")} }`;
|
|
284
|
-
const response = yield* Effect.tryPromise({
|
|
285
|
-
try: () => api.graphql(query),
|
|
286
|
-
catch: () => new StartReadError({ reason: "GitHub merges request failed" })
|
|
287
|
-
});
|
|
288
|
-
return yield* Schema.decodeUnknownEffect(Schema.Record(Schema.String, Schema.Unknown))(response).pipe(Effect.flatMap((data) => Effect.forEach(startRepos, (repo, index) => Schema.decodeUnknownEffect(Schema.Struct({
|
|
289
|
-
repository: Schema.Struct({ nameWithOwner: Schema.Literal(repo) }),
|
|
290
|
-
h6: Count,
|
|
291
|
-
h24: Count,
|
|
292
|
-
d7: Count
|
|
293
|
-
}))({
|
|
294
|
-
repository: data[`repo${index}`],
|
|
295
|
-
h6: data[`h6_${index}`],
|
|
296
|
-
h24: data[`h24_${index}`],
|
|
297
|
-
d7: data[`d7_${index}`]
|
|
298
|
-
}).pipe(Effect.map((row) => ({
|
|
299
|
-
repo,
|
|
300
|
-
h6: row.h6.issueCount,
|
|
301
|
-
h24: row.h24.issueCount,
|
|
302
|
-
d7: row.d7.issueCount
|
|
303
|
-
}))), { concurrency: 1 })), Effect.mapError(() => new StartReadError({ reason: "GitHub merges unread" })));
|
|
304
|
-
});
|
|
305
|
-
return Merges.of({ counts });
|
|
306
|
-
});
|
|
307
|
-
};
|
|
308
3
|
// src/starts/policy.ts
|
|
309
4
|
const BOAT_JOBS_PER_RUN = 2;
|
|
310
5
|
const Matrix = Schema.Record(Schema.String, Schema.Array(Schema.Unknown));
|
|
@@ -432,47 +127,4 @@ const boatJobFindings = (files) => {
|
|
|
432
127
|
}
|
|
433
128
|
return [...new Set(findings)];
|
|
434
129
|
};
|
|
435
|
-
|
|
436
|
-
var StartHeld = class extends Schema.TaggedError()("StartHeld", { line: Line }) {};
|
|
437
|
-
const admitStart = Effect.fn("Starts.admitStart")(function* (request, start, { deadline }) {
|
|
438
|
-
const boat = yield* BoatStarts;
|
|
439
|
-
const merges = yield* Merges;
|
|
440
|
-
let last = {
|
|
441
|
-
_tag: "Unread",
|
|
442
|
-
reason: "start deadline reached"
|
|
443
|
-
};
|
|
444
|
-
for (;;) {
|
|
445
|
-
const remaining = deadline - (yield* Clock.currentTimeMillis);
|
|
446
|
-
if (!Number.isFinite(remaining) || remaining <= 0) return yield* new StartHeld({ line: last });
|
|
447
|
-
const counts = yield* Effect.gen(function* () {
|
|
448
|
-
const plan = yield* boat.plan;
|
|
449
|
-
if (sharedWindows(request, plan).length === 0) return {
|
|
450
|
-
now: yield* Clock.currentTimeMillis,
|
|
451
|
-
plan
|
|
452
|
-
};
|
|
453
|
-
const repo = yield* boat.repoStarts(request.repo).pipe(Effect.catchTag("StartReadError", () => Effect.undefined));
|
|
454
|
-
const history = yield* merges.counts.pipe(Effect.catchTag("StartReadError", () => Effect.undefined));
|
|
455
|
-
return {
|
|
456
|
-
now: yield* Clock.currentTimeMillis,
|
|
457
|
-
plan,
|
|
458
|
-
repo,
|
|
459
|
-
merges: history
|
|
460
|
-
};
|
|
461
|
-
}).pipe(Effect.catchTag("StartReadError", () => Effect.undefined), Effect.timeoutOrElse({
|
|
462
|
-
duration: remaining,
|
|
463
|
-
orElse: () => Effect.fail(new StartHeld({ line: last }))
|
|
464
|
-
}));
|
|
465
|
-
const now = yield* Clock.currentTimeMillis;
|
|
466
|
-
const decision = decide(request, counts, now);
|
|
467
|
-
if (decision._tag === "Hold") return yield* new StartHeld({ line: decision.line });
|
|
468
|
-
if (!Number.isFinite(deadline) || now >= deadline) return yield* new StartHeld({ line: decision._tag === "Wait" ? decision.line : {
|
|
469
|
-
_tag: "Unread",
|
|
470
|
-
reason: "start deadline reached"
|
|
471
|
-
} });
|
|
472
|
-
if (decision._tag === "Admit") return yield* start;
|
|
473
|
-
last = decision.line;
|
|
474
|
-
const window = decision.line._tag === "Account" || decision.line._tag === "Budget" ? decision.line.window : "minute";
|
|
475
|
-
yield* Effect.sleep(Math.max(0, Math.min(decision.until, now + pollMillis[window], deadline) - now));
|
|
476
|
-
}
|
|
477
|
-
});
|
|
478
|
-
export { BOAT_JOBS_PER_RUN, BoatStarts, Merges, Repo, StartHeld, StartReadError, admitStart, boatJobFindings, decide, startRepos };
|
|
130
|
+
export { BOAT_JOBS_PER_RUN, boatJobFindings };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@milkenjoyer/toolchain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/birbprophet/agent-kit.git",
|
|
@@ -108,7 +108,6 @@
|
|
|
108
108
|
"prepack": "vp pack"
|
|
109
109
|
},
|
|
110
110
|
"dependencies": {
|
|
111
|
-
"@boatdev/sdk": "1.13.0",
|
|
112
111
|
"@effect/platform-node": "4.1.4",
|
|
113
112
|
"@effect/tsgo": "0.69.0",
|
|
114
113
|
"@foldkit/oxlint-plugin": "0.15.4",
|
package/src/starts.ts
CHANGED
|
@@ -1,101 +1 @@
|
|
|
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
1
|
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
|
-
});
|
package/src/starts/boat.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
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
|
-
finishedAt: Schema.optional(Schema.NullOr(Schema.Date)),
|
|
40
|
-
})
|
|
41
|
-
),
|
|
42
|
-
});
|
|
43
|
-
const settled = new Set<string>([
|
|
44
|
-
CiJobState.Success,
|
|
45
|
-
CiJobState.Failure,
|
|
46
|
-
CiJobState.Cancelled,
|
|
47
|
-
CiJobState.Skipped,
|
|
48
|
-
CiJobState.Failed,
|
|
49
|
-
CiJobState.Refused,
|
|
50
|
-
]);
|
|
51
|
-
|
|
52
|
-
export class BoatStarts extends Context.Service<
|
|
53
|
-
BoatStarts,
|
|
54
|
-
{
|
|
55
|
-
readonly plan: Effect.Effect<Plan, StartReadError>;
|
|
56
|
-
readonly repoStarts: (
|
|
57
|
-
repo: Repo
|
|
58
|
-
) => Effect.Effect<RepoStarts, StartReadError>;
|
|
59
|
-
}
|
|
60
|
-
>()("toolchain/BoatStarts") {
|
|
61
|
-
static readonly layer = (
|
|
62
|
-
key: Redacted.Redacted<string>,
|
|
63
|
-
options: {
|
|
64
|
-
readonly fetch?: (
|
|
65
|
-
url: string | URL | Request,
|
|
66
|
-
init?: RequestInit
|
|
67
|
-
) => Promise<Response>;
|
|
68
|
-
} = {}
|
|
69
|
-
) =>
|
|
70
|
-
Layer.sync(BoatStarts, () => {
|
|
71
|
-
const api = new BoatApi(
|
|
72
|
-
new Configuration({
|
|
73
|
-
accessToken: Redacted.value(key),
|
|
74
|
-
...(options.fetch === undefined ? {} : { fetchApi: options.fetch }),
|
|
75
|
-
})
|
|
76
|
-
);
|
|
77
|
-
const plan = Effect.gen(function* () {
|
|
78
|
-
const response = yield* Effect.tryPromise({
|
|
79
|
-
try: () => api.limits(),
|
|
80
|
-
catch: () =>
|
|
81
|
-
new StartReadError({ reason: "Boat limits request failed" }),
|
|
82
|
-
});
|
|
83
|
-
const limits = yield* Schema.decodeUnknownEffect(Limits)(response).pipe(
|
|
84
|
-
Effect.mapError(
|
|
85
|
-
() => new StartReadError({ reason: "Boat limits unread" })
|
|
86
|
-
)
|
|
87
|
-
);
|
|
88
|
-
return {
|
|
89
|
-
limits: {
|
|
90
|
-
minute: limits.starts.minute.limit,
|
|
91
|
-
hour: limits.starts.hour.limit,
|
|
92
|
-
day: limits.starts.day.limit,
|
|
93
|
-
},
|
|
94
|
-
used: {
|
|
95
|
-
minute: limits.starts.minute.used,
|
|
96
|
-
hour: limits.starts.hour.used,
|
|
97
|
-
day: limits.starts.day.used,
|
|
98
|
-
},
|
|
99
|
-
nextStartDollars: limits.nextStartDollars ?? undefined,
|
|
100
|
-
};
|
|
101
|
-
});
|
|
102
|
-
const repoStarts = Effect.fn("BoatStarts.repoStarts")(function* (
|
|
103
|
-
repo: Repo
|
|
104
|
-
) {
|
|
105
|
-
const response = yield* Effect.tryPromise({
|
|
106
|
-
try: () => api.getCiOverview({ repo, limit: 500 }),
|
|
107
|
-
catch: () => new StartReadError({ reason: "Boat CI request failed" }),
|
|
108
|
-
});
|
|
109
|
-
const now = yield* Clock.currentTimeMillis;
|
|
110
|
-
const { jobs } = yield* Schema.decodeUnknownEffect(Ci)(response).pipe(
|
|
111
|
-
Effect.mapError(
|
|
112
|
-
() => new StartReadError({ reason: "Boat CI history unread" })
|
|
113
|
-
)
|
|
114
|
-
);
|
|
115
|
-
if (jobs.some((job) => job.repo !== repo))
|
|
116
|
-
return yield* new StartReadError({
|
|
117
|
-
reason: "Boat CI history differs from request",
|
|
118
|
-
});
|
|
119
|
-
const oldest = Math.min(
|
|
120
|
-
...jobs.map((job) =>
|
|
121
|
-
Math.max(job.queuedAt.getTime(), job.runStartedAt?.getTime() ?? 0)
|
|
122
|
-
)
|
|
123
|
-
);
|
|
124
|
-
return {
|
|
125
|
-
repo,
|
|
126
|
-
starts: jobs.flatMap((job) => {
|
|
127
|
-
const at =
|
|
128
|
-
job.runStartedAt?.getTime() ??
|
|
129
|
-
(settled.has(job.state) ||
|
|
130
|
-
(job.finishedAt !== undefined && job.finishedAt !== null)
|
|
131
|
-
? undefined
|
|
132
|
-
: now);
|
|
133
|
-
return job.label.split("/")[0]?.startsWith("boat") === true &&
|
|
134
|
-
at !== undefined &&
|
|
135
|
-
at > now - windowMillis.day
|
|
136
|
-
? [Math.min(at, now)]
|
|
137
|
-
: [];
|
|
138
|
-
}),
|
|
139
|
-
complete: jobs.length < 500 || oldest <= now - windowMillis.day,
|
|
140
|
-
};
|
|
141
|
-
});
|
|
142
|
-
return BoatStarts.of({ plan, repoStarts });
|
|
143
|
-
});
|
|
144
|
-
}
|
package/src/starts/budget.ts
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
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
|
-
};
|
package/src/starts/merges.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
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
|
-
}
|