@teamclaws/teamclaw 2026.11.2 → 2026.11.4

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 (38) hide show
  1. package/dist/src/controller/http-server.js +18 -0
  2. package/dist/src/controller/journal.js +68 -0
  3. package/dist/src/controller/provisioning/process-supervisor.js +58 -2
  4. package/dist/src/controller/provisioning-config.js +53 -0
  5. package/dist/src/controller/route-table.js +5 -0
  6. package/dist/src/controller/security.js +39 -0
  7. package/dist/src/controller/teamauto-routes.js +12 -0
  8. package/dist/src/controller/tunnel-manager.js +33 -1
  9. package/dist/src/controller/tunnel-routes.js +17 -1
  10. package/dist/src/controller/websocket.js +4 -0
  11. package/dist/src/controller/worker-provisioning.js +36 -0
  12. package/dist/src/state.js +77 -1
  13. package/dist/src/teamauto/github-client.js +39 -0
  14. package/dist/src/teamauto/scanner.js +9 -1
  15. package/dist/src/teamauto/scheduler.js +368 -17
  16. package/dist/src/teamauto/store.js +13 -0
  17. package/dist/src/ui/app.js +44 -0
  18. package/dist/src/ui/index.html +15 -0
  19. package/openclaw.plugin.json +1 -1
  20. package/package.json +1 -1
  21. package/src/controller/http-server.ts +18 -0
  22. package/src/controller/journal.ts +89 -0
  23. package/src/controller/provisioning/process-supervisor.ts +56 -2
  24. package/src/controller/provisioning-config.ts +54 -0
  25. package/src/controller/route-table.ts +5 -0
  26. package/src/controller/security.ts +40 -0
  27. package/src/controller/teamauto-routes.ts +12 -0
  28. package/src/controller/tunnel-manager.ts +34 -1
  29. package/src/controller/tunnel-routes.ts +18 -1
  30. package/src/controller/websocket.ts +5 -0
  31. package/src/controller/worker-provisioning.ts +34 -0
  32. package/src/state.ts +73 -2
  33. package/src/teamauto/github-client.ts +43 -0
  34. package/src/teamauto/scanner.ts +9 -1
  35. package/src/teamauto/scheduler.ts +372 -17
  36. package/src/teamauto/store.ts +40 -1
  37. package/src/ui/app.js +44 -0
  38. package/src/ui/index.html +15 -0
package/dist/src/state.js CHANGED
@@ -209,6 +209,25 @@ async function writeFileAtomically(filePath, contents) {
209
209
  // filesystems (e.g. network mounts) reject explicit chmod.
210
210
  }
211
211
  await fs.rename(tmpPath, filePath);
212
+ // #267-4 hygiene: crash/rename failures used to leave *.tmp-* siblings
213
+ // forever (observed: 16 residue files in the runtime dir). Best-effort
214
+ // sweep of same-prefix leftovers older than 1h - never throws.
215
+ try {
216
+ const dir = path.dirname(filePath);
217
+ const prefix = path.basename(filePath) + ".tmp-";
218
+ const stale = Date.now() - 3_600_000;
219
+ for (const name of await fs.readdir(dir)) {
220
+ if (!name.startsWith(prefix))
221
+ continue;
222
+ const full = path.join(dir, name);
223
+ const st = await fs.stat(full).catch(() => null);
224
+ if (st && st.mtimeMs < stale)
225
+ await fs.unlink(full).catch(() => { });
226
+ }
227
+ }
228
+ catch {
229
+ // Hygiene is best-effort: any fs failure here is silently ignored.
230
+ }
212
231
  }
213
232
  function enqueueAtomicWrite(filePath, contents) {
214
233
  const previous = writeQueues.get(filePath) ?? Promise.resolve();
@@ -243,7 +262,6 @@ function errorMessage(error) {
243
262
  return error instanceof Error ? error.message : String(error);
244
263
  }
245
264
  async function loadTeamState(teamName) {
246
- // Review fix (Phase 1.6): the instance-scoped team-state.json is the
247
265
  // primary state file, and when it EXISTS it is authoritative. A
248
266
  // present-but-corrupt primary is a fatal error, never a silent fallback to
249
267
  // the legacy snapshot - falling back would rewind the team to the
@@ -252,6 +270,12 @@ async function loadTeamState(teamName) {
252
270
  let primaryRaw = null;
253
271
  try {
254
272
  primaryRaw = await fs.readFile(teamStateFilePath(), "utf8");
273
+ // #270: baseline the mtime right after reading so a foreign write between
274
+ // load and our first save is detected (not blindly overwritten).
275
+ try {
276
+ teamStateMtime.set(teamStateFilePath(), (await fs.stat(teamStateFilePath())).mtimeMs);
277
+ }
278
+ catch { /* stat race - next save re-baselines */ }
255
279
  }
256
280
  catch (error) {
257
281
  if (!isFileMissing(error)) {
@@ -286,6 +310,41 @@ async function loadTeamState(teamName) {
286
310
  return null;
287
311
  }
288
312
  }
313
+ // #270 lazy merge-on-write: last mtime this process observed for the team
314
+ // state file. A disk file NEWER than this stamp means another process wrote
315
+ // it (multi-gateway sharing one store) - the next save merges instead of
316
+ // blindly overwriting (which is how tasks "evaporated": 155/81 -> 135/72).
317
+ // Single-process cost: one stat() per save; the 24MB re-read+merge only
318
+ // happens when a foreign write is actually detected.
319
+ const teamStateMtime = new Map();
320
+ /** #270: fold foreign keys back in. Key-level policy - keys present in the
321
+ * foreign snapshot but missing from OUR in-memory state are re-added (they
322
+ * were created by another process and a blind full-file write would erase
323
+ * them). Keys we hold locally always win (this process is the most recent
324
+ * writer for them). Messages union by id. */
325
+ function mergeForeignTeamState(state, disk) {
326
+ const recordKeys = ["workers", "tasks", "controllerRuns", "projects", "clarifications", "previews", "reports"];
327
+ for (const key of recordKeys) {
328
+ const foreign = disk[key];
329
+ if (!foreign || typeof foreign !== "object")
330
+ continue;
331
+ const ours = (state[key] ?? {});
332
+ for (const [k, v] of Object.entries(foreign)) {
333
+ if (!(k in ours))
334
+ ours[k] = v;
335
+ }
336
+ state[key] = ours;
337
+ }
338
+ const foreignMsgs = Array.isArray(disk.messages) ? disk.messages : [];
339
+ if (foreignMsgs.length) {
340
+ const seen = new Set(state.messages.map((m) => m.id));
341
+ for (const m of foreignMsgs) {
342
+ if (m && typeof m === "object" && typeof m.id === "string" && !seen.has(m.id)) {
343
+ state.messages.push(m);
344
+ }
345
+ }
346
+ }
347
+ }
289
348
  async function saveTeamState(state) {
290
349
  await ensureDir(STATE_DIR);
291
350
  const filePath = teamStateFilePath();
@@ -304,7 +363,24 @@ async function saveTeamState(state) {
304
363
  state.projects = state.projects && typeof state.projects === "object"
305
364
  ? state.projects
306
365
  : {};
366
+ // #270: if the disk file is newer than what THIS process last observed,
367
+ // another gateway wrote it - fold its keys back in before overwriting.
368
+ try {
369
+ const st = await fs.stat(filePath);
370
+ const known = teamStateMtime.get(filePath);
371
+ if (known !== undefined && st.mtimeMs > known) {
372
+ const disk = JSON.parse(await fs.readFile(filePath, "utf8"));
373
+ mergeForeignTeamState(state, disk);
374
+ }
375
+ }
376
+ catch {
377
+ /* first save / stat race - proceed with a plain write */
378
+ }
307
379
  await enqueueAtomicWrite(filePath, `${JSON.stringify(state, null, 2)}\n`);
380
+ try {
381
+ teamStateMtime.set(filePath, (await fs.stat(filePath)).mtimeMs);
382
+ }
383
+ catch { /* renamed away mid-write - next save re-baselines */ }
308
384
  }
309
385
  // ---------------------------------------------------------------------------
310
386
  // Team profile switch archive (Phase 2.1, design section 2.3 step 3).
@@ -15,7 +15,13 @@ export class GithubClientError extends Error {
15
15
  }
16
16
  }
17
17
  export function createGithubClient(auth) {
18
+ // #267-3: cumulative API-call counters; runSweep reads etagStats() before/
19
+ // after each sweep and stores the DELTA on the repo's sweep record, so the
20
+ // shared client still yields correct per-repo attribution (sweeps are
21
+ // serialized by the sweeping set).
22
+ const etagCounters = { calls: 0, hits: 0 };
18
23
  async function request(method, urlPath, options = {}) {
24
+ etagCounters.calls += 1;
19
25
  const headers = {
20
26
  ...(await auth.getAuthHeaders()),
21
27
  "User-Agent": USER_AGENT,
@@ -57,6 +63,9 @@ export function createGithubClient(auth) {
57
63
  const message = data?.message ?? `HTTP ${response.status}`;
58
64
  throw new GithubClientError(message, response.status);
59
65
  }
66
+ if (response.status >= 200 && response.status < 300) {
67
+ etagCounters.hits += 1; // #267-3: "hits" = calls that returned fresh data (non-304 successes)
68
+ }
60
69
  return { status: response.status, data, notModified: false };
61
70
  }
62
71
  async function getJson(urlPath, etags) {
@@ -65,6 +74,10 @@ export function createGithubClient(auth) {
65
74
  }
66
75
  return {
67
76
  request,
77
+ /** #267-3: cumulative etag/API counters (see etagCounters above). */
78
+ etagStats() {
79
+ return { ...etagCounters };
80
+ },
68
81
  async getRepoInfo(owner, repo) {
69
82
  const data = await getJson(`/repos/${owner}/${repo}`);
70
83
  if (!data) {
@@ -164,6 +177,32 @@ export function createGithubClient(auth) {
164
177
  async assignIssue(owner, repo, issueNumber, assignee) {
165
178
  await request("POST", `/repos/${owner}/${repo}/issues/${issueNumber}/assignees`, { body: { assignees: [assignee] } });
166
179
  },
180
+ /** #claim-recovery: remove the bot's own assignment from an issue whose
181
+ * ownership task failed, so skipAssignedToHumans does not block the
182
+ * re-candidate (other human assignees are untouched). */
183
+ async removeIssueAssignees(owner, repo, issueNumber, assignees) {
184
+ await request("DELETE", `/repos/${owner}/${repo}/issues/${issueNumber}/assignees`, { body: { assignees } });
185
+ },
186
+ /** Fresh issue state for claim-time re-checks (#268): the scan-time
187
+ * assignee snapshot is seconds stale by enqueue time - a human may have
188
+ * self-assigned in between, and claiming over a human is worse than not
189
+ * claiming. */
190
+ async getIssue(owner, repo, issueNumber) {
191
+ const { data } = await request("GET", `/repos/${owner}/${repo}/issues/${issueNumber}`);
192
+ const raw = data;
193
+ return {
194
+ assignees: Array.isArray(raw.assignees) ? raw.assignees.map((a) => String(a.login ?? "")).filter(Boolean) : [],
195
+ state: String(raw.state ?? "open"),
196
+ updatedAt: String(raw.updated_at ?? ""),
197
+ };
198
+ },
199
+ /** Login of the authenticated account (#268): the assignee string for
200
+ * self-claiming. Kept here (not on the auth manager) so the claim path
201
+ * stays client-only. */
202
+ async getAuthenticatedUser() {
203
+ const { data } = await request("GET", "/user");
204
+ return { login: String(data.login ?? "") };
205
+ },
167
206
  /** Open a PR from a task worktree branch (collaboration contract delivery). */
168
207
  async createPullRequest(owner, repo, head, base, title, body) {
169
208
  const { data } = await request("POST", `/repos/${owner}/${repo}/pulls`, {
@@ -107,7 +107,15 @@ export async function sweepRepo(repo, client, now = Date.now()) {
107
107
  role: "qa",
108
108
  writeMode: "read-only",
109
109
  };
110
- if (repo.policy.actions.issueReview) {
110
+ // #269 6th case (ggcode#4785): an issue eligible for BOTH actions got
111
+ // two tasks in the same sweep, each delivering its own comment - the
112
+ // delivery layer correctly treats them as independent entries. Collapse
113
+ // at the source: when ownership applies (open + enabled) it wins and
114
+ // subsumes the triage read; issueReview fires only when ownership is
115
+ // disabled for this repo (or the issue is closed and un-reviewable via
116
+ // ownership, e.g. follow-up triage on a closed issue).
117
+ const ownershipApplies = repo.policy.actions.takeOwnership && issue.state === "open";
118
+ if (repo.policy.actions.issueReview && !ownershipApplies) {
111
119
  candidates.push({
112
120
  ...base,
113
121
  kind: "issueReview",