@loucompanion/forge-bridge 0.1.3-dev.fbe15023cdfe → 0.1.4-dev.03584818ede5

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 (275) hide show
  1. package/README.md +146 -5
  2. package/dist/bridge/bridge-chat-provider-command.d.ts +16 -0
  3. package/dist/bridge/bridge-chat-provider-command.js +137 -0
  4. package/dist/bridge/bridge-chat.types.d.ts +121 -0
  5. package/dist/bridge/bridge-chat.types.js +416 -0
  6. package/dist/bridge/bridge.base.d.ts +13 -1
  7. package/dist/bridge/bridge.service.d.ts +155 -4
  8. package/dist/bridge/bridge.service.js +1805 -155
  9. package/dist/bridge/bridge.types.d.ts +68 -1
  10. package/dist/bridge/dispatch-result-outbox.d.ts +35 -0
  11. package/dist/bridge/dispatch-result-outbox.js +87 -0
  12. package/dist/bridge/dispatch-result-worker.d.ts +2 -0
  13. package/dist/bridge/dispatch-result-worker.js +122 -0
  14. package/dist/bridge/internals.d.ts +2 -0
  15. package/dist/bridge/internals.js +1 -0
  16. package/dist/bridge/local-yield-flag.d.ts +6 -0
  17. package/dist/bridge/local-yield-flag.js +34 -0
  18. package/dist/bridge/readiness-result-outbox.d.ts +21 -0
  19. package/dist/bridge/readiness-result-outbox.js +54 -0
  20. package/dist/bridge/readiness-result-worker.d.ts +2 -0
  21. package/dist/bridge/readiness-result-worker.js +121 -0
  22. package/dist/bridge/runner-host-capabilities.d.ts +20 -0
  23. package/dist/bridge/runner-host-capabilities.js +110 -0
  24. package/dist/bridge/test-postgres-capability.d.ts +14 -0
  25. package/dist/bridge/test-postgres-capability.js +120 -0
  26. package/dist/cleanup/cleanup.service.d.ts +10 -0
  27. package/dist/cleanup/cleanup.service.js +128 -104
  28. package/dist/cleanup/cleanup.types.d.ts +4 -0
  29. package/dist/cleanup/forge-owned-paths.d.ts +9 -0
  30. package/dist/cleanup/forge-owned-paths.js +53 -0
  31. package/dist/cleanup/host-runtime-ledger.d.ts +5 -0
  32. package/dist/cleanup/host-runtime-ledger.js +109 -0
  33. package/dist/cleanup/managed-branch-sweep.d.ts +27 -0
  34. package/dist/cleanup/managed-branch-sweep.js +111 -0
  35. package/dist/cli/cli.help.js +22 -4
  36. package/dist/cli/cli.service.d.ts +30 -6
  37. package/dist/cli/cli.service.js +216 -31
  38. package/dist/cli/local-config.d.ts +5 -0
  39. package/dist/cli/local-config.js +30 -4
  40. package/dist/runtime/aspnet-runtime-prebuild.d.ts +36 -0
  41. package/dist/runtime/aspnet-runtime-prebuild.js +429 -0
  42. package/dist/runtime/attempt-database-broker.d.ts +89 -0
  43. package/dist/runtime/attempt-database-broker.js +390 -0
  44. package/dist/runtime/attempt-node-runtime-launcher.d.ts +2 -1
  45. package/dist/runtime/attempt-node-runtime-launcher.js +57 -18
  46. package/dist/runtime/attempt-path-operation.d.ts +21 -0
  47. package/dist/runtime/attempt-path-operation.js +94 -0
  48. package/dist/runtime/attempt-private-directory.d.ts +6 -0
  49. package/dist/runtime/attempt-private-directory.js +54 -0
  50. package/dist/runtime/attempt-process-supervisor.d.ts +35 -0
  51. package/dist/runtime/attempt-process-supervisor.js +144 -23
  52. package/dist/runtime/canonical-source-file.d.ts +3 -0
  53. package/dist/runtime/canonical-source-file.js +54 -0
  54. package/dist/runtime/docker-engine-probe.d.ts +90 -0
  55. package/dist/runtime/docker-engine-probe.js +333 -0
  56. package/dist/runtime/generated-output-reset.d.ts +10 -0
  57. package/dist/runtime/generated-output-reset.js +62 -8
  58. package/dist/runtime/host-database-capability-boundary.d.ts +16 -0
  59. package/dist/runtime/host-database-capability-boundary.js +63 -0
  60. package/dist/runtime/host-diagnostics.d.ts +11 -0
  61. package/dist/runtime/host-diagnostics.js +192 -0
  62. package/dist/runtime/host-execution-root.d.ts +4 -0
  63. package/dist/runtime/host-execution-root.js +39 -0
  64. package/dist/runtime/host-facts.d.ts +36 -0
  65. package/dist/runtime/host-facts.js +95 -0
  66. package/dist/runtime/host-listener-inspector.d.ts +8 -1
  67. package/dist/runtime/host-listener-inspector.js +121 -60
  68. package/dist/runtime/host-load-admission.d.ts +67 -0
  69. package/dist/runtime/host-load-admission.js +99 -0
  70. package/dist/runtime/host-load-child-environment.d.ts +27 -0
  71. package/dist/runtime/host-load-child-environment.js +76 -0
  72. package/dist/runtime/host-load-forge-cgroups.d.ts +49 -0
  73. package/dist/runtime/host-load-forge-cgroups.js +140 -0
  74. package/dist/runtime/host-load-governor-config.d.ts +52 -0
  75. package/dist/runtime/host-load-governor-config.js +83 -0
  76. package/dist/runtime/host-load-governor.d.ts +184 -0
  77. package/dist/runtime/host-load-governor.js +632 -0
  78. package/dist/runtime/host-load-ledger-lock.d.ts +69 -0
  79. package/dist/runtime/host-load-ledger-lock.js +185 -0
  80. package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
  81. package/dist/runtime/host-load-runtime-settings.js +78 -0
  82. package/dist/runtime/host-load-sampler.d.ts +149 -0
  83. package/dist/runtime/host-load-sampler.js +291 -0
  84. package/dist/runtime/host-load-step-class.d.ts +70 -0
  85. package/dist/runtime/host-load-step-class.js +161 -0
  86. package/dist/runtime/host-load-step-reaper.d.ts +27 -0
  87. package/dist/runtime/host-load-step-reaper.js +95 -0
  88. package/dist/runtime/host-load-step-run.d.ts +73 -0
  89. package/dist/runtime/host-load-step-run.js +102 -0
  90. package/dist/runtime/host-load-test-slot.d.ts +43 -0
  91. package/dist/runtime/host-load-test-slot.js +81 -0
  92. package/dist/runtime/host-load-token-ledger.d.ts +200 -0
  93. package/dist/runtime/host-load-token-ledger.js +453 -0
  94. package/dist/runtime/host-load-windows-cpu.d.ts +34 -0
  95. package/dist/runtime/host-load-windows-cpu.js +85 -0
  96. package/dist/runtime/host-pressure-stall.d.ts +61 -0
  97. package/dist/runtime/host-pressure-stall.js +168 -0
  98. package/dist/runtime/host-runtime-contract.d.ts +16 -1
  99. package/dist/runtime/host-runtime-contract.js +174 -56
  100. package/dist/runtime/host-runtime-lease.d.ts +163 -9
  101. package/dist/runtime/host-runtime-lease.js +1464 -121
  102. package/dist/runtime/host-runtime-promotion.d.ts +35 -3
  103. package/dist/runtime/host-runtime-promotion.js +107 -12
  104. package/dist/runtime/host-runtime-requirements-check.d.ts +17 -0
  105. package/dist/runtime/host-runtime-requirements-check.js +29 -0
  106. package/dist/runtime/host-runtime-requirements.d.ts +36 -0
  107. package/dist/runtime/host-runtime-requirements.js +117 -0
  108. package/dist/runtime/managed-command-teardown.d.ts +62 -0
  109. package/dist/runtime/managed-command-teardown.js +607 -0
  110. package/dist/runtime/memory-capacity.d.ts +70 -0
  111. package/dist/runtime/memory-capacity.js +123 -0
  112. package/dist/runtime/native-test-postgres-fallback.d.ts +25 -0
  113. package/dist/runtime/native-test-postgres-fallback.js +91 -0
  114. package/dist/runtime/node-host-runtime-adapter.d.ts +3 -0
  115. package/dist/runtime/node-host-runtime-adapter.js +29 -11
  116. package/dist/runtime/pinned-directory-process.d.ts +9 -0
  117. package/dist/runtime/pinned-directory-process.js +61 -0
  118. package/dist/runtime/pinned-source-directory.d.ts +17 -0
  119. package/dist/runtime/pinned-source-directory.js +94 -0
  120. package/dist/runtime/playwright-browser-cache.d.ts +39 -0
  121. package/dist/runtime/playwright-browser-cache.js +227 -0
  122. package/dist/runtime/playwright-host-runtime-adapter.d.ts +19 -0
  123. package/dist/runtime/playwright-host-runtime-adapter.js +113 -59
  124. package/dist/runtime/prepared-runtime-identity.d.ts +6 -0
  125. package/dist/runtime/prepared-runtime-identity.js +218 -0
  126. package/dist/runtime/prepared-runtime-readiness.d.ts +30 -0
  127. package/dist/runtime/prepared-runtime-readiness.js +285 -0
  128. package/dist/runtime/runner-settings.d.ts +100 -0
  129. package/dist/runtime/runner-settings.js +214 -0
  130. package/dist/runtime/runtime-isolation-lease.js +4 -1
  131. package/dist/runtime/workspace-resource-estimator.d.ts +17 -0
  132. package/dist/runtime/workspace-resource-estimator.js +174 -0
  133. package/dist/sandbox/host-capacity-broker.d.ts +113 -0
  134. package/dist/sandbox/host-capacity-broker.js +334 -0
  135. package/dist/sandbox/preflight.d.ts +31 -1
  136. package/dist/sandbox/preflight.js +237 -51
  137. package/dist/sandbox/repository-lock.d.ts +2 -1
  138. package/dist/sandbox/repository-lock.js +19 -9
  139. package/dist/sandbox/runtime.d.ts +16 -2
  140. package/dist/sandbox/runtime.js +182 -109
  141. package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
  142. package/dist/sandbox/sandbox-cgroups.js +42 -0
  143. package/dist/sandbox/sandbox.types.d.ts +7 -0
  144. package/dist/sandbox/workspace.service.js +2 -0
  145. package/dist/session/architect-waits.d.ts +2 -0
  146. package/dist/session/architect-waits.js +138 -0
  147. package/dist/session/managed-attempt-tools.d.ts +44 -1
  148. package/dist/session/managed-attempt-tools.js +816 -46
  149. package/dist/session/managed-build-coordinator.d.ts +5 -0
  150. package/dist/session/managed-build-coordinator.js +109 -0
  151. package/dist/session/managed-build-receipt.d.ts +21 -0
  152. package/dist/session/managed-build-receipt.js +337 -0
  153. package/dist/session/managed-command-host-load.d.ts +47 -0
  154. package/dist/session/managed-command-host-load.js +83 -0
  155. package/dist/session/managed-verification-timeouts.d.ts +8 -0
  156. package/dist/session/managed-verification-timeouts.js +17 -0
  157. package/dist/session/mcp-argument-coercion.d.ts +23 -0
  158. package/dist/session/mcp-argument-coercion.js +121 -0
  159. package/dist/session/phase-completion.artifacts.d.ts +24 -0
  160. package/dist/session/phase-completion.artifacts.js +319 -44
  161. package/dist/session/phase-completion.command-validation.d.ts +7 -0
  162. package/dist/session/phase-completion.command-validation.js +77 -0
  163. package/dist/session/phase-completion.d.ts +15 -1
  164. package/dist/session/phase-completion.helpers.d.ts +5 -0
  165. package/dist/session/phase-completion.helpers.js +43 -19
  166. package/dist/session/phase-completion.js +18 -3
  167. package/dist/session/phase-completion.prompts.js +22 -18
  168. package/dist/session/phase-completion.runtime.d.ts +6 -2
  169. package/dist/session/phase-completion.runtime.js +191 -47
  170. package/dist/session/phase-completion.schema-parts.d.ts +19 -0
  171. package/dist/session/phase-completion.schema-parts.js +188 -0
  172. package/dist/session/phase-completion.schemas.d.ts +3 -3
  173. package/dist/session/phase-completion.schemas.js +250 -226
  174. package/dist/session/phase-completion.validation.d.ts +3 -3
  175. package/dist/session/phase-completion.validation.js +135 -89
  176. package/dist/session/pre-publication-host-load.d.ts +27 -0
  177. package/dist/session/pre-publication-host-load.js +111 -0
  178. package/dist/session/pre-publication-pass.d.ts +61 -0
  179. package/dist/session/pre-publication-pass.js +148 -0
  180. package/dist/session/pre-publication-process.d.ts +23 -0
  181. package/dist/session/pre-publication-process.js +109 -0
  182. package/dist/session/pre-publication-skipped-tests.d.ts +21 -0
  183. package/dist/session/pre-publication-skipped-tests.js +109 -0
  184. package/dist/session/pre-publication-step-execution.d.ts +11 -0
  185. package/dist/session/pre-publication-step-execution.js +49 -0
  186. package/dist/session/pre-publication-validation.d.ts +87 -0
  187. package/dist/session/pre-publication-validation.js +668 -0
  188. package/dist/session/provider-cli/bridge-cli.conformance-fixtures.d.ts +72 -0
  189. package/dist/session/provider-cli/bridge-cli.conformance-fixtures.js +462 -0
  190. package/dist/session/provider-cli/ci-repair-readiness.d.ts +8 -0
  191. package/dist/session/provider-cli/ci-repair-readiness.js +23 -3
  192. package/dist/session/provider-cli/claude-background-work.d.ts +66 -0
  193. package/dist/session/provider-cli/claude-background-work.js +180 -0
  194. package/dist/session/provider-cli/pre-publication-repair.d.ts +40 -0
  195. package/dist/session/provider-cli/pre-publication-repair.js +243 -0
  196. package/dist/session/provider-cli/provider-cli.artifacts.d.ts +6 -3
  197. package/dist/session/provider-cli/provider-cli.artifacts.js +62 -22
  198. package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
  199. package/dist/session/provider-cli/provider-cli.child-environment.js +73 -0
  200. package/dist/session/provider-cli/provider-cli.codex-models-cache.d.ts +3 -0
  201. package/dist/session/provider-cli/provider-cli.codex-models-cache.js +37 -0
  202. package/dist/session/provider-cli/provider-cli.deterministic.js +34 -2
  203. package/dist/session/provider-cli/provider-cli.merge-integrity.d.ts +89 -0
  204. package/dist/session/provider-cli/provider-cli.merge-integrity.js +310 -0
  205. package/dist/session/provider-cli/provider-cli.process.d.ts +92 -14
  206. package/dist/session/provider-cli/provider-cli.process.js +583 -210
  207. package/dist/session/provider-cli/provider-cli.prompts.d.ts +12 -0
  208. package/dist/session/provider-cli/provider-cli.prompts.js +200 -25
  209. package/dist/session/provider-cli/provider-cli.repair-epoch.d.ts +6 -0
  210. package/dist/session/provider-cli/provider-cli.repair-epoch.js +11 -0
  211. package/dist/session/provider-cli/provider-cli.runtime.d.ts +14 -1
  212. package/dist/session/provider-cli/provider-cli.runtime.js +61 -3
  213. package/dist/session/provider-cli/provider-cli.service.d.ts +33 -5
  214. package/dist/session/provider-cli/provider-cli.service.js +819 -84
  215. package/dist/session/provider-cli/provider-cli.types.d.ts +32 -0
  216. package/dist/session/provider-cli/provider-cli.windows-environment.d.ts +10 -0
  217. package/dist/session/provider-cli/provider-cli.windows-environment.js +151 -0
  218. package/dist/session/provider-cli/provider-in-flight-work.d.ts +10 -0
  219. package/dist/session/provider-cli/provider-in-flight-work.js +92 -0
  220. package/dist/session/provider-cli/provider-output-pipes.d.ts +30 -0
  221. package/dist/session/provider-cli/provider-output-pipes.js +67 -0
  222. package/dist/session/provider-cli/provider-output-state.d.ts +18 -0
  223. package/dist/session/provider-cli/provider-output-state.js +376 -17
  224. package/dist/session/provider-cli/provider-usage.d.ts +9 -1
  225. package/dist/session/provider-cli/provider-usage.js +28 -5
  226. package/dist/session/publishable-worktree-changes.d.ts +54 -0
  227. package/dist/session/publishable-worktree-changes.js +193 -0
  228. package/dist/session/sandbox-host-capacity-gate.d.ts +30 -0
  229. package/dist/session/sandbox-host-capacity-gate.js +238 -0
  230. package/dist/session/session-redaction.js +434 -9
  231. package/dist/session/session.base.d.ts +9 -1
  232. package/dist/session/session.service.d.ts +9 -2
  233. package/dist/session/session.service.js +264 -27
  234. package/dist/session/session.types.d.ts +150 -2
  235. package/dist/session/session.types.js +599 -7
  236. package/dist/session/unreferenced-exports.d.ts +12 -0
  237. package/dist/session/unreferenced-exports.js +59 -0
  238. package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
  239. package/dist/session/workspace-runtime-fingerprint.js +138 -0
  240. package/dist/shared/format/format-bytes.d.ts +1 -0
  241. package/dist/shared/format/format-bytes.js +9 -0
  242. package/dist/shared/git/git.js +2 -1
  243. package/dist/shared/path/path.d.ts +1 -0
  244. package/dist/shared/path/path.js +18 -0
  245. package/dist/shared/process/dotnet-toolchain.d.ts +17 -0
  246. package/dist/shared/process/dotnet-toolchain.js +307 -0
  247. package/dist/shared/process/host-process-recovery.js +3 -2
  248. package/dist/shared/process/host-process-supervisor.d.ts +3 -0
  249. package/dist/shared/process/host-process-supervisor.js +61 -12
  250. package/dist/shared/process/process-tree.d.ts +52 -0
  251. package/dist/shared/process/process-tree.js +144 -0
  252. package/dist/shared/process/process.d.ts +69 -0
  253. package/dist/shared/process/process.js +223 -16
  254. package/dist/shared/process/windows-app-execution-alias.d.ts +19 -0
  255. package/dist/shared/process/windows-app-execution-alias.js +45 -0
  256. package/dist/shared/process/windows-launch.d.ts +18 -0
  257. package/dist/shared/process/windows-launch.js +68 -0
  258. package/dist/shared/provider-capabilities.d.ts +3 -0
  259. package/dist/shared/provider-capabilities.js +84 -0
  260. package/dist/shared/redaction/redaction.d.ts +1 -0
  261. package/dist/shared/redaction/redaction.js +39 -3
  262. package/dist/shared/time/monotonic.d.ts +1 -0
  263. package/dist/shared/time/monotonic.js +8 -0
  264. package/dist/version.d.ts +1 -1
  265. package/dist/version.js +1 -1
  266. package/dist/worktree/dependency-warmer.d.ts +66 -9
  267. package/dist/worktree/dependency-warmer.js +609 -24
  268. package/dist/worktree/index.d.ts +1 -1
  269. package/dist/worktree/story-qa-reference-hydrator.d.ts +4 -0
  270. package/dist/worktree/story-qa-reference-hydrator.js +253 -0
  271. package/dist/worktree/worktree.service.d.ts +17 -1
  272. package/dist/worktree/worktree.service.js +293 -21
  273. package/dist/worktree/worktree.types.d.ts +14 -1
  274. package/package.json +8 -6
  275. package/runtime/windows-job-supervisor.ps1 +380 -14
@@ -1,5 +1,5 @@
1
1
  export { GitWorktreeService } from "./worktree.service.js";
2
2
  export { managedRepoRoot, resolveManagedWorktreeRoot, resolveManagedWorktreeRootForRepo, } from "./worktree-paths.js";
3
3
  export { detectWarmPaths, warmWorktreeDependencies, createDirectoryLink, lockfilesDiffer, requiresInstallFallback } from "./dependency-warmer.js";
4
- export type { WarmDependenciesResult, WarmPathEntry, InstallFallbackResult } from "./dependency-warmer.js";
4
+ export type { WarmDependenciesResult, WarmPathEntry, InstallFallbackResult, DependencyPreparationPolicy, DependencyPreparationReceiptResult, } from "./dependency-warmer.js";
5
5
  export type { WorktreeService } from "./worktree.base.js";
@@ -0,0 +1,4 @@
1
+ import { GitClient } from "../shared/git/git.js";
2
+ import type { WorktreePrepareRequest } from "./worktree.types.js";
3
+ export declare function storyQaReferenceRoot(storyId: string, runId: string): string;
4
+ export declare function materializeStoryQaReferences(worktreePath: string, request: WorktreePrepareRequest, git?: GitClient): Promise<void>;
@@ -0,0 +1,253 @@
1
+ import { createHash, randomUUID } from "node:crypto";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { GitClient } from "../shared/git/git.js";
5
+ import { isSubPath } from "../shared/path/path.js";
6
+ import { stableStringify } from "../session/phase-completion.helpers.js";
7
+ const MAX_FILES = 128;
8
+ const MAX_FILE_BYTES = 2 * 1024 * 1024;
9
+ const MAX_TOTAL_BYTES = 16 * 1024 * 1024;
10
+ const hash = (body) => createHash("sha256").update(body).digest("hex");
11
+ const fail = (reason) => new Error(`qa_handoff_invalid: ${reason}`);
12
+ const segment = (value) => {
13
+ if (!/^[a-zA-Z0-9_-]{1,128}$/.test(value))
14
+ throw fail("invalid reference identity");
15
+ return value;
16
+ };
17
+ export function storyQaReferenceRoot(storyId, runId) {
18
+ return `.forge/work-items/${segment(storyId)}/qa-inputs/${segment(runId)}`;
19
+ }
20
+ // All inputs are reference-only. No phase verdict is created, promoted or replaced here.
21
+ export async function materializeStoryQaReferences(worktreePath, request, git = new GitClient()) {
22
+ const story = request.storyQa;
23
+ const bundle = story?.references;
24
+ if (!bundle)
25
+ return;
26
+ if (!request.referenceRunId || bundle.schemaVersion !== 1 || bundle.storyWorkItemId !== request.workItem.id
27
+ || bundle.cycleRunId !== request.referenceCycleRunId || bundle.storyWorkItemId !== request.ownerId
28
+ || bundle.storyWorkItemId !== story.storyWorkItemId || bundle.repoId !== request.repo.id
29
+ || bundle.projectId !== request.workItem.projectId || bundle.effectiveAmendmentHash !== request.effectivePlanAmendmentHash
30
+ || bundle.planId !== story.planId || bundle.planContentHash !== story.planContentHash || story.worktreeKey !== request.worktreeKey
31
+ || !/^[a-f0-9]{64}$/i.test(bundle.effectiveAmendmentHash))
32
+ throw fail("reference owner/plan identity mismatch");
33
+ const relativeRoot = storyQaReferenceRoot(bundle.storyWorkItemId, request.referenceRunId);
34
+ const root = path.resolve(worktreePath, relativeRoot);
35
+ await safePath(worktreePath, root);
36
+ const files = [...bundle.files];
37
+ validateFiles(files);
38
+ const checkpoint = await captureCheckpoint(worktreePath, bundle, git);
39
+ if (checkpoint)
40
+ files.push(checkpoint);
41
+ validateFiles(files);
42
+ const manifest = JSON.stringify({ ...bundle, files: files.map(({ body: _body, ...file }) => file),
43
+ notice: "Reference input only. Historical PASS and repair checkpoint status are unchanged. Fresh QA is still required." }, null, 2) + "\n";
44
+ // Prepare a sibling directory and publish by rename; never leave a partially written manifest.
45
+ await fs.mkdir(path.dirname(root), { recursive: true });
46
+ await safePath(worktreePath, path.dirname(root));
47
+ const existing = await fs.stat(root).catch(error => { if (error.code === "ENOENT")
48
+ return null; throw error; });
49
+ if (existing) {
50
+ // A resumed cycle may reuse only its exact immutable reference snapshot.
51
+ await safePath(worktreePath, path.join(root, "manifest.json"));
52
+ const previous = JSON.parse(await fs.readFile(path.join(root, "manifest.json"), "utf8"));
53
+ const current = JSON.parse(manifest);
54
+ delete previous.capturedAt;
55
+ delete current.capturedAt;
56
+ if (stableStringify(previous) !== stableStringify(current))
57
+ throw fail("reference snapshot changed for this run");
58
+ for (const file of files) {
59
+ await safePath(worktreePath, path.join(root, file.path));
60
+ if (hash(await fs.readFile(path.join(root, file.path))) !== file.contentHash.toLowerCase())
61
+ throw fail("existing reference hash mismatch");
62
+ }
63
+ return;
64
+ }
65
+ const staging = `${root}.tmp-${randomUUID()}`;
66
+ await fs.mkdir(staging);
67
+ try {
68
+ for (const file of files) {
69
+ const target = path.join(staging, file.path);
70
+ await safePath(worktreePath, target);
71
+ await fs.mkdir(path.dirname(target), { recursive: true });
72
+ await fs.writeFile(target, file.body, { encoding: "utf8", flag: "wx", mode: 0o600 });
73
+ }
74
+ await fs.writeFile(path.join(staging, "manifest.json"), manifest, { flag: "wx", mode: 0o600 });
75
+ await safePath(worktreePath, root);
76
+ await fs.rename(staging, root);
77
+ }
78
+ finally {
79
+ await fs.rm(staging, { recursive: true, force: true });
80
+ }
81
+ }
82
+ function validateFiles(files) {
83
+ if (files.length === 0 || files.length > MAX_FILES)
84
+ throw fail("reference file count exceeds bounds");
85
+ const paths = new Set();
86
+ let total = 0;
87
+ for (const file of files) {
88
+ if (!/^(original|effective|children|amendments|checkpoint)\/[a-zA-Z0-9_./-]+$/.test(file.path)
89
+ || file.path.split("/").some(part => !part || part === "." || part === ".." || part.endsWith(".")))
90
+ throw fail("unsafe reference path");
91
+ const key = file.path.toLowerCase();
92
+ if (paths.has(key))
93
+ throw fail("duplicate reference path");
94
+ paths.add(key);
95
+ const bytes = Buffer.byteLength(file.body, "utf8");
96
+ total += bytes;
97
+ if (bytes > MAX_FILE_BYTES || total > MAX_TOTAL_BYTES)
98
+ throw fail("reference size exceeds bounds");
99
+ if (!/^[a-f0-9]{64}$/i.test(file.contentHash) || hash(file.body) !== file.contentHash.toLowerCase())
100
+ throw fail("reference content hash mismatch");
101
+ if (!file.provenance.trim())
102
+ throw fail("reference provenance missing");
103
+ }
104
+ }
105
+ async function captureCheckpoint(worktreePath, bundle, git) {
106
+ const storyId = bundle.storyWorkItemId;
107
+ const originalPath = path.join(worktreePath, ".forge", "work-items", segment(storyId), "artifacts", "comment-repair.handoff.json");
108
+ await safePath(worktreePath, originalPath);
109
+ let sourcePath = originalPath;
110
+ let archivedManifestPath;
111
+ let stat = await fs.stat(sourcePath).catch(error => { if (error.code === "ENOENT")
112
+ return null; throw error; });
113
+ if (!stat) {
114
+ // A previous QA already cleared outputs. A preserved input is not a new output:
115
+ // validate its immutable snapshot before deciding whether it still describes this tree.
116
+ const historyRoot = path.join(worktreePath, ".forge", "work-items", segment(storyId), "qa-inputs");
117
+ await safePath(worktreePath, historyRoot);
118
+ const entries = await fs.readdir(historyRoot, { withFileTypes: true }).catch(error => { if (error.code === "ENOENT")
119
+ return []; throw error; });
120
+ const candidates = [];
121
+ if (entries.length > MAX_FILES)
122
+ throw fail("checkpoint reference history exceeds bounds");
123
+ for (const entry of entries) {
124
+ if (!entry.isDirectory() || entry.name.includes(".tmp-"))
125
+ continue;
126
+ const candidate = path.join(historyRoot, segment(entry.name), "checkpoint", "comment-repair.handoff.json");
127
+ await safePath(worktreePath, candidate);
128
+ const candidateStat = await fs.stat(candidate).catch(error => { if (error.code === "ENOENT")
129
+ return null; throw error; });
130
+ if (candidateStat)
131
+ candidates.push({ path: candidate, modified: candidateStat.mtimeMs });
132
+ }
133
+ candidates.sort((a, b) => b.modified - a.modified);
134
+ if (!candidates[0])
135
+ return null;
136
+ sourcePath = candidates[0].path;
137
+ archivedManifestPath = path.join(path.dirname(path.dirname(sourcePath)), "manifest.json");
138
+ stat = await fs.stat(sourcePath);
139
+ }
140
+ if (!stat.isFile() || stat.size > MAX_FILE_BYTES)
141
+ throw fail("checkpoint size exceeds bounds");
142
+ const body = await fs.readFile(sourcePath, "utf8");
143
+ const checkpoint = JSON.parse(body);
144
+ const recordedHash = checkpoint.content_hash;
145
+ const withoutHash = { ...checkpoint };
146
+ delete withoutHash.content_hash;
147
+ if (typeof recordedHash !== "string" || hash(stableStringify(withoutHash)) !== recordedHash.toLowerCase())
148
+ throw fail("checkpoint content hash mismatch");
149
+ const legacyOwner = checkpoint.owner_id === undefined && checkpoint.owner_type === undefined;
150
+ if (checkpoint.phase !== "comment_repair" || checkpoint.work_item_id !== storyId
151
+ || (!legacyOwner && (checkpoint.owner_id !== storyId || !["work_item", "story"].includes(String(checkpoint.owner_type))))
152
+ || !["pass", "needs_rework", "blocked"].includes(String(checkpoint.status))
153
+ || !Array.isArray(checkpoint.changed_files))
154
+ throw fail("checkpoint provenance mismatch");
155
+ // Ordinary repair artifacts describe paths/summaries but do not certify current bytes.
156
+ // Preserve them only as historical claims; extended checkpoints must pass every exact-tree fence.
157
+ const extended = checkpoint.attempt_dispatch_id !== undefined
158
+ || checkpoint.changed_files.some(value => value && typeof value === "object" && "sha256" in value);
159
+ const ordinaryProvenance = `historical-local-repair-claim:status=${checkpoint.status};not-current-tree-validation;not-authoritative-validation`;
160
+ const exactProvenance = `preserved-local-agent-claim:${checkpoint.attempt_dispatch_id}:HEAD=${checkpoint.repair_head_sha}:status=${checkpoint.status};not-authoritative-validation`;
161
+ const historicalProvenance = `historical-local-repair-claim:${checkpoint.attempt_dispatch_id}:recordedHEAD=${checkpoint.repair_head_sha}:status=${checkpoint.status};not-current-tree-validation;not-authoritative-validation`;
162
+ const archivedProvenance = archivedManifestPath
163
+ ? await validateArchivedCheckpoint(worktreePath, archivedManifestPath, bundle, body, extended ? [exactProvenance, historicalProvenance] : [ordinaryProvenance])
164
+ : undefined;
165
+ if (!extended)
166
+ return { path: "checkpoint/comment-repair.handoff.json", body, contentHash: hash(body), provenance: ordinaryProvenance };
167
+ if (typeof checkpoint.attempt_dispatch_id !== "string" || !/^[a-zA-Z0-9_-]{1,128}$/.test(checkpoint.attempt_dispatch_id))
168
+ throw fail("checkpoint attempt identity missing");
169
+ if (typeof checkpoint.repair_head_sha !== "string" || !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(checkpoint.repair_head_sha))
170
+ throw fail("checkpoint HEAD invalid");
171
+ if (checkpoint.changed_files.length > 512)
172
+ throw fail("checkpoint file count exceeds bounds");
173
+ const recorded = new Set();
174
+ for (const value of checkpoint.changed_files) {
175
+ if (!value || typeof value !== "object")
176
+ throw fail("invalid checkpoint file");
177
+ const file = value;
178
+ if (typeof file.path !== "string" || file.path.includes("\\") || file.path.split("/").some(part => !part || part === "." || part === "..")
179
+ || path.isAbsolute(file.path) || file.path.includes(":") || [...file.path].some(character => character.charCodeAt(0) <= 31)
180
+ || typeof file.sha256 !== "string" || !/^[a-f0-9]{64}$/i.test(file.sha256))
181
+ throw fail("unsafe checkpoint file");
182
+ const filePath = file.path;
183
+ if ([...recorded].some(value => value.toLowerCase() === filePath.toLowerCase()))
184
+ throw fail("duplicate checkpoint file");
185
+ recorded.add(filePath);
186
+ }
187
+ const head = (await git.run(worktreePath, ["rev-parse", "HEAD"])).trim();
188
+ if (archivedProvenance && (checkpoint.repair_head_sha !== head || archivedProvenance === historicalProvenance)) {
189
+ // Publication (and later repair) may legitimately advance the owned branch. This
190
+ // snapshot remains an unchanged historical claim, NEVER proof of the new tree.
191
+ // Do not apply this path to fresh outputs or infer a PASS/publication receipt.
192
+ await git.run(worktreePath, ["merge-base", "--is-ancestor", checkpoint.repair_head_sha, head])
193
+ .catch(() => { throw fail("checkpoint history is not an ancestor of the prepared HEAD"); });
194
+ if ((await git.run(worktreePath, ["rev-parse", "HEAD"])).trim() !== head)
195
+ throw fail("checkpoint HEAD changed during verification");
196
+ return { path: "checkpoint/comment-repair.handoff.json", body, contentHash: hash(body), provenance: historicalProvenance };
197
+ }
198
+ if (checkpoint.repair_head_sha !== head)
199
+ throw fail("checkpoint HEAD changed");
200
+ for (const value of checkpoint.changed_files) {
201
+ const file = value;
202
+ const target = path.resolve(worktreePath, file.path);
203
+ await safePath(worktreePath, target);
204
+ const fileStat = await fs.stat(target);
205
+ if (!fileStat.isFile() || fileStat.size > 32 * 1024 * 1024 || hash(await fs.readFile(target)) !== file.sha256.toLowerCase())
206
+ throw fail("checkpoint changed-file hash mismatch");
207
+ }
208
+ const tracked = (await git.run(worktreePath, ["diff", "--name-only", "-z", "HEAD"])).split("\0").filter(Boolean);
209
+ const untracked = (await git.run(worktreePath, ["ls-files", "--others", "--exclude-standard", "-z"])).split("\0")
210
+ .filter(file => file && !/^\.forge\/(work-items|stories)\//.test(file));
211
+ const changed = new Set([...tracked, ...untracked]);
212
+ if (recorded.size !== changed.size || [...changed].some(file => !recorded.has(file)))
213
+ throw fail("checkpoint does not cover the complete prepared diff");
214
+ if ((await git.run(worktreePath, ["rev-parse", "HEAD"])).trim() !== head)
215
+ throw fail("checkpoint HEAD changed during verification");
216
+ return { path: "checkpoint/comment-repair.handoff.json", body, contentHash: hash(body),
217
+ provenance: exactProvenance };
218
+ }
219
+ async function validateArchivedCheckpoint(worktreePath, manifestPath, bundle, body, allowedProvenance) {
220
+ await safePath(worktreePath, manifestPath);
221
+ const stat = await fs.stat(manifestPath).catch(() => { throw fail("checkpoint reference manifest missing"); });
222
+ if (!stat.isFile() || stat.size > MAX_TOTAL_BYTES)
223
+ throw fail("checkpoint reference manifest exceeds bounds");
224
+ const manifest = JSON.parse(await fs.readFile(manifestPath, "utf8"));
225
+ if (manifest?.schemaVersion !== 1 || manifest.storyWorkItemId !== bundle.storyWorkItemId
226
+ || manifest.projectId !== bundle.projectId || manifest.repoId !== bundle.repoId
227
+ || manifest.planId !== bundle.planId || manifest.planContentHash !== bundle.planContentHash
228
+ || manifest.effectiveAmendmentHash !== bundle.effectiveAmendmentHash
229
+ || typeof manifest.cycleRunId !== "string" || !/^[a-zA-Z0-9_-]{1,128}$/.test(manifest.cycleRunId)
230
+ || !Array.isArray(manifest.files) || manifest.files.length > MAX_FILES)
231
+ throw fail("checkpoint reference identity mismatch");
232
+ const files = manifest.files.filter((file) => typeof file?.path === "string" && file.path.toLowerCase() === "checkpoint/comment-repair.handoff.json");
233
+ if (files.length !== 1 || files[0].path !== "checkpoint/comment-repair.handoff.json"
234
+ || files[0].contentHash !== hash(body) || !allowedProvenance.includes(files[0].provenance))
235
+ throw fail("checkpoint reference integrity mismatch");
236
+ return files[0].provenance;
237
+ }
238
+ async function safePath(root, target) {
239
+ if (!isSubPath(root, target))
240
+ throw fail("reference path escapes worktree");
241
+ let current = path.resolve(root);
242
+ for (const part of path.relative(current, target).split(path.sep)) {
243
+ if (!part)
244
+ continue;
245
+ current = path.join(current, part);
246
+ const stat = await fs.lstat(current).catch(error => { if (error.code === "ENOENT")
247
+ return null; throw error; });
248
+ if (!stat)
249
+ return;
250
+ if (stat.isSymbolicLink())
251
+ throw fail("reference path traverses symlink");
252
+ }
253
+ }
@@ -1,14 +1,25 @@
1
1
  import { GitClient } from "../shared/git/git.js";
2
+ import type { HostLoadStepGate } from "../runtime/host-load-governor.js";
2
3
  import type { WorktreeLifecycleCoordinator, WorktreeService } from "./worktree.base.js";
3
4
  import type { WorktreeHandle, WorktreePrepareRequest } from "./worktree.types.js";
4
5
  export declare class GitWorktreeService implements WorktreeService {
5
6
  private readonly git;
6
7
  private readonly coordinator;
7
- constructor(git?: GitClient, coordinator?: WorktreeLifecycleCoordinator);
8
+ /** Gates dependency install fallbacks; inert unless the governor is on. */
9
+ private readonly hostLoadGate?;
10
+ /** Holds the host runtime lease ledger; branches of live leases are never swept on release. */
11
+ private readonly leaseStateRoot;
12
+ constructor(git?: GitClient, coordinator?: WorktreeLifecycleCoordinator,
13
+ /** Gates dependency install fallbacks; inert unless the governor is on. */
14
+ hostLoadGate?: HostLoadStepGate | undefined,
15
+ /** Holds the host runtime lease ledger; branches of live leases are never swept on release. */
16
+ leaseStateRoot?: string);
8
17
  prepare(request: WorktreePrepareRequest): Promise<WorktreeHandle>;
9
18
  private trackedForgePaths;
19
+ private registeredManagedWorktreePath;
10
20
  private repoCreationCoordinationPath;
11
21
  release(handle: WorktreeHandle): Promise<void>;
22
+ private restoreOrphanedWorktree;
12
23
  private resolveRepoPath;
13
24
  private managedRepoPath;
14
25
  private findExistingRepoPath;
@@ -19,5 +30,10 @@ export declare class GitWorktreeService implements WorktreeService {
19
30
  private currentBranch;
20
31
  private ensureGitIdentity;
21
32
  }
33
+ type WorktreeRename = (source: string, destination: string) => Promise<void>;
34
+ type WorktreeRenameDelay = (milliseconds: number) => Promise<void>;
35
+ export declare function renameWorktreeForRelease(source: string, destination: string, rename?: WorktreeRename, wait?: WorktreeRenameDelay, maxAttempts?: number): Promise<void>;
36
+ export declare function requiresMutableEnvironmentPreparation(request: Pick<WorktreePrepareRequest, "applicationRuntimeRequired">): boolean;
22
37
  export declare function clearRunArtifactRoots(worktreePath: string, request: WorktreePrepareRequest): Promise<void>;
23
38
  export declare function normalizeBranchName(value: string | null | undefined, fallback: string): string;
39
+ export {};