@loucompanion/forge-bridge 0.1.4-dev.1ff37038366e → 0.1.4-dev.318bc3bd5b6a
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 +1 -1
- package/dist/cli/cli.help.js +2 -2
- package/dist/runtime/attempt-process-supervisor.d.ts +24 -0
- package/dist/runtime/attempt-process-supervisor.js +77 -3
- package/dist/runtime/host-diagnostics.js +4 -3
- package/dist/runtime/host-runtime-lease.js +50 -9
- package/dist/runtime/managed-command-teardown.d.ts +62 -0
- package/dist/runtime/managed-command-teardown.js +607 -0
- package/dist/runtime/node-host-runtime-adapter.js +5 -4
- package/dist/sandbox/repository-lock.js +3 -2
- package/dist/sandbox/runtime.js +5 -4
- package/dist/session/managed-attempt-tools.d.ts +25 -0
- package/dist/session/managed-attempt-tools.js +216 -18
- package/dist/session/managed-build-coordinator.d.ts +1 -1
- package/dist/session/managed-build-coordinator.js +8 -4
- package/dist/session/phase-completion.runtime.js +22 -17
- package/dist/session/provider-cli/provider-cli.process.d.ts +6 -1
- package/dist/session/provider-cli/provider-cli.process.js +198 -16
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +6 -0
- package/dist/session/provider-cli/provider-cli.runtime.js +11 -0
- package/dist/session/provider-cli/provider-cli.service.js +10 -5
- package/dist/session/provider-cli/provider-usage.d.ts +2 -1
- package/dist/shared/process/host-process-recovery.js +3 -2
- package/dist/shared/process/host-process-supervisor.js +5 -4
- package/dist/shared/time/monotonic.d.ts +1 -0
- package/dist/shared/time/monotonic.js +8 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/dependency-warmer.js +74 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,7 +65,7 @@ Show or change the saved server without editing `bridge.json`:
|
|
|
65
65
|
```bash
|
|
66
66
|
forge-bridge server show
|
|
67
67
|
forge-bridge server set http://localhost:5208
|
|
68
|
-
forge-bridge server set https://
|
|
68
|
+
forge-bridge server set https://forge.example.ts.net:8443
|
|
69
69
|
```
|
|
70
70
|
|
|
71
71
|
`FORGE_URL` overrides the saved server. Changing the saved URL retains runner credentials so aliases for the same deployment keep working; pair again with `--server` when switching to a different Forge deployment.
|
package/dist/cli/cli.help.js
CHANGED
|
@@ -28,7 +28,7 @@ export function helpText() {
|
|
|
28
28
|
"",
|
|
29
29
|
"Examples:",
|
|
30
30
|
" npm install -g @loucompanion/forge-bridge@next",
|
|
31
|
-
" forge-bridge pair 1234-5678 --server https://
|
|
31
|
+
" forge-bridge pair 1234-5678 --server https://forge.example.ts.net:8443 --repo-root \"%USERPROFILE%\\ForgeRepos\"",
|
|
32
32
|
" forge-bridge connect",
|
|
33
33
|
" forge-bridge status --check-updates",
|
|
34
34
|
" forge-bridge server set http://localhost:5208",
|
|
@@ -116,7 +116,7 @@ export function commandHelpText(command) {
|
|
|
116
116
|
"",
|
|
117
117
|
"Examples:",
|
|
118
118
|
" forge-bridge server set http://localhost:5208",
|
|
119
|
-
" forge-bridge server set https://
|
|
119
|
+
" forge-bridge server set https://forge.example.ts.net:8443",
|
|
120
120
|
"",
|
|
121
121
|
].join("\n");
|
|
122
122
|
case "add-repo":
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ProviderProcessHandlers, ProviderProcessRunRequest, ProviderProcessRunner, ProviderProcessRunResult } from "../session/provider-cli/provider-cli.process.js";
|
|
2
|
+
import { type ManagedCommandTeardownOptions, type ManagedCommandTeardownOutcome } from "./managed-command-teardown.js";
|
|
2
3
|
export type AttemptProcessChildStatus = "starting" | "running" | "stopping" | "exited" | "failed";
|
|
3
4
|
export type AttemptProcessSupervisorStatus = "active" | "stopping" | "stopped" | "degraded";
|
|
4
5
|
export interface AttemptProcessSupervisorIdentity {
|
|
@@ -32,6 +33,8 @@ export interface AttemptProcessSupervisorLedger {
|
|
|
32
33
|
readonly status: AttemptProcessSupervisorStatus;
|
|
33
34
|
readonly updatedAt: string;
|
|
34
35
|
readonly children: readonly AttemptProcessChildLedgerEntry[];
|
|
36
|
+
/** A provider-exit managed-command cleanup that failed and was left to attempt teardown. */
|
|
37
|
+
readonly deferredManagedCommandTeardownFailure?: string;
|
|
35
38
|
}
|
|
36
39
|
export interface AttemptProcessChildHandle {
|
|
37
40
|
readonly name: string;
|
|
@@ -46,6 +49,9 @@ export interface AttemptProcessChildHandle {
|
|
|
46
49
|
export interface AttemptProcessSupervisorOptions {
|
|
47
50
|
readonly metadataReadyTimeoutMs?: number;
|
|
48
51
|
readonly signal?: AbortSignal;
|
|
52
|
+
readonly replaceLedgerFile?: (temporaryPath: string, ledgerPath: string) => Promise<void>;
|
|
53
|
+
readonly ledgerReplaceRetryMs?: number;
|
|
54
|
+
readonly managedCommandTeardownGraceMs?: number;
|
|
49
55
|
}
|
|
50
56
|
export interface AttemptProcessChildReadyOptions {
|
|
51
57
|
readonly maxStartAttempts?: number;
|
|
@@ -62,6 +68,9 @@ export declare class AttemptProcessSupervisor {
|
|
|
62
68
|
readonly ledgerPath: string;
|
|
63
69
|
private readonly identity;
|
|
64
70
|
private readonly metadataReadyTimeoutMs;
|
|
71
|
+
private readonly replaceLedgerFile;
|
|
72
|
+
private readonly ledgerReplaceRetryMs;
|
|
73
|
+
private readonly managedCommandTeardownGraceMs;
|
|
65
74
|
private readonly records;
|
|
66
75
|
private readonly currentByName;
|
|
67
76
|
private readonly attemptController;
|
|
@@ -69,6 +78,8 @@ export declare class AttemptProcessSupervisor {
|
|
|
69
78
|
private persistTail;
|
|
70
79
|
private persistenceFailure;
|
|
71
80
|
private stopOperation;
|
|
81
|
+
private managedCommandTeardownTail;
|
|
82
|
+
private deferredManagedCommandTeardownFailure;
|
|
72
83
|
constructor(identity: AttemptProcessSupervisorIdentity, runner: ProviderProcessRunner, options?: AttemptProcessSupervisorOptions);
|
|
73
84
|
startChild(name: string, request: ProviderProcessRunRequest, handlers?: ProviderProcessHandlers, signal?: AbortSignal): Promise<AttemptProcessChildHandle>;
|
|
74
85
|
/**
|
|
@@ -82,10 +93,23 @@ export declare class AttemptProcessSupervisor {
|
|
|
82
93
|
stopChild(name: string, generation: number, fencingToken: string, reason?: unknown): Promise<ProviderProcessRunResult>;
|
|
83
94
|
stopAll(fencingToken: string, reason?: unknown): Promise<void>;
|
|
84
95
|
private performStopAll;
|
|
96
|
+
/**
|
|
97
|
+
* Stops the managed commands this attempt's MCP servers started and finalizes their
|
|
98
|
+
* receipts. With `onlyOrphaned`, a command whose owning MCP server is still alive is
|
|
99
|
+
* left running: it survives wait slices and MCP call boundaries within a live provider.
|
|
100
|
+
*/
|
|
101
|
+
terminateManagedCommands(options: Omit<ManagedCommandTeardownOptions, "graceMs">): Promise<readonly ManagedCommandTeardownOutcome[]>;
|
|
102
|
+
/**
|
|
103
|
+
* After a provider exits, stops the managed commands its MCP server left behind. It never
|
|
104
|
+
* fails the provider's result: a command it cannot prove stopped is recorded in the ledger
|
|
105
|
+
* and left to attempt teardown, which fails closed.
|
|
106
|
+
*/
|
|
107
|
+
stopOrphanedManagedCommands(): Promise<void>;
|
|
85
108
|
snapshot(): AttemptProcessSupervisorLedger;
|
|
86
109
|
providerMemoryTelemetry(): AttemptProcessTreeMemoryTelemetry | null;
|
|
87
110
|
private observeChildReady;
|
|
88
111
|
private settleChild;
|
|
112
|
+
private replaceLedger;
|
|
89
113
|
private assertFence;
|
|
90
114
|
private persist;
|
|
91
115
|
}
|
|
@@ -2,8 +2,15 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { ForgeFailureError, sanitizeFailureError } from "../session/session.types.js";
|
|
5
|
+
import { terminateAttemptManagedCommands, } from "./managed-command-teardown.js";
|
|
6
|
+
import { monotonicNowMs } from "../shared/time/monotonic.js";
|
|
5
7
|
const terminalChildStatuses = new Set(["exited", "failed"]);
|
|
6
8
|
const defaultMetadataReadyTimeoutMs = process.platform === "win32" ? 60_000 : 10_000;
|
|
9
|
+
// Windows refuses to replace a file another process (a scanner or indexer) briefly holds
|
|
10
|
+
// open. One such refusal must not degrade the attempt, so the ledger replace is retried.
|
|
11
|
+
const ledgerReplaceAttempts = 40;
|
|
12
|
+
const defaultLedgerReplaceRetryMs = 25;
|
|
13
|
+
const transientLedgerReplaceCodes = new Set(["EPERM", "EACCES", "EBUSY"]);
|
|
7
14
|
/**
|
|
8
15
|
* Owns the independently supervised process trees that belong to one host
|
|
9
16
|
* dispatch attempt. Each named child gets a distinct Windows Job Object or
|
|
@@ -15,6 +22,9 @@ export class AttemptProcessSupervisor {
|
|
|
15
22
|
ledgerPath;
|
|
16
23
|
identity;
|
|
17
24
|
metadataReadyTimeoutMs;
|
|
25
|
+
replaceLedgerFile;
|
|
26
|
+
ledgerReplaceRetryMs;
|
|
27
|
+
managedCommandTeardownGraceMs;
|
|
18
28
|
records = [];
|
|
19
29
|
currentByName = new Map();
|
|
20
30
|
attemptController = new AbortController();
|
|
@@ -22,10 +32,15 @@ export class AttemptProcessSupervisor {
|
|
|
22
32
|
persistTail = Promise.resolve();
|
|
23
33
|
persistenceFailure;
|
|
24
34
|
stopOperation = null;
|
|
35
|
+
managedCommandTeardownTail = Promise.resolve();
|
|
36
|
+
deferredManagedCommandTeardownFailure;
|
|
25
37
|
constructor(identity, runner, options = {}) {
|
|
26
38
|
this.runner = runner;
|
|
27
39
|
this.identity = validateIdentity(identity);
|
|
28
40
|
this.metadataReadyTimeoutMs = boundedInteger(options.metadataReadyTimeoutMs ?? defaultMetadataReadyTimeoutMs, 100, 120_000, "metadataReadyTimeoutMs");
|
|
41
|
+
this.replaceLedgerFile = options.replaceLedgerFile ?? ((temporaryPath, ledgerPath) => fs.rename(temporaryPath, ledgerPath));
|
|
42
|
+
this.ledgerReplaceRetryMs = options.ledgerReplaceRetryMs ?? defaultLedgerReplaceRetryMs;
|
|
43
|
+
this.managedCommandTeardownGraceMs = options.managedCommandTeardownGraceMs;
|
|
29
44
|
this.ledgerPath = path.join(this.identity.logDirectory, "attempt-process-supervisor.json");
|
|
30
45
|
if (options.signal?.aborted) {
|
|
31
46
|
this.status = "stopping";
|
|
@@ -189,6 +204,14 @@ export class AttemptProcessSupervisor {
|
|
|
189
204
|
const rejected = results.flatMap((result, index) => result.status === "rejected"
|
|
190
205
|
? [sanitizeFailureError(result.reason, active[index].failureRedactionValues)]
|
|
191
206
|
: []);
|
|
207
|
+
// Managed commands run in their own process groups, outside every child tree stopped
|
|
208
|
+
// above. The attempt is not stopped until each of them is proven stopped too.
|
|
209
|
+
try {
|
|
210
|
+
await this.terminateManagedCommands({ reason: "attempt_teardown" });
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
rejected.push(error instanceof Error ? error : new Error(String(error)));
|
|
214
|
+
}
|
|
192
215
|
const terminalStatus = rejected.length > 0 || this.persistenceFailure !== undefined ? "degraded" : "stopped";
|
|
193
216
|
await this.persist({
|
|
194
217
|
...this.snapshot(),
|
|
@@ -202,6 +225,40 @@ export class AttemptProcessSupervisor {
|
|
|
202
225
|
if (this.persistenceFailure !== undefined)
|
|
203
226
|
throw this.persistenceFailure;
|
|
204
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Stops the managed commands this attempt's MCP servers started and finalizes their
|
|
230
|
+
* receipts. With `onlyOrphaned`, a command whose owning MCP server is still alive is
|
|
231
|
+
* left running: it survives wait slices and MCP call boundaries within a live provider.
|
|
232
|
+
*/
|
|
233
|
+
async terminateManagedCommands(options) {
|
|
234
|
+
// One sweep at a time: a provider-exit sweep and attempt teardown would otherwise
|
|
235
|
+
// signal the same groups and write the same receipts concurrently, and stopAll must
|
|
236
|
+
// not report the attempt stopped while an earlier sweep is still writing.
|
|
237
|
+
const sweep = this.managedCommandTeardownTail.then(async () => await terminateAttemptManagedCommands({
|
|
238
|
+
logDirectory: this.identity.logDirectory,
|
|
239
|
+
dispatchAttemptId: this.identity.dispatchAttemptId,
|
|
240
|
+
runtimeLeaseId: this.identity.executionLeaseId,
|
|
241
|
+
}, {
|
|
242
|
+
...options,
|
|
243
|
+
...(this.managedCommandTeardownGraceMs === undefined ? {} : { graceMs: this.managedCommandTeardownGraceMs }),
|
|
244
|
+
}));
|
|
245
|
+
this.managedCommandTeardownTail = sweep.catch(() => undefined);
|
|
246
|
+
return await sweep;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* After a provider exits, stops the managed commands its MCP server left behind. It never
|
|
250
|
+
* fails the provider's result: a command it cannot prove stopped is recorded in the ledger
|
|
251
|
+
* and left to attempt teardown, which fails closed.
|
|
252
|
+
*/
|
|
253
|
+
async stopOrphanedManagedCommands() {
|
|
254
|
+
try {
|
|
255
|
+
await this.terminateManagedCommands({ reason: "owner_exited", onlyOrphaned: true });
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
this.deferredManagedCommandTeardownFailure = boundedFailure(error, []);
|
|
259
|
+
await this.persist().catch(() => undefined);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
205
262
|
snapshot() {
|
|
206
263
|
return {
|
|
207
264
|
schemaVersion: 1,
|
|
@@ -210,6 +267,9 @@ export class AttemptProcessSupervisor {
|
|
|
210
267
|
executionLeaseId: this.identity.executionLeaseId,
|
|
211
268
|
status: this.status,
|
|
212
269
|
updatedAt: new Date().toISOString(),
|
|
270
|
+
...(this.deferredManagedCommandTeardownFailure
|
|
271
|
+
? { deferredManagedCommandTeardownFailure: this.deferredManagedCommandTeardownFailure }
|
|
272
|
+
: {}),
|
|
213
273
|
children: this.records.map(record => ({
|
|
214
274
|
name: record.name,
|
|
215
275
|
generation: record.generation,
|
|
@@ -248,8 +308,8 @@ export class AttemptProcessSupervisor {
|
|
|
248
308
|
};
|
|
249
309
|
}
|
|
250
310
|
async observeChildReady(record) {
|
|
251
|
-
const deadline =
|
|
252
|
-
while (
|
|
311
|
+
const deadline = monotonicNowMs() + this.metadataReadyTimeoutMs;
|
|
312
|
+
while (monotonicNowMs() <= deadline) {
|
|
253
313
|
if (await fileExists(record.metadataPath)) {
|
|
254
314
|
if (record.status === "starting") {
|
|
255
315
|
record.status = "running";
|
|
@@ -290,6 +350,20 @@ export class AttemptProcessSupervisor {
|
|
|
290
350
|
this.status = "degraded";
|
|
291
351
|
});
|
|
292
352
|
}
|
|
353
|
+
async replaceLedger(temporary) {
|
|
354
|
+
for (let attempt = 1;; attempt += 1) {
|
|
355
|
+
try {
|
|
356
|
+
await this.replaceLedgerFile(temporary, this.ledgerPath);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
catch (error) {
|
|
360
|
+
const code = error.code;
|
|
361
|
+
if (attempt >= ledgerReplaceAttempts || !code || !transientLedgerReplaceCodes.has(code))
|
|
362
|
+
throw error;
|
|
363
|
+
await wait(this.ledgerReplaceRetryMs);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
293
367
|
assertFence(fencingToken) {
|
|
294
368
|
if (fencingToken !== this.identity.fencingToken) {
|
|
295
369
|
throw new Error("attempt_process_supervisor_fenced: fencing token does not own this attempt.");
|
|
@@ -308,7 +382,7 @@ export class AttemptProcessSupervisor {
|
|
|
308
382
|
finally {
|
|
309
383
|
await handle.close();
|
|
310
384
|
}
|
|
311
|
-
await
|
|
385
|
+
await this.replaceLedger(temporary);
|
|
312
386
|
}
|
|
313
387
|
catch (error) {
|
|
314
388
|
await fs.rm(temporary, { force: true }).catch(() => undefined);
|
|
@@ -6,6 +6,7 @@ import { assertCanonicalDirectory } from "./attempt-private-directory.js";
|
|
|
6
6
|
import { runAttemptArchiveOperation, runAttemptPathOperation } from "./attempt-path-operation.js";
|
|
7
7
|
import { readPinnedSourceDirectory } from "./pinned-source-directory.js";
|
|
8
8
|
import { compactHostAttemptId } from "./host-execution-root.js";
|
|
9
|
+
import { monotonicNowMs } from "../shared/time/monotonic.js";
|
|
9
10
|
const maximumEvidenceBytes = 200 * 1024 * 1024;
|
|
10
11
|
const diagnosticsRetentionDeadlineMs = 30_000;
|
|
11
12
|
const diagnosticsHelperTerminationConfirmationMs = 2_000;
|
|
@@ -21,7 +22,7 @@ export async function retainHostDiagnostics(stateRoot, attemptRoot, attemptId, e
|
|
|
21
22
|
const configuredDeadlineMs = Math.max(1, Math.min(diagnosticsRetentionDeadlineMs, Number.isFinite(requestedDeadlineMs) && (requestedDeadlineMs ?? 0) > 0
|
|
22
23
|
? Math.floor(requestedDeadlineMs)
|
|
23
24
|
: diagnosticsRetentionDeadlineMs));
|
|
24
|
-
const retentionDeadline =
|
|
25
|
+
const retentionDeadline = monotonicNowMs() + configuredDeadlineMs;
|
|
25
26
|
const identity = attemptId.replaceAll("-", "").toLowerCase();
|
|
26
27
|
const compactAttemptRoot = path.resolve(attemptOwnerRoot, "a", compactHostAttemptId(attemptId));
|
|
27
28
|
const legacyAttemptRoot = path.resolve(stateRoot, "host-attempts", identity);
|
|
@@ -155,7 +156,7 @@ function remainingHelperTiming(retentionDeadline, configuredDeadlineMs) {
|
|
|
155
156
|
return { operationTimeoutMs, terminationConfirmationMs };
|
|
156
157
|
}
|
|
157
158
|
function remainingRetentionMs(retentionDeadline, configuredDeadlineMs) {
|
|
158
|
-
const remainingMs = retentionDeadline -
|
|
159
|
+
const remainingMs = retentionDeadline - monotonicNowMs();
|
|
159
160
|
if (remainingMs <= 0)
|
|
160
161
|
throw diagnosticsDeadlineError(configuredDeadlineMs);
|
|
161
162
|
return remainingMs;
|
|
@@ -181,7 +182,7 @@ async function runWithinRetentionHelperDeadline(operation, retentionDeadline, co
|
|
|
181
182
|
return await operation();
|
|
182
183
|
}
|
|
183
184
|
catch (error) {
|
|
184
|
-
if (
|
|
185
|
+
if (monotonicNowMs() >= retentionDeadline)
|
|
185
186
|
throw diagnosticsDeadlineError(configuredDeadlineMs);
|
|
186
187
|
throw error;
|
|
187
188
|
}
|
|
@@ -16,6 +16,7 @@ import { hostPortEnvironment, loadHostRuntimeContract, } from "./host-runtime-co
|
|
|
16
16
|
import { HostPortBroker, } from "./host-port-broker.js";
|
|
17
17
|
import { NodeHostRuntimeAdapter, } from "./node-host-runtime-adapter.js";
|
|
18
18
|
import { AttemptProcessSupervisorRegistry } from "./attempt-process-supervisor-registry.js";
|
|
19
|
+
import { terminateAttemptManagedCommands } from "./managed-command-teardown.js";
|
|
19
20
|
import { attemptDatabaseCapabilityEnvironment, attemptDatabaseCapabilitySecrets, } from "./attempt-database-broker.js";
|
|
20
21
|
import { AttemptNodeRuntimeLauncher } from "./attempt-node-runtime-launcher.js";
|
|
21
22
|
import { HostListenerInspector } from "./host-listener-inspector.js";
|
|
@@ -1532,18 +1533,46 @@ export class HostRuntimeLeaseManager {
|
|
|
1532
1533
|
if (logsEntry.kind !== "directory") {
|
|
1533
1534
|
throw new Error("host_runtime_reaper_invalid: process ownership log path is not an ordinary directory.");
|
|
1534
1535
|
}
|
|
1535
|
-
const entries = await readPinnedSourceDirectory(logsRoot, logsEntry.identity);
|
|
1536
1536
|
const outcomes = [];
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
const
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1537
|
+
const failures = [];
|
|
1538
|
+
try {
|
|
1539
|
+
const entries = await readPinnedSourceDirectory(logsRoot, logsEntry.identity);
|
|
1540
|
+
for (const entry of entries) {
|
|
1541
|
+
if (entry.kind !== "file" || !/^process-supervisor\.[a-z][a-z0-9_-]{0,31}\.\d+\.json$/u.test(entry.name))
|
|
1542
|
+
continue;
|
|
1543
|
+
const metadataPath = path.join(logsRoot, entry.name);
|
|
1544
|
+
const value = JSON.parse((await readCanonicalSourceFile(metadataPath, maximumProcessRecoveryMetadataBytes, entry.identity)).toString("utf8"));
|
|
1545
|
+
if (!validHostProcessSupervisorMetadata(value, lease)) {
|
|
1546
|
+
throw new Error("host_runtime_reaper_invalid: process ownership metadata did not match the runtime lease.");
|
|
1547
|
+
}
|
|
1548
|
+
outcomes.push(await this.processRecovery.recover(value));
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
catch (error) {
|
|
1552
|
+
failures.push(error);
|
|
1553
|
+
}
|
|
1554
|
+
// Managed commands run in their own process groups and record their ownership apart
|
|
1555
|
+
// from the attempt's supervised children. They are recovered even when recovering a
|
|
1556
|
+
// supervised child failed, and both failures are reported together.
|
|
1557
|
+
try {
|
|
1558
|
+
const managed = await terminateAttemptManagedCommands({
|
|
1559
|
+
logDirectory: logsRoot,
|
|
1560
|
+
dispatchAttemptId: lease.dispatchAttemptId,
|
|
1561
|
+
runtimeLeaseId: lease.executionLeaseId,
|
|
1562
|
+
pinnedLogDirectory: logsEntry.identity,
|
|
1563
|
+
}, { reason: "attempt_teardown" });
|
|
1564
|
+
for (const command of managed) {
|
|
1565
|
+
if (command.outcome === "terminated" || command.outcome === "pid_reused")
|
|
1566
|
+
outcomes.push(command.outcome);
|
|
1544
1567
|
}
|
|
1545
|
-
outcomes.push(await this.processRecovery.recover(value));
|
|
1546
1568
|
}
|
|
1569
|
+
catch (error) {
|
|
1570
|
+
failures.push(error);
|
|
1571
|
+
}
|
|
1572
|
+
if (failures.length === 1)
|
|
1573
|
+
throw failures[0];
|
|
1574
|
+
if (failures.length > 1)
|
|
1575
|
+
throw new AggregateError(failures, "host_runtime_reaper_recovery_failed");
|
|
1547
1576
|
return outcomes;
|
|
1548
1577
|
}
|
|
1549
1578
|
async markLedgerDegraded(ledger) {
|
|
@@ -1919,6 +1948,18 @@ class AttemptSupervisedProviderProcessRunner {
|
|
|
1919
1948
|
: new Set();
|
|
1920
1949
|
const child = await this.supervisor.startChildReady("provider", request, handlers, signal, { maxStartAttempts: request.stdin === undefined ? 1 : 3, retryDelayMs: 100 });
|
|
1921
1950
|
this.onProcessTreeStarted();
|
|
1951
|
+
try {
|
|
1952
|
+
return await this.awaitProviderResult(child, baselineListeners, signal);
|
|
1953
|
+
}
|
|
1954
|
+
finally {
|
|
1955
|
+
// The provider tree, and the MCP server inside it, is gone. A managed command that
|
|
1956
|
+
// server started now has no supervisor and no receipt writer, so it must not keep
|
|
1957
|
+
// running into the next provider generation. This never rejects; a command it
|
|
1958
|
+
// cannot prove stopped is left to attempt teardown, which fails closed.
|
|
1959
|
+
await this.supervisor.stopOrphanedManagedCommands();
|
|
1960
|
+
}
|
|
1961
|
+
}
|
|
1962
|
+
async awaitProviderResult(child, baselineListeners, signal) {
|
|
1922
1963
|
if (!this.enforceNoListeners)
|
|
1923
1964
|
return await child.result;
|
|
1924
1965
|
let settled = false;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { PinnedDirectoryIdentity } from "./pinned-directory-process.js";
|
|
2
|
+
/**
|
|
3
|
+
* Why Forge stopped a managed command that had not finished:
|
|
4
|
+
* - `owner_exited`: the provider process whose MCP server started it has exited, so no
|
|
5
|
+
* process is left to supervise it or write its receipt.
|
|
6
|
+
* - `attempt_teardown`: the dispatch attempt is being torn down.
|
|
7
|
+
*/
|
|
8
|
+
export type ManagedCommandTeardownReason = "owner_exited" | "attempt_teardown";
|
|
9
|
+
export interface ManagedCommandTeardownScope {
|
|
10
|
+
/** The attempt's private FORGE_LOG_DIR; managed commands record ownership under it. */
|
|
11
|
+
readonly logDirectory: string;
|
|
12
|
+
readonly dispatchAttemptId: string;
|
|
13
|
+
readonly runtimeLeaseId: string;
|
|
14
|
+
/**
|
|
15
|
+
* Reads the ownership records through pinned directory and file identities instead of
|
|
16
|
+
* pathnames. Restart recovery supplies the verified identity of the attempt's logs.
|
|
17
|
+
*/
|
|
18
|
+
readonly pinnedLogDirectory?: PinnedDirectoryIdentity;
|
|
19
|
+
}
|
|
20
|
+
export interface ManagedCommandTeardownOptions {
|
|
21
|
+
readonly reason: ManagedCommandTeardownReason;
|
|
22
|
+
/**
|
|
23
|
+
* Stop only commands whose owning MCP server process is gone. A command whose owner is
|
|
24
|
+
* alive is still supervised by it and must survive wait slices and MCP call boundaries.
|
|
25
|
+
*/
|
|
26
|
+
readonly onlyOrphaned?: boolean;
|
|
27
|
+
readonly graceMs?: number;
|
|
28
|
+
readonly platform?: NodeJS.Platform;
|
|
29
|
+
}
|
|
30
|
+
export type ManagedCommandTeardownOutcomeKind =
|
|
31
|
+
/** The recorded process tree had already exited. */
|
|
32
|
+
"absent"
|
|
33
|
+
/** Forge stopped the recorded process tree and proved it gone. */
|
|
34
|
+
| "terminated"
|
|
35
|
+
/** The recorded root PID now names another process, so the recorded tree is gone. */
|
|
36
|
+
| "pid_reused"
|
|
37
|
+
/** The command's owning MCP server is alive and still supervises it. */
|
|
38
|
+
| "owner_alive"
|
|
39
|
+
/** The command finished on its own; nothing was signalled or rewritten. */
|
|
40
|
+
| "already_terminal"
|
|
41
|
+
/** The command never recorded a process: it was queued, or its launch was interrupted. */
|
|
42
|
+
| "no_process_record";
|
|
43
|
+
export interface ManagedCommandTeardownOutcome {
|
|
44
|
+
readonly commandId: string;
|
|
45
|
+
readonly outcome: ManagedCommandTeardownOutcomeKind;
|
|
46
|
+
readonly receiptFinalized: boolean;
|
|
47
|
+
}
|
|
48
|
+
/** Test support: points the Linux process inventory at another directory (null restores /proc). */
|
|
49
|
+
export declare function setManagedCommandProcRootForTesting(root: string | null): void;
|
|
50
|
+
/**
|
|
51
|
+
* Stops managed commands that an attempt's MCP servers started and proves their process
|
|
52
|
+
* trees are gone. Managed commands run in their own process group (Unix) or Job Object
|
|
53
|
+
* (Windows), outside the provider tree that the attempt supervisor stops, so stopping the
|
|
54
|
+
* provider alone leaves them running. Each command's durable receipt is given a terminal
|
|
55
|
+
* status when its owner can no longer write one.
|
|
56
|
+
*
|
|
57
|
+
* Every signal is fenced: the recorded root must still carry the recorded birth identity
|
|
58
|
+
* and, on Linux, this command's supervisor and attempt identity in its environment. A
|
|
59
|
+
* tree that cannot be proven to be the command's own fails the teardown instead of being
|
|
60
|
+
* reported stopped.
|
|
61
|
+
*/
|
|
62
|
+
export declare function terminateAttemptManagedCommands(scope: ManagedCommandTeardownScope, options: ManagedCommandTeardownOptions): Promise<readonly ManagedCommandTeardownOutcome[]>;
|