@teamclaws/teamclaw 2026.11.4 → 2026.11.5

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.
@@ -245,7 +245,7 @@ export function createRepoSync(auth, logger) {
245
245
  * matches) or nonexistent (no commits of its own). Undelivered local work
246
246
  * is retained and reported - never silently destroyed.
247
247
  */
248
- async function safeRemoveTaskWorktree(repoId, taskId, deliveryOracle) {
248
+ async function safeRemoveTaskWorktree(repoId, taskId, deliveryOracle, targetClosedOracle) {
249
249
  const base = await repoDir(repoId);
250
250
  const wtName = taskWorktreeDirName(repoId, taskId);
251
251
  const wtPath = path.join(path.dirname(base), path.basename(wtName));
@@ -273,6 +273,19 @@ export function createRepoSync(auth, logger) {
273
273
  if (inUsePid !== null) {
274
274
  return { removed: false, reason: `retained: in use by live process (pid ${inUsePid})` };
275
275
  }
276
+ // #orphan-gc: the target issue/PR is CLOSED - no delivery this worktree
277
+ // could ever produce would be wanted (the issue was fixed/rejected
278
+ // elsewhere, the PR superseded). This is the authoritative orphan
279
+ // signal: it overrides the dirty guard, because "uncommitted work"
280
+ // toward a closed target has no future. Placed AFTER the in-use check -
281
+ // a live foreign controller may still be finishing a delivery race.
282
+ if (targetClosedOracle) {
283
+ const closed = await targetClosedOracle(repoId, taskId).catch(() => null);
284
+ if (closed === true) {
285
+ await removeTaskWorktree(repoId, taskId);
286
+ return { removed: true, reason: "orphaned: target issue/PR closed (dirty tree dropped - no possible delivery)" };
287
+ }
288
+ }
276
289
  // #173 follow-up 1: the dirty guard must cover BOTH removal paths. The
277
290
  // tip-match branch used to remove without any dirty check, silently
278
291
  // destroying post-delivery worker edits (small window, but real).
@@ -327,7 +340,7 @@ export function createRepoSync(auth, logger) {
327
340
  * sibling whose task is terminal (or unknown), apply the delivery-safety
328
341
  * removal. Heals the historical backlog that pre-dates terminal cleanup.
329
342
  */
330
- async function gcTaskWorktrees(repoId, isTerminal, deliveryOracle) {
343
+ async function gcTaskWorktrees(repoId, isTerminal, deliveryOracle, targetClosedOracle) {
331
344
  const base = await repoDir(repoId);
332
345
  const siblings = path.dirname(base);
333
346
  // taskWorktreeDirName(repoId, "x") = "teamauto/<slug>__task-x"; the
@@ -338,7 +351,7 @@ export function createRepoSync(auth, logger) {
338
351
  const retained = [];
339
352
  let stale = 0;
340
353
  const apply = async (taskId) => {
341
- const r = await safeRemoveTaskWorktree(repoId, taskId, deliveryOracle);
354
+ const r = await safeRemoveTaskWorktree(repoId, taskId, deliveryOracle, targetClosedOracle);
342
355
  if (r.removed) {
343
356
  removed.push(taskId);
344
357
  logger.warn(`TeamAuto: worktree GC removed ${taskId} (${r.reason})`);
@@ -246,6 +246,35 @@ export function createTeamAutoScheduler(deps) {
246
246
  return false;
247
247
  }
248
248
  };
249
+ // #orphan-gc: is the worktree's TARGET (issue/PR) closed? An issue closed
250
+ // without our delivery (fixed elsewhere / rejected / the PR superseded)
251
+ // makes the worktree an orphan no matter how dirty it is - the pre-fix GC
252
+ // retained such trees forever ("retained: dirty worktree", 42 and
253
+ // climbing on the live instance). Resolves true only on a POSITIVE closed
254
+ // state; unknown task / unparsable marker / API failure all resolve null
255
+ // (fail toward keep).
256
+ const targetClosedOracle = async (repoId, taskId) => {
257
+ const teamState = deps.getTeamState();
258
+ const task = teamState?.tasks?.[taskId];
259
+ if (!task || typeof task.description !== "string") {
260
+ return null;
261
+ }
262
+ const parsed = parseClaimMarker(task.description);
263
+ if (!parsed) {
264
+ return null;
265
+ }
266
+ try {
267
+ const [owner, name] = parsed.repoId.split("/");
268
+ if (!owner || !name) {
269
+ return null;
270
+ }
271
+ const fresh = await client.getIssue(owner, name, parsed.number);
272
+ return fresh.state === "closed";
273
+ }
274
+ catch {
275
+ return null;
276
+ }
277
+ };
249
278
  const timers = new Map();
250
279
  const sweeping = new Set();
251
280
  let running = false;
@@ -743,7 +772,7 @@ export function createTeamAutoScheduler(deps) {
743
772
  for (const w of deadOnes) {
744
773
  const t = state0.tasks[w.taskId];
745
774
  if (t && t.writeMode === "write" && t.projectDir?.includes("__task-")) {
746
- const r = await sync.safeRemoveTaskWorktree(w.repoId, w.taskId, squashDeliveryOracle);
775
+ const r = await sync.safeRemoveTaskWorktree(w.repoId, w.taskId, squashDeliveryOracle, targetClosedOracle);
747
776
  if (r.removed) {
748
777
  // #173 follow-up 4: positive removal line - without it a successful
749
778
  // terminal cleanup is invisible in logs (only kept/failed logged).
@@ -827,7 +856,7 @@ export function createTeamAutoScheduler(deps) {
827
856
  // path left - remove it when the worker never produced deliverable
828
857
  // commits (safety-checked, undelivered work is retained + logged).
829
858
  if (task.writeMode === "write" && task.projectDir?.includes("__task-")) {
830
- const r = await sync.safeRemoveTaskWorktree(entry.repoId, task.id, squashDeliveryOracle);
859
+ const r = await sync.safeRemoveTaskWorktree(entry.repoId, task.id, squashDeliveryOracle, targetClosedOracle);
831
860
  if (r.removed) {
832
861
  // #173 follow-up 4: positive removal line - symmetric with the
833
862
  // boot-GC path which logs both directions.
@@ -1201,7 +1230,7 @@ export function createTeamAutoScheduler(deps) {
1201
1230
  // too - safeRemoveTaskWorktree still retains unpushed work.
1202
1231
  return !s.writebacks.some((w) => w.taskId === taskId && !w.doneAt && !w.failedAt);
1203
1232
  };
1204
- const r = await sync.gcTaskWorktrees(repo.id, isTerminal, squashDeliveryOracle);
1233
+ const r = await sync.gcTaskWorktrees(repo.id, isTerminal, squashDeliveryOracle, targetClosedOracle);
1205
1234
  removed += r.removed.length;
1206
1235
  retained += r.retained.length;
1207
1236
  stale += r.stale;
@@ -2,7 +2,7 @@
2
2
  "id": "teamclaw",
3
3
  "name": "TeamClaw",
4
4
  "description": "Virtual team collaboration - multiple OpenClaw instances form a virtual software company with role-based task routing.",
5
- "version": "2026.11.4",
5
+ "version": "2026.11.5",
6
6
  "activation": {
7
7
  "onStartup": true
8
8
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamclaws/teamclaw",
3
- "version": "2026.11.4",
3
+ "version": "2026.11.5",
4
4
  "description": "OpenClaw virtual software team orchestration plugin - controller, role-based workers, Web UI with internet tunnel sharing",
5
5
  "private": false,
6
6
  "keywords": [
@@ -26,6 +26,16 @@ export type TaskDeliveryOracle = (
26
26
  headTip: string,
27
27
  ) => Promise<boolean>;
28
28
 
29
+ /**
30
+ * #orphan-gc oracle: is the worktree's TARGET (issue/PR) closed? true =
31
+ * positively closed (the worktree is an orphan - even a dirty tree has no
32
+ * possible delivery), null/false = unknown or still open (fail toward keep).
33
+ */
34
+ export type TaskTargetClosedOracle = (
35
+ repoId: string,
36
+ taskId: string,
37
+ ) => Promise<boolean | null>;
38
+
29
39
  /** TaskInfo.projectDir slug for a bound repo (relative to the projects dir). */
30
40
  export function teamAutoProjectDir(repoId: string): string {
31
41
  return `teamauto/${repoSlugToDirName(repoId)}`;
@@ -283,6 +293,7 @@ export function createRepoSync(auth: GithubAuthManager, logger: { warn: (m: stri
283
293
  repoId: string,
284
294
  taskId: string,
285
295
  deliveryOracle?: TaskDeliveryOracle,
296
+ targetClosedOracle?: TaskTargetClosedOracle,
286
297
  ): Promise<{ removed: boolean; reason: string }> {
287
298
  const base = await repoDir(repoId);
288
299
  const wtName = taskWorktreeDirName(repoId, taskId);
@@ -310,6 +321,19 @@ export function createRepoSync(auth: GithubAuthManager, logger: { warn: (m: stri
310
321
  if (inUsePid !== null) {
311
322
  return { removed: false, reason: `retained: in use by live process (pid ${inUsePid})` };
312
323
  }
324
+ // #orphan-gc: the target issue/PR is CLOSED - no delivery this worktree
325
+ // could ever produce would be wanted (the issue was fixed/rejected
326
+ // elsewhere, the PR superseded). This is the authoritative orphan
327
+ // signal: it overrides the dirty guard, because "uncommitted work"
328
+ // toward a closed target has no future. Placed AFTER the in-use check -
329
+ // a live foreign controller may still be finishing a delivery race.
330
+ if (targetClosedOracle) {
331
+ const closed = await targetClosedOracle(repoId, taskId).catch(() => null);
332
+ if (closed === true) {
333
+ await removeTaskWorktree(repoId, taskId);
334
+ return { removed: true, reason: "orphaned: target issue/PR closed (dirty tree dropped - no possible delivery)" };
335
+ }
336
+ }
313
337
  // #173 follow-up 1: the dirty guard must cover BOTH removal paths. The
314
338
  // tip-match branch used to remove without any dirty check, silently
315
339
  // destroying post-delivery worker edits (small window, but real).
@@ -368,6 +392,7 @@ export function createRepoSync(auth: GithubAuthManager, logger: { warn: (m: stri
368
392
  repoId: string,
369
393
  isTerminal: (taskId: string) => boolean,
370
394
  deliveryOracle?: TaskDeliveryOracle,
395
+ targetClosedOracle?: TaskTargetClosedOracle,
371
396
  ): Promise<{ removed: string[]; retained: { taskId: string; reason: string }[]; stale: number }> {
372
397
  const base = await repoDir(repoId);
373
398
  const siblings = path.dirname(base);
@@ -379,7 +404,7 @@ export function createRepoSync(auth: GithubAuthManager, logger: { warn: (m: stri
379
404
  const retained: { taskId: string; reason: string }[] = [];
380
405
  let stale = 0;
381
406
  const apply = async (taskId: string): Promise<void> => {
382
- const r = await safeRemoveTaskWorktree(repoId, taskId, deliveryOracle);
407
+ const r = await safeRemoveTaskWorktree(repoId, taskId, deliveryOracle, targetClosedOracle);
383
408
  if (r.removed) {
384
409
  removed.push(taskId);
385
410
  logger.warn(`TeamAuto: worktree GC removed ${taskId} (${r.reason})`);
@@ -11,7 +11,7 @@ import { getActiveDefaultRole, validateActiveRoleId } from "../profile-loader.js
11
11
  import type { TaskInfo, TeamState } from "../types.js";
12
12
  import { createGithubClient } from "./github-client.js";
13
13
  import { learnNorms, renderNormsBlock } from "./norms.js";
14
- import { createRepoSync, teamAutoProjectDir, type TaskDeliveryOracle } from "./repo-sync.js";
14
+ import { createRepoSync, teamAutoProjectDir, type TaskDeliveryOracle, type TaskTargetClosedOracle } from "./repo-sync.js";
15
15
  import { nextFireMs } from "./schedule.js";
16
16
  import { resolveTeamAutoPaths } from "./store.js";
17
17
  import { advanceCursors, sweepRepo, type Candidate } from "./scanner.js";
@@ -325,6 +325,34 @@ export function createTeamAutoScheduler(deps: TeamAutoSchedulerDeps) {
325
325
  return false;
326
326
  }
327
327
  };
328
+ // #orphan-gc: is the worktree's TARGET (issue/PR) closed? An issue closed
329
+ // without our delivery (fixed elsewhere / rejected / the PR superseded)
330
+ // makes the worktree an orphan no matter how dirty it is - the pre-fix GC
331
+ // retained such trees forever ("retained: dirty worktree", 42 and
332
+ // climbing on the live instance). Resolves true only on a POSITIVE closed
333
+ // state; unknown task / unparsable marker / API failure all resolve null
334
+ // (fail toward keep).
335
+ const targetClosedOracle: TaskTargetClosedOracle = async (repoId, taskId) => {
336
+ const teamState = deps.getTeamState();
337
+ const task = teamState?.tasks?.[taskId];
338
+ if (!task || typeof task.description !== "string") {
339
+ return null;
340
+ }
341
+ const parsed = parseClaimMarker(task.description);
342
+ if (!parsed) {
343
+ return null;
344
+ }
345
+ try {
346
+ const [owner, name] = parsed.repoId.split("/");
347
+ if (!owner || !name) {
348
+ return null;
349
+ }
350
+ const fresh = await client.getIssue(owner, name, parsed.number);
351
+ return fresh.state === "closed";
352
+ } catch {
353
+ return null;
354
+ }
355
+ };
328
356
  const timers = new Map<string, NodeJS.Timeout>();
329
357
  const sweeping = new Set<string>();
330
358
  let running = false;
@@ -823,7 +851,7 @@ export function createTeamAutoScheduler(deps: TeamAutoSchedulerDeps) {
823
851
  for (const w of deadOnes) {
824
852
  const t = state0.tasks[w.taskId];
825
853
  if (t && t.writeMode === "write" && t.projectDir?.includes("__task-")) {
826
- const r = await sync.safeRemoveTaskWorktree(w.repoId, w.taskId, squashDeliveryOracle);
854
+ const r = await sync.safeRemoveTaskWorktree(w.repoId, w.taskId, squashDeliveryOracle, targetClosedOracle);
827
855
  if (r.removed) {
828
856
  // #173 follow-up 4: positive removal line - without it a successful
829
857
  // terminal cleanup is invisible in logs (only kept/failed logged).
@@ -911,7 +939,7 @@ export function createTeamAutoScheduler(deps: TeamAutoSchedulerDeps) {
911
939
  // path left - remove it when the worker never produced deliverable
912
940
  // commits (safety-checked, undelivered work is retained + logged).
913
941
  if (task.writeMode === "write" && task.projectDir?.includes("__task-")) {
914
- const r = await sync.safeRemoveTaskWorktree(entry.repoId, task.id, squashDeliveryOracle);
942
+ const r = await sync.safeRemoveTaskWorktree(entry.repoId, task.id, squashDeliveryOracle, targetClosedOracle);
915
943
  if (r.removed) {
916
944
  // #173 follow-up 4: positive removal line - symmetric with the
917
945
  // boot-GC path which logs both directions.
@@ -1279,7 +1307,7 @@ export function createTeamAutoScheduler(deps: TeamAutoSchedulerDeps) {
1279
1307
  // too - safeRemoveTaskWorktree still retains unpushed work.
1280
1308
  return !s.writebacks.some((w) => w.taskId === taskId && !w.doneAt && !w.failedAt);
1281
1309
  };
1282
- const r = await sync.gcTaskWorktrees(repo.id, isTerminal, squashDeliveryOracle);
1310
+ const r = await sync.gcTaskWorktrees(repo.id, isTerminal, squashDeliveryOracle, targetClosedOracle);
1283
1311
  removed += r.removed.length;
1284
1312
  retained += r.retained.length;
1285
1313
  stale += r.stale;