@loucompanion/forge-bridge 0.1.4-dev.028808a841c8 → 0.1.4-dev.19e5db286f12
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 +2 -2
- package/dist/bridge/bridge.service.js +4 -3
- package/dist/cli/cli.service.js +3 -1
- package/dist/runtime/host-load-admission.d.ts +52 -0
- package/dist/runtime/host-load-admission.js +84 -0
- package/dist/runtime/host-load-forge-cgroups.d.ts +8 -0
- package/dist/runtime/host-load-forge-cgroups.js +51 -0
- package/dist/runtime/host-load-governor-config.d.ts +34 -0
- package/dist/runtime/host-load-governor-config.js +46 -0
- package/dist/runtime/host-load-governor.d.ts +123 -0
- package/dist/runtime/host-load-governor.js +415 -0
- package/dist/runtime/host-load-ledger-lock.d.ts +69 -0
- package/dist/runtime/host-load-ledger-lock.js +185 -0
- package/dist/runtime/host-load-sampler.d.ts +38 -0
- package/dist/runtime/host-load-sampler.js +66 -6
- package/dist/runtime/host-load-step-class.d.ts +33 -0
- package/dist/runtime/host-load-step-class.js +46 -0
- package/dist/runtime/host-load-token-ledger.d.ts +135 -0
- package/dist/runtime/host-load-token-ledger.js +345 -0
- package/dist/runtime/host-pressure-stall.d.ts +21 -1
- package/dist/runtime/host-pressure-stall.js +48 -1
- package/dist/runtime/host-runtime-lease.d.ts +7 -1
- package/dist/runtime/host-runtime-lease.js +12 -2
- package/dist/session/pre-publication-host-load.d.ts +27 -0
- package/dist/session/pre-publication-host-load.js +96 -0
- package/dist/session/pre-publication-step-execution.d.ts +11 -0
- package/dist/session/pre-publication-step-execution.js +49 -0
- package/dist/session/pre-publication-validation.d.ts +9 -9
- package/dist/session/pre-publication-validation.js +17 -55
- package/dist/session/provider-cli/provider-cli.process.d.ts +7 -0
- package/dist/session/provider-cli/provider-cli.process.js +13 -5
- package/dist/session/provider-cli/provider-cli.service.js +28 -5
- package/dist/session/session.types.js +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -34,6 +34,35 @@ export interface HostLoadSource {
|
|
|
34
34
|
stop(): void;
|
|
35
35
|
facts(): HostFacts | null;
|
|
36
36
|
current(): HostLoadSnapshot | null;
|
|
37
|
+
/**
|
|
38
|
+
* Recent samples, oldest first, ending with `current()`. The governor reads its
|
|
39
|
+
* pressure streak from them, so samples taken while no step waited still count.
|
|
40
|
+
*/
|
|
41
|
+
recent(): readonly HostLoadSnapshot[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Host CPU time split into the bridge's own share and everything else, over one
|
|
45
|
+
* sampling window (decision 0013). Kept in the bridge for the host load governor;
|
|
46
|
+
* it is not part of the heartbeat payload.
|
|
47
|
+
*/
|
|
48
|
+
export interface HostCpuAttribution {
|
|
49
|
+
readonly sampledAt: string;
|
|
50
|
+
/** Busy cores on the whole host, from /proc/stat deltas. */
|
|
51
|
+
readonly hostCoresInUse: number;
|
|
52
|
+
/** Cores used by the bridge's dedicated cgroup; null when the cgroup is not dedicated. */
|
|
53
|
+
readonly ownCoresInUse: number | null;
|
|
54
|
+
/** hostCoresInUse minus ownCoresInUse; null unless the own share is known. */
|
|
55
|
+
readonly externalCoresInUse: number | null;
|
|
56
|
+
}
|
|
57
|
+
/** The governor's view: the load sample plus the CPU attribution from the same window. */
|
|
58
|
+
export interface HostLoadGovernorSource extends Pick<HostLoadSource, "start" | "facts" | "current" | "recent"> {
|
|
59
|
+
cpuAttribution?(): HostCpuAttribution | null;
|
|
60
|
+
/**
|
|
61
|
+
* Free memory under the tightest cgroup `memory.max` on the bridge's path
|
|
62
|
+
* (null when none is set), from the latest sample. The governor budgets
|
|
63
|
+
* against the smaller of this and MemAvailable (decision 0013).
|
|
64
|
+
*/
|
|
65
|
+
cgroupMemoryHeadroomBytes?(): number | null;
|
|
37
66
|
}
|
|
38
67
|
export interface HostLoadSamplerOptions {
|
|
39
68
|
readonly environment?: HostEnvironment;
|
|
@@ -63,10 +92,17 @@ export declare class HostLoadSampler implements HostLoadSource {
|
|
|
63
92
|
private inFlight;
|
|
64
93
|
private hostFacts;
|
|
65
94
|
private latest;
|
|
95
|
+
private readonly history;
|
|
66
96
|
private cpuMark;
|
|
97
|
+
private hostCpuMark;
|
|
98
|
+
private latestCpuAttribution;
|
|
99
|
+
private latestMemoryHeadroom;
|
|
67
100
|
constructor(options?: HostLoadSamplerOptions);
|
|
68
101
|
facts(): HostFacts | null;
|
|
69
102
|
current(): HostLoadSnapshot | null;
|
|
103
|
+
cpuAttribution(): HostCpuAttribution | null;
|
|
104
|
+
cgroupMemoryHeadroomBytes(): number | null;
|
|
105
|
+
recent(): readonly HostLoadSnapshot[];
|
|
70
106
|
/** Detects static facts once, takes a first sample, then samples on the interval. Idempotent. */
|
|
71
107
|
start(): Promise<void>;
|
|
72
108
|
stop(): void;
|
|
@@ -74,9 +110,11 @@ export declare class HostLoadSampler implements HostLoadSource {
|
|
|
74
110
|
sample(): Promise<HostLoadSnapshot>;
|
|
75
111
|
private startOnce;
|
|
76
112
|
private readSnapshot;
|
|
113
|
+
private readCgroupMemoryHeadroom;
|
|
77
114
|
private readSystemPressure;
|
|
78
115
|
private readOwnCgroup;
|
|
79
116
|
private coresInUse;
|
|
117
|
+
private readHostCoresInUse;
|
|
80
118
|
private readMemoryAvailable;
|
|
81
119
|
private fallbackFreeMemory;
|
|
82
120
|
private readPressure;
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
2
|
import { defaultHostEnvironment, detectHostFacts, readOptional, } from "./host-facts.js";
|
|
3
|
-
import { cgroupLeafName, isDedicatedBridgeUnit, parseCgroupCpuUsageUsec, parseCgroupMemoryAnonBytes, parseMemAvailableBytes, parseNonNegativeInteger, parsePressureStall, parseUnifiedCgroupPath, } from "./host-pressure-stall.js";
|
|
3
|
+
import { cgroupLeafName, isDedicatedBridgeUnit, parseCgroupCpuUsageUsec, parseCgroupMemoryAnonBytes, parseMemAvailableBytes, parseNonNegativeInteger, parsePressureStall, parseProcStatCpuTotals, parseUnifiedCgroupMembership, parseUnifiedCgroupPath, } from "./host-pressure-stall.js";
|
|
4
|
+
import { readCgroupMemoryHeadroomBytes } from "./host-load-forge-cgroups.js";
|
|
4
5
|
export const defaultHostLoadSampleIntervalMs = 5_000;
|
|
6
|
+
/** Samples kept for the governor's pressure streak (FORGE_HOST_LOAD_BACKPRESSURE_SAMPLES is at most 60). */
|
|
7
|
+
const retainedSampleCount = 64;
|
|
5
8
|
/**
|
|
6
9
|
* Samples Linux PSI and the bridge's own cgroup on an interval and caches the
|
|
7
10
|
* latest snapshot. Every read is async and failure-tolerant: a host without PSI
|
|
@@ -19,7 +22,11 @@ export class HostLoadSampler {
|
|
|
19
22
|
inFlight = null;
|
|
20
23
|
hostFacts = null;
|
|
21
24
|
latest = null;
|
|
25
|
+
history = [];
|
|
22
26
|
cpuMark = null;
|
|
27
|
+
hostCpuMark = null;
|
|
28
|
+
latestCpuAttribution = null;
|
|
29
|
+
latestMemoryHeadroom = null;
|
|
23
30
|
constructor(options = {}) {
|
|
24
31
|
this.environment = options.environment ?? defaultHostEnvironment();
|
|
25
32
|
this.intervalMs = options.intervalMs !== undefined && Number.isFinite(options.intervalMs) && options.intervalMs > 0
|
|
@@ -37,6 +44,15 @@ export class HostLoadSampler {
|
|
|
37
44
|
current() {
|
|
38
45
|
return this.latest;
|
|
39
46
|
}
|
|
47
|
+
cpuAttribution() {
|
|
48
|
+
return this.latestCpuAttribution;
|
|
49
|
+
}
|
|
50
|
+
cgroupMemoryHeadroomBytes() {
|
|
51
|
+
return this.latestMemoryHeadroom;
|
|
52
|
+
}
|
|
53
|
+
recent() {
|
|
54
|
+
return [...this.history];
|
|
55
|
+
}
|
|
40
56
|
/** Detects static facts once, takes a first sample, then samples on the interval. Idempotent. */
|
|
41
57
|
start() {
|
|
42
58
|
if (this.timer)
|
|
@@ -58,6 +74,9 @@ export class HostLoadSampler {
|
|
|
58
74
|
this.inFlight ??= this.readSnapshot()
|
|
59
75
|
.then(snapshot => {
|
|
60
76
|
this.latest = snapshot;
|
|
77
|
+
this.history.push(snapshot);
|
|
78
|
+
if (this.history.length > retainedSampleCount)
|
|
79
|
+
this.history.splice(0, this.history.length - retainedSampleCount);
|
|
61
80
|
return snapshot;
|
|
62
81
|
})
|
|
63
82
|
.finally(() => {
|
|
@@ -82,11 +101,20 @@ export class HostLoadSampler {
|
|
|
82
101
|
const sampledAtMs = this.now();
|
|
83
102
|
const monotonicMs = this.monotonicNow();
|
|
84
103
|
const linux = this.environment.platform === "linux";
|
|
85
|
-
const [systemPressure, ownCgroup, memoryAvailableBytes] = linux
|
|
86
|
-
? await Promise.all([
|
|
87
|
-
|
|
104
|
+
const [systemPressure, ownCgroup, memoryAvailableBytes, hostCoresInUse, cgroupMemoryHeadroomBytes] = linux
|
|
105
|
+
? await Promise.all([
|
|
106
|
+
this.readSystemPressure(),
|
|
107
|
+
this.readOwnCgroup(monotonicMs),
|
|
108
|
+
this.readMemoryAvailable(),
|
|
109
|
+
this.readHostCoresInUse(),
|
|
110
|
+
this.readCgroupMemoryHeadroom(),
|
|
111
|
+
])
|
|
112
|
+
: [null, null, this.fallbackFreeMemory(), null, null];
|
|
113
|
+
const sampledAt = new Date(sampledAtMs).toISOString();
|
|
114
|
+
this.latestMemoryHeadroom = cgroupMemoryHeadroomBytes;
|
|
115
|
+
this.latestCpuAttribution = hostCpuAttribution(sampledAt, hostCoresInUse, ownCgroup?.cpuCoresInUse ?? null);
|
|
88
116
|
return {
|
|
89
|
-
sampledAt
|
|
117
|
+
sampledAt,
|
|
90
118
|
psiAvailable: systemPressure !== null,
|
|
91
119
|
systemPressure,
|
|
92
120
|
ownCgroupDedicated: ownCgroup !== null,
|
|
@@ -94,6 +122,11 @@ export class HostLoadSampler {
|
|
|
94
122
|
memoryAvailableBytes,
|
|
95
123
|
};
|
|
96
124
|
}
|
|
125
|
+
async readCgroupMemoryHeadroom() {
|
|
126
|
+
const membership = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "self", "cgroup"));
|
|
127
|
+
const cgroupPath = membership ? parseUnifiedCgroupMembership(membership) : null;
|
|
128
|
+
return cgroupPath ? await readCgroupMemoryHeadroomBytes(this.environment, cgroupPath).catch(() => null) : null;
|
|
129
|
+
}
|
|
97
130
|
async readSystemPressure() {
|
|
98
131
|
const pressureRoot = path.posix.join(this.environment.procRoot, "pressure");
|
|
99
132
|
const [cpu, memory, io] = await Promise.all(["cpu", "memory", "io"].map(resource => this.readPressure(path.posix.join(pressureRoot, resource))));
|
|
@@ -142,7 +175,20 @@ export class HostLoadSampler {
|
|
|
142
175
|
// A restarted unit resets its counter; elapsed time comes from a monotonic clock.
|
|
143
176
|
if (elapsedUsec <= 0 || usedUsec < 0)
|
|
144
177
|
return null;
|
|
145
|
-
return
|
|
178
|
+
return roundCores(usedUsec / elapsedUsec);
|
|
179
|
+
}
|
|
180
|
+
async readHostCoresInUse() {
|
|
181
|
+
const content = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "stat"));
|
|
182
|
+
const totals = content ? parseProcStatCpuTotals(content) : null;
|
|
183
|
+
const previous = this.hostCpuMark;
|
|
184
|
+
this.hostCpuMark = totals;
|
|
185
|
+
if (!totals || !previous || previous.cpuCount !== totals.cpuCount)
|
|
186
|
+
return null;
|
|
187
|
+
const totalDelta = totals.totalTicks - previous.totalTicks;
|
|
188
|
+
const busyDelta = totals.busyTicks - previous.busyTicks;
|
|
189
|
+
if (totalDelta <= 0 || busyDelta < 0)
|
|
190
|
+
return null;
|
|
191
|
+
return roundCores((busyDelta / totalDelta) * totals.cpuCount);
|
|
146
192
|
}
|
|
147
193
|
async readMemoryAvailable() {
|
|
148
194
|
const meminfo = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "meminfo"));
|
|
@@ -162,3 +208,17 @@ export class HostLoadSampler {
|
|
|
162
208
|
return content ? parsePressureStall(content) : null;
|
|
163
209
|
}
|
|
164
210
|
}
|
|
211
|
+
function roundCores(value) {
|
|
212
|
+
return Math.round(value * 100) / 100;
|
|
213
|
+
}
|
|
214
|
+
function hostCpuAttribution(sampledAt, hostCoresInUse, ownCoresInUse) {
|
|
215
|
+
if (hostCoresInUse === null)
|
|
216
|
+
return null;
|
|
217
|
+
return {
|
|
218
|
+
sampledAt,
|
|
219
|
+
hostCoresInUse,
|
|
220
|
+
ownCoresInUse,
|
|
221
|
+
// Own usage only exists for a dedicated cgroup; a shared scope never yields external load.
|
|
222
|
+
externalCoresInUse: ownCoresInUse === null ? null : roundCores(Math.max(0, hostCoresInUse - ownCoresInUse)),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface HostLoadStepCost {
|
|
2
|
+
/** Cores the step is expected to keep busy. */
|
|
3
|
+
readonly cpuCores: number;
|
|
4
|
+
/** Peak resident memory the step is expected to need. */
|
|
5
|
+
readonly memoryBytes: number;
|
|
6
|
+
}
|
|
7
|
+
export interface HostLoadStepClassification {
|
|
8
|
+
readonly stepClass: string;
|
|
9
|
+
readonly cost: HostLoadStepCost;
|
|
10
|
+
}
|
|
11
|
+
export declare const defaultHostLoadStepCosts: {
|
|
12
|
+
readonly dotnetTier: {
|
|
13
|
+
readonly cpuCores: 2;
|
|
14
|
+
readonly memoryBytes: number;
|
|
15
|
+
};
|
|
16
|
+
readonly vitest: {
|
|
17
|
+
readonly cpuCores: 1;
|
|
18
|
+
readonly memoryBytes: number;
|
|
19
|
+
};
|
|
20
|
+
readonly playwright: {
|
|
21
|
+
readonly cpuCores: 2;
|
|
22
|
+
readonly memoryBytes: number;
|
|
23
|
+
};
|
|
24
|
+
readonly generic: {
|
|
25
|
+
readonly cpuCores: 1;
|
|
26
|
+
readonly memoryBytes: number;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Maps a pre-publication validation step to its class: `dotnet.tier.<tier>`,
|
|
31
|
+
* `vitest.<suite>`, `playwright.e2e`, or `generic` for everything else.
|
|
32
|
+
*/
|
|
33
|
+
export declare function classifyValidationStep(checkId: string, command: string, args: readonly string[]): HostLoadStepClassification;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Step classes and conservative default costs for governed heavy steps
|
|
2
|
+
// (decision 0013). Step 4 replaces the defaults with P75 estimates from history.
|
|
3
|
+
const gib = 1024 ** 3;
|
|
4
|
+
export const defaultHostLoadStepCosts = {
|
|
5
|
+
dotnetTier: { cpuCores: 2, memoryBytes: 3 * gib },
|
|
6
|
+
vitest: { cpuCores: 1, memoryBytes: 1.5 * gib },
|
|
7
|
+
playwright: { cpuCores: 2, memoryBytes: 2 * gib },
|
|
8
|
+
generic: { cpuCores: 1, memoryBytes: 1 * gib },
|
|
9
|
+
};
|
|
10
|
+
const dotnetTierScripts = ["scripts/run-dotnet-test-tier.ps1", "scripts/run-dotnet-test-tier-set.ps1"];
|
|
11
|
+
const affectedDotnetScript = "scripts/run-affected-dotnet-tests.mjs";
|
|
12
|
+
const vitestSuites = new Set(["client", "bridge"]);
|
|
13
|
+
/**
|
|
14
|
+
* Maps a pre-publication validation step to its class: `dotnet.tier.<tier>`,
|
|
15
|
+
* `vitest.<suite>`, `playwright.e2e`, or `generic` for everything else.
|
|
16
|
+
*/
|
|
17
|
+
export function classifyValidationStep(checkId, command, args) {
|
|
18
|
+
const normalized = args.map(argument => argument.replaceAll("\\", "/"));
|
|
19
|
+
if (checkId === "playwright-e2e") {
|
|
20
|
+
return { stepClass: "playwright.e2e", cost: defaultHostLoadStepCosts.playwright };
|
|
21
|
+
}
|
|
22
|
+
if (normalized.some(argument => dotnetTierScripts.some(script => argument.endsWith(script)))) {
|
|
23
|
+
return { stepClass: `dotnet.tier.${stepClassSegment(tierArgument(normalized))}`, cost: defaultHostLoadStepCosts.dotnetTier };
|
|
24
|
+
}
|
|
25
|
+
if (normalized.some(argument => argument.endsWith(affectedDotnetScript))) {
|
|
26
|
+
return { stepClass: "dotnet.tier.affected", cost: defaultHostLoadStepCosts.dotnetTier };
|
|
27
|
+
}
|
|
28
|
+
if (vitestSuites.has(checkId) && isTestScript(command, normalized)) {
|
|
29
|
+
return { stepClass: `vitest.${checkId}`, cost: defaultHostLoadStepCosts.vitest };
|
|
30
|
+
}
|
|
31
|
+
return { stepClass: "generic", cost: defaultHostLoadStepCosts.generic };
|
|
32
|
+
}
|
|
33
|
+
function tierArgument(args) {
|
|
34
|
+
const index = args.findIndex(argument => /^-(tier|tiers)$/iu.test(argument));
|
|
35
|
+
return index >= 0 ? args[index + 1] ?? "unknown" : "unknown";
|
|
36
|
+
}
|
|
37
|
+
function isTestScript(command, args) {
|
|
38
|
+
if (!["npm", "pnpm", "npx", "vitest"].includes(command))
|
|
39
|
+
return false;
|
|
40
|
+
const last = args.at(-1);
|
|
41
|
+
return last === "test" || command === "vitest" || args.includes("vitest");
|
|
42
|
+
}
|
|
43
|
+
function stepClassSegment(value) {
|
|
44
|
+
const segment = value.trim().toLowerCase().replace(/[^a-z0-9]+/gu, "-").replace(/^-+|-+$/gu, "");
|
|
45
|
+
return segment || "unknown";
|
|
46
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { ForgeFailureError } from "../session/session.types.js";
|
|
2
|
+
import type { HostLoadStepCost } from "./host-load-step-class.js";
|
|
3
|
+
/**
|
|
4
|
+
* File-backed token ledger for the host load governor (decision 0013). Every
|
|
5
|
+
* process on the host that runs heavy steps shares it through the coordinator
|
|
6
|
+
* state root. Each token names its owner (pid, OS process birth marker and a
|
|
7
|
+
* per-process instance id), carries a fencing token, and holds an `expiresAt`
|
|
8
|
+
* lease the owner renews. A token is reclaimed only when its owner is provably
|
|
9
|
+
* gone. A lapsed lease with a live owner fences the token instead: it keeps its
|
|
10
|
+
* reserved cost (the owner's step may still be running), renewals are refused
|
|
11
|
+
* so the owner stops that step, and the capacity returns when the owner
|
|
12
|
+
* releases it or exits.
|
|
13
|
+
*/
|
|
14
|
+
export type HostLoadTokenPriority = "foreground" | "background";
|
|
15
|
+
export interface HostLoadTokenOwner {
|
|
16
|
+
readonly pid: number;
|
|
17
|
+
/** OS-issued birth marker (Linux boot id + start ticks); null where unsupported. */
|
|
18
|
+
readonly processIdentity: string | null;
|
|
19
|
+
/** Random per process: a reused pid in a new process never matches it. */
|
|
20
|
+
readonly processInstanceId: string;
|
|
21
|
+
}
|
|
22
|
+
export interface HostLoadToken {
|
|
23
|
+
readonly tokenId: string;
|
|
24
|
+
readonly fencingToken: number;
|
|
25
|
+
readonly stepClass: string;
|
|
26
|
+
readonly label: string;
|
|
27
|
+
readonly cost: HostLoadStepCost;
|
|
28
|
+
readonly dispatchAttemptId: string | null;
|
|
29
|
+
readonly priority: HostLoadTokenPriority;
|
|
30
|
+
readonly owner: HostLoadTokenOwner;
|
|
31
|
+
readonly admittedAt: string;
|
|
32
|
+
readonly expiresAt: string;
|
|
33
|
+
/** Set when the lease lapsed while the owner was still live: cost kept, renewal refused. */
|
|
34
|
+
readonly fencedAt?: string | null;
|
|
35
|
+
}
|
|
36
|
+
export interface HostLoadTokenRequest {
|
|
37
|
+
readonly stepClass: string;
|
|
38
|
+
readonly label: string;
|
|
39
|
+
readonly cost: HostLoadStepCost;
|
|
40
|
+
readonly dispatchAttemptId: string | null;
|
|
41
|
+
readonly priority: HostLoadTokenPriority;
|
|
42
|
+
}
|
|
43
|
+
/** The live (post-reclaim) token set inside one locked ledger transaction. */
|
|
44
|
+
export interface HostLoadLedgerTransaction {
|
|
45
|
+
readonly tokens: readonly HostLoadToken[];
|
|
46
|
+
/**
|
|
47
|
+
* Set for one lease after a corrupt ledger was quarantined: owners are still
|
|
48
|
+
* restoring the reservations it lost, so no new step may be admitted yet.
|
|
49
|
+
*/
|
|
50
|
+
readonly recoveringUntil: string | null;
|
|
51
|
+
issue(request: HostLoadTokenRequest): HostLoadToken;
|
|
52
|
+
renew(tokenId: string, fencingToken: number): HostLoadToken | null;
|
|
53
|
+
remove(tokenId: string, fencingToken: number): boolean;
|
|
54
|
+
}
|
|
55
|
+
export interface HostLoadOwnerLiveness {
|
|
56
|
+
isLive(owner: HostLoadTokenOwner): boolean;
|
|
57
|
+
}
|
|
58
|
+
export interface HostLoadTokenLedgerOptions {
|
|
59
|
+
readonly stateRoot: string;
|
|
60
|
+
readonly leaseMs: number;
|
|
61
|
+
readonly now?: () => number;
|
|
62
|
+
readonly liveness?: HostLoadOwnerLiveness;
|
|
63
|
+
/** Owner recorded on tokens this ledger issues; defaults to the current process (resolved lazily). */
|
|
64
|
+
readonly owner?: HostLoadTokenOwner;
|
|
65
|
+
readonly lockTimeoutMs?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Tokens this owner still holds for running steps. Each transaction renews
|
|
68
|
+
* them (or restores one another process reclaimed during a stall) before it
|
|
69
|
+
* reclaims anything, so a running step's cost is never silently dropped.
|
|
70
|
+
*/
|
|
71
|
+
readonly heldTokens?: () => readonly HostLoadToken[];
|
|
72
|
+
/**
|
|
73
|
+
* After each committed transaction: which held tokens were renewed, and which
|
|
74
|
+
* are fenced (lease lapsed, or lost and restored), so the owner stops those steps.
|
|
75
|
+
*/
|
|
76
|
+
readonly onHeldTokens?: (outcome: {
|
|
77
|
+
readonly renewed: readonly HostLoadToken[];
|
|
78
|
+
readonly fenced: readonly HostLoadToken[];
|
|
79
|
+
}) => void;
|
|
80
|
+
/** Test seam passed to the ledger lock. */
|
|
81
|
+
readonly beforeStaleLockBreak?: () => Promise<void>;
|
|
82
|
+
}
|
|
83
|
+
export declare class HostCapacityTokenFencedError extends ForgeFailureError {
|
|
84
|
+
readonly tokenId: string;
|
|
85
|
+
readonly code = "host_capacity_token_fenced";
|
|
86
|
+
constructor(tokenId: string, action: "renew" | "release" | "released_fenced");
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* This process as a token owner, computed once. The birth marker is read only
|
|
90
|
+
* on Linux (a cheap /proc read); elsewhere it would mean a synchronous process
|
|
91
|
+
* spawn, so pid reuse there is covered by the lease and the instance id.
|
|
92
|
+
*/
|
|
93
|
+
export declare function currentHostLoadTokenOwner(): HostLoadTokenOwner;
|
|
94
|
+
/** Owner liveness from the OS: dead pid, or a pid reused by a different process, is gone. */
|
|
95
|
+
export declare const processHostLoadOwnerLiveness: HostLoadOwnerLiveness;
|
|
96
|
+
export declare class HostLoadTokenLedger {
|
|
97
|
+
private readonly options;
|
|
98
|
+
private readonly root;
|
|
99
|
+
private readonly ledgerPath;
|
|
100
|
+
private readonly now;
|
|
101
|
+
private readonly liveness;
|
|
102
|
+
private resolvedOwner;
|
|
103
|
+
private readonly lock;
|
|
104
|
+
private tail;
|
|
105
|
+
constructor(options: HostLoadTokenLedgerOptions);
|
|
106
|
+
private get owner();
|
|
107
|
+
/**
|
|
108
|
+
* Runs `action` under the cross-process ledger lock against the live token
|
|
109
|
+
* set. Dead or expired tokens are reclaimed first, so every admission
|
|
110
|
+
* decision sees only real reservations.
|
|
111
|
+
*/
|
|
112
|
+
transaction<T>(action: (transaction: HostLoadLedgerTransaction) => T | Promise<T>): Promise<{
|
|
113
|
+
readonly result: T;
|
|
114
|
+
readonly reclaimed: readonly HostLoadToken[];
|
|
115
|
+
}>;
|
|
116
|
+
reclaim(): Promise<readonly HostLoadToken[]>;
|
|
117
|
+
list(): Promise<readonly HostLoadToken[]>;
|
|
118
|
+
/** Extends a token's lease. A fenced, reclaimed or superseded token is rejected. */
|
|
119
|
+
renew(tokenId: string, fencingToken: number): Promise<HostLoadToken>;
|
|
120
|
+
/**
|
|
121
|
+
* Returns a token. A reclaimed or superseded token is rejected, never released
|
|
122
|
+
* twice. A fenced token is removed (its owner's step is over) but still
|
|
123
|
+
* reported as fenced.
|
|
124
|
+
*/
|
|
125
|
+
release(tokenId: string, fencingToken: number): Promise<void>;
|
|
126
|
+
private lockedTransaction;
|
|
127
|
+
private read;
|
|
128
|
+
/**
|
|
129
|
+
* A corrupt ledger is kept aside for inspection and none of its tokens are
|
|
130
|
+
* trusted. Admissions pause for one lease while live owners, which renew
|
|
131
|
+
* several times per lease, put their running steps' reservations back.
|
|
132
|
+
*/
|
|
133
|
+
private quarantine;
|
|
134
|
+
private write;
|
|
135
|
+
}
|