@teamclaws/teamclaw 2026.8.0 → 2026.8.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/api.js +1 -0
- package/dist/index.js +325 -0
- package/dist/src/config.js +381 -0
- package/dist/src/controller/controller-capacity.js +14 -0
- package/dist/src/controller/controller-service.js +393 -0
- package/dist/src/controller/controller-tools.js +582 -0
- package/dist/src/controller/delivery-report.js +458 -0
- package/dist/src/controller/github-auth.js +564 -0
- package/dist/src/controller/http-server.js +6032 -0
- package/dist/src/controller/kickoff-orchestrator.js +220 -0
- package/dist/src/controller/managed-gateway-process.js +286 -0
- package/dist/src/controller/message-router.js +38 -0
- package/dist/src/controller/orchestration-manifest.js +173 -0
- package/dist/src/controller/preview-manager.js +586 -0
- package/dist/src/controller/prompt-injector.js +187 -0
- package/dist/src/controller/role-inference.js +35 -0
- package/dist/src/controller/task-router.js +67 -0
- package/dist/src/controller/tunnel-manager.js +548 -0
- package/dist/src/controller/websocket.js +42 -0
- package/dist/src/controller/worker-provisioning.js +1648 -0
- package/dist/src/discovery.js +86 -0
- package/dist/src/git-collaboration.js +694 -0
- package/dist/src/identity.js +131 -0
- package/dist/src/install-defaults.js +1 -0
- package/dist/src/interaction-contracts.js +531 -0
- package/dist/src/networking.js +91 -0
- package/dist/src/openclaw-workspace.js +903 -0
- package/dist/src/prompt-policy.js +373 -0
- package/dist/src/protocol.js +58 -0
- package/dist/src/roles.js +349 -0
- package/dist/src/state.js +183 -0
- package/dist/src/task-executor.js +1033 -0
- package/dist/src/teamauto/github-client.js +192 -0
- package/dist/src/teamauto/norms.js +85 -0
- package/dist/src/teamauto/repo-sync.js +201 -0
- package/dist/src/teamauto/scanner.js +179 -0
- package/dist/src/teamauto/schedule.js +123 -0
- package/dist/src/teamauto/scheduler.js +688 -0
- package/dist/src/teamauto/store.js +133 -0
- package/dist/src/types.js +243 -0
- package/dist/src/ui/app.js +4292 -0
- package/dist/src/ui/assets/teamclaw-app-icon.png +0 -0
- package/dist/src/ui/github-login.html +296 -0
- package/dist/src/ui/index.html +414 -0
- package/dist/src/ui/style.css +2611 -0
- package/dist/src/worker/http-handler.js +146 -0
- package/dist/src/worker/message-queue.js +24 -0
- package/dist/src/worker/prompt-injector.js +42 -0
- package/dist/src/worker/skill-installer.js +253 -0
- package/dist/src/worker/tools.js +541 -0
- package/dist/src/worker/worker-service.js +234 -0
- package/dist/src/workspace-browser.js +269 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { resolveTeamClawWorkspaceDir } from "./openclaw-workspace.js";
|
|
6
|
+
const TEAMCLAW_IMPORT_REF_PREFIX = "refs/teamclaw/imports";
|
|
7
|
+
const BUNDLE_IMPORT_MAX_RETRIES = 3;
|
|
8
|
+
const BUNDLE_IMPORT_RETRY_DELAY_MS = 2_000;
|
|
9
|
+
const TEAMCLAW_RUNTIME_EXCLUDES = [
|
|
10
|
+
".openclaw/",
|
|
11
|
+
".clawhub/",
|
|
12
|
+
"AGENTS.md",
|
|
13
|
+
"BOOTSTRAP.md",
|
|
14
|
+
"HEARTBEAT.md",
|
|
15
|
+
"IDENTITY.md",
|
|
16
|
+
"SOUL.md",
|
|
17
|
+
"skills/",
|
|
18
|
+
"TOOLS.md",
|
|
19
|
+
"USER.md",
|
|
20
|
+
];
|
|
21
|
+
const repoLocks = new Map();
|
|
22
|
+
export async function ensureControllerGitRepo(config, logger) {
|
|
23
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
24
|
+
return await withRepoLock(workspaceDir, async () => ensureControllerGitRepoUnlocked(config, logger, workspaceDir));
|
|
25
|
+
}
|
|
26
|
+
async function ensureControllerGitRepoUnlocked(config, logger, workspaceDir) {
|
|
27
|
+
if (!config.gitEnabled) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
await fs.mkdir(workspaceDir, { recursive: true });
|
|
31
|
+
const gitDir = path.join(workspaceDir, ".git");
|
|
32
|
+
const repoAlreadyExists = await pathExists(gitDir);
|
|
33
|
+
if (!repoAlreadyExists) {
|
|
34
|
+
logger.info(`TeamClaw: initializing git workspace repo at ${workspaceDir}`);
|
|
35
|
+
await runGit(["init"], { cwd: workspaceDir });
|
|
36
|
+
}
|
|
37
|
+
await configureGitIdentity(workspaceDir, config);
|
|
38
|
+
await configureGitWorkspaceExcludes(workspaceDir);
|
|
39
|
+
await ensureBranchHead(workspaceDir, config.gitDefaultBranch);
|
|
40
|
+
if (!repoAlreadyExists && !await hasHeadCommit(workspaceDir)) {
|
|
41
|
+
await runGit(["add", "-A"], { cwd: workspaceDir });
|
|
42
|
+
await runGit(["commit", "--allow-empty", "-m", "chore: bootstrap TeamClaw workspace"], { cwd: workspaceDir });
|
|
43
|
+
}
|
|
44
|
+
let remoteReady = false;
|
|
45
|
+
if (config.gitRemoteUrl) {
|
|
46
|
+
remoteReady = await ensureOriginRemote(workspaceDir, config, logger);
|
|
47
|
+
}
|
|
48
|
+
return await readGitRepoState(config, remoteReady);
|
|
49
|
+
}
|
|
50
|
+
export function buildRepoSyncInfo(repo, sharedWorkspace) {
|
|
51
|
+
if (!repo?.enabled) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
if (sharedWorkspace) {
|
|
55
|
+
return {
|
|
56
|
+
enabled: true,
|
|
57
|
+
mode: "shared",
|
|
58
|
+
defaultBranch: repo.defaultBranch,
|
|
59
|
+
headCommit: repo.headCommit,
|
|
60
|
+
headSummary: repo.headSummary,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (repo.remoteReady && repo.remoteUrl) {
|
|
64
|
+
return {
|
|
65
|
+
enabled: true,
|
|
66
|
+
mode: "remote",
|
|
67
|
+
defaultBranch: repo.defaultBranch,
|
|
68
|
+
remoteUrl: repo.remoteUrl,
|
|
69
|
+
headCommit: repo.headCommit,
|
|
70
|
+
headSummary: repo.headSummary,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
enabled: true,
|
|
75
|
+
mode: "bundle",
|
|
76
|
+
defaultBranch: repo.defaultBranch,
|
|
77
|
+
bundleUrl: "/api/v1/repo/bundle",
|
|
78
|
+
importUrl: "/api/v1/repo/import",
|
|
79
|
+
headCommit: repo.headCommit,
|
|
80
|
+
headSummary: repo.headSummary,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export async function exportControllerGitBundle(config, logger) {
|
|
84
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
85
|
+
return await withRepoLock(workspaceDir, async () => {
|
|
86
|
+
const repo = await ensureControllerGitRepoUnlocked(config, logger, workspaceDir);
|
|
87
|
+
if (!repo?.enabled) {
|
|
88
|
+
throw new Error("TeamClaw git collaboration is disabled");
|
|
89
|
+
}
|
|
90
|
+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "teamclaw-bundle-"));
|
|
91
|
+
const bundlePath = path.join(tempDir, "workspace.bundle");
|
|
92
|
+
try {
|
|
93
|
+
await runGit(["bundle", "create", bundlePath, config.gitDefaultBranch], { cwd: workspaceDir });
|
|
94
|
+
const data = await fs.readFile(bundlePath);
|
|
95
|
+
return {
|
|
96
|
+
repo,
|
|
97
|
+
data,
|
|
98
|
+
filename: `teamclaw-${sanitizeRefPart(config.teamName)}-${sanitizeRefPart(config.gitDefaultBranch)}.bundle`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {
|
|
103
|
+
// best-effort temp cleanup
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
export async function removeTrackedWorkspaceSubtree(config, logger, absoluteTargetPath) {
|
|
109
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
110
|
+
return await withRepoLock(workspaceDir, async () => {
|
|
111
|
+
if (!config.gitEnabled) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (!await pathExists(path.join(workspaceDir, ".git"))) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const targetPath = path.resolve(absoluteTargetPath);
|
|
118
|
+
const relative = path.relative(workspaceDir, targetPath);
|
|
119
|
+
if (!relative || relative === "." || relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
120
|
+
throw new Error(`Refusing to remove tracked paths outside the TeamClaw workspace: ${absoluteTargetPath}`);
|
|
121
|
+
}
|
|
122
|
+
const trackedResult = await tryGit(["ls-files", "--", relative], { cwd: workspaceDir });
|
|
123
|
+
if (trackedResult.exitCode !== 0) {
|
|
124
|
+
throw new Error(formatCommandError(`git ls-files -- ${relative}`, trackedResult));
|
|
125
|
+
}
|
|
126
|
+
if (!trackedResult.stdout.trim()) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
logger.info(`TeamClaw: removing tracked workspace paths under ${relative}`);
|
|
130
|
+
await runGit(["rm", "-r", "--ignore-unmatch", "--", relative], { cwd: workspaceDir });
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
export async function importControllerGitBundle(config, logger, bundle, meta = {}) {
|
|
134
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
135
|
+
return await withRepoLock(workspaceDir, async () => {
|
|
136
|
+
const repo = await ensureControllerGitRepoUnlocked(config, logger, workspaceDir);
|
|
137
|
+
if (!repo?.enabled) {
|
|
138
|
+
throw new Error("TeamClaw git collaboration is disabled");
|
|
139
|
+
}
|
|
140
|
+
const refreshedBeforeImport = await readGitRepoState(config, repo.remoteReady);
|
|
141
|
+
let hadStashed = false;
|
|
142
|
+
if (refreshedBeforeImport.dirty) {
|
|
143
|
+
logger.info("Controller workspace has uncommitted changes; stashing before bundle import");
|
|
144
|
+
const stashResult = await tryGit(["stash", "--include-untracked"], { cwd: workspaceDir });
|
|
145
|
+
if (stashResult.exitCode === 0) {
|
|
146
|
+
hadStashed = true;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return {
|
|
150
|
+
merged: false,
|
|
151
|
+
fastForwarded: false,
|
|
152
|
+
alreadyUpToDate: false,
|
|
153
|
+
repo: refreshedBeforeImport,
|
|
154
|
+
message: "Controller workspace has uncommitted changes that cannot be stashed; refusing bundle import.",
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "teamclaw-import-"));
|
|
159
|
+
const bundlePath = path.join(tempDir, "worker.bundle");
|
|
160
|
+
const importRef = `${TEAMCLAW_IMPORT_REF_PREFIX}/${sanitizeRefPart(meta.workerId ?? "worker")}-${Date.now().toString(36)}`;
|
|
161
|
+
try {
|
|
162
|
+
await fs.writeFile(bundlePath, bundle);
|
|
163
|
+
await runGit(["fetch", bundlePath, `refs/heads/${config.gitDefaultBranch}:${importRef}`], { cwd: workspaceDir });
|
|
164
|
+
const importedCommit = await revParse(workspaceDir, importRef);
|
|
165
|
+
const currentHead = await revParseOrEmpty(workspaceDir, "HEAD");
|
|
166
|
+
if (currentHead && currentHead === importedCommit) {
|
|
167
|
+
const currentRepo = await readGitRepoState(config, repo.remoteReady);
|
|
168
|
+
return {
|
|
169
|
+
merged: false,
|
|
170
|
+
fastForwarded: false,
|
|
171
|
+
alreadyUpToDate: true,
|
|
172
|
+
repo: currentRepo,
|
|
173
|
+
message: "Controller repo already includes the worker commit.",
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
let fastForwarded = true;
|
|
177
|
+
const ffResult = await tryGit(["merge", "--ff-only", importRef], { cwd: workspaceDir });
|
|
178
|
+
if (ffResult.exitCode !== 0) {
|
|
179
|
+
fastForwarded = false;
|
|
180
|
+
const mergeResult = await tryGit(["merge", "--no-edit", importRef], { cwd: workspaceDir });
|
|
181
|
+
if (mergeResult.exitCode !== 0) {
|
|
182
|
+
// Both ff-only and regular merge failed — worker history diverged from controller.
|
|
183
|
+
// Fall back to "theirs" strategy to preserve worker changes (the agent just finished work).
|
|
184
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
185
|
+
const theirsResult = await tryGit(["merge", "--no-edit", "-X", "theirs", importRef], { cwd: workspaceDir });
|
|
186
|
+
if (theirsResult.exitCode !== 0) {
|
|
187
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
188
|
+
const currentRepo = await readGitRepoState(config, repo.remoteReady);
|
|
189
|
+
return {
|
|
190
|
+
merged: false,
|
|
191
|
+
fastForwarded: false,
|
|
192
|
+
alreadyUpToDate: false,
|
|
193
|
+
repo: currentRepo,
|
|
194
|
+
message: `Failed to merge worker bundle for task ${meta.taskId ?? "unknown"} even with theirs strategy: ${formatCommandError("git merge", theirsResult)}`,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const currentRepo = await readGitRepoState(config, repo.remoteReady);
|
|
200
|
+
return {
|
|
201
|
+
merged: true,
|
|
202
|
+
fastForwarded,
|
|
203
|
+
alreadyUpToDate: false,
|
|
204
|
+
repo: currentRepo,
|
|
205
|
+
message: fastForwarded
|
|
206
|
+
? `Imported worker bundle from ${meta.workerId ?? "worker"} with a fast-forward update.`
|
|
207
|
+
: `Imported worker bundle from ${meta.workerId ?? "worker"} with a merge commit.`,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
if (hadStashed) {
|
|
212
|
+
const popResult = await tryGit(["stash", "pop"], { cwd: workspaceDir });
|
|
213
|
+
if (popResult.exitCode !== 0) {
|
|
214
|
+
logger.warn(`Failed to restore stashed changes after bundle import: ${popResult.stderr?.trim() || "unknown error"}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
await tryGit(["update-ref", "-d", importRef], { cwd: workspaceDir });
|
|
218
|
+
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {
|
|
219
|
+
// best-effort temp cleanup
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
export async function syncWorkerRepo(config, logger, controllerUrl, repoInfo) {
|
|
225
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
226
|
+
return await withRepoLock(workspaceDir, async () => syncWorkerRepoUnlocked(config, logger, controllerUrl, repoInfo, workspaceDir));
|
|
227
|
+
}
|
|
228
|
+
async function syncWorkerRepoUnlocked(config, logger, controllerUrl, repoInfo, workspaceDir) {
|
|
229
|
+
await fs.mkdir(workspaceDir, { recursive: true });
|
|
230
|
+
if (repoInfo.mode === "shared") {
|
|
231
|
+
if (!await pathExists(path.join(workspaceDir, ".git"))) {
|
|
232
|
+
throw new Error("Shared workspace repo is missing its .git directory");
|
|
233
|
+
}
|
|
234
|
+
const repo = await readGitRepoState(config, false);
|
|
235
|
+
return {
|
|
236
|
+
repo,
|
|
237
|
+
message: `Using shared git workspace on branch ${repo.defaultBranch}.`,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
if (!await pathExists(path.join(workspaceDir, ".git"))) {
|
|
241
|
+
await runGit(["init"], { cwd: workspaceDir });
|
|
242
|
+
await ensureBranchHead(workspaceDir, repoInfo.defaultBranch);
|
|
243
|
+
}
|
|
244
|
+
await configureGitIdentity(workspaceDir, config);
|
|
245
|
+
await configureGitWorkspaceExcludes(workspaceDir);
|
|
246
|
+
const localRepo = await readGitRepoState(config, false);
|
|
247
|
+
if (localRepo.dirty) {
|
|
248
|
+
// Auto-commit leftover changes (e.g. from a prior failed task) instead of
|
|
249
|
+
// blocking sync. This mirrors publishWorkerRepoUnlocked's auto-commit and
|
|
250
|
+
// the controller's stash logic in importControllerGitBundle.
|
|
251
|
+
logger.info("Worker workspace has uncommitted changes; auto-committing before sync");
|
|
252
|
+
await runGit(["add", "-A"], { cwd: workspaceDir });
|
|
253
|
+
const commitResult = await tryGit(["commit", "-m", "chore(teamclaw): auto-commit uncommitted changes before sync"], { cwd: workspaceDir });
|
|
254
|
+
if (commitResult.exitCode !== 0) {
|
|
255
|
+
// Commit can fail on a repo with no HEAD yet — fall back to stash
|
|
256
|
+
const stashResult = await tryGit(["stash", "--include-untracked"], { cwd: workspaceDir });
|
|
257
|
+
if (stashResult.exitCode !== 0) {
|
|
258
|
+
throw new Error("Worker workspace has uncommitted changes that cannot be committed or stashed");
|
|
259
|
+
}
|
|
260
|
+
logger.info("Stashed uncommitted changes (no HEAD commit yet)");
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
if (repoInfo.mode === "remote") {
|
|
264
|
+
if (!repoInfo.remoteUrl) {
|
|
265
|
+
throw new Error("Remote repo sync requested but no remoteUrl was provided");
|
|
266
|
+
}
|
|
267
|
+
await runGit(["remote", "remove", "origin"], { cwd: workspaceDir }).catch(() => {
|
|
268
|
+
// ignore missing remote
|
|
269
|
+
});
|
|
270
|
+
await runGit(["remote", "add", "origin", repoInfo.remoteUrl], { cwd: workspaceDir });
|
|
271
|
+
await runGit(["fetch", "origin", repoInfo.defaultBranch], { cwd: workspaceDir });
|
|
272
|
+
await checkoutTrackingBranch(workspaceDir, repoInfo.defaultBranch, `refs/remotes/origin/${repoInfo.defaultBranch}`);
|
|
273
|
+
const mergeResult = await tryGit(["merge", "--ff-only", `refs/remotes/origin/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
274
|
+
if (mergeResult.exitCode !== 0) {
|
|
275
|
+
// Worker has local commits that diverge from origin — fall back to merge.
|
|
276
|
+
const regularMerge = await tryGit(["merge", "--no-edit", `refs/remotes/origin/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
277
|
+
if (regularMerge.exitCode !== 0) {
|
|
278
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
279
|
+
const theirsMerge = await tryGit(["merge", "--no-edit", "-X", "theirs", `refs/remotes/origin/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
280
|
+
if (theirsMerge.exitCode !== 0) {
|
|
281
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
282
|
+
logger.info("Worker repo has unrelated history; resetting to remote branch");
|
|
283
|
+
await runGit(["reset", "--hard", `refs/remotes/origin/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
if (!repoInfo.bundleUrl) {
|
|
290
|
+
throw new Error("Bundle repo sync requested but no bundleUrl was provided");
|
|
291
|
+
}
|
|
292
|
+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "teamclaw-worker-sync-"));
|
|
293
|
+
const bundlePath = path.join(tempDir, "controller.bundle");
|
|
294
|
+
try {
|
|
295
|
+
const res = await fetch(resolveApiUrl(repoInfo.bundleUrl, controllerUrl));
|
|
296
|
+
if (!res.ok) {
|
|
297
|
+
throw new Error(`Bundle download failed with status ${res.status}`);
|
|
298
|
+
}
|
|
299
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
300
|
+
await fs.writeFile(bundlePath, buffer);
|
|
301
|
+
await runGit(["fetch", bundlePath, `refs/heads/${repoInfo.defaultBranch}:refs/remotes/teamclaw/${repoInfo.defaultBranch}`], {
|
|
302
|
+
cwd: workspaceDir,
|
|
303
|
+
});
|
|
304
|
+
await checkoutTrackingBranch(workspaceDir, repoInfo.defaultBranch, `refs/remotes/teamclaw/${repoInfo.defaultBranch}`);
|
|
305
|
+
const mergeResult = await tryGit(["merge", "--ff-only", `refs/remotes/teamclaw/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
306
|
+
if (mergeResult.exitCode !== 0) {
|
|
307
|
+
// Worker has local commits that diverge from controller — fall back to merge.
|
|
308
|
+
const regularMerge = await tryGit(["merge", "--no-edit", `refs/remotes/teamclaw/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
309
|
+
if (regularMerge.exitCode !== 0) {
|
|
310
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
311
|
+
const theirsMerge = await tryGit(["merge", "--no-edit", "-X", "theirs", `refs/remotes/teamclaw/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
312
|
+
if (theirsMerge.exitCode !== 0) {
|
|
313
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
314
|
+
// Final fallback: worker history is completely unrelated (e.g. fresh
|
|
315
|
+
// container with auto-committed workspace files). Adopt the
|
|
316
|
+
// controller's branch wholesale — this is safe because the
|
|
317
|
+
// controller is the source of truth for repo state.
|
|
318
|
+
logger.info("Worker repo has unrelated history; resetting to controller branch");
|
|
319
|
+
await runGit(["reset", "--hard", `refs/remotes/teamclaw/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
finally {
|
|
325
|
+
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {
|
|
326
|
+
// best-effort temp cleanup
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const repo = await readGitRepoState(config, repoInfo.mode === "remote");
|
|
331
|
+
return {
|
|
332
|
+
repo,
|
|
333
|
+
message: `Repo sync complete on ${repoInfo.mode} mode (${repo.defaultBranch}).`,
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
export async function publishWorkerRepo(config, logger, controllerUrl, repoInfo, meta) {
|
|
337
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
338
|
+
return await withRepoLock(workspaceDir, async () => publishWorkerRepoUnlocked(config, logger, controllerUrl, repoInfo, meta, workspaceDir));
|
|
339
|
+
}
|
|
340
|
+
async function publishWorkerRepoUnlocked(config, logger, controllerUrl, repoInfo, meta, workspaceDir) {
|
|
341
|
+
if (repoInfo.mode === "shared") {
|
|
342
|
+
const repo = await readGitRepoState(config, false);
|
|
343
|
+
return {
|
|
344
|
+
repo,
|
|
345
|
+
published: false,
|
|
346
|
+
message: "Shared workspace repo does not require controller-mediated publish.",
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
if (!await pathExists(path.join(workspaceDir, ".git"))) {
|
|
350
|
+
throw new Error("Worker repo is not initialized");
|
|
351
|
+
}
|
|
352
|
+
await configureGitIdentity(workspaceDir, config);
|
|
353
|
+
await configureGitWorkspaceExcludes(workspaceDir);
|
|
354
|
+
const dirtyCheck = await runGit(["status", "--porcelain"], { cwd: workspaceDir });
|
|
355
|
+
if (dirtyCheck.stdout.trim()) {
|
|
356
|
+
await runGit(["add", "-A"], { cwd: workspaceDir });
|
|
357
|
+
const commitMessage = `chore(teamclaw): checkpoint ${meta.taskId} (${meta.role ?? "worker"})`;
|
|
358
|
+
await runGit(["commit", "-m", commitMessage], { cwd: workspaceDir });
|
|
359
|
+
}
|
|
360
|
+
if (repoInfo.mode === "remote") {
|
|
361
|
+
if (!repoInfo.remoteUrl) {
|
|
362
|
+
throw new Error("Remote publish requested but no remoteUrl was provided");
|
|
363
|
+
}
|
|
364
|
+
await runGit(["fetch", "origin", repoInfo.defaultBranch], { cwd: workspaceDir });
|
|
365
|
+
const remoteRef = `refs/remotes/origin/${repoInfo.defaultBranch}`;
|
|
366
|
+
if (await revParseOrEmpty(workspaceDir, remoteRef)) {
|
|
367
|
+
const ffResult = await tryGit(["merge", "--ff-only", remoteRef], { cwd: workspaceDir });
|
|
368
|
+
if (ffResult.exitCode !== 0) {
|
|
369
|
+
const mergeResult = await tryGit(["merge", "--no-edit", remoteRef], { cwd: workspaceDir });
|
|
370
|
+
if (mergeResult.exitCode !== 0) {
|
|
371
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
372
|
+
throw new Error(`Failed to merge latest remote changes before push: ${formatCommandError("git merge", mergeResult)}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
await runGit(["push", "origin", `HEAD:refs/heads/${repoInfo.defaultBranch}`], { cwd: workspaceDir });
|
|
377
|
+
const repo = await readGitRepoState(config, true);
|
|
378
|
+
return {
|
|
379
|
+
repo,
|
|
380
|
+
published: true,
|
|
381
|
+
message: `Pushed worker changes for task ${meta.taskId} to origin/${repoInfo.defaultBranch}.`,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
if (!repoInfo.importUrl) {
|
|
385
|
+
throw new Error("Bundle publish requested but no importUrl was provided");
|
|
386
|
+
}
|
|
387
|
+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "teamclaw-worker-publish-"));
|
|
388
|
+
const bundlePath = path.join(tempDir, "worker.bundle");
|
|
389
|
+
try {
|
|
390
|
+
for (let attempt = 0; attempt <= BUNDLE_IMPORT_MAX_RETRIES; attempt++) {
|
|
391
|
+
if (attempt > 0) {
|
|
392
|
+
// Before retry, rebase local worker commits onto the latest controller state
|
|
393
|
+
// so the next bundle is based on the current controller HEAD.
|
|
394
|
+
await new Promise((resolve) => setTimeout(resolve, BUNDLE_IMPORT_RETRY_DELAY_MS * attempt));
|
|
395
|
+
await tryRebaseWorkerOntoController(workspaceDir, repoInfo, controllerUrl, config, logger);
|
|
396
|
+
}
|
|
397
|
+
await runGit(["bundle", "create", bundlePath, repoInfo.defaultBranch], { cwd: workspaceDir });
|
|
398
|
+
const bundle = await fs.readFile(bundlePath);
|
|
399
|
+
const importUrl = new URL(resolveApiUrl(repoInfo.importUrl, controllerUrl));
|
|
400
|
+
importUrl.searchParams.set("taskId", meta.taskId);
|
|
401
|
+
importUrl.searchParams.set("workerId", meta.workerId);
|
|
402
|
+
if (meta.role) {
|
|
403
|
+
importUrl.searchParams.set("role", meta.role);
|
|
404
|
+
}
|
|
405
|
+
const res = await fetch(importUrl.toString(), {
|
|
406
|
+
method: "POST",
|
|
407
|
+
headers: { "Content-Type": "application/octet-stream" },
|
|
408
|
+
body: bundle,
|
|
409
|
+
});
|
|
410
|
+
if (res.ok) {
|
|
411
|
+
const payload = await res.json();
|
|
412
|
+
return {
|
|
413
|
+
repo: payload.repo ?? await readGitRepoState(config, false),
|
|
414
|
+
published: true,
|
|
415
|
+
message: payload.message ?? `Imported bundle for task ${meta.taskId}.`,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
// Only retry on 409 Conflict — other errors are not worth retrying.
|
|
419
|
+
if (res.status !== 409 || attempt === BUNDLE_IMPORT_MAX_RETRIES) {
|
|
420
|
+
const text = await res.text();
|
|
421
|
+
throw new Error(`Bundle import failed with status ${res.status}: ${text}`);
|
|
422
|
+
}
|
|
423
|
+
logger.warn(`Worker: bundle import for task ${meta.taskId} returned 409 (attempt ${attempt + 1}/${BUNDLE_IMPORT_MAX_RETRIES}); rebasing onto controller HEAD and retrying.`);
|
|
424
|
+
}
|
|
425
|
+
// Should not reach here, but satisfy TypeScript.
|
|
426
|
+
throw new Error("Bundle import failed after all retries");
|
|
427
|
+
}
|
|
428
|
+
finally {
|
|
429
|
+
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {
|
|
430
|
+
// best-effort temp cleanup
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
export async function readGitRepoState(config, remoteReady) {
|
|
435
|
+
const workspaceDir = resolveTeamClawWorkspaceDir();
|
|
436
|
+
const headCommit = await revParseOrEmpty(workspaceDir, "HEAD");
|
|
437
|
+
const headSummary = headCommit
|
|
438
|
+
? (await runGit(["log", "-1", "--pretty=%s"], { cwd: workspaceDir })).stdout.trim() || undefined
|
|
439
|
+
: undefined;
|
|
440
|
+
const dirty = await hasDirtyWorktree(workspaceDir);
|
|
441
|
+
return {
|
|
442
|
+
enabled: config.gitEnabled,
|
|
443
|
+
mode: remoteReady && config.gitRemoteUrl ? "remote" : "bundle",
|
|
444
|
+
defaultBranch: config.gitDefaultBranch,
|
|
445
|
+
remoteUrl: config.gitRemoteUrl || undefined,
|
|
446
|
+
remoteReady,
|
|
447
|
+
headCommit: headCommit || undefined,
|
|
448
|
+
headSummary,
|
|
449
|
+
dirty,
|
|
450
|
+
lastPreparedAt: Date.now(),
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
async function ensureOriginRemote(workspaceDir, config, logger) {
|
|
454
|
+
if (!config.gitRemoteUrl) {
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
const currentOrigin = await runGit(["remote", "get-url", "origin"], { cwd: workspaceDir }).catch(() => null);
|
|
458
|
+
if (!currentOrigin) {
|
|
459
|
+
await runGit(["remote", "add", "origin", config.gitRemoteUrl], { cwd: workspaceDir });
|
|
460
|
+
}
|
|
461
|
+
else if (currentOrigin.stdout.trim() !== config.gitRemoteUrl) {
|
|
462
|
+
await runGit(["remote", "set-url", "origin", config.gitRemoteUrl], { cwd: workspaceDir });
|
|
463
|
+
}
|
|
464
|
+
const remoteHeads = await tryGit(["ls-remote", "--heads", "origin", config.gitDefaultBranch], { cwd: workspaceDir });
|
|
465
|
+
if (remoteHeads.exitCode === 0 && remoteHeads.stdout.trim()) {
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
const pushResult = await tryGit(["push", "-u", "origin", `HEAD:refs/heads/${config.gitDefaultBranch}`], { cwd: workspaceDir });
|
|
469
|
+
if (pushResult.exitCode === 0) {
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
logger.warn(`TeamClaw: configured git remote is not ready; falling back to bundle sync. ${formatCommandError("git push", pushResult)}`);
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
async function checkoutTrackingBranch(workspaceDir, branch, trackingRef) {
|
|
476
|
+
const currentBranch = await currentBranchName(workspaceDir);
|
|
477
|
+
if (!currentBranch) {
|
|
478
|
+
await runGit(["checkout", "-f", "-B", branch, trackingRef], { cwd: workspaceDir });
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
if (currentBranch !== branch) {
|
|
482
|
+
const switchResult = await tryGit(["checkout", "-f", branch], { cwd: workspaceDir });
|
|
483
|
+
if (switchResult.exitCode !== 0) {
|
|
484
|
+
await runGit(["checkout", "-f", "-B", branch, trackingRef], { cwd: workspaceDir });
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
async function configureGitIdentity(workspaceDir, config) {
|
|
489
|
+
const currentName = (await tryGit(["config", "--get", "user.name"], { cwd: workspaceDir })).stdout.trim();
|
|
490
|
+
if (currentName !== config.gitAuthorName) {
|
|
491
|
+
await runGit(["config", "user.name", config.gitAuthorName], { cwd: workspaceDir });
|
|
492
|
+
}
|
|
493
|
+
const currentEmail = (await tryGit(["config", "--get", "user.email"], { cwd: workspaceDir })).stdout.trim();
|
|
494
|
+
if (currentEmail !== config.gitAuthorEmail) {
|
|
495
|
+
await runGit(["config", "user.email", config.gitAuthorEmail], { cwd: workspaceDir });
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
async function configureGitWorkspaceExcludes(workspaceDir) {
|
|
499
|
+
const gitDir = path.join(workspaceDir, ".git");
|
|
500
|
+
if (!await pathExists(gitDir)) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
const infoDir = path.join(gitDir, "info");
|
|
504
|
+
await fs.mkdir(infoDir, { recursive: true });
|
|
505
|
+
const excludePath = path.join(infoDir, "exclude");
|
|
506
|
+
const existing = await fs.readFile(excludePath, "utf8").catch(() => "");
|
|
507
|
+
const existingLines = new Set(existing.split(/\r?\n/).map((line) => line.trim()).filter(Boolean));
|
|
508
|
+
const missingPatterns = TEAMCLAW_RUNTIME_EXCLUDES.filter((pattern) => !existingLines.has(pattern));
|
|
509
|
+
if (missingPatterns.length === 0) {
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const prefix = existing.length === 0 ? "" : (existing.endsWith("\n") ? "" : "\n");
|
|
513
|
+
const header = existing.includes("# TeamClaw runtime workspace noise") ? "" : "# TeamClaw runtime workspace noise\n";
|
|
514
|
+
await fs.writeFile(excludePath, `${existing}${prefix}${header}${missingPatterns.join("\n")}\n`);
|
|
515
|
+
}
|
|
516
|
+
async function ensureBranchHead(workspaceDir, branch) {
|
|
517
|
+
const hasGit = await pathExists(path.join(workspaceDir, ".git"));
|
|
518
|
+
if (!hasGit) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
await runGit(["symbolic-ref", "HEAD", `refs/heads/${branch}`], { cwd: workspaceDir }).catch(() => {
|
|
522
|
+
// symbolic-ref can fail if HEAD is already attached to a branch; ignore
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
async function hasHeadCommit(workspaceDir) {
|
|
526
|
+
const result = await tryGit(["rev-parse", "--verify", "HEAD"], { cwd: workspaceDir });
|
|
527
|
+
return result.exitCode === 0;
|
|
528
|
+
}
|
|
529
|
+
async function hasDirtyWorktree(workspaceDir) {
|
|
530
|
+
if (!await pathExists(path.join(workspaceDir, ".git"))) {
|
|
531
|
+
return false;
|
|
532
|
+
}
|
|
533
|
+
const status = await runGit(["status", "--porcelain"], { cwd: workspaceDir });
|
|
534
|
+
return status.stdout.trim().length > 0;
|
|
535
|
+
}
|
|
536
|
+
async function revParse(workspaceDir, ref) {
|
|
537
|
+
const result = await runGit(["rev-parse", ref], { cwd: workspaceDir });
|
|
538
|
+
return result.stdout.trim();
|
|
539
|
+
}
|
|
540
|
+
async function revParseOrEmpty(workspaceDir, ref) {
|
|
541
|
+
const result = await tryGit(["rev-parse", ref], { cwd: workspaceDir });
|
|
542
|
+
return result.exitCode === 0 ? result.stdout.trim() : "";
|
|
543
|
+
}
|
|
544
|
+
async function currentBranchName(workspaceDir) {
|
|
545
|
+
const result = await tryGit(["branch", "--show-current"], { cwd: workspaceDir });
|
|
546
|
+
return result.exitCode === 0 ? result.stdout.trim() : "";
|
|
547
|
+
}
|
|
548
|
+
async function abortMergeIfNeeded(workspaceDir) {
|
|
549
|
+
await tryGit(["merge", "--abort"], { cwd: workspaceDir });
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Fetch the latest controller bundle and rebase local commits on top.
|
|
553
|
+
* Used as a recovery step when a bundle import fails with 409 Conflict.
|
|
554
|
+
*/
|
|
555
|
+
async function tryRebaseWorkerOntoController(workspaceDir, repoInfo, controllerUrl, config, logger) {
|
|
556
|
+
try {
|
|
557
|
+
if (repoInfo.mode === "shared") {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
// Build the bundle URL based on mode
|
|
561
|
+
const bundleUrlSuffix = repoInfo.mode === "remote" ? repoInfo.remoteUrl : repoInfo.bundleUrl;
|
|
562
|
+
if (!bundleUrlSuffix) {
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
if (repoInfo.mode === "remote") {
|
|
566
|
+
// Remote mode: fetch from origin and rebase
|
|
567
|
+
await runGit(["fetch", "origin", repoInfo.defaultBranch], { cwd: workspaceDir });
|
|
568
|
+
const remoteRef = `refs/remotes/origin/${repoInfo.defaultBranch}`;
|
|
569
|
+
if (!await revParseOrEmpty(workspaceDir, remoteRef)) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
const rebaseResult = await tryGit(["rebase", remoteRef], { cwd: workspaceDir });
|
|
573
|
+
if (rebaseResult.exitCode !== 0) {
|
|
574
|
+
await tryGit(["rebase", "--abort"], { cwd: workspaceDir });
|
|
575
|
+
// Fall back to merge if rebase fails
|
|
576
|
+
const mergeResult = await tryGit(["merge", "--no-edit", "-X", "theirs", remoteRef], { cwd: workspaceDir });
|
|
577
|
+
if (mergeResult.exitCode !== 0) {
|
|
578
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
579
|
+
logger.warn("Worker: failed to rebase or merge onto controller state; will retry bundle as-is.");
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
else {
|
|
584
|
+
// Bundle mode: download controller bundle, fetch it, and rebase
|
|
585
|
+
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "teamclaw-worker-rebase-"));
|
|
586
|
+
const tempBundlePath = path.join(tempDir, "controller.bundle");
|
|
587
|
+
try {
|
|
588
|
+
const res = await fetch(resolveApiUrl(bundleUrlSuffix, controllerUrl));
|
|
589
|
+
if (!res.ok) {
|
|
590
|
+
logger.warn(`Worker: failed to download controller bundle for rebase (status ${res.status}); will retry as-is.`);
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
594
|
+
await fs.writeFile(tempBundlePath, buffer);
|
|
595
|
+
const remoteBranch = `refs/remotes/teamclaw/${repoInfo.defaultBranch}`;
|
|
596
|
+
await runGit(["fetch", tempBundlePath, `refs/heads/${repoInfo.defaultBranch}:${remoteBranch}`], { cwd: workspaceDir });
|
|
597
|
+
const rebaseResult = await tryGit(["rebase", remoteBranch], { cwd: workspaceDir });
|
|
598
|
+
if (rebaseResult.exitCode !== 0) {
|
|
599
|
+
await tryGit(["rebase", "--abort"], { cwd: workspaceDir });
|
|
600
|
+
const mergeResult = await tryGit(["merge", "--no-edit", "-X", "theirs", remoteBranch], { cwd: workspaceDir });
|
|
601
|
+
if (mergeResult.exitCode !== 0) {
|
|
602
|
+
await abortMergeIfNeeded(workspaceDir);
|
|
603
|
+
logger.warn("Worker: failed to rebase or merge onto controller state; will retry bundle as-is.");
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
finally {
|
|
608
|
+
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => { });
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
catch (err) {
|
|
613
|
+
logger.warn(`Worker: rebase onto controller failed: ${err instanceof Error ? err.message : String(err)}; will retry bundle as-is.`);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
async function pathExists(filePath) {
|
|
617
|
+
try {
|
|
618
|
+
await fs.access(filePath);
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
catch {
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
async function runGit(args, options) {
|
|
626
|
+
const result = await runCommand("git", args, options);
|
|
627
|
+
if (result.exitCode !== 0) {
|
|
628
|
+
throw new Error(formatCommandError(`git ${args.join(" ")}`, result));
|
|
629
|
+
}
|
|
630
|
+
return result;
|
|
631
|
+
}
|
|
632
|
+
async function tryGit(args, options) {
|
|
633
|
+
return await runCommand("git", args, options);
|
|
634
|
+
}
|
|
635
|
+
async function withRepoLock(lockKey, operation) {
|
|
636
|
+
const prior = repoLocks.get(lockKey) ?? Promise.resolve();
|
|
637
|
+
let release;
|
|
638
|
+
const gate = new Promise((resolve) => {
|
|
639
|
+
release = resolve;
|
|
640
|
+
});
|
|
641
|
+
const queued = prior.then(() => gate);
|
|
642
|
+
repoLocks.set(lockKey, queued);
|
|
643
|
+
await prior;
|
|
644
|
+
try {
|
|
645
|
+
return await operation();
|
|
646
|
+
}
|
|
647
|
+
finally {
|
|
648
|
+
release();
|
|
649
|
+
if (repoLocks.get(lockKey) === queued) {
|
|
650
|
+
repoLocks.delete(lockKey);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
async function runCommand(command, args, options) {
|
|
655
|
+
return await new Promise((resolve, reject) => {
|
|
656
|
+
const child = spawn(command, args, {
|
|
657
|
+
cwd: options.cwd,
|
|
658
|
+
env: {
|
|
659
|
+
...process.env,
|
|
660
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
661
|
+
},
|
|
662
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
663
|
+
});
|
|
664
|
+
const stdoutChunks = [];
|
|
665
|
+
const stderrChunks = [];
|
|
666
|
+
child.stdout.on("data", (chunk) => {
|
|
667
|
+
stdoutChunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
668
|
+
});
|
|
669
|
+
child.stderr.on("data", (chunk) => {
|
|
670
|
+
stderrChunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
671
|
+
});
|
|
672
|
+
child.on("error", reject);
|
|
673
|
+
child.on("close", (code) => {
|
|
674
|
+
resolve({
|
|
675
|
+
stdout: Buffer.concat(stdoutChunks).toString("utf8"),
|
|
676
|
+
stderr: Buffer.concat(stderrChunks).toString("utf8"),
|
|
677
|
+
exitCode: code ?? 0,
|
|
678
|
+
});
|
|
679
|
+
});
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
function formatCommandError(command, result) {
|
|
683
|
+
const detail = result.stderr.trim() || result.stdout.trim() || `exit ${result.exitCode}`;
|
|
684
|
+
return `${command}: ${detail}`;
|
|
685
|
+
}
|
|
686
|
+
function resolveApiUrl(urlOrPath, controllerUrl) {
|
|
687
|
+
if (!urlOrPath) {
|
|
688
|
+
throw new Error("Missing controller API URL");
|
|
689
|
+
}
|
|
690
|
+
return new URL(urlOrPath, controllerUrl).toString();
|
|
691
|
+
}
|
|
692
|
+
function sanitizeRefPart(value) {
|
|
693
|
+
return value.replace(/[^a-zA-Z0-9._-]+/g, "-");
|
|
694
|
+
}
|