@leitwerk-dev/worker 0.3.1 → 0.4.1
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/dist/{chunk-4WPLQCF6.js → chunk-KJCBXN5F.js} +1700 -1244
- package/dist/chunk-KJCBXN5F.js.map +1 -0
- package/dist/container-entry-main.js +160 -3
- package/dist/container-entry-main.js.map +1 -1
- package/dist/container-entry.d.ts +1 -0
- package/dist/container-entry.d.ts.map +1 -1
- package/dist/development-tool-environment.d.ts.map +1 -1
- package/dist/diagnostics.d.ts +5 -1
- package/dist/diagnostics.d.ts.map +1 -1
- package/dist/docker-registry-credentials.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/ipc.d.ts.map +1 -1
- package/dist/logical-prompt-runner.d.ts.map +1 -1
- package/dist/missing-tool-recovery-runner.d.ts.map +1 -1
- package/dist/pi-adapter.d.ts +2 -0
- package/dist/pi-adapter.d.ts.map +1 -1
- package/dist/pi-event-reporter.d.ts +2 -0
- package/dist/pi-event-reporter.d.ts.map +1 -1
- package/dist/pi-inspection.d.ts +5 -0
- package/dist/pi-inspection.d.ts.map +1 -0
- package/dist/runtime/bootstrap-session.d.ts +4 -1
- package/dist/runtime/bootstrap-session.d.ts.map +1 -1
- package/dist/runtime/bootstrap.d.ts.map +1 -1
- package/dist/runtime/failure-policy.d.ts.map +1 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/llm-turn-execution.d.ts +2 -0
- package/dist/runtime/llm-turn-execution.d.ts.map +1 -1
- package/dist/runtime/turn-execution.d.ts.map +1 -1
- package/dist/session-snapshot-exchange.d.ts.map +1 -1
- package/dist/strict-outcome-schema.d.ts +11 -0
- package/dist/strict-outcome-schema.d.ts.map +1 -0
- package/dist/test-helpers/llm-turn.d.ts +4 -0
- package/dist/test-helpers/llm-turn.d.ts.map +1 -0
- package/dist/test-helpers/pi-provider.d.ts +13 -0
- package/dist/test-helpers/pi-provider.d.ts.map +1 -0
- package/dist/transport-error.d.ts +16 -0
- package/dist/transport-error.d.ts.map +1 -0
- package/dist/turn-outcome-tool-session.d.ts +6 -1
- package/dist/turn-outcome-tool-session.d.ts.map +1 -1
- package/dist/turn-result-markdown.d.ts +5 -0
- package/dist/turn-result-markdown.d.ts.map +1 -1
- package/dist/virtual-machine-docker.d.ts +10 -0
- package/dist/virtual-machine-docker.d.ts.map +1 -0
- package/dist/worker-entry.js +1 -1
- package/dist/workspace/checkout-tool.d.ts +6 -0
- package/dist/workspace/checkout-tool.d.ts.map +1 -0
- package/dist/workspace/manifest.d.ts +2 -0
- package/dist/workspace/manifest.d.ts.map +1 -1
- package/dist/workspace/node-run-root-git-ops.d.ts +1 -0
- package/dist/workspace/node-run-root-git-ops.d.ts.map +1 -1
- package/dist/workspace/run-root.d.ts +5 -3
- package/dist/workspace/run-root.d.ts.map +1 -1
- package/package.json +9 -9
- package/src/container-entry.ts +22 -0
- package/src/development-tool-environment.ts +2 -0
- package/src/diagnostics.ts +6 -1
- package/src/docker-registry-credentials.ts +8 -0
- package/src/ipc.ts +4 -24
- package/src/logical-prompt-runner.ts +45 -41
- package/src/missing-tool-recovery-runner.ts +26 -18
- package/src/pi-adapter.ts +219 -337
- package/src/pi-event-reporter.ts +8 -0
- package/src/pi-inspection.ts +126 -0
- package/src/runtime/bootstrap-session.ts +12 -1
- package/src/runtime/bootstrap.ts +15 -5
- package/src/runtime/failure-policy.ts +20 -2
- package/src/runtime/index.ts +65 -10
- package/src/runtime/llm-turn-execution.ts +28 -1
- package/src/runtime/turn-execution.ts +70 -2
- package/src/session-snapshot-exchange.ts +2 -0
- package/src/strict-outcome-schema.ts +84 -0
- package/src/test-helpers/llm-turn.ts +22 -0
- package/src/test-helpers/pi-provider.ts +128 -0
- package/src/transport-error.ts +92 -0
- package/src/turn-outcome-tool-session.ts +123 -26
- package/src/turn-result-markdown.ts +23 -6
- package/src/virtual-machine-docker.ts +191 -0
- package/src/workspace/checkout-tool.ts +68 -0
- package/src/workspace/manifest.ts +3 -0
- package/src/workspace/node-run-root-git-ops.ts +17 -0
- package/src/workspace/run-root.ts +62 -26
- package/dist/chunk-4WPLQCF6.js.map +0 -1
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
|
|
5
|
+
const CGROUP = "/sys/fs/cgroup";
|
|
6
|
+
const PROC_SYS = "/proc/sys";
|
|
7
|
+
const PROC_NET = "/proc/sys/net";
|
|
8
|
+
|
|
9
|
+
interface Mount {
|
|
10
|
+
id: string;
|
|
11
|
+
parent: string;
|
|
12
|
+
root: string;
|
|
13
|
+
target: string;
|
|
14
|
+
options: string[];
|
|
15
|
+
optional: string[];
|
|
16
|
+
type: string;
|
|
17
|
+
source: string;
|
|
18
|
+
superOptions: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface MountPreparationDeps {
|
|
22
|
+
platform: string;
|
|
23
|
+
uid: number | undefined;
|
|
24
|
+
read: (path: string) => Promise<string>;
|
|
25
|
+
mount: (args: string[], signal: AbortSignal) => Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function requireValue(condition: unknown, message: string): asserts condition {
|
|
29
|
+
if (!condition) throw new Error(message);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function mounts(value: string): Mount[] {
|
|
33
|
+
return value
|
|
34
|
+
.trim()
|
|
35
|
+
.split("\n")
|
|
36
|
+
.map((line) => {
|
|
37
|
+
const fields = line.split(" ");
|
|
38
|
+
const separator = fields.indexOf("-");
|
|
39
|
+
requireValue(separator >= 6 && fields.length >= separator + 4, "Invalid mount information");
|
|
40
|
+
const decode = (field: string) =>
|
|
41
|
+
field.replace(/\\([0-7]{3})/gu, (_, octal: string) =>
|
|
42
|
+
String.fromCharCode(Number.parseInt(octal, 8)),
|
|
43
|
+
);
|
|
44
|
+
return {
|
|
45
|
+
id: fields[0],
|
|
46
|
+
parent: fields[1],
|
|
47
|
+
root: decode(fields[3]),
|
|
48
|
+
target: decode(fields[4]),
|
|
49
|
+
options: fields[5].split(",").sort(),
|
|
50
|
+
optional: fields.slice(6, separator),
|
|
51
|
+
type: fields[separator + 1],
|
|
52
|
+
source: fields[separator + 2],
|
|
53
|
+
superOptions: fields[separator + 3].split(",").sort(),
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function atMount(entries: Mount[], target: string): Mount {
|
|
59
|
+
const matches = entries.filter((entry) => entry.target === target);
|
|
60
|
+
requireValue(matches.length === 1, `Expected one mount at ${target}`);
|
|
61
|
+
return matches[0];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function writableSuperblock(mount: Mount): boolean {
|
|
65
|
+
return mount.superOptions.includes("rw") && !mount.superOptions.includes("ro");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function snapshot(deps: MountPreparationDeps) {
|
|
69
|
+
const [mountinfo, membership, cpu, memory] = await Promise.all([
|
|
70
|
+
deps.read("/proc/self/mountinfo"),
|
|
71
|
+
deps.read("/proc/self/cgroup"),
|
|
72
|
+
deps.read(`${CGROUP}/cpu.max`),
|
|
73
|
+
deps.read(`${CGROUP}/memory.max`),
|
|
74
|
+
]);
|
|
75
|
+
const entries = mounts(mountinfo);
|
|
76
|
+
const cgroup = atMount(entries, CGROUP);
|
|
77
|
+
const proc = atMount(entries, "/proc");
|
|
78
|
+
const sys = atMount(entries, PROC_SYS);
|
|
79
|
+
requireValue(
|
|
80
|
+
membership.trim() === "0::/" && cgroup.type === "cgroup2" && cgroup.root === "/",
|
|
81
|
+
"A namespace-root cgroup2 mount is required",
|
|
82
|
+
);
|
|
83
|
+
requireValue(
|
|
84
|
+
writableSuperblock(cgroup) && cgroup.superOptions.includes("nsdelegate"),
|
|
85
|
+
"A writable, namespace-delegating cgroup2 superblock is required",
|
|
86
|
+
);
|
|
87
|
+
requireValue(
|
|
88
|
+
["nosuid", "nodev", "noexec"].every((flag) => cgroup.options.includes(flag)),
|
|
89
|
+
"The cgroup2 mount must retain nosuid, nodev and noexec",
|
|
90
|
+
);
|
|
91
|
+
const cpuValues = cpu.trim().split(/\s+/u);
|
|
92
|
+
requireValue(
|
|
93
|
+
cpuValues.length === 2 && cpuValues.every((value) => /^\d+$/u.test(value) && BigInt(value) > 0),
|
|
94
|
+
"A finite worker CPU quota is required",
|
|
95
|
+
);
|
|
96
|
+
requireValue(
|
|
97
|
+
/^\d+$/u.test(memory.trim()) && BigInt(memory.trim()) > 0,
|
|
98
|
+
"A finite worker memory limit is required",
|
|
99
|
+
);
|
|
100
|
+
requireValue(
|
|
101
|
+
proc.type === "proc" &&
|
|
102
|
+
proc.root === "/" &&
|
|
103
|
+
sys.type === "proc" &&
|
|
104
|
+
sys.root === "/sys" &&
|
|
105
|
+
sys.options.includes("ro") &&
|
|
106
|
+
!sys.options.includes("rw") &&
|
|
107
|
+
writableSuperblock(sys),
|
|
108
|
+
"A read-only /proc/sys mount on a writable proc superblock is required",
|
|
109
|
+
);
|
|
110
|
+
return { entries, cgroup, proc, sys, cpu, memory };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** @internal */
|
|
114
|
+
export async function prepareVirtualMachineDockerMounts(
|
|
115
|
+
signal: AbortSignal,
|
|
116
|
+
deps: MountPreparationDeps = {
|
|
117
|
+
platform: process.platform,
|
|
118
|
+
uid: process.getuid?.(),
|
|
119
|
+
read: (path) => readFile(path, "utf8"),
|
|
120
|
+
mount: (args, signal) =>
|
|
121
|
+
promisify(execFile)("mount", args, { signal, timeout: 5_000, maxBuffer: 4_096 }).then(
|
|
122
|
+
() => undefined,
|
|
123
|
+
),
|
|
124
|
+
},
|
|
125
|
+
): Promise<void> {
|
|
126
|
+
try {
|
|
127
|
+
requireValue(
|
|
128
|
+
deps.platform === "linux" && deps.uid === 0,
|
|
129
|
+
"VM Docker mount preparation requires Linux container root",
|
|
130
|
+
);
|
|
131
|
+
signal.throwIfAborted();
|
|
132
|
+
const before = await snapshot(deps);
|
|
133
|
+
const net = before.entries.filter((entry) => entry.target === PROC_NET);
|
|
134
|
+
requireValue(
|
|
135
|
+
net.length <= 1 &&
|
|
136
|
+
(!net.length ||
|
|
137
|
+
(net[0].type === "proc" &&
|
|
138
|
+
net[0].root === "/sys/net" &&
|
|
139
|
+
JSON.stringify(net[0].superOptions) === JSON.stringify(before.sys.superOptions))),
|
|
140
|
+
"Unexpected /proc/sys/net mount",
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
// Bind remounts change only this mount. Plain remounts would affect the
|
|
144
|
+
// shared guest superblock and can make later Kata exec calls fail.
|
|
145
|
+
const remount = async (target: string) => {
|
|
146
|
+
signal.throwIfAborted();
|
|
147
|
+
await deps.mount(["-o", "remount,bind,rw,nosuid,nodev,noexec", target], signal);
|
|
148
|
+
};
|
|
149
|
+
await remount(CGROUP);
|
|
150
|
+
if (!net.length) {
|
|
151
|
+
signal.throwIfAborted();
|
|
152
|
+
await deps.mount(["--bind", PROC_NET, PROC_NET], signal);
|
|
153
|
+
}
|
|
154
|
+
await remount(PROC_NET);
|
|
155
|
+
signal.throwIfAborted();
|
|
156
|
+
const after = await snapshot(deps);
|
|
157
|
+
requireValue(
|
|
158
|
+
before.cpu === after.cpu && before.memory === after.memory,
|
|
159
|
+
"Mount preparation changed the worker resource limits",
|
|
160
|
+
);
|
|
161
|
+
requireValue(
|
|
162
|
+
JSON.stringify(before.proc) === JSON.stringify(after.proc) &&
|
|
163
|
+
JSON.stringify(before.sys) === JSON.stringify(after.sys),
|
|
164
|
+
"Mount preparation changed the parent proc mounts",
|
|
165
|
+
);
|
|
166
|
+
const expectedCgroup = {
|
|
167
|
+
...before.cgroup,
|
|
168
|
+
options: [
|
|
169
|
+
...before.cgroup.options.filter((flag) => flag !== "ro" && flag !== "rw"),
|
|
170
|
+
"rw",
|
|
171
|
+
].sort(),
|
|
172
|
+
};
|
|
173
|
+
requireValue(
|
|
174
|
+
JSON.stringify(expectedCgroup) === JSON.stringify(after.cgroup),
|
|
175
|
+
"Mount preparation changed the cgroup2 superblock or mount flags",
|
|
176
|
+
);
|
|
177
|
+
const preparedNet = atMount(after.entries, PROC_NET);
|
|
178
|
+
requireValue(
|
|
179
|
+
preparedNet.type === "proc" &&
|
|
180
|
+
preparedNet.root === "/sys/net" &&
|
|
181
|
+
preparedNet.parent === before.sys.id &&
|
|
182
|
+
["rw", "nosuid", "nodev", "noexec"].every((flag) => preparedNet.options.includes(flag)) &&
|
|
183
|
+
!preparedNet.options.includes("ro") &&
|
|
184
|
+
JSON.stringify(preparedNet.superOptions) === JSON.stringify(before.sys.superOptions),
|
|
185
|
+
"The network sysctl mount was not prepared without changing its superblock",
|
|
186
|
+
);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
const message = error instanceof Error ? error.message : "Unknown mount preparation error";
|
|
189
|
+
throw new Error(`VM Docker mount preparation failed: ${message.slice(0, 2_048)}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ProcessProject } from "@leitwerk-dev/domain";
|
|
3
|
+
import type { PiCustomTool } from "@leitwerk-dev/process-sdk";
|
|
4
|
+
import { planRunRoot, prepareOnDemandRunRoot, type RunRootGitOps } from "./run-root.js";
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
export function createCheckoutTool(
|
|
8
|
+
workspaceRoot: string,
|
|
9
|
+
instanceId: string,
|
|
10
|
+
projects: readonly ProcessProject[],
|
|
11
|
+
git: RunRootGitOps,
|
|
12
|
+
): PiCustomTool {
|
|
13
|
+
return {
|
|
14
|
+
name: "checkout_repository",
|
|
15
|
+
description:
|
|
16
|
+
"Materialize a full clone for one authorized project and return its checked-out revision. Repeated calls reuse the checkout. Inspect only; do not publish changes.",
|
|
17
|
+
parameters: {
|
|
18
|
+
type: "object",
|
|
19
|
+
properties: {
|
|
20
|
+
projectKey: { type: "string" },
|
|
21
|
+
revision: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description: "Captured full commit SHA; refuses local changes.",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
required: ["projectKey"],
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
},
|
|
29
|
+
executionMode: "sequential",
|
|
30
|
+
async execute(args) {
|
|
31
|
+
if (typeof args.projectKey !== "string") throw new Error("projectKey is required");
|
|
32
|
+
if (
|
|
33
|
+
args.revision !== undefined &&
|
|
34
|
+
(typeof args.revision !== "string" ||
|
|
35
|
+
!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(args.revision))
|
|
36
|
+
)
|
|
37
|
+
throw new Error("Use a captured full commit SHA");
|
|
38
|
+
const plan = planRunRoot(
|
|
39
|
+
workspaceRoot,
|
|
40
|
+
instanceId,
|
|
41
|
+
projects.map((project) => ({
|
|
42
|
+
key: project.key,
|
|
43
|
+
repoLocator: project.repoLocator,
|
|
44
|
+
baseBranch: project.baseBranch,
|
|
45
|
+
workBranch: project.workBranch || project.baseBranch,
|
|
46
|
+
})),
|
|
47
|
+
);
|
|
48
|
+
const result = await prepareOnDemandRunRoot(
|
|
49
|
+
plan,
|
|
50
|
+
git,
|
|
51
|
+
args.projectKey,
|
|
52
|
+
args.revision as string | undefined,
|
|
53
|
+
);
|
|
54
|
+
const entry = result.manifest.components.find(
|
|
55
|
+
(component) => component.key === args.projectKey,
|
|
56
|
+
);
|
|
57
|
+
if (!entry) throw new Error("Checkout did not produce repository evidence");
|
|
58
|
+
const repositoryPath = path.join(workspaceRoot, entry.key);
|
|
59
|
+
return {
|
|
60
|
+
...entry,
|
|
61
|
+
headSha: await git.getHeadSha(repositoryPath),
|
|
62
|
+
path: repositoryPath,
|
|
63
|
+
instructions:
|
|
64
|
+
"Read this repository's AGENTS.md before inspection; revalidate any wiki guidance against this revision. This checkout is retained, not automatically updated to the remote head.",
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
@@ -17,6 +17,8 @@ export interface ComponentCheckoutPlan {
|
|
|
17
17
|
export interface ComponentManifestEntry extends ComponentCheckoutPlan {
|
|
18
18
|
clonedAt: string;
|
|
19
19
|
headSha: string;
|
|
20
|
+
/** Exact read-only checkout requested by a process preparation. */
|
|
21
|
+
revision?: string;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export interface ManifestDiff {
|
|
@@ -45,6 +47,7 @@ const componentManifestEntrySchema = v.pipe(
|
|
|
45
47
|
workBranch: v.string(),
|
|
46
48
|
clonedAt: v.string(),
|
|
47
49
|
headSha: v.string(),
|
|
50
|
+
revision: v.optional(v.string()),
|
|
48
51
|
}),
|
|
49
52
|
);
|
|
50
53
|
|
|
@@ -122,6 +122,23 @@ export class NodeRunRootGitOps implements RunRootGitOps {
|
|
|
122
122
|
await this.runGit(["checkout", branch], repoDir);
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
async checkoutRevision(repoDir: string, revision: string): Promise<void> {
|
|
126
|
+
if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(revision))
|
|
127
|
+
throw new Error("Use a captured full commit SHA");
|
|
128
|
+
if ((await this.runGit(["status", "--porcelain", "--untracked-files=all"], repoDir)).trim())
|
|
129
|
+
throw new Error(
|
|
130
|
+
"Repository has local changes; preserve or remove them before retrying inspection",
|
|
131
|
+
);
|
|
132
|
+
try {
|
|
133
|
+
await this.runGit(["cat-file", "-e", `${revision}^{commit}`], repoDir);
|
|
134
|
+
} catch {
|
|
135
|
+
await this.runGit(["fetch", "origin", revision], repoDir, path.basename(repoDir));
|
|
136
|
+
}
|
|
137
|
+
await this.runGit(["checkout", "--detach", revision], repoDir);
|
|
138
|
+
if ((await this.getHeadSha(repoDir)).toLowerCase() !== revision.toLowerCase())
|
|
139
|
+
throw new Error("Checkout HEAD does not match captured commit");
|
|
140
|
+
}
|
|
141
|
+
|
|
125
142
|
async createBranch(repoDir: string, branchName: string, startPoint: string): Promise<void> {
|
|
126
143
|
await this.runGit(["branch", branchName, startPoint], repoDir);
|
|
127
144
|
}
|
|
@@ -17,6 +17,8 @@ export interface GitOps {
|
|
|
17
17
|
clone(repoLocator: string, targetDir: string): Promise<void>;
|
|
18
18
|
/** @internal */
|
|
19
19
|
checkout(repoDir: string, branch: string): Promise<void>;
|
|
20
|
+
/** Refuse dirty worktrees and pin an exact commit. @internal */
|
|
21
|
+
checkoutRevision?(repoDir: string, revision: string): Promise<void>;
|
|
20
22
|
/** @internal */
|
|
21
23
|
createBranch(repoDir: string, branchName: string, startPoint: string): Promise<void>;
|
|
22
24
|
/** @internal */
|
|
@@ -48,11 +50,9 @@ export interface RunRootPlan {
|
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
export interface MaterializeResult {
|
|
51
|
-
ok: boolean;
|
|
52
53
|
manifest: ComponentManifest;
|
|
53
54
|
aggregatedAgentsMdSources: string[];
|
|
54
55
|
loadedSkills: string[];
|
|
55
|
-
errors: string[];
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export interface ValidationResult {
|
|
@@ -131,22 +131,20 @@ async function collectMaterializedEntries(
|
|
|
131
131
|
plan: RunRootPlan,
|
|
132
132
|
git: RunRootGitOps,
|
|
133
133
|
errors: string[],
|
|
134
|
-
|
|
135
|
-
existingEntryForKey?: (componentKey: string) => ComponentManifestEntry | null,
|
|
134
|
+
reusable = new Map<string, ComponentManifestEntry>(),
|
|
136
135
|
): Promise<ComponentManifestEntry[]> {
|
|
137
136
|
const entries: ComponentManifestEntry[] = [];
|
|
138
137
|
for (const comp of plan.components) {
|
|
139
|
-
if (!shouldMaterialize(comp.key)) {
|
|
140
|
-
const existing = existingEntryForKey?.(comp.key) ?? null;
|
|
141
|
-
if (existing) {
|
|
142
|
-
entries.push(existing);
|
|
143
|
-
} else {
|
|
144
|
-
errors.push(`${comp.key}: no existing manifest entry for unchanged key`);
|
|
145
|
-
}
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
138
|
try {
|
|
149
|
-
|
|
139
|
+
const retained = reusable.get(comp.key);
|
|
140
|
+
if (retained?.revision) {
|
|
141
|
+
if (!git.checkoutRevision) throw new Error("Pinned repository checkout is unavailable");
|
|
142
|
+
const dir = componentDir(plan.workspaceRoot, comp.key);
|
|
143
|
+
await git.checkoutRevision(dir, retained.revision);
|
|
144
|
+
if ((await git.getHeadSha(dir)).toLowerCase() !== retained.revision.toLowerCase())
|
|
145
|
+
throw new Error("Recovered checkout HEAD does not match captured commit");
|
|
146
|
+
}
|
|
147
|
+
entries.push(retained ?? (await materializeComponentEntry(plan, comp, git)));
|
|
150
148
|
} catch (e) {
|
|
151
149
|
errors.push(`${comp.key}: ${e instanceof Error ? e.message : String(e)}`);
|
|
152
150
|
}
|
|
@@ -255,11 +253,9 @@ async function finalizeRunRoot(
|
|
|
255
253
|
throw new RunRootPreparationError(errors);
|
|
256
254
|
}
|
|
257
255
|
return {
|
|
258
|
-
ok: true,
|
|
259
256
|
manifest,
|
|
260
257
|
aggregatedAgentsMdSources,
|
|
261
258
|
loadedSkills,
|
|
262
|
-
errors,
|
|
263
259
|
};
|
|
264
260
|
}
|
|
265
261
|
|
|
@@ -272,6 +268,51 @@ export async function materializeRunRoot(
|
|
|
272
268
|
return finalizeRunRoot(plan, git, entries, errors);
|
|
273
269
|
}
|
|
274
270
|
|
|
271
|
+
/** @internal */
|
|
272
|
+
export async function prepareOnDemandRunRoot(
|
|
273
|
+
plan: RunRootPlan,
|
|
274
|
+
git: RunRootGitOps,
|
|
275
|
+
projectKey?: string,
|
|
276
|
+
revision?: string,
|
|
277
|
+
): Promise<MaterializeResult> {
|
|
278
|
+
if (revision !== undefined && (!projectKey || !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(revision)))
|
|
279
|
+
throw new Error("Use a captured full commit SHA");
|
|
280
|
+
if (
|
|
281
|
+
projectKey !== undefined &&
|
|
282
|
+
!plan.components.some((component) => component.key === projectKey)
|
|
283
|
+
)
|
|
284
|
+
throw new Error("Repository is outside this process's authorized scope");
|
|
285
|
+
const validation = await validateRunRoot(plan.workspaceRoot, plan.components, git);
|
|
286
|
+
const existingKeys = new Set(
|
|
287
|
+
validation.existingManifest?.components.map((component) => component.key) ?? [],
|
|
288
|
+
);
|
|
289
|
+
const selected = {
|
|
290
|
+
...plan,
|
|
291
|
+
components: plan.components.filter(
|
|
292
|
+
(component) => existingKeys.has(component.key) || component.key === projectKey,
|
|
293
|
+
),
|
|
294
|
+
};
|
|
295
|
+
// Selection only omits missing components, so retained entries keep the same validation.
|
|
296
|
+
const result = await repairRunRoot(validation, selected, git);
|
|
297
|
+
if (revision !== undefined) {
|
|
298
|
+
if (!git.checkoutRevision) throw new Error("Pinned repository checkout is unavailable");
|
|
299
|
+
const dir = componentDir(plan.workspaceRoot, projectKey!);
|
|
300
|
+
await git.checkoutRevision(dir, revision);
|
|
301
|
+
const headSha = await git.getHeadSha(dir);
|
|
302
|
+
if (headSha.toLowerCase() !== revision.toLowerCase())
|
|
303
|
+
throw new Error("Checkout HEAD does not match captured commit");
|
|
304
|
+
return finalizeRunRoot(
|
|
305
|
+
selected,
|
|
306
|
+
git,
|
|
307
|
+
result.manifest.components.map((entry) =>
|
|
308
|
+
entry.key === projectKey ? { ...entry, headSha, revision } : entry,
|
|
309
|
+
),
|
|
310
|
+
[],
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
return result;
|
|
314
|
+
}
|
|
315
|
+
|
|
275
316
|
export async function validateRunRoot(
|
|
276
317
|
workspaceRoot: string,
|
|
277
318
|
serverProjects: ComponentCheckoutPlan[],
|
|
@@ -301,22 +342,17 @@ export async function validateRunRoot(
|
|
|
301
342
|
}
|
|
302
343
|
|
|
303
344
|
export async function repairRunRoot(
|
|
304
|
-
_workspaceRoot: string,
|
|
305
345
|
validation: ValidationResult,
|
|
306
346
|
plan: RunRootPlan,
|
|
307
347
|
git: RunRootGitOps,
|
|
308
348
|
): Promise<MaterializeResult> {
|
|
309
349
|
const errors: string[] = [];
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
errors,
|
|
315
|
-
(componentKey) => toRepair.has(componentKey),
|
|
316
|
-
(componentKey) =>
|
|
317
|
-
validation.existingManifest?.components.find((component) => component.key === componentKey) ??
|
|
318
|
-
null,
|
|
350
|
+
const reusable = new Map(
|
|
351
|
+
validation.existingManifest?.components
|
|
352
|
+
.filter((component) => validation.diff.unchanged.includes(component.key))
|
|
353
|
+
.map((component) => [component.key, component]),
|
|
319
354
|
);
|
|
355
|
+
const manifestEntries = await collectMaterializedEntries(plan, git, errors, reusable);
|
|
320
356
|
|
|
321
357
|
return finalizeRunRoot(plan, git, manifestEntries, errors);
|
|
322
358
|
}
|