@loucompanion/forge-bridge 0.1.3-dev.fbe15023cdfe → 0.1.4-dev.028808a841c8

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.
Files changed (185) hide show
  1. package/README.md +89 -3
  2. package/dist/bridge/bridge-chat.types.d.ts +111 -0
  3. package/dist/bridge/bridge-chat.types.js +401 -0
  4. package/dist/bridge/bridge.base.d.ts +4 -1
  5. package/dist/bridge/bridge.service.d.ts +83 -3
  6. package/dist/bridge/bridge.service.js +1609 -152
  7. package/dist/bridge/bridge.types.d.ts +52 -1
  8. package/dist/bridge/dispatch-result-outbox.d.ts +35 -0
  9. package/dist/bridge/dispatch-result-outbox.js +87 -0
  10. package/dist/bridge/dispatch-result-worker.d.ts +2 -0
  11. package/dist/bridge/dispatch-result-worker.js +122 -0
  12. package/dist/bridge/internals.d.ts +2 -0
  13. package/dist/bridge/internals.js +1 -0
  14. package/dist/bridge/readiness-result-outbox.d.ts +21 -0
  15. package/dist/bridge/readiness-result-outbox.js +54 -0
  16. package/dist/bridge/readiness-result-worker.d.ts +2 -0
  17. package/dist/bridge/readiness-result-worker.js +121 -0
  18. package/dist/bridge/runner-host-capabilities.d.ts +15 -0
  19. package/dist/bridge/runner-host-capabilities.js +93 -0
  20. package/dist/cleanup/cleanup.service.d.ts +1 -0
  21. package/dist/cleanup/cleanup.service.js +70 -9
  22. package/dist/cli/cli.help.js +4 -4
  23. package/dist/cli/cli.service.d.ts +14 -6
  24. package/dist/cli/cli.service.js +75 -16
  25. package/dist/cli/local-config.d.ts +1 -0
  26. package/dist/cli/local-config.js +4 -0
  27. package/dist/runtime/aspnet-runtime-prebuild.d.ts +36 -0
  28. package/dist/runtime/aspnet-runtime-prebuild.js +341 -0
  29. package/dist/runtime/attempt-database-broker.d.ts +89 -0
  30. package/dist/runtime/attempt-database-broker.js +390 -0
  31. package/dist/runtime/attempt-node-runtime-launcher.d.ts +2 -1
  32. package/dist/runtime/attempt-node-runtime-launcher.js +57 -18
  33. package/dist/runtime/attempt-path-operation.d.ts +21 -0
  34. package/dist/runtime/attempt-path-operation.js +94 -0
  35. package/dist/runtime/attempt-private-directory.d.ts +6 -0
  36. package/dist/runtime/attempt-private-directory.js +54 -0
  37. package/dist/runtime/attempt-process-supervisor.d.ts +35 -0
  38. package/dist/runtime/attempt-process-supervisor.js +144 -23
  39. package/dist/runtime/canonical-source-file.d.ts +3 -0
  40. package/dist/runtime/canonical-source-file.js +54 -0
  41. package/dist/runtime/docker-engine-probe.d.ts +90 -0
  42. package/dist/runtime/docker-engine-probe.js +333 -0
  43. package/dist/runtime/generated-output-reset.d.ts +10 -0
  44. package/dist/runtime/generated-output-reset.js +62 -8
  45. package/dist/runtime/host-database-capability-boundary.d.ts +16 -0
  46. package/dist/runtime/host-database-capability-boundary.js +63 -0
  47. package/dist/runtime/host-diagnostics.d.ts +11 -0
  48. package/dist/runtime/host-diagnostics.js +192 -0
  49. package/dist/runtime/host-execution-root.d.ts +4 -0
  50. package/dist/runtime/host-execution-root.js +39 -0
  51. package/dist/runtime/host-facts.d.ts +35 -0
  52. package/dist/runtime/host-facts.js +95 -0
  53. package/dist/runtime/host-listener-inspector.d.ts +8 -1
  54. package/dist/runtime/host-listener-inspector.js +121 -60
  55. package/dist/runtime/host-load-sampler.d.ts +83 -0
  56. package/dist/runtime/host-load-sampler.js +164 -0
  57. package/dist/runtime/host-pressure-stall.d.ts +41 -0
  58. package/dist/runtime/host-pressure-stall.js +121 -0
  59. package/dist/runtime/host-runtime-contract.d.ts +13 -1
  60. package/dist/runtime/host-runtime-contract.js +170 -55
  61. package/dist/runtime/host-runtime-lease.d.ts +92 -8
  62. package/dist/runtime/host-runtime-lease.js +1193 -120
  63. package/dist/runtime/host-runtime-promotion.d.ts +12 -3
  64. package/dist/runtime/host-runtime-promotion.js +59 -12
  65. package/dist/runtime/managed-command-teardown.d.ts +62 -0
  66. package/dist/runtime/managed-command-teardown.js +607 -0
  67. package/dist/runtime/memory-capacity.d.ts +70 -0
  68. package/dist/runtime/memory-capacity.js +123 -0
  69. package/dist/runtime/native-test-postgres-fallback.d.ts +25 -0
  70. package/dist/runtime/native-test-postgres-fallback.js +91 -0
  71. package/dist/runtime/node-host-runtime-adapter.d.ts +3 -0
  72. package/dist/runtime/node-host-runtime-adapter.js +29 -11
  73. package/dist/runtime/pinned-directory-process.d.ts +9 -0
  74. package/dist/runtime/pinned-directory-process.js +61 -0
  75. package/dist/runtime/pinned-source-directory.d.ts +17 -0
  76. package/dist/runtime/pinned-source-directory.js +94 -0
  77. package/dist/runtime/playwright-browser-cache.d.ts +39 -0
  78. package/dist/runtime/playwright-browser-cache.js +227 -0
  79. package/dist/runtime/playwright-host-runtime-adapter.d.ts +12 -0
  80. package/dist/runtime/playwright-host-runtime-adapter.js +100 -58
  81. package/dist/runtime/prepared-runtime-identity.d.ts +6 -0
  82. package/dist/runtime/prepared-runtime-identity.js +218 -0
  83. package/dist/runtime/prepared-runtime-readiness.d.ts +30 -0
  84. package/dist/runtime/prepared-runtime-readiness.js +285 -0
  85. package/dist/runtime/runtime-isolation-lease.js +4 -1
  86. package/dist/runtime/workspace-resource-estimator.d.ts +17 -0
  87. package/dist/runtime/workspace-resource-estimator.js +174 -0
  88. package/dist/sandbox/preflight.d.ts +31 -1
  89. package/dist/sandbox/preflight.js +237 -51
  90. package/dist/sandbox/repository-lock.d.ts +2 -1
  91. package/dist/sandbox/repository-lock.js +19 -9
  92. package/dist/sandbox/runtime.d.ts +6 -1
  93. package/dist/sandbox/runtime.js +137 -107
  94. package/dist/sandbox/sandbox.types.d.ts +7 -0
  95. package/dist/sandbox/workspace.service.js +2 -0
  96. package/dist/session/managed-attempt-tools.d.ts +42 -1
  97. package/dist/session/managed-attempt-tools.js +692 -40
  98. package/dist/session/managed-build-coordinator.d.ts +5 -0
  99. package/dist/session/managed-build-coordinator.js +109 -0
  100. package/dist/session/managed-build-receipt.d.ts +21 -0
  101. package/dist/session/managed-build-receipt.js +337 -0
  102. package/dist/session/managed-verification-timeouts.d.ts +8 -0
  103. package/dist/session/managed-verification-timeouts.js +17 -0
  104. package/dist/session/phase-completion.artifacts.d.ts +17 -0
  105. package/dist/session/phase-completion.artifacts.js +228 -22
  106. package/dist/session/phase-completion.d.ts +11 -1
  107. package/dist/session/phase-completion.helpers.d.ts +5 -0
  108. package/dist/session/phase-completion.helpers.js +30 -7
  109. package/dist/session/phase-completion.js +14 -3
  110. package/dist/session/phase-completion.prompts.js +17 -18
  111. package/dist/session/phase-completion.runtime.d.ts +3 -1
  112. package/dist/session/phase-completion.runtime.js +148 -34
  113. package/dist/session/phase-completion.schemas.js +49 -2
  114. package/dist/session/phase-completion.validation.d.ts +1 -0
  115. package/dist/session/phase-completion.validation.js +111 -21
  116. package/dist/session/pre-publication-process.d.ts +20 -0
  117. package/dist/session/pre-publication-process.js +97 -0
  118. package/dist/session/pre-publication-skipped-tests.d.ts +21 -0
  119. package/dist/session/pre-publication-skipped-tests.js +109 -0
  120. package/dist/session/pre-publication-validation.d.ts +75 -0
  121. package/dist/session/pre-publication-validation.js +631 -0
  122. package/dist/session/provider-cli/bridge-cli.conformance-fixtures.d.ts +72 -0
  123. package/dist/session/provider-cli/bridge-cli.conformance-fixtures.js +462 -0
  124. package/dist/session/provider-cli/ci-repair-readiness.d.ts +8 -0
  125. package/dist/session/provider-cli/ci-repair-readiness.js +23 -3
  126. package/dist/session/provider-cli/provider-cli.artifacts.d.ts +6 -3
  127. package/dist/session/provider-cli/provider-cli.artifacts.js +62 -22
  128. package/dist/session/provider-cli/provider-cli.deterministic.js +34 -2
  129. package/dist/session/provider-cli/provider-cli.merge-integrity.d.ts +89 -0
  130. package/dist/session/provider-cli/provider-cli.merge-integrity.js +310 -0
  131. package/dist/session/provider-cli/provider-cli.process.d.ts +80 -0
  132. package/dist/session/provider-cli/provider-cli.process.js +694 -28
  133. package/dist/session/provider-cli/provider-cli.prompts.d.ts +10 -0
  134. package/dist/session/provider-cli/provider-cli.prompts.js +151 -16
  135. package/dist/session/provider-cli/provider-cli.repair-epoch.d.ts +6 -0
  136. package/dist/session/provider-cli/provider-cli.repair-epoch.js +11 -0
  137. package/dist/session/provider-cli/provider-cli.runtime.d.ts +14 -1
  138. package/dist/session/provider-cli/provider-cli.runtime.js +54 -3
  139. package/dist/session/provider-cli/provider-cli.service.d.ts +30 -5
  140. package/dist/session/provider-cli/provider-cli.service.js +858 -77
  141. package/dist/session/provider-cli/provider-cli.types.d.ts +2 -0
  142. package/dist/session/provider-cli/provider-in-flight-work.d.ts +6 -0
  143. package/dist/session/provider-cli/provider-in-flight-work.js +47 -0
  144. package/dist/session/provider-cli/provider-output-state.d.ts +16 -0
  145. package/dist/session/provider-cli/provider-output-state.js +370 -17
  146. package/dist/session/provider-cli/provider-usage.d.ts +9 -1
  147. package/dist/session/provider-cli/provider-usage.js +28 -5
  148. package/dist/session/publishable-worktree-changes.d.ts +54 -0
  149. package/dist/session/publishable-worktree-changes.js +193 -0
  150. package/dist/session/session-redaction.js +434 -9
  151. package/dist/session/session.service.js +145 -23
  152. package/dist/session/session.types.d.ts +147 -2
  153. package/dist/session/session.types.js +599 -7
  154. package/dist/session/unreferenced-exports.d.ts +12 -0
  155. package/dist/session/unreferenced-exports.js +59 -0
  156. package/dist/shared/format/format-bytes.d.ts +1 -0
  157. package/dist/shared/format/format-bytes.js +9 -0
  158. package/dist/shared/git/git.js +2 -1
  159. package/dist/shared/path/path.d.ts +1 -0
  160. package/dist/shared/path/path.js +18 -0
  161. package/dist/shared/process/dotnet-toolchain.d.ts +17 -0
  162. package/dist/shared/process/dotnet-toolchain.js +307 -0
  163. package/dist/shared/process/host-process-recovery.js +3 -2
  164. package/dist/shared/process/host-process-supervisor.d.ts +2 -0
  165. package/dist/shared/process/host-process-supervisor.js +60 -11
  166. package/dist/shared/process/process.d.ts +2 -0
  167. package/dist/shared/process/process.js +12 -7
  168. package/dist/shared/provider-capabilities.d.ts +3 -0
  169. package/dist/shared/provider-capabilities.js +85 -0
  170. package/dist/shared/redaction/redaction.d.ts +1 -0
  171. package/dist/shared/redaction/redaction.js +39 -3
  172. package/dist/shared/time/monotonic.d.ts +1 -0
  173. package/dist/shared/time/monotonic.js +8 -0
  174. package/dist/version.d.ts +1 -1
  175. package/dist/version.js +1 -1
  176. package/dist/worktree/dependency-warmer.d.ts +41 -9
  177. package/dist/worktree/dependency-warmer.js +571 -22
  178. package/dist/worktree/index.d.ts +1 -1
  179. package/dist/worktree/story-qa-reference-hydrator.d.ts +4 -0
  180. package/dist/worktree/story-qa-reference-hydrator.js +253 -0
  181. package/dist/worktree/worktree.service.d.ts +7 -0
  182. package/dist/worktree/worktree.service.js +253 -21
  183. package/dist/worktree/worktree.types.d.ts +7 -1
  184. package/package.json +7 -5
  185. package/runtime/windows-job-supervisor.ps1 +380 -14
@@ -0,0 +1,83 @@
1
+ import { type HostEnvironment, type HostFacts } from "./host-facts.js";
2
+ import { type PressureStallResource } from "./host-pressure-stall.js";
3
+ export interface HostSystemPressure {
4
+ readonly cpu: PressureStallResource | null;
5
+ readonly memory: PressureStallResource | null;
6
+ readonly io: PressureStallResource | null;
7
+ }
8
+ /** Load attributed to the systemd unit (cgroup v2) the bridge itself runs in. */
9
+ export interface HostOwnCgroupLoad {
10
+ readonly unit: string;
11
+ readonly cpuPressure: PressureStallResource | null;
12
+ readonly memoryPressure: PressureStallResource | null;
13
+ /** CPU cores consumed by the unit, from `cpu.stat` usage deltas between samples. */
14
+ readonly cpuCoresInUse: number | null;
15
+ /** Anonymous memory (`memory.stat` anon): the RSS-like figure, excluding page cache. */
16
+ readonly memoryAnonBytes: number | null;
17
+ /** All memory charged to the unit (`memory.current`), including page cache. */
18
+ readonly memoryChargedBytes: number | null;
19
+ }
20
+ /** Live host load. Reported in every heartbeat but never a material capability signal. */
21
+ export interface HostLoadSnapshot {
22
+ readonly sampledAt: string;
23
+ readonly psiAvailable: boolean;
24
+ readonly systemPressure: HostSystemPressure | null;
25
+ /** True only when the bridge runs in a cgroup of its own (see isDedicatedBridgeUnit). */
26
+ readonly ownCgroupDedicated: boolean;
27
+ /** Reported only for a dedicated unit; never a shared terminal/session/app scope. */
28
+ readonly ownCgroup: HostOwnCgroupLoad | null;
29
+ readonly memoryAvailableBytes: number | null;
30
+ }
31
+ /** What the bridge service needs: static facts plus the latest cached load sample. */
32
+ export interface HostLoadSource {
33
+ start(): Promise<void>;
34
+ stop(): void;
35
+ facts(): HostFacts | null;
36
+ current(): HostLoadSnapshot | null;
37
+ }
38
+ export interface HostLoadSamplerOptions {
39
+ readonly environment?: HostEnvironment;
40
+ readonly intervalMs?: number;
41
+ /** Wall clock for `sampledAt`. */
42
+ readonly now?: () => number;
43
+ /** Monotonic clock (ms) for usage deltas, so wall-clock steps cannot skew cores in use. */
44
+ readonly monotonicNow?: () => number;
45
+ /** Systemd unit the operator dedicates to the bridge (FORGE_BRIDGE_CGROUP_UNIT). */
46
+ readonly cgroupUnit?: string | null;
47
+ }
48
+ export declare const defaultHostLoadSampleIntervalMs = 5000;
49
+ /**
50
+ * Samples Linux PSI and the bridge's own cgroup on an interval and caches the
51
+ * latest snapshot. Every read is async and failure-tolerant: a host without PSI
52
+ * (non-Linux, old kernel, `psi=0`) reports `psiAvailable: false` and no pressure.
53
+ */
54
+ export declare class HostLoadSampler implements HostLoadSource {
55
+ private readonly environment;
56
+ private readonly intervalMs;
57
+ private readonly now;
58
+ private readonly monotonicNow;
59
+ private readonly cgroupUnit;
60
+ private generation;
61
+ private timer;
62
+ private starting;
63
+ private inFlight;
64
+ private hostFacts;
65
+ private latest;
66
+ private cpuMark;
67
+ constructor(options?: HostLoadSamplerOptions);
68
+ facts(): HostFacts | null;
69
+ current(): HostLoadSnapshot | null;
70
+ /** Detects static facts once, takes a first sample, then samples on the interval. Idempotent. */
71
+ start(): Promise<void>;
72
+ stop(): void;
73
+ /** Takes one sample now and caches it. Concurrent callers share one read. */
74
+ sample(): Promise<HostLoadSnapshot>;
75
+ private startOnce;
76
+ private readSnapshot;
77
+ private readSystemPressure;
78
+ private readOwnCgroup;
79
+ private coresInUse;
80
+ private readMemoryAvailable;
81
+ private fallbackFreeMemory;
82
+ private readPressure;
83
+ }
@@ -0,0 +1,164 @@
1
+ import path from "node:path";
2
+ import { defaultHostEnvironment, detectHostFacts, readOptional, } from "./host-facts.js";
3
+ import { cgroupLeafName, isDedicatedBridgeUnit, parseCgroupCpuUsageUsec, parseCgroupMemoryAnonBytes, parseMemAvailableBytes, parseNonNegativeInteger, parsePressureStall, parseUnifiedCgroupPath, } from "./host-pressure-stall.js";
4
+ export const defaultHostLoadSampleIntervalMs = 5_000;
5
+ /**
6
+ * Samples Linux PSI and the bridge's own cgroup on an interval and caches the
7
+ * latest snapshot. Every read is async and failure-tolerant: a host without PSI
8
+ * (non-Linux, old kernel, `psi=0`) reports `psiAvailable: false` and no pressure.
9
+ */
10
+ export class HostLoadSampler {
11
+ environment;
12
+ intervalMs;
13
+ now;
14
+ monotonicNow;
15
+ cgroupUnit;
16
+ generation = 0;
17
+ timer = null;
18
+ starting = null;
19
+ inFlight = null;
20
+ hostFacts = null;
21
+ latest = null;
22
+ cpuMark = null;
23
+ constructor(options = {}) {
24
+ this.environment = options.environment ?? defaultHostEnvironment();
25
+ this.intervalMs = options.intervalMs !== undefined && Number.isFinite(options.intervalMs) && options.intervalMs > 0
26
+ ? options.intervalMs
27
+ : defaultHostLoadSampleIntervalMs;
28
+ this.now = options.now ?? Date.now;
29
+ this.monotonicNow = options.monotonicNow ?? (() => performance.now());
30
+ this.cgroupUnit = options.cgroupUnit !== undefined
31
+ ? options.cgroupUnit
32
+ : process.env.FORGE_BRIDGE_CGROUP_UNIT?.trim() || null;
33
+ }
34
+ facts() {
35
+ return this.hostFacts;
36
+ }
37
+ current() {
38
+ return this.latest;
39
+ }
40
+ /** Detects static facts once, takes a first sample, then samples on the interval. Idempotent. */
41
+ start() {
42
+ if (this.timer)
43
+ return Promise.resolve();
44
+ this.starting ??= this.startOnce().finally(() => {
45
+ this.starting = null;
46
+ });
47
+ return this.starting;
48
+ }
49
+ stop() {
50
+ // Invalidates an in-progress start() so it cannot arm the interval afterwards.
51
+ this.generation += 1;
52
+ if (this.timer)
53
+ clearInterval(this.timer);
54
+ this.timer = null;
55
+ }
56
+ /** Takes one sample now and caches it. Concurrent callers share one read. */
57
+ sample() {
58
+ this.inFlight ??= this.readSnapshot()
59
+ .then(snapshot => {
60
+ this.latest = snapshot;
61
+ return snapshot;
62
+ })
63
+ .finally(() => {
64
+ this.inFlight = null;
65
+ });
66
+ return this.inFlight;
67
+ }
68
+ async startOnce() {
69
+ const generation = this.generation;
70
+ if (!this.hostFacts) {
71
+ this.hostFacts = await detectHostFacts(this.environment).catch(() => null);
72
+ }
73
+ await this.sample().catch(() => undefined);
74
+ if (this.timer || generation !== this.generation)
75
+ return;
76
+ this.timer = setInterval(() => {
77
+ void this.sample().catch(() => undefined);
78
+ }, this.intervalMs);
79
+ this.timer.unref?.();
80
+ }
81
+ async readSnapshot() {
82
+ const sampledAtMs = this.now();
83
+ const monotonicMs = this.monotonicNow();
84
+ const linux = this.environment.platform === "linux";
85
+ const [systemPressure, ownCgroup, memoryAvailableBytes] = linux
86
+ ? await Promise.all([this.readSystemPressure(), this.readOwnCgroup(monotonicMs), this.readMemoryAvailable()])
87
+ : [null, null, this.fallbackFreeMemory()];
88
+ return {
89
+ sampledAt: new Date(sampledAtMs).toISOString(),
90
+ psiAvailable: systemPressure !== null,
91
+ systemPressure,
92
+ ownCgroupDedicated: ownCgroup !== null,
93
+ ownCgroup,
94
+ memoryAvailableBytes,
95
+ };
96
+ }
97
+ async readSystemPressure() {
98
+ const pressureRoot = path.posix.join(this.environment.procRoot, "pressure");
99
+ const [cpu, memory, io] = await Promise.all(["cpu", "memory", "io"].map(resource => this.readPressure(path.posix.join(pressureRoot, resource))));
100
+ // CPU pressure is the governor's primary signal; without it PSI is unavailable.
101
+ return cpu ? { cpu, memory: memory ?? null, io: io ?? null } : null;
102
+ }
103
+ async readOwnCgroup(monotonicMs) {
104
+ const membership = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "self", "cgroup"));
105
+ const cgroupPath = membership ? parseUnifiedCgroupPath(membership) : null;
106
+ const unit = cgroupPath ? cgroupLeafName(cgroupPath) : null;
107
+ if (!cgroupPath || !unit || !isDedicatedBridgeUnit(unit, this.cgroupUnit)) {
108
+ this.cpuMark = null;
109
+ return null;
110
+ }
111
+ const directory = path.posix.join(this.environment.cgroupRoot, cgroupPath);
112
+ if (!directory.startsWith(`${this.environment.cgroupRoot.replace(/\/+$/u, "")}/`))
113
+ return null;
114
+ const [cpuPressure, memoryPressure, cpuStat, memoryStat, memoryCurrent] = await Promise.all([
115
+ this.readPressure(path.posix.join(directory, "cpu.pressure")),
116
+ this.readPressure(path.posix.join(directory, "memory.pressure")),
117
+ readOptional(this.environment, path.posix.join(directory, "cpu.stat")),
118
+ readOptional(this.environment, path.posix.join(directory, "memory.stat")),
119
+ readOptional(this.environment, path.posix.join(directory, "memory.current")),
120
+ ]);
121
+ const usageUsec = cpuStat ? parseCgroupCpuUsageUsec(cpuStat) : null;
122
+ const memoryAnonBytes = memoryStat ? parseCgroupMemoryAnonBytes(memoryStat) : null;
123
+ const memoryChargedBytes = memoryCurrent ? parseNonNegativeInteger(memoryCurrent) : null;
124
+ // Ownership is settled by membership and the unit name above; metric files that are
125
+ // unavailable yield null values (and reset the CPU mark) but never demote the unit to shared.
126
+ return {
127
+ unit,
128
+ cpuPressure,
129
+ memoryPressure,
130
+ cpuCoresInUse: this.coresInUse(cgroupPath, usageUsec, monotonicMs),
131
+ memoryAnonBytes,
132
+ memoryChargedBytes,
133
+ };
134
+ }
135
+ coresInUse(cgroupPath, usageUsec, atMs) {
136
+ const previous = this.cpuMark;
137
+ this.cpuMark = usageUsec === null ? null : { cgroupPath, usageUsec, atMs };
138
+ if (!previous || usageUsec === null || previous.cgroupPath !== cgroupPath)
139
+ return null;
140
+ const elapsedUsec = (atMs - previous.atMs) * 1_000;
141
+ const usedUsec = usageUsec - previous.usageUsec;
142
+ // A restarted unit resets its counter; elapsed time comes from a monotonic clock.
143
+ if (elapsedUsec <= 0 || usedUsec < 0)
144
+ return null;
145
+ return Math.round((usedUsec / elapsedUsec) * 100) / 100;
146
+ }
147
+ async readMemoryAvailable() {
148
+ const meminfo = await readOptional(this.environment, path.posix.join(this.environment.procRoot, "meminfo"));
149
+ return (meminfo ? parseMemAvailableBytes(meminfo) : null) ?? this.fallbackFreeMemory();
150
+ }
151
+ fallbackFreeMemory() {
152
+ try {
153
+ const free = this.environment.freeMemoryBytes();
154
+ return Number.isFinite(free) && free >= 0 ? free : null;
155
+ }
156
+ catch {
157
+ return null;
158
+ }
159
+ }
160
+ async readPressure(filePath) {
161
+ const content = await readOptional(this.environment, filePath);
162
+ return content ? parsePressureStall(content) : null;
163
+ }
164
+ }
@@ -0,0 +1,41 @@
1
+ export interface PressureStallAverages {
2
+ /** Percent of wall time stalled over the last 10 seconds (0-100). */
3
+ readonly avg10: number;
4
+ readonly avg60: number;
5
+ readonly avg300: number;
6
+ }
7
+ export interface PressureStallResource {
8
+ readonly some: PressureStallAverages | null;
9
+ readonly full: PressureStallAverages | null;
10
+ }
11
+ /**
12
+ * Parses one PSI file. Returns null when the content has no well-formed
13
+ * `some`/`full` line, so callers can report PSI as unavailable instead of
14
+ * publishing zeros for a file they could not interpret.
15
+ */
16
+ export declare function parsePressureStall(content: string): PressureStallResource | null;
17
+ /** Reads `usage_usec` from a cgroup v2 `cpu.stat` file. */
18
+ export declare function parseCgroupCpuUsageUsec(content: string): number | null;
19
+ /** Reads anonymous (RSS-like, excludes page cache) bytes from cgroup v2 `memory.stat`. */
20
+ export declare function parseCgroupMemoryAnonBytes(content: string): number | null;
21
+ /** Reads a single non-negative integer file such as cgroup v2 `memory.current`. */
22
+ export declare function parseNonNegativeInteger(content: string): number | null;
23
+ /** Reads `MemAvailable` (bytes) from /proc/meminfo. */
24
+ export declare function parseMemAvailableBytes(content: string): number | null;
25
+ /**
26
+ * Resolves the unified (cgroup v2) hierarchy path from /proc/self/cgroup.
27
+ * Returns null on pure cgroup v1 hosts (no `0::` entry) and for the root cgroup.
28
+ */
29
+ export declare function parseUnifiedCgroupPath(content: string): string | null;
30
+ /** The leaf cgroup name, e.g. `forge-bridge.service`. */
31
+ export declare function cgroupLeafName(cgroupPath: string): string | null;
32
+ /**
33
+ * Whether the bridge's cgroup is dedicated to it, so its usage can later be
34
+ * subtracted from system pressure as "Forge's own load". Only a systemd
35
+ * `.service` qualifies by default, or exactly the unit an operator names via
36
+ * FORGE_BRIDGE_CGROUP_UNIT. A `.scope` is rejected: a bridge started from a
37
+ * terminal, SSH session or desktop app shares `session-N.scope` /
38
+ * `app-*.scope` with unrelated work (possibly the owner's game).
39
+ * Container cgroups (e.g. `docker-<id>.scope`) are added in decision 0013 step 2.
40
+ */
41
+ export declare function isDedicatedBridgeUnit(unit: string, configuredUnit: string | null | undefined): boolean;
@@ -0,0 +1,121 @@
1
+ // Linux Pressure Stall Information (PSI) parsing shared by the host load sampler.
2
+ // Format (system-wide /proc/pressure/<resource> and cgroup v2 <resource>.pressure):
3
+ // some avg10=0.12 avg60=0.34 avg300=0.56 total=123456
4
+ // full avg10=0.00 avg60=0.00 avg300=0.00 total=0
5
+ const pressureLinePattern = /^(some|full)\s+(.*)$/u;
6
+ /**
7
+ * Parses one PSI file. Returns null when the content has no well-formed
8
+ * `some`/`full` line, so callers can report PSI as unavailable instead of
9
+ * publishing zeros for a file they could not interpret.
10
+ */
11
+ export function parsePressureStall(content) {
12
+ let some = null;
13
+ let full = null;
14
+ for (const rawLine of content.split(/\r?\n/u)) {
15
+ const match = pressureLinePattern.exec(rawLine.trim());
16
+ if (!match)
17
+ continue;
18
+ const averages = parseAverages(match[2]);
19
+ if (!averages)
20
+ continue;
21
+ if (match[1] === "some")
22
+ some = averages;
23
+ else
24
+ full = averages;
25
+ }
26
+ return some || full ? { some, full } : null;
27
+ }
28
+ const pressureFieldPattern = /^(avg10|avg60|avg300|total)=(\d+(?:\.\d+)?)$/u;
29
+ /** A PSI line must carry exactly avg10, avg60, avg300 and an integer total; anything else is malformed. */
30
+ function parseAverages(fields) {
31
+ const values = new Map();
32
+ for (const field of fields.trim().split(/\s+/u)) {
33
+ const match = pressureFieldPattern.exec(field);
34
+ if (!match || values.has(match[1]))
35
+ return null;
36
+ values.set(match[1], Number(match[2]));
37
+ }
38
+ const avg10 = values.get("avg10");
39
+ const avg60 = values.get("avg60");
40
+ const avg300 = values.get("avg300");
41
+ const total = values.get("total");
42
+ if (avg10 === undefined || avg60 === undefined || avg300 === undefined
43
+ || total === undefined || !Number.isSafeInteger(total))
44
+ return null;
45
+ return { avg10: clampPercent(avg10), avg60: clampPercent(avg60), avg300: clampPercent(avg300) };
46
+ }
47
+ function clampPercent(value) {
48
+ return Math.min(100, Math.max(0, value));
49
+ }
50
+ /** Reads `usage_usec` from a cgroup v2 `cpu.stat` file. */
51
+ export function parseCgroupCpuUsageUsec(content) {
52
+ const match = /^usage_usec\s+(\d+)\s*$/mu.exec(content);
53
+ if (!match)
54
+ return null;
55
+ const value = Number(match[1]);
56
+ return Number.isSafeInteger(value) ? value : null;
57
+ }
58
+ /** Reads anonymous (RSS-like, excludes page cache) bytes from cgroup v2 `memory.stat`. */
59
+ export function parseCgroupMemoryAnonBytes(content) {
60
+ const match = /^anon\s+(\d+)\s*$/mu.exec(content);
61
+ if (!match)
62
+ return null;
63
+ const value = Number(match[1]);
64
+ return Number.isSafeInteger(value) ? value : null;
65
+ }
66
+ /** Reads a single non-negative integer file such as cgroup v2 `memory.current`. */
67
+ export function parseNonNegativeInteger(content) {
68
+ const trimmed = content.trim();
69
+ if (!/^\d+$/u.test(trimmed))
70
+ return null;
71
+ const value = Number(trimmed);
72
+ return Number.isSafeInteger(value) ? value : null;
73
+ }
74
+ /** Reads `MemAvailable` (bytes) from /proc/meminfo. */
75
+ export function parseMemAvailableBytes(content) {
76
+ const match = /^MemAvailable:\s+(\d+)\s*kB\s*$/mu.exec(content);
77
+ if (!match)
78
+ return null;
79
+ const value = Number(match[1]) * 1024;
80
+ return Number.isSafeInteger(value) ? value : null;
81
+ }
82
+ /**
83
+ * Resolves the unified (cgroup v2) hierarchy path from /proc/self/cgroup.
84
+ * Returns null on pure cgroup v1 hosts (no `0::` entry) and for the root cgroup.
85
+ */
86
+ export function parseUnifiedCgroupPath(content) {
87
+ for (const rawLine of content.split(/\r?\n/u)) {
88
+ const line = rawLine.trim();
89
+ if (!line.startsWith("0::"))
90
+ continue;
91
+ const cgroupPath = line.slice(3).trim();
92
+ if (!cgroupPath.startsWith("/") || cgroupPath === "/")
93
+ return null;
94
+ if (cgroupPath.split("/").some(segment => segment === ".."))
95
+ return null;
96
+ return cgroupPath;
97
+ }
98
+ return null;
99
+ }
100
+ /** The leaf cgroup name, e.g. `forge-bridge.service`. */
101
+ export function cgroupLeafName(cgroupPath) {
102
+ return cgroupPath.split("/").filter(Boolean).at(-1) ?? null;
103
+ }
104
+ /**
105
+ * Whether the bridge's cgroup is dedicated to it, so its usage can later be
106
+ * subtracted from system pressure as "Forge's own load". Only a systemd
107
+ * `.service` qualifies by default, or exactly the unit an operator names via
108
+ * FORGE_BRIDGE_CGROUP_UNIT. A `.scope` is rejected: a bridge started from a
109
+ * terminal, SSH session or desktop app shares `session-N.scope` /
110
+ * `app-*.scope` with unrelated work (possibly the owner's game).
111
+ * Container cgroups (e.g. `docker-<id>.scope`) are added in decision 0013 step 2.
112
+ */
113
+ export function isDedicatedBridgeUnit(unit, configuredUnit) {
114
+ const configured = configuredUnit?.trim();
115
+ if (configured)
116
+ return unit === configured;
117
+ // `app-*` services are desktop-launched apps (XDG naming); `user@N` is the whole user manager.
118
+ return /^[^@\s]+(@[^\s]+)?\.service$/u.test(unit)
119
+ && !/^app-/u.test(unit)
120
+ && !/^user@\d+\.service$/u.test(unit);
121
+ }
@@ -1,4 +1,7 @@
1
1
  export declare const hostRuntimeContractPath = ".forge/forge.runtime.yml";
2
+ export declare const hostRuntimeTestPostgresAdminEnvironment = "FORGE_TEST_POSTGRES_ADMIN_CONNECTION_STRING";
3
+ export declare const hostRuntimeLegacyTestPostgresEnvironment = "FORGE_TEST_POSTGRES_CONNECTION_STRING";
4
+ export declare const hostRuntimeTestPostgresProvisionerEnvironment = "FORGE_TEST_POSTGRES_PROVISIONER_CONNECTION_STRING";
2
5
  export type RequestedHostConcurrency = "serial" | "parallel" | "disabled";
3
6
  export type HostRuntimePortAdapter = "node" | "vite" | "next" | "aspnetcore";
4
7
  export interface HostRuntimeLaunchConfig {
@@ -41,7 +44,16 @@ export interface HostRuntimeDiscovery {
41
44
  readonly summary: string;
42
45
  readonly candidateFingerprint?: string | null;
43
46
  }
44
- export declare function loadHostRuntimeContract(worktreePath: string): Promise<LoadedHostRuntimeContract>;
47
+ /** Returns retained bytes (or retained absence) for one canonical discovery input. */
48
+ export type HostRuntimeSourceSnapshot = (target: string) => Promise<Buffer | null>;
49
+ export interface HostRuntimeDirectoryEntry {
50
+ readonly name: string;
51
+ readonly isDirectory: boolean;
52
+ readonly isFile: boolean;
53
+ }
54
+ /** Returns one identity-bound directory inventory for runtime discovery. */
55
+ export type HostRuntimeDirectorySnapshot = (target: string) => Promise<readonly HostRuntimeDirectoryEntry[]>;
56
+ export declare function loadHostRuntimeContract(worktreePath: string, snapshot?: HostRuntimeSourceSnapshot, directorySnapshot?: HostRuntimeDirectorySnapshot): Promise<LoadedHostRuntimeContract>;
45
57
  export declare function defaultHostRuntimeContract(): HostRuntimeContract;
46
58
  export declare function validateHostRuntimeContract(source: string): HostRuntimeContract;
47
59
  export declare function hostPortEnvironment(config: HostRuntimePortConfig, hostIp: "127.0.0.1", hostPort: number): Readonly<Record<string, string>>;