opencode-forgekeeper 6.6.0 → 6.7.1

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
  },
@@ -3998,7 +4018,11 @@ function createStatusClient(options) {
3998
4018
  // packages/forgekeeper/src/client-core.ts
3999
4019
  var at = (directory) => ({ location: { directory } });
4000
4020
  var loadFeedback = async (rpc, directory) => await rpc.feedback({}, at(directory));
4021
+ var rpcUnavailable = (error) => typeof error === "object" && error !== null && error.type === "rpc.unavailable";
4022
+ var UNAVAILABLE_RETRIES = 10;
4023
+ var UNAVAILABLE_RETRY_DELAY = 1000;
4001
4024
  function createFeedbackLoader(options) {
4025
+ const wait = options.wait ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
4002
4026
  const requests = new Map;
4003
4027
  let generation = 0;
4004
4028
  const reload = async (directory) => {
@@ -4006,15 +4030,22 @@ function createFeedbackLoader(options) {
4006
4030
  requests.set(directory, request);
4007
4031
  const current = generation;
4008
4032
  const latest = () => current === generation && requests.get(directory) === request;
4009
- try {
4010
- const state = await options.load(directory);
4011
- if (latest())
4012
- options.changed(directory, state);
4013
- return state;
4014
- } catch (error) {
4015
- if (latest())
4016
- options.failed(directory, errorMessage(error));
4017
- return;
4033
+ for (let attempt = 0;; attempt++) {
4034
+ try {
4035
+ const state = await options.load(directory);
4036
+ if (latest())
4037
+ options.changed(directory, state);
4038
+ return state;
4039
+ } catch (error) {
4040
+ if (rpcUnavailable(error) && attempt < UNAVAILABLE_RETRIES && latest()) {
4041
+ await wait(UNAVAILABLE_RETRY_DELAY);
4042
+ if (latest())
4043
+ continue;
4044
+ }
4045
+ if (latest())
4046
+ options.failed(directory, errorMessage(error));
4047
+ return;
4048
+ }
4018
4049
  }
4019
4050
  };
4020
4051
  return {
@@ -4154,12 +4185,97 @@ function formatCleanupProgress(progress, mode = "scan", defaultForge) {
4154
4185
  }
4155
4186
  case "targets":
4156
4187
  return `Checking ${plural} and tracked issues\u2026${counts}`;
4188
+ case "worktrees":
4189
+ return "Checking Forgekeeper worktrees\u2026";
4157
4190
  case "verifying":
4158
4191
  return `Verifying target status before archiving\u2026${counts}`;
4159
4192
  case "archiving":
4160
4193
  return `Archiving sessions\u2026${counts}`;
4161
4194
  }
4162
4195
  }
4196
+ function formatWorktreeCleanupSubtitle(candidate) {
4197
+ const status = candidate.superseded ? "superseded" : candidate.state;
4198
+ const parts = [`${candidate.reference} ${status}`];
4199
+ if (candidate.reasons.includes("busy")) {
4200
+ parts.push("session running");
4201
+ }
4202
+ if (candidate.dirtyFiles > 0) {
4203
+ parts.push(candidate.dirtyFiles === 1 ? "1 uncommitted file" : `${candidate.dirtyFiles} uncommitted files`);
4204
+ }
4205
+ if (candidate.hasLocalCommits) {
4206
+ parts.push("unpushed commits");
4207
+ }
4208
+ if (candidate.extraSessions.length > 0) {
4209
+ const count = candidate.extraSessions.length;
4210
+ parts.push(count === 1 ? "1 unarchived session" : `${count} unarchived sessions`);
4211
+ } else if (candidate.reasons.includes("user-sessions")) {
4212
+ parts.push("unarchived session");
4213
+ }
4214
+ if (parts.length === 1) {
4215
+ parts.push("ready to remove");
4216
+ }
4217
+ return parts.join(" \xB7 ");
4218
+ }
4219
+ function worktreeNeedsForceConfirm(candidate) {
4220
+ return candidate.dirtyFiles > 0 || candidate.hasLocalCommits || candidate.extraSessions.length > 0 || candidate.reasons.includes("user-sessions") || candidate.superseded;
4221
+ }
4222
+ function formatWorktreeConfirmSummary(candidates) {
4223
+ const dirty = candidates.reduce((sum, item) => sum + item.dirtyFiles, 0);
4224
+ const commits = candidates.filter((item) => item.hasLocalCommits).length;
4225
+ const sessions = candidates.reduce((sum, item) => sum + item.extraSessions.length, 0);
4226
+ const hasUnlistedUserSession = candidates.some((item) => item.extraSessions.length === 0 && item.reasons.includes("user-sessions"));
4227
+ const details = [];
4228
+ if (dirty > 0) {
4229
+ details.push(dirty === 1 ? "1 uncommitted file" : `${dirty} uncommitted files`);
4230
+ }
4231
+ if (commits > 0) {
4232
+ details.push(commits === 1 ? "unpushed commits" : `unpushed commits in ${commits} worktrees`);
4233
+ }
4234
+ if (sessions > 0) {
4235
+ details.push(sessions === 1 ? "1 unarchived session" : `${sessions} unarchived sessions`);
4236
+ } else if (hasUnlistedUserSession) {
4237
+ details.push("unarchived sessions");
4238
+ }
4239
+ if (details.length === 0) {
4240
+ return candidates.length === 1 ? "Remove this Forgekeeper worktree?" : `Remove ${candidates.length} Forgekeeper worktrees?`;
4241
+ }
4242
+ return `Force-removing will discard/archive: ${details.join(", ")}.`;
4243
+ }
4244
+ function formatWorktreeCleanupFailures(failures) {
4245
+ const counts = new Map;
4246
+ for (const failure of failures) {
4247
+ counts.set(failure.reason, (counts.get(failure.reason) ?? 0) + 1);
4248
+ }
4249
+ return [...counts.entries()].map(([reason, count]) => count === 1 ? reason : `${reason} (${count} worktrees)`).join("; ");
4250
+ }
4251
+ function applySessionCleanupToScan(scan, archivedSessionIDs, sweptWorktrees = []) {
4252
+ const archivedSet = new Set(archivedSessionIDs);
4253
+ const sweptSet = new Set(sweptWorktrees);
4254
+ const candidates = scan.candidates.filter((candidate) => !archivedSet.has(candidate.sessionID));
4255
+ if (!scan.worktrees)
4256
+ return { ...scan, candidates };
4257
+ const worktrees = scan.worktrees.filter((worktree) => !sweptSet.has(worktree.directory)).map((worktree) => {
4258
+ const extraSessions = worktree.extraSessions.filter((session) => !archivedSet.has(session.sessionID));
4259
+ const linkedSessionIDs = worktree.linkedSessionIDs.filter((id) => !archivedSet.has(id));
4260
+ const droppedUserSessions = worktree.extraSessions.length > 0 && extraSessions.length === 0;
4261
+ const reasons = droppedUserSessions ? worktree.reasons.filter((reason) => reason !== "user-sessions") : worktree.reasons;
4262
+ return {
4263
+ ...worktree,
4264
+ extraSessions,
4265
+ linkedSessionIDs,
4266
+ reasons,
4267
+ autoSafe: reasons.length === 0 && !worktree.superseded
4268
+ };
4269
+ });
4270
+ return { ...scan, candidates, worktrees };
4271
+ }
4272
+ function applyWorktreeCleanupToScan(scan, removedDirectories, archivedSessionIDs) {
4273
+ const removedSet = new Set(removedDirectories);
4274
+ const archivedSet = new Set(archivedSessionIDs);
4275
+ const candidates = scan.candidates.filter((candidate) => !archivedSet.has(candidate.sessionID));
4276
+ const worktrees = scan.worktrees?.filter((worktree) => !removedSet.has(worktree.directory));
4277
+ return { ...scan, candidates, ...worktrees ? { worktrees } : {} };
4278
+ }
4163
4279
  function formatCleanupFailures(failures) {
4164
4280
  const counts = new Map;
4165
4281
  for (const failure of failures) {
@@ -4205,14 +4321,15 @@ var nextCleanupScanSeq = 0;
4205
4321
  var nextScanID = () => `cleanup-${Date.now()}-${++nextCleanupScanSeq}`;
4206
4322
  function emptyCleanupMessage(skippedErrors, forge) {
4207
4323
  if (skippedErrors > 0) {
4208
- return `No eligible sessions found (${skippedErrors} skipped due to lookup errors).`;
4324
+ return `No eligible sessions or worktrees found (${skippedErrors} skipped due to lookup errors).`;
4209
4325
  }
4210
- return `No sessions with all ${requestPlural(forge)} merged or tracked issues closed.`;
4326
+ return `No sessions with all ${requestPlural(forge)} merged or tracked issues closed, and no leftover Forgekeeper worktrees.`;
4211
4327
  }
4212
- function cleanupDialogTitle(skippedErrors) {
4328
+ function cleanupDialogTitle(skippedErrors, hasWorktrees = false) {
4329
+ const base = hasWorktrees ? "Clean up finished sessions & worktrees" : "Clean up finished sessions";
4213
4330
  if (skippedErrors > 0)
4214
- return `Clean up finished sessions (${skippedErrors} skipped)`;
4215
- return "Clean up finished sessions";
4331
+ return `${base} (${skippedErrors} skipped)`;
4332
+ return base;
4216
4333
  }
4217
4334
  var tui_default = {
4218
4335
  id: "pedropombeiro.forgekeeper",
@@ -4827,6 +4944,7 @@ var tui_default = {
4827
4944
  const archived = [];
4828
4945
  const alreadyArchived = [];
4829
4946
  const failed = [];
4947
+ const sweptWorktrees = [];
4830
4948
  for (let i = 0;i < sessionIDs.length; i += 500) {
4831
4949
  const chunk = sessionIDs.slice(i, i + 500);
4832
4950
  const part = await rpc.cleanupSessions({
@@ -4841,6 +4959,8 @@ var tui_default = {
4841
4959
  archived.push(...part.archived);
4842
4960
  alreadyArchived.push(...part.alreadyArchived);
4843
4961
  failed.push(...part.failed);
4962
+ if (part.sweptWorktrees)
4963
+ sweptWorktrees.push(...part.sweptWorktrees);
4844
4964
  const pendingClientArchive = part.needsClientArchive ?? [];
4845
4965
  if (pendingClientArchive.length > 0) {
4846
4966
  let checked = 0;
@@ -4885,13 +5005,18 @@ var tui_default = {
4885
5005
  archived.push(...finalized.archived);
4886
5006
  alreadyArchived.push(...finalized.alreadyArchived);
4887
5007
  failed.push(...finalized.failed);
5008
+ if (finalized.sweptWorktrees)
5009
+ sweptWorktrees.push(...finalized.sweptWorktrees);
4888
5010
  }
4889
5011
  }
4890
5012
  }
4891
5013
  const result = {
4892
5014
  archived,
4893
5015
  alreadyArchived,
4894
- failed
5016
+ failed,
5017
+ ...sweptWorktrees.length > 0 ? {
5018
+ sweptWorktrees: [...new Set(sweptWorktrees)]
5019
+ } : {}
4895
5020
  };
4896
5021
  if (progressOpen)
4897
5022
  context.ui.dialog.clear();
@@ -4928,6 +5053,77 @@ var tui_default = {
4928
5053
  offProgress();
4929
5054
  }
4930
5055
  }
5056
+ async function runWorktreeCleanup(directory, targets) {
5057
+ const removable = targets.filter((item) => item.removable);
5058
+ if (removable.length === 0) {
5059
+ context.ui.toast.show({
5060
+ message: "Cannot remove worktree while a session is running in it.",
5061
+ variant: "info"
5062
+ });
5063
+ return;
5064
+ }
5065
+ if (removable.some(worktreeNeedsForceConfirm)) {
5066
+ const confirmed = await context.ui.dialog.select({
5067
+ title: formatWorktreeConfirmSummary(removable),
5068
+ options: [{
5069
+ title: "Cancel",
5070
+ value: "cancel"
5071
+ }, {
5072
+ title: removable.length === 1 ? "Remove worktree" : `Remove ${removable.length} worktrees`,
5073
+ value: "remove"
5074
+ }]
5075
+ });
5076
+ if (confirmed !== "remove")
5077
+ return;
5078
+ }
5079
+ try {
5080
+ const result = await rpc.cleanupWorktrees({
5081
+ directories: removable.map((item) => item.directory),
5082
+ allowClientArchive: true
5083
+ }, {
5084
+ location: {
5085
+ directory
5086
+ }
5087
+ });
5088
+ const clientArchived = [];
5089
+ if (result.needsClientArchive?.length) {
5090
+ await mapLimit(result.needsClientArchive, 8, async (sessionID) => {
5091
+ try {
5092
+ await context.client.session.update({
5093
+ sessionID,
5094
+ archived: true
5095
+ });
5096
+ clientArchived.push(sessionID);
5097
+ } catch {}
5098
+ });
5099
+ }
5100
+ const allArchivedSessions = [...result.archivedSessions, ...clientArchived];
5101
+ closeCleanedSessions(allArchivedSessions);
5102
+ if (result.removed.length > 0) {
5103
+ const count = result.removed.length;
5104
+ context.ui.toast.show({
5105
+ message: count === 1 ? "Removed 1 Forgekeeper worktree." : `Removed ${count} Forgekeeper worktrees.`,
5106
+ variant: "info"
5107
+ });
5108
+ }
5109
+ if (result.failed.length > 0) {
5110
+ context.ui.toast.show({
5111
+ message: formatWorktreeCleanupFailures(result.failed),
5112
+ variant: "error"
5113
+ });
5114
+ }
5115
+ return {
5116
+ ...result,
5117
+ archivedSessions: allArchivedSessions
5118
+ };
5119
+ } catch (error) {
5120
+ context.ui.toast.show({
5121
+ message: errorMessage(error),
5122
+ variant: "error"
5123
+ });
5124
+ return;
5125
+ }
5126
+ }
4931
5127
  function StatusDialog(props) {
4932
5128
  const [terminalHeight, setTerminalHeight] = createSignal(context.renderer.height);
4933
5129
  const onResize = () => setTerminalHeight(context.renderer.height);
@@ -5524,7 +5720,7 @@ var tui_default = {
5524
5720
  });
5525
5721
  return;
5526
5722
  }
5527
- if (!scan.candidates.length) {
5723
+ if (!scan.candidates.length && !(scan.worktrees?.length ?? 0)) {
5528
5724
  transitioning = true;
5529
5725
  if (progressOpen)
5530
5726
  context.ui.dialog.clear();
@@ -5535,12 +5731,15 @@ var tui_default = {
5535
5731
  return;
5536
5732
  }
5537
5733
  transitioning = true;
5538
- } else if (!scan.candidates.length) {
5734
+ } else if (!scan.candidates.length && !(scan.worktrees?.length ?? 0)) {
5539
5735
  return;
5540
5736
  }
5737
+ const worktrees = scan.worktrees ?? [];
5738
+ const removableWorktrees = worktrees.filter((item) => item.removable);
5541
5739
  const openTabIDs = new Set(context.ui.tabs.enabled() ? context.ui.tabs.list().map((tab) => tab.sessionID) : []);
5542
5740
  const directories = new Set(scan.candidates.map((candidate) => candidate.directory));
5543
5741
  const multiWorktree = directories.size > 1;
5742
+ const hasWorktrees = worktrees.length > 0;
5544
5743
  context.ui.dialog.set({
5545
5744
  size: "large",
5546
5745
  centered: true
@@ -5549,31 +5748,68 @@ var tui_default = {
5549
5748
  size: "large",
5550
5749
  centered: true
5551
5750
  }));
5552
- const selected = await context.ui.dialog.select({
5553
- title: cleanupDialogTitle(scan.skippedErrors),
5554
- options: scan.candidates.map((candidate) => ({
5751
+ const sessionOptionCategory = (sessionDirectory) => {
5752
+ if (multiWorktree)
5753
+ return worktreeLabel(sessionDirectory);
5754
+ if (hasWorktrees)
5755
+ return "Finished sessions";
5756
+ return;
5757
+ };
5758
+ const sessionOptions = scan.candidates.map((candidate) => {
5759
+ const category = sessionOptionCategory(candidate.directory);
5760
+ return {
5555
5761
  title: candidate.title,
5556
5762
  description: formatCleanupSubtitle(candidate),
5557
5763
  ...openTabIDs.has(candidate.sessionID) ? {
5558
5764
  footer: "open tab"
5559
5765
  } : {},
5560
- ...multiWorktree ? {
5561
- category: worktreeLabel(candidate.directory)
5766
+ ...category ? {
5767
+ category
5562
5768
  } : {},
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) => {
5769
+ value: `session:${candidate.sessionID}`
5770
+ };
5771
+ });
5772
+ const worktreeOptions = worktrees.map((worktree) => ({
5773
+ title: worktree.name,
5774
+ description: formatWorktreeCleanupSubtitle(worktree),
5775
+ ...worktree.removable ? {} : {
5776
+ footer: "busy"
5777
+ },
5778
+ category: "Forgekeeper worktrees",
5779
+ value: `worktree:${worktree.directory}`
5780
+ }));
5781
+ const reopenIfRemaining = (next) => {
5782
+ if (next.candidates.length > 0 || (next.worktrees?.length ?? 0) > 0) {
5783
+ showCleanup(next, directory);
5784
+ }
5785
+ };
5786
+ const actions = [...scan.candidates.length > 0 ? [{
5787
+ bind: "ctrl+a",
5788
+ title: hasWorktrees ? `Clean up sessions (${scan.candidates.length})` : `Clean up all (${scan.candidates.length})`,
5789
+ selection: "none",
5790
+ onTrigger: async () => {
5791
+ context.ui.dialog.clear();
5792
+ const result = await runSessionCleanup(directory, scan.candidates.map((candidate) => candidate.sessionID), true);
5793
+ const doneIDs = [...result?.archived ?? [], ...result?.alreadyArchived ?? []];
5794
+ const next = applySessionCleanupToScan(scan, doneIDs, result?.sweptWorktrees);
5795
+ reopenIfRemaining(next);
5796
+ }
5797
+ }] : [], ...removableWorktrees.length > 0 ? [{
5798
+ bind: "ctrl+w",
5799
+ title: `Remove worktrees (${removableWorktrees.length})`,
5800
+ selection: "none",
5801
+ onTrigger: async () => {
5802
+ context.ui.dialog.clear();
5803
+ const result = await runWorktreeCleanup(directory, removableWorktrees);
5804
+ const next = result ? applyWorktreeCleanupToScan(scan, result.removed, result.archivedSessions) : scan;
5805
+ reopenIfRemaining(next);
5806
+ }
5807
+ }] : [], {
5808
+ bind: "ctrl+o",
5809
+ title: "Open session",
5810
+ onTrigger: (selectedValue) => {
5811
+ if (selectedValue.startsWith("session:")) {
5812
+ const sessionID = selectedValue.slice("session:".length);
5577
5813
  context.ui.dialog.clear();
5578
5814
  if (context.ui.tabs.enabled() && context.ui.tabs.focus(sessionID))
5579
5815
  return;
@@ -5581,29 +5817,56 @@ var tui_default = {
5581
5817
  type: "session",
5582
5818
  sessionID
5583
5819
  });
5820
+ return;
5584
5821
  }
5585
- }, {
5586
- bind: "ctrl+r",
5587
- title: "Refresh",
5588
- selection: "none",
5589
- onTrigger: () => {
5822
+ if (selectedValue.startsWith("worktree:")) {
5823
+ const dir = selectedValue.slice("worktree:".length);
5824
+ const target = worktrees.find((item) => item.directory === dir);
5825
+ const sessionID = target?.extraSessions[0]?.sessionID ?? target?.linkedSessionIDs[0];
5826
+ if (!sessionID)
5827
+ return;
5590
5828
  context.ui.dialog.clear();
5591
- showCleanup(undefined, directory);
5829
+ if (context.ui.tabs.enabled() && context.ui.tabs.focus(sessionID))
5830
+ return;
5831
+ context.ui.router.navigate({
5832
+ type: "session",
5833
+ sessionID
5834
+ });
5592
5835
  }
5593
- }]
5836
+ }
5837
+ }, {
5838
+ bind: "ctrl+r",
5839
+ title: "Refresh",
5840
+ selection: "none",
5841
+ onTrigger: () => {
5842
+ context.ui.dialog.clear();
5843
+ showCleanup(undefined, directory);
5844
+ }
5845
+ }];
5846
+ const selected = await context.ui.dialog.select({
5847
+ title: cleanupDialogTitle(scan.skippedErrors, hasWorktrees),
5848
+ options: [...sessionOptions, ...worktreeOptions],
5849
+ actions
5594
5850
  });
5595
5851
  if (!selected)
5596
5852
  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);
5853
+ if (selected.startsWith("session:")) {
5854
+ const sessionID = selected.slice("session:".length);
5855
+ const result = await runSessionCleanup(directory, [sessionID]);
5856
+ const doneIDs = [...result?.archived ?? [], ...result?.alreadyArchived ?? []];
5857
+ if (doneIDs.length > 0) {
5858
+ reopenIfRemaining(applySessionCleanupToScan(scan, doneIDs, result?.sweptWorktrees));
5606
5859
  }
5860
+ return;
5861
+ }
5862
+ if (selected.startsWith("worktree:")) {
5863
+ const dir = selected.slice("worktree:".length);
5864
+ const target = worktrees.find((item) => item.directory === dir);
5865
+ if (!target)
5866
+ return;
5867
+ const result = await runWorktreeCleanup(directory, [target]);
5868
+ const next = result ? applyWorktreeCleanupToScan(scan, result.removed, result.archivedSessions) : scan;
5869
+ reopenIfRemaining(next);
5607
5870
  }
5608
5871
  }
5609
5872
  context.ui.slot({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-forgekeeper",
3
- "version": "6.6.0",
3
+ "version": "6.7.1",
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",