@workit-dev/opencode 1.1.2 → 1.1.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.
Files changed (2) hide show
  1. package/dist/plugin.js +404 -20
  2. package/package.json +2 -2
package/dist/plugin.js CHANGED
@@ -9078,6 +9078,67 @@ var line = (run) => {
9078
9078
  return value ? value : null;
9079
9079
  };
9080
9080
  var safeArg = (value) => value.length > 0 && !value.startsWith("-");
9081
+ function networkGitInvocation(cwd, args, timeoutMs) {
9082
+ const seconds = Math.max(1, Math.ceil(timeoutMs / 1000));
9083
+ const env = {
9084
+ ...process.env,
9085
+ GIT_TERMINAL_PROMPT: "0",
9086
+ GIT_ASKPASS: "",
9087
+ SSH_ASKPASS: "",
9088
+ SSH_ASKPASS_REQUIRE: "never",
9089
+ GCM_INTERACTIVE: "never"
9090
+ };
9091
+ const base = process.env.GIT_SSH_COMMAND || line(git(cwd, ["config", "--get", "core.sshCommand"])) || (process.env.GIT_SSH ? null : "ssh");
9092
+ if (base)
9093
+ env.GIT_SSH_COMMAND = `${base} -o BatchMode=yes -o ConnectTimeout=${Math.min(10, seconds)}`;
9094
+ return {
9095
+ args: ["-c", "http.lowSpeedLimit=1", "-c", `http.lowSpeedTime=${seconds}`, ...args],
9096
+ env
9097
+ };
9098
+ }
9099
+ var WATCHDOG = `
9100
+ const cp = require("node:child_process");
9101
+ const { ms, args } = JSON.parse(process.env.WORKIT_GIT_WATCHDOG);
9102
+ delete process.env.WORKIT_GIT_WATCHDOG;
9103
+ const win = process.platform === "win32";
9104
+ const child = cp.spawn("git", args, { stdio: "inherit", detached: !win, windowsHide: true });
9105
+ const killTree = () => {
9106
+ if (!child.pid) return;
9107
+ if (win) cp.spawnSync("taskkill", ["/pid", String(child.pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
9108
+ else try { process.kill(-child.pid, "SIGKILL"); } catch {}
9109
+ };
9110
+ let timedOut = false;
9111
+ const timer = setTimeout(() => { timedOut = true; killTree(); }, ms);
9112
+ child.on("error", () => { clearTimeout(timer); process.exit(127); });
9113
+ child.on("exit", (code) => {
9114
+ clearTimeout(timer);
9115
+ if (!win) killTree();
9116
+ process.exit(timedOut ? 124 : code ?? 1);
9117
+ });
9118
+ `;
9119
+ var WATCHDOG_TIMEOUT_EXIT = 124;
9120
+ var gitNetwork = (cwd, args, timeoutMs) => {
9121
+ const invocation = networkGitInvocation(cwd, args, timeoutMs);
9122
+ const result = spawnSync3(process.execPath, ["-e", WATCHDOG], {
9123
+ cwd,
9124
+ encoding: "utf8",
9125
+ env: {
9126
+ ...invocation.env,
9127
+ WORKIT_GIT_WATCHDOG: JSON.stringify({ ms: timeoutMs, args: invocation.args })
9128
+ },
9129
+ maxBuffer: MAX_BUFFER,
9130
+ timeout: timeoutMs + 1e4,
9131
+ killSignal: "SIGKILL",
9132
+ windowsHide: true
9133
+ });
9134
+ const error = result.error;
9135
+ return {
9136
+ ok: result.status === 0,
9137
+ stdout: result.stdout ?? "",
9138
+ stderr: result.stderr ?? "",
9139
+ timedOut: result.status === WATCHDOG_TIMEOUT_EXIT || error?.code === "ETIMEDOUT"
9140
+ };
9141
+ };
9081
9142
  var UNPARSEABLE = "<unparseable remote>";
9082
9143
  function redactRemote(raw) {
9083
9144
  const value = raw.trim();
@@ -9229,12 +9290,44 @@ function patchIdOfDiff(cwd, diff) {
9229
9290
  const id = git(cwd, ["patch-id", "--stable"], { input: diff });
9230
9291
  return line(id)?.split(/\s+/u)[0] ?? null;
9231
9292
  }
9293
+ function fetchRefs(cwd, remote, refspecs, options = {}) {
9294
+ if (!safeArg(remote) || refspecs.length === 0 || !refspecs.every((spec) => safeArg(spec.replace(/^\+/u, ""))))
9295
+ return { ok: false, code: "invalid_input", error: "remote and refspecs must not start with -" };
9296
+ const timeoutMs = options.timeoutMs ?? GIT_TIMEOUTS.network;
9297
+ const fetched = gitNetwork(cwd, [
9298
+ "fetch",
9299
+ "--no-tags",
9300
+ "--no-recurse-submodules",
9301
+ "--no-write-fetch-head",
9302
+ "--quiet",
9303
+ remote,
9304
+ ...refspecs
9305
+ ], timeoutMs);
9306
+ if (fetched.ok)
9307
+ return { ok: true };
9308
+ if (/\.lock'?:? File exists|unable to lock|another git process/iu.test(fetched.stderr))
9309
+ return {
9310
+ ok: false,
9311
+ code: "busy",
9312
+ error: `git fetch ${redactRemote(remote)}: the repository is locked by another git process`
9313
+ };
9314
+ return {
9315
+ ok: false,
9316
+ code: "unavailable",
9317
+ error: fetched.timedOut ? `git fetch ${redactRemote(remote)} timed out after ${timeoutMs} ms` : `git fetch ${redactRemote(remote)} failed (unreachable, not authorized, or ref missing)`
9318
+ };
9319
+ }
9232
9320
  function remoteNames(cwd) {
9233
9321
  return (git(cwd, ["remote"]).stdout ?? "").split(/\r?\n/u).map((value) => value.trim()).filter(Boolean);
9234
9322
  }
9235
9323
  function gitCommonDir(cwd) {
9236
9324
  return gitDirs(cwd)?.commonDir ?? null;
9237
9325
  }
9326
+ function hasCommit(cwd, rev) {
9327
+ if (!safeArg(rev))
9328
+ return false;
9329
+ return git(cwd, ["cat-file", "-e", `${rev}^{commit}`]).ok;
9330
+ }
9238
9331
  function resolveRef(cwd, ref) {
9239
9332
  if (!safeArg(ref))
9240
9333
  return null;
@@ -28049,10 +28142,217 @@ var workerCoordinatorFor = (task, worker) => {
28049
28142
  };
28050
28143
 
28051
28144
  // packages/workit-core/src/fanout.ts
28145
+ import { spawnSync as spawnSync28 } from "node:child_process";
28052
28146
  import { createHash as createHash19 } from "node:crypto";
28053
28147
  import fs31 from "node:fs";
28054
28148
  import path50 from "node:path";
28055
28149
 
28150
+ // packages/workit-core/src/fanout-landed.ts
28151
+ import { spawnSync as spawnSync25 } from "node:child_process";
28152
+ function trunkRefresher(cwd, trunk) {
28153
+ const match = /^refs\/remotes\/([^/]+)\/(.+)$/u.exec(trunk);
28154
+ if (!match)
28155
+ return;
28156
+ const [, remote, name] = match;
28157
+ let result;
28158
+ return () => {
28159
+ if (result !== undefined)
28160
+ return result;
28161
+ const fetched = fetchRefs(cwd, remote, [`+refs/heads/${name}:${trunk}`]);
28162
+ result = fetched.ok ? null : `${remote}/${name} could not be refreshed, so a revert check may be stale: run git fetch ${remote} ${name}`;
28163
+ return result;
28164
+ };
28165
+ }
28166
+ var isAncestor = (cwd, a, b) => gitRun3(cwd, ["merge-base", "--is-ancestor", a, b]).ok;
28167
+ function branchCreatedAt(cwd, branch) {
28168
+ if (!branch || branch.startsWith("-"))
28169
+ return null;
28170
+ const log = gitRun3(cwd, ["log", "-g", "--format=%H", `refs/heads/${branch}`, "--"]);
28171
+ if (!log.ok)
28172
+ return null;
28173
+ return log.stdout.trim().split(`
28174
+ `).findLast(Boolean) ?? null;
28175
+ }
28176
+ function hasOwnCommits(cwd, branch, start, head) {
28177
+ if (!branch || branch.startsWith("-"))
28178
+ return false;
28179
+ const log = gitRun3(cwd, ["log", "-g", "--format=%H%x00%gs", `refs/heads/${branch}`, "--"]);
28180
+ if (!log.ok)
28181
+ return false;
28182
+ for (const line of log.stdout.split(`
28183
+ `)) {
28184
+ const [sha, subject = ""] = line.split("\x00");
28185
+ if (!sha)
28186
+ continue;
28187
+ const made = /^(?:commit(?: \((?:initial|amend)\))?|cherry-pick|rebase(?: -i)? \(pick\)):/u.test(subject);
28188
+ const onto = /^rebase(?: -i)? \(finish\): \S+ onto ([0-9a-f]+)/u.exec(subject)?.[1];
28189
+ if (!made && !(onto && !sha.startsWith(onto) && !onto.startsWith(sha)))
28190
+ continue;
28191
+ if (sha !== head && !isAncestor(cwd, sha, head))
28192
+ continue;
28193
+ if (start && (sha === start || isAncestor(cwd, sha, start)))
28194
+ continue;
28195
+ return true;
28196
+ }
28197
+ return false;
28198
+ }
28199
+ function reverted(cwd, trunk, landedHead, start) {
28200
+ const fork = mergeBase(cwd, landedHead, trunk);
28201
+ if (fork && fork !== landedHead)
28202
+ return unchangedSince(cwd, fork, landedHead, trunk);
28203
+ const before = start && start !== landedHead && isAncestor(cwd, start, landedHead) ? start : gitRun3(cwd, ["rev-parse", "--verify", "-q", `${landedHead}^1`]).stdout.trim();
28204
+ return before ? unchangedSince(cwd, before, landedHead, trunk) : false;
28205
+ }
28206
+ function unchangedSince(cwd, before, after, trunk) {
28207
+ const paths = gitRun3(cwd, ["diff", "--name-only", "--no-renames", "-z", before, after]);
28208
+ const files = paths.ok ? paths.stdout.split("\x00").filter(Boolean) : [];
28209
+ if (files.length === 0)
28210
+ return false;
28211
+ return gitRun3(cwd, ["diff", "--quiet", before, trunk, "--", ...files]).ok;
28212
+ }
28213
+ var TRUNK_SCAN = 500;
28214
+ function trunkPatchIds(ctx, fork) {
28215
+ const cache = ctx.cache ??= new Map;
28216
+ const known = cache.get(fork);
28217
+ if (known)
28218
+ return known;
28219
+ const ids = new Set;
28220
+ const log = gitRun3(ctx.cwd, [
28221
+ "log",
28222
+ "--no-merges",
28223
+ `--max-count=${TRUNK_SCAN}`,
28224
+ "-p",
28225
+ "--format=commit %H",
28226
+ "-U3",
28227
+ "--diff-algorithm=myers",
28228
+ "--indent-heuristic",
28229
+ "--no-color",
28230
+ "--no-ext-diff",
28231
+ "--no-textconv",
28232
+ "--no-renames",
28233
+ "--full-index",
28234
+ "--src-prefix=a/",
28235
+ "--dst-prefix=b/",
28236
+ `${fork}..${ctx.trunk}`,
28237
+ "--"
28238
+ ]);
28239
+ if (log.ok && log.stdout.trim()) {
28240
+ const run = spawnSync25("git", ["patch-id", "--stable"], {
28241
+ cwd: ctx.cwd,
28242
+ input: log.stdout,
28243
+ encoding: "utf8",
28244
+ timeout: GIT_TIMEOUTS.local,
28245
+ windowsHide: true,
28246
+ maxBuffer: 64 * 1024 * 1024
28247
+ });
28248
+ for (const line of (run.stdout ?? "").split(`
28249
+ `)) {
28250
+ const id = line.trim().split(/\s+/u)[0];
28251
+ if (id)
28252
+ ids.add(id);
28253
+ }
28254
+ }
28255
+ cache.set(fork, ids);
28256
+ return ids;
28257
+ }
28258
+ function inSliceRun(row, slice) {
28259
+ if (row.type === "fanout.worktree.created" && typeof row.sliceHash === "string" && slice.hash)
28260
+ return row.sliceHash === slice.hash;
28261
+ const since = Date.parse(slice.hashSince);
28262
+ return Number.isNaN(since) || Date.parse(String(row.at)) >= since;
28263
+ }
28264
+ function linked(rows, input, slice, head) {
28265
+ const since = input.since ? Date.parse(input.since) : Number.NaN;
28266
+ const recent = (row) => Number.isNaN(since) || Date.parse(String(row.at)) >= since;
28267
+ return rows.some((row) => {
28268
+ if (row.type === "fanout.worktree.created" && row.fanout === input.fanout && row.slice === slice.id)
28269
+ return typeof row.sliceHash === "string" && input.sliceHash ? row.sliceHash === input.sliceHash : recent(row);
28270
+ return recent(row) && head !== null && row.branch === slice.branch && row.head === head;
28271
+ });
28272
+ }
28273
+ function detectLanding(ctx, slice, input) {
28274
+ const out = {
28275
+ landed: false,
28276
+ how: null,
28277
+ pr: null,
28278
+ landedHead: null,
28279
+ forgeError: null,
28280
+ note: null
28281
+ };
28282
+ const landed = (how, landedHead) => {
28283
+ if (!hasCommit(ctx.cwd, landedHead) || !reverted(ctx.cwd, ctx.trunk, landedHead, input.createdFrom))
28284
+ return { ...out, landed: true, how, landedHead };
28285
+ if (how === "pr_merged" && ctx.forge) {
28286
+ ctx.refreshTrunk ??= trunkRefresher(ctx.cwd, ctx.trunk) ?? (() => null);
28287
+ const failed = ctx.refreshTrunk();
28288
+ if (failed)
28289
+ return { ...out, landed: true, how, landedHead, note: failed };
28290
+ if (!reverted(ctx.cwd, ctx.trunk, landedHead, input.createdFrom))
28291
+ return { ...out, landed: true, how, landedHead };
28292
+ }
28293
+ return {
28294
+ ...out,
28295
+ note: `landed (${how}${out.pr ? ` #${out.pr.number}` : ""}), then reverted on the trunk: its paths read as before it`
28296
+ };
28297
+ };
28298
+ const { head } = input;
28299
+ if (ctx.forge) {
28300
+ const found = ctx.forge.forge.findPr(slice.branch, {
28301
+ owner: ctx.forge.headRepo.split("/")[0] ?? null,
28302
+ projectId: ctx.forge.headProjectId,
28303
+ sha: head
28304
+ });
28305
+ if (!found.ok)
28306
+ out.forgeError = found.error;
28307
+ else if (found.data) {
28308
+ out.pr = found.data;
28309
+ if (found.data.state === "merged") {
28310
+ const merged = found.data.headSha;
28311
+ const label = `#${found.data.number}`;
28312
+ if (!head) {
28313
+ if (!linked(input.rows, input, slice, merged)) {
28314
+ out.pr = null;
28315
+ out.note = `${slice.branch} is gone; merged ${label} is not linked to this slice in the ledger (an older branch of the same name?)`;
28316
+ return out;
28317
+ }
28318
+ return merged ? landed("pr_merged", merged) : { ...out, landed: true, how: "pr_merged" };
28319
+ }
28320
+ if (!merged || head === merged || isAncestor(ctx.cwd, head, merged))
28321
+ return landed("pr_merged", merged ?? head);
28322
+ out.note = isAncestor(ctx.cwd, merged, head) ? `${label} merged at ${merged.slice(0, 12)}; ${slice.branch} is ahead of it with newer commits` : `${label} merged at ${merged.slice(0, 12)}; ${slice.branch} has diverged from it (rewritten after the merge)`;
28323
+ return out;
28324
+ }
28325
+ }
28326
+ }
28327
+ if (!head)
28328
+ return out;
28329
+ const fork = mergeBase(ctx.cwd, head, ctx.trunk);
28330
+ if (fork === head) {
28331
+ return hasOwnCommits(ctx.cwd, slice.branch, input.createdFrom, head) ? landed("on_trunk", head) : out;
28332
+ }
28333
+ if (!fork)
28334
+ return out;
28335
+ const trunkIds = trunkPatchIds(ctx, fork);
28336
+ if (trunkIds.size === 0)
28337
+ return out;
28338
+ const froms = [fork];
28339
+ if (input.parentHead && input.parentHead !== head && isAncestor(ctx.cwd, input.parentHead, head))
28340
+ froms.push(input.parentHead);
28341
+ for (const from of froms) {
28342
+ const id = patchId(ctx.cwd, from, head);
28343
+ if (id && trunkIds.has(id))
28344
+ return landed("squash_patch", head);
28345
+ }
28346
+ return out;
28347
+ }
28348
+ function sliceStart(cwd, rows, fanout, slice) {
28349
+ const reflog = branchCreatedAt(cwd, slice.branch);
28350
+ if (reflog)
28351
+ return reflog;
28352
+ const row = rows.findLast((candidate) => candidate.type === "fanout.worktree.created" && candidate.fanout === fanout && candidate.slice === slice.id && typeof candidate.baseSha === "string" && inSliceRun(candidate, slice));
28353
+ return row?.baseSha ?? null;
28354
+ }
28355
+
28056
28356
  // packages/workit-core/src/checks.ts
28057
28357
  var MAX_LOG_BYTES = 2 * 1024 * 1024;
28058
28358
  var BATCH_BYTES = 64 * 1024;
@@ -28072,18 +28372,18 @@ var GRANT_TTL_MS = 2 * 3600000;
28072
28372
  var NOTICE_TTL_MS = 7 * 86400000;
28073
28373
 
28074
28374
  // packages/workit-core/src/stack.ts
28075
- import { spawnSync as spawnSync26 } from "node:child_process";
28375
+ import { spawnSync as spawnSync27 } from "node:child_process";
28076
28376
  import { createHash as createHash18, randomBytes as randomBytes4 } from "node:crypto";
28077
28377
  import fs30 from "node:fs";
28078
28378
  import path49 from "node:path";
28079
28379
 
28080
28380
  // packages/workit-core/src/git/ops.ts
28081
- import { spawnSync as spawnSync25 } from "node:child_process";
28381
+ import { spawnSync as spawnSync26 } from "node:child_process";
28082
28382
  import { copyFileSync as copyFileSync2, existsSync as existsSync14, readdirSync as readdirSync5, readFileSync as readFileSync12, rmSync as rmSync3 } from "node:fs";
28083
28383
  import path48 from "node:path";
28084
28384
  var COMMIT_TIMEOUT_MS = 10 * 60000;
28085
28385
  var git5 = (cwd, args, options = {}) => {
28086
- const result = spawnSync25("git", args, {
28386
+ const result = spawnSync26("git", args, {
28087
28387
  cwd,
28088
28388
  encoding: "utf8",
28089
28389
  env: { ...options.env ?? process.env, GIT_TERMINAL_PROMPT: "0" },
@@ -28189,8 +28489,8 @@ var stackDeps = {
28189
28489
  adapter: { name: "git", restack: (cwd, input) => gitRestack(cwd, input) },
28190
28490
  rebaseTimeoutMs: Number(process.env.WORKIT_STACK_REBASE_TIMEOUT_MS) || 10 * 60000
28191
28491
  };
28192
- var gitRun3 = (cwd, args, timeoutMs = GIT_TIMEOUTS.local) => {
28193
- const run = spawnSync26("git", args, {
28492
+ var gitRun4 = (cwd, args, timeoutMs = GIT_TIMEOUTS.local) => {
28493
+ const run = spawnSync27("git", args, {
28194
28494
  cwd,
28195
28495
  encoding: "utf8",
28196
28496
  timeout: timeoutMs,
@@ -28216,7 +28516,7 @@ var gitRun3 = (cwd, args, timeoutMs = GIT_TIMEOUTS.local) => {
28216
28516
  };
28217
28517
  var branchTip = (cwd, branch) => resolveRef(cwd, `refs/heads/${branch}`);
28218
28518
  function worktreeOf(cwd, branch) {
28219
- const list = gitRun3(cwd, ["worktree", "list", "--porcelain"]);
28519
+ const list = gitRun4(cwd, ["worktree", "list", "--porcelain"]);
28220
28520
  if (!list.ok)
28221
28521
  return null;
28222
28522
  let current = null;
@@ -28233,7 +28533,7 @@ function worktreeOf(cwd, branch) {
28233
28533
  }
28234
28534
  function rebasingBranch2(dir) {
28235
28535
  for (const name of ["rebase-merge/head-name", "rebase-apply/head-name"]) {
28236
- const found = gitRun3(dir, ["rev-parse", "--path-format=absolute", "--git-path", name]);
28536
+ const found = gitRun4(dir, ["rev-parse", "--path-format=absolute", "--git-path", name]);
28237
28537
  if (!found.ok)
28238
28538
  continue;
28239
28539
  try {
@@ -28244,7 +28544,7 @@ function rebasingBranch2(dir) {
28244
28544
  }
28245
28545
  function rebaseInProgress(dir) {
28246
28546
  for (const name of ["rebase-merge", "rebase-apply"]) {
28247
- const found = gitRun3(dir, ["rev-parse", "--path-format=absolute", "--git-path", name]);
28547
+ const found = gitRun4(dir, ["rev-parse", "--path-format=absolute", "--git-path", name]);
28248
28548
  if (found.ok && fs30.existsSync(found.stdout.trim()))
28249
28549
  return true;
28250
28550
  }
@@ -28268,7 +28568,7 @@ function gitRestack(cwd, input) {
28268
28568
  };
28269
28569
  fs30.mkdirSync(scratch, { recursive: true });
28270
28570
  dir = path49.join(scratch, `${input.branch.replace(/[^A-Za-z0-9._-]+/gu, "_").slice(0, 40)}-${randomBytes4(4).toString("hex")}`);
28271
- const added = gitRun3(cwd, ["worktree", "add", "-q", dir, input.branch], GIT_TIMEOUTS.worktree);
28571
+ const added = gitRun4(cwd, ["worktree", "add", "-q", dir, input.branch], GIT_TIMEOUTS.worktree);
28272
28572
  if (!added.ok)
28273
28573
  return {
28274
28574
  ok: false,
@@ -28277,7 +28577,7 @@ function gitRestack(cwd, input) {
28277
28577
  error: firstLine(added)
28278
28578
  };
28279
28579
  }
28280
- const run = gitRun3(dir, [
28580
+ const run = gitRun4(dir, [
28281
28581
  "-c",
28282
28582
  "rebase.autoStash=false",
28283
28583
  "-c",
@@ -28305,11 +28605,11 @@ function gitRestack(cwd, input) {
28305
28605
  error: firstLine(run)
28306
28606
  };
28307
28607
  if (!owner)
28308
- gitRun3(cwd, ["worktree", "remove", "--force", dir]);
28608
+ gitRun4(cwd, ["worktree", "remove", "--force", dir]);
28309
28609
  return { ok: false, conflict: false, worktree: dir, error: firstLine(run) };
28310
28610
  }
28311
28611
  if (!owner)
28312
- gitRun3(cwd, ["worktree", "remove", "--force", dir]);
28612
+ gitRun4(cwd, ["worktree", "remove", "--force", dir]);
28313
28613
  const head = branchTip(cwd, input.branch);
28314
28614
  return head ? { ok: true, head } : {
28315
28615
  ok: false,
@@ -28420,6 +28720,41 @@ function findCycle(slices) {
28420
28720
  }
28421
28721
  return null;
28422
28722
  }
28723
+ var gitRun3 = (cwd, args, timeoutMs = GIT_TIMEOUTS.local) => {
28724
+ const run = spawnSync28("git", args, {
28725
+ cwd,
28726
+ encoding: "utf8",
28727
+ timeout: timeoutMs,
28728
+ killSignal: "SIGKILL",
28729
+ windowsHide: true,
28730
+ maxBuffer: 64 * 1024 * 1024,
28731
+ env: { ...process.env, GIT_TERMINAL_PROMPT: "0", LC_ALL: "C", LANGUAGE: "C" }
28732
+ });
28733
+ return {
28734
+ ok: run.status === 0,
28735
+ status: run.status,
28736
+ stdout: run.stdout ?? "",
28737
+ stderr: run.error ? String(run.error.message) : run.stderr ?? ""
28738
+ };
28739
+ };
28740
+ function branchRef(cwd, branch) {
28741
+ if (!branch || branch.startsWith("-"))
28742
+ return null;
28743
+ const local = resolveRef(cwd, `refs/heads/${branch}`);
28744
+ if (local)
28745
+ return local;
28746
+ const remote = pushRemoteName(cwd) ?? "origin";
28747
+ return resolveRef(cwd, `refs/remotes/${remote}/${branch}`);
28748
+ }
28749
+ function trunkRef(cwd, trunk) {
28750
+ if (!trunk || trunk.startsWith("-"))
28751
+ return null;
28752
+ const remote = pushRemoteName(cwd) ?? "origin";
28753
+ for (const ref of [`refs/remotes/${remote}/${trunk}`, `refs/heads/${trunk}`, trunk])
28754
+ if (resolveRef(cwd, ref))
28755
+ return ref;
28756
+ return null;
28757
+ }
28423
28758
  function sliceHash(slice) {
28424
28759
  const {
28425
28760
  worktree: _worktree,
@@ -28540,6 +28875,36 @@ function listFanouts(cwd) {
28540
28875
  }
28541
28876
  return { ok: true, data: out.toSorted((a, b) => a.name.localeCompare(b.name)) };
28542
28877
  }
28878
+ function planFinished(cwd, plan, rows) {
28879
+ if (plan.slices.length === 0)
28880
+ return false;
28881
+ const trunk = trunkRef(cwd, plan.trunk);
28882
+ const ctx = trunk ? { cwd, trunk, forge: null } : null;
28883
+ return plan.slices.every((slice) => {
28884
+ const head = branchRef(cwd, slice.branch);
28885
+ const merged = rows.some((row) => (row.type === "pr.merged" || row.type === "pr.merge.verified") && row.branch === slice.branch && (head === null || row.head === head) && inSliceRun(row, slice));
28886
+ if (merged)
28887
+ return true;
28888
+ if (!head || !ctx)
28889
+ return false;
28890
+ const released = rows.some((row) => row.type === "fanout.worktree.released" && row.fanout === plan.name && row.slice === slice.id && row.outcome === "removed" && inSliceRun(row, slice));
28891
+ if (released) {
28892
+ const ahead = gitRun3(cwd, ["rev-list", "--count", `${ctx.trunk}..${head}`]);
28893
+ if (ahead.ok && ahead.stdout.trim() === "0")
28894
+ return true;
28895
+ }
28896
+ const parent = plan.slices.find((other) => other.branch === slice.base);
28897
+ return detectLanding(ctx, slice, {
28898
+ head,
28899
+ createdFrom: sliceStart(cwd, rows, plan.name, slice),
28900
+ parentHead: parent ? branchRef(cwd, parent.branch) : null,
28901
+ fanout: plan.name,
28902
+ since: slice.hashSince || plan.createdAt || null,
28903
+ sliceHash: slice.hash || null,
28904
+ rows
28905
+ }).landed;
28906
+ });
28907
+ }
28543
28908
  function selectFanout(cwd, name, current, session = null) {
28544
28909
  if (name) {
28545
28910
  const read = readFanout(cwd, name);
@@ -28550,17 +28915,36 @@ function selectFanout(cwd, name, current, session = null) {
28550
28915
  const all = listFanouts(cwd);
28551
28916
  if (!all.ok)
28552
28917
  return all;
28553
- const holding = all.data.filter((plan) => plan.slices.some((slice) => slice.branch === current));
28918
+ if (all.data.length === 0)
28919
+ return fanoutFail("not_found", "no fanout plan is recorded here", "workit fanout plan <plan.json>");
28920
+ let rows = null;
28921
+ const finished = new Map;
28922
+ const isFinished = (plan) => {
28923
+ if (!finished.has(plan.name)) {
28924
+ if (!rows) {
28925
+ const ledger = readLedger(cwd);
28926
+ rows = ledger.ok ? ledger.value.rows : [];
28927
+ }
28928
+ finished.set(plan.name, planFinished(cwd, plan, rows));
28929
+ }
28930
+ return finished.get(plan.name) === true;
28931
+ };
28932
+ const active = (plans) => {
28933
+ if (plans.length < 2)
28934
+ return plans;
28935
+ const live = plans.filter((plan) => !isFinished(plan));
28936
+ return live.length ? live : plans;
28937
+ };
28938
+ const holding = active(all.data.filter((plan) => plan.slices.some((slice) => slice.branch === current)));
28554
28939
  if (holding.length === 1)
28555
28940
  return { ok: true, data: holding[0] };
28556
- const led = session ? all.data.filter((plan) => plan.leadSession === session) : [];
28557
- if (holding.length === 0 && led.length === 1)
28941
+ const led = holding.length === 0 && session ? active(all.data.filter((plan) => plan.leadSession === session)) : [];
28942
+ if (led.length === 1)
28558
28943
  return { ok: true, data: led[0] };
28559
- if (all.data.length === 1)
28560
- return { ok: true, data: all.data[0] };
28561
- if (all.data.length === 0)
28562
- return fanoutFail("not_found", "no fanout plan is recorded here", "workit fanout plan <plan.json>");
28563
- const candidates = (holding.length > 1 ? holding : led.length > 1 ? led : all.data).map((plan) => plan.name);
28944
+ const pool = holding.length > 1 ? holding : led.length > 1 ? led : active(all.data);
28945
+ if (pool.length === 1)
28946
+ return { ok: true, data: pool[0] };
28947
+ const candidates = pool.map((plan) => plan.name);
28564
28948
  return fanoutFail("invalid_input", `several fanout plans match; pick one with --fanout <name>: ${candidates.join(", ")}`, `pass --fanout <name>, one of: ${candidates.join(", ")}`, { candidates });
28565
28949
  }
28566
28950
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workit-dev/opencode",
3
- "version": "1.1.2",
3
+ "version": "1.1.5",
4
4
  "private": false,
5
5
  "description": "Workit OpenCode plugin — native tools, adaptive policy, and sixteen method skills",
6
6
  "keywords": [
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@opencode/plugin": "2.0.18",
44
- "@workit-dev/core": "^1.1.2"
44
+ "@workit-dev/core": "^1.1.5"
45
45
  },
46
46
  "engines": {
47
47
  "node": ">=24"