@loucompanion/forge-bridge 0.1.3-dev.f66395c3ac21 → 0.1.4-dev.bfa11ebe98c6
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 +165 -1
- package/dist/bridge/bridge-chat.types.d.ts +111 -0
- package/dist/bridge/bridge-chat.types.js +401 -0
- package/dist/bridge/bridge.base.d.ts +9 -1
- package/dist/bridge/bridge.service.d.ts +129 -3
- package/dist/bridge/bridge.service.js +2426 -70
- package/dist/bridge/bridge.types.d.ts +218 -2
- package/dist/bridge/dispatch-result-outbox.d.ts +25 -0
- package/dist/bridge/dispatch-result-outbox.js +62 -0
- package/dist/bridge/dispatch-result-worker.d.ts +2 -0
- package/dist/bridge/dispatch-result-worker.js +122 -0
- package/dist/bridge/internals.d.ts +2 -0
- package/dist/bridge/internals.js +1 -0
- package/dist/bridge/readiness-result-outbox.d.ts +21 -0
- package/dist/bridge/readiness-result-outbox.js +54 -0
- package/dist/bridge/readiness-result-worker.d.ts +2 -0
- package/dist/bridge/readiness-result-worker.js +121 -0
- package/dist/bridge/runner-host-capabilities.d.ts +15 -0
- package/dist/bridge/runner-host-capabilities.js +93 -0
- package/dist/cleanup/cleanup.service.d.ts +14 -2
- package/dist/cleanup/cleanup.service.js +496 -3
- package/dist/cleanup/cleanup.types.d.ts +10 -0
- package/dist/cleanup/index.d.ts +1 -1
- package/dist/cleanup/index.js +1 -1
- package/dist/cleanup/internals.d.ts +2 -2
- package/dist/cleanup/internals.js +1 -1
- package/dist/cli/cli.help.d.ts +2 -0
- package/dist/cli/cli.help.js +217 -0
- package/dist/cli/cli.service.d.ts +55 -7
- package/dist/cli/cli.service.js +429 -219
- package/dist/cli/local-config.d.ts +7 -0
- package/dist/cli/local-config.js +51 -3
- package/dist/runtime/aspnet-runtime-prebuild.d.ts +36 -0
- package/dist/runtime/aspnet-runtime-prebuild.js +341 -0
- package/dist/runtime/attempt-node-runtime-launcher.d.ts +18 -0
- package/dist/runtime/attempt-node-runtime-launcher.js +140 -0
- package/dist/runtime/attempt-path-operation.d.ts +21 -0
- package/dist/runtime/attempt-path-operation.js +94 -0
- package/dist/runtime/attempt-private-directory.d.ts +6 -0
- package/dist/runtime/attempt-private-directory.js +54 -0
- package/dist/runtime/attempt-process-supervisor-registry.d.ts +20 -0
- package/dist/runtime/attempt-process-supervisor-registry.js +64 -0
- package/dist/runtime/attempt-process-supervisor.d.ts +97 -0
- package/dist/runtime/attempt-process-supervisor.js +397 -0
- package/dist/runtime/canonical-source-file.d.ts +3 -0
- package/dist/runtime/canonical-source-file.js +54 -0
- package/dist/runtime/docker-engine-probe.d.ts +90 -0
- package/dist/runtime/docker-engine-probe.js +333 -0
- package/dist/runtime/generated-output-reset.d.ts +20 -0
- package/dist/runtime/generated-output-reset.js +157 -0
- package/dist/runtime/host-database-capability-boundary.d.ts +16 -0
- package/dist/runtime/host-database-capability-boundary.js +62 -0
- package/dist/runtime/host-diagnostics.d.ts +11 -0
- package/dist/runtime/host-diagnostics.js +191 -0
- package/dist/runtime/host-execution-root.d.ts +4 -0
- package/dist/runtime/host-execution-root.js +39 -0
- package/dist/runtime/host-listener-inspector.d.ts +51 -0
- package/dist/runtime/host-listener-inspector.js +385 -0
- package/dist/runtime/host-port-broker.d.ts +76 -0
- package/dist/runtime/host-port-broker.js +333 -0
- package/dist/runtime/host-runtime-contract.d.ts +58 -0
- package/dist/runtime/host-runtime-contract.js +754 -0
- package/dist/runtime/host-runtime-lease.d.ts +215 -0
- package/dist/runtime/host-runtime-lease.js +2449 -0
- package/dist/runtime/host-runtime-promotion.d.ts +32 -0
- package/dist/runtime/host-runtime-promotion.js +206 -0
- package/dist/runtime/memory-capacity.d.ts +70 -0
- package/dist/runtime/memory-capacity.js +123 -0
- package/dist/runtime/native-test-postgres-fallback.d.ts +25 -0
- package/dist/runtime/native-test-postgres-fallback.js +91 -0
- package/dist/runtime/node-host-runtime-adapter.d.ts +54 -0
- package/dist/runtime/node-host-runtime-adapter.js +285 -0
- package/dist/runtime/pinned-directory-process.d.ts +9 -0
- package/dist/runtime/pinned-directory-process.js +61 -0
- package/dist/runtime/pinned-source-directory.d.ts +17 -0
- package/dist/runtime/pinned-source-directory.js +94 -0
- package/dist/runtime/playwright-host-runtime-adapter.d.ts +21 -0
- package/dist/runtime/playwright-host-runtime-adapter.js +268 -0
- package/dist/runtime/prepared-runtime-identity.d.ts +6 -0
- package/dist/runtime/prepared-runtime-identity.js +218 -0
- package/dist/runtime/prepared-runtime-readiness.d.ts +30 -0
- package/dist/runtime/prepared-runtime-readiness.js +285 -0
- package/dist/runtime/runtime-isolation-lease.d.ts +67 -0
- package/dist/runtime/runtime-isolation-lease.js +161 -0
- package/dist/runtime/workspace-resource-estimator.d.ts +17 -0
- package/dist/runtime/workspace-resource-estimator.js +174 -0
- package/dist/sandbox/command-runner.d.ts +4 -0
- package/dist/sandbox/command-runner.js +47 -0
- package/dist/sandbox/contract.d.ts +5 -0
- package/dist/sandbox/contract.js +276 -0
- package/dist/sandbox/event-budget.d.ts +11 -0
- package/dist/sandbox/event-budget.js +27 -0
- package/dist/sandbox/image-policy.d.ts +35 -0
- package/dist/sandbox/image-policy.js +247 -0
- package/dist/sandbox/index.d.ts +5 -0
- package/dist/sandbox/index.js +5 -0
- package/dist/sandbox/lease-watchdog.d.ts +18 -0
- package/dist/sandbox/lease-watchdog.js +67 -0
- package/dist/sandbox/preflight.d.ts +84 -0
- package/dist/sandbox/preflight.js +521 -0
- package/dist/sandbox/process.d.ts +20 -0
- package/dist/sandbox/process.js +209 -0
- package/dist/sandbox/provider-auth/codex.d.ts +26 -0
- package/dist/sandbox/provider-auth/codex.js +212 -0
- package/dist/sandbox/repository-lock.d.ts +6 -0
- package/dist/sandbox/repository-lock.js +128 -0
- package/dist/sandbox/runtime.d.ts +302 -0
- package/dist/sandbox/runtime.js +2367 -0
- package/dist/sandbox/sandbox.types.d.ts +93 -0
- package/dist/sandbox/sandbox.types.js +1 -0
- package/dist/sandbox/smoke.d.ts +76 -0
- package/dist/sandbox/smoke.js +380 -0
- package/dist/sandbox/worker-supervisor.d.ts +13 -0
- package/dist/sandbox/worker-supervisor.js +36 -0
- package/dist/sandbox/worker.d.ts +1 -0
- package/dist/sandbox/worker.js +225 -0
- package/dist/sandbox/workspace.service.d.ts +13 -0
- package/dist/sandbox/workspace.service.js +74 -0
- package/dist/session/managed-attempt-tools.d.ts +28 -0
- package/dist/session/managed-attempt-tools.js +1011 -0
- package/dist/session/managed-build-coordinator.d.ts +5 -0
- package/dist/session/managed-build-coordinator.js +105 -0
- package/dist/session/managed-build-receipt.d.ts +21 -0
- package/dist/session/managed-build-receipt.js +337 -0
- package/dist/session/managed-verification-timeouts.d.ts +8 -0
- package/dist/session/managed-verification-timeouts.js +17 -0
- package/dist/session/phase-completion.artifacts.d.ts +27 -0
- package/dist/session/phase-completion.artifacts.js +829 -0
- package/dist/session/phase-completion.d.ts +53 -42
- package/dist/session/phase-completion.helpers.d.ts +117 -0
- package/dist/session/phase-completion.helpers.js +707 -0
- package/dist/session/phase-completion.js +125 -3007
- package/dist/session/phase-completion.prompts.d.ts +14 -0
- package/dist/session/phase-completion.prompts.js +233 -0
- package/dist/session/phase-completion.runtime.d.ts +49 -0
- package/dist/session/phase-completion.runtime.js +833 -0
- package/dist/session/phase-completion.schemas.d.ts +3 -0
- package/dist/session/phase-completion.schemas.js +859 -0
- package/dist/session/phase-completion.validation.d.ts +38 -0
- package/dist/session/phase-completion.validation.js +918 -0
- package/dist/session/pre-publication-validation.d.ts +75 -0
- package/dist/session/pre-publication-validation.js +657 -0
- package/dist/session/provider-cli/bridge-cli.conformance-fixtures.d.ts +72 -0
- package/dist/session/provider-cli/bridge-cli.conformance-fixtures.js +462 -0
- package/dist/session/provider-cli/ci-repair-readiness.d.ts +16 -0
- package/dist/session/provider-cli/ci-repair-readiness.js +110 -0
- package/dist/session/provider-cli/provider-cli.artifacts.d.ts +95 -0
- package/dist/session/provider-cli/provider-cli.artifacts.js +812 -0
- package/dist/session/provider-cli/provider-cli.base.d.ts +1 -0
- package/dist/session/provider-cli/provider-cli.deterministic-payloads.d.ts +12 -0
- package/dist/session/provider-cli/provider-cli.deterministic-payloads.js +279 -0
- package/dist/session/provider-cli/provider-cli.deterministic.d.ts +14 -0
- package/dist/session/provider-cli/provider-cli.deterministic.js +407 -0
- package/dist/session/provider-cli/provider-cli.process.d.ts +99 -0
- package/dist/session/provider-cli/provider-cli.process.js +874 -0
- package/dist/session/provider-cli/provider-cli.prompts.d.ts +52 -0
- package/dist/session/provider-cli/provider-cli.prompts.js +715 -0
- package/dist/session/provider-cli/provider-cli.qa-sidecars.d.ts +11 -0
- package/dist/session/provider-cli/provider-cli.qa-sidecars.js +66 -0
- package/dist/session/provider-cli/provider-cli.repair-epoch.d.ts +6 -0
- package/dist/session/provider-cli/provider-cli.repair-epoch.js +11 -0
- package/dist/session/provider-cli/provider-cli.runtime.d.ts +16 -0
- package/dist/session/provider-cli/provider-cli.runtime.js +78 -0
- package/dist/session/provider-cli/provider-cli.service.d.ts +47 -53
- package/dist/session/provider-cli/provider-cli.service.js +1257 -2497
- package/dist/session/provider-cli/provider-cli.types.d.ts +8 -0
- package/dist/session/provider-cli/provider-output-state.d.ts +32 -0
- package/dist/session/provider-cli/provider-output-state.js +817 -0
- package/dist/session/provider-cli/provider-usage.d.ts +7 -0
- package/dist/session/provider-cli/provider-usage.js +28 -5
- package/dist/session/session-redaction.d.ts +3 -0
- package/dist/session/session-redaction.js +448 -0
- package/dist/session/session.base.d.ts +6 -1
- package/dist/session/session.service.d.ts +8 -2
- package/dist/session/session.service.js +586 -59
- package/dist/session/session.types.d.ts +324 -3
- package/dist/session/session.types.js +939 -10
- package/dist/session/unreferenced-exports.d.ts +12 -0
- package/dist/session/unreferenced-exports.js +59 -0
- package/dist/shared/format/format-bytes.d.ts +1 -0
- package/dist/shared/format/format-bytes.js +9 -0
- package/dist/shared/git/git.js +2 -1
- package/dist/shared/path/path.d.ts +1 -0
- package/dist/shared/path/path.js +21 -1
- package/dist/shared/process/dotnet-toolchain.d.ts +17 -0
- package/dist/shared/process/dotnet-toolchain.js +307 -0
- package/dist/shared/process/host-process-recovery.d.ts +12 -0
- package/dist/shared/process/host-process-recovery.js +78 -0
- package/dist/shared/process/host-process-supervisor.d.ts +59 -0
- package/dist/shared/process/host-process-supervisor.js +330 -0
- package/dist/shared/process/process.d.ts +3 -0
- package/dist/shared/process/process.js +13 -7
- package/dist/shared/process/windows-launch.d.ts +8 -0
- package/dist/shared/process/windows-launch.js +41 -0
- package/dist/shared/provider-capabilities.d.ts +4 -0
- package/dist/shared/provider-capabilities.js +88 -0
- package/dist/shared/redaction/redaction.d.ts +1 -0
- package/dist/shared/redaction/redaction.js +47 -4
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/worktree/artifact-injector.d.ts +3 -2
- package/dist/worktree/artifact-injector.js +82 -10
- package/dist/worktree/dependency-warmer.d.ts +53 -1
- package/dist/worktree/dependency-warmer.js +619 -11
- package/dist/worktree/index.d.ts +2 -1
- package/dist/worktree/index.js +1 -0
- package/dist/worktree/prior-phase-artifact-hydrator.d.ts +2 -0
- package/dist/worktree/prior-phase-artifact-hydrator.js +136 -0
- package/dist/worktree/story-qa-reference-hydrator.d.ts +4 -0
- package/dist/worktree/story-qa-reference-hydrator.js +253 -0
- package/dist/worktree/worktree-paths.d.ts +4 -0
- package/dist/worktree/worktree-paths.js +30 -0
- package/dist/worktree/worktree.base.d.ts +3 -0
- package/dist/worktree/worktree.service.d.ts +16 -3
- package/dist/worktree/worktree.service.js +334 -54
- package/dist/worktree/worktree.types.d.ts +9 -1
- package/package.json +12 -7
- package/runtime/windows-job-supervisor.ps1 +722 -0
- package/runtime/windows-listener-owner.ps1 +33 -0
|
@@ -1,5 +1,498 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { constants as fsConstants } from "node:fs";
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { RepositoryCoordinatorLock } from "../sandbox/repository-lock.js";
|
|
6
|
+
import { GitClient } from "../shared/git/git.js";
|
|
7
|
+
import { isSubPath, resolveExistingDirectoryPath, resolvePathIdentity } from "../shared/path/path.js";
|
|
8
|
+
import { validRuntimeIsolationLease } from "../runtime/runtime-isolation-lease.js";
|
|
9
|
+
import { defaultWorktreeCoordinatorStateRoot, resolveManagedWorktreeRootForRepo, } from "../worktree/worktree-paths.js";
|
|
10
|
+
const staleRuntimeReadinessAgeMs = 30 * 60 * 1000;
|
|
11
|
+
export class ManagedWorktreeCleanupService {
|
|
12
|
+
stateRoot;
|
|
13
|
+
git;
|
|
14
|
+
coordinator;
|
|
15
|
+
constructor(stateRoot = defaultWorktreeCoordinatorStateRoot(), git = new GitClient(), coordinator = new RepositoryCoordinatorLock(stateRoot)) {
|
|
16
|
+
this.stateRoot = stateRoot;
|
|
17
|
+
this.git = git;
|
|
18
|
+
this.coordinator = coordinator;
|
|
4
19
|
}
|
|
20
|
+
async run(request) {
|
|
21
|
+
const [repoPath, allowedRepoRoot] = await Promise.all([
|
|
22
|
+
resolveExistingDirectoryPath(request.repoRoot),
|
|
23
|
+
request.allowedRepoRoot
|
|
24
|
+
? resolvePathIdentity(request.allowedRepoRoot)
|
|
25
|
+
: Promise.resolve(undefined),
|
|
26
|
+
]);
|
|
27
|
+
const worktreeRoot = resolveManagedWorktreeRootForRepo(repoPath, allowedRepoRoot);
|
|
28
|
+
return await this.coordinator.withLock(repoPath, async () => {
|
|
29
|
+
return await this.runLocked(repoPath, worktreeRoot, request);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async runLocked(repoPath, worktreeRoot, request) {
|
|
33
|
+
if (request.mode === "merged") {
|
|
34
|
+
if (request.force)
|
|
35
|
+
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
36
|
+
return emptyResult(worktreeRoot, request.force);
|
|
37
|
+
}
|
|
38
|
+
const safeRoot = await resolveSafeManagedRoot(worktreeRoot);
|
|
39
|
+
if (!safeRoot) {
|
|
40
|
+
if (request.force)
|
|
41
|
+
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
42
|
+
return emptyResult(worktreeRoot, request.force);
|
|
43
|
+
}
|
|
44
|
+
const [listedWorktrees, runtimeLeases] = await Promise.all([
|
|
45
|
+
this.listWorktrees(repoPath),
|
|
46
|
+
this.readRuntimeLeases(),
|
|
47
|
+
]);
|
|
48
|
+
const activeRuntimePaths = await Promise.all(runtimeLeases
|
|
49
|
+
.filter(lease => lease.state !== "released")
|
|
50
|
+
.map(lease => resolveRuntimeLeasePath(lease.worktreePath)));
|
|
51
|
+
const metadataRoot = await this.worktreeMetadataRoot(repoPath);
|
|
52
|
+
const staleReadinessPaths = await this.staleRuntimeReadinessPaths(listedWorktrees, activeRuntimePaths);
|
|
53
|
+
const activeWorktrees = activePaths(listedWorktrees)
|
|
54
|
+
.filter(activePath => !staleReadinessPaths.some(stalePath => samePath(stalePath, activePath)));
|
|
55
|
+
if (request.mode === "all") {
|
|
56
|
+
const protectedPaths = uniquePaths([
|
|
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
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const scan = await this.scanManagedCandidates(metadataRoot, safeRoot, activeWorktrees, activeRuntimePaths);
|
|
82
|
+
if (!request.force) {
|
|
83
|
+
return {
|
|
84
|
+
worktreeRoot: safeRoot,
|
|
85
|
+
removedWorktrees: 0,
|
|
86
|
+
candidateWorktrees: scan.candidates.length,
|
|
87
|
+
protectedWorktrees: scan.protectedWorktrees,
|
|
88
|
+
skippedUnownedDirectories: scan.skippedUnownedDirectories,
|
|
89
|
+
removedRoot: false,
|
|
90
|
+
dryRun: true,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
let removedWorktrees = 0;
|
|
94
|
+
for (const candidate of scan.candidates) {
|
|
95
|
+
const [currentWorktrees, currentLeases] = await Promise.all([
|
|
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") {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const candidateIdentity = await forgeManagedCandidateIdentity(metadataRoot, candidate);
|
|
112
|
+
if (!candidateIdentity) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (await quarantineAndRemoveCandidate(safeRoot, candidate, candidateIdentity)) {
|
|
116
|
+
removedWorktrees += 1;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
await this.git.run(repoPath, ["worktree", "prune"]);
|
|
120
|
+
return {
|
|
121
|
+
worktreeRoot: safeRoot,
|
|
122
|
+
removedWorktrees,
|
|
123
|
+
candidateWorktrees: scan.candidates.length,
|
|
124
|
+
protectedWorktrees: scan.protectedWorktrees,
|
|
125
|
+
skippedUnownedDirectories: scan.skippedUnownedDirectories,
|
|
126
|
+
removedRoot: false,
|
|
127
|
+
dryRun: false,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
async listWorktrees(repoPath) {
|
|
131
|
+
const output = await this.git.run(repoPath, ["worktree", "list", "--porcelain", "-z"]);
|
|
132
|
+
return parseWorktreeListing(repoPath, output);
|
|
133
|
+
}
|
|
134
|
+
async staleRuntimeReadinessPaths(worktrees, runtimePaths) {
|
|
135
|
+
const stale = [];
|
|
136
|
+
for (const worktree of worktrees) {
|
|
137
|
+
if (worktree.prunable || !isRuntimeReadinessWorktree(worktree))
|
|
138
|
+
continue;
|
|
139
|
+
if (worktreePathState(worktree.worktreePath, runtimePaths) !== "none")
|
|
140
|
+
continue;
|
|
141
|
+
const stat = await fs.stat(worktree.worktreePath).catch(error => {
|
|
142
|
+
if (isEnoent(error))
|
|
143
|
+
return null;
|
|
144
|
+
throw error;
|
|
145
|
+
});
|
|
146
|
+
if (stat && Date.now() - stat.mtimeMs >= staleRuntimeReadinessAgeMs) {
|
|
147
|
+
stale.push(worktree.worktreePath);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return stale;
|
|
151
|
+
}
|
|
152
|
+
async scanManagedCandidates(metadataRoot, worktreeRoot, activeWorktrees, runtimePaths) {
|
|
153
|
+
const candidates = [];
|
|
154
|
+
let protectedWorktrees = 0;
|
|
155
|
+
let skippedUnownedDirectories = 0;
|
|
156
|
+
const visit = async (currentPath, root = false) => {
|
|
157
|
+
const entries = await fs.readdir(currentPath, { withFileTypes: true }).catch(error => {
|
|
158
|
+
if (isEnoent(error))
|
|
159
|
+
return [];
|
|
160
|
+
throw error;
|
|
161
|
+
});
|
|
162
|
+
let containsManagedDirectory = false;
|
|
163
|
+
let containsUnownedContent = false;
|
|
164
|
+
for (const entry of entries) {
|
|
165
|
+
if (entry.name === ".forge-cleanup-quarantine") {
|
|
166
|
+
containsUnownedContent = true;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const entryPath = path.join(currentPath, entry.name);
|
|
170
|
+
const stat = await fs.lstat(entryPath).catch(error => {
|
|
171
|
+
if (isEnoent(error))
|
|
172
|
+
return null;
|
|
173
|
+
throw error;
|
|
174
|
+
});
|
|
175
|
+
if (!stat)
|
|
176
|
+
continue;
|
|
177
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
178
|
+
containsUnownedContent = true;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const realEntry = await fs.realpath(entryPath).catch(error => {
|
|
182
|
+
if (isEnoent(error))
|
|
183
|
+
return null;
|
|
184
|
+
throw error;
|
|
185
|
+
});
|
|
186
|
+
if (!realEntry)
|
|
187
|
+
continue;
|
|
188
|
+
if (!isSubPath(worktreeRoot, realEntry)) {
|
|
189
|
+
throw new Error(`Managed worktree cleanup path escaped ${worktreeRoot}: ${realEntry}.`);
|
|
190
|
+
}
|
|
191
|
+
const activeState = worktreePathState(realEntry, activeWorktrees);
|
|
192
|
+
const runtimeState = worktreePathState(realEntry, runtimePaths);
|
|
193
|
+
if (activeState === "active" || runtimeState === "active") {
|
|
194
|
+
protectedWorktrees += 1;
|
|
195
|
+
containsManagedDirectory = true;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (activeState === "ancestor" || runtimeState === "ancestor") {
|
|
199
|
+
containsManagedDirectory = await visit(realEntry) || containsManagedDirectory;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (await forgeManagedCandidateIdentity(metadataRoot, realEntry)) {
|
|
203
|
+
candidates.push(realEntry);
|
|
204
|
+
containsManagedDirectory = true;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (await visit(realEntry)) {
|
|
208
|
+
containsManagedDirectory = true;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
skippedUnownedDirectories += 1;
|
|
212
|
+
containsUnownedContent = true;
|
|
213
|
+
}
|
|
214
|
+
return !containsUnownedContent && (root || containsManagedDirectory);
|
|
215
|
+
};
|
|
216
|
+
const rootRemovalSafe = await visit(worktreeRoot, true);
|
|
217
|
+
return { candidates, protectedWorktrees, skippedUnownedDirectories, rootRemovalSafe };
|
|
218
|
+
}
|
|
219
|
+
async readRuntimeLeases() {
|
|
220
|
+
const ledgerRoot = path.join(this.stateRoot, "runtime-ledger", "host");
|
|
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;
|
|
242
|
+
}
|
|
243
|
+
async worktreeMetadataRoot(repoPath) {
|
|
244
|
+
const commonDirRaw = (await this.git.run(repoPath, ["rev-parse", "--git-common-dir"])).trim();
|
|
245
|
+
const commonDirPath = path.isAbsolute(commonDirRaw) ? commonDirRaw : path.resolve(repoPath, commonDirRaw);
|
|
246
|
+
return path.join(await fs.realpath(commonDirPath), "worktrees");
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async function resolveRuntimeLeasePath(leasePath) {
|
|
250
|
+
const resolved = path.resolve(leasePath);
|
|
251
|
+
const before = await fs.lstat(resolved).catch(error => {
|
|
252
|
+
if (isEnoent(error))
|
|
253
|
+
return null;
|
|
254
|
+
throw error;
|
|
255
|
+
});
|
|
256
|
+
if (!before)
|
|
257
|
+
return await resolvePathIdentity(resolved);
|
|
258
|
+
if (before.isSymbolicLink() || !before.isDirectory()) {
|
|
259
|
+
throw new Error(`Refusing managed worktree cleanup because runtime lease path ${resolved} is redirected or is not a directory.`);
|
|
260
|
+
}
|
|
261
|
+
const real = await fs.realpath(resolved);
|
|
262
|
+
const after = await fs.lstat(resolved);
|
|
263
|
+
if (after.isSymbolicLink()
|
|
264
|
+
|| !after.isDirectory()
|
|
265
|
+
|| (before.dev !== 0 && before.ino !== 0 && after.dev !== 0 && after.ino !== 0
|
|
266
|
+
&& (before.dev !== after.dev || before.ino !== after.ino))) {
|
|
267
|
+
throw new Error(`Refusing managed worktree cleanup because runtime lease path ${resolved} changed while being resolved.`);
|
|
268
|
+
}
|
|
269
|
+
return real;
|
|
270
|
+
}
|
|
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
|
+
function emptyResult(worktreeRoot, force) {
|
|
339
|
+
return {
|
|
340
|
+
worktreeRoot: path.resolve(worktreeRoot),
|
|
341
|
+
removedWorktrees: 0,
|
|
342
|
+
candidateWorktrees: 0,
|
|
343
|
+
protectedWorktrees: 0,
|
|
344
|
+
skippedUnownedDirectories: 0,
|
|
345
|
+
removedRoot: false,
|
|
346
|
+
dryRun: !force,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
async function resolveSafeManagedRoot(worktreeRoot) {
|
|
350
|
+
const rootPath = path.resolve(worktreeRoot);
|
|
351
|
+
const rootStat = await fs.lstat(rootPath).catch(error => {
|
|
352
|
+
if (isEnoent(error))
|
|
353
|
+
return null;
|
|
354
|
+
throw error;
|
|
355
|
+
});
|
|
356
|
+
if (!rootStat)
|
|
357
|
+
return null;
|
|
358
|
+
if (rootStat.isSymbolicLink() || !rootStat.isDirectory()) {
|
|
359
|
+
throw new Error(`Refusing cleanup for redirected or non-directory managed worktree root ${rootPath}.`);
|
|
360
|
+
}
|
|
361
|
+
const parentPath = path.dirname(rootPath);
|
|
362
|
+
const parentStat = await fs.lstat(parentPath);
|
|
363
|
+
if (parentStat.isSymbolicLink() || !parentStat.isDirectory()) {
|
|
364
|
+
throw new Error(`Refusing cleanup for redirected managed worktree root parent ${parentPath}.`);
|
|
365
|
+
}
|
|
366
|
+
const [realParent, realRoot] = await Promise.all([fs.realpath(parentPath), fs.realpath(rootPath)]);
|
|
367
|
+
if (!samePath(path.join(realParent, path.basename(rootPath)), realRoot)) {
|
|
368
|
+
throw new Error(`Refusing cleanup for redirected managed worktree root ${rootPath}.`);
|
|
369
|
+
}
|
|
370
|
+
return realRoot;
|
|
371
|
+
}
|
|
372
|
+
async function forgeManagedCandidateIdentity(metadataRoot, candidatePath) {
|
|
373
|
+
const gitFile = path.join(candidatePath, ".git");
|
|
374
|
+
const stat = await fs.lstat(gitFile).catch(error => {
|
|
375
|
+
if (isEnoent(error))
|
|
376
|
+
return null;
|
|
377
|
+
throw error;
|
|
378
|
+
});
|
|
379
|
+
if (!stat || stat.isSymbolicLink() || !stat.isFile() || stat.size > 16 * 1024)
|
|
380
|
+
return null;
|
|
381
|
+
const contents = (await fs.readFile(gitFile, "utf8")).trim();
|
|
382
|
+
if (!contents.toLowerCase().startsWith("gitdir:"))
|
|
383
|
+
return null;
|
|
384
|
+
const gitDir = contents.slice("gitdir:".length).trim();
|
|
385
|
+
if (!gitDir)
|
|
386
|
+
return null;
|
|
387
|
+
const candidateGitDir = path.resolve(candidatePath, gitDir);
|
|
388
|
+
return !samePath(metadataRoot, candidateGitDir) && isSubPath(metadataRoot, candidateGitDir)
|
|
389
|
+
? contents
|
|
390
|
+
: null;
|
|
391
|
+
}
|
|
392
|
+
async function quarantineAndRemoveCandidate(worktreeRoot, candidatePath, expectedIdentity) {
|
|
393
|
+
const quarantineRoot = path.join(worktreeRoot, ".forge-cleanup-quarantine");
|
|
394
|
+
await ensureSafeQuarantineRoot(worktreeRoot, quarantineRoot);
|
|
395
|
+
const quarantinePath = path.join(quarantineRoot, randomUUID());
|
|
396
|
+
try {
|
|
397
|
+
await fs.rename(candidatePath, quarantinePath);
|
|
398
|
+
}
|
|
399
|
+
catch (error) {
|
|
400
|
+
if (isEnoent(error))
|
|
401
|
+
return false;
|
|
402
|
+
throw error;
|
|
403
|
+
}
|
|
404
|
+
const quarantinedIdentity = await readGitFileIdentity(quarantinePath);
|
|
405
|
+
if (quarantinedIdentity !== expectedIdentity) {
|
|
406
|
+
await fs.rename(quarantinePath, candidatePath).catch(() => undefined);
|
|
407
|
+
throw new Error(`Refusing cleanup because managed worktree ownership changed while quarantining ${candidatePath}.`);
|
|
408
|
+
}
|
|
409
|
+
await fs.rm(quarantinePath, { recursive: true, force: true });
|
|
410
|
+
await fs.rmdir(quarantineRoot).catch(error => {
|
|
411
|
+
if (!isEnoent(error) && error.code !== "ENOTEMPTY")
|
|
412
|
+
throw error;
|
|
413
|
+
});
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
async function readGitFileIdentity(candidatePath) {
|
|
417
|
+
const gitFile = path.join(candidatePath, ".git");
|
|
418
|
+
const stat = await fs.lstat(gitFile).catch(error => {
|
|
419
|
+
if (isEnoent(error))
|
|
420
|
+
return null;
|
|
421
|
+
throw error;
|
|
422
|
+
});
|
|
423
|
+
if (!stat || stat.isSymbolicLink() || !stat.isFile() || stat.size > 16 * 1024)
|
|
424
|
+
return null;
|
|
425
|
+
return (await fs.readFile(gitFile, "utf8")).trim();
|
|
426
|
+
}
|
|
427
|
+
async function ensureSafeQuarantineRoot(worktreeRoot, quarantineRoot) {
|
|
428
|
+
await fs.mkdir(quarantineRoot, { recursive: true, mode: 0o700 });
|
|
429
|
+
const stat = await fs.lstat(quarantineRoot);
|
|
430
|
+
if (stat.isSymbolicLink() || !stat.isDirectory()) {
|
|
431
|
+
throw new Error(`Refusing cleanup through redirected quarantine root ${quarantineRoot}.`);
|
|
432
|
+
}
|
|
433
|
+
const realQuarantine = await fs.realpath(quarantineRoot);
|
|
434
|
+
if (!isSubPath(worktreeRoot, realQuarantine)) {
|
|
435
|
+
throw new Error(`Cleanup quarantine escaped managed worktree root ${worktreeRoot}.`);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async function quarantineAndRemoveRoot(worktreeRoot) {
|
|
439
|
+
const parent = path.dirname(worktreeRoot);
|
|
440
|
+
const quarantinePath = path.join(parent, `.forge-cleanup-${path.basename(worktreeRoot)}-${randomUUID()}`);
|
|
441
|
+
await fs.rename(worktreeRoot, quarantinePath);
|
|
442
|
+
await fs.rm(quarantinePath, { recursive: true, force: true });
|
|
443
|
+
}
|
|
444
|
+
function parseWorktreeListing(repoPath, output) {
|
|
445
|
+
const worktrees = [];
|
|
446
|
+
let current = null;
|
|
447
|
+
for (const field of output.split("\0")) {
|
|
448
|
+
if (field.startsWith("worktree ")) {
|
|
449
|
+
if (current)
|
|
450
|
+
worktrees.push(current);
|
|
451
|
+
const rawPath = field.slice("worktree ".length);
|
|
452
|
+
current = {
|
|
453
|
+
worktreePath: path.resolve(path.isAbsolute(rawPath) ? rawPath : path.join(repoPath, rawPath)),
|
|
454
|
+
prunable: false,
|
|
455
|
+
branchName: null,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
else if (field.startsWith("prunable") && current) {
|
|
459
|
+
current.prunable = true;
|
|
460
|
+
}
|
|
461
|
+
else if (field.startsWith("branch ") && current) {
|
|
462
|
+
current.branchName = field.slice("branch ".length).trim();
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (current)
|
|
466
|
+
worktrees.push(current);
|
|
467
|
+
return worktrees;
|
|
468
|
+
}
|
|
469
|
+
function isRuntimeReadinessWorktree(worktree) {
|
|
470
|
+
return worktree.branchName?.startsWith("refs/heads/runtime-readiness-") === true
|
|
471
|
+
|| path.basename(worktree.worktreePath).startsWith("runtime-readiness-");
|
|
472
|
+
}
|
|
473
|
+
function activePaths(worktrees) {
|
|
474
|
+
return worktrees.filter(entry => !entry.prunable).map(entry => entry.worktreePath);
|
|
475
|
+
}
|
|
476
|
+
function worktreePathState(candidate, activePathsToCheck) {
|
|
477
|
+
if (activePathsToCheck.some(activePath => samePath(activePath, candidate)))
|
|
478
|
+
return "active";
|
|
479
|
+
if (activePathsToCheck.some(activePath => isSubPath(candidate, activePath)))
|
|
480
|
+
return "ancestor";
|
|
481
|
+
return "none";
|
|
482
|
+
}
|
|
483
|
+
function uniquePaths(values) {
|
|
484
|
+
return values.filter((value, index) => values.findIndex(candidate => samePath(candidate, value)) === index);
|
|
485
|
+
}
|
|
486
|
+
function samePath(left, right) {
|
|
487
|
+
const normalizedLeft = path.resolve(left);
|
|
488
|
+
const normalizedRight = path.resolve(right);
|
|
489
|
+
return process.platform === "win32"
|
|
490
|
+
? normalizedLeft.toLowerCase() === normalizedRight.toLowerCase()
|
|
491
|
+
: normalizedLeft === normalizedRight;
|
|
492
|
+
}
|
|
493
|
+
function isEnoent(error) {
|
|
494
|
+
return error?.code === "ENOENT";
|
|
495
|
+
}
|
|
496
|
+
function isRecord(value) {
|
|
497
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
498
|
}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
+
export type CleanupMode = "stale" | "merged" | "all";
|
|
1
2
|
export interface CleanupRequest {
|
|
2
3
|
readonly repoRoot: string;
|
|
4
|
+
readonly allowedRepoRoot?: string;
|
|
5
|
+
readonly mode: CleanupMode;
|
|
6
|
+
readonly force: boolean;
|
|
3
7
|
}
|
|
4
8
|
export interface CleanupResult {
|
|
9
|
+
readonly worktreeRoot: string;
|
|
5
10
|
readonly removedWorktrees: number;
|
|
11
|
+
readonly candidateWorktrees: number;
|
|
12
|
+
readonly protectedWorktrees: number;
|
|
13
|
+
readonly skippedUnownedDirectories: number;
|
|
14
|
+
readonly removedRoot: boolean;
|
|
15
|
+
readonly dryRun: boolean;
|
|
6
16
|
}
|
package/dist/cleanup/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ManagedWorktreeCleanupService } from "./cleanup.service.js";
|
|
2
2
|
export type { CleanupService } from "./cleanup.base.js";
|
package/dist/cleanup/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ManagedWorktreeCleanupService } from "./cleanup.service.js";
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ManagedWorktreeCleanupService } from "./cleanup.service.js";
|
|
2
2
|
export type { CleanupService } from "./cleanup.base.js";
|
|
3
|
-
export type { CleanupRequest, CleanupResult } from "./cleanup.types.js";
|
|
3
|
+
export type { CleanupMode, CleanupRequest, CleanupResult } from "./cleanup.types.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { ManagedWorktreeCleanupService } from "./cleanup.service.js";
|