@loucompanion/forge-bridge 0.1.4-dev.fb770705136a → 0.2.1-dev.0f07274d438b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +183 -4
- package/dist/bridge/bridge-chat-giphy-look.d.ts +48 -0
- package/dist/bridge/bridge-chat-giphy-look.js +55 -0
- package/dist/bridge/bridge-chat-provider-command.d.ts +40 -0
- package/dist/bridge/bridge-chat-provider-command.js +230 -0
- package/dist/bridge/bridge-chat.types.d.ts +20 -0
- package/dist/bridge/bridge-chat.types.js +28 -1
- package/dist/bridge/bridge-state-file.d.ts +161 -0
- package/dist/bridge/bridge-state-file.js +376 -0
- package/dist/bridge/bridge-state-io.d.ts +15 -0
- package/dist/bridge/bridge-state-io.js +68 -0
- package/dist/bridge/bridge-state-lock.d.ts +11 -0
- package/dist/bridge/bridge-state-lock.js +102 -0
- package/dist/bridge/bridge-state-unpublished.d.ts +17 -0
- package/dist/bridge/bridge-state-unpublished.js +111 -0
- package/dist/bridge/bridge.base.d.ts +9 -0
- package/dist/bridge/bridge.service.d.ts +102 -14
- package/dist/bridge/bridge.service.js +499 -471
- package/dist/bridge/bridge.types.d.ts +37 -0
- package/dist/bridge/bridge.types.js +2 -1
- package/dist/bridge/default-session-service.d.ts +13 -0
- package/dist/bridge/default-session-service.js +22 -0
- package/dist/bridge/dispatch-offer-policy.d.ts +22 -0
- package/dist/bridge/dispatch-offer-policy.js +58 -0
- package/dist/bridge/in-flight-registry.d.ts +62 -0
- package/dist/bridge/in-flight-registry.js +207 -0
- package/dist/bridge/local-yield-flag.d.ts +6 -0
- package/dist/bridge/local-yield-flag.js +34 -0
- package/dist/bridge/readiness-result-outbox.d.ts +5 -1
- package/dist/bridge/readiness-result-outbox.js +20 -0
- package/dist/bridge/runner-host-capabilities.d.ts +5 -0
- package/dist/bridge/runner-host-capabilities.js +31 -1
- package/dist/bridge/sandbox-resource-accounting.d.ts +16 -0
- package/dist/bridge/sandbox-resource-accounting.js +150 -0
- package/dist/bridge/test-postgres-capability.d.ts +14 -0
- package/dist/bridge/test-postgres-capability.js +120 -0
- package/dist/bridge/windows-bash-resolution.d.ts +31 -0
- package/dist/bridge/windows-bash-resolution.js +153 -0
- package/dist/cleanup/cleanup-wip.d.ts +12 -0
- package/dist/cleanup/cleanup-wip.js +66 -0
- package/dist/cleanup/cleanup.service.d.ts +20 -1
- package/dist/cleanup/cleanup.service.js +150 -169
- package/dist/cleanup/cleanup.types.d.ts +6 -0
- package/dist/cleanup/forge-owned-paths.d.ts +11 -0
- package/dist/cleanup/forge-owned-paths.js +62 -0
- package/dist/cleanup/host-runtime-ledger.d.ts +7 -0
- package/dist/cleanup/host-runtime-ledger.js +130 -0
- package/dist/cleanup/managed-branch-sweep.d.ts +27 -0
- package/dist/cleanup/managed-branch-sweep.js +111 -0
- package/dist/cli/cli.help.js +75 -13
- package/dist/cli/cli.service.d.ts +19 -10
- package/dist/cli/cli.service.js +161 -228
- package/dist/cli/idle-status.d.ts +84 -0
- package/dist/cli/idle-status.js +181 -0
- package/dist/cli/local-config.d.ts +18 -1
- package/dist/cli/local-config.js +67 -5
- package/dist/cli/provider-cli-advertising.d.ts +24 -0
- package/dist/cli/provider-cli-advertising.js +88 -0
- package/dist/cli/provider-cli-detection.d.ts +10 -0
- package/dist/cli/provider-cli-detection.js +160 -0
- package/dist/runtime/aspnet-runtime-prebuild.d.ts +20 -0
- package/dist/runtime/aspnet-runtime-prebuild.js +184 -18
- package/dist/runtime/attempt-database-broker.d.ts +1 -1
- package/dist/runtime/attempt-node-runtime-launcher.d.ts +36 -0
- package/dist/runtime/attempt-node-runtime-launcher.js +169 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +2 -0
- package/dist/runtime/attempt-process-supervisor.js +12 -6
- package/dist/runtime/docker-engine-probe.d.ts +5 -0
- package/dist/runtime/docker-engine-probe.js +7 -0
- package/dist/runtime/dotnet-build-outputs.d.ts +107 -0
- package/dist/runtime/dotnet-build-outputs.js +402 -0
- package/dist/runtime/ephemeral-test-postgres-grant.d.ts +37 -0
- package/dist/runtime/ephemeral-test-postgres-grant.js +85 -0
- package/dist/runtime/ephemeral-test-postgres.d.ts +57 -0
- package/dist/runtime/ephemeral-test-postgres.js +303 -0
- package/dist/runtime/host-facts.d.ts +2 -1
- package/dist/runtime/host-listener-inspector.d.ts +26 -0
- package/dist/runtime/host-listener-inspector.js +84 -3
- package/dist/runtime/host-load-admission.d.ts +72 -0
- package/dist/runtime/host-load-admission.js +99 -0
- package/dist/runtime/host-load-capacity.d.ts +198 -0
- package/dist/runtime/host-load-capacity.js +353 -0
- package/dist/runtime/host-load-child-environment.d.ts +40 -0
- package/dist/runtime/host-load-child-environment.js +110 -0
- package/dist/runtime/host-load-forge-cgroups.d.ts +49 -0
- package/dist/runtime/host-load-forge-cgroups.js +140 -0
- package/dist/runtime/host-load-governor-config.d.ts +82 -0
- package/dist/runtime/host-load-governor-config.js +104 -0
- package/dist/runtime/host-load-governor.d.ts +273 -0
- package/dist/runtime/host-load-governor.js +831 -0
- package/dist/runtime/host-load-ledger-lock.d.ts +69 -0
- package/dist/runtime/host-load-ledger-lock.js +187 -0
- package/dist/runtime/host-load-live-test-slots.d.ts +61 -0
- package/dist/runtime/host-load-live-test-slots.js +193 -0
- package/dist/runtime/host-load-runtime-settings.d.ts +25 -0
- package/dist/runtime/host-load-runtime-settings.js +80 -0
- package/dist/runtime/host-load-sampler.d.ts +92 -2
- package/dist/runtime/host-load-sampler.js +166 -8
- package/dist/runtime/host-load-step-class.d.ts +70 -0
- package/dist/runtime/host-load-step-class.js +161 -0
- package/dist/runtime/host-load-step-reaper.d.ts +27 -0
- package/dist/runtime/host-load-step-reaper.js +95 -0
- package/dist/runtime/host-load-step-run.d.ts +107 -0
- package/dist/runtime/host-load-step-run.js +158 -0
- package/dist/runtime/host-load-test-slot.d.ts +46 -0
- package/dist/runtime/host-load-test-slot.js +100 -0
- package/dist/runtime/host-load-token-ledger.d.ts +200 -0
- package/dist/runtime/host-load-token-ledger.js +453 -0
- package/dist/runtime/host-load-windows-cpu.d.ts +34 -0
- package/dist/runtime/host-load-windows-cpu.js +85 -0
- package/dist/runtime/host-pressure-stall.d.ts +21 -1
- package/dist/runtime/host-pressure-stall.js +48 -1
- package/dist/runtime/host-runtime-contract.d.ts +15 -0
- package/dist/runtime/host-runtime-contract.js +39 -6
- package/dist/runtime/host-runtime-lease.d.ts +101 -6
- package/dist/runtime/host-runtime-lease.js +408 -294
- package/dist/runtime/host-runtime-ledger-recovery.d.ts +20 -0
- package/dist/runtime/host-runtime-ledger-recovery.js +134 -0
- package/dist/runtime/host-runtime-promotion.d.ts +101 -2
- package/dist/runtime/host-runtime-promotion.js +355 -14
- package/dist/runtime/host-runtime-requirements-check.d.ts +17 -0
- package/dist/runtime/host-runtime-requirements-check.js +29 -0
- package/dist/runtime/host-runtime-requirements.d.ts +36 -0
- package/dist/runtime/host-runtime-requirements.js +117 -0
- package/dist/runtime/managed-command-teardown.js +5 -16
- package/dist/runtime/native-test-postgres-fallback.d.ts +51 -7
- package/dist/runtime/native-test-postgres-fallback.js +60 -8
- package/dist/runtime/playwright-host-runtime-adapter.d.ts +7 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +13 -1
- package/dist/runtime/prepared-runtime-identity.d.ts +4 -0
- package/dist/runtime/prepared-runtime-identity.js +130 -9
- package/dist/runtime/prepared-runtime-readiness.d.ts +23 -1
- package/dist/runtime/prepared-runtime-readiness.js +89 -18
- package/dist/runtime/runner-settings.d.ts +104 -0
- package/dist/runtime/runner-settings.js +236 -0
- package/dist/runtime/supervised-runtime-build-guard.d.ts +45 -0
- package/dist/runtime/supervised-runtime-build-guard.js +112 -0
- package/dist/sandbox/host-capacity-broker.d.ts +115 -0
- package/dist/sandbox/host-capacity-broker.js +336 -0
- package/dist/sandbox/managed-branch.d.ts +13 -0
- package/dist/sandbox/managed-branch.js +90 -0
- package/dist/sandbox/provider-auth/codex.js +11 -11
- package/dist/sandbox/runtime.d.ts +11 -3
- package/dist/sandbox/runtime.js +53 -20
- package/dist/sandbox/sandbox-cgroups.d.ts +14 -0
- package/dist/sandbox/sandbox-cgroups.js +42 -0
- package/dist/sandbox/sandbox-input.d.ts +22 -0
- package/dist/sandbox/sandbox-input.js +49 -0
- package/dist/sandbox/workspace.service.js +15 -2
- package/dist/session/architect-waits.d.ts +2 -0
- package/dist/session/architect-waits.js +138 -0
- package/dist/session/managed-attempt-tools.d.ts +2 -7
- package/dist/session/managed-attempt-tools.js +204 -141
- package/dist/session/managed-build-coordinator.d.ts +16 -0
- package/dist/session/managed-build-coordinator.js +10 -0
- package/dist/session/managed-build-receipt.d.ts +7 -0
- package/dist/session/managed-build-receipt.js +11 -3
- package/dist/session/managed-command-environment.d.ts +8 -0
- package/dist/session/managed-command-environment.js +28 -0
- package/dist/session/managed-command-host-load.d.ts +52 -0
- package/dist/session/managed-command-host-load.js +87 -0
- package/dist/session/mcp-argument-coercion.d.ts +23 -0
- package/dist/session/mcp-argument-coercion.js +121 -0
- package/dist/session/phase-completion.artifact-bundle.d.ts +20 -0
- package/dist/session/phase-completion.artifact-bundle.js +167 -0
- package/dist/session/phase-completion.artifacts.d.ts +9 -17
- package/dist/session/phase-completion.artifacts.js +158 -190
- package/dist/session/phase-completion.command-validation.d.ts +7 -0
- package/dist/session/phase-completion.command-validation.js +77 -0
- package/dist/session/phase-completion.d.ts +31 -2
- package/dist/session/phase-completion.helpers.js +18 -65
- package/dist/session/phase-completion.js +19 -32
- package/dist/session/phase-completion.mcp-args.d.ts +9 -0
- package/dist/session/phase-completion.mcp-args.js +195 -0
- package/dist/session/phase-completion.phase-aliases.d.ts +5 -0
- package/dist/session/phase-completion.phase-aliases.js +43 -0
- package/dist/session/phase-completion.pr-review-schema.d.ts +1 -0
- package/dist/session/phase-completion.pr-review-schema.js +75 -0
- package/dist/session/phase-completion.pr-review-validation.d.ts +11 -0
- package/dist/session/phase-completion.pr-review-validation.js +212 -0
- package/dist/session/phase-completion.pr-review.d.ts +36 -0
- package/dist/session/phase-completion.pr-review.js +294 -0
- package/dist/session/phase-completion.prompts.d.ts +1 -1
- package/dist/session/phase-completion.prompts.js +20 -1
- package/dist/session/phase-completion.runtime.d.ts +13 -12
- package/dist/session/phase-completion.runtime.js +113 -214
- package/dist/session/phase-completion.schema-parts.d.ts +19 -0
- package/dist/session/phase-completion.schema-parts.js +188 -0
- package/dist/session/phase-completion.schemas.d.ts +3 -3
- package/dist/session/phase-completion.schemas.js +219 -232
- package/dist/session/phase-completion.validation.d.ts +3 -3
- package/dist/session/phase-completion.validation.js +58 -94
- package/dist/session/pr-review-diff-base.d.ts +18 -0
- package/dist/session/pr-review-diff-base.js +44 -0
- package/dist/session/pr-review-placement.d.ts +50 -0
- package/dist/session/pr-review-placement.js +91 -0
- package/dist/session/pre-publication-change-set.d.ts +62 -0
- package/dist/session/pre-publication-change-set.js +166 -0
- package/dist/session/pre-publication-host-load.d.ts +27 -0
- package/dist/session/pre-publication-host-load.js +112 -0
- package/dist/session/pre-publication-native-postgres.d.ts +30 -0
- package/dist/session/pre-publication-native-postgres.js +111 -0
- package/dist/session/pre-publication-pass.d.ts +61 -0
- package/dist/session/pre-publication-pass.js +148 -0
- package/dist/session/pre-publication-process.d.ts +3 -0
- package/dist/session/pre-publication-process.js +32 -19
- package/dist/session/pre-publication-step-execution.d.ts +12 -0
- package/dist/session/pre-publication-step-execution.js +50 -0
- package/dist/session/pre-publication-validation.d.ts +39 -22
- package/dist/session/pre-publication-validation.js +241 -207
- package/dist/session/provider-cli/claude-background-work.d.ts +67 -0
- package/dist/session/provider-cli/claude-background-work.js +181 -0
- package/dist/session/provider-cli/dispatch-target-branch.d.ts +7 -0
- package/dist/session/provider-cli/dispatch-target-branch.js +8 -0
- package/dist/session/provider-cli/host-build-lock.d.ts +18 -0
- package/dist/session/provider-cli/host-build-lock.js +30 -0
- package/dist/session/provider-cli/pr-review-credential-isolation.d.ts +83 -0
- package/dist/session/provider-cli/pr-review-credential-isolation.js +144 -0
- package/dist/session/provider-cli/pr-review-guardrails.d.ts +70 -0
- package/dist/session/provider-cli/pr-review-guardrails.js +360 -0
- package/dist/session/provider-cli/pr-review-prompt.d.ts +2 -0
- package/dist/session/provider-cli/pr-review-prompt.js +119 -0
- package/dist/session/provider-cli/pre-publication-repair.d.ts +56 -0
- package/dist/session/provider-cli/pre-publication-repair.js +281 -0
- package/dist/session/provider-cli/provider-cli.base.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.child-environment.d.ts +7 -0
- package/dist/session/provider-cli/provider-cli.child-environment.js +75 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.d.ts +3 -0
- package/dist/session/provider-cli/provider-cli.codex-models-cache.js +37 -0
- package/dist/session/provider-cli/provider-cli.deterministic.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.deterministic.js +5 -0
- package/dist/session/provider-cli/provider-cli.merge-integrity.d.ts +6 -3
- package/dist/session/provider-cli/provider-cli.merge-integrity.js +29 -10
- package/dist/session/provider-cli/provider-cli.pr-review.d.ts +23 -0
- package/dist/session/provider-cli/provider-cli.pr-review.js +213 -0
- package/dist/session/provider-cli/provider-cli.process.d.ts +15 -36
- package/dist/session/provider-cli/provider-cli.process.js +41 -432
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +15 -3
- package/dist/session/provider-cli/provider-cli.prompts.js +183 -90
- package/dist/session/provider-cli/provider-cli.reapply-conflicts.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.reapply-conflicts.js +37 -0
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +15 -0
- package/dist/session/provider-cli/provider-cli.runtime.js +43 -1
- package/dist/session/provider-cli/provider-cli.service-support.d.ts +37 -0
- package/dist/session/provider-cli/provider-cli.service-support.js +146 -0
- package/dist/session/provider-cli/provider-cli.service.d.ts +18 -18
- package/dist/session/provider-cli/provider-cli.service.js +127 -317
- package/dist/session/provider-cli/provider-cli.types.d.ts +35 -0
- package/dist/session/provider-cli/provider-cli.windows-environment.d.ts +10 -0
- package/dist/session/provider-cli/provider-cli.windows-environment.js +151 -0
- package/dist/session/provider-cli/provider-cli.windows-launch.d.ts +23 -0
- package/dist/session/provider-cli/provider-cli.windows-launch.js +131 -0
- package/dist/session/provider-cli/provider-in-flight-work.d.ts +4 -0
- package/dist/session/provider-cli/provider-in-flight-work.js +45 -0
- package/dist/session/provider-cli/provider-output-pipes.d.ts +30 -0
- package/dist/session/provider-cli/provider-output-pipes.js +67 -0
- package/dist/session/provider-cli/provider-output-state.d.ts +2 -0
- package/dist/session/provider-cli/provider-output-state.js +6 -0
- package/dist/session/provider-cli/provider-user-config-isolation.d.ts +69 -0
- package/dist/session/provider-cli/provider-user-config-isolation.js +164 -0
- package/dist/session/qa-sidecar-validation.d.ts +6 -0
- package/dist/session/qa-sidecar-validation.js +28 -0
- package/dist/session/sandbox-host-capacity-gate.d.ts +51 -0
- package/dist/session/sandbox-host-capacity-gate.js +279 -0
- package/dist/session/session-host-project.d.ts +22 -0
- package/dist/session/session-host-project.js +98 -0
- package/dist/session/session-redaction.js +31 -181
- package/dist/session/session.base.d.ts +9 -1
- package/dist/session/session.parse-helpers.d.ts +10 -0
- package/dist/session/session.parse-helpers.js +84 -0
- package/dist/session/session.runtime-refresh.d.ts +46 -0
- package/dist/session/session.runtime-refresh.js +116 -0
- package/dist/session/session.service.d.ts +16 -2
- package/dist/session/session.service.js +208 -155
- package/dist/session/session.types.d.ts +8 -0
- package/dist/session/session.types.js +13 -83
- package/dist/session/session.types.pr-review.d.ts +48 -0
- package/dist/session/session.types.pr-review.js +51 -0
- package/dist/session/sidecar-json-shape.d.ts +31 -0
- package/dist/session/sidecar-json-shape.js +217 -0
- package/dist/session/workspace-runtime-fingerprint.d.ts +32 -0
- package/dist/session/workspace-runtime-fingerprint.js +138 -0
- package/dist/shared/fs/file-identity.d.ts +10 -0
- package/dist/shared/fs/file-identity.js +15 -0
- package/dist/shared/fs/replace-file.d.ts +7 -0
- package/dist/shared/fs/replace-file.js +33 -0
- package/dist/shared/process/host-process-recovery.d.ts +14 -3
- package/dist/shared/process/host-process-recovery.js +23 -3
- package/dist/shared/process/host-process-supervisor.d.ts +14 -1
- package/dist/shared/process/host-process-supervisor.js +67 -15
- package/dist/shared/process/process-tree.d.ts +52 -0
- package/dist/shared/process/process-tree.js +144 -0
- package/dist/shared/process/process.d.ts +67 -0
- package/dist/shared/process/process.js +213 -11
- package/dist/shared/process/windows-app-execution-alias.d.ts +19 -0
- package/dist/shared/process/windows-app-execution-alias.js +45 -0
- package/dist/shared/process/windows-launch.d.ts +18 -0
- package/dist/shared/process/windows-launch.js +68 -0
- package/dist/shared/provider-capabilities.d.ts +2 -0
- package/dist/shared/provider-capabilities.js +23 -7
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/artifact-injector.js +24 -0
- package/dist/worktree/dependency-warmer.d.ts +25 -0
- package/dist/worktree/dependency-warmer.js +46 -10
- package/dist/worktree/orphan-relink.d.ts +19 -0
- package/dist/worktree/orphan-relink.js +106 -0
- package/dist/worktree/wip-git.d.ts +58 -0
- package/dist/worktree/wip-git.js +182 -0
- package/dist/worktree/wip-refs.d.ts +85 -0
- package/dist/worktree/wip-refs.js +169 -0
- package/dist/worktree/wip-restore.d.ts +51 -0
- package/dist/worktree/wip-restore.js +324 -0
- package/dist/worktree/wip-snapshot.d.ts +53 -0
- package/dist/worktree/wip-snapshot.js +270 -0
- package/dist/worktree/worktree-inputs.d.ts +8 -0
- package/dist/worktree/worktree-inputs.js +134 -0
- package/dist/worktree/worktree-origin-sync.d.ts +35 -0
- package/dist/worktree/worktree-origin-sync.js +128 -0
- package/dist/worktree/worktree-wip-events.d.ts +20 -0
- package/dist/worktree/worktree-wip-events.js +148 -0
- package/dist/worktree/worktree-wip.d.ts +57 -0
- package/dist/worktree/worktree-wip.js +200 -0
- package/dist/worktree/worktree.base.d.ts +7 -1
- package/dist/worktree/worktree.service.d.ts +58 -5
- package/dist/worktree/worktree.service.js +237 -161
- package/dist/worktree/worktree.types.d.ts +54 -0
- package/package.json +2 -2
- package/runtime/windows-listener-owner.ps1 +12 -1
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { constants as fsConstants } from "node:fs";
|
|
3
2
|
import fs from "node:fs/promises";
|
|
4
3
|
import path from "node:path";
|
|
5
4
|
import { RepositoryCoordinatorLock } from "../sandbox/repository-lock.js";
|
|
6
5
|
import { GitClient } from "../shared/git/git.js";
|
|
7
6
|
import { isSubPath, resolveExistingDirectoryPath, resolvePathIdentity } from "../shared/path/path.js";
|
|
8
|
-
import { validRuntimeIsolationLease } from "../runtime/runtime-isolation-lease.js";
|
|
9
7
|
import { defaultWorktreeCoordinatorStateRoot, resolveManagedWorktreeRootForRepo, } from "../worktree/worktree-paths.js";
|
|
8
|
+
import { saveStaleWorktreeWork } from "./cleanup-wip.js";
|
|
9
|
+
import { isForgeOwnedWorktreePath, isForgeWorktreeContainer, isRuntimeReadinessName } from "./forge-owned-paths.js";
|
|
10
|
+
import { liveLeaseBranches, readHostRuntimeLeases, resolveRuntimeLeasePath } from "./host-runtime-ledger.js";
|
|
11
|
+
import { sweepManagedBranches } from "./managed-branch-sweep.js";
|
|
10
12
|
const staleRuntimeReadinessAgeMs = 30 * 60 * 1000;
|
|
11
13
|
export class ManagedWorktreeCleanupService {
|
|
12
14
|
stateRoot;
|
|
13
15
|
git;
|
|
14
16
|
coordinator;
|
|
15
|
-
|
|
17
|
+
warn;
|
|
18
|
+
constructor(stateRoot = defaultWorktreeCoordinatorStateRoot(), git = new GitClient(), coordinator = new RepositoryCoordinatorLock(stateRoot),
|
|
19
|
+
/** Where cleanup explains each worktree it kept because its work could not be saved. */
|
|
20
|
+
warn = message => process.stderr.write(`${message}\n`)) {
|
|
16
21
|
this.stateRoot = stateRoot;
|
|
17
22
|
this.git = git;
|
|
18
23
|
this.coordinator = coordinator;
|
|
24
|
+
this.warn = warn;
|
|
19
25
|
}
|
|
20
26
|
async run(request) {
|
|
21
27
|
const [repoPath, allowedRepoRoot] = await Promise.all([
|
|
@@ -31,15 +37,30 @@ export class ManagedWorktreeCleanupService {
|
|
|
31
37
|
}
|
|
32
38
|
async runLocked(repoPath, worktreeRoot, request) {
|
|
33
39
|
if (request.mode === "merged") {
|
|
34
|
-
if (request.force)
|
|
35
|
-
|
|
36
|
-
|
|
40
|
+
if (!request.force)
|
|
41
|
+
return emptyResult(worktreeRoot, request.force);
|
|
42
|
+
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
43
|
+
// Forge branches whose work is already on the base, and leftover readiness branches.
|
|
44
|
+
const baseBranch = await this.remoteBaseBranch(repoPath);
|
|
45
|
+
const sweep = await sweepManagedBranches(this.git, repoPath, {
|
|
46
|
+
...(baseBranch ? { baseBranch } : {}),
|
|
47
|
+
includeMergedWorkBranches: baseBranch !== null,
|
|
48
|
+
protectedBranches: liveLeaseBranches(await this.readRuntimeLeases()),
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
...emptyResult(worktreeRoot, request.force),
|
|
52
|
+
deletedBranches: sweep.deletedBranches.length,
|
|
53
|
+
...(baseBranch ? {} : { mergedBaseUnresolved: true }),
|
|
54
|
+
};
|
|
37
55
|
}
|
|
38
56
|
const safeRoot = await resolveSafeManagedRoot(worktreeRoot);
|
|
39
57
|
if (!safeRoot) {
|
|
40
|
-
if (request.force)
|
|
41
|
-
|
|
42
|
-
|
|
58
|
+
if (!request.force)
|
|
59
|
+
return emptyResult(worktreeRoot, request.force);
|
|
60
|
+
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
61
|
+
// No managed root left, but readiness branches can outlive it.
|
|
62
|
+
const sweep = await this.sweepReadinessBranches(repoPath);
|
|
63
|
+
return { ...emptyResult(worktreeRoot, request.force), deletedBranches: sweep.deletedBranches.length };
|
|
43
64
|
}
|
|
44
65
|
const [listedWorktrees, runtimeLeases] = await Promise.all([
|
|
45
66
|
this.listWorktrees(repoPath),
|
|
@@ -49,34 +70,11 @@ export class ManagedWorktreeCleanupService {
|
|
|
49
70
|
.filter(lease => lease.state !== "released")
|
|
50
71
|
.map(lease => resolveRuntimeLeasePath(lease.worktreePath)));
|
|
51
72
|
const metadataRoot = await this.worktreeMetadataRoot(repoPath);
|
|
52
|
-
const staleReadinessPaths = await this.staleRuntimeReadinessPaths(listedWorktrees, activeRuntimePaths);
|
|
73
|
+
const staleReadinessPaths = await this.staleRuntimeReadinessPaths(safeRoot, listedWorktrees, activeRuntimePaths);
|
|
53
74
|
const activeWorktrees = activePaths(listedWorktrees)
|
|
54
75
|
.filter(activePath => !staleReadinessPaths.some(stalePath => samePath(stalePath, activePath)));
|
|
55
76
|
if (request.mode === "all") {
|
|
56
|
-
|
|
57
|
-
...activeWorktrees.filter(activePath => isSubPath(safeRoot, activePath)),
|
|
58
|
-
...activeRuntimePaths.filter(activePath => isSubPath(safeRoot, activePath)),
|
|
59
|
-
]);
|
|
60
|
-
if (protectedPaths.length > 0) {
|
|
61
|
-
throw new Error(`Refusing to remove managed worktree root ${safeRoot}; ${protectedPaths.length} active worktree or runtime lease(s) remain.`);
|
|
62
|
-
}
|
|
63
|
-
const scan = await this.scanManagedCandidates(metadataRoot, safeRoot, activeWorktrees, activeRuntimePaths);
|
|
64
|
-
if (request.force && !scan.rootRemovalSafe) {
|
|
65
|
-
throw new Error(`Refusing to remove managed worktree root ${safeRoot}; unowned content remains (${scan.skippedUnownedDirectories} skipped director${scan.skippedUnownedDirectories === 1 ? "y" : "ies"}).`);
|
|
66
|
-
}
|
|
67
|
-
if (request.force) {
|
|
68
|
-
await quarantineAndRemoveRoot(safeRoot);
|
|
69
|
-
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
70
|
-
}
|
|
71
|
-
return {
|
|
72
|
-
worktreeRoot: safeRoot,
|
|
73
|
-
removedWorktrees: request.force ? scan.candidates.length : 0,
|
|
74
|
-
candidateWorktrees: scan.candidates.length,
|
|
75
|
-
protectedWorktrees: scan.protectedWorktrees,
|
|
76
|
-
skippedUnownedDirectories: scan.skippedUnownedDirectories,
|
|
77
|
-
removedRoot: request.force,
|
|
78
|
-
dryRun: !request.force,
|
|
79
|
-
};
|
|
77
|
+
return await this.removeWholeRoot(repoPath, safeRoot, metadataRoot, activeWorktrees, activeRuntimePaths, request.force);
|
|
80
78
|
}
|
|
81
79
|
const scan = await this.scanManagedCandidates(metadataRoot, safeRoot, activeWorktrees, activeRuntimePaths);
|
|
82
80
|
if (!request.force) {
|
|
@@ -91,32 +89,27 @@ export class ManagedWorktreeCleanupService {
|
|
|
91
89
|
};
|
|
92
90
|
}
|
|
93
91
|
let removedWorktrees = 0;
|
|
92
|
+
let preservedWorktrees = 0;
|
|
94
93
|
for (const candidate of scan.candidates) {
|
|
95
|
-
|
|
96
|
-
this.listWorktrees(repoPath),
|
|
97
|
-
this.readRuntimeLeases(),
|
|
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);
|
|
103
|
-
const currentlyProtected = [
|
|
104
|
-
...activePaths(currentWorktrees)
|
|
105
|
-
.filter(activePath => !currentStaleReadinessPaths.some(stalePath => samePath(stalePath, activePath))),
|
|
106
|
-
...currentRuntimePaths,
|
|
107
|
-
];
|
|
108
|
-
if (worktreePathState(candidate, currentlyProtected) !== "none") {
|
|
94
|
+
if (await this.isProtectedNow(repoPath, safeRoot, candidate)) {
|
|
109
95
|
continue;
|
|
110
96
|
}
|
|
111
97
|
const candidateIdentity = await forgeManagedCandidateIdentity(metadataRoot, candidate);
|
|
112
98
|
if (!candidateIdentity) {
|
|
113
99
|
continue;
|
|
114
100
|
}
|
|
101
|
+
// Never delete uncommitted work that could not be saved first.
|
|
102
|
+
if (await this.saveCandidatesWork(repoPath, safeRoot, [candidate]) > 0) {
|
|
103
|
+
preservedWorktrees += 1;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
115
106
|
if (await quarantineAndRemoveCandidate(safeRoot, candidate, candidateIdentity)) {
|
|
116
107
|
removedWorktrees += 1;
|
|
117
108
|
}
|
|
118
109
|
}
|
|
119
110
|
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
111
|
+
// A readiness branch goes with its worktree; this also clears ones whose worktree is long gone.
|
|
112
|
+
const sweep = await this.sweepReadinessBranches(repoPath);
|
|
120
113
|
return {
|
|
121
114
|
worktreeRoot: safeRoot,
|
|
122
115
|
removedWorktrees,
|
|
@@ -125,16 +118,112 @@ export class ManagedWorktreeCleanupService {
|
|
|
125
118
|
skippedUnownedDirectories: scan.skippedUnownedDirectories,
|
|
126
119
|
removedRoot: false,
|
|
127
120
|
dryRun: false,
|
|
121
|
+
deletedBranches: sweep.deletedBranches.length,
|
|
122
|
+
preservedWorktrees,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** All mode: removes the managed root itself once nothing in it is active, unowned or unsaved. */
|
|
126
|
+
async removeWholeRoot(repoPath, safeRoot, metadataRoot, activeWorktrees, activeRuntimePaths, force) {
|
|
127
|
+
const protectedPaths = uniquePaths([
|
|
128
|
+
...activeWorktrees.filter(activePath => isSubPath(safeRoot, activePath)),
|
|
129
|
+
...activeRuntimePaths.filter(activePath => isSubPath(safeRoot, activePath)),
|
|
130
|
+
]);
|
|
131
|
+
if (protectedPaths.length > 0) {
|
|
132
|
+
throw new Error(`Refusing to remove managed worktree root ${safeRoot}; ${protectedPaths.length} active worktree or runtime lease(s) remain.`);
|
|
133
|
+
}
|
|
134
|
+
const scan = await this.scanManagedCandidates(metadataRoot, safeRoot, activeWorktrees, activeRuntimePaths);
|
|
135
|
+
if (force && !scan.rootRemovalSafe) {
|
|
136
|
+
throw new Error(`Refusing to remove managed worktree root ${safeRoot}; unowned content remains (${scan.skippedUnownedDirectories} skipped director${scan.skippedUnownedDirectories === 1 ? "y" : "ies"}).`);
|
|
137
|
+
}
|
|
138
|
+
if (force) {
|
|
139
|
+
// Every candidate's work is saved first; one that can't be saved keeps the whole root.
|
|
140
|
+
const kept = await this.saveCandidatesWork(repoPath, safeRoot, scan.candidates);
|
|
141
|
+
if (kept > 0) {
|
|
142
|
+
throw new Error(`Refusing to remove managed worktree root ${safeRoot}; ${kept} worktree(s) hold uncommitted work that could not be saved to a WIP ref.`);
|
|
143
|
+
}
|
|
144
|
+
await quarantineAndRemoveRoot(safeRoot);
|
|
145
|
+
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
worktreeRoot: safeRoot,
|
|
149
|
+
removedWorktrees: force ? scan.candidates.length : 0,
|
|
150
|
+
candidateWorktrees: scan.candidates.length,
|
|
151
|
+
protectedWorktrees: scan.protectedWorktrees,
|
|
152
|
+
skippedUnownedDirectories: scan.skippedUnownedDirectories,
|
|
153
|
+
removedRoot: force,
|
|
154
|
+
dryRun: !force,
|
|
128
155
|
};
|
|
129
156
|
}
|
|
157
|
+
/** Re-reads registrations and leases: a candidate may have become active since the scan. */
|
|
158
|
+
async isProtectedNow(repoPath, safeRoot, candidate) {
|
|
159
|
+
const [currentWorktrees, currentLeases] = await Promise.all([
|
|
160
|
+
this.listWorktrees(repoPath),
|
|
161
|
+
this.readRuntimeLeases(),
|
|
162
|
+
]);
|
|
163
|
+
const currentRuntimePaths = await Promise.all(currentLeases
|
|
164
|
+
.filter(lease => lease.state !== "released")
|
|
165
|
+
.map(lease => resolveRuntimeLeasePath(lease.worktreePath)));
|
|
166
|
+
const currentStaleReadinessPaths = await this.staleRuntimeReadinessPaths(safeRoot, currentWorktrees, currentRuntimePaths);
|
|
167
|
+
const currentlyProtected = [
|
|
168
|
+
...activePaths(currentWorktrees)
|
|
169
|
+
.filter(activePath => !currentStaleReadinessPaths.some(stalePath => samePath(stalePath, activePath))),
|
|
170
|
+
...currentRuntimePaths,
|
|
171
|
+
];
|
|
172
|
+
return worktreePathState(candidate, currentlyProtected) !== "none";
|
|
173
|
+
}
|
|
174
|
+
/** Saves each candidate's uncommitted work to a WIP ref; returns how many could not be saved. */
|
|
175
|
+
async saveCandidatesWork(repoPath, safeRoot, candidates) {
|
|
176
|
+
let kept = 0;
|
|
177
|
+
for (const candidate of candidates) {
|
|
178
|
+
const outcome = await saveStaleWorktreeWork(this.git, repoPath, safeRoot, candidate);
|
|
179
|
+
if (outcome.status === "saved") {
|
|
180
|
+
this.warn(`Saved uncommitted work in stale worktree ${candidate} to ${outcome.ref} before removing it.`);
|
|
181
|
+
}
|
|
182
|
+
else if (outcome.status === "failed") {
|
|
183
|
+
kept += 1;
|
|
184
|
+
this.warn(`Kept stale worktree ${candidate}: its uncommitted work could not be saved to a WIP ref (${outcome.reason}).`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return kept;
|
|
188
|
+
}
|
|
189
|
+
async sweepReadinessBranches(repoPath) {
|
|
190
|
+
return await sweepManagedBranches(this.git, repoPath, {
|
|
191
|
+
includeMergedWorkBranches: false,
|
|
192
|
+
protectedBranches: liveLeaseBranches(await this.readRuntimeLeases()),
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The branch merged work lands on: origin/HEAD when it points at a remote-tracking ref that
|
|
197
|
+
* exists, otherwise the only one of origin/develop, origin/main and origin/master that exists.
|
|
198
|
+
* Null when that's ambiguous or none exist, so the merged sweep is skipped (and reported)
|
|
199
|
+
* rather than measured against the wrong base.
|
|
200
|
+
*/
|
|
201
|
+
async remoteBaseBranch(repoPath) {
|
|
202
|
+
const originHead = await this.git.run(repoPath, ["symbolic-ref", "--quiet", "--short", "refs/remotes/origin/HEAD"])
|
|
203
|
+
.catch(() => "");
|
|
204
|
+
const headBranch = originHead.trim().replace(/^origin\//, "");
|
|
205
|
+
// A stale origin/HEAD can name a branch whose remote-tracking ref has been pruned.
|
|
206
|
+
if (headBranch && await this.remoteBranchExists(repoPath, headBranch))
|
|
207
|
+
return headBranch;
|
|
208
|
+
const present = [];
|
|
209
|
+
for (const candidate of ["develop", "main", "master"]) {
|
|
210
|
+
if (await this.remoteBranchExists(repoPath, candidate))
|
|
211
|
+
present.push(candidate);
|
|
212
|
+
}
|
|
213
|
+
return present.length === 1 ? present[0] : null;
|
|
214
|
+
}
|
|
215
|
+
async remoteBranchExists(repoPath, branch) {
|
|
216
|
+
return await this.git.run(repoPath, ["rev-parse", "--verify", "--quiet", `refs/remotes/origin/${branch}^{commit}`])
|
|
217
|
+
.then(() => true, () => false);
|
|
218
|
+
}
|
|
130
219
|
async listWorktrees(repoPath) {
|
|
131
220
|
const output = await this.git.run(repoPath, ["worktree", "list", "--porcelain", "-z"]);
|
|
132
221
|
return parseWorktreeListing(repoPath, output);
|
|
133
222
|
}
|
|
134
|
-
async staleRuntimeReadinessPaths(worktrees, runtimePaths) {
|
|
223
|
+
async staleRuntimeReadinessPaths(worktreeRoot, worktrees, runtimePaths) {
|
|
135
224
|
const stale = [];
|
|
136
225
|
for (const worktree of worktrees) {
|
|
137
|
-
if (worktree.prunable || !isRuntimeReadinessWorktree(worktree))
|
|
226
|
+
if (worktree.prunable || !isRuntimeReadinessWorktree(worktreeRoot, worktree))
|
|
138
227
|
continue;
|
|
139
228
|
if (worktreePathState(worktree.worktreePath, runtimePaths) !== "none")
|
|
140
229
|
continue;
|
|
@@ -199,12 +288,13 @@ export class ManagedWorktreeCleanupService {
|
|
|
199
288
|
containsManagedDirectory = await visit(realEntry) || containsManagedDirectory;
|
|
200
289
|
continue;
|
|
201
290
|
}
|
|
202
|
-
|
|
291
|
+
const relativePath = path.relative(worktreeRoot, realEntry);
|
|
292
|
+
if (isForgeOwnedWorktreePath(relativePath) && await forgeManagedCandidateIdentity(metadataRoot, realEntry)) {
|
|
203
293
|
candidates.push(realEntry);
|
|
204
294
|
containsManagedDirectory = true;
|
|
205
295
|
continue;
|
|
206
296
|
}
|
|
207
|
-
if (await visit(realEntry)) {
|
|
297
|
+
if (isForgeWorktreeContainer(relativePath) && await visit(realEntry)) {
|
|
208
298
|
containsManagedDirectory = true;
|
|
209
299
|
continue;
|
|
210
300
|
}
|
|
@@ -217,28 +307,7 @@ export class ManagedWorktreeCleanupService {
|
|
|
217
307
|
return { candidates, protectedWorktrees, skippedUnownedDirectories, rootRemovalSafe };
|
|
218
308
|
}
|
|
219
309
|
async readRuntimeLeases() {
|
|
220
|
-
|
|
221
|
-
const entries = await fs.readdir(ledgerRoot, { withFileTypes: true }).catch(error => {
|
|
222
|
-
if (isEnoent(error))
|
|
223
|
-
return [];
|
|
224
|
-
throw error;
|
|
225
|
-
});
|
|
226
|
-
const leases = [];
|
|
227
|
-
for (const entry of entries) {
|
|
228
|
-
if (!entry.name.endsWith(".json"))
|
|
229
|
-
continue;
|
|
230
|
-
if (!entry.isFile() || entry.isSymbolicLink()) {
|
|
231
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entry.name} is not a regular file.`);
|
|
232
|
-
}
|
|
233
|
-
const target = path.join(ledgerRoot, entry.name);
|
|
234
|
-
const value = await readRuntimeLedger(target, entry.name);
|
|
235
|
-
const runtimeLease = isRecord(value) ? value.runtimeLease : null;
|
|
236
|
-
if (!validRuntimeIsolationLease(runtimeLease) || runtimeLease.mode !== "host") {
|
|
237
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entry.name} has no trustworthy host lease.`);
|
|
238
|
-
}
|
|
239
|
-
leases.push(runtimeLease);
|
|
240
|
-
}
|
|
241
|
-
return leases;
|
|
310
|
+
return await readHostRuntimeLeases(this.stateRoot);
|
|
242
311
|
}
|
|
243
312
|
async worktreeMetadataRoot(repoPath) {
|
|
244
313
|
const commonDirRaw = (await this.git.run(repoPath, ["rev-parse", "--git-common-dir"])).trim();
|
|
@@ -246,95 +315,6 @@ export class ManagedWorktreeCleanupService {
|
|
|
246
315
|
return path.join(await fs.realpath(commonDirPath), "worktrees");
|
|
247
316
|
}
|
|
248
317
|
}
|
|
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
|
-
}
|
|
271
|
-
async function readRuntimeLedger(target, entryName) {
|
|
272
|
-
const pathStat = await fs.lstat(target).catch(error => {
|
|
273
|
-
if (isEnoent(error))
|
|
274
|
-
return null;
|
|
275
|
-
throw error;
|
|
276
|
-
});
|
|
277
|
-
if (!pathStat) {
|
|
278
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} disappeared.`);
|
|
279
|
-
}
|
|
280
|
-
validateRuntimeLedgerStat(entryName, pathStat);
|
|
281
|
-
let handle;
|
|
282
|
-
try {
|
|
283
|
-
handle = await fs.open(target, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
|
|
284
|
-
}
|
|
285
|
-
catch (error) {
|
|
286
|
-
if (isEnoent(error) || errorCode(error) === "ELOOP") {
|
|
287
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is not a regular file.`, { cause: error });
|
|
288
|
-
}
|
|
289
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} could not be opened safely.`, { cause: error });
|
|
290
|
-
}
|
|
291
|
-
try {
|
|
292
|
-
const openedStat = await handle.stat();
|
|
293
|
-
const reopenedPathStat = await fs.lstat(target);
|
|
294
|
-
validateRuntimeLedgerStat(entryName, openedStat);
|
|
295
|
-
validateRuntimeLedgerStat(entryName, reopenedPathStat);
|
|
296
|
-
validateSameRuntimeLedgerFile(entryName, pathStat, openedStat);
|
|
297
|
-
validateSameRuntimeLedgerFile(entryName, openedStat, reopenedPathStat);
|
|
298
|
-
const content = await handle.readFile("utf8");
|
|
299
|
-
const afterStat = await handle.stat();
|
|
300
|
-
const afterPathStat = await fs.lstat(target);
|
|
301
|
-
validateRuntimeLedgerStat(entryName, afterStat);
|
|
302
|
-
validateRuntimeLedgerStat(entryName, afterPathStat);
|
|
303
|
-
validateSameRuntimeLedgerFile(entryName, openedStat, afterStat);
|
|
304
|
-
validateSameRuntimeLedgerFile(entryName, afterStat, afterPathStat);
|
|
305
|
-
if (openedStat.size !== afterStat.size || Buffer.byteLength(content, "utf8") !== afterStat.size) {
|
|
306
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} changed while being read.`);
|
|
307
|
-
}
|
|
308
|
-
try {
|
|
309
|
-
return JSON.parse(content);
|
|
310
|
-
}
|
|
311
|
-
catch (error) {
|
|
312
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is invalid.`, { cause: error });
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
finally {
|
|
316
|
-
await handle.close();
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
function validateRuntimeLedgerStat(entryName, stat) {
|
|
320
|
-
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
321
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is not a regular file.`);
|
|
322
|
-
}
|
|
323
|
-
if (stat.size > 256 * 1024) {
|
|
324
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is oversized.`);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
function validateSameRuntimeLedgerFile(entryName, before, after) {
|
|
328
|
-
if (before.dev !== 0 && before.ino !== 0 && after.dev !== 0 && after.ino !== 0
|
|
329
|
-
&& (before.dev !== after.dev || before.ino !== after.ino)) {
|
|
330
|
-
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} changed while being read.`);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
function errorCode(error) {
|
|
334
|
-
return typeof error === "object" && error !== null && "code" in error
|
|
335
|
-
? String(error.code)
|
|
336
|
-
: undefined;
|
|
337
|
-
}
|
|
338
318
|
function emptyResult(worktreeRoot, force) {
|
|
339
319
|
return {
|
|
340
320
|
worktreeRoot: path.resolve(worktreeRoot),
|
|
@@ -466,9 +446,13 @@ function parseWorktreeListing(repoPath, output) {
|
|
|
466
446
|
worktrees.push(current);
|
|
467
447
|
return worktrees;
|
|
468
448
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
449
|
+
// A registered worktree counts as a readiness probe only when Forge made it: its folder sits
|
|
450
|
+
// directly in the managed root under the probe's exact name, and it has the probe's exact branch.
|
|
451
|
+
function isRuntimeReadinessWorktree(worktreeRoot, worktree) {
|
|
452
|
+
const folderName = path.basename(worktree.worktreePath);
|
|
453
|
+
return samePath(path.dirname(worktree.worktreePath), worktreeRoot)
|
|
454
|
+
&& isRuntimeReadinessName(folderName)
|
|
455
|
+
&& worktree.branchName === `refs/heads/${folderName}`;
|
|
472
456
|
}
|
|
473
457
|
function activePaths(worktrees) {
|
|
474
458
|
return worktrees.filter(entry => !entry.prunable).map(entry => entry.worktreePath);
|
|
@@ -493,6 +477,3 @@ function samePath(left, right) {
|
|
|
493
477
|
function isEnoent(error) {
|
|
494
478
|
return error?.code === "ENOENT";
|
|
495
479
|
}
|
|
496
|
-
function isRecord(value) {
|
|
497
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
498
|
-
}
|
|
@@ -13,4 +13,10 @@ export interface CleanupResult {
|
|
|
13
13
|
readonly skippedUnownedDirectories: number;
|
|
14
14
|
readonly removedRoot: boolean;
|
|
15
15
|
readonly dryRun: boolean;
|
|
16
|
+
/** Forge-created local branches deleted by this run: readiness branches, and in merged mode also merged work branches. */
|
|
17
|
+
readonly deletedBranches?: number;
|
|
18
|
+
/** Merged mode only: origin/HEAD was unset or stale and origin had none or several of develop, main and master, so merged work branches were left alone. */
|
|
19
|
+
readonly mergedBaseUnresolved?: boolean;
|
|
20
|
+
/** Stale mode only: stale worktrees kept because their uncommitted work could not be saved first. */
|
|
21
|
+
readonly preservedWorktrees?: number;
|
|
16
22
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
type PathSeparator = typeof path.posix.sep | typeof path.win32.sep;
|
|
3
|
+
/** True when `relativePath` (relative to the managed root) is a worktree path Forge creates. */
|
|
4
|
+
export declare function isForgeOwnedWorktreePath(relativePath: string, separator?: PathSeparator): boolean;
|
|
5
|
+
/** True when `relativePath` is one of the folders Forge groups its worktrees in. */
|
|
6
|
+
export declare function isForgeWorktreeContainer(relativePath: string, separator?: PathSeparator): boolean;
|
|
7
|
+
/** True for a readiness probe's exact folder or branch name: runtime-readiness-<UUID>. */
|
|
8
|
+
export declare function isRuntimeReadinessName(name: string): boolean;
|
|
9
|
+
/** True for a Forge PR review's worktree key, pr-review/<12 hex>-<12 hex>: nothing in it is anyone's work. */
|
|
10
|
+
export declare function isPrReviewWorktreeKey(name: string): boolean;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Cleanup may delete a worktree folder only when Forge itself created it for one of its own
|
|
2
|
+
// attempts. Forge places every such worktree at an exact path under the repository's managed root
|
|
3
|
+
// (`<repo parent>/.forge-worktrees/<repo name>`), built from the lease's worktree key:
|
|
4
|
+
// <kind>/<12 hex> a work item (story, task, bug, feature, work_order) or
|
|
5
|
+
// its Architect phase (architect)
|
|
6
|
+
// decompose/<feature|work_order>/<12 hex> a decompose session
|
|
7
|
+
// pr-review/<12 hex>-<12 hex> a Forge PR review of one head (work item, head SHA); it is
|
|
8
|
+
// detached and review-only, so it holds nothing to save
|
|
9
|
+
// runtime-readiness-<UUID> a runtime readiness probe (session.service.ts)
|
|
10
|
+
// draft-<32 hex> a drafting session (DraftingService on the server)
|
|
11
|
+
// A release that couldn't finish deleting can leave `<one of those>.forge-release-<UUID>` beside it.
|
|
12
|
+
// Anything else under the root, such as a folder a person or another tool made there, is never
|
|
13
|
+
// removed, even if it is a Git worktree of the same repository.
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
const hex12 = "[0-9a-f]{12}";
|
|
16
|
+
const uuid = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
|
|
17
|
+
const releaseSuffix = `(?:\\.forge-release-${uuid})?`;
|
|
18
|
+
const forgeOwnedWorktreePathPattern = new RegExp("^(?:"
|
|
19
|
+
+ `(?:story|task|bug|feature|work_order|architect)/${hex12}`
|
|
20
|
+
+ `|decompose/(?:feature|work_order)/${hex12}`
|
|
21
|
+
+ `|pr-review/${hex12}-${hex12}`
|
|
22
|
+
+ `|runtime-readiness-${uuid}`
|
|
23
|
+
+ "|draft-[0-9a-f]{32}"
|
|
24
|
+
+ `)${releaseSuffix}$`);
|
|
25
|
+
const runtimeReadinessNamePattern = new RegExp(`^runtime-readiness-${uuid}$`);
|
|
26
|
+
const prReviewWorktreeKeyPattern = new RegExp(`^pr-review/${hex12}-${hex12}$`);
|
|
27
|
+
// Folders that only group Forge's worktrees; cleanup may look inside them but never removes them.
|
|
28
|
+
const forgeWorktreeContainers = new Set([
|
|
29
|
+
"story",
|
|
30
|
+
"task",
|
|
31
|
+
"bug",
|
|
32
|
+
"feature",
|
|
33
|
+
"work_order",
|
|
34
|
+
"architect",
|
|
35
|
+
"decompose",
|
|
36
|
+
"decompose/feature",
|
|
37
|
+
"decompose/work_order",
|
|
38
|
+
"pr-review",
|
|
39
|
+
]);
|
|
40
|
+
// Split only on the separators the filesystem itself uses. On POSIX a backslash is an ordinary
|
|
41
|
+
// filename character, so a folder a person named `story\0123456789ab` must not read as Forge's
|
|
42
|
+
// `story/0123456789ab`. Windows accepts both `\` and `/`.
|
|
43
|
+
function normalizeRelativePath(relativePath, separator) {
|
|
44
|
+
const separators = separator === path.win32.sep ? /[\\/]+/u : /\/+/u;
|
|
45
|
+
return relativePath.split(separators).filter(segment => segment.length > 0).join("/");
|
|
46
|
+
}
|
|
47
|
+
/** True when `relativePath` (relative to the managed root) is a worktree path Forge creates. */
|
|
48
|
+
export function isForgeOwnedWorktreePath(relativePath, separator = path.sep) {
|
|
49
|
+
return forgeOwnedWorktreePathPattern.test(normalizeRelativePath(relativePath, separator));
|
|
50
|
+
}
|
|
51
|
+
/** True when `relativePath` is one of the folders Forge groups its worktrees in. */
|
|
52
|
+
export function isForgeWorktreeContainer(relativePath, separator = path.sep) {
|
|
53
|
+
return forgeWorktreeContainers.has(normalizeRelativePath(relativePath, separator));
|
|
54
|
+
}
|
|
55
|
+
/** True for a readiness probe's exact folder or branch name: runtime-readiness-<UUID>. */
|
|
56
|
+
export function isRuntimeReadinessName(name) {
|
|
57
|
+
return runtimeReadinessNamePattern.test(name);
|
|
58
|
+
}
|
|
59
|
+
/** True for a Forge PR review's worktree key, pr-review/<12 hex>-<12 hex>: nothing in it is anyone's work. */
|
|
60
|
+
export function isPrReviewWorktreeKey(name) {
|
|
61
|
+
return prReviewWorktreeKeyPattern.test(name);
|
|
62
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type RuntimeIsolationLease } from "../runtime/runtime-isolation-lease.js";
|
|
2
|
+
/** Reads every host runtime lease, or throws if any ledger file can't be trusted. */
|
|
3
|
+
export declare function readHostRuntimeLeases(stateRoot: string): Promise<readonly RuntimeIsolationLease[]>;
|
|
4
|
+
/** Branches that a lease which isn't released yet may still use. */
|
|
5
|
+
export declare function liveLeaseBranches(leases: readonly RuntimeIsolationLease[]): ReadonlySet<string>;
|
|
6
|
+
/** A runtime lease's worktree path, resolved; throws when it is redirected or changes while being resolved. */
|
|
7
|
+
export declare function resolveRuntimeLeasePath(leasePath: string): Promise<string>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { constants as fsConstants } from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { validRuntimeIsolationLease } from "../runtime/runtime-isolation-lease.js";
|
|
5
|
+
import { fileIdentityChanged } from "../shared/fs/file-identity.js";
|
|
6
|
+
import { resolvePathIdentity } from "../shared/path/path.js";
|
|
7
|
+
// The host runtime ledger (<state root>/runtime-ledger/host/*.json) records every runtime lease this
|
|
8
|
+
// bridge holds. Cleanup reads it to keep live leases' worktrees and branches; any ledger it can't
|
|
9
|
+
// trust stops the cleanup rather than guessing.
|
|
10
|
+
/** Reads every host runtime lease, or throws if any ledger file can't be trusted. */
|
|
11
|
+
export async function readHostRuntimeLeases(stateRoot) {
|
|
12
|
+
const ledgerRoot = path.join(stateRoot, "runtime-ledger", "host");
|
|
13
|
+
const entries = await fs.readdir(ledgerRoot, { withFileTypes: true }).catch(error => {
|
|
14
|
+
if (isEnoent(error))
|
|
15
|
+
return [];
|
|
16
|
+
throw error;
|
|
17
|
+
});
|
|
18
|
+
const leases = [];
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
if (!entry.name.endsWith(".json"))
|
|
21
|
+
continue;
|
|
22
|
+
if (!entry.isFile() || entry.isSymbolicLink()) {
|
|
23
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entry.name} is not a regular file.`);
|
|
24
|
+
}
|
|
25
|
+
const target = path.join(ledgerRoot, entry.name);
|
|
26
|
+
const value = await readRuntimeLedger(target, entry.name);
|
|
27
|
+
const runtimeLease = isRecord(value) ? value.runtimeLease : null;
|
|
28
|
+
if (!validRuntimeIsolationLease(runtimeLease) || runtimeLease.mode !== "host") {
|
|
29
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entry.name} has no trustworthy host lease.`);
|
|
30
|
+
}
|
|
31
|
+
leases.push(runtimeLease);
|
|
32
|
+
}
|
|
33
|
+
return leases;
|
|
34
|
+
}
|
|
35
|
+
async function readRuntimeLedger(target, entryName) {
|
|
36
|
+
const pathStat = await fs.lstat(target, { bigint: true }).catch(error => {
|
|
37
|
+
if (isEnoent(error))
|
|
38
|
+
return null;
|
|
39
|
+
throw error;
|
|
40
|
+
});
|
|
41
|
+
if (!pathStat) {
|
|
42
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} disappeared.`);
|
|
43
|
+
}
|
|
44
|
+
validateRuntimeLedgerStat(entryName, pathStat);
|
|
45
|
+
let handle;
|
|
46
|
+
try {
|
|
47
|
+
handle = await fs.open(target, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
if (isEnoent(error) || errorCode(error) === "ELOOP") {
|
|
51
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is not a regular file.`, { cause: error });
|
|
52
|
+
}
|
|
53
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} could not be opened safely.`, { cause: error });
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const openedStat = await handle.stat({ bigint: true });
|
|
57
|
+
const reopenedPathStat = await fs.lstat(target, { bigint: true });
|
|
58
|
+
validateRuntimeLedgerStat(entryName, openedStat);
|
|
59
|
+
validateRuntimeLedgerStat(entryName, reopenedPathStat);
|
|
60
|
+
validateSameRuntimeLedgerFile(entryName, pathStat, openedStat);
|
|
61
|
+
validateSameRuntimeLedgerFile(entryName, openedStat, reopenedPathStat);
|
|
62
|
+
const content = await handle.readFile("utf8");
|
|
63
|
+
const afterStat = await handle.stat({ bigint: true });
|
|
64
|
+
const afterPathStat = await fs.lstat(target, { bigint: true });
|
|
65
|
+
validateRuntimeLedgerStat(entryName, afterStat);
|
|
66
|
+
validateRuntimeLedgerStat(entryName, afterPathStat);
|
|
67
|
+
validateSameRuntimeLedgerFile(entryName, openedStat, afterStat);
|
|
68
|
+
validateSameRuntimeLedgerFile(entryName, afterStat, afterPathStat);
|
|
69
|
+
if (openedStat.size !== afterStat.size || BigInt(Buffer.byteLength(content, "utf8")) !== afterStat.size) {
|
|
70
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} changed while being read.`);
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return JSON.parse(content);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is invalid.`, { cause: error });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
await handle.close();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function validateRuntimeLedgerStat(entryName, stat) {
|
|
84
|
+
if (stat.isSymbolicLink() || !stat.isFile()) {
|
|
85
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is not a regular file.`);
|
|
86
|
+
}
|
|
87
|
+
if (stat.size > 256n * 1024n) {
|
|
88
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} is oversized.`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function validateSameRuntimeLedgerFile(entryName, before, after) {
|
|
92
|
+
if (fileIdentityChanged(before, after)) {
|
|
93
|
+
throw new Error(`Refusing managed worktree cleanup because runtime ledger ${entryName} changed while being read.`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function errorCode(error) {
|
|
97
|
+
return typeof error === "object" && error !== null && "code" in error
|
|
98
|
+
? String(error.code)
|
|
99
|
+
: undefined;
|
|
100
|
+
}
|
|
101
|
+
/** Branches that a lease which isn't released yet may still use. */
|
|
102
|
+
export function liveLeaseBranches(leases) {
|
|
103
|
+
return new Set(leases.filter(lease => lease.state !== "released").map(lease => lease.worktreeKey));
|
|
104
|
+
}
|
|
105
|
+
function isEnoent(error) {
|
|
106
|
+
return error?.code === "ENOENT";
|
|
107
|
+
}
|
|
108
|
+
function isRecord(value) {
|
|
109
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
110
|
+
}
|
|
111
|
+
/** A runtime lease's worktree path, resolved; throws when it is redirected or changes while being resolved. */
|
|
112
|
+
export async function resolveRuntimeLeasePath(leasePath) {
|
|
113
|
+
const resolved = path.resolve(leasePath);
|
|
114
|
+
const before = await fs.lstat(resolved, { bigint: true }).catch(error => {
|
|
115
|
+
if (isEnoent(error))
|
|
116
|
+
return null;
|
|
117
|
+
throw error;
|
|
118
|
+
});
|
|
119
|
+
if (!before)
|
|
120
|
+
return await resolvePathIdentity(resolved);
|
|
121
|
+
if (before.isSymbolicLink() || !before.isDirectory()) {
|
|
122
|
+
throw new Error(`Refusing managed worktree cleanup because runtime lease path ${resolved} is redirected or is not a directory.`);
|
|
123
|
+
}
|
|
124
|
+
const real = await fs.realpath(resolved);
|
|
125
|
+
const after = await fs.lstat(resolved, { bigint: true });
|
|
126
|
+
if (after.isSymbolicLink() || !after.isDirectory() || fileIdentityChanged(before, after)) {
|
|
127
|
+
throw new Error(`Refusing managed worktree cleanup because runtime lease path ${resolved} changed while being resolved.`);
|
|
128
|
+
}
|
|
129
|
+
return real;
|
|
130
|
+
}
|