@loucompanion/forge-bridge 0.1.3-dev.8687c5099dce → 0.1.3-dev.88e93f53db5b

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 (75) hide show
  1. package/README.md +18 -1
  2. package/dist/bridge/bridge.base.d.ts +2 -0
  3. package/dist/bridge/bridge.service.d.ts +25 -1
  4. package/dist/bridge/bridge.service.js +341 -74
  5. package/dist/bridge/bridge.types.d.ts +4 -0
  6. package/dist/cleanup/cleanup.service.d.ts +1 -0
  7. package/dist/cleanup/cleanup.service.js +70 -9
  8. package/dist/cli/cli.service.js +9 -4
  9. package/dist/runtime/aspnet-runtime-prebuild.d.ts +36 -0
  10. package/dist/runtime/aspnet-runtime-prebuild.js +340 -0
  11. package/dist/runtime/attempt-node-runtime-launcher.js +5 -1
  12. package/dist/runtime/attempt-process-supervisor.d.ts +11 -0
  13. package/dist/runtime/attempt-process-supervisor.js +36 -2
  14. package/dist/runtime/host-listener-inspector.d.ts +2 -0
  15. package/dist/runtime/host-listener-inspector.js +25 -0
  16. package/dist/runtime/host-runtime-lease.d.ts +66 -5
  17. package/dist/runtime/host-runtime-lease.js +714 -38
  18. package/dist/runtime/memory-capacity.d.ts +68 -0
  19. package/dist/runtime/memory-capacity.js +121 -0
  20. package/dist/runtime/node-host-runtime-adapter.d.ts +3 -0
  21. package/dist/runtime/node-host-runtime-adapter.js +24 -8
  22. package/dist/runtime/playwright-host-runtime-adapter.d.ts +1 -0
  23. package/dist/runtime/playwright-host-runtime-adapter.js +5 -0
  24. package/dist/runtime/workspace-resource-estimator.d.ts +17 -0
  25. package/dist/runtime/workspace-resource-estimator.js +174 -0
  26. package/dist/sandbox/preflight.d.ts +31 -1
  27. package/dist/sandbox/preflight.js +237 -51
  28. package/dist/sandbox/repository-lock.js +4 -2
  29. package/dist/sandbox/sandbox.types.d.ts +7 -0
  30. package/dist/session/managed-attempt-tools.d.ts +7 -1
  31. package/dist/session/managed-attempt-tools.js +257 -13
  32. package/dist/session/managed-build-coordinator.d.ts +5 -0
  33. package/dist/session/managed-build-coordinator.js +105 -0
  34. package/dist/session/managed-build-receipt.d.ts +21 -0
  35. package/dist/session/managed-build-receipt.js +335 -0
  36. package/dist/session/phase-completion.artifacts.d.ts +17 -0
  37. package/dist/session/phase-completion.artifacts.js +224 -22
  38. package/dist/session/phase-completion.prompts.js +2 -2
  39. package/dist/session/phase-completion.runtime.js +10 -3
  40. package/dist/session/phase-completion.schemas.js +1 -1
  41. package/dist/session/phase-completion.validation.js +7 -3
  42. package/dist/session/provider-cli/ci-repair-readiness.d.ts +6 -0
  43. package/dist/session/provider-cli/ci-repair-readiness.js +12 -2
  44. package/dist/session/provider-cli/provider-cli.artifacts.d.ts +2 -1
  45. package/dist/session/provider-cli/provider-cli.artifacts.js +37 -3
  46. package/dist/session/provider-cli/provider-cli.process.d.ts +7 -0
  47. package/dist/session/provider-cli/provider-cli.process.js +218 -17
  48. package/dist/session/provider-cli/provider-cli.prompts.js +17 -4
  49. package/dist/session/provider-cli/provider-cli.runtime.js +3 -1
  50. package/dist/session/provider-cli/provider-cli.service.d.ts +5 -2
  51. package/dist/session/provider-cli/provider-cli.service.js +83 -34
  52. package/dist/session/provider-cli/provider-output-state.js +61 -14
  53. package/dist/session/provider-cli/provider-usage.d.ts +7 -0
  54. package/dist/session/provider-cli/provider-usage.js +28 -5
  55. package/dist/session/session-redaction.js +16 -1
  56. package/dist/session/session.service.js +81 -9
  57. package/dist/session/session.types.d.ts +15 -0
  58. package/dist/session/session.types.js +3 -1
  59. package/dist/shared/format/format-bytes.d.ts +1 -0
  60. package/dist/shared/format/format-bytes.js +9 -0
  61. package/dist/shared/path/path.d.ts +1 -0
  62. package/dist/shared/path/path.js +18 -0
  63. package/dist/shared/process/host-process-supervisor.d.ts +2 -0
  64. package/dist/shared/process/host-process-supervisor.js +57 -9
  65. package/dist/shared/redaction/redaction.js +6 -1
  66. package/dist/version.d.ts +1 -1
  67. package/dist/version.js +1 -1
  68. package/dist/worktree/dependency-warmer.d.ts +18 -0
  69. package/dist/worktree/dependency-warmer.js +218 -8
  70. package/dist/worktree/index.d.ts +1 -1
  71. package/dist/worktree/worktree.service.d.ts +2 -0
  72. package/dist/worktree/worktree.service.js +158 -17
  73. package/dist/worktree/worktree.types.d.ts +2 -0
  74. package/package.json +3 -2
  75. package/runtime/windows-job-supervisor.ps1 +89 -1
@@ -1,6 +1,7 @@
1
1
  import type { ApiKeySecretGrant, DispatchSandboxResourceRequest, RunOnceResultPacket, RunnerDispatchEvent } from "../session/session.types.js";
2
2
  import type { SandboxFeatureMatrix } from "../sandbox/sandbox.types.js";
3
3
  import type { HostRuntimeCapacitySnapshot } from "../runtime/host-runtime-lease.js";
4
+ import type { MemoryAdmissionDiagnostics } from "../runtime/memory-capacity.js";
4
5
  export interface BridgeServiceConfig {
5
6
  readonly serverUrl: string;
6
7
  readonly runnerId: string;
@@ -119,7 +120,10 @@ export interface SandboxMemoryCapacityDiagnostics {
119
120
  readonly activeReservationBytes: number;
120
121
  readonly daemonTotalBytes: number;
121
122
  readonly daemonAvailableBytes: number;
123
+ readonly hostAvailableBytes: number;
124
+ readonly conservativeAvailableBytes: number;
122
125
  readonly availableForAdmissionBytes: number;
126
+ readonly memoryAdmission: MemoryAdmissionDiagnostics;
123
127
  }
124
128
  export interface BridgeHelloRequest {
125
129
  readonly version: string;
@@ -10,6 +10,7 @@ export declare class ManagedWorktreeCleanupService implements CleanupService {
10
10
  run(request: CleanupRequest): Promise<CleanupResult>;
11
11
  private runLocked;
12
12
  private listWorktrees;
13
+ private staleRuntimeReadinessPaths;
13
14
  private scanManagedCandidates;
14
15
  private readRuntimeLeases;
15
16
  private worktreeMetadataRoot;
@@ -4,9 +4,10 @@ import fs from "node:fs/promises";
4
4
  import path from "node:path";
5
5
  import { RepositoryCoordinatorLock } from "../sandbox/repository-lock.js";
6
6
  import { GitClient } from "../shared/git/git.js";
7
- import { isSubPath, resolveExistingDirectoryPath } from "../shared/path/path.js";
7
+ import { isSubPath, resolveExistingDirectoryPath, resolvePathIdentity } from "../shared/path/path.js";
8
8
  import { validRuntimeIsolationLease } from "../runtime/runtime-isolation-lease.js";
9
9
  import { defaultWorktreeCoordinatorStateRoot, resolveManagedWorktreeRootForRepo, } from "../worktree/worktree-paths.js";
10
+ const staleRuntimeReadinessAgeMs = 30 * 60 * 1000;
10
11
  export class ManagedWorktreeCleanupService {
11
12
  stateRoot;
12
13
  git;
@@ -17,8 +18,13 @@ export class ManagedWorktreeCleanupService {
17
18
  this.coordinator = coordinator;
18
19
  }
19
20
  async run(request) {
20
- const repoPath = await resolveExistingDirectoryPath(request.repoRoot);
21
- const worktreeRoot = resolveManagedWorktreeRootForRepo(repoPath, request.allowedRepoRoot);
21
+ const [repoPath, allowedRepoRoot] = await Promise.all([
22
+ resolveExistingDirectoryPath(request.repoRoot),
23
+ request.allowedRepoRoot
24
+ ? resolvePathIdentity(request.allowedRepoRoot)
25
+ : Promise.resolve(undefined),
26
+ ]);
27
+ const worktreeRoot = resolveManagedWorktreeRootForRepo(repoPath, allowedRepoRoot);
22
28
  return await this.coordinator.withLock(repoPath, async () => {
23
29
  return await this.runLocked(repoPath, worktreeRoot, request);
24
30
  });
@@ -39,11 +45,13 @@ export class ManagedWorktreeCleanupService {
39
45
  this.listWorktrees(repoPath),
40
46
  this.readRuntimeLeases(),
41
47
  ]);
42
- const metadataRoot = await this.worktreeMetadataRoot(repoPath);
43
- const activeWorktrees = activePaths(listedWorktrees);
44
- const activeRuntimePaths = runtimeLeases
48
+ const activeRuntimePaths = await Promise.all(runtimeLeases
45
49
  .filter(lease => lease.state !== "released")
46
- .map(lease => path.resolve(lease.worktreePath));
50
+ .map(lease => resolveRuntimeLeasePath(lease.worktreePath)));
51
+ const metadataRoot = await this.worktreeMetadataRoot(repoPath);
52
+ const staleReadinessPaths = await this.staleRuntimeReadinessPaths(listedWorktrees, activeRuntimePaths);
53
+ const activeWorktrees = activePaths(listedWorktrees)
54
+ .filter(activePath => !staleReadinessPaths.some(stalePath => samePath(stalePath, activePath)));
47
55
  if (request.mode === "all") {
48
56
  const protectedPaths = uniquePaths([
49
57
  ...activeWorktrees.filter(activePath => isSubPath(safeRoot, activePath)),
@@ -88,9 +96,14 @@ export class ManagedWorktreeCleanupService {
88
96
  this.listWorktrees(repoPath),
89
97
  this.readRuntimeLeases(),
90
98
  ]);
99
+ const currentRuntimePaths = await Promise.all(currentLeases
100
+ .filter(lease => lease.state !== "released")
101
+ .map(lease => resolveRuntimeLeasePath(lease.worktreePath)));
102
+ const currentStaleReadinessPaths = await this.staleRuntimeReadinessPaths(currentWorktrees, currentRuntimePaths);
91
103
  const currentlyProtected = [
92
- ...activePaths(currentWorktrees),
93
- ...currentLeases.filter(lease => lease.state !== "released").map(lease => path.resolve(lease.worktreePath)),
104
+ ...activePaths(currentWorktrees)
105
+ .filter(activePath => !currentStaleReadinessPaths.some(stalePath => samePath(stalePath, activePath))),
106
+ ...currentRuntimePaths,
94
107
  ];
95
108
  if (worktreePathState(candidate, currentlyProtected) !== "none") {
96
109
  continue;
@@ -118,6 +131,24 @@ export class ManagedWorktreeCleanupService {
118
131
  const output = await this.git.run(repoPath, ["worktree", "list", "--porcelain", "-z"]);
119
132
  return parseWorktreeListing(repoPath, output);
120
133
  }
134
+ async staleRuntimeReadinessPaths(worktrees, runtimePaths) {
135
+ const stale = [];
136
+ for (const worktree of worktrees) {
137
+ if (worktree.prunable || !isRuntimeReadinessWorktree(worktree))
138
+ continue;
139
+ if (worktreePathState(worktree.worktreePath, runtimePaths) !== "none")
140
+ continue;
141
+ const stat = await fs.stat(worktree.worktreePath).catch(error => {
142
+ if (isEnoent(error))
143
+ return null;
144
+ throw error;
145
+ });
146
+ if (stat && Date.now() - stat.mtimeMs >= staleRuntimeReadinessAgeMs) {
147
+ stale.push(worktree.worktreePath);
148
+ }
149
+ }
150
+ return stale;
151
+ }
121
152
  async scanManagedCandidates(metadataRoot, worktreeRoot, activeWorktrees, runtimePaths) {
122
153
  const candidates = [];
123
154
  let protectedWorktrees = 0;
@@ -215,6 +246,28 @@ export class ManagedWorktreeCleanupService {
215
246
  return path.join(await fs.realpath(commonDirPath), "worktrees");
216
247
  }
217
248
  }
249
+ async function resolveRuntimeLeasePath(leasePath) {
250
+ const resolved = path.resolve(leasePath);
251
+ const before = await fs.lstat(resolved).catch(error => {
252
+ if (isEnoent(error))
253
+ return null;
254
+ throw error;
255
+ });
256
+ if (!before)
257
+ return await resolvePathIdentity(resolved);
258
+ if (before.isSymbolicLink() || !before.isDirectory()) {
259
+ throw new Error(`Refusing managed worktree cleanup because runtime lease path ${resolved} is redirected or is not a directory.`);
260
+ }
261
+ const real = await fs.realpath(resolved);
262
+ const after = await fs.lstat(resolved);
263
+ if (after.isSymbolicLink()
264
+ || !after.isDirectory()
265
+ || (before.dev !== 0 && before.ino !== 0 && after.dev !== 0 && after.ino !== 0
266
+ && (before.dev !== after.dev || before.ino !== after.ino))) {
267
+ throw new Error(`Refusing managed worktree cleanup because runtime lease path ${resolved} changed while being resolved.`);
268
+ }
269
+ return real;
270
+ }
218
271
  async function readRuntimeLedger(target, entryName) {
219
272
  const pathStat = await fs.lstat(target).catch(error => {
220
273
  if (isEnoent(error))
@@ -399,16 +452,24 @@ function parseWorktreeListing(repoPath, output) {
399
452
  current = {
400
453
  worktreePath: path.resolve(path.isAbsolute(rawPath) ? rawPath : path.join(repoPath, rawPath)),
401
454
  prunable: false,
455
+ branchName: null,
402
456
  };
403
457
  }
404
458
  else if (field.startsWith("prunable") && current) {
405
459
  current.prunable = true;
406
460
  }
461
+ else if (field.startsWith("branch ") && current) {
462
+ current.branchName = field.slice("branch ".length).trim();
463
+ }
407
464
  }
408
465
  if (current)
409
466
  worktrees.push(current);
410
467
  return worktrees;
411
468
  }
469
+ function isRuntimeReadinessWorktree(worktree) {
470
+ return worktree.branchName?.startsWith("refs/heads/runtime-readiness-") === true
471
+ || path.basename(worktree.worktreePath).startsWith("runtime-readiness-");
472
+ }
412
473
  function activePaths(worktrees) {
413
474
  return worktrees.filter(entry => !entry.prunable).map(entry => entry.worktreePath);
414
475
  }
@@ -72,6 +72,10 @@ export class ForgeBridgeCliService {
72
72
  process.once("SIGINT", stop);
73
73
  process.once("SIGTERM", stop);
74
74
  try {
75
+ if (config.cleanupWorktrees && config.repoMappings.some(mapping => mapping.localPath?.trim())) {
76
+ io.stdout.write("Cleaning stale managed worktrees before accepting dispatches.\n");
77
+ await cleanupRepos(config, "stale", true, this.cleanupFactory(config.sandboxStateRoot), io);
78
+ }
75
79
  io.stdout.write(connectStartupText(config));
76
80
  await this.bridgeFactory(config).run(controller.signal);
77
81
  return 0;
@@ -714,7 +718,7 @@ function parsePairArgs(argv) {
714
718
  name: name?.trim() || defaultName,
715
719
  fingerprint: fingerprint?.trim() || `local-bridge:${os.hostname()}:${user}`,
716
720
  repoRoot: repoRoot?.trim() ? path.resolve(repoRoot) : undefined,
717
- cleanupWorktrees: cleanupWorktrees ?? localConfig.cleanupWorktrees ?? false,
721
+ cleanupWorktrees: cleanupWorktrees ?? defaultConfig.cleanupWorktrees,
718
722
  cliProviders: cliProviders.length > 0 ? cliProviders : localConfig.cliProviders && localConfig.cliProviders.length > 0 ? [...localConfig.cliProviders] : ["codex"],
719
723
  projectLabels: projectLabels.length > 0 ? projectLabels : localConfig.projectLabels ? [...localConfig.projectLabels] : []
720
724
  };
@@ -1105,8 +1109,8 @@ function uniqueModels(models) {
1105
1109
  function isRecord(value) {
1106
1110
  return typeof value === "object" && value !== null;
1107
1111
  }
1108
- async function withDetectedCliProviders(config) {
1109
- const detected = await detectCliProviders(configuredProviderCandidates(config.cliProviders));
1112
+ async function withDetectedCliProviders(config, candidates = config.cliProviders) {
1113
+ const detected = await detectCliProviders(uniqueProviders(candidates));
1110
1114
  const found = detected.filter((result) => result.found);
1111
1115
  if (found.length === 0) {
1112
1116
  return config;
@@ -1130,6 +1134,7 @@ async function withDetectedCliProviders(config) {
1130
1134
  };
1131
1135
  }
1132
1136
  async function withDetectedPairCliProviders(args) {
1137
+ const configured = configuredProviderCandidates(args.cliProviders);
1133
1138
  const detectedConfig = await withDetectedCliProviders({
1134
1139
  ...defaultBridgeServiceConfig({
1135
1140
  serverUrl: args.serverUrl,
@@ -1139,7 +1144,7 @@ async function withDetectedPairCliProviders(args) {
1139
1144
  cleanupWorktrees: args.cleanupWorktrees,
1140
1145
  defaultRepoRoot: args.repoRoot ?? defaultBridgeServiceConfig().defaultRepoRoot
1141
1146
  })
1142
- });
1147
+ }, configured);
1143
1148
  return {
1144
1149
  ...args,
1145
1150
  cliProviders: detectedConfig.cliProviders
@@ -0,0 +1,36 @@
1
+ export interface AspNetRuntimePrebuildPlan {
2
+ readonly fingerprint: string;
3
+ readonly receiptPath: string;
4
+ readonly command: string;
5
+ readonly args: readonly string[];
6
+ readonly cwd: string;
7
+ readonly environment: Readonly<Record<string, string>>;
8
+ readonly projectPath: string;
9
+ readonly worktreePath: string;
10
+ readonly inputCommitSha: string;
11
+ }
12
+ export interface AspNetRuntimePrebuildResult {
13
+ readonly cacheHit: boolean;
14
+ readonly receiptPath: string;
15
+ readonly fingerprint: string;
16
+ }
17
+ export interface AspNetRuntimeBuildExecutionResult {
18
+ readonly exitCode: number;
19
+ readonly stdoutTail: string;
20
+ readonly stderrTail: string;
21
+ readonly stdoutLogPath: string;
22
+ readonly stderrLogPath: string;
23
+ }
24
+ export type AspNetRuntimeBuildExecutor = (plan: AspNetRuntimePrebuildPlan, signal?: AbortSignal) => Promise<AspNetRuntimeBuildExecutionResult>;
25
+ export declare function createAspNetRuntimePrebuildPlan(options: {
26
+ readonly command: string;
27
+ readonly args: readonly string[];
28
+ readonly cwd: string;
29
+ readonly worktreePath: string;
30
+ readonly environment?: Readonly<Record<string, string>>;
31
+ readonly inputCommitSha: string;
32
+ readonly runtimeContractFingerprint: string;
33
+ }): AspNetRuntimePrebuildPlan | null;
34
+ export declare function ensureAspNetRuntimePrebuilt(plan: AspNetRuntimePrebuildPlan, execute: AspNetRuntimeBuildExecutor, signal?: AbortSignal): Promise<AspNetRuntimePrebuildResult>;
35
+ export declare function withAspNetPrebuiltRunArguments(command: string, args: readonly string[]): readonly string[];
36
+ export declare function resolveWorktreeCommitSha(worktreePath: string, supplied?: string | null): Promise<string>;
@@ -0,0 +1,340 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import { spawn } from "node:child_process";
3
+ import fs from "node:fs/promises";
4
+ import path from "node:path";
5
+ const inFlightPrebuilds = new Map();
6
+ export function createAspNetRuntimePrebuildPlan(options) {
7
+ if (!isDotnetRun(options.command, options.args))
8
+ return null;
9
+ const projectArgument = readOptionValue(options.args, "--project");
10
+ const projectPath = path.resolve(options.cwd, projectArgument ?? ".");
11
+ const normalizedWorktree = path.resolve(options.worktreePath);
12
+ const relativeProject = path.relative(normalizedWorktree, projectPath);
13
+ if (relativeProject.startsWith("..") || path.isAbsolute(relativeProject)) {
14
+ throw new Error("host_runtime_build_invalid: the ASP.NET project must remain inside the managed worktree.");
15
+ }
16
+ const projectDirectory = projectArgument && path.extname(projectPath).toLowerCase() === ".csproj"
17
+ ? path.dirname(projectPath)
18
+ : projectPath;
19
+ const buildArgs = [
20
+ "build",
21
+ ...(projectArgument ? [projectArgument] : []),
22
+ "--nologo",
23
+ "--no-restore",
24
+ "--disable-build-servers",
25
+ "-p:UseSharedCompilation=false",
26
+ "-p:DefaultItemExcludesInProjectFolder=TestResults/**",
27
+ ...forwardBuildOptions(options.args),
28
+ ];
29
+ const fingerprint = `sha256:${createHash("sha256").update(JSON.stringify({
30
+ inputCommitSha: options.inputCommitSha,
31
+ runtimeContractFingerprint: options.runtimeContractFingerprint,
32
+ project: relativeProject.replaceAll("\\", "/"),
33
+ args: buildArgs,
34
+ environment: buildEnvironmentContract(options.environment ?? {}),
35
+ })).digest("hex")}`;
36
+ return {
37
+ fingerprint,
38
+ receiptPath: path.join(projectDirectory, "obj", ".forge-runtime-build.json"),
39
+ command: options.command,
40
+ args: buildArgs,
41
+ cwd: options.cwd,
42
+ environment: options.environment ?? {},
43
+ projectPath,
44
+ worktreePath: normalizedWorktree,
45
+ inputCommitSha: options.inputCommitSha,
46
+ };
47
+ }
48
+ export async function ensureAspNetRuntimePrebuilt(plan, execute, signal) {
49
+ const pending = inFlightPrebuilds.get(plan.receiptPath);
50
+ if (pending)
51
+ return await pending;
52
+ const started = ensureAspNetRuntimePrebuiltCore(plan, execute, signal);
53
+ inFlightPrebuilds.set(plan.receiptPath, started);
54
+ try {
55
+ return await started;
56
+ }
57
+ finally {
58
+ if (inFlightPrebuilds.get(plan.receiptPath) === started)
59
+ inFlightPrebuilds.delete(plan.receiptPath);
60
+ }
61
+ }
62
+ async function ensureAspNetRuntimePrebuiltCore(plan, execute, signal) {
63
+ const inputs = await aspNetBuildInputs(plan);
64
+ const fingerprint = `sha256:${createHash("sha256").update(JSON.stringify({
65
+ planFingerprint: plan.fingerprint,
66
+ ...inputs,
67
+ })).digest("hex")}`;
68
+ const receipt = await readReceipt(plan.receiptPath);
69
+ if (receipt?.fingerprint === fingerprint
70
+ && receipt.planFingerprint === plan.fingerprint
71
+ && receipt.inputCommitSha === plan.inputCommitSha
72
+ && await aspNetOutputsMatch(plan, receipt.outputs)) {
73
+ return { cacheHit: true, receiptPath: plan.receiptPath, fingerprint };
74
+ }
75
+ const result = await execute(plan, signal);
76
+ if (result.exitCode !== 0) {
77
+ const evidence = [
78
+ diagnostic("stderr", result.stderrTail),
79
+ diagnostic("stdout", result.stdoutTail),
80
+ `stdout log: ${result.stdoutLogPath}`,
81
+ `stderr log: ${result.stderrLogPath}`,
82
+ ].filter(Boolean).join("; ");
83
+ throw new Error(`host_runtime_build_failed: dotnet build exited with code ${result.exitCode}.${evidence ? ` ${evidence}` : ""}`);
84
+ }
85
+ await fs.mkdir(path.dirname(plan.receiptPath), { recursive: true });
86
+ const outputs = await collectAspNetBuildOutputs(plan);
87
+ if (outputs.length === 0) {
88
+ throw new Error("host_runtime_build_output_missing: dotnet build succeeded without producing a verifiable project output.");
89
+ }
90
+ const temporaryReceipt = `${plan.receiptPath}.${randomUUID()}.tmp`;
91
+ const nextReceipt = {
92
+ schemaVersion: 2,
93
+ fingerprint,
94
+ planFingerprint: plan.fingerprint,
95
+ inputCommitSha: plan.inputCommitSha,
96
+ projectPath: plan.projectPath,
97
+ ...inputs,
98
+ outputs,
99
+ completedAt: new Date().toISOString(),
100
+ };
101
+ await fs.writeFile(temporaryReceipt, `${JSON.stringify(nextReceipt)}\n`, { encoding: "utf8", mode: 0o600 });
102
+ try {
103
+ await fs.rename(temporaryReceipt, plan.receiptPath);
104
+ }
105
+ finally {
106
+ await fs.rm(temporaryReceipt, { force: true }).catch(() => undefined);
107
+ }
108
+ return { cacheHit: false, receiptPath: plan.receiptPath, fingerprint };
109
+ }
110
+ export function withAspNetPrebuiltRunArguments(command, args) {
111
+ if (!isDotnetRun(command, args)) {
112
+ throw new Error("node_runtime_adapter_invalid: aspnetcore adapter requires a dotnet run command.");
113
+ }
114
+ const applicationArguments = args.indexOf("--");
115
+ const insertionIndex = applicationArguments < 0 ? args.length : applicationArguments;
116
+ const beforeApplication = args.slice(0, insertionIndex);
117
+ return [
118
+ ...beforeApplication,
119
+ ...(!beforeApplication.includes("--no-build") ? ["--no-build"] : []),
120
+ ...(!beforeApplication.includes("--no-restore") ? ["--no-restore"] : []),
121
+ ...args.slice(insertionIndex),
122
+ ];
123
+ }
124
+ export async function resolveWorktreeCommitSha(worktreePath, supplied) {
125
+ if (supplied && /^[0-9a-f]{40,64}$/iu.test(supplied))
126
+ return supplied.toLowerCase();
127
+ return await new Promise((resolve, reject) => {
128
+ const child = spawn("git", ["rev-parse", "--verify", "HEAD^{commit}"], {
129
+ cwd: worktreePath,
130
+ windowsHide: true,
131
+ stdio: ["ignore", "pipe", "pipe"],
132
+ });
133
+ let stdout = "";
134
+ let stderr = "";
135
+ child.stdout.setEncoding("utf8");
136
+ child.stderr.setEncoding("utf8");
137
+ child.stdout.on("data", chunk => { stdout = `${stdout}${String(chunk)}`.slice(-256); });
138
+ child.stderr.on("data", chunk => { stderr = `${stderr}${String(chunk)}`.slice(-1_000); });
139
+ child.once("error", reject);
140
+ child.once("close", code => {
141
+ const commit = stdout.trim();
142
+ if (code === 0 && /^[0-9a-f]{40,64}$/iu.test(commit))
143
+ resolve(commit.toLowerCase());
144
+ else
145
+ reject(new Error(`host_runtime_build_commit_unavailable: git rev-parse failed (${code ?? "unknown"}): ${stderr.trim()}`));
146
+ });
147
+ });
148
+ }
149
+ function isDotnetRun(command, args) {
150
+ const executable = path.basename(command).toLowerCase();
151
+ return (executable === "dotnet" || executable === "dotnet.exe") && args[0]?.toLowerCase() === "run";
152
+ }
153
+ function readOptionValue(args, option) {
154
+ const index = args.findIndex(argument => argument === option || argument.startsWith(`${option}=`));
155
+ if (index < 0)
156
+ return null;
157
+ const argument = args[index];
158
+ return argument.startsWith(`${option}=`) ? argument.slice(option.length + 1) : args[index + 1] ?? null;
159
+ }
160
+ function forwardBuildOptions(args) {
161
+ const forwarded = [];
162
+ const optionsWithValues = new Set(["-c", "--configuration", "-f", "--framework", "-r", "--runtime", "--arch", "--os"]);
163
+ for (let index = 1; index < args.length; index += 1) {
164
+ const argument = args[index];
165
+ if (argument === "--")
166
+ break;
167
+ if (argument === "--project") {
168
+ index += 1;
169
+ continue;
170
+ }
171
+ if (argument.startsWith("--project="))
172
+ continue;
173
+ if (optionsWithValues.has(argument)) {
174
+ const value = args[index + 1];
175
+ if (value !== undefined) {
176
+ forwarded.push(argument, value);
177
+ index += 1;
178
+ }
179
+ continue;
180
+ }
181
+ if ([...optionsWithValues].some(option => argument.startsWith(`${option}=`))
182
+ || argument.startsWith("-p:")
183
+ || argument.startsWith("/p:")) {
184
+ forwarded.push(argument);
185
+ }
186
+ }
187
+ return forwarded;
188
+ }
189
+ async function readReceipt(receiptPath) {
190
+ try {
191
+ const parsed = JSON.parse(await fs.readFile(receiptPath, "utf8"));
192
+ return parsed.schemaVersion === 2
193
+ && typeof parsed.fingerprint === "string"
194
+ && typeof parsed.planFingerprint === "string"
195
+ && typeof parsed.inputCommitSha === "string"
196
+ && typeof parsed.sourceDigest === "string"
197
+ && typeof parsed.dependencyDigest === "string"
198
+ && typeof parsed.sdkVersion === "string"
199
+ && Array.isArray(parsed.outputs)
200
+ ? parsed
201
+ : null;
202
+ }
203
+ catch (error) {
204
+ if (error.code === "ENOENT" || error instanceof SyntaxError)
205
+ return null;
206
+ throw error;
207
+ }
208
+ }
209
+ async function aspNetBuildInputs(plan) {
210
+ const projectDirectory = path.extname(plan.projectPath).toLowerCase() === ".csproj"
211
+ ? path.dirname(plan.projectPath)
212
+ : plan.projectPath;
213
+ const sourceDigest = await directoryContentDigest(projectDirectory);
214
+ const dependencyReceipt = path.join(projectDirectory, "obj", ".forge-dependency-preparation.json");
215
+ const dependencyContent = await fs.readFile(dependencyReceipt).catch(() => null);
216
+ const globalJson = await findAncestorFile(projectDirectory, plan.worktreePath, "global.json");
217
+ const globalJsonContent = globalJson ? await fs.readFile(globalJson) : null;
218
+ return {
219
+ sourceDigest,
220
+ dependencyDigest: `sha256:${createHash("sha256")
221
+ .update(dependencyContent ?? Buffer.alloc(0))
222
+ .update(globalJsonContent ?? Buffer.alloc(0))
223
+ .digest("hex")}`,
224
+ sdkVersion: await dotnetSdkVersion(plan.cwd),
225
+ };
226
+ }
227
+ async function directoryContentDigest(directory) {
228
+ const files = [];
229
+ await collectInputFiles(directory, directory, files);
230
+ const hash = createHash("sha256");
231
+ for (const file of files.sort((left, right) => left.localeCompare(right))) {
232
+ hash.update(path.relative(directory, file).replaceAll("\\", "/"));
233
+ hash.update("\0");
234
+ hash.update(await fs.readFile(file));
235
+ hash.update("\0");
236
+ }
237
+ return `sha256:${hash.digest("hex")}`;
238
+ }
239
+ async function collectInputFiles(root, directory, files) {
240
+ const entries = await fs.readdir(directory, { withFileTypes: true }).catch(() => []);
241
+ for (const entry of entries) {
242
+ const absolute = path.join(directory, entry.name);
243
+ if (entry.isDirectory()) {
244
+ if (!["bin", "obj", "node_modules", ".git", "TestResults"].includes(entry.name)) {
245
+ await collectInputFiles(root, absolute, files);
246
+ }
247
+ }
248
+ else if (entry.isFile()) {
249
+ files.push(absolute);
250
+ }
251
+ }
252
+ }
253
+ async function collectAspNetBuildOutputs(plan) {
254
+ const projectDirectory = path.extname(plan.projectPath).toLowerCase() === ".csproj"
255
+ ? path.dirname(plan.projectPath)
256
+ : plan.projectPath;
257
+ const outputRoot = path.join(projectDirectory, "bin");
258
+ const outputs = [];
259
+ await collectOutputFiles(outputRoot, projectDirectory, outputs);
260
+ return outputs.sort((left, right) => left.path.localeCompare(right.path));
261
+ }
262
+ async function collectOutputFiles(directory, projectDirectory, outputs) {
263
+ const entries = await fs.readdir(directory, { withFileTypes: true }).catch(() => []);
264
+ for (const entry of entries) {
265
+ const absolute = path.join(directory, entry.name);
266
+ if (entry.isDirectory())
267
+ await collectOutputFiles(absolute, projectDirectory, outputs);
268
+ else if (entry.isFile()) {
269
+ const stat = await fs.stat(absolute);
270
+ outputs.push({
271
+ path: path.relative(projectDirectory, absolute).replaceAll("\\", "/"),
272
+ size: stat.size,
273
+ digest: createHash("sha256").update(await fs.readFile(absolute)).digest("hex"),
274
+ });
275
+ }
276
+ }
277
+ }
278
+ async function aspNetOutputsMatch(plan, outputs) {
279
+ if (outputs.length === 0)
280
+ return false;
281
+ const projectDirectory = path.extname(plan.projectPath).toLowerCase() === ".csproj"
282
+ ? path.dirname(plan.projectPath)
283
+ : plan.projectPath;
284
+ for (const output of outputs) {
285
+ const absolute = path.resolve(projectDirectory, output.path);
286
+ const relative = path.relative(projectDirectory, absolute);
287
+ if (relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative))
288
+ return false;
289
+ const stat = await fs.stat(absolute).catch(() => null);
290
+ if (!stat?.isFile() || stat.size !== output.size)
291
+ return false;
292
+ const digest = createHash("sha256").update(await fs.readFile(absolute)).digest("hex");
293
+ if (digest !== output.digest)
294
+ return false;
295
+ }
296
+ return true;
297
+ }
298
+ async function findAncestorFile(directory, root, name) {
299
+ let current = path.resolve(directory);
300
+ const resolvedRoot = path.resolve(root);
301
+ while (true) {
302
+ const candidate = path.join(current, name);
303
+ if (await fs.stat(candidate).then(stat => stat.isFile()).catch(() => false))
304
+ return candidate;
305
+ if (current === resolvedRoot)
306
+ return null;
307
+ const parent = path.dirname(current);
308
+ if (parent === current || path.relative(resolvedRoot, parent).startsWith(".."))
309
+ return null;
310
+ current = parent;
311
+ }
312
+ }
313
+ async function dotnetSdkVersion(cwd) {
314
+ return await new Promise(resolve => {
315
+ const child = spawn("dotnet", ["--version"], { cwd, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
316
+ let output = "";
317
+ child.stdout.setEncoding("utf8");
318
+ child.stderr.setEncoding("utf8");
319
+ child.stdout.on("data", chunk => { output += String(chunk); });
320
+ child.stderr.on("data", chunk => { output += String(chunk); });
321
+ child.once("error", () => resolve("unavailable"));
322
+ child.once("close", code => resolve(code === 0 ? output.trim().slice(0, 256) : `unavailable:${code ?? "unknown"}`));
323
+ });
324
+ }
325
+ function buildEnvironmentContract(environment) {
326
+ return {
327
+ DOTNET_ROOT: environment.DOTNET_ROOT ?? process.env.DOTNET_ROOT ?? null,
328
+ DOTNET_ROLL_FORWARD: environment.DOTNET_ROLL_FORWARD ?? process.env.DOTNET_ROLL_FORWARD ?? null,
329
+ DOTNET_MULTILEVEL_LOOKUP: environment.DOTNET_MULTILEVEL_LOOKUP ?? process.env.DOTNET_MULTILEVEL_LOOKUP ?? null,
330
+ MSBuildSDKsPath: environment.MSBuildSDKsPath ?? process.env.MSBuildSDKsPath ?? null,
331
+ NUGET_PACKAGES: environment.NUGET_PACKAGES ?? process.env.NUGET_PACKAGES ?? null,
332
+ FORGE_BUILD_CACHE_EPOCH: createHash("sha256")
333
+ .update(environment.FORGE_BUILD_CACHE_EPOCH ?? process.env.FORGE_BUILD_CACHE_EPOCH ?? "default")
334
+ .digest("hex"),
335
+ };
336
+ }
337
+ function diagnostic(label, value) {
338
+ const normalized = value.replaceAll(/[\r\n\t]+/gu, " ").trim();
339
+ return normalized ? `${label} tail: ${normalized.slice(-2_000)}` : "";
340
+ }
@@ -20,7 +20,7 @@ export class AttemptNodeRuntimeLauncher {
20
20
  const childName = projectChildName(request.port.name);
21
21
  const stdoutPath = path.join(this.logDirectory, `${childName}.stdout.log`);
22
22
  const stderrPath = path.join(this.logDirectory, `${childName}.stderr.log`);
23
- const diagnostics = { exited: false, stdoutTail: "", stderrTail: "" };
23
+ const diagnostics = { exited: false, stdoutTail: "", stderrTail: "", outputRevision: 0 };
24
24
  let child;
25
25
  try {
26
26
  child = await this.supervisor.startChildReady(childName, {
@@ -32,10 +32,12 @@ export class AttemptNodeRuntimeLauncher {
32
32
  }, {
33
33
  onStdoutLine: async (line) => {
34
34
  diagnostics.stdoutTail = appendTail(diagnostics.stdoutTail, line);
35
+ diagnostics.outputRevision += 1;
35
36
  await appendLine(stdoutPath, line);
36
37
  },
37
38
  onStderrLine: async (line) => {
38
39
  diagnostics.stderrTail = appendTail(diagnostics.stderrTail, line);
40
+ diagnostics.outputRevision += 1;
39
41
  await appendLine(stderrPath, line);
40
42
  },
41
43
  }, signal, { maxStartAttempts: 3, retryDelayMs: 250 });
@@ -49,9 +51,11 @@ export class AttemptNodeRuntimeLauncher {
49
51
  diagnostics.exitCode = result.exitCode;
50
52
  diagnostics.stdoutTail = appendTail(diagnostics.stdoutTail, result.stdout);
51
53
  diagnostics.stderrTail = appendTail(diagnostics.stderrTail, result.stderr);
54
+ diagnostics.outputRevision += 1;
52
55
  }, error => {
53
56
  diagnostics.exited = true;
54
57
  diagnostics.stderrTail = appendTail(diagnostics.stderrTail, error instanceof Error ? error.message : String(error));
58
+ diagnostics.outputRevision += 1;
55
59
  });
56
60
  return child;
57
61
  }
@@ -19,6 +19,10 @@ export interface AttemptProcessChildLedgerEntry {
19
19
  readonly settledAt?: string;
20
20
  readonly exitCode?: number;
21
21
  readonly failure?: string;
22
+ readonly peakMemoryBytes?: number;
23
+ readonly memorySampleCount?: number;
24
+ readonly memorySource?: string;
25
+ readonly memoryObservedAt?: string;
22
26
  }
23
27
  export interface AttemptProcessSupervisorLedger {
24
28
  readonly schemaVersion: 1;
@@ -79,8 +83,15 @@ export declare class AttemptProcessSupervisor {
79
83
  stopAll(fencingToken: string, reason?: unknown): Promise<void>;
80
84
  private performStopAll;
81
85
  snapshot(): AttemptProcessSupervisorLedger;
86
+ providerMemoryTelemetry(): AttemptProcessTreeMemoryTelemetry | null;
82
87
  private observeChildReady;
83
88
  private settleChild;
84
89
  private assertFence;
85
90
  private persist;
86
91
  }
92
+ export interface AttemptProcessTreeMemoryTelemetry {
93
+ readonly peakMemoryBytes: number;
94
+ readonly sampleCount: number;
95
+ readonly source: string;
96
+ readonly observedAt: string;
97
+ }