@loucompanion/forge-bridge 0.1.4-dev.b1c678efffd2 → 0.1.4-dev.bb5355d0d83f
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 +1 -1
- package/dist/bridge/bridge-chat-provider-command.js +16 -4
- package/dist/bridge/bridge-chat.types.d.ts +10 -0
- package/dist/bridge/bridge-chat.types.js +16 -1
- package/dist/bridge/runner-host-capabilities.d.ts +3 -0
- package/dist/bridge/runner-host-capabilities.js +13 -0
- package/dist/runtime/attempt-database-broker.d.ts +1 -1
- package/dist/runtime/docker-engine-probe.d.ts +5 -0
- package/dist/runtime/docker-engine-probe.js +7 -0
- package/dist/runtime/ephemeral-test-postgres-grant.d.ts +37 -0
- package/dist/runtime/ephemeral-test-postgres-grant.js +85 -0
- package/dist/runtime/ephemeral-test-postgres.d.ts +57 -0
- package/dist/runtime/ephemeral-test-postgres.js +303 -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 +34 -4
- package/dist/runtime/host-load-sampler.js +73 -7
- 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/runtime/native-test-postgres-fallback.d.ts +51 -7
- package/dist/runtime/native-test-postgres-fallback.js +60 -8
- 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.d.ts +0 -7
- package/dist/session/managed-attempt-tools.js +51 -62
- 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.runtime.js +1 -1
- package/dist/session/phase-completion.schemas.js +3 -11
- package/dist/session/phase-completion.validation.js +3 -1
- package/dist/session/pre-publication-native-postgres.d.ts +30 -0
- package/dist/session/pre-publication-native-postgres.js +111 -0
- package/dist/session/pre-publication-pass.d.ts +2 -1
- package/dist/session/pre-publication-pass.js +8 -5
- package/dist/session/pre-publication-step-execution.d.ts +2 -1
- package/dist/session/pre-publication-step-execution.js +6 -5
- package/dist/session/pre-publication-validation.d.ts +9 -3
- package/dist/session/pre-publication-validation.js +62 -34
- 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 +6 -16
- package/dist/session/provider-cli/provider-cli.process.js +22 -283
- 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-output-pipes.d.ts +30 -0
- package/dist/session/provider-cli/provider-output-pipes.js +67 -0
- package/dist/session/sandbox-host-capacity-gate.js +12 -3
- 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
package/README.md
CHANGED
|
@@ -149,6 +149,19 @@ capacity (`FORGE_HOST_MAX_CONCURRENT_ATTEMPTS`, `FORGE_HOST_CPU_BUDGET_PERCENT`,
|
|
|
149
149
|
`FORGE_HOST_LOAD_CPU_BUDGET_FRACTION`, `FORGE_HOST_MAX_HEAVY_STEPS`) and a floor on the reserve
|
|
150
150
|
(`FORGE_HOST_MEMORY_RESERVE_BYTES`). Without a server value, the env var or the bridge default applies.
|
|
151
151
|
The CPU budget, yield budget and heavy-step limit take effect only with `FORGE_HOST_LOAD_GOVERNOR=on`.
|
|
152
|
+
They gate on Linux PSI. Windows has no PSI, so a Windows bridge estimates CPU pressure from whole-host
|
|
153
|
+
CPU use and reports it with `pressureSource: "windows_estimate"` (decision 0021). Forge's Load column
|
|
154
|
+
and the budgets then work there too. Without either signal, the governor falls back to one heavy step at a time.
|
|
155
|
+
|
|
156
|
+
Two or more attempts on one host take turns at the host test slot (decision 0018): builds, test runs,
|
|
157
|
+
Playwright, pre-publication checks and the ASP.NET runtime build each wait for it in one host-wide queue,
|
|
158
|
+
foreground first, then first come, first served, across the bridge and every completion MCP server. Plain
|
|
159
|
+
managed commands and dependency installs never wait for it. A waiting step reports who holds the slot
|
|
160
|
+
(`prepublication_host_capacity_waiting`, `host_runtime_build_waiting_for_host_capacity`, or
|
|
161
|
+
`host_load_wait_detail` on a managed command's receipt), and its own timeout starts only once it runs.
|
|
162
|
+
A holder that crashes or stalls keeps the slot only until the host load ledger takes its token back (its
|
|
163
|
+
process tree stopped, its lease fenced, or attempt teardown). `FORGE_HOST_LOAD_TEST_SLOTS` sets how many
|
|
164
|
+
run at once (default 1; `0` turns the slot off).
|
|
152
165
|
|
|
153
166
|
Yield this machine to foreground work (for example while gaming) from the host itself:
|
|
154
167
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ProviderCliCommand, ProviderProcessRunResult } from "../session/provider-cli/provider-cli.process.js";
|
|
2
|
-
import type
|
|
2
|
+
import { type ChatTurnDispatchPacket } from "./bridge-chat.types.js";
|
|
3
3
|
/** The turn-scoped handoff metadata file the bridge writes into the chat workspace. */
|
|
4
4
|
export declare const chatTurnMetadataFileName = "forge-chat-metadata.json";
|
|
5
5
|
/**
|
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* metadata.
|
|
5
5
|
*/
|
|
6
6
|
import { claudeHeadlessEnvironment, claudeHeadlessToolArgs, providerTurnEndedWithBackgroundWorkFailure } from "../session/provider-cli/claude-background-work.js";
|
|
7
|
-
import { codexSandboxModeForPlatform } from "../session/provider-cli/provider-cli.prompts.js";
|
|
7
|
+
import { codexSandboxModeForPlatform, providerEffort } from "../session/provider-cli/provider-cli.prompts.js";
|
|
8
|
+
import { chatProviderKind } from "./bridge-chat.types.js";
|
|
8
9
|
/** The turn-scoped handoff metadata file the bridge writes into the chat workspace. */
|
|
9
10
|
export const chatTurnMetadataFileName = "forge-chat-metadata.json";
|
|
10
11
|
/**
|
|
@@ -17,16 +18,23 @@ export function assertChatTurnFinished(result) {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
export function buildChatProviderCommand(packet, cwd) {
|
|
20
|
-
|
|
21
|
+
// The provider kind picks the CLI and its effort levels; an auth-path suffix such as `:bridge` does not.
|
|
22
|
+
const provider = chatProviderKind(packet.providerKind);
|
|
21
23
|
const model = packet.model.trim();
|
|
22
24
|
const prompt = buildChatPrompt(packet);
|
|
23
25
|
const resumeThreadId = packet.providerThreadId?.trim() || null;
|
|
26
|
+
// Levels the provider does not accept are dropped, so the CLI keeps its own default.
|
|
27
|
+
const effort = providerEffort(provider, packet.effort);
|
|
24
28
|
if (provider === "codex") {
|
|
25
29
|
const args = ["-s", codexSandboxModeForPlatform(process.platform, "host"), "-a", "never", "exec"];
|
|
26
30
|
if (resumeThreadId) {
|
|
27
31
|
args.push("--cd", cwd, "resume");
|
|
28
32
|
}
|
|
29
|
-
args.push("--model", model
|
|
33
|
+
args.push("--model", model);
|
|
34
|
+
if (effort) {
|
|
35
|
+
args.push("-c", `model_reasoning_effort=${JSON.stringify(effort)}`);
|
|
36
|
+
}
|
|
37
|
+
args.push("--json");
|
|
30
38
|
if (!resumeThreadId) {
|
|
31
39
|
args.push("--cd", cwd);
|
|
32
40
|
}
|
|
@@ -46,7 +54,11 @@ export function buildChatProviderCommand(packet, cwd) {
|
|
|
46
54
|
return { command: { command: "cursor-agent", args } };
|
|
47
55
|
}
|
|
48
56
|
if (provider === "claude") {
|
|
49
|
-
const args = ["-p", "--output-format", "stream-json", "--verbose", "--model", model
|
|
57
|
+
const args = ["-p", "--output-format", "stream-json", "--verbose", "--model", model];
|
|
58
|
+
if (effort) {
|
|
59
|
+
args.push("--effort", effort);
|
|
60
|
+
}
|
|
61
|
+
args.push("--permission-mode", "bypassPermissions", ...claudeHeadlessToolArgs());
|
|
50
62
|
if (resumeThreadId) {
|
|
51
63
|
args.push("--resume", resumeThreadId);
|
|
52
64
|
}
|
|
@@ -35,6 +35,11 @@ export interface ChatTurnDispatchPacket {
|
|
|
35
35
|
readonly contextRefs: readonly ChatTurnContextRef[];
|
|
36
36
|
readonly idempotencyKey: string;
|
|
37
37
|
readonly providerThreadId?: string | null;
|
|
38
|
+
/**
|
|
39
|
+
* The Chat role's reasoning effort for this provider and model, or null for the provider default.
|
|
40
|
+
* Older servers omit it. The command builder drops levels the provider does not accept.
|
|
41
|
+
*/
|
|
42
|
+
readonly effort?: string | null;
|
|
38
43
|
readonly protocolVersion: typeof bridgeChatProtocolVersion;
|
|
39
44
|
readonly requestedCapabilities: readonly BridgeChatRequestedCapability[];
|
|
40
45
|
readonly attachmentHandoffs: readonly ChatAttachmentHandoffHandle[];
|
|
@@ -107,5 +112,10 @@ export interface ChatTurnCancelPacket {
|
|
|
107
112
|
export declare function unavailableBridgeChatCapability(protocolVersion?: string, unavailableCode?: string | null, unavailableReason?: string | null): BridgeChatCapability;
|
|
108
113
|
export declare function normalizeBridgeChatCapability(value: BridgeChatCapability | null | undefined): BridgeChatCapability;
|
|
109
114
|
export declare function parseChatTurnDispatchPacket(value: unknown, capability?: BridgeChatCapability): ChatTurnDispatchPacket;
|
|
115
|
+
/**
|
|
116
|
+
* The provider kind of a saved provider selection: `codex:bridge` and `codex` are both `codex`.
|
|
117
|
+
* Forge stores the Chat role's provider with its auth-path suffix and may send it as is.
|
|
118
|
+
*/
|
|
119
|
+
export declare function chatProviderKind(provider: string): string;
|
|
110
120
|
export declare function isTerminalChatTurnCallbackKind(kind: ChatTurnCallbackKind): boolean;
|
|
111
121
|
export declare function assertBridgeChatCallbackPayload(value: unknown, label?: string): void;
|
|
@@ -87,18 +87,26 @@ export function parseChatTurnDispatchPacket(value, capability = unavailableBridg
|
|
|
87
87
|
leaseId,
|
|
88
88
|
fencingToken,
|
|
89
89
|
runnerInstanceId: requiredUuid(packet, "runnerInstanceId"),
|
|
90
|
-
providerKind:
|
|
90
|
+
providerKind: requiredProviderKind(packet),
|
|
91
91
|
model: requiredString(packet, "model").trim(),
|
|
92
92
|
messages,
|
|
93
93
|
contextRefs,
|
|
94
94
|
idempotencyKey: requiredString(packet, "idempotencyKey").trim(),
|
|
95
95
|
providerThreadId: optionalString(packet, "providerThreadId", bridgeChatMaxProviderThreadIdChars),
|
|
96
|
+
effort: optionalString(packet, "effort")?.toLowerCase() ?? null,
|
|
96
97
|
protocolVersion: bridgeChatProtocolVersion,
|
|
97
98
|
requestedCapabilities,
|
|
98
99
|
attachmentHandoffs,
|
|
99
100
|
actionBrokerGrants,
|
|
100
101
|
};
|
|
101
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* The provider kind of a saved provider selection: `codex:bridge` and `codex` are both `codex`.
|
|
105
|
+
* Forge stores the Chat role's provider with its auth-path suffix and may send it as is.
|
|
106
|
+
*/
|
|
107
|
+
export function chatProviderKind(provider) {
|
|
108
|
+
return (provider.split(":", 1)[0] ?? "").trim().toLowerCase();
|
|
109
|
+
}
|
|
102
110
|
export function isTerminalChatTurnCallbackKind(kind) {
|
|
103
111
|
return kind === "output_completed" || kind === "failed" || kind === "cancelled" || kind === "expired";
|
|
104
112
|
}
|
|
@@ -304,6 +312,13 @@ function requiredUuid(source, key) {
|
|
|
304
312
|
}
|
|
305
313
|
return value;
|
|
306
314
|
}
|
|
315
|
+
function requiredProviderKind(source) {
|
|
316
|
+
const kind = chatProviderKind(requiredString(source, "providerKind"));
|
|
317
|
+
if (kind.length === 0) {
|
|
318
|
+
throw new Error("providerKind must be a non-empty string.");
|
|
319
|
+
}
|
|
320
|
+
return kind;
|
|
321
|
+
}
|
|
307
322
|
function requiredString(source, key) {
|
|
308
323
|
const value = source[key];
|
|
309
324
|
if (typeof value !== "string" || value.trim().length === 0) {
|
|
@@ -11,6 +11,9 @@ export interface RunnerHostCapabilityProbe {
|
|
|
11
11
|
detect(config: BridgeServiceConfig): Promise<RunnerHostCapabilitySnapshot>;
|
|
12
12
|
}
|
|
13
13
|
export declare class ProcessRunnerHostCapabilityProbe implements RunnerHostCapabilityProbe {
|
|
14
|
+
private readonly warn;
|
|
15
|
+
private readonly providerEnvironment;
|
|
16
|
+
constructor(warn?: (message: string) => void, providerEnvironment?: () => NodeJS.ProcessEnv);
|
|
14
17
|
detect(config: BridgeServiceConfig): Promise<RunnerHostCapabilitySnapshot>;
|
|
15
18
|
}
|
|
16
19
|
export declare function isUnavailablePlaywrightConfigError(error: unknown): boolean;
|
|
@@ -3,9 +3,22 @@ import fs from "node:fs/promises";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { promisify } from "node:util";
|
|
5
5
|
import { probeTestPostgresCapability } from "./test-postgres-capability.js";
|
|
6
|
+
import { appExecutionAliasWarning } from "../shared/process/windows-app-execution-alias.js";
|
|
7
|
+
import { buildProviderChildEnvironment } from "../session/provider-cli/provider-cli.process.js";
|
|
6
8
|
const execFileAsync = promisify(execFile);
|
|
7
9
|
export class ProcessRunnerHostCapabilityProbe {
|
|
10
|
+
warn;
|
|
11
|
+
providerEnvironment;
|
|
12
|
+
constructor(warn = message => process.stderr.write(`[${new Date().toISOString()}] ${message}\n`), providerEnvironment = () => buildProviderChildEnvironment()) {
|
|
13
|
+
this.warn = warn;
|
|
14
|
+
this.providerEnvironment = providerEnvironment;
|
|
15
|
+
}
|
|
8
16
|
async detect(config) {
|
|
17
|
+
// Readiness, not a capability: the bridge still settles an attempt whose descendant escaped
|
|
18
|
+
// the Job Object (see OutputPipeRelease), but the host should not hand out aliases.
|
|
19
|
+
const aliasWarning = appExecutionAliasWarning(this.providerEnvironment());
|
|
20
|
+
if (aliasWarning)
|
|
21
|
+
this.warn(aliasWarning);
|
|
9
22
|
const [pnpm, dotnet, git, bash, pwsh, windowsPowerShell] = await Promise.all([
|
|
10
23
|
probe("pnpm", ["--version"]),
|
|
11
24
|
probe("dotnet", ["--version"]),
|
|
@@ -75,7 +75,7 @@ export interface NativeTestClusterAdmin {
|
|
|
75
75
|
readonly username: string;
|
|
76
76
|
readonly password: string;
|
|
77
77
|
}
|
|
78
|
-
export declare function nativeTestClusterAdmin(fallback: NativeTestPostgresFallback): NativeTestClusterAdmin;
|
|
78
|
+
export declare function nativeTestClusterAdmin(fallback: Pick<NativeTestPostgresFallback, "connectionString" | "endpoint">): NativeTestClusterAdmin;
|
|
79
79
|
/** Runs SQL through the psql that ships with the PostgreSQL install hosting the native test cluster. */
|
|
80
80
|
export declare function psqlSqlRunner(admin: NativeTestClusterAdmin, psqlPath: string, timeoutMs?: number): AttemptDatabaseSqlRunner;
|
|
81
81
|
/** psql beside the cluster's pg_ctl: FORGE_NATIVE_TEST_POSTGRES_PSQL, a Windows install, or PATH. */
|
|
@@ -62,6 +62,11 @@ export declare class DockerEngineProbe {
|
|
|
62
62
|
constructor(options?: DockerEngineProbeOptions, commands?: CommandRunner, cacheMs?: number);
|
|
63
63
|
current(): DockerEngineCapabilitySnapshot | null;
|
|
64
64
|
refresh(force?: boolean): Promise<DockerEngineCapabilitySnapshot>;
|
|
65
|
+
/**
|
|
66
|
+
* One fresh daemon reading that never starts recovery and leaves the cache and the
|
|
67
|
+
* failure streak alone, for callers that only need to know whether Docker is up.
|
|
68
|
+
*/
|
|
69
|
+
probeWithoutRecovery(): Promise<DockerEngineCapabilitySnapshot>;
|
|
65
70
|
/**
|
|
66
71
|
* Establishes a fresh daemon fence for work that declares Docker as a hard
|
|
67
72
|
* prerequisite. This remains bounded, cooldown protected and single-flight;
|
|
@@ -69,6 +69,13 @@ export class DockerEngineProbe {
|
|
|
69
69
|
this.inFlight = operation;
|
|
70
70
|
return await operation;
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* One fresh daemon reading that never starts recovery and leaves the cache and the
|
|
74
|
+
* failure streak alone, for callers that only need to know whether Docker is up.
|
|
75
|
+
*/
|
|
76
|
+
async probeWithoutRecovery() {
|
|
77
|
+
return await this.inspectSafely();
|
|
78
|
+
}
|
|
72
79
|
/**
|
|
73
80
|
* Establishes a fresh daemon fence for work that declares Docker as a hard
|
|
74
81
|
* prerequisite. This remains bounded, cooldown protected and single-flight;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type EphemeralTestPostgresCluster, type EphemeralTestPostgresOptions } from "./ephemeral-test-postgres.js";
|
|
2
|
+
import { type NativeTestPostgresFallback, type NativeTestPostgresGrant } from "./native-test-postgres-fallback.js";
|
|
3
|
+
export type StartEphemeralTestPostgres = (options: EphemeralTestPostgresOptions) => Promise<EphemeralTestPostgresCluster>;
|
|
4
|
+
/** The bin directory an ephemeral cluster runs from: the file's `postgresBin`, else the one holding psql. */
|
|
5
|
+
export declare function ephemeralTestPostgresBinDirectory(fallback: Pick<NativeTestPostgresFallback, "postgresBin">, psqlPath?: () => string | null): string;
|
|
6
|
+
/**
|
|
7
|
+
* Starts a throwaway cluster for one check and returns its grant. The grant's mode keeps the
|
|
8
|
+
* Docker semantics of the shared cluster: with Docker up, pinned-engine tests still start their
|
|
9
|
+
* own containers; with Docker down, they are skipped. The caller must call `release`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function startEphemeralTestPostgresGrant(fallback: Pick<NativeTestPostgresFallback, "postgresBin">, dockerAvailable: boolean, start?: StartEphemeralTestPostgres): Promise<NativeTestPostgresGrant>;
|
|
12
|
+
/** A managed verification command's database environment, and what to release when the command ends. */
|
|
13
|
+
export interface ManagedVerificationDatabase {
|
|
14
|
+
readonly environment: Readonly<Record<string, string>>;
|
|
15
|
+
/** The connection strings and their passwords, for redaction; never the mode or the cluster id. */
|
|
16
|
+
readonly secrets: readonly string[];
|
|
17
|
+
readonly release: () => Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/** A command that runs no verification profile gets no database. */
|
|
20
|
+
export declare const noManagedVerificationDatabase: ManagedVerificationDatabase;
|
|
21
|
+
/**
|
|
22
|
+
* Builds a command's setup from its database. If the setup throws, the command never starts and
|
|
23
|
+
* no caller holds the database, so it is released here before the error is rethrown.
|
|
24
|
+
*/
|
|
25
|
+
export declare function setUpWithManagedVerificationDatabase<T>(database: Pick<ManagedVerificationDatabase, "release">, setUp: () => T): Promise<T>;
|
|
26
|
+
/**
|
|
27
|
+
* Releases a command's database once the command has ended. A cluster that would not stop is left
|
|
28
|
+
* disowned for the next start's reap; the failure goes to `report` instead of being dropped.
|
|
29
|
+
*/
|
|
30
|
+
export declare function releaseManagedVerificationDatabase(database: Pick<ManagedVerificationDatabase, "release">, report: (message: string) => Promise<void>): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Managed verification on an `ephemeral` host gets a throwaway cluster for the command's
|
|
33
|
+
* lifetime; every other host keeps managedVerificationDatabaseEnvironment's rules. A cluster
|
|
34
|
+
* that cannot start fails the command rather than silently handing it Docker or the shared
|
|
35
|
+
* cluster's narrower login.
|
|
36
|
+
*/
|
|
37
|
+
export declare function managedVerificationDatabase(dockerAvailable?: () => Promise<boolean>, readFallback?: () => NativeTestPostgresFallback | null, start?: StartEphemeralTestPostgres): Promise<ManagedVerificationDatabase>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { attemptDatabaseCapabilitySecrets, resolvePsqlPath } from "./attempt-database-broker.js";
|
|
3
|
+
import { EphemeralTestPostgresError, ephemeralTestPostgresClusterEnvironment, startEphemeralTestPostgres, } from "./ephemeral-test-postgres.js";
|
|
4
|
+
import { DockerEngineProbe } from "./docker-engine-probe.js";
|
|
5
|
+
import { managedVerificationDatabaseEnvironment, nativeTestPostgresFallbackConfigEnvironment, nativeTestPostgresFallbackDatabaseMode, nativeTestPostgresFallbackEnvironment, nativeTestPostgresPreferredDatabaseMode, readNativeTestPostgresFallback, } from "./native-test-postgres-fallback.js";
|
|
6
|
+
/** The bin directory an ephemeral cluster runs from: the file's `postgresBin`, else the one holding psql. */
|
|
7
|
+
export function ephemeralTestPostgresBinDirectory(fallback, psqlPath = resolvePsqlPath) {
|
|
8
|
+
if (fallback.postgresBin)
|
|
9
|
+
return fallback.postgresBin;
|
|
10
|
+
const psql = psqlPath();
|
|
11
|
+
if (!psql) {
|
|
12
|
+
throw new EphemeralTestPostgresError("no PostgreSQL install was found; set 'postgresBin' in native-test-postgres.json or FORGE_NATIVE_TEST_POSTGRES_PSQL.");
|
|
13
|
+
}
|
|
14
|
+
return path.dirname(psql);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Starts a throwaway cluster for one check and returns its grant. The grant's mode keeps the
|
|
18
|
+
* Docker semantics of the shared cluster: with Docker up, pinned-engine tests still start their
|
|
19
|
+
* own containers; with Docker down, they are skipped. The caller must call `release`.
|
|
20
|
+
*/
|
|
21
|
+
export async function startEphemeralTestPostgresGrant(fallback, dockerAvailable, start = startEphemeralTestPostgres) {
|
|
22
|
+
const cluster = await start({ binDirectory: ephemeralTestPostgresBinDirectory(fallback) });
|
|
23
|
+
return {
|
|
24
|
+
connectionString: cluster.connectionString,
|
|
25
|
+
endpoint: cluster.endpoint,
|
|
26
|
+
mode: dockerAvailable ? nativeTestPostgresPreferredDatabaseMode : nativeTestPostgresFallbackDatabaseMode,
|
|
27
|
+
ephemeralClusterId: cluster.id,
|
|
28
|
+
release: cluster.release,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** A command that runs no verification profile gets no database. */
|
|
32
|
+
export const noManagedVerificationDatabase = {
|
|
33
|
+
environment: {},
|
|
34
|
+
secrets: [],
|
|
35
|
+
release: async () => undefined,
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Builds a command's setup from its database. If the setup throws, the command never starts and
|
|
39
|
+
* no caller holds the database, so it is released here before the error is rethrown.
|
|
40
|
+
*/
|
|
41
|
+
export async function setUpWithManagedVerificationDatabase(database, setUp) {
|
|
42
|
+
try {
|
|
43
|
+
return setUp();
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
await database.release().catch(() => undefined);
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Releases a command's database once the command has ended. A cluster that would not stop is left
|
|
52
|
+
* disowned for the next start's reap; the failure goes to `report` instead of being dropped.
|
|
53
|
+
*/
|
|
54
|
+
export async function releaseManagedVerificationDatabase(database, report) {
|
|
55
|
+
try {
|
|
56
|
+
await database.release();
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
await report(error instanceof Error ? error.message : String(error)).catch(() => undefined);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function databaseSecrets(environment) {
|
|
63
|
+
return Object.entries(environment)
|
|
64
|
+
.filter(([name]) => name !== "FORGE_MANAGED_VERIFICATION_DATABASE_MODE" && name !== ephemeralTestPostgresClusterEnvironment)
|
|
65
|
+
.flatMap(([, value]) => attemptDatabaseCapabilitySecrets(value));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Managed verification on an `ephemeral` host gets a throwaway cluster for the command's
|
|
69
|
+
* lifetime; every other host keeps managedVerificationDatabaseEnvironment's rules. A cluster
|
|
70
|
+
* that cannot start fails the command rather than silently handing it Docker or the shared
|
|
71
|
+
* cluster's narrower login.
|
|
72
|
+
*/
|
|
73
|
+
export async function managedVerificationDatabase(dockerAvailable = async () => (await new DockerEngineProbe().probeWithoutRecovery()).available, readFallback = () => {
|
|
74
|
+
const configPath = process.env[nativeTestPostgresFallbackConfigEnvironment]?.trim();
|
|
75
|
+
return configPath ? readNativeTestPostgresFallback(configPath) : null;
|
|
76
|
+
}, start = startEphemeralTestPostgres) {
|
|
77
|
+
const fallback = readFallback();
|
|
78
|
+
if (fallback?.use !== "ephemeral") {
|
|
79
|
+
const environment = await managedVerificationDatabaseEnvironment(dockerAvailable, () => fallback);
|
|
80
|
+
return { environment, secrets: databaseSecrets(environment), release: async () => undefined };
|
|
81
|
+
}
|
|
82
|
+
const grant = await startEphemeralTestPostgresGrant(fallback, await dockerAvailable(), start);
|
|
83
|
+
const environment = nativeTestPostgresFallbackEnvironment(grant);
|
|
84
|
+
return { environment, secrets: databaseSecrets(environment), release: grant.release ?? (async () => undefined) };
|
|
85
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** Set for a test run on an ephemeral cluster: its id, which the server reports as `forge.ephemeral_cluster`. */
|
|
2
|
+
export declare const ephemeralTestPostgresClusterEnvironment = "FORGE_TEST_POSTGRES_EPHEMERAL_CLUSTER";
|
|
3
|
+
export declare const ephemeralTestPostgresMarkerSetting = "forge.ephemeral_cluster";
|
|
4
|
+
export declare class EphemeralTestPostgresError extends Error {
|
|
5
|
+
readonly code = "ephemeral_test_postgres_unavailable";
|
|
6
|
+
constructor(detail: string);
|
|
7
|
+
}
|
|
8
|
+
export interface EphemeralTestPostgresCluster {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly connectionString: string;
|
|
11
|
+
readonly endpoint: string;
|
|
12
|
+
readonly password: string;
|
|
13
|
+
/**
|
|
14
|
+
* Stops the server and deletes its directory. Safe to call more than once. Rejects, keeping the
|
|
15
|
+
* directory, when the server is still running after the stop.
|
|
16
|
+
*/
|
|
17
|
+
release(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
export interface ToolResult {
|
|
20
|
+
readonly code: number | null;
|
|
21
|
+
readonly output: string;
|
|
22
|
+
}
|
|
23
|
+
/** Runs one PostgreSQL program. `detached` programs (pg_ctl start) leave a server running, so their output goes to its log. */
|
|
24
|
+
export type EphemeralToolRunner = (file: string, args: readonly string[], options: {
|
|
25
|
+
readonly env: Readonly<Record<string, string>>;
|
|
26
|
+
readonly timeoutMs: number;
|
|
27
|
+
readonly detached?: boolean;
|
|
28
|
+
}) => Promise<ToolResult>;
|
|
29
|
+
export interface EphemeralTestPostgresOptions {
|
|
30
|
+
/** The directory holding initdb, pg_ctl and psql of the PostgreSQL install to run. */
|
|
31
|
+
readonly binDirectory: string;
|
|
32
|
+
readonly root?: string;
|
|
33
|
+
readonly ownerPid?: number;
|
|
34
|
+
readonly runTool?: EphemeralToolRunner;
|
|
35
|
+
readonly isProcessAlive?: (pid: number) => boolean;
|
|
36
|
+
readonly freePort?: () => Promise<number>;
|
|
37
|
+
readonly now?: () => number;
|
|
38
|
+
}
|
|
39
|
+
export declare function defaultEphemeralTestPostgresRoot(): string;
|
|
40
|
+
/**
|
|
41
|
+
* A superuser of a cluster run by the `postgres` OS user can read every cluster that user runs,
|
|
42
|
+
* including live ones (Debian and Ubuntu packages run them all as `postgres`). An ephemeral
|
|
43
|
+
* cluster therefore runs only as an ordinary user, the same one that already runs the attempt.
|
|
44
|
+
*/
|
|
45
|
+
export declare function assertEphemeralClusterOsUser(userName?: string, uid?: number | undefined): void;
|
|
46
|
+
/** Creates, starts and prepares one cluster. On any failure nothing of it is left behind. */
|
|
47
|
+
export declare function startEphemeralTestPostgres(options: EphemeralTestPostgresOptions): Promise<EphemeralTestPostgresCluster>;
|
|
48
|
+
/**
|
|
49
|
+
* Removes clusters whose owning Bridge process is gone. Only directories named like a cluster
|
|
50
|
+
* id inside the root are considered, and a live owner's clusters are never touched.
|
|
51
|
+
*/
|
|
52
|
+
export declare function reapEphemeralTestPostgres(options: EphemeralTestPostgresOptions): Promise<readonly string[]>;
|
|
53
|
+
/**
|
|
54
|
+
* On Windows a started postgres inherits pg_ctl's standard handles, so a pipe would stay open for
|
|
55
|
+
* the server's lifetime; a detached program gets no pipes and settles when it exits.
|
|
56
|
+
*/
|
|
57
|
+
export declare const runPostgresTool: EphemeralToolRunner;
|