opencode-forgekeeper 6.5.0 → 6.7.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/dist/tui.js CHANGED
@@ -3773,6 +3773,23 @@ var ForgeSessionRpc = {
3773
3773
  additionalProperties: false
3774
3774
  },
3775
3775
  output: { type: "object", title: "CleanupResult" }
3776
+ },
3777
+ cleanupWorktrees: {
3778
+ input: {
3779
+ type: "object",
3780
+ properties: {
3781
+ directories: {
3782
+ type: "array",
3783
+ items: { type: "string" },
3784
+ minItems: 1,
3785
+ maxItems: 200
3786
+ },
3787
+ allowClientArchive: { type: "boolean" }
3788
+ },
3789
+ required: ["directories"],
3790
+ additionalProperties: false
3791
+ },
3792
+ output: { type: "object", title: "WorktreeCleanupResult" }
3776
3793
  }
3777
3794
  },
3778
3795
  events: {
@@ -3781,7 +3798,10 @@ var ForgeSessionRpc = {
3781
3798
  type: "object",
3782
3799
  properties: {
3783
3800
  scanID: { type: "string" },
3784
- phase: { type: "string", enum: ["sessions", "targets", "verifying", "archiving"] },
3801
+ phase: {
3802
+ type: "string",
3803
+ enum: ["sessions", "targets", "worktrees", "verifying", "archiving"]
3804
+ },
3785
3805
  checked: { type: "number" },
3786
3806
  total: { type: "number" }
3787
3807
  },
@@ -4154,12 +4174,97 @@ function formatCleanupProgress(progress, mode = "scan", defaultForge) {
4154
4174
  }
4155
4175
  case "targets":
4156
4176
  return `Checking ${plural} and tracked issues\u2026${counts}`;
4177
+ case "worktrees":
4178
+ return "Checking Forgekeeper worktrees\u2026";
4157
4179
  case "verifying":
4158
4180
  return `Verifying target status before archiving\u2026${counts}`;
4159
4181
  case "archiving":
4160
4182
  return `Archiving sessions\u2026${counts}`;
4161
4183
  }
4162
4184
  }
4185
+ function formatWorktreeCleanupSubtitle(candidate) {
4186
+ const status = candidate.superseded ? "superseded" : candidate.state;
4187
+ const parts = [`${candidate.reference} ${status}`];
4188
+ if (candidate.reasons.includes("busy")) {
4189
+ parts.push("session running");
4190
+ }
4191
+ if (candidate.dirtyFiles > 0) {
4192
+ parts.push(candidate.dirtyFiles === 1 ? "1 uncommitted file" : `${candidate.dirtyFiles} uncommitted files`);
4193
+ }
4194
+ if (candidate.hasLocalCommits) {
4195
+ parts.push("unpushed commits");
4196
+ }
4197
+ if (candidate.extraSessions.length > 0) {
4198
+ const count = candidate.extraSessions.length;
4199
+ parts.push(count === 1 ? "1 unarchived session" : `${count} unarchived sessions`);
4200
+ } else if (candidate.reasons.includes("user-sessions")) {
4201
+ parts.push("unarchived session");
4202
+ }
4203
+ if (parts.length === 1) {
4204
+ parts.push("ready to remove");
4205
+ }
4206
+ return parts.join(" \xB7 ");
4207
+ }
4208
+ function worktreeNeedsForceConfirm(candidate) {
4209
+ return candidate.dirtyFiles > 0 || candidate.hasLocalCommits || candidate.extraSessions.length > 0 || candidate.reasons.includes("user-sessions") || candidate.superseded;
4210
+ }
4211
+ function formatWorktreeConfirmSummary(candidates) {
4212
+ const dirty = candidates.reduce((sum, item) => sum + item.dirtyFiles, 0);
4213
+ const commits = candidates.filter((item) => item.hasLocalCommits).length;
4214
+ const sessions = candidates.reduce((sum, item) => sum + item.extraSessions.length, 0);
4215
+ const hasUnlistedUserSession = candidates.some((item) => item.extraSessions.length === 0 && item.reasons.includes("user-sessions"));
4216
+ const details = [];
4217
+ if (dirty > 0) {
4218
+ details.push(dirty === 1 ? "1 uncommitted file" : `${dirty} uncommitted files`);
4219
+ }
4220
+ if (commits > 0) {
4221
+ details.push(commits === 1 ? "unpushed commits" : `unpushed commits in ${commits} worktrees`);
4222
+ }
4223
+ if (sessions > 0) {
4224
+ details.push(sessions === 1 ? "1 unarchived session" : `${sessions} unarchived sessions`);
4225
+ } else if (hasUnlistedUserSession) {
4226
+ details.push("unarchived sessions");
4227
+ }
4228
+ if (details.length === 0) {
4229
+ return candidates.length === 1 ? "Remove this Forgekeeper worktree?" : `Remove ${candidates.length} Forgekeeper worktrees?`;
4230
+ }
4231
+ return `Force-removing will discard/archive: ${details.join(", ")}.`;
4232
+ }
4233
+ function formatWorktreeCleanupFailures(failures) {
4234
+ const counts = new Map;
4235
+ for (const failure of failures) {
4236
+ counts.set(failure.reason, (counts.get(failure.reason) ?? 0) + 1);
4237
+ }
4238
+ return [...counts.entries()].map(([reason, count]) => count === 1 ? reason : `${reason} (${count} worktrees)`).join("; ");
4239
+ }
4240
+ function applySessionCleanupToScan(scan, archivedSessionIDs, sweptWorktrees = []) {
4241
+ const archivedSet = new Set(archivedSessionIDs);
4242
+ const sweptSet = new Set(sweptWorktrees);
4243
+ const candidates = scan.candidates.filter((candidate) => !archivedSet.has(candidate.sessionID));
4244
+ if (!scan.worktrees)
4245
+ return { ...scan, candidates };
4246
+ const worktrees = scan.worktrees.filter((worktree) => !sweptSet.has(worktree.directory)).map((worktree) => {
4247
+ const extraSessions = worktree.extraSessions.filter((session) => !archivedSet.has(session.sessionID));
4248
+ const linkedSessionIDs = worktree.linkedSessionIDs.filter((id) => !archivedSet.has(id));
4249
+ const droppedUserSessions = worktree.extraSessions.length > 0 && extraSessions.length === 0;
4250
+ const reasons = droppedUserSessions ? worktree.reasons.filter((reason) => reason !== "user-sessions") : worktree.reasons;
4251
+ return {
4252
+ ...worktree,
4253
+ extraSessions,
4254
+ linkedSessionIDs,
4255
+ reasons,
4256
+ autoSafe: reasons.length === 0 && !worktree.superseded
4257
+ };
4258
+ });
4259
+ return { ...scan, candidates, worktrees };
4260
+ }
4261
+ function applyWorktreeCleanupToScan(scan, removedDirectories, archivedSessionIDs) {
4262
+ const removedSet = new Set(removedDirectories);
4263
+ const archivedSet = new Set(archivedSessionIDs);
4264
+ const candidates = scan.candidates.filter((candidate) => !archivedSet.has(candidate.sessionID));
4265
+ const worktrees = scan.worktrees?.filter((worktree) => !removedSet.has(worktree.directory));
4266
+ return { ...scan, candidates, ...worktrees ? { worktrees } : {} };
4267
+ }
4163
4268
  function formatCleanupFailures(failures) {
4164
4269
  const counts = new Map;
4165
4270
  for (const failure of failures) {
@@ -4205,14 +4310,15 @@ var nextCleanupScanSeq = 0;
4205
4310
  var nextScanID = () => `cleanup-${Date.now()}-${++nextCleanupScanSeq}`;
4206
4311
  function emptyCleanupMessage(skippedErrors, forge) {
4207
4312
  if (skippedErrors > 0) {
4208
- return `No eligible sessions found (${skippedErrors} skipped due to lookup errors).`;
4313
+ return `No eligible sessions or worktrees found (${skippedErrors} skipped due to lookup errors).`;
4209
4314
  }
4210
- return `No sessions with all ${requestPlural(forge)} merged or tracked issues closed.`;
4315
+ return `No sessions with all ${requestPlural(forge)} merged or tracked issues closed, and no leftover Forgekeeper worktrees.`;
4211
4316
  }
4212
- function cleanupDialogTitle(skippedErrors) {
4317
+ function cleanupDialogTitle(skippedErrors, hasWorktrees = false) {
4318
+ const base = hasWorktrees ? "Clean up finished sessions & worktrees" : "Clean up finished sessions";
4213
4319
  if (skippedErrors > 0)
4214
- return `Clean up finished sessions (${skippedErrors} skipped)`;
4215
- return "Clean up finished sessions";
4320
+ return `${base} (${skippedErrors} skipped)`;
4321
+ return base;
4216
4322
  }
4217
4323
  var tui_default = {
4218
4324
  id: "pedropombeiro.forgekeeper",
@@ -4827,6 +4933,7 @@ var tui_default = {
4827
4933
  const archived = [];
4828
4934
  const alreadyArchived = [];
4829
4935
  const failed = [];
4936
+ const sweptWorktrees = [];
4830
4937
  for (let i = 0;i < sessionIDs.length; i += 500) {
4831
4938
  const chunk = sessionIDs.slice(i, i + 500);
4832
4939
  const part = await rpc.cleanupSessions({
@@ -4841,6 +4948,8 @@ var tui_default = {
4841
4948
  archived.push(...part.archived);
4842
4949
  alreadyArchived.push(...part.alreadyArchived);
4843
4950
  failed.push(...part.failed);
4951
+ if (part.sweptWorktrees)
4952
+ sweptWorktrees.push(...part.sweptWorktrees);
4844
4953
  const pendingClientArchive = part.needsClientArchive ?? [];
4845
4954
  if (pendingClientArchive.length > 0) {
4846
4955
  let checked = 0;
@@ -4885,13 +4994,18 @@ var tui_default = {
4885
4994
  archived.push(...finalized.archived);
4886
4995
  alreadyArchived.push(...finalized.alreadyArchived);
4887
4996
  failed.push(...finalized.failed);
4997
+ if (finalized.sweptWorktrees)
4998
+ sweptWorktrees.push(...finalized.sweptWorktrees);
4888
4999
  }
4889
5000
  }
4890
5001
  }
4891
5002
  const result = {
4892
5003
  archived,
4893
5004
  alreadyArchived,
4894
- failed
5005
+ failed,
5006
+ ...sweptWorktrees.length > 0 ? {
5007
+ sweptWorktrees: [...new Set(sweptWorktrees)]
5008
+ } : {}
4895
5009
  };
4896
5010
  if (progressOpen)
4897
5011
  context.ui.dialog.clear();
@@ -4928,6 +5042,77 @@ var tui_default = {
4928
5042
  offProgress();
4929
5043
  }
4930
5044
  }
5045
+ async function runWorktreeCleanup(directory, targets) {
5046
+ const removable = targets.filter((item) => item.removable);
5047
+ if (removable.length === 0) {
5048
+ context.ui.toast.show({
5049
+ message: "Cannot remove worktree while a session is running in it.",
5050
+ variant: "info"
5051
+ });
5052
+ return;
5053
+ }
5054
+ if (removable.some(worktreeNeedsForceConfirm)) {
5055
+ const confirmed = await context.ui.dialog.select({
5056
+ title: formatWorktreeConfirmSummary(removable),
5057
+ options: [{
5058
+ title: "Cancel",
5059
+ value: "cancel"
5060
+ }, {
5061
+ title: removable.length === 1 ? "Remove worktree" : `Remove ${removable.length} worktrees`,
5062
+ value: "remove"
5063
+ }]
5064
+ });
5065
+ if (confirmed !== "remove")
5066
+ return;
5067
+ }
5068
+ try {
5069
+ const result = await rpc.cleanupWorktrees({
5070
+ directories: removable.map((item) => item.directory),
5071
+ allowClientArchive: true
5072
+ }, {
5073
+ location: {
5074
+ directory
5075
+ }
5076
+ });
5077
+ const clientArchived = [];
5078
+ if (result.needsClientArchive?.length) {
5079
+ await mapLimit(result.needsClientArchive, 8, async (sessionID) => {
5080
+ try {
5081
+ await context.client.session.update({
5082
+ sessionID,
5083
+ archived: true
5084
+ });
5085
+ clientArchived.push(sessionID);
5086
+ } catch {}
5087
+ });
5088
+ }
5089
+ const allArchivedSessions = [...result.archivedSessions, ...clientArchived];
5090
+ closeCleanedSessions(allArchivedSessions);
5091
+ if (result.removed.length > 0) {
5092
+ const count = result.removed.length;
5093
+ context.ui.toast.show({
5094
+ message: count === 1 ? "Removed 1 Forgekeeper worktree." : `Removed ${count} Forgekeeper worktrees.`,
5095
+ variant: "info"
5096
+ });
5097
+ }
5098
+ if (result.failed.length > 0) {
5099
+ context.ui.toast.show({
5100
+ message: formatWorktreeCleanupFailures(result.failed),
5101
+ variant: "error"
5102
+ });
5103
+ }
5104
+ return {
5105
+ ...result,
5106
+ archivedSessions: allArchivedSessions
5107
+ };
5108
+ } catch (error) {
5109
+ context.ui.toast.show({
5110
+ message: errorMessage(error),
5111
+ variant: "error"
5112
+ });
5113
+ return;
5114
+ }
5115
+ }
4931
5116
  function StatusDialog(props) {
4932
5117
  const [terminalHeight, setTerminalHeight] = createSignal(context.renderer.height);
4933
5118
  const onResize = () => setTerminalHeight(context.renderer.height);
@@ -5524,7 +5709,7 @@ var tui_default = {
5524
5709
  });
5525
5710
  return;
5526
5711
  }
5527
- if (!scan.candidates.length) {
5712
+ if (!scan.candidates.length && !(scan.worktrees?.length ?? 0)) {
5528
5713
  transitioning = true;
5529
5714
  if (progressOpen)
5530
5715
  context.ui.dialog.clear();
@@ -5535,12 +5720,15 @@ var tui_default = {
5535
5720
  return;
5536
5721
  }
5537
5722
  transitioning = true;
5538
- } else if (!scan.candidates.length) {
5723
+ } else if (!scan.candidates.length && !(scan.worktrees?.length ?? 0)) {
5539
5724
  return;
5540
5725
  }
5726
+ const worktrees = scan.worktrees ?? [];
5727
+ const removableWorktrees = worktrees.filter((item) => item.removable);
5541
5728
  const openTabIDs = new Set(context.ui.tabs.enabled() ? context.ui.tabs.list().map((tab) => tab.sessionID) : []);
5542
5729
  const directories = new Set(scan.candidates.map((candidate) => candidate.directory));
5543
5730
  const multiWorktree = directories.size > 1;
5731
+ const hasWorktrees = worktrees.length > 0;
5544
5732
  context.ui.dialog.set({
5545
5733
  size: "large",
5546
5734
  centered: true
@@ -5549,31 +5737,68 @@ var tui_default = {
5549
5737
  size: "large",
5550
5738
  centered: true
5551
5739
  }));
5552
- const selected = await context.ui.dialog.select({
5553
- title: cleanupDialogTitle(scan.skippedErrors),
5554
- options: scan.candidates.map((candidate) => ({
5740
+ const sessionOptionCategory = (sessionDirectory) => {
5741
+ if (multiWorktree)
5742
+ return worktreeLabel(sessionDirectory);
5743
+ if (hasWorktrees)
5744
+ return "Finished sessions";
5745
+ return;
5746
+ };
5747
+ const sessionOptions = scan.candidates.map((candidate) => {
5748
+ const category = sessionOptionCategory(candidate.directory);
5749
+ return {
5555
5750
  title: candidate.title,
5556
5751
  description: formatCleanupSubtitle(candidate),
5557
5752
  ...openTabIDs.has(candidate.sessionID) ? {
5558
5753
  footer: "open tab"
5559
5754
  } : {},
5560
- ...multiWorktree ? {
5561
- category: worktreeLabel(candidate.directory)
5755
+ ...category ? {
5756
+ category
5562
5757
  } : {},
5563
- value: candidate.sessionID
5564
- })),
5565
- actions: [{
5566
- bind: "ctrl+a",
5567
- title: `Clean up all (${scan.candidates.length})`,
5568
- selection: "none",
5569
- onTrigger: async () => {
5570
- context.ui.dialog.clear();
5571
- await runSessionCleanup(directory, scan.candidates.map((candidate) => candidate.sessionID), true);
5572
- }
5573
- }, {
5574
- bind: "ctrl+o",
5575
- title: "Open session",
5576
- onTrigger: (sessionID) => {
5758
+ value: `session:${candidate.sessionID}`
5759
+ };
5760
+ });
5761
+ const worktreeOptions = worktrees.map((worktree) => ({
5762
+ title: worktree.name,
5763
+ description: formatWorktreeCleanupSubtitle(worktree),
5764
+ ...worktree.removable ? {} : {
5765
+ footer: "busy"
5766
+ },
5767
+ category: "Forgekeeper worktrees",
5768
+ value: `worktree:${worktree.directory}`
5769
+ }));
5770
+ const reopenIfRemaining = (next) => {
5771
+ if (next.candidates.length > 0 || (next.worktrees?.length ?? 0) > 0) {
5772
+ showCleanup(next, directory);
5773
+ }
5774
+ };
5775
+ const actions = [...scan.candidates.length > 0 ? [{
5776
+ bind: "ctrl+a",
5777
+ title: hasWorktrees ? `Clean up sessions (${scan.candidates.length})` : `Clean up all (${scan.candidates.length})`,
5778
+ selection: "none",
5779
+ onTrigger: async () => {
5780
+ context.ui.dialog.clear();
5781
+ const result = await runSessionCleanup(directory, scan.candidates.map((candidate) => candidate.sessionID), true);
5782
+ const doneIDs = [...result?.archived ?? [], ...result?.alreadyArchived ?? []];
5783
+ const next = applySessionCleanupToScan(scan, doneIDs, result?.sweptWorktrees);
5784
+ reopenIfRemaining(next);
5785
+ }
5786
+ }] : [], ...removableWorktrees.length > 0 ? [{
5787
+ bind: "ctrl+w",
5788
+ title: `Remove worktrees (${removableWorktrees.length})`,
5789
+ selection: "none",
5790
+ onTrigger: async () => {
5791
+ context.ui.dialog.clear();
5792
+ const result = await runWorktreeCleanup(directory, removableWorktrees);
5793
+ const next = result ? applyWorktreeCleanupToScan(scan, result.removed, result.archivedSessions) : scan;
5794
+ reopenIfRemaining(next);
5795
+ }
5796
+ }] : [], {
5797
+ bind: "ctrl+o",
5798
+ title: "Open session",
5799
+ onTrigger: (selectedValue) => {
5800
+ if (selectedValue.startsWith("session:")) {
5801
+ const sessionID = selectedValue.slice("session:".length);
5577
5802
  context.ui.dialog.clear();
5578
5803
  if (context.ui.tabs.enabled() && context.ui.tabs.focus(sessionID))
5579
5804
  return;
@@ -5581,29 +5806,56 @@ var tui_default = {
5581
5806
  type: "session",
5582
5807
  sessionID
5583
5808
  });
5809
+ return;
5584
5810
  }
5585
- }, {
5586
- bind: "ctrl+r",
5587
- title: "Refresh",
5588
- selection: "none",
5589
- onTrigger: () => {
5811
+ if (selectedValue.startsWith("worktree:")) {
5812
+ const dir = selectedValue.slice("worktree:".length);
5813
+ const target = worktrees.find((item) => item.directory === dir);
5814
+ const sessionID = target?.extraSessions[0]?.sessionID ?? target?.linkedSessionIDs[0];
5815
+ if (!sessionID)
5816
+ return;
5590
5817
  context.ui.dialog.clear();
5591
- showCleanup(undefined, directory);
5818
+ if (context.ui.tabs.enabled() && context.ui.tabs.focus(sessionID))
5819
+ return;
5820
+ context.ui.router.navigate({
5821
+ type: "session",
5822
+ sessionID
5823
+ });
5592
5824
  }
5593
- }]
5825
+ }
5826
+ }, {
5827
+ bind: "ctrl+r",
5828
+ title: "Refresh",
5829
+ selection: "none",
5830
+ onTrigger: () => {
5831
+ context.ui.dialog.clear();
5832
+ showCleanup(undefined, directory);
5833
+ }
5834
+ }];
5835
+ const selected = await context.ui.dialog.select({
5836
+ title: cleanupDialogTitle(scan.skippedErrors, hasWorktrees),
5837
+ options: [...sessionOptions, ...worktreeOptions],
5838
+ actions
5594
5839
  });
5595
5840
  if (!selected)
5596
5841
  return;
5597
- const result = await runSessionCleanup(directory, [selected]);
5598
- const doneIDs = new Set([...result?.archived ?? [], ...result?.alreadyArchived ?? []]);
5599
- if (scan.candidates.length > 1 && doneIDs.size > 0) {
5600
- const remaining = scan.candidates.filter((candidate) => !doneIDs.has(candidate.sessionID));
5601
- if (remaining.length > 0) {
5602
- showCleanup({
5603
- ...scan,
5604
- candidates: remaining
5605
- }, directory);
5842
+ if (selected.startsWith("session:")) {
5843
+ const sessionID = selected.slice("session:".length);
5844
+ const result = await runSessionCleanup(directory, [sessionID]);
5845
+ const doneIDs = [...result?.archived ?? [], ...result?.alreadyArchived ?? []];
5846
+ if (doneIDs.length > 0) {
5847
+ reopenIfRemaining(applySessionCleanupToScan(scan, doneIDs, result?.sweptWorktrees));
5606
5848
  }
5849
+ return;
5850
+ }
5851
+ if (selected.startsWith("worktree:")) {
5852
+ const dir = selected.slice("worktree:".length);
5853
+ const target = worktrees.find((item) => item.directory === dir);
5854
+ if (!target)
5855
+ return;
5856
+ const result = await runWorktreeCleanup(directory, [target]);
5857
+ const next = result ? applyWorktreeCleanupToScan(scan, result.removed, result.archivedSessions) : scan;
5858
+ reopenIfRemaining(next);
5607
5859
  }
5608
5860
  }
5609
5861
  context.ui.slot({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-forgekeeper",
3
- "version": "6.5.0",
3
+ "version": "6.7.0",
4
4
  "description": "OpenCode plugin that tracks each session's GitHub and GitLab issues and PRs/MRs: title prefixes, a PR/MR status footer, and review notifications",
5
5
  "author": {
6
6
  "name": "Pedro Pombeiro",