werkmeister-core 0.95.3 → 0.95.5
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/CHANGELOG.md +10 -0
- package/dist/fixture_fakes.d.ts +6 -0
- package/dist/fixture_fakes.js +61 -0
- package/dist/fixture_workspace.d.ts +6 -0
- package/dist/fixture_workspace.js +47 -11
- package/dist/lifecycle.js +3 -2
- package/dist/scheduler.d.ts +4 -2
- package/dist/scheduler.js +37 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# sandbox-core
|
|
2
2
|
|
|
3
|
+
## 0.95.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 55bc3c0: `with-fixture-workspace` gains `--postmortem` (seeds `postmortem.md`, so `workspace abandon` and `close` run without `--skip-postmortem`), `--worktrees` (each repo a worktree of its source clone on `<user>/demo`, as init makes them, so `pr push` takes it) and `--jira` (a fake Jira for Jira keys, its calls printed as `--- jira calls`). `withFixtureWorkspace` takes `postmortem`, `worktrees` and `jira: true` (calls in `fx.jiraCalls`) (SW-896).
|
|
8
|
+
- 4a8f03e: The fixture workspace's ports (`with-fixture-workspace`, `withFixtureWorkspace`) start at 29100 instead of 39100. 39100 is inside Linux's ephemeral range, so a test server or client socket could land on it and fail the gate fixture's port check with `port 39100 held by a host process` (SW-899).
|
|
9
|
+
- 1b7e7d2: `with-fixture-workspace --workspace <file>` (and `withFixtureWorkspace`'s `workspace` option) now takes only `auto_stop`, `autonomous`, `context_strategy`, `description`, `group`, `issue_id`, `labels`, `model`, `repos`, `starting_prompt`, `task` and `work_type`. Any other key is refused with exit 2, as `--config` already does, since the wrapper is pre-approved and its arguments are its only limit (SW-900).
|
|
10
|
+
|
|
11
|
+
## 0.95.4
|
|
12
|
+
|
|
3
13
|
## 0.95.3
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/dist/fixture_fakes.d.ts
CHANGED
|
@@ -53,3 +53,9 @@ export interface FakeDaemon {
|
|
|
53
53
|
* Any other tool fails with code FAKE_DAEMON, so a path the fake doesn't cover fails loudly.
|
|
54
54
|
*/
|
|
55
55
|
export declare function startFakeDaemon(tools?: Record<string, unknown>): Promise<FakeDaemon>;
|
|
56
|
+
/**
|
|
57
|
+
* A stand-in for the Jira REST paths jira-rest.ts calls (SW-896): every issue starts `To Do`,
|
|
58
|
+
* `POST …/transitions` moves it, `POST …/comment` is taken. Any other path fails 501, so a path
|
|
59
|
+
* the fake doesn't cover fails loudly. Point JIRA_BASE_URL at `url` with any email and token.
|
|
60
|
+
*/
|
|
61
|
+
export declare function startFakeJira(): Promise<FakeDaemon>;
|
package/dist/fixture_fakes.js
CHANGED
|
@@ -126,6 +126,67 @@ export async function startFakeDaemon(tools = {}) {
|
|
|
126
126
|
}
|
|
127
127
|
reply(404, { error: `fake daemon: unhandled ${req.method} ${req.url}`, code: "FAKE_DAEMON" });
|
|
128
128
|
});
|
|
129
|
+
return listening(server, calls);
|
|
130
|
+
}
|
|
131
|
+
const JIRA_STATUSES = ["To Do", "In Progress", "In Review", "Done"];
|
|
132
|
+
/**
|
|
133
|
+
* A stand-in for the Jira REST paths jira-rest.ts calls (SW-896): every issue starts `To Do`,
|
|
134
|
+
* `POST …/transitions` moves it, `POST …/comment` is taken. Any other path fails 501, so a path
|
|
135
|
+
* the fake doesn't cover fails loudly. Point JIRA_BASE_URL at `url` with any email and token.
|
|
136
|
+
*/
|
|
137
|
+
export async function startFakeJira() {
|
|
138
|
+
const calls = [];
|
|
139
|
+
const status = new Map();
|
|
140
|
+
const statusOf = (key) => status.get(key) ?? "To Do";
|
|
141
|
+
const issue = (key) => ({
|
|
142
|
+
key,
|
|
143
|
+
fields: {
|
|
144
|
+
summary: "fixture issue",
|
|
145
|
+
status: { name: statusOf(key) },
|
|
146
|
+
issuetype: { name: "Task" },
|
|
147
|
+
comment: { comments: [] },
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
const server = http.createServer(async (req, res) => {
|
|
151
|
+
let body = "";
|
|
152
|
+
for await (const chunk of req)
|
|
153
|
+
body += chunk;
|
|
154
|
+
const url = new URL(req.url ?? "/", "http://fake");
|
|
155
|
+
calls.push(`${req.method} ${url.pathname}${body ? ` ${body}` : ""}`);
|
|
156
|
+
const reply = (code, json) => res
|
|
157
|
+
.writeHead(code, { "content-type": "application/json" })
|
|
158
|
+
.end(json === undefined ? "" : JSON.stringify(json));
|
|
159
|
+
const m = /^\/rest\/api\/2\/issue\/([A-Z][A-Z0-9]*-\d+)(\/transitions|\/comment)?$/.exec(url.pathname);
|
|
160
|
+
const key = m?.[1] ?? "";
|
|
161
|
+
if (m && req.method === "GET" && !m[2])
|
|
162
|
+
return reply(200, issue(key));
|
|
163
|
+
if (m?.[2] === "/transitions" && req.method === "GET")
|
|
164
|
+
return reply(200, {
|
|
165
|
+
transitions: JIRA_STATUSES.map((name, i) => ({ id: String(i + 1), name, to: { name } })),
|
|
166
|
+
});
|
|
167
|
+
if (m?.[2] === "/transitions" && req.method === "POST") {
|
|
168
|
+
const id = Number(JSON.parse(body || "{}").transition?.id);
|
|
169
|
+
const to = JIRA_STATUSES[id - 1];
|
|
170
|
+
if (!to)
|
|
171
|
+
return reply(400, { errorMessages: [`fake jira: no transition ${id}`] });
|
|
172
|
+
status.set(key, to);
|
|
173
|
+
return reply(204);
|
|
174
|
+
}
|
|
175
|
+
if (m?.[2] === "/comment" && req.method === "POST")
|
|
176
|
+
return reply(201, {});
|
|
177
|
+
if (req.method === "GET" && url.pathname === "/rest/api/2/search/jql") {
|
|
178
|
+
const keys = /key in \(([^)]*)\)/.exec(url.searchParams.get("jql") ?? "")?.[1] ?? "";
|
|
179
|
+
const found = keys
|
|
180
|
+
.split(",")
|
|
181
|
+
.map((k) => k.trim())
|
|
182
|
+
.filter(Boolean);
|
|
183
|
+
return reply(200, { issues: found.map(issue) });
|
|
184
|
+
}
|
|
185
|
+
reply(501, { errorMessages: [`fake jira: unhandled ${req.method} ${url.pathname}`] });
|
|
186
|
+
});
|
|
187
|
+
return listening(server, calls);
|
|
188
|
+
}
|
|
189
|
+
async function listening(server, calls) {
|
|
129
190
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
130
191
|
const { port } = server.address();
|
|
131
192
|
return {
|
|
@@ -61,6 +61,12 @@ export interface FixtureOptions {
|
|
|
61
61
|
* `since` is that many minutes back (and `services`, a service start), so admission prices and ages it (SW-809).
|
|
62
62
|
*/
|
|
63
63
|
queue?: string[];
|
|
64
|
+
/** A `postmortem.md` at the workspace root, on the record branch, so close-out runs without `--skip-postmortem` (SW-896). */
|
|
65
|
+
postmortem?: boolean;
|
|
66
|
+
/** Each repo a worktree of its source clone, as init makes them, on `<os user>/demo`, so `pr push` takes it (SW-896). */
|
|
67
|
+
worktrees?: boolean;
|
|
68
|
+
/** JIRA_BASE_URL for the fixture (a fake Jira's URL, `startFakeJira`). Default: blank Jira credentials. */
|
|
69
|
+
jira?: string;
|
|
64
70
|
}
|
|
65
71
|
/** One running container as `docker inspect` reports it to the scheduler. */
|
|
66
72
|
export interface FixtureContainer {
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { execFileSync } from "node:child_process";
|
|
8
8
|
import * as fs from "node:fs";
|
|
9
|
+
import * as os from "node:os";
|
|
9
10
|
import * as path from "node:path";
|
|
10
11
|
import YAML from "yaml";
|
|
11
12
|
import { waitingPath } from "./agent_state.js";
|
|
@@ -20,6 +21,22 @@ const NAME = /^[a-z0-9][a-z0-9-]*$/;
|
|
|
20
21
|
const QUEUE_ENTRY = /^([a-z0-9][a-z0-9-]*)(?::(\d+)(?::([a-z0-9][a-z0-9-]*(?:,[a-z0-9][a-z0-9-]*)*))?)?$/;
|
|
21
22
|
// The only config.yaml keys --config may add: others (jira, github_ingest, paths…) reach real services or repos from a pre-approved run.
|
|
22
23
|
const CONFIG_KEYS = ["budget", "workspace_ask_rules"];
|
|
24
|
+
// The only workspace.yml keys --workspace may set (SW-900): text and flags the fixture's own files take, and
|
|
25
|
+
// `repos`, whose names init keeps under repos_root. Tracker refs reach the fake gh or blank/fake Jira creds.
|
|
26
|
+
const WORKSPACE_KEYS = [
|
|
27
|
+
"auto_stop",
|
|
28
|
+
"autonomous",
|
|
29
|
+
"context_strategy",
|
|
30
|
+
"description",
|
|
31
|
+
"group",
|
|
32
|
+
"issue_id",
|
|
33
|
+
"labels",
|
|
34
|
+
"model",
|
|
35
|
+
"repos",
|
|
36
|
+
"starting_prompt",
|
|
37
|
+
"task",
|
|
38
|
+
"work_type",
|
|
39
|
+
];
|
|
23
40
|
export const FIXTURE_FEATURE = "demo";
|
|
24
41
|
export const FIXTURE_BRANCH = "me/demo";
|
|
25
42
|
export const FIXTURE_REPO_SLUG = "o/app";
|
|
@@ -46,10 +63,13 @@ repo:
|
|
|
46
63
|
packages/lib: lib
|
|
47
64
|
`;
|
|
48
65
|
const FRAGMENT_FILE = path.join(".werkmeister", "services.yaml");
|
|
49
|
-
//
|
|
66
|
+
// Above real workspaces' slots, so a real `up` from a fixture (SW-284) can't clash, and below every
|
|
67
|
+
// OS's ephemeral range (Linux 32768+) and k8s NodePorts (30000+), where listen(0) servers and client
|
|
68
|
+
// sockets land: one on the gate's port probe read as "held by a host process" (SW-899).
|
|
69
|
+
// shortcut: every fixture shares this block, so two parallel gate probes can still meet for under a ms; give each fixture its own block if that flakes.
|
|
50
70
|
const PORTS = `feature_slots:
|
|
51
71
|
count: 4
|
|
52
|
-
base_port:
|
|
72
|
+
base_port: 29100
|
|
53
73
|
ports_per_slot: 20
|
|
54
74
|
`;
|
|
55
75
|
const FIXTURE_PROJECT = "fixture";
|
|
@@ -81,6 +101,9 @@ export function fixturePreset({ gate, bare, change, ...opts }) {
|
|
|
81
101
|
const refused = Object.keys(opts.config ?? {}).filter((k) => !CONFIG_KEYS.includes(k));
|
|
82
102
|
if (refused.length)
|
|
83
103
|
throw new Error(`--config may not set ${refused.join(", ")}: only ${CONFIG_KEYS.join(", ")}`);
|
|
104
|
+
const unlisted = Object.keys(opts.workspace ?? {}).filter((k) => !WORKSPACE_KEYS.includes(k));
|
|
105
|
+
if (unlisted.length)
|
|
106
|
+
throw new Error(`--workspace may not set ${unlisted.join(", ")}: only ${WORKSPACE_KEYS.join(", ")}`);
|
|
84
107
|
for (const c of opts.containers ?? []) {
|
|
85
108
|
const bad = !NAME.test(c.feature)
|
|
86
109
|
? c.feature
|
|
@@ -95,6 +118,9 @@ export function fixturePreset({ gate, bare, change, ...opts }) {
|
|
|
95
118
|
const badEntry = opts.queue?.find((q) => !QUEUE_ENTRY.test(q));
|
|
96
119
|
if (badEntry !== undefined)
|
|
97
120
|
throw new Error(`--queue: bad entry ${JSON.stringify(badEntry)}: <feature>[:<waitedMin>[:<service>,...]], lowercase names and whole minutes`);
|
|
121
|
+
// A worktree shares its source's origin/main, so it can't stay behind until a fetch.
|
|
122
|
+
if (opts.worktrees && opts.behind)
|
|
123
|
+
throw new Error("--worktrees and --behind don't combine");
|
|
98
124
|
const libChange = "packages/lib/index.js";
|
|
99
125
|
const gateChange = change ?? (opts.fragmentChange === undefined ? libChange : undefined);
|
|
100
126
|
if (gate && opts.services && gateChange === libChange) {
|
|
@@ -161,6 +187,8 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
|
|
|
161
187
|
const extra = (Object.keys(prFields).length ? YAML.stringify(prFields) : "") +
|
|
162
188
|
(opts.prLines ?? []).map((l) => `${l}\n`).join("");
|
|
163
189
|
let applyFragmentChange;
|
|
190
|
+
// pr push takes only the workspace's own <os user>/<workspace> branch (pushOne); the clone keeps me/demo.
|
|
191
|
+
const branch = opts.worktrees ? `${os.userInfo().username}/${FIXTURE_FEATURE}` : FIXTURE_BRANCH;
|
|
164
192
|
for (const { name, change } of repos) {
|
|
165
193
|
// Ends in owner/repo.git so originSlug matches the PR file's repo: (SW-381).
|
|
166
194
|
const remote = path.join(root, "remotes", "o", `${name}.git`);
|
|
@@ -176,7 +204,11 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
|
|
|
176
204
|
if (opts.fragment && name === repos[0]?.name)
|
|
177
205
|
write(path.join(source, FRAGMENT_FILE), opts.fragment);
|
|
178
206
|
const repo = path.join(workspace, name);
|
|
179
|
-
|
|
207
|
+
// Detached: the source still has main checked out until it moves to fixture-source below.
|
|
208
|
+
if (opts.worktrees)
|
|
209
|
+
git(source, "worktree", "add", "-q", "--detach", repo);
|
|
210
|
+
else
|
|
211
|
+
git(workspace, "clone", "-q", remote, name);
|
|
180
212
|
const frag = name === repos[0]?.name ? opts.fragmentChange : undefined;
|
|
181
213
|
const changed = [change, frag === undefined ? undefined : FRAGMENT_FILE].filter(Boolean);
|
|
182
214
|
if (changed.length) {
|
|
@@ -186,9 +218,10 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
|
|
|
186
218
|
write(path.join(repo, FRAGMENT_FILE), opts.fragment ?? BASE_FRAGMENT);
|
|
187
219
|
git(repo, "add", ".");
|
|
188
220
|
git(repo, "commit", "-q", "-m", "base");
|
|
189
|
-
|
|
221
|
+
// HEAD:main, since a detached worktree's `main` is the source's, still at init.
|
|
222
|
+
git(repo, "push", "-q", "origin", "HEAD:main");
|
|
190
223
|
}
|
|
191
|
-
git(repo, "switch", "-q", "-c",
|
|
224
|
+
git(repo, "switch", "-q", "-c", branch);
|
|
192
225
|
if (opts.behind) {
|
|
193
226
|
// Pushed from the source clone, so the workspace clone's origin/main stays stale until a fetch.
|
|
194
227
|
git(source, "fetch", "-q", "origin", "main");
|
|
@@ -211,10 +244,12 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
|
|
|
211
244
|
git(repo, "commit", "-q", "-am", `change ${FRAGMENT_FILE}`);
|
|
212
245
|
};
|
|
213
246
|
if (opts.prFile && changed.length)
|
|
214
|
-
write(path.join(notes, `pr-${name}.md`), `---\nrepo: o/${name}\nbranch: ${
|
|
247
|
+
write(path.join(notes, `pr-${name}.md`), `---\nrepo: o/${name}\nbranch: ${branch}\nbase: main\nstatus: ${status}\n${extra}---\n# feat: fixture change\n\n${PASS}`);
|
|
215
248
|
}
|
|
216
249
|
if (opts.prFile)
|
|
217
250
|
write(path.join(notes, "exercise.md"), PASS);
|
|
251
|
+
if (opts.postmortem)
|
|
252
|
+
write(path.join(workspace, "postmortem.md"), "# Post-mortem\n\nseeded by the fixture\n");
|
|
218
253
|
// The workspace folder is a git repo with its own local origin, like a real one, so `pr open`
|
|
219
254
|
// commits and pushes the record (its code clones are ignored).
|
|
220
255
|
const record = path.join(root, "remotes", "o", "ws.git");
|
|
@@ -299,10 +334,11 @@ ${githubProject(repos.map((r) => r.name))}${opts.config ? YAML.stringify(opts.co
|
|
|
299
334
|
// dockerode honours DOCKER_HOST before any context, so no engine call reaches the host (SW-164).
|
|
300
335
|
// A fixed short path: one under a deep temp root passes macOS's 104-byte socket limit and crashes with EINVAL.
|
|
301
336
|
DOCKER_HOST: "unix:///nonexistent/werkmeister-fixture-docker.sock",
|
|
302
|
-
// jiraCreds() reads these before config; blank, the fixture config's missing `jira:` wins
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
337
|
+
// jiraCreds() reads these before config; blank, the fixture config's missing `jira:` wins, so
|
|
338
|
+
// only the fake Jira (`jira`) is ever reached.
|
|
339
|
+
JIRA_BASE_URL: opts.jira ?? "",
|
|
340
|
+
JIRA_EMAIL: opts.jira ? "fixture@example.invalid" : "",
|
|
341
|
+
JIRA_API_TOKEN: opts.jira ? "fixture" : "",
|
|
306
342
|
// A fixture step must not act as the caller on Anthropic, a cluster or GitHub (SW-424).
|
|
307
343
|
ANTHROPIC_API_KEY: "",
|
|
308
344
|
CLAUDE_CODE_OAUTH_TOKEN: "",
|
|
@@ -330,7 +366,7 @@ const SCRIPT = `#!/bin/sh
|
|
|
330
366
|
# Generated by \`werkmeister workspace init\` (SW-292); rerun it to regenerate.
|
|
331
367
|
# Runs this workspace's own built werkmeister CLI against a seeded fixture workspace, removed on exit:
|
|
332
368
|
# temp HOME and registry, a local bare origin, logging fakes for gh and docker, dead Docker and daemon.
|
|
333
|
-
# usage: with-fixture-workspace [--no-wait] [--gate] [--bare] [--daemon] [--exec-runs] [--merged] [--behind] [--slots-full] [--running <file>] [--queue <feature>[,<feature>]... | --queue <feature>:<waitedMin>[:<service>,...]]... [--issue <ref>] [--pr-field <key>=<value>]... [--pr-line '<key>: <value>']... [--workspace <file>] [--config <file>] [--services <file>] [--fragment <file>] [--change <path>] [--change-base <file>] [--change-branch <file>] [--fragment-change <file>] [--stale-compose] [--print <file>]... <werkmeister args> [--then <werkmeister args>]...
|
|
369
|
+
# usage: with-fixture-workspace [--no-wait] [--gate] [--bare] [--daemon] [--exec-runs] [--merged] [--behind] [--slots-full] [--postmortem] [--worktrees] [--jira] [--running <file>] [--queue <feature>[,<feature>]... | --queue <feature>:<waitedMin>[:<service>,...]]... [--issue <ref>] [--pr-field <key>=<value>]... [--pr-line '<key>: <value>']... [--workspace <file>] [--config <file>] [--services <file>] [--fragment <file>] [--change <path>] [--change-base <file>] [--change-branch <file>] [--fragment-change <file>] [--stale-compose] [--print <file>]... <werkmeister args> [--then <werkmeister args>]...
|
|
334
370
|
# It only ever runs that CLI, never another command, so one allow rule can cover it (SW-172, SW-231);
|
|
335
371
|
# --exec-runs, given first, also runs the registry's rows on the host, so the workspace asks before it (SW-473).
|
|
336
372
|
ws=$(cd "$(dirname "$0")/../.." && pwd -P) || exit 1
|
package/dist/lifecycle.js
CHANGED
|
@@ -50,7 +50,7 @@ import { run } from "./proc.js";
|
|
|
50
50
|
import { workspaceServices } from "./registry_sync.js";
|
|
51
51
|
import { info, raw, success, warn } from "./reporter.js";
|
|
52
52
|
import { withStartRecord } from "./resources.js";
|
|
53
|
-
import { queueOrder, STARVE_MS } from "./scheduler.js";
|
|
53
|
+
import { headCanWaitOut, queueOrder, STARVE_MS, } from "./scheduler.js";
|
|
54
54
|
import { schedulerOp, waitingQueue } from "./scheduler_client.js";
|
|
55
55
|
import { dequeue, enqueue, featureCost, loadBudget, readLeases, readQueue, requestAdmission, requeueFirst, serviceStarts, setupsCurrent, waiters, waitForSetupLoad, } from "./scheduler_state.js";
|
|
56
56
|
import { getSecretsPath, loadSecrets, resolveSecrets } from "./secrets.js";
|
|
@@ -470,7 +470,8 @@ export async function capacityWait(feature, position, stays, need) {
|
|
|
470
470
|
const ws = readLeases()[head.feature]?.workspace;
|
|
471
471
|
const need = ws ? featureCost(ws, readWaiting(ws)?.services) : null;
|
|
472
472
|
lines.push(`behind ${head.feature}${need ? ` (needs ${cpus(need.cpus)}, ${gib(need.memory)})` : ""}` +
|
|
473
|
-
|
|
473
|
+
// A parked head never holds (SW-701), nor one only a person's stop could fit (SW-903).
|
|
474
|
+
(head.waitedMs >= STARVE_MS && !head.parked && headCanWaitOut(budget, running, head)
|
|
474
475
|
? `, which has waited ${Math.round(head.waitedMs / 60_000)} min and gets the next free room`
|
|
475
476
|
: ""));
|
|
476
477
|
}
|
package/dist/scheduler.d.ts
CHANGED
|
@@ -64,9 +64,11 @@ export declare function parseSize(s: string | number): number;
|
|
|
64
64
|
/** Gate waiters first, then the rest, each first come first served: a gate frees its whole feature soonest (SW-675). */
|
|
65
65
|
export declare function queueOrder(queue: Waiter[]): Waiter[];
|
|
66
66
|
/**
|
|
67
|
-
* Whether *
|
|
68
|
-
*
|
|
67
|
+
* Whether the starved *head* can wait its room out: it fits once every lease but a person's leaves.
|
|
68
|
+
* A person's lease never ends by itself, so holding for one only stalls the queue (SW-903).
|
|
69
|
+
* A head of unknown cost (a service start) is assumed to.
|
|
69
70
|
*/
|
|
71
|
+
export declare function headCanWaitOut(budget: Budget, running: Lease[], head: Waiter): boolean;
|
|
70
72
|
export declare function admit(budget: Budget, running: Lease[], queue: Waiter[], req: Request): Decision;
|
|
71
73
|
/**
|
|
72
74
|
* The slot *feature* should hold. *held* maps slot → owning feature. Its own
|
package/dist/scheduler.js
CHANGED
|
@@ -29,19 +29,47 @@ const RANK_ORDER = ["idle", "human-check", "working"];
|
|
|
29
29
|
export function queueOrder(queue) {
|
|
30
30
|
return [...queue.filter((w) => w.gate), ...queue.filter((w) => !w.gate)];
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether the starved *head* can wait its room out: it fits once every lease but a person's leaves.
|
|
34
|
+
* A person's lease never ends by itself, so holding for one only stalls the queue (SW-903).
|
|
35
|
+
* A head of unknown cost (a service start) is assumed to.
|
|
36
|
+
*/
|
|
37
|
+
export function headCanWaitOut(budget, running, head) {
|
|
38
|
+
if (!head.cost)
|
|
39
|
+
return true;
|
|
40
|
+
const kept = running
|
|
41
|
+
.filter((l) => l.rank === "person" && l.feature !== head.feature)
|
|
42
|
+
.reduce((a, l) => ({ memory: a.memory + l.cost.memory, cpus: a.cpus + l.cost.cpus }), {
|
|
43
|
+
memory: 0,
|
|
44
|
+
cpus: 0,
|
|
45
|
+
});
|
|
46
|
+
return (kept.memory + head.cost.memory <= budget.memory && kept.cpus + head.cost.cpus <= budget.cpus);
|
|
47
|
+
}
|
|
32
48
|
/**
|
|
33
49
|
* Whether *req* may start now. *running* is ordered longest-inactive first;
|
|
34
50
|
* *queue* is the waiting requests, first come first served.
|
|
35
51
|
*/
|
|
52
|
+
/** A whole `up` of *feature* adds only what its running lease doesn't already hold (SW-889). */
|
|
53
|
+
function added(cost, running, feature) {
|
|
54
|
+
const own = running.find((l) => l.feature === feature);
|
|
55
|
+
if (!own)
|
|
56
|
+
return cost;
|
|
57
|
+
return {
|
|
58
|
+
memory: Math.max(0, cost.memory - own.cost.memory),
|
|
59
|
+
cpus: Math.max(0, cost.cpus - own.cost.cpus),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
36
62
|
export function admit(budget, running, queue, req) {
|
|
37
|
-
|
|
63
|
+
const reup = !req.services && running.some((l) => l.feature === req.feature);
|
|
64
|
+
const cost = req.services ? req.cost : added(req.cost, running, req.feature);
|
|
65
|
+
if (reup && !cost.memory && !cost.cpus)
|
|
38
66
|
return { kind: "admit" };
|
|
39
67
|
if (req.cost.memory > budget.memory || req.cost.cpus > budget.cpus) {
|
|
40
68
|
return { kind: "never-fits", cost: req.cost };
|
|
41
69
|
}
|
|
42
70
|
const used = running.reduce((a, l) => ({ memory: a.memory + l.cost.memory, cpus: a.cpus + l.cost.cpus }), { memory: 0, cpus: 0 });
|
|
43
|
-
const fits = (freed) => used.memory - freed.memory +
|
|
44
|
-
used.cpus - freed.cpus +
|
|
71
|
+
const fits = (freed) => used.memory - freed.memory + cost.memory <= budget.memory &&
|
|
72
|
+
used.cpus - freed.cpus + cost.cpus <= budget.cpus;
|
|
45
73
|
const gate = !!req.gate || (!!req.services && running.some((l) => l.feature === req.feature));
|
|
46
74
|
const queued = queue.find((w) => w.feature === req.feature);
|
|
47
75
|
const self = { feature: req.feature, waitedMs: queued?.waitedMs ?? 0, gate };
|
|
@@ -56,7 +84,10 @@ export function admit(budget, running, queue, req) {
|
|
|
56
84
|
const endsGate = !!req.services && running.some((l) => l.feature === req.feature && l.busy);
|
|
57
85
|
// Only a release's grant starts a whole-feature head, and room can free without one (a timed-out
|
|
58
86
|
// gate's stop, a Docker restart), so holding behind one that fits now waits forever (SW-729).
|
|
59
|
-
|
|
87
|
+
// A parked head's cost is just its services; a whole-feature head's, its set less what already runs.
|
|
88
|
+
const headCost = order[0].cost && !order[0].parked
|
|
89
|
+
? added(order[0].cost, running, order[0].feature)
|
|
90
|
+
: order[0].cost;
|
|
60
91
|
const headAwaitsGrant = !!headCost &&
|
|
61
92
|
used.memory + headCost.memory <= budget.memory &&
|
|
62
93
|
used.cpus + headCost.cpus <= budget.cpus;
|
|
@@ -65,7 +96,8 @@ export function admit(budget, running, queue, req) {
|
|
|
65
96
|
!endsGate &&
|
|
66
97
|
!headAwaitsGrant &&
|
|
67
98
|
!order[0].parked &&
|
|
68
|
-
(order[0].gate ||
|
|
99
|
+
(order[0].gate ||
|
|
100
|
+
(order[0].waitedMs >= STARVE_MS && headCanWaitOut(budget, running, order[0])));
|
|
69
101
|
if (fits({ memory: 0, cpus: 0 }) && (req.byPerson || !headHolds))
|
|
70
102
|
return { kind: "admit" };
|
|
71
103
|
// An idle or human-check lease (a session parked on a question) has nobody in it, so the queue head
|
package/package.json
CHANGED