@teamclaws/teamclaw 2026.8.1 → 2026.11.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/cli.mjs +318 -17
- package/dist/index.js +54 -32
- package/dist/src/config.js +57 -2
- package/dist/src/controller/auth-routes.js +129 -0
- package/dist/src/controller/clarification-routes.js +315 -0
- package/dist/src/controller/controller-intake.js +895 -0
- package/dist/src/controller/controller-planning.js +814 -0
- package/dist/src/controller/controller-routes.js +195 -0
- package/dist/src/controller/controller-runs.js +175 -0
- package/dist/src/controller/controller-service.js +338 -50
- package/dist/src/controller/controller-tools.js +14 -7
- package/dist/src/controller/github-auth.js +2 -1
- package/dist/src/controller/http-server.js +290 -6003
- package/dist/src/controller/kickoff-orchestrator.js +33 -43
- package/dist/src/controller/message-delivery.js +31 -0
- package/dist/src/controller/message-routes.js +114 -0
- package/dist/src/controller/orchestration-manifest.js +11 -93
- package/dist/src/controller/preview-manager.js +51 -3
- package/dist/src/controller/preview-proxy.js +98 -0
- package/dist/src/controller/profile-switch.js +309 -0
- package/dist/src/controller/project-registry.js +294 -0
- package/dist/src/controller/prompt-injector.js +19 -9
- package/dist/src/controller/provisioning/backend-contract.js +9 -0
- package/dist/src/controller/provisioning/container-shared.js +64 -0
- package/dist/src/controller/provisioning/docker.js +214 -0
- package/dist/src/controller/provisioning/kubernetes.js +244 -0
- package/dist/src/controller/provisioning/plugin-root.js +41 -0
- package/dist/src/controller/provisioning/process-supervisor.js +190 -0
- package/dist/src/controller/provisioning-config.js +239 -0
- package/dist/src/controller/repo-routes.js +129 -0
- package/dist/src/controller/report-routes.js +56 -0
- package/dist/src/controller/role-inference.js +231 -20
- package/dist/src/controller/route-table.js +375 -0
- package/dist/src/controller/security.js +775 -0
- package/dist/src/controller/status-routes.js +287 -0
- package/dist/src/controller/task-assignment.js +511 -0
- package/dist/src/controller/task-cleanup.js +258 -0
- package/dist/src/controller/task-execution.js +271 -0
- package/dist/src/controller/task-results.js +471 -0
- package/dist/src/controller/task-router.js +74 -4
- 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 +41 -8
- package/dist/src/controller/tunnel-routes.js +225 -0
- package/dist/src/controller/ui-assets.js +68 -0
- package/dist/src/controller/websocket.js +6 -2
- package/dist/src/controller/webui-routes.js +49 -0
- package/dist/src/controller/worker-provisioning.js +69 -953
- package/dist/src/controller/worker-routes.js +188 -0
- package/dist/src/controller/workflow-routes.js +407 -0
- package/dist/src/controller/workspace-routes.js +200 -0
- package/dist/src/git-collaboration.js +4 -9
- package/dist/src/identity.js +15 -3
- package/dist/src/interaction-contracts.js +7 -7
- package/dist/src/openclaw-workspace.js +40 -18
- package/dist/src/profile-loader.js +604 -0
- package/dist/src/profiles/banking.json +202 -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 +281 -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 +42 -17
- package/dist/src/protocol.js +66 -3
- package/dist/src/role-ids.js +29 -0
- package/dist/src/roles.js +54 -13
- package/dist/src/session-roles.js +14 -0
- package/dist/src/shared/clarification-schema.js +65 -0
- package/dist/src/shared/github-auth.js +15 -0
- package/dist/src/shared/kickoff-prompt.js +37 -0
- package/dist/src/shared/utils.js +47 -0
- package/dist/src/state.js +275 -11
- package/dist/src/task-executor.js +7 -17
- package/dist/src/teamauto/github-client.js +18 -0
- package/dist/src/teamauto/repo-sync.js +228 -6
- package/dist/src/teamauto/scanner.js +40 -12
- package/dist/src/teamauto/scheduler.js +231 -12
- package/dist/src/types.js +77 -16
- package/dist/src/ui/app.js +1434 -82
- package/dist/src/ui/github-login.html +1 -0
- package/dist/src/ui/index.html +144 -45
- package/dist/src/ui/style.css +194 -0
- package/dist/src/worker/http-handler.js +59 -0
- package/dist/src/worker/prompt-injector.js +44 -8
- package/dist/src/worker/skill-installer.js +6 -10
- package/dist/src/worker/tools.js +20 -10
- package/dist/src/worker/worker-service.js +93 -7
- package/index.ts +61 -32
- package/openclaw.plugin.json +50 -2
- package/package.json +3 -2
- package/src/config.ts +57 -2
- package/src/controller/auth-routes.ts +157 -0
- package/src/controller/clarification-routes.ts +404 -0
- package/src/controller/controller-intake.ts +1113 -0
- package/src/controller/controller-planning.ts +1037 -0
- package/src/controller/controller-routes.ts +237 -0
- package/src/controller/controller-runs.ts +239 -0
- package/src/controller/controller-service.ts +422 -50
- package/src/controller/controller-tools.ts +16 -9
- package/src/controller/github-auth.ts +28 -63
- package/src/controller/http-server.ts +336 -6964
- package/src/controller/kickoff-orchestrator.ts +69 -45
- package/src/controller/message-delivery.ts +52 -0
- package/src/controller/message-routes.ts +137 -0
- package/src/controller/orchestration-manifest.ts +14 -100
- package/src/controller/preview-manager.ts +53 -3
- package/src/controller/preview-proxy.ts +127 -0
- package/src/controller/profile-switch.ts +396 -0
- package/src/controller/project-registry.ts +361 -0
- package/src/controller/prompt-injector.ts +23 -8
- package/src/controller/provisioning/backend-contract.ts +51 -0
- package/src/controller/provisioning/container-shared.ts +84 -0
- package/src/controller/provisioning/docker.ts +260 -0
- package/src/controller/provisioning/kubernetes.ts +263 -0
- package/src/controller/provisioning/plugin-root.ts +42 -0
- package/src/controller/provisioning/process-supervisor.ts +214 -0
- package/src/controller/provisioning-config.ts +289 -0
- package/src/controller/repo-routes.ts +163 -0
- package/src/controller/report-routes.ts +64 -0
- package/src/controller/role-inference.ts +260 -22
- package/src/controller/route-table.ts +597 -0
- package/src/controller/security.ts +899 -0
- package/src/controller/status-routes.ts +354 -0
- package/src/controller/task-assignment.ts +622 -0
- package/src/controller/task-cleanup.ts +344 -0
- package/src/controller/task-execution.ts +399 -0
- package/src/controller/task-results.ts +556 -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/tunnel-manager.ts +45 -8
- package/src/controller/tunnel-routes.ts +231 -0
- package/src/controller/ui-assets.ts +70 -0
- package/src/controller/websocket.ts +32 -4
- package/src/controller/webui-routes.ts +55 -0
- package/src/controller/worker-provisioning.ts +83 -1121
- package/src/controller/worker-routes.ts +241 -0
- package/src/controller/workflow-routes.ts +498 -0
- package/src/controller/workspace-routes.ts +222 -0
- package/src/git-collaboration.ts +4 -9
- package/src/identity.ts +16 -3
- package/src/interaction-contracts.ts +7 -7
- package/src/openclaw-workspace.ts +44 -18
- package/src/profile-loader.ts +816 -0
- package/src/profiles/banking.json +202 -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 +281 -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 +44 -17
- package/src/protocol.ts +72 -2
- package/src/role-ids.ts +31 -0
- package/src/roles.ts +60 -14
- package/src/session-roles.ts +14 -0
- package/src/shared/clarification-schema.ts +81 -0
- package/src/shared/github-auth.ts +92 -0
- package/src/shared/kickoff-prompt.ts +47 -0
- package/src/shared/utils.ts +51 -0
- package/src/state.ts +326 -10
- package/src/task-executor.ts +7 -18
- package/src/teamauto/github-client.ts +23 -1
- package/src/teamauto/repo-sync.ts +248 -7
- package/src/teamauto/scanner.ts +47 -13
- package/src/teamauto/scheduler.ts +261 -17
- package/src/teamauto/store.ts +12 -0
- package/src/types.ts +263 -17
- package/src/ui/app.js +1434 -82
- package/src/ui/github-login.html +1 -0
- package/src/ui/index.html +144 -45
- package/src/ui/style.css +194 -0
- package/src/worker/http-handler.ts +64 -1
- package/src/worker/prompt-injector.ts +48 -8
- package/src/worker/skill-installer.ts +6 -10
- package/src/worker/tools.ts +21 -10
- package/src/worker/worker-service.ts +101 -7
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Issue #226 W2 (route-table carve-up): the controller runs / projects /
|
|
2
|
+
// intake route handlers moved verbatim from route-table.ts (PR-D had moved
|
|
3
|
+
// them verbatim from http-server.ts before that) - bodies byte-identical,
|
|
4
|
+
// only the module home changed. route-table.ts keeps the table entries that
|
|
5
|
+
// bind these handlers and re-exports the retry-hint constant for
|
|
6
|
+
// http-server.ts's dispatch gates. Like security.ts, this module imports
|
|
7
|
+
// route-table.js as TYPE ONLY (RouteContext), so the module graph stays
|
|
8
|
+
// acyclic.
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
import { parseJsonBody, sendJson, sendError } from "../protocol.js";
|
|
11
|
+
import { normalizeControllerManifest } from "./orchestration-manifest.js";
|
|
12
|
+
import {
|
|
13
|
+
normalizeControllerIntakeSessionKey,
|
|
14
|
+
} from "./task-execution.js";
|
|
15
|
+
import {
|
|
16
|
+
findLatestControllerRunIdForSession,
|
|
17
|
+
syncProjectRegistryEntry,
|
|
18
|
+
} from "./project-registry.js";
|
|
19
|
+
import {
|
|
20
|
+
appendControllerRunEvent,
|
|
21
|
+
serializeControllerRun,
|
|
22
|
+
updateControllerRun,
|
|
23
|
+
} from "./controller-runs.js";
|
|
24
|
+
import {
|
|
25
|
+
deleteControllerRunCascade,
|
|
26
|
+
deleteProjectCascade,
|
|
27
|
+
resolveSafeProjectDirectory,
|
|
28
|
+
} from "./task-cleanup.js";
|
|
29
|
+
import {
|
|
30
|
+
ControllerIntakeSwitchInProgressError,
|
|
31
|
+
reconcileControllerClarifications,
|
|
32
|
+
runControllerIntake,
|
|
33
|
+
} from "./controller-intake.js";
|
|
34
|
+
import { buildControllerManifestEventMessage } from "./controller-planning.js";
|
|
35
|
+
import {
|
|
36
|
+
buildStartupReadinessMessage,
|
|
37
|
+
shouldBlockControllerUntilProvisioningReady,
|
|
38
|
+
} from "./worker-routes.js";
|
|
39
|
+
import type { RouteContext } from "./route-table.js";
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Retry hint shared by the transition-matrix gate (http-server.ts, task
|
|
43
|
+
* submission / repo import) and the intake handler in this module. Lived in
|
|
44
|
+
* route-table.ts since PR-D (the intake handler body carried it in 503
|
|
45
|
+
* bodies); W2 moved it here with its consumer - route-table re-exports it so
|
|
46
|
+
* http-server.ts's gate import is unchanged.
|
|
47
|
+
*/
|
|
48
|
+
export const SWITCH_TASK_SUBMISSION_RETRY_AFTER_SECONDS = 5;
|
|
49
|
+
|
|
50
|
+
async function controllerManifestRoute(ctx: RouteContext): Promise<void> {
|
|
51
|
+
const { req, res, deps } = ctx;
|
|
52
|
+
const { logger, getTeamState, updateTeamState, wsServer } = deps;
|
|
53
|
+
const body = await parseJsonBody(req);
|
|
54
|
+
const sessionKey = normalizeControllerIntakeSessionKey(body.sessionKey);
|
|
55
|
+
const manifest = normalizeControllerManifest(body.manifest);
|
|
56
|
+
if (!manifest) {
|
|
57
|
+
sendError(res, 400, "manifest is required and must include requirementSummary");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const runId = findLatestControllerRunIdForSession(sessionKey, deps.getTeamState(), {
|
|
62
|
+
preferActive: true,
|
|
63
|
+
});
|
|
64
|
+
if (!runId) {
|
|
65
|
+
sendError(res, 404, "Controller run not found for session");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const updatedRun = updateControllerRun(runId, deps, (run) => {
|
|
70
|
+
run.manifest = manifest;
|
|
71
|
+
const state = deps.getTeamState();
|
|
72
|
+
if (state) {
|
|
73
|
+
const project = syncProjectRegistryEntry(state, {
|
|
74
|
+
projectId: manifest.projectName ?? run.projectId,
|
|
75
|
+
projectDir: run.projectDir ?? manifest.projectName,
|
|
76
|
+
aliases: [manifest.projectName, run.projectId],
|
|
77
|
+
summary: manifest.requirementSummary,
|
|
78
|
+
updatedAt: Date.now(),
|
|
79
|
+
});
|
|
80
|
+
run.projectId = project.projectId;
|
|
81
|
+
run.projectDir = project.projectDir;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (run.projectDir) {
|
|
85
|
+
const state = deps.getTeamState();
|
|
86
|
+
if (state) {
|
|
87
|
+
for (const taskId of run.createdTaskIds) {
|
|
88
|
+
const task = state.tasks[taskId];
|
|
89
|
+
if (task) {
|
|
90
|
+
if (!task.projectDir) {
|
|
91
|
+
task.projectDir = run.projectDir;
|
|
92
|
+
}
|
|
93
|
+
if (!task.projectId) {
|
|
94
|
+
task.projectId = run.projectId;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
appendControllerRunEvent(run, {
|
|
102
|
+
type: "output",
|
|
103
|
+
phase: "manifest_recorded",
|
|
104
|
+
source: "controller",
|
|
105
|
+
status: "running",
|
|
106
|
+
sessionKey,
|
|
107
|
+
message: buildControllerManifestEventMessage(manifest),
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
if (!updatedRun) {
|
|
112
|
+
sendError(res, 404, "Controller run not found");
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (manifest.requirementFullyComplete) {
|
|
117
|
+
logger.info(`Controller: requirement fully complete for session ${sessionKey} (run ${runId})`);
|
|
118
|
+
wsServer.broadcastUpdate({
|
|
119
|
+
type: "requirement:complete",
|
|
120
|
+
data: {
|
|
121
|
+
runId,
|
|
122
|
+
sessionKey,
|
|
123
|
+
requirementSummary: manifest.requirementSummary,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
sendJson(res, 201, {
|
|
129
|
+
controllerRun: serializeControllerRun(updatedRun),
|
|
130
|
+
manifest,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function controllerRunsListRoute(ctx: RouteContext): Promise<void> {
|
|
135
|
+
const { res, deps } = ctx;
|
|
136
|
+
const state = reconcileControllerClarifications(deps);
|
|
137
|
+
const controllerRuns = state
|
|
138
|
+
? Object.values(state.controllerRuns)
|
|
139
|
+
.sort((left, right) => right.updatedAt - left.updatedAt)
|
|
140
|
+
.map((run) => serializeControllerRun(run))
|
|
141
|
+
: [];
|
|
142
|
+
sendJson(res, 200, { controllerRuns });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function projectDeleteRoute(ctx: RouteContext): Promise<void> {
|
|
146
|
+
const { res, pathname, deps } = ctx;
|
|
147
|
+
const projectDir = decodeURIComponent(pathname.split("/").pop()!);
|
|
148
|
+
const result = await deleteProjectCascade(projectDir, deps);
|
|
149
|
+
if (!result.deleted) {
|
|
150
|
+
sendError(res, 404, "Project not found or project cleanup could not be confirmed");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const safeProjectPath = resolveSafeProjectDirectory(projectDir);
|
|
154
|
+
const stillExists = safeProjectPath ? fs.existsSync(safeProjectPath) : true;
|
|
155
|
+
if (stillExists) {
|
|
156
|
+
sendError(res, 409, "Project cleanup could not be confirmed");
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
sendJson(res, 200, {
|
|
160
|
+
status: "deleted",
|
|
161
|
+
confirmed: true,
|
|
162
|
+
projectDir,
|
|
163
|
+
cleanup: result.summary,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function controllerRunDeleteRoute(ctx: RouteContext): Promise<void> {
|
|
168
|
+
const { res, pathname, deps } = ctx;
|
|
169
|
+
const { getTeamState } = deps;
|
|
170
|
+
const runId = pathname.split("/").pop()!;
|
|
171
|
+
const result = await deleteControllerRunCascade(runId, deps);
|
|
172
|
+
if (!result.deleted) {
|
|
173
|
+
sendError(res, 404, "Controller run not found");
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const stillExists = !!getTeamState()?.controllerRuns[runId];
|
|
177
|
+
if (stillExists) {
|
|
178
|
+
sendError(res, 409, "Planning run cleanup could not be confirmed");
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
sendJson(res, 200, {
|
|
182
|
+
status: "deleted",
|
|
183
|
+
confirmed: true,
|
|
184
|
+
runId,
|
|
185
|
+
cleanup: result.summary,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function controllerIntakeRoute(ctx: RouteContext): Promise<void> {
|
|
190
|
+
const { req, res, deps, currentState } = ctx;
|
|
191
|
+
const { logger } = deps;
|
|
192
|
+
const readinessGate = shouldBlockControllerUntilProvisioningReady(deps, currentState);
|
|
193
|
+
if (readinessGate.blocked && readinessGate.readiness) {
|
|
194
|
+
sendError(res, 503, buildStartupReadinessMessage(readinessGate.readiness));
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const body = await parseJsonBody(req);
|
|
198
|
+
const message = typeof body.message === "string" ? body.message.trim() : "";
|
|
199
|
+
if (!message) {
|
|
200
|
+
sendError(res, 400, "message is required");
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const sessionKey = normalizeControllerIntakeSessionKey(body.sessionKey);
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
const result = await runControllerIntake(message, sessionKey, deps);
|
|
208
|
+
sendJson(res, 200, result);
|
|
209
|
+
} catch (err) {
|
|
210
|
+
if (err instanceof ControllerIntakeSwitchInProgressError) {
|
|
211
|
+
// Same response shape as the URL-level transition gate (503 +
|
|
212
|
+
// Retry-After); reachable here when a switch starts between the gate
|
|
213
|
+
// and the intake entry (the gate reads the state once per request).
|
|
214
|
+
res.setHeader("Retry-After", String(SWITCH_TASK_SUBMISSION_RETRY_AFTER_SECONDS));
|
|
215
|
+
sendJson(res, 503, {
|
|
216
|
+
error: err.message,
|
|
217
|
+
retryAfterSeconds: SWITCH_TASK_SUBMISSION_RETRY_AFTER_SECONDS,
|
|
218
|
+
transition: {
|
|
219
|
+
phase: err.transition.phase,
|
|
220
|
+
targetProfileId: err.transition.targetProfileId,
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
226
|
+
logger.warn(`Controller: intake failed for ${sessionKey}: ${errorMessage}`);
|
|
227
|
+
sendError(res, errorMessage.includes("timed out") ? 504 : 500, errorMessage);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export {
|
|
232
|
+
controllerManifestRoute,
|
|
233
|
+
controllerRunsListRoute,
|
|
234
|
+
projectDeleteRoute,
|
|
235
|
+
controllerRunDeleteRoute,
|
|
236
|
+
controllerIntakeRoute,
|
|
237
|
+
};
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// PR-C of issue #204 (http-server monolith split): the controller-run
|
|
2
|
+
// lifecycle (execution event ring, create/update/serialize) extracted
|
|
3
|
+
// verbatim from controller/http-server.ts. Zero behavior change - callers
|
|
4
|
+
// pass the same deps object as before. This module must NEVER import
|
|
5
|
+
// http-server (the reverse edge exists: http-server -> controller-runs).
|
|
6
|
+
import type { PluginLogger } from "../../api.js";
|
|
7
|
+
import type {
|
|
8
|
+
ControllerRunInfo,
|
|
9
|
+
ControllerRunSource,
|
|
10
|
+
TaskExecution,
|
|
11
|
+
TaskExecutionEvent,
|
|
12
|
+
TaskExecutionEventInput,
|
|
13
|
+
TeamState,
|
|
14
|
+
} from "../types.js";
|
|
15
|
+
import { generateId } from "../protocol.js";
|
|
16
|
+
import { deriveProjectSlug } from "../openclaw-workspace.js";
|
|
17
|
+
import { buildTaskExecutionSummary, MAX_TASK_EXECUTION_EVENTS } from "./task-execution.js";
|
|
18
|
+
import {
|
|
19
|
+
extractExplicitProjectNameHint,
|
|
20
|
+
resolveExistingProjectIdentityFromMessage,
|
|
21
|
+
resolveProjectIdentityForSession,
|
|
22
|
+
resolveProjectIdentityForTaskId,
|
|
23
|
+
syncProjectRegistryEntry,
|
|
24
|
+
} from "./project-registry.js";
|
|
25
|
+
import type { ControllerTaskDomainDeps } from "./task-execution.js";
|
|
26
|
+
|
|
27
|
+
const MAX_CONTROLLER_RUNS = 40;
|
|
28
|
+
|
|
29
|
+
export function ensureControllerRunExecution(run: ControllerRunInfo): TaskExecution {
|
|
30
|
+
if (!run.execution) {
|
|
31
|
+
run.execution = {
|
|
32
|
+
status: run.status,
|
|
33
|
+
runId: run.runId,
|
|
34
|
+
startedAt: run.startedAt,
|
|
35
|
+
endedAt: run.completedAt,
|
|
36
|
+
lastUpdatedAt: run.updatedAt,
|
|
37
|
+
events: [],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!Array.isArray(run.execution.events)) {
|
|
42
|
+
run.execution.events = [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
run.execution.status = run.status;
|
|
46
|
+
run.execution.runId = run.runId ?? run.execution.runId;
|
|
47
|
+
run.execution.startedAt = run.startedAt ?? run.execution.startedAt;
|
|
48
|
+
run.execution.endedAt = run.completedAt ?? run.execution.endedAt;
|
|
49
|
+
run.execution.lastUpdatedAt = run.updatedAt ?? run.execution.lastUpdatedAt;
|
|
50
|
+
return run.execution;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function appendControllerRunEvent(run: ControllerRunInfo, input: TaskExecutionEventInput): TaskExecutionEvent {
|
|
54
|
+
const now = input.createdAt ?? Date.now();
|
|
55
|
+
const execution = ensureControllerRunExecution(run);
|
|
56
|
+
|
|
57
|
+
if (input.runId) {
|
|
58
|
+
run.runId = input.runId;
|
|
59
|
+
execution.runId = input.runId;
|
|
60
|
+
}
|
|
61
|
+
if (input.sessionKey) {
|
|
62
|
+
execution.sessionKey = input.sessionKey;
|
|
63
|
+
}
|
|
64
|
+
if (input.status) {
|
|
65
|
+
run.status = input.status;
|
|
66
|
+
execution.status = input.status;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if ((input.status === "running" || input.phase === "run_started") && !run.startedAt) {
|
|
70
|
+
run.startedAt = now;
|
|
71
|
+
execution.startedAt = now;
|
|
72
|
+
}
|
|
73
|
+
if (run.status === "completed" || run.status === "failed") {
|
|
74
|
+
run.completedAt = run.completedAt ?? now;
|
|
75
|
+
execution.endedAt = execution.endedAt ?? now;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
run.updatedAt = now;
|
|
79
|
+
execution.lastUpdatedAt = now;
|
|
80
|
+
|
|
81
|
+
const event: TaskExecutionEvent = {
|
|
82
|
+
id: generateId(),
|
|
83
|
+
type: input.type,
|
|
84
|
+
createdAt: now,
|
|
85
|
+
message: input.message,
|
|
86
|
+
phase: input.phase,
|
|
87
|
+
source: input.source,
|
|
88
|
+
stream: input.stream,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
execution.events.push(event);
|
|
92
|
+
if (execution.events.length > MAX_TASK_EXECUTION_EVENTS) {
|
|
93
|
+
execution.events = execution.events.slice(-MAX_TASK_EXECUTION_EVENTS);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return event;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function trimControllerRuns(state: TeamState): void {
|
|
100
|
+
const runs = Object.values(state.controllerRuns)
|
|
101
|
+
.sort((left, right) => left.updatedAt - right.updatedAt);
|
|
102
|
+
if (runs.length <= MAX_CONTROLLER_RUNS) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
for (const run of runs.slice(0, runs.length - MAX_CONTROLLER_RUNS)) {
|
|
106
|
+
delete state.controllerRuns[run.id];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function serializeControllerRun(run?: ControllerRunInfo, includeExecutionEvents = true): Record<string, unknown> | undefined {
|
|
111
|
+
if (!run) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const payload: Record<string, unknown> = { ...run };
|
|
116
|
+
if (!run.execution) {
|
|
117
|
+
return payload;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
payload.execution = includeExecutionEvents
|
|
121
|
+
? {
|
|
122
|
+
status: run.execution.status,
|
|
123
|
+
runId: run.execution.runId,
|
|
124
|
+
startedAt: run.execution.startedAt,
|
|
125
|
+
endedAt: run.execution.endedAt,
|
|
126
|
+
lastUpdatedAt: run.execution.lastUpdatedAt,
|
|
127
|
+
events: run.execution.events.map((event) => ({ ...event })),
|
|
128
|
+
}
|
|
129
|
+
: buildTaskExecutionSummary(run.execution);
|
|
130
|
+
|
|
131
|
+
return payload;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function buildControllerRunTitle(
|
|
135
|
+
message: string,
|
|
136
|
+
source: ControllerRunSource,
|
|
137
|
+
sourceTaskTitle?: string,
|
|
138
|
+
): string {
|
|
139
|
+
if (source === "task_follow_up") {
|
|
140
|
+
const normalizedSourceTitle = String(sourceTaskTitle || "").replace(/\s+/g, " ").trim();
|
|
141
|
+
if (normalizedSourceTitle) {
|
|
142
|
+
const compactSourceTitle = normalizedSourceTitle.length <= 72
|
|
143
|
+
? normalizedSourceTitle
|
|
144
|
+
: `${normalizedSourceTitle.slice(0, 72).trimEnd()}…`;
|
|
145
|
+
return `Controller follow-up after ${compactSourceTitle}`;
|
|
146
|
+
}
|
|
147
|
+
return "Controller workflow follow-up";
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const normalized = message.replace(/\s+/g, " ").trim();
|
|
151
|
+
if (!normalized) {
|
|
152
|
+
return "Controller intake";
|
|
153
|
+
}
|
|
154
|
+
if (normalized.length <= 100) {
|
|
155
|
+
return normalized;
|
|
156
|
+
}
|
|
157
|
+
return `${normalized.slice(0, 100).trimEnd()}…`;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function createControllerRun(
|
|
161
|
+
message: string,
|
|
162
|
+
sessionKey: string,
|
|
163
|
+
deps: ControllerTaskDomainDeps,
|
|
164
|
+
options?: {
|
|
165
|
+
source?: ControllerRunSource;
|
|
166
|
+
sourceTaskId?: string;
|
|
167
|
+
sourceTaskTitle?: string;
|
|
168
|
+
},
|
|
169
|
+
): ControllerRunInfo {
|
|
170
|
+
const now = Date.now();
|
|
171
|
+
const existingState = deps.getTeamState();
|
|
172
|
+
const inheritedProject = options?.sourceTaskId
|
|
173
|
+
? resolveProjectIdentityForTaskId(options.sourceTaskId, existingState)
|
|
174
|
+
: resolveProjectIdentityForSession(sessionKey, existingState)
|
|
175
|
+
?? resolveExistingProjectIdentityFromMessage(message, existingState);
|
|
176
|
+
const explicitProjectId = extractExplicitProjectNameHint(message);
|
|
177
|
+
const projectId = inheritedProject?.projectId ?? explicitProjectId;
|
|
178
|
+
const projectDir = inheritedProject?.projectDir ?? projectId ?? deriveProjectSlug(message);
|
|
179
|
+
const run: ControllerRunInfo = {
|
|
180
|
+
id: generateId(),
|
|
181
|
+
title: buildControllerRunTitle(message, options?.source ?? "human", options?.sourceTaskTitle),
|
|
182
|
+
sessionKey,
|
|
183
|
+
projectId: projectId || undefined,
|
|
184
|
+
projectDir,
|
|
185
|
+
source: options?.source ?? "human",
|
|
186
|
+
sourceTaskId: options?.sourceTaskId,
|
|
187
|
+
sourceTaskTitle: options?.sourceTaskTitle,
|
|
188
|
+
request: message,
|
|
189
|
+
createdTaskIds: [],
|
|
190
|
+
status: "pending",
|
|
191
|
+
createdAt: now,
|
|
192
|
+
updatedAt: now,
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
const state = deps.updateTeamState((teamState) => {
|
|
196
|
+
syncProjectRegistryEntry(teamState, {
|
|
197
|
+
projectId: run.projectId,
|
|
198
|
+
projectDir: run.projectDir,
|
|
199
|
+
aliases: [run.projectId, extractExplicitProjectNameHint(message)],
|
|
200
|
+
summary: run.title,
|
|
201
|
+
updatedAt: now,
|
|
202
|
+
});
|
|
203
|
+
teamState.controllerRuns[run.id] = run;
|
|
204
|
+
trimControllerRuns(teamState);
|
|
205
|
+
});
|
|
206
|
+
const createdRun = state.controllerRuns[run.id] ?? run;
|
|
207
|
+
deps.wsServer.broadcastUpdate({ type: "controller:run", data: serializeControllerRun(createdRun) });
|
|
208
|
+
return createdRun;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function updateControllerRun(
|
|
212
|
+
runId: string,
|
|
213
|
+
deps: ControllerTaskDomainDeps,
|
|
214
|
+
updater: (run: ControllerRunInfo) => void,
|
|
215
|
+
): ControllerRunInfo | undefined {
|
|
216
|
+
const state = deps.updateTeamState((teamState) => {
|
|
217
|
+
const run = teamState.controllerRuns[runId];
|
|
218
|
+
if (!run) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
updater(run);
|
|
222
|
+
trimControllerRuns(teamState);
|
|
223
|
+
});
|
|
224
|
+
const updatedRun = state.controllerRuns[runId];
|
|
225
|
+
if (updatedRun) {
|
|
226
|
+
deps.wsServer.broadcastUpdate({ type: "controller:run", data: serializeControllerRun(updatedRun) });
|
|
227
|
+
}
|
|
228
|
+
return updatedRun;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function recordControllerRunEvent(
|
|
232
|
+
runId: string,
|
|
233
|
+
input: TaskExecutionEventInput,
|
|
234
|
+
deps: ControllerTaskDomainDeps,
|
|
235
|
+
): ControllerRunInfo | undefined {
|
|
236
|
+
return updateControllerRun(runId, deps, (run) => {
|
|
237
|
+
appendControllerRunEvent(run, input);
|
|
238
|
+
});
|
|
239
|
+
}
|