@ericrisco/rsc 4.1.2 → 4.1.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.
@@ -16,6 +16,7 @@
16
16
  //
17
17
  // Imported by `.rsc/session-start.mjs` (the sweep) and by `scripts/rsc.js` (`rsc worktrees`), so the
18
18
  // rule exists once and both entry points cannot drift apart. Same shape as `sello.mjs`.
19
+ import { readPolicy } from './risk-policy.mjs';
19
20
  import { existsSync, realpathSync, mkdirSync, readFileSync, readdirSync, writeFileSync, renameSync, chmodSync, copyFileSync } from 'node:fs';
20
21
  import { join, resolve, dirname, basename, relative, sep } from 'node:path';
21
22
  import { spawnSync } from 'node:child_process';
@@ -111,7 +112,8 @@ export function resolveMainRoot(cwd = process.cwd()) {
111
112
  }
112
113
 
113
114
  /** Is this ref one of the trunk names? Deleting one is never a side effect of a cleanup. */
114
- export function isTrunkName(branch) {
115
+ export function isTrunkName(branch, root = null) {
116
+ if (root && resolveTrunk(root)?.replace(/^origin\//, '') === branch) return true;
115
117
  return TRUNKS.some((t) => t === branch || t.endsWith(`/${branch}`));
116
118
  }
117
119
 
@@ -122,7 +124,7 @@ export function isCleanupEnabled(root) {
122
124
 
123
125
  /** The first trunk ref that actually exists, or null when there is nothing to compare against. */
124
126
  export function resolveTrunk(root) {
125
- return TRUNKS.find((ref) => git(root, ['rev-parse', '--verify', '--quiet', `${ref}^{commit}`]).ok) || null;
127
+ return readPolicy(root).trunk;
126
128
  }
127
129
 
128
130
  /** Every working tree git knows about. The first entry is always the main one. */
@@ -181,7 +183,36 @@ export function atWorkspaceRoot(p, home) {
181
183
  return home.startsWith(ws + sep) && home !== ws;
182
184
  }
183
185
 
186
+ // External managers own more than Git's directory (setup/teardown hooks, submodules and their
187
+ // registry). A matching branch name never grants us their lifecycle. The committed setting is
188
+ // authoritative even when the manager's executable is absent, e.g. a clone on another machine.
189
+ function orcaOwnership(root, path) {
190
+ for (const dir of new Set([root, path])) {
191
+ const manifest = join(dir, '.rsc.json');
192
+ if (!existsSync(manifest)) continue;
193
+ try {
194
+ if (JSON.parse(readFileSync(manifest, 'utf8'))?.worktreeManager === 'orca') return 'orca';
195
+ } catch { return 'manager-unknown'; }
196
+ }
197
+ const r = spawnSync('orca', ['worktree', 'current', '--json'], {
198
+ cwd: path, encoding: 'utf8', windowsHide: true, timeout: 1500, killSignal: 'SIGKILL', maxBuffer: 256 * 1024,
199
+ });
200
+ if (r.error?.code === 'ENOENT') return null;
201
+ if (r.error || r.status !== 0) return 'manager-unknown';
202
+ try {
203
+ const result = JSON.parse(r.stdout)?.result;
204
+ // Only an explicit empty result is evidence this checkout is unmanaged. A malformed response,
205
+ // timeout or caller identity resolving a different checkout must not authorise plain Git removal.
206
+ if (result && Object.hasOwn(result, 'worktree') && result.worktree === null) return null;
207
+ const wt = result?.worktree;
208
+ return typeof wt?.id === 'string' && wt.id && typeof wt.path === 'string' && real(wt.path) === real(path)
209
+ ? 'orca' : 'manager-unknown';
210
+ } catch { return 'manager-unknown'; }
211
+ }
212
+
184
213
  export function provenanceOf(root, wt) {
214
+ const manager = orcaOwnership(root, wt.path);
215
+ if (manager) return manager;
185
216
  const p = wt.path;
186
217
  const home = real(root);
187
218
  const inside = (dir) => p === dir || p.startsWith(dir + sep);
@@ -320,6 +351,7 @@ export function classifyWorktrees(root) {
320
351
 
321
352
  const provenance = provenanceOf(root, wt);
322
353
  if (provenance === 'foreign') return { ...base, reasons: ['foreign'] };
354
+ if (provenance === 'orca' || provenance === 'manager-unknown') return { ...base, reasons: [provenance] };
323
355
 
324
356
  if (wt.locked) return { ...base, reasons: ['locked'] };
325
357
 
@@ -389,7 +421,7 @@ export function reapWorktree(root, targetPath, { confirmed = false } = {}) {
389
421
  // `-d` refuses an unmerged branch, which is the recovery net — but it happily deletes the TRUNK,
390
422
  // which is an ancestor of everything. A sibling checkout of `main` classified as ambiguous, the user
391
423
  // confirmed removing a stray directory, and the trunk ref went with it.
392
- const branchDeleted = candidate.branch && !isTrunkName(candidate.branch)
424
+ const branchDeleted = candidate.branch && !isTrunkName(candidate.branch, root)
393
425
  ? git(root, ['branch', '-d', candidate.branch]).ok
394
426
  : false;
395
427
  // No `git worktree prune` here. It has no expiry of its own, so running it while another worktree's
@@ -479,7 +511,7 @@ export function reapMergedBranches(root) {
479
511
  for (const line of refs.out.split('\n').filter(Boolean)) {
480
512
  const [branch, track = ''] = line.split('\0');
481
513
  const keep = (reason) => out.kept.push({ branch, reason });
482
- if (branch === defaultName || isTrunkName(branch) || KEEP_BRANCHES.includes(branch)) continue;
514
+ if (branch === defaultName || isTrunkName(branch, root) || KEEP_BRANCHES.includes(branch)) continue;
483
515
  if (branch === current) { keep('current branch'); continue; }
484
516
  if (checkedOut.has(branch)) { keep('checked out in a worktree'); continue; }
485
517
  if (!git(root, ['merge-base', '--is-ancestor', `refs/heads/${branch}`, trunk]).ok) continue; // live work
@@ -651,6 +683,12 @@ export function refusal(candidate) {
651
683
  const d = candidate.details || {};
652
684
  const parts = (candidate.reasons || []).map((reason) => {
653
685
  switch (reason) {
686
+ case 'orca':
687
+ return 'Orca manages this checkout, so rsc will not remove it through Git, even with --confirm. ' +
688
+ 'After reviewing its state, use `orca worktree rm --worktree path:<checkout> --run-hooks --json` with its exact path so Orca runs teardown hooks.';
689
+ case 'manager-unknown':
690
+ return 'worktree-manager ownership could not be verified; nothing was removed. Check `.rsc.json` and run `orca worktree current --json` from that checkout. ' +
691
+ 'If Orca owns it, remove it through `orca worktree rm --worktree path:<checkout> --run-hooks --json` after reviewing its state.';
654
692
  case 'foreign':
655
693
  return 'it was not created by rsc, so it is not ours to delete. Remove it yourself with `git worktree remove` if you want it gone.';
656
694
  case 'not-integrated':