@teamclaws/teamclaw 2026.8.0 → 2026.11.0
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/cli.mjs +46 -15
- package/dist/api.js +1 -0
- package/dist/index.js +325 -0
- package/dist/src/config.js +412 -0
- package/dist/src/controller/clarification-routes.js +315 -0
- package/dist/src/controller/controller-capacity.js +14 -0
- package/dist/src/controller/controller-intake.js +895 -0
- package/dist/src/controller/controller-planning.js +813 -0
- package/dist/src/controller/controller-runs.js +175 -0
- package/dist/src/controller/controller-service.js +624 -0
- package/dist/src/controller/controller-tools.js +587 -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 +284 -0
- package/dist/src/controller/kickoff-orchestrator.js +246 -0
- package/dist/src/controller/managed-gateway-process.js +286 -0
- package/dist/src/controller/message-delivery.js +28 -0
- package/dist/src/controller/message-router.js +38 -0
- package/dist/src/controller/message-routes.js +114 -0
- package/dist/src/controller/orchestration-manifest.js +151 -0
- package/dist/src/controller/preview-manager.js +586 -0
- package/dist/src/controller/preview-proxy.js +98 -0
- package/dist/src/controller/profile-switch.js +289 -0
- package/dist/src/controller/project-registry.js +294 -0
- package/dist/src/controller/prompt-injector.js +197 -0
- package/dist/src/controller/repo-routes.js +63 -0
- package/dist/src/controller/role-inference.js +246 -0
- package/dist/src/controller/route-table.js +1353 -0
- package/dist/src/controller/task-assignment.js +508 -0
- package/dist/src/controller/task-cleanup.js +258 -0
- package/dist/src/controller/task-execution.js +240 -0
- package/dist/src/controller/task-results.js +461 -0
- package/dist/src/controller/task-router.js +137 -0
- package/dist/src/controller/task-routes.js +703 -0
- package/dist/src/controller/teamauto-routes.js +124 -0
- package/dist/src/controller/tunnel-manager.js +548 -0
- package/dist/src/controller/ui-assets.js +68 -0
- package/dist/src/controller/websocket.js +42 -0
- package/dist/src/controller/worker-provisioning.js +1674 -0
- package/dist/src/controller/worker-routes.js +188 -0
- package/dist/src/discovery.js +86 -0
- package/dist/src/git-collaboration.js +694 -0
- package/dist/src/identity.js +132 -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 +925 -0
- package/dist/src/profile-loader.js +486 -0
- package/dist/src/profiles/banking.json +178 -0
- package/dist/src/profiles/crossborder-ecom.json +253 -0
- package/dist/src/profiles/insurance.json +157 -0
- package/dist/src/profiles/knowledge/hs-structure.md +97 -0
- package/dist/src/profiles/knowledge/incoterms-2020.md +188 -0
- package/dist/src/profiles/knowledge/vat-principles.md +85 -0
- package/dist/src/profiles/schema.json +201 -0
- package/dist/src/profiles/securities.json +137 -0
- package/dist/src/profiles/software-rd.json +264 -0
- package/dist/src/profiles/trading-co.json +200 -0
- package/dist/src/prompt-policy.js +386 -0
- package/dist/src/protocol.js +58 -0
- package/dist/src/role-ids.js +29 -0
- package/dist/src/roles.js +390 -0
- package/dist/src/session-roles.js +14 -0
- package/dist/src/state.js +361 -0
- package/dist/src/task-executor.js +1023 -0
- package/dist/src/teamauto/github-client.js +210 -0
- package/dist/src/teamauto/norms.js +85 -0
- package/dist/src/teamauto/repo-sync.js +423 -0
- package/dist/src/teamauto/scanner.js +207 -0
- package/dist/src/teamauto/schedule.js +123 -0
- package/dist/src/teamauto/scheduler.js +907 -0
- package/dist/src/teamauto/store.js +133 -0
- package/dist/src/types.js +290 -0
- package/dist/src/ui/app.js +4860 -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 +447 -0
- package/dist/src/ui/style.css +2805 -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 +78 -0
- package/dist/src/worker/skill-installer.js +255 -0
- package/dist/src/worker/tools.js +541 -0
- package/dist/src/worker/worker-service.js +307 -0
- package/dist/src/workspace-browser.js +269 -0
- package/openclaw.plugin.json +28 -1
- package/package.json +3 -2
- package/src/config.ts +33 -2
- package/src/controller/clarification-routes.ts +404 -0
- package/src/controller/controller-intake.ts +1113 -0
- package/src/controller/controller-planning.ts +1029 -0
- package/src/controller/controller-runs.ts +239 -0
- package/src/controller/controller-service.ts +295 -41
- package/src/controller/controller-tools.ts +11 -7
- package/src/controller/http-server.ts +294 -6971
- package/src/controller/kickoff-orchestrator.ts +70 -7
- package/src/controller/message-delivery.ts +49 -0
- package/src/controller/message-routes.ts +137 -0
- package/src/controller/orchestration-manifest.ts +6 -29
- package/src/controller/preview-proxy.ts +127 -0
- package/src/controller/profile-switch.ts +366 -0
- package/src/controller/project-registry.ts +361 -0
- package/src/controller/prompt-injector.ts +23 -8
- package/src/controller/repo-routes.ts +91 -0
- package/src/controller/role-inference.ts +260 -22
- package/src/controller/route-table.ts +1616 -0
- package/src/controller/task-assignment.ts +618 -0
- package/src/controller/task-cleanup.ts +344 -0
- package/src/controller/task-execution.ts +356 -0
- package/src/controller/task-results.ts +544 -0
- package/src/controller/task-router.ts +98 -6
- package/src/controller/task-routes.ts +812 -0
- package/src/controller/teamauto-routes.ts +121 -0
- package/src/controller/ui-assets.ts +70 -0
- package/src/controller/websocket.ts +13 -1
- package/src/controller/worker-provisioning.ts +32 -4
- package/src/controller/worker-routes.ts +241 -0
- package/src/identity.ts +3 -2
- package/src/interaction-contracts.ts +7 -7
- package/src/openclaw-workspace.ts +44 -18
- package/src/profile-loader.ts +650 -0
- package/src/profiles/banking.json +178 -0
- package/src/profiles/crossborder-ecom.json +253 -0
- package/src/profiles/insurance.json +157 -0
- package/src/profiles/knowledge/hs-structure.md +97 -0
- package/src/profiles/knowledge/incoterms-2020.md +188 -0
- package/src/profiles/knowledge/vat-principles.md +85 -0
- package/src/profiles/schema.json +201 -0
- package/src/profiles/securities.json +137 -0
- package/src/profiles/software-rd.json +264 -0
- package/src/profiles/trading-co.json +200 -0
- package/src/prompt-policy.ts +27 -13
- package/src/role-ids.ts +31 -0
- package/src/roles.ts +60 -14
- package/src/session-roles.ts +14 -0
- package/src/state.ts +236 -9
- package/src/task-executor.ts +7 -18
- package/src/teamauto/github-client.ts +21 -0
- package/src/teamauto/repo-sync.ts +246 -6
- package/src/teamauto/scanner.ts +47 -13
- package/src/teamauto/scheduler.ts +259 -16
- package/src/teamauto/store.ts +12 -0
- package/src/types.ts +133 -16
- package/src/ui/app.js +592 -24
- package/src/ui/index.html +44 -11
- package/src/ui/style.css +194 -0
- package/src/worker/prompt-injector.ts +48 -8
- package/src/worker/skill-installer.ts +3 -1
- package/src/worker/worker-service.ts +83 -3
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// PR-C of issue #204 (http-server monolith split): the task/run/project
|
|
2
|
+
// cascade cleanup family extracted verbatim from controller/http-server.ts.
|
|
3
|
+
// Zero behavior change - callers pass the same deps object as before. This
|
|
4
|
+
// module must NEVER import http-server (the reverse edge exists:
|
|
5
|
+
// http-server -> task-cleanup); workspace git tracking and preview teardown
|
|
6
|
+
// arrive through direct peer imports and the deps slice below.
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { removeTrackedWorkspaceSubtree } from "../git-collaboration.js";
|
|
10
|
+
import { resolveTeamClawProjectsDir } from "../openclaw-workspace.js";
|
|
11
|
+
import { cancelTaskExecution, normalizeControllerIntakeSessionKey } from "./task-execution.js";
|
|
12
|
+
import { findProjectRecordByDir } from "./project-registry.js";
|
|
13
|
+
export function buildEmptyTaskCleanupSummary() {
|
|
14
|
+
return {
|
|
15
|
+
deletedTaskIds: [],
|
|
16
|
+
deletedClarificationIds: [],
|
|
17
|
+
deletedMessageIds: [],
|
|
18
|
+
stoppedPreviewIds: [],
|
|
19
|
+
deletedReportIds: [],
|
|
20
|
+
deletedProjectDirs: [],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function resolveSafeProjectDirectory(projectDir) {
|
|
24
|
+
const normalized = String(projectDir ?? "").trim().replace(/^\/+|\/+$/gu, "");
|
|
25
|
+
if (!normalized || normalized === "." || normalized === "..") {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (normalized.split("/").some((segment) => !segment || segment === "." || segment === "..")) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const projectsRoot = path.resolve(resolveTeamClawProjectsDir());
|
|
32
|
+
const projectPath = path.resolve(projectsRoot, normalized);
|
|
33
|
+
const relative = path.relative(projectsRoot, projectPath);
|
|
34
|
+
if (!relative || relative === "" || relative === "." || relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return projectPath;
|
|
38
|
+
}
|
|
39
|
+
export async function deleteProjectDirectoryIfUnused(projectDir, deps, summary) {
|
|
40
|
+
if (!projectDir) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const state = deps.getTeamState();
|
|
44
|
+
const stillReferencedByTask = Object.values(state?.tasks ?? {}).some((task) => task.projectDir === projectDir);
|
|
45
|
+
const stillReferencedByRun = Object.values(state?.controllerRuns ?? {}).some((run) => run.projectDir === projectDir);
|
|
46
|
+
if (stillReferencedByTask || stillReferencedByRun) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const safeProjectPath = resolveSafeProjectDirectory(projectDir);
|
|
50
|
+
if (!safeProjectPath) {
|
|
51
|
+
deps.logger.warn(`Controller: skipped unsafe project directory cleanup for ${String(projectDir)}`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (!fs.existsSync(safeProjectPath)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
await removeTrackedWorkspaceSubtree(deps.config, deps.logger, safeProjectPath);
|
|
58
|
+
await fs.promises.rm(safeProjectPath, { recursive: true, force: true });
|
|
59
|
+
if (fs.existsSync(safeProjectPath)) {
|
|
60
|
+
throw new Error(`Project directory cleanup could not be confirmed for ${projectDir}`);
|
|
61
|
+
}
|
|
62
|
+
deps.updateTeamState((stateDraft) => {
|
|
63
|
+
const projectRecord = findProjectRecordByDir(stateDraft, projectDir);
|
|
64
|
+
if (projectRecord) {
|
|
65
|
+
delete stateDraft.projects[projectRecord.id];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
summary.deletedProjectDirs.push(projectDir);
|
|
69
|
+
}
|
|
70
|
+
export function isTaskTerminalForCleanup(task) {
|
|
71
|
+
if (!task) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return task.status === "completed" || task.status === "failed";
|
|
75
|
+
}
|
|
76
|
+
export function canAutoDeleteStaleControllerRun(run, state, now, staleAfterMs) {
|
|
77
|
+
if (run.status !== "pending" && run.status !== "running") {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
const lastActivityAt = run.execution?.lastUpdatedAt ?? run.updatedAt ?? run.createdAt ?? 0;
|
|
81
|
+
if (!lastActivityAt || now - lastActivityAt < staleAfterMs) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const ownedTasks = Object.values(state?.tasks ?? {}).filter((task) => task.controllerRunId === run.id);
|
|
85
|
+
if (ownedTasks.some((task) => !isTaskTerminalForCleanup(task))) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
const hasPendingClarification = Object.values(state?.clarifications ?? {}).some((clarification) => clarification.controllerRunId === run.id && clarification.status === "pending");
|
|
89
|
+
return !hasPendingClarification;
|
|
90
|
+
}
|
|
91
|
+
export async function pruneStaleControllerRuns(deps, options) {
|
|
92
|
+
const state = deps.getTeamState();
|
|
93
|
+
const now = Date.now();
|
|
94
|
+
const staleAfterMs = deps.config.taskTimeoutMs * 2;
|
|
95
|
+
const staleRunIds = Object.values(state?.controllerRuns ?? {})
|
|
96
|
+
.filter((run) => run.id !== options?.excludeRunId)
|
|
97
|
+
.filter((run) => canAutoDeleteStaleControllerRun(run, state, now, staleAfterMs))
|
|
98
|
+
.sort((left, right) => left.updatedAt - right.updatedAt)
|
|
99
|
+
.map((run) => run.id);
|
|
100
|
+
for (const staleRunId of staleRunIds) {
|
|
101
|
+
await deleteControllerRunCascade(staleRunId, deps, { preserveSessionArtifacts: true });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export async function deleteProjectCascade(projectDir, deps) {
|
|
105
|
+
const safeProjectPath = resolveSafeProjectDirectory(projectDir);
|
|
106
|
+
if (!safeProjectPath) {
|
|
107
|
+
return { deleted: false, summary: { ...buildEmptyTaskCleanupSummary(), projectDir: undefined } };
|
|
108
|
+
}
|
|
109
|
+
const summary = {
|
|
110
|
+
...buildEmptyTaskCleanupSummary(),
|
|
111
|
+
projectDir,
|
|
112
|
+
};
|
|
113
|
+
const state = deps.getTeamState();
|
|
114
|
+
const runIds = Object.values(state?.controllerRuns ?? {})
|
|
115
|
+
.filter((run) => run.projectDir === projectDir)
|
|
116
|
+
.map((run) => run.id);
|
|
117
|
+
const taskIds = Object.values(state?.tasks ?? {})
|
|
118
|
+
.filter((task) => task.projectDir === projectDir)
|
|
119
|
+
.map((task) => task.id);
|
|
120
|
+
for (const runId of runIds) {
|
|
121
|
+
const result = await deleteControllerRunCascade(runId, deps);
|
|
122
|
+
if (!result.deleted) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
summary.deletedTaskIds.push(...result.summary.deletedTaskIds);
|
|
126
|
+
summary.deletedClarificationIds.push(...result.summary.deletedClarificationIds);
|
|
127
|
+
summary.deletedMessageIds.push(...result.summary.deletedMessageIds);
|
|
128
|
+
summary.stoppedPreviewIds.push(...result.summary.stoppedPreviewIds);
|
|
129
|
+
summary.deletedReportIds.push(...result.summary.deletedReportIds);
|
|
130
|
+
summary.deletedProjectDirs.push(...result.summary.deletedProjectDirs);
|
|
131
|
+
}
|
|
132
|
+
for (const taskId of taskIds) {
|
|
133
|
+
await deleteTaskCascade(taskId, deps, summary);
|
|
134
|
+
}
|
|
135
|
+
await deleteProjectDirectoryIfUnused(projectDir, deps, summary);
|
|
136
|
+
if (!summary.deletedProjectDirs.includes(projectDir) && !fs.existsSync(safeProjectPath)) {
|
|
137
|
+
summary.deletedProjectDirs.push(projectDir);
|
|
138
|
+
}
|
|
139
|
+
if (summary.deletedProjectDirs.includes(projectDir)) {
|
|
140
|
+
deps.wsServer.broadcastUpdate({ type: "project:deleted", data: { projectDir } });
|
|
141
|
+
}
|
|
142
|
+
return { deleted: summary.deletedProjectDirs.includes(projectDir), summary };
|
|
143
|
+
}
|
|
144
|
+
export async function deleteTaskCascade(taskId, deps, summary) {
|
|
145
|
+
const existingTask = deps.getTeamState()?.tasks[taskId];
|
|
146
|
+
if (!existingTask) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
await cancelTaskExecution(taskId, existingTask.assignedWorkerId, "cleanup deletion", deps);
|
|
150
|
+
await deps.previewManager?.stopTaskPreviews(taskId, "task deleted during cleanup");
|
|
151
|
+
const previewIds = Object.values(deps.getTeamState()?.previews ?? {})
|
|
152
|
+
.filter((preview) => preview.taskId === taskId)
|
|
153
|
+
.map((preview) => preview.id);
|
|
154
|
+
const deletedClarificationIds = [];
|
|
155
|
+
const deletedMessageIds = [];
|
|
156
|
+
const deletedReportIds = [];
|
|
157
|
+
const normalizedSessionKey = existingTask.controllerSessionKey
|
|
158
|
+
? normalizeControllerIntakeSessionKey(existingTask.controllerSessionKey)
|
|
159
|
+
: undefined;
|
|
160
|
+
deps.updateTeamState((state) => {
|
|
161
|
+
delete state.tasks[taskId];
|
|
162
|
+
for (const clarification of Object.values(state.clarifications)) {
|
|
163
|
+
if (clarification.taskId === taskId) {
|
|
164
|
+
deletedClarificationIds.push(clarification.id);
|
|
165
|
+
delete state.clarifications[clarification.id];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (state.messages.length > 0) {
|
|
169
|
+
const keptMessages = [];
|
|
170
|
+
for (const message of state.messages) {
|
|
171
|
+
if (message.taskId === taskId) {
|
|
172
|
+
deletedMessageIds.push(message.id);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
keptMessages.push(message);
|
|
176
|
+
}
|
|
177
|
+
state.messages = keptMessages;
|
|
178
|
+
}
|
|
179
|
+
if (normalizedSessionKey && state.reports) {
|
|
180
|
+
for (const report of Object.values(state.reports)) {
|
|
181
|
+
if (normalizeControllerIntakeSessionKey(report.sessionKey) === normalizedSessionKey) {
|
|
182
|
+
deletedReportIds.push(report.id);
|
|
183
|
+
delete state.reports[report.id];
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const run of Object.values(state.controllerRuns)) {
|
|
188
|
+
if (run.sourceTaskId === taskId) {
|
|
189
|
+
delete run.sourceTaskId;
|
|
190
|
+
}
|
|
191
|
+
if (run.createdTaskIds.includes(taskId)) {
|
|
192
|
+
run.createdTaskIds = run.createdTaskIds.filter((id) => id !== taskId);
|
|
193
|
+
run.updatedAt = Date.now();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
summary.deletedTaskIds.push(taskId);
|
|
198
|
+
summary.deletedClarificationIds.push(...deletedClarificationIds);
|
|
199
|
+
summary.deletedMessageIds.push(...deletedMessageIds);
|
|
200
|
+
summary.stoppedPreviewIds.push(...previewIds);
|
|
201
|
+
summary.deletedReportIds.push(...deletedReportIds);
|
|
202
|
+
await deleteProjectDirectoryIfUnused(existingTask.projectDir, deps, summary);
|
|
203
|
+
deps.wsServer.broadcastUpdate({ type: "task:deleted", data: { taskId } });
|
|
204
|
+
for (const clarificationId of deletedClarificationIds) {
|
|
205
|
+
deps.wsServer.broadcastUpdate({ type: "clarification:deleted", data: { clarificationId, taskId } });
|
|
206
|
+
}
|
|
207
|
+
for (const messageId of deletedMessageIds) {
|
|
208
|
+
deps.wsServer.broadcastUpdate({ type: "message:deleted", data: { messageId, taskId } });
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
export async function deleteControllerRunCascade(runId, deps, options) {
|
|
213
|
+
const run = deps.getTeamState()?.controllerRuns[runId];
|
|
214
|
+
if (!run) {
|
|
215
|
+
return { deleted: false, summary: { ...buildEmptyTaskCleanupSummary() } };
|
|
216
|
+
}
|
|
217
|
+
const summary = {
|
|
218
|
+
...buildEmptyTaskCleanupSummary(),
|
|
219
|
+
deletedRunId: undefined,
|
|
220
|
+
};
|
|
221
|
+
const linkedTaskIds = new Set(run.createdTaskIds);
|
|
222
|
+
for (const task of Object.values(deps.getTeamState()?.tasks ?? {})) {
|
|
223
|
+
if (task.controllerRunId === run.id) {
|
|
224
|
+
linkedTaskIds.add(task.id);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
for (const taskId of linkedTaskIds) {
|
|
228
|
+
await deleteTaskCascade(taskId, deps, summary);
|
|
229
|
+
}
|
|
230
|
+
const deletedClarificationIds = [];
|
|
231
|
+
const deletedReportIds = [];
|
|
232
|
+
deps.updateTeamState((state) => {
|
|
233
|
+
delete state.controllerRuns[runId];
|
|
234
|
+
summary.deletedRunId = runId;
|
|
235
|
+
for (const clarification of Object.values(state.clarifications)) {
|
|
236
|
+
if (clarification.controllerRunId === runId) {
|
|
237
|
+
deletedClarificationIds.push(clarification.id);
|
|
238
|
+
delete state.clarifications[clarification.id];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (state.reports) {
|
|
242
|
+
const normalizedSessionKey = normalizeControllerIntakeSessionKey(run.sessionKey);
|
|
243
|
+
for (const report of Object.values(state.reports)) {
|
|
244
|
+
if (!options?.preserveSessionArtifacts && normalizeControllerIntakeSessionKey(report.sessionKey) === normalizedSessionKey) {
|
|
245
|
+
deletedReportIds.push(report.id);
|
|
246
|
+
delete state.reports[report.id];
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
summary.deletedClarificationIds.push(...deletedClarificationIds);
|
|
252
|
+
summary.deletedReportIds.push(...deletedReportIds);
|
|
253
|
+
deps.wsServer.broadcastUpdate({ type: "controller:run_deleted", data: { runId } });
|
|
254
|
+
for (const clarificationId of deletedClarificationIds) {
|
|
255
|
+
deps.wsServer.broadcastUpdate({ type: "clarification:deleted", data: { clarificationId, controllerRunId: runId } });
|
|
256
|
+
}
|
|
257
|
+
return { deleted: true, summary };
|
|
258
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { generateId } from "../protocol.js";
|
|
2
|
+
import { buildTeamClawAgentSessionKey } from "../openclaw-workspace.js";
|
|
3
|
+
export const MAX_TASK_EXECUTION_EVENTS = 250;
|
|
4
|
+
const CONTROLLER_INTAKE_SESSION_PREFIX = "teamclaw-controller-web:";
|
|
5
|
+
const CONTROLLER_INTAKE_AGENT_SESSION_RE = /^agent:[^:]+:(teamclaw-controller-web:[a-zA-Z0-9:_-]{1,120})$/;
|
|
6
|
+
export function mapTaskStatusToExecutionStatus(taskStatus, current) {
|
|
7
|
+
switch (taskStatus) {
|
|
8
|
+
case "completed":
|
|
9
|
+
return "completed";
|
|
10
|
+
case "failed":
|
|
11
|
+
return "failed";
|
|
12
|
+
case "in_progress":
|
|
13
|
+
case "review":
|
|
14
|
+
return "running";
|
|
15
|
+
case "pending":
|
|
16
|
+
case "assigned":
|
|
17
|
+
return current ?? "pending";
|
|
18
|
+
case "blocked":
|
|
19
|
+
return current ?? "running";
|
|
20
|
+
default:
|
|
21
|
+
return current ?? "pending";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function ensureTaskExecution(task) {
|
|
25
|
+
if (!task.execution) {
|
|
26
|
+
task.execution = {
|
|
27
|
+
status: mapTaskStatusToExecutionStatus(task.status),
|
|
28
|
+
startedAt: task.startedAt,
|
|
29
|
+
endedAt: task.completedAt,
|
|
30
|
+
lastUpdatedAt: task.updatedAt,
|
|
31
|
+
events: [],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (!Array.isArray(task.execution.events)) {
|
|
35
|
+
task.execution.events = [];
|
|
36
|
+
}
|
|
37
|
+
task.execution.status = task.execution.status ?? mapTaskStatusToExecutionStatus(task.status);
|
|
38
|
+
task.execution.startedAt = task.execution.startedAt ?? task.startedAt;
|
|
39
|
+
task.execution.endedAt = task.execution.endedAt ?? task.completedAt;
|
|
40
|
+
task.execution.lastUpdatedAt = task.execution.lastUpdatedAt ?? task.updatedAt;
|
|
41
|
+
return task.execution;
|
|
42
|
+
}
|
|
43
|
+
export function resetTaskForFreshAttempt(task) {
|
|
44
|
+
delete task.startedAt;
|
|
45
|
+
delete task.completedAt;
|
|
46
|
+
delete task.result;
|
|
47
|
+
delete task.error;
|
|
48
|
+
delete task.resultContract;
|
|
49
|
+
if (task.execution) {
|
|
50
|
+
task.execution.status = "pending";
|
|
51
|
+
delete task.execution.runId;
|
|
52
|
+
delete task.execution.sessionKey;
|
|
53
|
+
delete task.execution.startedAt;
|
|
54
|
+
delete task.execution.endedAt;
|
|
55
|
+
task.execution.lastUpdatedAt = Date.now();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export function buildTaskExecutionIdentity(taskId, workerId) {
|
|
59
|
+
const attemptId = generateId();
|
|
60
|
+
return {
|
|
61
|
+
executionSessionKey: buildTeamClawAgentSessionKey(`teamclaw-task-${taskId}-${attemptId}`),
|
|
62
|
+
executionIdempotencyKey: `teamclaw-${taskId}-${workerId}-${attemptId}`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export function appendTaskExecutionEvent(task, input) {
|
|
66
|
+
const now = input.createdAt ?? Date.now();
|
|
67
|
+
const execution = ensureTaskExecution(task);
|
|
68
|
+
if (input.runId) {
|
|
69
|
+
execution.runId = input.runId;
|
|
70
|
+
}
|
|
71
|
+
if (input.sessionKey) {
|
|
72
|
+
execution.sessionKey = input.sessionKey;
|
|
73
|
+
}
|
|
74
|
+
if (input.status) {
|
|
75
|
+
execution.status = input.status;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
execution.status = mapTaskStatusToExecutionStatus(task.status, execution.status);
|
|
79
|
+
}
|
|
80
|
+
if ((input.status === "running" || input.phase === "run_started") && !execution.startedAt) {
|
|
81
|
+
execution.startedAt = now;
|
|
82
|
+
}
|
|
83
|
+
if ((input.status === "running" || input.phase === "run_started") && !task.startedAt) {
|
|
84
|
+
task.startedAt = now;
|
|
85
|
+
}
|
|
86
|
+
if ((input.status === "running" || input.phase === "run_started") && (task.status === "pending" || task.status === "assigned")) {
|
|
87
|
+
task.status = "in_progress";
|
|
88
|
+
}
|
|
89
|
+
if (execution.status === "completed" || execution.status === "failed") {
|
|
90
|
+
execution.endedAt = execution.endedAt ?? now;
|
|
91
|
+
}
|
|
92
|
+
execution.lastUpdatedAt = now;
|
|
93
|
+
task.updatedAt = now;
|
|
94
|
+
const event = {
|
|
95
|
+
id: generateId(),
|
|
96
|
+
type: input.type,
|
|
97
|
+
createdAt: now,
|
|
98
|
+
message: input.message,
|
|
99
|
+
phase: input.phase,
|
|
100
|
+
source: input.source,
|
|
101
|
+
stream: input.stream,
|
|
102
|
+
role: input.role ?? task.assignedRole,
|
|
103
|
+
workerId: input.workerId ?? task.assignedWorkerId,
|
|
104
|
+
};
|
|
105
|
+
execution.events.push(event);
|
|
106
|
+
if (execution.events.length > MAX_TASK_EXECUTION_EVENTS) {
|
|
107
|
+
execution.events = execution.events.slice(-MAX_TASK_EXECUTION_EVENTS);
|
|
108
|
+
}
|
|
109
|
+
return event;
|
|
110
|
+
}
|
|
111
|
+
export function buildTaskExecutionSummary(execution) {
|
|
112
|
+
if (!execution) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
status: execution.status,
|
|
117
|
+
runId: execution.runId,
|
|
118
|
+
startedAt: execution.startedAt,
|
|
119
|
+
endedAt: execution.endedAt,
|
|
120
|
+
lastUpdatedAt: execution.lastUpdatedAt,
|
|
121
|
+
eventCount: execution.events.length,
|
|
122
|
+
lastEvent: execution.events[execution.events.length - 1],
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export function serializeTask(task, includeExecutionEvents = false) {
|
|
126
|
+
if (!task) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
const payload = { ...task };
|
|
130
|
+
if (!task.execution) {
|
|
131
|
+
return payload;
|
|
132
|
+
}
|
|
133
|
+
payload.execution = includeExecutionEvents
|
|
134
|
+
? {
|
|
135
|
+
status: task.execution.status,
|
|
136
|
+
runId: task.execution.runId,
|
|
137
|
+
startedAt: task.execution.startedAt,
|
|
138
|
+
endedAt: task.execution.endedAt,
|
|
139
|
+
lastUpdatedAt: task.execution.lastUpdatedAt,
|
|
140
|
+
events: task.execution.events.map((event) => ({ ...event })),
|
|
141
|
+
}
|
|
142
|
+
: buildTaskExecutionSummary(task.execution);
|
|
143
|
+
return payload;
|
|
144
|
+
}
|
|
145
|
+
export function normalizeControllerIntakeSessionKey(input) {
|
|
146
|
+
const fallback = `${CONTROLLER_INTAKE_SESSION_PREFIX}default`;
|
|
147
|
+
if (typeof input !== "string") {
|
|
148
|
+
return fallback;
|
|
149
|
+
}
|
|
150
|
+
const trimmed = input.trim();
|
|
151
|
+
if (!trimmed) {
|
|
152
|
+
return fallback;
|
|
153
|
+
}
|
|
154
|
+
const runtimeMatch = trimmed.match(CONTROLLER_INTAKE_AGENT_SESSION_RE);
|
|
155
|
+
if (trimmed.startsWith("agent:") && !runtimeMatch) {
|
|
156
|
+
return fallback;
|
|
157
|
+
}
|
|
158
|
+
const logicalKey = runtimeMatch?.[1] ?? trimmed;
|
|
159
|
+
if (!/^[a-zA-Z0-9:_-]{1,120}$/.test(logicalKey)) {
|
|
160
|
+
return fallback;
|
|
161
|
+
}
|
|
162
|
+
const normalizedLogicalKey = logicalKey.startsWith(CONTROLLER_INTAKE_SESSION_PREFIX)
|
|
163
|
+
? logicalKey
|
|
164
|
+
: `${CONTROLLER_INTAKE_SESSION_PREFIX}${logicalKey}`;
|
|
165
|
+
return buildTeamClawAgentSessionKey(normalizedLogicalKey);
|
|
166
|
+
}
|
|
167
|
+
export function broadcastTaskExecutionEvent(taskId, task, event, deps) {
|
|
168
|
+
deps.wsServer.broadcastUpdate({
|
|
169
|
+
type: "task:execution",
|
|
170
|
+
data: {
|
|
171
|
+
taskId,
|
|
172
|
+
event,
|
|
173
|
+
execution: buildTaskExecutionSummary(task.execution),
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
export function recordTaskExecutionEvent(taskId, input, deps) {
|
|
178
|
+
const { updateTeamState, wsServer } = deps;
|
|
179
|
+
let statusChanged = false;
|
|
180
|
+
let event;
|
|
181
|
+
const state = updateTeamState((s) => {
|
|
182
|
+
const task = s.tasks[taskId];
|
|
183
|
+
if (!task) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const previousStatus = task.status;
|
|
187
|
+
event = appendTaskExecutionEvent(task, input);
|
|
188
|
+
statusChanged = previousStatus !== task.status;
|
|
189
|
+
});
|
|
190
|
+
const updatedTask = state.tasks[taskId];
|
|
191
|
+
if (updatedTask && event) {
|
|
192
|
+
broadcastTaskExecutionEvent(taskId, updatedTask, event, deps);
|
|
193
|
+
if (statusChanged) {
|
|
194
|
+
wsServer.broadcastUpdate({ type: "task:updated", data: serializeTask(updatedTask) });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return { task: updatedTask, event, statusChanged };
|
|
198
|
+
}
|
|
199
|
+
export function canAcceptWorkerUpdate(task, workerId) {
|
|
200
|
+
if (!task || task.assignedWorkerId !== workerId || task.completedAt) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
return task.status === "assigned" ||
|
|
204
|
+
task.status === "in_progress" ||
|
|
205
|
+
task.status === "review" ||
|
|
206
|
+
task.status === "completed" ||
|
|
207
|
+
task.status === "failed";
|
|
208
|
+
}
|
|
209
|
+
export async function cancelTaskExecution(taskId, workerId, reason, deps) {
|
|
210
|
+
if (!workerId) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const worker = deps.getTeamState()?.workers[workerId];
|
|
214
|
+
if (!worker) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
let cancelled = false;
|
|
218
|
+
try {
|
|
219
|
+
const res = await fetch(`${worker.url}/api/v1/tasks/${taskId}/cancel`, {
|
|
220
|
+
method: "POST",
|
|
221
|
+
});
|
|
222
|
+
cancelled = res.ok;
|
|
223
|
+
if (!res.ok) {
|
|
224
|
+
deps.logger.warn(`Controller: worker cancel failed for ${taskId} on ${workerId} (${res.status})`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
catch (err) {
|
|
228
|
+
deps.logger.warn(`Controller: failed to cancel task ${taskId} on ${workerId}: ${String(err)}`);
|
|
229
|
+
}
|
|
230
|
+
if (!cancelled) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
recordTaskExecutionEvent(taskId, {
|
|
234
|
+
type: "lifecycle",
|
|
235
|
+
phase: "execution_cancelled",
|
|
236
|
+
source: "controller",
|
|
237
|
+
message: `Cancelled active execution before ${reason}.`,
|
|
238
|
+
workerId,
|
|
239
|
+
}, deps);
|
|
240
|
+
}
|