@loucompanion/forge-bridge 0.1.4-dev.e3697e9f5c98 → 0.1.4-dev.f22f5e3d01fe
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 +13 -0
- package/dist/bridge/bridge-chat-provider-command.d.ts +16 -0
- package/dist/bridge/bridge-chat-provider-command.js +125 -0
- package/dist/bridge/bridge.service.d.ts +1 -1
- package/dist/bridge/bridge.service.js +4 -110
- package/dist/bridge/runner-host-capabilities.d.ts +3 -0
- package/dist/bridge/runner-host-capabilities.js +13 -0
- package/dist/runtime/host-facts.d.ts +2 -1
- package/dist/runtime/host-load-admission.d.ts +9 -2
- package/dist/runtime/host-load-child-environment.d.ts +11 -6
- package/dist/runtime/host-load-child-environment.js +19 -10
- package/dist/runtime/host-load-governor-config.d.ts +10 -1
- package/dist/runtime/host-load-governor-config.js +7 -0
- package/dist/runtime/host-load-governor.d.ts +10 -0
- package/dist/runtime/host-load-governor.js +125 -34
- package/dist/runtime/host-load-sampler.d.ts +26 -3
- package/dist/runtime/host-load-sampler.js +61 -2
- package/dist/runtime/host-load-step-class.d.ts +3 -1
- package/dist/runtime/host-load-step-class.js +60 -5
- package/dist/runtime/host-load-test-slot.d.ts +43 -0
- package/dist/runtime/host-load-test-slot.js +81 -0
- package/dist/runtime/host-load-token-ledger.d.ts +26 -0
- package/dist/runtime/host-load-token-ledger.js +93 -61
- package/dist/runtime/host-load-windows-cpu.d.ts +34 -0
- package/dist/runtime/host-load-windows-cpu.js +85 -0
- package/dist/runtime/host-runtime-lease.d.ts +1 -1
- package/dist/runtime/host-runtime-lease.js +1 -1
- package/dist/sandbox/host-capacity-broker.d.ts +7 -1
- package/dist/sandbox/host-capacity-broker.js +23 -4
- package/dist/session/architect-waits.d.ts +2 -0
- package/dist/session/architect-waits.js +138 -0
- package/dist/session/managed-attempt-tools.js +8 -9
- package/dist/session/managed-command-host-load.d.ts +2 -0
- package/dist/session/managed-command-host-load.js +2 -1
- package/dist/session/phase-completion.schemas.js +25 -23
- package/dist/session/phase-completion.validation.js +3 -1
- package/dist/session/pre-publication-pass.d.ts +2 -1
- package/dist/session/pre-publication-pass.js +8 -5
- package/dist/session/pre-publication-validation.d.ts +2 -0
- package/dist/session/pre-publication-validation.js +30 -4
- package/dist/session/provider-cli/claude-background-work.d.ts +66 -0
- package/dist/session/provider-cli/claude-background-work.js +180 -0
- package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
- package/dist/session/provider-cli/provider-cli.child-environment.js +73 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.d.ts +3 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.js +37 -0
- package/dist/session/provider-cli/provider-cli.process.d.ts +13 -22
- package/dist/session/provider-cli/provider-cli.process.js +36 -337
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +2 -0
- package/dist/session/provider-cli/provider-cli.prompts.js +45 -8
- package/dist/session/provider-cli/provider-cli.runtime.js +7 -0
- package/dist/session/provider-cli/provider-cli.service.js +2 -8
- package/dist/session/provider-cli/provider-cli.windows-environment.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.windows-environment.js +151 -0
- package/dist/session/provider-cli/provider-in-flight-work.d.ts +4 -0
- package/dist/session/provider-cli/provider-in-flight-work.js +45 -0
- package/dist/session/provider-cli/provider-output-pipes.d.ts +30 -0
- package/dist/session/provider-cli/provider-output-pipes.js +67 -0
- package/dist/session/provider-cli/provider-output-state.d.ts +2 -0
- package/dist/session/provider-cli/provider-output-state.js +6 -0
- package/dist/session/sandbox-host-capacity-gate.js +12 -3
- package/dist/session/session.types.js +1 -1
- package/dist/shared/process/windows-app-execution-alias.d.ts +19 -0
- package/dist/shared/process/windows-app-execution-alias.js +45 -0
- package/dist/shared/process/windows-launch.d.ts +18 -0
- package/dist/shared/process/windows-launch.js +68 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ForgeFailureError } from "../session/session.types.js";
|
|
2
2
|
import type { HostLoadStepCost } from "./host-load-step-class.js";
|
|
3
|
+
import { type HostTestSlotTicket, type HostTestSlotTicketRequest } from "./host-load-test-slot.js";
|
|
3
4
|
import { type HostLoadStepProcess, type HostLoadStepTerminator } from "./host-load-step-reaper.js";
|
|
4
5
|
/**
|
|
5
6
|
* File-backed token ledger for the host load governor (decision 0013). Every
|
|
@@ -40,6 +41,12 @@ export interface HostLoadToken {
|
|
|
40
41
|
readonly degradedAt?: string | null;
|
|
41
42
|
/** Set when the lease lapsed while the owner was still live: cost kept, renewal refused. */
|
|
42
43
|
readonly fencedAt?: string | null;
|
|
44
|
+
/**
|
|
45
|
+
* Set when the step ended but part of its process tree could not be proven
|
|
46
|
+
* stopped: the token is kept until it is (or until attempt teardown), and the
|
|
47
|
+
* attempt's own later steps do not wait for it (decision 0018).
|
|
48
|
+
*/
|
|
49
|
+
readonly retainedAt?: string | null;
|
|
43
50
|
}
|
|
44
51
|
export interface HostLoadTokenRequest {
|
|
45
52
|
readonly stepClass: string;
|
|
@@ -56,11 +63,17 @@ export interface HostLoadLedgerTransaction {
|
|
|
56
63
|
* restoring the reservations it lost, so no new step may be admitted yet.
|
|
57
64
|
*/
|
|
58
65
|
readonly recoveringUntil: string | null;
|
|
66
|
+
/** Live host test slot tickets of every owner (decision 0018). */
|
|
67
|
+
readonly queue: readonly HostTestSlotTicket[];
|
|
59
68
|
issue(request: HostLoadTokenRequest): HostLoadToken;
|
|
69
|
+
/** Takes an admitted step's ticket out of the queue in the same commit as its token. */
|
|
70
|
+
removeTicket(ticketId: string): void;
|
|
60
71
|
renew(tokenId: string, fencingToken: number): HostLoadToken | null;
|
|
61
72
|
remove(tokenId: string, fencingToken: number): boolean;
|
|
62
73
|
/** Records the step's process tree on its token; null when the token is fenced. */
|
|
63
74
|
attach(tokenId: string, fencingToken: number, stepProcess: HostLoadStepProcess): HostLoadToken | null;
|
|
75
|
+
/** Marks the token kept after its step ended (`retainedAt`), fenced or not; null when it is gone. */
|
|
76
|
+
retain(tokenId: string, fencingToken: number): HostLoadToken | null;
|
|
64
77
|
}
|
|
65
78
|
export interface HostLoadTransactionOutcome<T> {
|
|
66
79
|
readonly result: T;
|
|
@@ -91,6 +104,11 @@ export interface HostLoadTokenLedgerOptions {
|
|
|
91
104
|
* cost: it is put back (fenced, unless a quarantine is still recovering).
|
|
92
105
|
*/
|
|
93
106
|
readonly heldTokens?: () => readonly HostLoadToken[];
|
|
107
|
+
/**
|
|
108
|
+
* The steps this owner still waits on for the host test slot (decision 0018). Each
|
|
109
|
+
* transaction renews their tickets and drops the owner's other tickets.
|
|
110
|
+
*/
|
|
111
|
+
readonly waitingTickets?: () => readonly HostTestSlotTicketRequest[];
|
|
94
112
|
/** Stops a reclaimed token's step; defaults to the exact process-tree reaper. */
|
|
95
113
|
readonly terminateStep?: HostLoadStepTerminator;
|
|
96
114
|
/** Host suspend counter (test seam); defaults to hostSuspendedMs. */
|
|
@@ -145,6 +163,14 @@ export declare class HostLoadTokenLedger {
|
|
|
145
163
|
*/
|
|
146
164
|
release(tokenId: string, fencingToken: number): Promise<void>;
|
|
147
165
|
private lockedTransaction;
|
|
166
|
+
/** The live token set and host test slot queue one locked transaction reads and changes. */
|
|
167
|
+
private transactionView;
|
|
168
|
+
/**
|
|
169
|
+
* The host test slot queue (decision 0018): other owners' tickets while their owner
|
|
170
|
+
* lives and renews them, and this owner's current waiters, renewed. A ticket this
|
|
171
|
+
* owner no longer waits on (admitted, cancelled, timed out) leaves the queue here.
|
|
172
|
+
*/
|
|
173
|
+
private liveQueue;
|
|
148
174
|
/**
|
|
149
175
|
* Decides every token another owner can no longer vouch for (decision 0013):
|
|
150
176
|
* - owner alive but its lease lapsed (it stalled): the token is fenced, never
|
|
@@ -5,6 +5,7 @@ import path from "node:path";
|
|
|
5
5
|
import { ForgeFailureError } from "../session/session.types.js";
|
|
6
6
|
import { captureHostProcessIdentity } from "../shared/process/host-process-supervisor.js";
|
|
7
7
|
import { HostCapacityLedgerUnavailableError, HostLoadLedgerLock } from "./host-load-ledger-lock.js";
|
|
8
|
+
import { validHostTestSlotTicket } from "./host-load-test-slot.js";
|
|
8
9
|
import { terminateHostLoadStepProcess, validHostLoadStepProcess, } from "./host-load-step-reaper.js";
|
|
9
10
|
const processInstanceId = randomUUID();
|
|
10
11
|
const maximumLedgerBytes = 1024 * 1024;
|
|
@@ -160,10 +161,9 @@ export class HostLoadTokenLedger {
|
|
|
160
161
|
const { ledger, quarantined } = await this.read();
|
|
161
162
|
const nowMs = this.now();
|
|
162
163
|
const nowIso = new Date(nowMs).toISOString();
|
|
163
|
-
const expiresAt = () => new Date(this.now() + this.options.leaseMs).toISOString();
|
|
164
164
|
const suspendedMs = this.suspendedMs();
|
|
165
165
|
// Each lease records the host's suspend counter, so time asleep never counts as a missed renewal.
|
|
166
|
-
const lease = () => ({ expiresAt:
|
|
166
|
+
const lease = () => ({ expiresAt: new Date(this.now() + this.options.leaseMs).toISOString(), suspendedMsAtRenewal: suspendedMs });
|
|
167
167
|
const recoveringUntil = ledger.recoveringUntil && Date.parse(ledger.recoveringUntil) > nowMs
|
|
168
168
|
? ledger.recoveringUntil
|
|
169
169
|
: null;
|
|
@@ -183,72 +183,31 @@ export class HostLoadTokenLedger {
|
|
|
183
183
|
...restored,
|
|
184
184
|
];
|
|
185
185
|
const { reclaimed, degraded, fencedNow } = await this.reapExpired(current, nowMs, suspendedMs, nowIso);
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
issue: request => {
|
|
196
|
-
const token = {
|
|
197
|
-
tokenId: randomUUID(),
|
|
198
|
-
fencingToken: nextFencingToken,
|
|
199
|
-
stepClass: request.stepClass,
|
|
200
|
-
label: request.label,
|
|
201
|
-
cost: { cpuCores: request.cost.cpuCores, memoryBytes: request.cost.memoryBytes },
|
|
202
|
-
dispatchAttemptId: request.dispatchAttemptId,
|
|
203
|
-
priority: request.priority,
|
|
204
|
-
owner: this.owner,
|
|
205
|
-
admittedAt: new Date(this.now()).toISOString(),
|
|
206
|
-
...lease(),
|
|
207
|
-
};
|
|
208
|
-
nextFencingToken += 1;
|
|
209
|
-
tokens = [...tokens, token];
|
|
210
|
-
changed = true;
|
|
211
|
-
return token;
|
|
212
|
-
},
|
|
213
|
-
renew: (tokenId, fencingToken) => {
|
|
214
|
-
const index = tokens.findIndex(token => token.tokenId === tokenId && token.fencingToken === fencingToken);
|
|
215
|
-
if (index < 0 || tokens[index].fencedAt)
|
|
216
|
-
return null;
|
|
217
|
-
const renewed = { ...tokens[index], ...lease() };
|
|
218
|
-
tokens = tokens.map((token, position) => position === index ? renewed : token);
|
|
219
|
-
changed = true;
|
|
220
|
-
return renewed;
|
|
221
|
-
},
|
|
222
|
-
attach: (tokenId, fencingToken, stepProcess) => {
|
|
223
|
-
const index = tokens.findIndex(token => token.tokenId === tokenId && token.fencingToken === fencingToken);
|
|
224
|
-
if (index < 0 || tokens[index].fencedAt)
|
|
225
|
-
return null;
|
|
226
|
-
const attached = { ...tokens[index], stepProcess };
|
|
227
|
-
tokens = tokens.map((token, position) => position === index ? attached : token);
|
|
228
|
-
changed = true;
|
|
229
|
-
return attached;
|
|
230
|
-
},
|
|
231
|
-
remove: (tokenId, fencingToken) => {
|
|
232
|
-
const before = tokens.length;
|
|
233
|
-
tokens = tokens.filter(token => !(token.tokenId === tokenId && token.fencingToken === fencingToken));
|
|
234
|
-
changed ||= tokens.length !== before;
|
|
235
|
-
return tokens.length !== before;
|
|
236
|
-
},
|
|
186
|
+
const queue = this.liveQueue(ledger.slotQueue ?? [], nowMs, lease().expiresAt);
|
|
187
|
+
const state = {
|
|
188
|
+
tokens: current
|
|
189
|
+
.filter(token => !reclaimed.includes(token))
|
|
190
|
+
.map(token => degraded.get(token) ?? fencedNow.get(token) ?? token),
|
|
191
|
+
queue: queue.tickets,
|
|
192
|
+
nextFencingToken: Math.max(ledger.nextFencingToken, ...restored.map(token => token.fencingToken + 1)),
|
|
193
|
+
changed: quarantined || reclaimed.length > 0 || degraded.size > 0 || fencedNow.size > 0 || held.length > 0
|
|
194
|
+
|| queue.changed || (ledger.recoveringUntil !== undefined && recoveringUntil === null),
|
|
237
195
|
};
|
|
238
|
-
const result = await action(
|
|
239
|
-
if (changed) {
|
|
196
|
+
const result = await action(this.transactionView(state, recoveringUntil, lease));
|
|
197
|
+
if (state.changed) {
|
|
240
198
|
// A lock broken under us (a stalled holder taken for dead) must not commit.
|
|
241
199
|
if (!await this.lock.holds(lockToken)) {
|
|
242
200
|
throw new HostCapacityLedgerUnavailableError("the ledger lock was lost before the update could be written.");
|
|
243
201
|
}
|
|
244
202
|
await this.write({
|
|
245
203
|
schemaVersion: 1,
|
|
246
|
-
nextFencingToken,
|
|
247
|
-
tokens,
|
|
204
|
+
nextFencingToken: state.nextFencingToken,
|
|
205
|
+
tokens: state.tokens,
|
|
206
|
+
...state.queue.length > 0 ? { slotQueue: state.queue } : {},
|
|
248
207
|
...recoveringUntil ? { recoveringUntil } : {},
|
|
249
208
|
});
|
|
250
209
|
}
|
|
251
|
-
const heldNow = tokens.filter(token => heldIds.has(key(token)));
|
|
210
|
+
const heldNow = state.tokens.filter(token => heldIds.has(key(token)));
|
|
252
211
|
return {
|
|
253
212
|
result,
|
|
254
213
|
reclaimed,
|
|
@@ -260,6 +219,74 @@ export class HostLoadTokenLedger {
|
|
|
260
219
|
await this.lock.release(lockToken);
|
|
261
220
|
}
|
|
262
221
|
}
|
|
222
|
+
/** The live token set and host test slot queue one locked transaction reads and changes. */
|
|
223
|
+
transactionView(state, recoveringUntil, lease) {
|
|
224
|
+
const replace = (tokenId, fencingToken, change, evenFenced = false) => {
|
|
225
|
+
const index = state.tokens.findIndex(token => token.tokenId === tokenId && token.fencingToken === fencingToken);
|
|
226
|
+
if (index < 0 || (state.tokens[index].fencedAt && !evenFenced))
|
|
227
|
+
return null;
|
|
228
|
+
const changed = change(state.tokens[index]);
|
|
229
|
+
state.tokens = state.tokens.map((token, position) => position === index ? changed : token);
|
|
230
|
+
state.changed = true;
|
|
231
|
+
return changed;
|
|
232
|
+
};
|
|
233
|
+
return {
|
|
234
|
+
get tokens() { return state.tokens; },
|
|
235
|
+
get queue() { return state.queue; },
|
|
236
|
+
recoveringUntil,
|
|
237
|
+
issue: request => {
|
|
238
|
+
const token = {
|
|
239
|
+
tokenId: randomUUID(),
|
|
240
|
+
fencingToken: state.nextFencingToken,
|
|
241
|
+
stepClass: request.stepClass,
|
|
242
|
+
label: request.label,
|
|
243
|
+
cost: { cpuCores: request.cost.cpuCores, memoryBytes: request.cost.memoryBytes },
|
|
244
|
+
dispatchAttemptId: request.dispatchAttemptId,
|
|
245
|
+
priority: request.priority,
|
|
246
|
+
owner: this.owner,
|
|
247
|
+
admittedAt: new Date(this.now()).toISOString(),
|
|
248
|
+
...lease(),
|
|
249
|
+
};
|
|
250
|
+
state.nextFencingToken += 1;
|
|
251
|
+
state.tokens = [...state.tokens, token];
|
|
252
|
+
state.changed = true;
|
|
253
|
+
return token;
|
|
254
|
+
},
|
|
255
|
+
renew: (tokenId, fencingToken) => replace(tokenId, fencingToken, token => ({ ...token, ...lease() })),
|
|
256
|
+
attach: (tokenId, fencingToken, stepProcess) => replace(tokenId, fencingToken, token => ({ ...token, stepProcess })),
|
|
257
|
+
// A fenced token's step can end unproven too: the marker is metadata, not a renewal.
|
|
258
|
+
retain: (tokenId, fencingToken) => replace(tokenId, fencingToken, token => ({ ...token, retainedAt: new Date(this.now()).toISOString() }), true),
|
|
259
|
+
remove: (tokenId, fencingToken) => {
|
|
260
|
+
const before = state.tokens.length;
|
|
261
|
+
state.tokens = state.tokens.filter(token => !(token.tokenId === tokenId && token.fencingToken === fencingToken));
|
|
262
|
+
state.changed ||= state.tokens.length !== before;
|
|
263
|
+
return state.tokens.length !== before;
|
|
264
|
+
},
|
|
265
|
+
removeTicket: ticketId => {
|
|
266
|
+
const before = state.queue.length;
|
|
267
|
+
state.queue = state.queue.filter(ticket => ticket.ticketId !== ticketId);
|
|
268
|
+
state.changed ||= state.queue.length !== before;
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* The host test slot queue (decision 0018): other owners' tickets while their owner
|
|
274
|
+
* lives and renews them, and this owner's current waiters, renewed. A ticket this
|
|
275
|
+
* owner no longer waits on (admitted, cancelled, timed out) leaves the queue here.
|
|
276
|
+
*/
|
|
277
|
+
liveQueue(stored, nowMs, expiresAt) {
|
|
278
|
+
const owner = this.owner;
|
|
279
|
+
const mine = (ticket) => ticket.owner.pid === owner.pid
|
|
280
|
+
&& ticket.owner.processInstanceId === owner.processInstanceId;
|
|
281
|
+
const others = stored.filter(ticket => !mine(ticket)
|
|
282
|
+
&& Date.parse(ticket.expiresAt) > nowMs
|
|
283
|
+
&& this.liveness.isLive(ticket.owner));
|
|
284
|
+
const waiting = (this.options.waitingTickets?.() ?? []).map(ticket => ({ ...ticket, owner, expiresAt }));
|
|
285
|
+
return {
|
|
286
|
+
tickets: [...others, ...waiting],
|
|
287
|
+
changed: waiting.length > 0 || others.length + waiting.length !== stored.length,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
263
290
|
/**
|
|
264
291
|
* Decides every token another owner can no longer vouch for (decision 0013):
|
|
265
292
|
* - owner alive but its lease lapsed (it stalled): the token is fenced, never
|
|
@@ -342,6 +369,7 @@ export class HostLoadTokenLedger {
|
|
|
342
369
|
schemaVersion: 1,
|
|
343
370
|
nextFencingToken,
|
|
344
371
|
tokens: parsed.tokens,
|
|
372
|
+
slotQueue: Array.isArray(parsed.slotQueue) ? parsed.slotQueue.filter(validHostTestSlotTicket) : [],
|
|
345
373
|
...parsed.recoveringUntil !== undefined ? { recoveringUntil: parsed.recoveringUntil } : {},
|
|
346
374
|
},
|
|
347
375
|
quarantined: false,
|
|
@@ -414,8 +442,12 @@ function validToken(value) {
|
|
|
414
442
|
&& validOwner(token.owner)
|
|
415
443
|
&& typeof token.admittedAt === "string"
|
|
416
444
|
&& typeof token.expiresAt === "string"
|
|
417
|
-
&& (token
|
|
445
|
+
&& validOptionalTokenFields(token);
|
|
446
|
+
}
|
|
447
|
+
/** The fields a token gains as its step runs, ends or loses its owner. */
|
|
448
|
+
function validOptionalTokenFields(token) {
|
|
449
|
+
const optionalTime = (time) => time === undefined || time === null || typeof time === "string";
|
|
450
|
+
return (token.stepProcess === undefined || token.stepProcess === null || validHostLoadStepProcess(token.stepProcess))
|
|
418
451
|
&& (token.suspendedMsAtRenewal === undefined || Number.isFinite(token.suspendedMsAtRenewal))
|
|
419
|
-
&& (token.degradedAt
|
|
420
|
-
&& (token.fencedAt === undefined || token.fencedAt === null || typeof token.fencedAt === "string");
|
|
452
|
+
&& optionalTime(token.degradedAt) && optionalTime(token.fencedAt) && optionalTime(token.retainedAt);
|
|
421
453
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type os from "node:os";
|
|
2
|
+
import type { PressureStallResource } from "./host-pressure-stall.js";
|
|
3
|
+
/** Host CPU time summed over every logical CPU, in milliseconds, from os.cpus(). */
|
|
4
|
+
export interface CpuTimeTotals {
|
|
5
|
+
readonly busyMs: number;
|
|
6
|
+
readonly totalMs: number;
|
|
7
|
+
readonly cpuCount: number;
|
|
8
|
+
}
|
|
9
|
+
export type CpuTimesSource = readonly Partial<Pick<os.CpuInfo, "times">>[];
|
|
10
|
+
/**
|
|
11
|
+
* Host CPU use at which the estimate starts to report pressure. Waits for a CPU
|
|
12
|
+
* are rare below it on a many-core host. 0.6 puts the default backpressure
|
|
13
|
+
* threshold (50%) at 80% host CPU use, the default CPU budget.
|
|
14
|
+
*/
|
|
15
|
+
export declare const windowsCpuPressureKnee = 0.6;
|
|
16
|
+
/** Busy is user + nice + sys + irq; idle is not busy. Null when any CPU lacks its times. */
|
|
17
|
+
export declare function cpuTimeTotals(cpus: CpuTimesSource): CpuTimeTotals | null;
|
|
18
|
+
/** Busy share of all logical CPUs (0-1) between two readings; null across a CPU count change or a counter reset. */
|
|
19
|
+
export declare function cpuUtilisation(previous: CpuTimeTotals | null, current: CpuTimeTotals | null): number | null;
|
|
20
|
+
/** Estimated share of wall time (percent) some runnable thread waited for a CPU, for one window. */
|
|
21
|
+
export declare function estimatedCpuStallPercent(utilisation: number): number;
|
|
22
|
+
/**
|
|
23
|
+
* Keeps 10/60/300-second exponentially decaying averages of the estimate, the
|
|
24
|
+
* way the kernel keeps PSI averages, so the governor's threshold and pressure
|
|
25
|
+
* streak read it exactly like PSI. The first window seeds every average, so a
|
|
26
|
+
* host already busy when the bridge starts reports pressure at once.
|
|
27
|
+
*/
|
|
28
|
+
export declare class WindowsCpuPressureEstimator {
|
|
29
|
+
private averages;
|
|
30
|
+
/** Folds in one window of host CPU use lasting `elapsedMs`. */
|
|
31
|
+
update(utilisation: number, elapsedMs: number): PressureStallResource;
|
|
32
|
+
current(): PressureStallResource | null;
|
|
33
|
+
reset(): void;
|
|
34
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host CPU use at which the estimate starts to report pressure. Waits for a CPU
|
|
3
|
+
* are rare below it on a many-core host. 0.6 puts the default backpressure
|
|
4
|
+
* threshold (50%) at 80% host CPU use, the default CPU budget.
|
|
5
|
+
*/
|
|
6
|
+
export const windowsCpuPressureKnee = 0.6;
|
|
7
|
+
/** Busy is user + nice + sys + irq; idle is not busy. Null when any CPU lacks its times. */
|
|
8
|
+
export function cpuTimeTotals(cpus) {
|
|
9
|
+
if (cpus.length === 0)
|
|
10
|
+
return null;
|
|
11
|
+
let busyMs = 0;
|
|
12
|
+
let idleMs = 0;
|
|
13
|
+
for (const cpu of cpus) {
|
|
14
|
+
const times = cpu.times;
|
|
15
|
+
if (!times)
|
|
16
|
+
return null;
|
|
17
|
+
const values = [times.user, times.nice, times.sys, times.irq, times.idle];
|
|
18
|
+
if (!values.every(value => Number.isFinite(value) && value >= 0))
|
|
19
|
+
return null;
|
|
20
|
+
busyMs += times.user + times.nice + times.sys + times.irq;
|
|
21
|
+
idleMs += times.idle;
|
|
22
|
+
}
|
|
23
|
+
return { busyMs, totalMs: busyMs + idleMs, cpuCount: cpus.length };
|
|
24
|
+
}
|
|
25
|
+
/** Busy share of all logical CPUs (0-1) between two readings; null across a CPU count change or a counter reset. */
|
|
26
|
+
export function cpuUtilisation(previous, current) {
|
|
27
|
+
if (!previous || !current || previous.cpuCount !== current.cpuCount)
|
|
28
|
+
return null;
|
|
29
|
+
const totalDelta = current.totalMs - previous.totalMs;
|
|
30
|
+
const busyDelta = current.busyMs - previous.busyMs;
|
|
31
|
+
if (totalDelta <= 0 || busyDelta < 0)
|
|
32
|
+
return null;
|
|
33
|
+
return Math.min(1, busyDelta / totalDelta);
|
|
34
|
+
}
|
|
35
|
+
/** Estimated share of wall time (percent) some runnable thread waited for a CPU, for one window. */
|
|
36
|
+
export function estimatedCpuStallPercent(utilisation) {
|
|
37
|
+
const share = (utilisation - windowsCpuPressureKnee) / (1 - windowsCpuPressureKnee);
|
|
38
|
+
return Math.min(1, Math.max(0, share)) * 100;
|
|
39
|
+
}
|
|
40
|
+
const averageWindowsSeconds = { avg10: 10, avg60: 60, avg300: 300 };
|
|
41
|
+
/**
|
|
42
|
+
* Keeps 10/60/300-second exponentially decaying averages of the estimate, the
|
|
43
|
+
* way the kernel keeps PSI averages, so the governor's threshold and pressure
|
|
44
|
+
* streak read it exactly like PSI. The first window seeds every average, so a
|
|
45
|
+
* host already busy when the bridge starts reports pressure at once.
|
|
46
|
+
*/
|
|
47
|
+
export class WindowsCpuPressureEstimator {
|
|
48
|
+
averages = null;
|
|
49
|
+
/** Folds in one window of host CPU use lasting `elapsedMs`. */
|
|
50
|
+
update(utilisation, elapsedMs) {
|
|
51
|
+
const stall = estimatedCpuStallPercent(utilisation);
|
|
52
|
+
const previous = this.averages;
|
|
53
|
+
this.averages = previous === null
|
|
54
|
+
? { avg10: stall, avg60: stall, avg300: stall }
|
|
55
|
+
: {
|
|
56
|
+
avg10: decay(previous.avg10, stall, elapsedMs, averageWindowsSeconds.avg10),
|
|
57
|
+
avg60: decay(previous.avg60, stall, elapsedMs, averageWindowsSeconds.avg60),
|
|
58
|
+
avg300: decay(previous.avg300, stall, elapsedMs, averageWindowsSeconds.avg300),
|
|
59
|
+
};
|
|
60
|
+
return this.current();
|
|
61
|
+
}
|
|
62
|
+
current() {
|
|
63
|
+
if (!this.averages)
|
|
64
|
+
return null;
|
|
65
|
+
return {
|
|
66
|
+
some: {
|
|
67
|
+
avg10: roundPercent(this.averages.avg10),
|
|
68
|
+
avg60: roundPercent(this.averages.avg60),
|
|
69
|
+
avg300: roundPercent(this.averages.avg300),
|
|
70
|
+
},
|
|
71
|
+
// `full` (every runnable thread stalled) is not meaningful for CPU and is not estimated.
|
|
72
|
+
full: null,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
reset() {
|
|
76
|
+
this.averages = null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function decay(previous, sample, elapsedMs, windowSeconds) {
|
|
80
|
+
const weight = Math.exp(-Math.max(0, elapsedMs) / (windowSeconds * 1_000));
|
|
81
|
+
return sample + (previous - sample) * weight;
|
|
82
|
+
}
|
|
83
|
+
function roundPercent(value) {
|
|
84
|
+
return Math.round(value * 100) / 100;
|
|
85
|
+
}
|
|
@@ -162,7 +162,7 @@ export declare class HostRuntimeLeaseManager implements HostRuntimeLeaseRuntime
|
|
|
162
162
|
private capacityConfiguration;
|
|
163
163
|
private readonly memoryAvailability;
|
|
164
164
|
private readonly executionRoot;
|
|
165
|
-
/** Host-wide admission for heavy steps (
|
|
165
|
+
/** Host-wide admission for heavy steps (decisions 0013, 0018): the host test slot, and the budget when on. */
|
|
166
166
|
readonly hostLoadGovernor: HostLoadGovernor;
|
|
167
167
|
private requirementsCheck;
|
|
168
168
|
/** Installs the decision 0013 accept-time requirements recheck for host attempts. */
|
|
@@ -71,7 +71,7 @@ export class HostRuntimeLeaseManager {
|
|
|
71
71
|
capacityConfiguration;
|
|
72
72
|
memoryAvailability;
|
|
73
73
|
executionRoot;
|
|
74
|
-
/** Host-wide admission for heavy steps (
|
|
74
|
+
/** Host-wide admission for heavy steps (decisions 0013, 0018): the host test slot, and the budget when on. */
|
|
75
75
|
hostLoadGovernor;
|
|
76
76
|
requirementsCheck = null;
|
|
77
77
|
/** Installs the decision 0013 accept-time requirements recheck for host attempts. */
|
|
@@ -9,7 +9,7 @@ export declare const sandboxHostCapacityResponseRoot = "/run/forge/host-capacity
|
|
|
9
9
|
export declare const sandboxHostCapacityResponseOwnerUid = 0;
|
|
10
10
|
export declare const sandboxHostCapacityMaxWaitEnvironment = "FORGE_HOST_LOAD_BROKER_MAX_WAIT_MS";
|
|
11
11
|
export interface SandboxHostCapacityRequest {
|
|
12
|
-
readonly action: "acquire" | "release";
|
|
12
|
+
readonly action: "acquire" | "release" | "retain";
|
|
13
13
|
readonly requestId: string;
|
|
14
14
|
readonly stepClass: string;
|
|
15
15
|
readonly label: string;
|
|
@@ -82,6 +82,12 @@ export declare class SandboxHostCapacityBroker {
|
|
|
82
82
|
private takeAllowance;
|
|
83
83
|
private track;
|
|
84
84
|
private acquire;
|
|
85
|
+
/**
|
|
86
|
+
* The sandbox's step ended but part of its tree may still run: the host token is
|
|
87
|
+
* kept, marked retained (so the attempt's own next step does not wait for it),
|
|
88
|
+
* until the sandbox releases the request or exits.
|
|
89
|
+
*/
|
|
90
|
+
private retain;
|
|
85
91
|
private release;
|
|
86
92
|
/** Queues the newest state for a request; at most one delivery per request is in flight. */
|
|
87
93
|
private respond;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { hostLoadStepCostForClass } from "../runtime/host-load-step-class.js";
|
|
2
|
+
import { takesHostTestSlot } from "../runtime/host-load-test-slot.js";
|
|
2
3
|
import { ForgeFailureError } from "../session/session.types.js";
|
|
3
4
|
// Host side of Docker-sandbox admission (decision 0013). The validator inside a
|
|
4
5
|
// container cannot reach the host ledger, so it asks the bridge over the
|
|
@@ -50,11 +51,13 @@ export function parseSandboxHostCapacityRequest(line) {
|
|
|
50
51
|
}
|
|
51
52
|
if (!value || value.type !== sandboxHostCapacityLineType || value.version !== 1)
|
|
52
53
|
return null;
|
|
53
|
-
if (value.action !== "acquire" && value.action !== "release")
|
|
54
|
+
if (value.action !== "acquire" && value.action !== "release" && value.action !== "retain")
|
|
54
55
|
return null;
|
|
55
56
|
if (typeof value.requestId !== "string" || !requestIdPattern.test(value.requestId))
|
|
56
57
|
return null;
|
|
57
|
-
const
|
|
58
|
+
const claimed = typeof value.stepClass === "string" && stepClassPattern.test(value.stepClass) ? value.stepClass : "generic";
|
|
59
|
+
// A sandbox brokers only validation steps; it cannot claim a class that skips the host test slot.
|
|
60
|
+
const stepClass = takesHostTestSlot(claimed) ? claimed : "generic";
|
|
58
61
|
// Diagnostics only, but never control characters or markup from the sandbox.
|
|
59
62
|
const label = typeof value.label === "string" ? value.label.replace(/[^\w .:/@+-]/gu, "_").slice(0, 128) : "";
|
|
60
63
|
return { action: value.action, requestId: value.requestId, stepClass, label };
|
|
@@ -117,7 +120,8 @@ export class SandboxHostCapacityBroker {
|
|
|
117
120
|
if (request.action === "acquire" && !this.tokens.has(request.requestId) && this.tokens.size >= this.maximumOutstanding) {
|
|
118
121
|
this.stop(`more than ${this.maximumOutstanding} host capacity requests outstanding at once`);
|
|
119
122
|
}
|
|
120
|
-
this.track(request.action === "acquire" ? this.acquire(request)
|
|
123
|
+
this.track(request.action === "acquire" ? this.acquire(request)
|
|
124
|
+
: request.action === "retain" ? Promise.resolve(this.retain(request.requestId)) : this.release(request.requestId));
|
|
121
125
|
return true;
|
|
122
126
|
}
|
|
123
127
|
/** The sandbox exited: cancel its waits and return every token it holds. */
|
|
@@ -152,7 +156,7 @@ export class SandboxHostCapacityBroker {
|
|
|
152
156
|
async acquire(request) {
|
|
153
157
|
if (this.closed || this.tokens.has(request.requestId))
|
|
154
158
|
return;
|
|
155
|
-
const entry = { controller: new AbortController(), admission: null, released: false };
|
|
159
|
+
const entry = { controller: new AbortController(), admission: null, released: false, retained: false };
|
|
156
160
|
this.tokens.set(request.requestId, entry);
|
|
157
161
|
let admission;
|
|
158
162
|
try {
|
|
@@ -219,6 +223,9 @@ export class SandboxHostCapacityBroker {
|
|
|
219
223
|
}
|
|
220
224
|
// Registered synchronously after the check above, so no fence can slip between them.
|
|
221
225
|
admission.fenced.addEventListener("abort", () => {
|
|
226
|
+
// Retention stops the step's reservation on purpose; the sandbox already knows.
|
|
227
|
+
if (entry.retained)
|
|
228
|
+
return;
|
|
222
229
|
void this.respond({
|
|
223
230
|
requestId: request.requestId,
|
|
224
231
|
status: "fenced",
|
|
@@ -230,6 +237,18 @@ export class SandboxHostCapacityBroker {
|
|
|
230
237
|
requestId: request.requestId, status: "admitted", waitMs: Math.round(admission.waitMs), reason: admission.decision.reason,
|
|
231
238
|
});
|
|
232
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* The sandbox's step ended but part of its tree may still run: the host token is
|
|
242
|
+
* kept, marked retained (so the attempt's own next step does not wait for it),
|
|
243
|
+
* until the sandbox releases the request or exits.
|
|
244
|
+
*/
|
|
245
|
+
retain(requestId) {
|
|
246
|
+
const entry = this.tokens.get(requestId);
|
|
247
|
+
if (!entry?.admission || entry.released || entry.retained)
|
|
248
|
+
return;
|
|
249
|
+
entry.retained = true;
|
|
250
|
+
entry.admission.retainUntilStopped(async () => entry.released, new Error("The sandbox step's processes could not be proven stopped; its host capacity is kept until the sandbox releases it."));
|
|
251
|
+
}
|
|
233
252
|
async release(requestId) {
|
|
234
253
|
const entry = this.tokens.get(requestId);
|
|
235
254
|
if (!entry || entry.released)
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Decision 0022: a blocked Architect verdict may say what it waits on (a sibling Story, a pull
|
|
2
|
+
// request, or something outside Forge) and which choice it needs from the owner. Forge records
|
|
3
|
+
// Story and pull request waits as dependency edges and re-plans the Story when they merge.
|
|
4
|
+
const architectWaitKinds = ["story", "pull_request", "external"];
|
|
5
|
+
const architectWaitUntil = ["merged"];
|
|
6
|
+
const storyIdPattern = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9a-f]{8,32})$/i;
|
|
7
|
+
// Architect tool properties for non-pass verdicts: revision prose plus the structured waits.
|
|
8
|
+
export function architectNonPassProperties() {
|
|
9
|
+
return {
|
|
10
|
+
revision_questions: {
|
|
11
|
+
type: "array",
|
|
12
|
+
description: "Required for needs_revision when blockers are not used.",
|
|
13
|
+
items: { type: "string" }
|
|
14
|
+
},
|
|
15
|
+
revision_reasons: {
|
|
16
|
+
type: "array",
|
|
17
|
+
description: "Concrete reasons that prevent pass status.",
|
|
18
|
+
items: { type: "string" }
|
|
19
|
+
},
|
|
20
|
+
waits_on: {
|
|
21
|
+
type: "array",
|
|
22
|
+
description: "Only with status blocked. What must merge before this Story can be planned: a sibling Story (kind story, story_id as the full id or its first 8+ hex digits), a pull request (kind pull_request, pr_number), or something outside Forge (kind external). Forge records Story and sibling pull request waits and plans the Story again when they merge; external waits hold for the owner.",
|
|
23
|
+
items: {
|
|
24
|
+
type: "object",
|
|
25
|
+
additionalProperties: false,
|
|
26
|
+
required: ["kind", "until", "reason"],
|
|
27
|
+
properties: {
|
|
28
|
+
kind: { type: "string", enum: [...architectWaitKinds] },
|
|
29
|
+
story_id: { type: "string", description: "Required for kind story." },
|
|
30
|
+
pr_number: { type: "integer", minimum: 1, description: "Required for kind pull_request." },
|
|
31
|
+
until: { type: "string", enum: [...architectWaitUntil] },
|
|
32
|
+
reason: { type: "string", description: "What this Story needs from the blocker." }
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
owner_decision: {
|
|
37
|
+
type: "object",
|
|
38
|
+
description: "Only with status blocked. A choice only the owner can make before the Story can be planned. Forge holds the Story for the owner and never picks an option.",
|
|
39
|
+
additionalProperties: false,
|
|
40
|
+
required: ["question", "options"],
|
|
41
|
+
properties: {
|
|
42
|
+
question: { type: "string" },
|
|
43
|
+
options: {
|
|
44
|
+
type: "array",
|
|
45
|
+
minItems: 2,
|
|
46
|
+
items: {
|
|
47
|
+
type: "object",
|
|
48
|
+
additionalProperties: false,
|
|
49
|
+
required: ["label", "summary"],
|
|
50
|
+
properties: {
|
|
51
|
+
label: { type: "string" },
|
|
52
|
+
summary: { type: "string" }
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
recommended: { type: "string", description: "Label of the recommended option." }
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
// An explicit null is absent, as the API reads it.
|
|
62
|
+
function isPresent(value) {
|
|
63
|
+
return value !== undefined && value !== null;
|
|
64
|
+
}
|
|
65
|
+
function hasArchitectWaitDeclarations(payload) {
|
|
66
|
+
return isPresent(payload.waits_on) || isPresent(payload.owner_decision);
|
|
67
|
+
}
|
|
68
|
+
// Throws an actionable error for a malformed waits_on or owner_decision. Absent (or null) fields are valid.
|
|
69
|
+
export function validateArchitectWaits(payload, status) {
|
|
70
|
+
if (!hasArchitectWaitDeclarations(payload)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (status !== "blocked") {
|
|
74
|
+
throw new Error("waits_on/owner_decision: allowed only with status blocked; correction: remove them, or return status blocked when the plan cannot pass until they are resolved.");
|
|
75
|
+
}
|
|
76
|
+
if (isPresent(payload.waits_on)) {
|
|
77
|
+
validateWaitsOn(payload.waits_on);
|
|
78
|
+
}
|
|
79
|
+
if (isPresent(payload.owner_decision)) {
|
|
80
|
+
validateOwnerDecision(payload.owner_decision);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function validateWaitsOn(value) {
|
|
84
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
85
|
+
throw new Error("waits_on: expected a non-empty array; correction: list each Story, pull request or external dependency, or omit waits_on.");
|
|
86
|
+
}
|
|
87
|
+
value.forEach((entry, index) => validateWait(entry, `waits_on[${index}]`));
|
|
88
|
+
}
|
|
89
|
+
function validateWait(entry, label) {
|
|
90
|
+
const wait = requireRecord(entry, label);
|
|
91
|
+
const kind = wait.kind;
|
|
92
|
+
if (typeof kind !== "string" || !architectWaitKinds.includes(kind)) {
|
|
93
|
+
throw new Error(`${label}.kind: expected one of story, pull_request, external.`);
|
|
94
|
+
}
|
|
95
|
+
if (wait.until !== "merged") {
|
|
96
|
+
throw new Error(`${label}.until: expected "merged".`);
|
|
97
|
+
}
|
|
98
|
+
requireText(wait.reason, `${label}.reason`);
|
|
99
|
+
if (kind === "story") {
|
|
100
|
+
const storyId = requireText(wait.story_id, `${label}.story_id`);
|
|
101
|
+
if (!storyIdPattern.test(storyId)) {
|
|
102
|
+
throw new Error(`${label}.story_id: expected the Story's full id or its first 8+ hex digits; got '${storyId}'.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (kind === "pull_request" && !(typeof wait.pr_number === "number" && Number.isInteger(wait.pr_number) && wait.pr_number > 0)) {
|
|
106
|
+
throw new Error(`${label}.pr_number: expected a positive integer pull request number.`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function validateOwnerDecision(value) {
|
|
110
|
+
const decision = requireRecord(value, "owner_decision");
|
|
111
|
+
requireText(decision.question, "owner_decision.question");
|
|
112
|
+
if (!Array.isArray(decision.options) || decision.options.length < 2) {
|
|
113
|
+
throw new Error("owner_decision.options: expected at least two options, each with a distinct label and a summary.");
|
|
114
|
+
}
|
|
115
|
+
const labels = decision.options.map((option, index) => {
|
|
116
|
+
const record = requireRecord(option, `owner_decision.options[${index}]`);
|
|
117
|
+
requireText(record.summary, `owner_decision.options[${index}].summary`);
|
|
118
|
+
return requireText(record.label, `owner_decision.options[${index}].label`);
|
|
119
|
+
});
|
|
120
|
+
if (new Set(labels).size !== labels.length) {
|
|
121
|
+
throw new Error("owner_decision.options: expected at least two options, each with a distinct label and a summary.");
|
|
122
|
+
}
|
|
123
|
+
if (isPresent(decision.recommended) && !labels.includes(requireText(decision.recommended, "owner_decision.recommended"))) {
|
|
124
|
+
throw new Error("owner_decision.recommended: expected the label of one of the options.");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function requireRecord(value, label) {
|
|
128
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
129
|
+
throw new Error(`${label}: expected an object.`);
|
|
130
|
+
}
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
function requireText(value, label) {
|
|
134
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
135
|
+
throw new Error(`${label}: expected a non-empty string.`);
|
|
136
|
+
}
|
|
137
|
+
return value.trim();
|
|
138
|
+
}
|