@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.
Files changed (149) hide show
  1. package/cli.mjs +46 -15
  2. package/dist/api.js +1 -0
  3. package/dist/index.js +325 -0
  4. package/dist/src/config.js +412 -0
  5. package/dist/src/controller/clarification-routes.js +315 -0
  6. package/dist/src/controller/controller-capacity.js +14 -0
  7. package/dist/src/controller/controller-intake.js +895 -0
  8. package/dist/src/controller/controller-planning.js +813 -0
  9. package/dist/src/controller/controller-runs.js +175 -0
  10. package/dist/src/controller/controller-service.js +624 -0
  11. package/dist/src/controller/controller-tools.js +587 -0
  12. package/dist/src/controller/delivery-report.js +458 -0
  13. package/dist/src/controller/github-auth.js +564 -0
  14. package/dist/src/controller/http-server.js +284 -0
  15. package/dist/src/controller/kickoff-orchestrator.js +246 -0
  16. package/dist/src/controller/managed-gateway-process.js +286 -0
  17. package/dist/src/controller/message-delivery.js +28 -0
  18. package/dist/src/controller/message-router.js +38 -0
  19. package/dist/src/controller/message-routes.js +114 -0
  20. package/dist/src/controller/orchestration-manifest.js +151 -0
  21. package/dist/src/controller/preview-manager.js +586 -0
  22. package/dist/src/controller/preview-proxy.js +98 -0
  23. package/dist/src/controller/profile-switch.js +289 -0
  24. package/dist/src/controller/project-registry.js +294 -0
  25. package/dist/src/controller/prompt-injector.js +197 -0
  26. package/dist/src/controller/repo-routes.js +63 -0
  27. package/dist/src/controller/role-inference.js +246 -0
  28. package/dist/src/controller/route-table.js +1353 -0
  29. package/dist/src/controller/task-assignment.js +508 -0
  30. package/dist/src/controller/task-cleanup.js +258 -0
  31. package/dist/src/controller/task-execution.js +240 -0
  32. package/dist/src/controller/task-results.js +461 -0
  33. package/dist/src/controller/task-router.js +137 -0
  34. package/dist/src/controller/task-routes.js +703 -0
  35. package/dist/src/controller/teamauto-routes.js +124 -0
  36. package/dist/src/controller/tunnel-manager.js +548 -0
  37. package/dist/src/controller/ui-assets.js +68 -0
  38. package/dist/src/controller/websocket.js +42 -0
  39. package/dist/src/controller/worker-provisioning.js +1674 -0
  40. package/dist/src/controller/worker-routes.js +188 -0
  41. package/dist/src/discovery.js +86 -0
  42. package/dist/src/git-collaboration.js +694 -0
  43. package/dist/src/identity.js +132 -0
  44. package/dist/src/install-defaults.js +1 -0
  45. package/dist/src/interaction-contracts.js +531 -0
  46. package/dist/src/networking.js +91 -0
  47. package/dist/src/openclaw-workspace.js +925 -0
  48. package/dist/src/profile-loader.js +486 -0
  49. package/dist/src/profiles/banking.json +178 -0
  50. package/dist/src/profiles/crossborder-ecom.json +253 -0
  51. package/dist/src/profiles/insurance.json +157 -0
  52. package/dist/src/profiles/knowledge/hs-structure.md +97 -0
  53. package/dist/src/profiles/knowledge/incoterms-2020.md +188 -0
  54. package/dist/src/profiles/knowledge/vat-principles.md +85 -0
  55. package/dist/src/profiles/schema.json +201 -0
  56. package/dist/src/profiles/securities.json +137 -0
  57. package/dist/src/profiles/software-rd.json +264 -0
  58. package/dist/src/profiles/trading-co.json +200 -0
  59. package/dist/src/prompt-policy.js +386 -0
  60. package/dist/src/protocol.js +58 -0
  61. package/dist/src/role-ids.js +29 -0
  62. package/dist/src/roles.js +390 -0
  63. package/dist/src/session-roles.js +14 -0
  64. package/dist/src/state.js +361 -0
  65. package/dist/src/task-executor.js +1023 -0
  66. package/dist/src/teamauto/github-client.js +210 -0
  67. package/dist/src/teamauto/norms.js +85 -0
  68. package/dist/src/teamauto/repo-sync.js +423 -0
  69. package/dist/src/teamauto/scanner.js +207 -0
  70. package/dist/src/teamauto/schedule.js +123 -0
  71. package/dist/src/teamauto/scheduler.js +907 -0
  72. package/dist/src/teamauto/store.js +133 -0
  73. package/dist/src/types.js +290 -0
  74. package/dist/src/ui/app.js +4860 -0
  75. package/dist/src/ui/assets/teamclaw-app-icon.png +0 -0
  76. package/dist/src/ui/github-login.html +296 -0
  77. package/dist/src/ui/index.html +447 -0
  78. package/dist/src/ui/style.css +2805 -0
  79. package/dist/src/worker/http-handler.js +146 -0
  80. package/dist/src/worker/message-queue.js +24 -0
  81. package/dist/src/worker/prompt-injector.js +78 -0
  82. package/dist/src/worker/skill-installer.js +255 -0
  83. package/dist/src/worker/tools.js +541 -0
  84. package/dist/src/worker/worker-service.js +307 -0
  85. package/dist/src/workspace-browser.js +269 -0
  86. package/openclaw.plugin.json +28 -1
  87. package/package.json +3 -2
  88. package/src/config.ts +33 -2
  89. package/src/controller/clarification-routes.ts +404 -0
  90. package/src/controller/controller-intake.ts +1113 -0
  91. package/src/controller/controller-planning.ts +1029 -0
  92. package/src/controller/controller-runs.ts +239 -0
  93. package/src/controller/controller-service.ts +295 -41
  94. package/src/controller/controller-tools.ts +11 -7
  95. package/src/controller/http-server.ts +294 -6971
  96. package/src/controller/kickoff-orchestrator.ts +70 -7
  97. package/src/controller/message-delivery.ts +49 -0
  98. package/src/controller/message-routes.ts +137 -0
  99. package/src/controller/orchestration-manifest.ts +6 -29
  100. package/src/controller/preview-proxy.ts +127 -0
  101. package/src/controller/profile-switch.ts +366 -0
  102. package/src/controller/project-registry.ts +361 -0
  103. package/src/controller/prompt-injector.ts +23 -8
  104. package/src/controller/repo-routes.ts +91 -0
  105. package/src/controller/role-inference.ts +260 -22
  106. package/src/controller/route-table.ts +1616 -0
  107. package/src/controller/task-assignment.ts +618 -0
  108. package/src/controller/task-cleanup.ts +344 -0
  109. package/src/controller/task-execution.ts +356 -0
  110. package/src/controller/task-results.ts +544 -0
  111. package/src/controller/task-router.ts +98 -6
  112. package/src/controller/task-routes.ts +812 -0
  113. package/src/controller/teamauto-routes.ts +121 -0
  114. package/src/controller/ui-assets.ts +70 -0
  115. package/src/controller/websocket.ts +13 -1
  116. package/src/controller/worker-provisioning.ts +32 -4
  117. package/src/controller/worker-routes.ts +241 -0
  118. package/src/identity.ts +3 -2
  119. package/src/interaction-contracts.ts +7 -7
  120. package/src/openclaw-workspace.ts +44 -18
  121. package/src/profile-loader.ts +650 -0
  122. package/src/profiles/banking.json +178 -0
  123. package/src/profiles/crossborder-ecom.json +253 -0
  124. package/src/profiles/insurance.json +157 -0
  125. package/src/profiles/knowledge/hs-structure.md +97 -0
  126. package/src/profiles/knowledge/incoterms-2020.md +188 -0
  127. package/src/profiles/knowledge/vat-principles.md +85 -0
  128. package/src/profiles/schema.json +201 -0
  129. package/src/profiles/securities.json +137 -0
  130. package/src/profiles/software-rd.json +264 -0
  131. package/src/profiles/trading-co.json +200 -0
  132. package/src/prompt-policy.ts +27 -13
  133. package/src/role-ids.ts +31 -0
  134. package/src/roles.ts +60 -14
  135. package/src/session-roles.ts +14 -0
  136. package/src/state.ts +236 -9
  137. package/src/task-executor.ts +7 -18
  138. package/src/teamauto/github-client.ts +21 -0
  139. package/src/teamauto/repo-sync.ts +246 -6
  140. package/src/teamauto/scanner.ts +47 -13
  141. package/src/teamauto/scheduler.ts +259 -16
  142. package/src/teamauto/store.ts +12 -0
  143. package/src/types.ts +133 -16
  144. package/src/ui/app.js +592 -24
  145. package/src/ui/index.html +44 -11
  146. package/src/ui/style.css +194 -0
  147. package/src/worker/prompt-injector.ts +48 -8
  148. package/src/worker/skill-installer.ts +3 -1
  149. 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, };