@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,361 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { validateRoleIdList } from "./roles.js";
|
|
5
|
+
// Phase 1.6: the legacy-state profileId migration defaults to the loader's
|
|
6
|
+
// software-rd baseline id (single literal source). profile-loader has no
|
|
7
|
+
// runtime edge back into this module, so no cycle.
|
|
8
|
+
import { SOFTWARE_RD_PROFILE_ID } from "./profile-loader.js";
|
|
9
|
+
const STATE_VERSION = 2;
|
|
10
|
+
function migrateState(raw) {
|
|
11
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
const state = raw;
|
|
15
|
+
// Basic structural guard — same checks as the old loadTeamState
|
|
16
|
+
if (typeof state.teamName !== "string" ||
|
|
17
|
+
typeof state.createdAt !== "number" ||
|
|
18
|
+
typeof state.updatedAt !== "number" ||
|
|
19
|
+
!state.workers ||
|
|
20
|
+
!state.tasks) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
// Ensure required nested objects exist (defensive, same as before)
|
|
24
|
+
if (!state.controllerRuns || typeof state.controllerRuns !== "object") {
|
|
25
|
+
state.controllerRuns = {};
|
|
26
|
+
}
|
|
27
|
+
if (!state.projects || typeof state.projects !== "object") {
|
|
28
|
+
state.projects = {};
|
|
29
|
+
}
|
|
30
|
+
if (!Array.isArray(state.messages)) {
|
|
31
|
+
state.messages = [];
|
|
32
|
+
}
|
|
33
|
+
if (!state.clarifications || typeof state.clarifications !== "object") {
|
|
34
|
+
state.clarifications = {};
|
|
35
|
+
}
|
|
36
|
+
if (state.repo && typeof state.repo !== "object") {
|
|
37
|
+
delete state.repo;
|
|
38
|
+
}
|
|
39
|
+
if (!state.provisioning || typeof state.provisioning !== "object") {
|
|
40
|
+
state.provisioning = createEmptyProvisioningState();
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const prov = state.provisioning;
|
|
44
|
+
if (!prov.workers || typeof prov.workers !== "object") {
|
|
45
|
+
prov.workers = {};
|
|
46
|
+
}
|
|
47
|
+
prov.startupReadiness = normalizeStartupReadiness(prov.startupReadiness);
|
|
48
|
+
}
|
|
49
|
+
const version = typeof state.version === "number" ? state.version : 1;
|
|
50
|
+
if (version > STATE_VERSION) {
|
|
51
|
+
throw new Error(`Saved team state is version ${version}, but this software only supports up to version ${STATE_VERSION}. ` +
|
|
52
|
+
"Please update TeamClaw to a newer version that supports this state format.");
|
|
53
|
+
}
|
|
54
|
+
// Version 1 → 2: no new required fields beyond `version` itself.
|
|
55
|
+
// startupReadiness was already optional and handled by normalizeStartupReadiness above.
|
|
56
|
+
state.version = STATE_VERSION;
|
|
57
|
+
// Phase 1.6 (design 2.3): legacy states carry no profileId; default them
|
|
58
|
+
// to the software-rd baseline. Idempotent - states saved by this build
|
|
59
|
+
// already carry the field and pass through unchanged.
|
|
60
|
+
if (typeof state.profileId !== "string" || !state.profileId.trim()) {
|
|
61
|
+
state.profileId = SOFTWARE_RD_PROFILE_ID;
|
|
62
|
+
}
|
|
63
|
+
// Phase 2.1: a malformed persisted transition is dropped (treated as "no
|
|
64
|
+
// switch in progress"). Aborting a corrupt switch keeps the previous team
|
|
65
|
+
// fully intact - the safe failure mode; the user can re-issue the switch.
|
|
66
|
+
if (state.transition !== undefined) {
|
|
67
|
+
const transition = state.transition;
|
|
68
|
+
const phaseOk = transition?.phase === "draining" || transition?.phase === "archiving" || transition?.phase === "warming";
|
|
69
|
+
const targetOk = typeof transition?.targetProfileId === "string" && Boolean(transition.targetProfileId.trim());
|
|
70
|
+
const startedAtOk = typeof transition?.startedAt === "number";
|
|
71
|
+
if (!phaseOk || !targetOk || !startedAtOk) {
|
|
72
|
+
delete state.transition;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return state;
|
|
76
|
+
}
|
|
77
|
+
function resolvePluginStateDir() {
|
|
78
|
+
const explicitStateDir = process.env.OPENCLAW_STATE_DIR?.trim();
|
|
79
|
+
if (explicitStateDir) {
|
|
80
|
+
return path.join(explicitStateDir, "plugins", "teamclaw");
|
|
81
|
+
}
|
|
82
|
+
const explicitHome = process.env.OPENCLAW_HOME?.trim() || process.env.HOME?.trim();
|
|
83
|
+
const homeDir = explicitHome ? path.resolve(explicitHome) : os.homedir();
|
|
84
|
+
return path.join(homeDir, ".openclaw", "plugins", "teamclaw");
|
|
85
|
+
}
|
|
86
|
+
const STATE_DIR = resolvePluginStateDir();
|
|
87
|
+
const writeQueues = new Map();
|
|
88
|
+
function createEmptyProvisioningState() {
|
|
89
|
+
return {
|
|
90
|
+
workers: {},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function normalizeStartupReadiness(value) {
|
|
94
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const record = value;
|
|
98
|
+
const status = record.status === "checking" || record.status === "ready" || record.status === "degraded"
|
|
99
|
+
? record.status
|
|
100
|
+
: undefined;
|
|
101
|
+
const startedAt = typeof record.startedAt === "number" ? record.startedAt : undefined;
|
|
102
|
+
const checkedAt = typeof record.checkedAt === "number" ? record.checkedAt : undefined;
|
|
103
|
+
const attempts = typeof record.attempts === "number" && record.attempts >= 0 ? Math.floor(record.attempts) : 0;
|
|
104
|
+
// Phase 0.3: persisted requiredRoles are untrusted; filter through the
|
|
105
|
+
// shared guard so corrupted state cannot inject arbitrary role strings.
|
|
106
|
+
const requiredRoles = validateRoleIdList(record.requiredRoles);
|
|
107
|
+
const readyWorkerIds = Array.isArray(record.readyWorkerIds)
|
|
108
|
+
? record.readyWorkerIds.filter((entry) => typeof entry === "string")
|
|
109
|
+
: [];
|
|
110
|
+
const message = typeof record.message === "string" && record.message.trim() ? record.message : undefined;
|
|
111
|
+
if (!status || startedAt === undefined || checkedAt === undefined) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
status,
|
|
116
|
+
startedAt,
|
|
117
|
+
checkedAt,
|
|
118
|
+
attempts,
|
|
119
|
+
requiredRoles,
|
|
120
|
+
readyWorkerIds,
|
|
121
|
+
message,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
async function ensureDir(dir) {
|
|
125
|
+
await fs.mkdir(dir, { recursive: true });
|
|
126
|
+
}
|
|
127
|
+
async function writeFileAtomically(filePath, contents) {
|
|
128
|
+
const tmpPath = `${filePath}.tmp-${process.pid}-${Date.now()}`;
|
|
129
|
+
await fs.writeFile(tmpPath, contents, "utf8");
|
|
130
|
+
await fs.rename(tmpPath, filePath);
|
|
131
|
+
}
|
|
132
|
+
function enqueueAtomicWrite(filePath, contents) {
|
|
133
|
+
const previous = writeQueues.get(filePath) ?? Promise.resolve();
|
|
134
|
+
const next = previous
|
|
135
|
+
.catch(() => { })
|
|
136
|
+
.then(() => writeFileAtomically(filePath, contents));
|
|
137
|
+
writeQueues.set(filePath, next);
|
|
138
|
+
return next.finally(() => {
|
|
139
|
+
if (writeQueues.get(filePath) === next) {
|
|
140
|
+
writeQueues.delete(filePath);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
// Phase 1.6 (design section 2.3): the persisted state file name is decoupled
|
|
145
|
+
// from teamName. TeamClaw is single-instance-single-team per state dir, so a
|
|
146
|
+
// fixed instance-scoped file name IS the instance identity - renaming a team
|
|
147
|
+
// no longer orphans the old `<teamName>-team-state.json` (the collision risk
|
|
148
|
+
// called out in the design). The legacy teamName-named file is still read as
|
|
149
|
+
// a one-time migration source; the next save writes the new name and the
|
|
150
|
+
// legacy file is simply never consulted again.
|
|
151
|
+
const TEAM_STATE_FILE = "team-state.json";
|
|
152
|
+
function teamStateFilePath() {
|
|
153
|
+
return path.join(STATE_DIR, TEAM_STATE_FILE);
|
|
154
|
+
}
|
|
155
|
+
function legacyTeamStateFilePath(teamName) {
|
|
156
|
+
return path.join(STATE_DIR, `${teamName}-team-state.json`);
|
|
157
|
+
}
|
|
158
|
+
function isFileMissing(error) {
|
|
159
|
+
return error?.code === "ENOENT";
|
|
160
|
+
}
|
|
161
|
+
function errorMessage(error) {
|
|
162
|
+
return error instanceof Error ? error.message : String(error);
|
|
163
|
+
}
|
|
164
|
+
async function loadTeamState(teamName) {
|
|
165
|
+
// Review fix (Phase 1.6): the instance-scoped team-state.json is the
|
|
166
|
+
// primary state file, and when it EXISTS it is authoritative. A
|
|
167
|
+
// present-but-corrupt primary is a fatal error, never a silent fallback to
|
|
168
|
+
// the legacy snapshot - falling back would rewind the team to the
|
|
169
|
+
// pre-migration state and lose everything saved since. Only a MISSING
|
|
170
|
+
// primary (ENOENT) falls through to the one-time legacy read.
|
|
171
|
+
let primaryRaw = null;
|
|
172
|
+
try {
|
|
173
|
+
primaryRaw = await fs.readFile(teamStateFilePath(), "utf8");
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (!isFileMissing(error)) {
|
|
177
|
+
throw new Error(`TeamClaw cannot read the persisted team state ${teamStateFilePath()}: ${errorMessage(error)}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (primaryRaw !== null) {
|
|
181
|
+
let parsed;
|
|
182
|
+
try {
|
|
183
|
+
parsed = JSON.parse(primaryRaw);
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
throw new Error(`TeamClaw team state file ${teamStateFilePath()} is not valid JSON (${errorMessage(error)}). `
|
|
187
|
+
+ "Refusing to start with a fresh team over a corrupt state file - fix or remove the file manually.");
|
|
188
|
+
}
|
|
189
|
+
const migrated = migrateState(parsed);
|
|
190
|
+
if (!migrated) {
|
|
191
|
+
throw new Error(`TeamClaw team state file ${teamStateFilePath()} exists but is not a valid team state. `
|
|
192
|
+
+ "Refusing to start with a fresh team over an unusable state file - fix or remove the file manually.");
|
|
193
|
+
}
|
|
194
|
+
return migrated;
|
|
195
|
+
}
|
|
196
|
+
// Primary absent: one-time legacy fallback (pre-Phase-1.6 file name).
|
|
197
|
+
// Lenient by design, matching the pre-Phase-1.6 single-file behavior: a
|
|
198
|
+
// missing or corrupt legacy file simply means first initialization, and a
|
|
199
|
+
// corrupt legacy file is never overwritten (saves go to the new name).
|
|
200
|
+
try {
|
|
201
|
+
const legacyRaw = await fs.readFile(legacyTeamStateFilePath(teamName), "utf8");
|
|
202
|
+
return migrateState(JSON.parse(legacyRaw));
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async function saveTeamState(state) {
|
|
209
|
+
await ensureDir(STATE_DIR);
|
|
210
|
+
const filePath = teamStateFilePath();
|
|
211
|
+
state.version = STATE_VERSION;
|
|
212
|
+
state.updatedAt = Date.now();
|
|
213
|
+
state.provisioning = state.provisioning && typeof state.provisioning === "object"
|
|
214
|
+
? state.provisioning
|
|
215
|
+
: createEmptyProvisioningState();
|
|
216
|
+
state.provisioning.workers = state.provisioning.workers && typeof state.provisioning.workers === "object"
|
|
217
|
+
? state.provisioning.workers
|
|
218
|
+
: {};
|
|
219
|
+
state.provisioning.startupReadiness = normalizeStartupReadiness(state.provisioning.startupReadiness);
|
|
220
|
+
state.controllerRuns = state.controllerRuns && typeof state.controllerRuns === "object"
|
|
221
|
+
? state.controllerRuns
|
|
222
|
+
: {};
|
|
223
|
+
state.projects = state.projects && typeof state.projects === "object"
|
|
224
|
+
? state.projects
|
|
225
|
+
: {};
|
|
226
|
+
await enqueueAtomicWrite(filePath, `${JSON.stringify(state, null, 2)}\n`);
|
|
227
|
+
}
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
// Team profile switch archive (Phase 2.1, design section 2.3 step 3).
|
|
230
|
+
//
|
|
231
|
+
// Switching profiles seals the WHOLE previous TeamState (tasks,
|
|
232
|
+
// clarifications, messages, previews, repo, projects, controllerRuns,
|
|
233
|
+
// reports) into state-dir/archived/<profileId>-<timestamp>.json together with
|
|
234
|
+
// an embedded snapshot of the profile's roles - so an archived banking team
|
|
235
|
+
// keeps its original role vocabulary even after the profile package evolves
|
|
236
|
+
// (design 2.2: archive queries must not suffer terminology drift). v1 only
|
|
237
|
+
// lists archive metadata; the full payload stays queryable offline.
|
|
238
|
+
// ---------------------------------------------------------------------------
|
|
239
|
+
const ARCHIVED_TEAMS_DIR_NAME = "archived";
|
|
240
|
+
const ARCHIVE_FILE_RE = /^([a-z0-9-]+)-(\d+)\.json$/;
|
|
241
|
+
function archivedTeamsDirPath() {
|
|
242
|
+
return path.join(STATE_DIR, ARCHIVED_TEAMS_DIR_NAME);
|
|
243
|
+
}
|
|
244
|
+
function parseArchiveFileName(name) {
|
|
245
|
+
const match = ARCHIVE_FILE_RE.exec(name);
|
|
246
|
+
if (!match) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
const timestamp = Number(match[2]);
|
|
250
|
+
return Number.isFinite(timestamp) ? { profileId: match[1], timestamp } : null;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Seal the given TeamState into the archive directory (design 2.3 step 3).
|
|
254
|
+
* Returns the archive file path. The write is atomic; the envelope embeds the
|
|
255
|
+
* profile snapshot so the archive is self-contained.
|
|
256
|
+
*/
|
|
257
|
+
async function archiveTeamState(state, profileSnapshot) {
|
|
258
|
+
const dir = archivedTeamsDirPath();
|
|
259
|
+
await ensureDir(dir);
|
|
260
|
+
const fileName = `${state.profileId}-${Date.now()}.json`;
|
|
261
|
+
const filePath = path.join(dir, fileName);
|
|
262
|
+
const envelope = {
|
|
263
|
+
archivedAt: Date.now(),
|
|
264
|
+
profile: profileSnapshot,
|
|
265
|
+
team: state,
|
|
266
|
+
};
|
|
267
|
+
await enqueueAtomicWrite(filePath, `${JSON.stringify(envelope, null, 2)}\n`);
|
|
268
|
+
return filePath;
|
|
269
|
+
}
|
|
270
|
+
/** List archived teams (newest first). Unreadable entries are skipped. */
|
|
271
|
+
async function listArchivedTeams() {
|
|
272
|
+
let files;
|
|
273
|
+
try {
|
|
274
|
+
files = await fs.readdir(archivedTeamsDirPath());
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
return [];
|
|
278
|
+
}
|
|
279
|
+
const summaries = [];
|
|
280
|
+
for (const name of files.sort().reverse()) {
|
|
281
|
+
if (!name.endsWith(".json") || !parseArchiveFileName(name)) {
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
const raw = await fs.readFile(path.join(archivedTeamsDirPath(), name), "utf8");
|
|
286
|
+
const parsed = JSON.parse(raw);
|
|
287
|
+
if (!parsed?.team || typeof parsed.team !== "object") {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
summaries.push({
|
|
291
|
+
file: name,
|
|
292
|
+
profileId: parsed.profile?.id ?? parsed.team.profileId ?? "unknown",
|
|
293
|
+
profileName: parsed.profile?.name ?? "",
|
|
294
|
+
teamName: typeof parsed.team.teamName === "string" ? parsed.team.teamName : "",
|
|
295
|
+
archivedAt: typeof parsed.archivedAt === "number" ? parsed.archivedAt : 0,
|
|
296
|
+
taskCount: parsed.team.tasks && typeof parsed.team.tasks === "object"
|
|
297
|
+
? Object.keys(parsed.team.tasks).length
|
|
298
|
+
: 0,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
// Skip unreadable/corrupt archive files - listing must never fail the API.
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return summaries;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Crash-resume helper (design 2.3): find an archive file written for the
|
|
309
|
+
* given (source) profileId at or after `startedAt` - the persisted
|
|
310
|
+
* transition.startedAt. Existence of such a file means the switch already got
|
|
311
|
+
* past the archive step, so a restart must jump straight to building the new
|
|
312
|
+
* team instead of re-draining.
|
|
313
|
+
*/
|
|
314
|
+
async function findTeamSwitchArchive(profileId, startedAt) {
|
|
315
|
+
let files;
|
|
316
|
+
try {
|
|
317
|
+
files = await fs.readdir(archivedTeamsDirPath());
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
for (const name of files.sort().reverse()) {
|
|
323
|
+
const parsed = parseArchiveFileName(name);
|
|
324
|
+
if (parsed && parsed.profileId === profileId && parsed.timestamp >= startedAt) {
|
|
325
|
+
return path.join(archivedTeamsDirPath(), name);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
async function loadWorkerIdentity() {
|
|
331
|
+
const filePath = path.join(STATE_DIR, "worker-identity.json");
|
|
332
|
+
try {
|
|
333
|
+
const raw = await fs.readFile(filePath, "utf8");
|
|
334
|
+
const parsed = JSON.parse(raw);
|
|
335
|
+
if (typeof parsed.workerId !== "string" ||
|
|
336
|
+
typeof parsed.role !== "string" ||
|
|
337
|
+
typeof parsed.controllerUrl !== "string" ||
|
|
338
|
+
typeof parsed.registeredAt !== "number") {
|
|
339
|
+
return null;
|
|
340
|
+
}
|
|
341
|
+
return parsed;
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
async function saveWorkerIdentity(identity) {
|
|
348
|
+
await ensureDir(STATE_DIR);
|
|
349
|
+
const filePath = path.join(STATE_DIR, "worker-identity.json");
|
|
350
|
+
await enqueueAtomicWrite(filePath, `${JSON.stringify(identity, null, 2)}\n`);
|
|
351
|
+
}
|
|
352
|
+
async function clearWorkerIdentity() {
|
|
353
|
+
const filePath = path.join(STATE_DIR, "worker-identity.json");
|
|
354
|
+
try {
|
|
355
|
+
await fs.unlink(filePath);
|
|
356
|
+
}
|
|
357
|
+
catch {
|
|
358
|
+
// ignore
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
export { STATE_DIR, loadTeamState, saveTeamState, archiveTeamState, listArchivedTeams, findTeamSwitchArchive, loadWorkerIdentity, saveWorkerIdentity, clearWorkerIdentity, };
|