@staff0rd/assist 0.1016.2 → 0.1018.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/index.js CHANGED
@@ -6,7 +6,7 @@ import { Command } from "commander";
6
6
  // package.json
7
7
  var package_default = {
8
8
  name: "@staff0rd/assist",
9
- version: "0.1016.2",
9
+ version: "0.1018.0",
10
10
  type: "module",
11
11
  main: "dist/index.js",
12
12
  bin: {
@@ -8656,8 +8656,8 @@ function subsequenceScore(text18, query) {
8656
8656
  function scoreFilePath(path109, query) {
8657
8657
  const needle = query.trim().toLowerCase();
8658
8658
  if (!needle) return 0;
8659
- const basename35 = path109.slice(path109.lastIndexOf("/") + 1);
8660
- const inBasename = subsequenceScore(basename35, needle);
8659
+ const basename36 = path109.slice(path109.lastIndexOf("/") + 1);
8660
+ const inBasename = subsequenceScore(basename36, needle);
8661
8661
  if (inBasename !== null) return BASENAME_WEIGHT + inBasename;
8662
8662
  return subsequenceScore(path109, needle);
8663
8663
  }
@@ -28590,8 +28590,8 @@ var pinnedPr;
28590
28590
  function getPinnedPr() {
28591
28591
  return pinnedPr;
28592
28592
  }
28593
- function currentPrSelector(currentBranch6) {
28594
- return pinnedPr === void 0 ? currentBranch6() : String(pinnedPr);
28593
+ function currentPrSelector(currentBranch7) {
28594
+ return pinnedPr === void 0 ? currentBranch7() : String(pinnedPr);
28595
28595
  }
28596
28596
 
28597
28597
  // src/commands/prs/shared.ts
@@ -47952,9 +47952,11 @@ function toSessionViewInfo({
47952
47952
  cols,
47953
47953
  rows,
47954
47954
  activeViewer,
47955
- activeViewerNode
47955
+ activeViewerNode,
47956
+ transfer
47956
47957
  }) {
47957
47958
  return {
47959
+ transfer,
47958
47960
  lastUserMessage,
47959
47961
  lastAssistantMessage,
47960
47962
  cols,
@@ -51656,7 +51658,7 @@ function killedByDaemonRestart(interrupted) {
51656
51658
 
51657
51659
  // src/commands/sessions/daemon/shouldAutoDismiss.ts
51658
51660
  function shouldAutoDismiss(session, exitCode) {
51659
- if (session.status !== "done" || exitCode !== 0) {
51661
+ if (session.status !== "done" || exitCode !== 0 || session.transfer) {
51660
51662
  return false;
51661
51663
  }
51662
51664
  if (session.reviewStarted) return true;
@@ -53557,19 +53559,27 @@ function toPeerMessage(data) {
53557
53559
  }
53558
53560
 
53559
53561
  // src/commands/sessions/daemon/links/forwardLinkCreate.ts
53562
+ var TRANSFER_CREATE_TIMEOUT_MS = 3e5;
53563
+ function createTimeoutMs(ctx, data) {
53564
+ const base = ctx.deps.createTimeoutMs;
53565
+ return data.transfer ? Math.max(base, TRANSFER_CREATE_TIMEOUT_MS) : base;
53566
+ }
53560
53567
  function armCreateTimeout(ctx, client, data) {
53561
53568
  const node = ctx.spec.name;
53562
- const timer = setTimeout(() => {
53563
- const pending = ctx.relay.pendingCreators;
53564
- const index2 = pending.findIndex((p) => p.timer === timer);
53565
- if (index2 === -1) return;
53566
- pending.splice(index2, 1);
53567
- daemonLog(`link ${node} ws: ${data.type} timed out (cwd=${data.cwd})`);
53568
- sendTo(client, {
53569
- type: "error",
53570
- message: `Session on ${node} did not start: the peer did not respond.`
53571
- });
53572
- }, ctx.deps.createTimeoutMs);
53569
+ const timer = setTimeout(
53570
+ () => {
53571
+ const pending = ctx.relay.pendingCreators;
53572
+ const index2 = pending.findIndex((p) => p.timer === timer);
53573
+ if (index2 === -1) return;
53574
+ pending.splice(index2, 1);
53575
+ daemonLog(`link ${node} ws: ${data.type} timed out (cwd=${data.cwd})`);
53576
+ sendTo(client, {
53577
+ type: "error",
53578
+ message: `Session on ${node} did not start: the peer did not respond.`
53579
+ });
53580
+ },
53581
+ createTimeoutMs(ctx, data)
53582
+ );
53573
53583
  timer.unref?.();
53574
53584
  ctx.relay.pendingCreators.push({ client, timer });
53575
53585
  }
@@ -53584,7 +53594,7 @@ function forwardLinkCreate(ctx, client, data) {
53584
53594
  });
53585
53595
  return;
53586
53596
  }
53587
- const traceId = newTraceId();
53597
+ const traceId = typeof data.traceId === "string" ? data.traceId : newTraceId();
53588
53598
  daemonLog(
53589
53599
  `link ${node} ws: routing ${data.type} (cwd=${data.cwd ?? "default"}) trace=${traceId}`
53590
53600
  );
@@ -54041,6 +54051,7 @@ var NodeLinks = class {
54041
54051
  local: this.localNode(),
54042
54052
  links: this.all().map((link3) => link3.status())
54043
54053
  });
54054
+ isConnected = (node) => this.links.get(node)?.status().state === "connected";
54044
54055
  sendNodes = (client) => sendTo(client, this.nodes());
54045
54056
  route(client, data) {
54046
54057
  if (this.clients.isPeer(client)) return false;
@@ -54404,6 +54415,7 @@ var SessionManager = class {
54404
54415
  );
54405
54416
  }
54406
54417
  hasSession = (id) => this.sessions.has(id);
54418
+ session = (id) => this.sessions.get(id);
54407
54419
  localSessions = () => [...this.sessions.values()].map(toSessionInfo);
54408
54420
  listSessions = () => this.localSessions().concat(this.links.sessions());
54409
54421
  notify = () => {
@@ -55366,6 +55378,123 @@ var sessionSettingHandlers = {
55366
55378
  )
55367
55379
  };
55368
55380
 
55381
+ // src/commands/sessions/daemon/worktree/allocatePreparedAndBind.ts
55382
+ function allocatePreparedAndBind(ctx, cwd, create, prepare, options2 = {}, context) {
55383
+ const alloc = allocateTree(cwd, boundTreeRoots(ctx.sessions), options2);
55384
+ const prepared = (alloc.ready ?? Promise.resolve()).then(
55385
+ () => prepare(alloc.cwd)
55386
+ );
55387
+ prepared.catch(() => {
55388
+ });
55389
+ const id = ctx.spawnWith((sid) => create(sid, alloc.cwd), context);
55390
+ const session = ctx.sessions.get(id);
55391
+ bindNewWorktree(
55392
+ session,
55393
+ { ...alloc, ready: prepared },
55394
+ ctx.notify,
55395
+ ctx.startHeld
55396
+ );
55397
+ if (session && !alloc.created)
55398
+ prepared.then(
55399
+ () => ctx.startHeld(session),
55400
+ () => {
55401
+ }
55402
+ );
55403
+ return { id, prepared };
55404
+ }
55405
+
55406
+ // src/commands/sessions/daemon/transfer/restoreTransferSnapshot.ts
55407
+ var FETCH_TIMEOUT_MS2 = 12e4;
55408
+ async function switchToSnapshot(path109, snapshot, sha) {
55409
+ const { branch: branch2 } = snapshot;
55410
+ if (!branch2 || branch2 === await remoteDefaultBranch(path109)) {
55411
+ await git2(path109, ["reset", "--hard", "--quiet", sha]);
55412
+ return await gitOrNull(path109, ["symbolic-ref", "--short", "HEAD"]) ?? sha;
55413
+ }
55414
+ await git2(path109, ["checkout", "--quiet", "-B", branch2, sha]);
55415
+ return branch2;
55416
+ }
55417
+ async function trackUpstream(path109, upstream) {
55418
+ await gitResult(path109, ["fetch", "--quiet", "origin", upstream]);
55419
+ const tracked = await gitResult(path109, [
55420
+ "branch",
55421
+ `--set-upstream-to=origin/${upstream}`
55422
+ ]);
55423
+ if (!tracked.ok)
55424
+ daemonLog(
55425
+ `transfer restore ${path109}: could not track origin/${upstream}: ${tracked.error}`
55426
+ );
55427
+ }
55428
+ async function restoreTransferSnapshot(path109, snapshot) {
55429
+ if (await gitOrNull(path109, ["status", "--porcelain"]))
55430
+ throw new Error(`${path109} has uncommitted changes`);
55431
+ await git2(
55432
+ path109,
55433
+ ["fetch", "--quiet", "origin", `refs/heads/${snapshot.ref}`],
55434
+ FETCH_TIMEOUT_MS2
55435
+ );
55436
+ const sha = (await git2(path109, ["rev-parse", "FETCH_HEAD"])).trim();
55437
+ const branch2 = await switchToSnapshot(path109, snapshot, sha);
55438
+ await git2(path109, ["reset", "--soft", "--quiet", "HEAD~1"]);
55439
+ if (snapshot.upstream) await trackUpstream(path109, snapshot.upstream);
55440
+ daemonLog(
55441
+ `transfer restore ${path109}: ${snapshot.ref} restored onto ${branch2}${snapshot.upstream ? ` tracking origin/${snapshot.upstream}` : ""}`
55442
+ );
55443
+ const deleted = await gitResult(
55444
+ path109,
55445
+ ["push", "--no-verify", "--quiet", "origin", "--delete", snapshot.ref],
55446
+ FETCH_TIMEOUT_MS2
55447
+ );
55448
+ daemonLog(
55449
+ deleted.ok ? `transfer restore ${path109}: deleted ${snapshot.ref} from origin` : `transfer restore ${path109}: could not delete ${snapshot.ref} from origin: ${deleted.error}`
55450
+ );
55451
+ }
55452
+
55453
+ // src/commands/sessions/daemon/transfer/spawnTransferredRun.ts
55454
+ async function spawnTransferredRun(ctx, discard, { assistArgs, cwd, meta, snapshot, traceId }, context) {
55455
+ const trace = `trace=${traceId ?? "none"}`;
55456
+ const { id, prepared } = allocatePreparedAndBind(
55457
+ ctx,
55458
+ cwd,
55459
+ (sid, resolvedCwd) => createAssistSession(sid, assistArgs, resolvedCwd, meta, true),
55460
+ async (path109) => {
55461
+ if (!path109) throw new Error("no working tree to restore into");
55462
+ daemonLog(`transfer restore ${path109}: fetching ${snapshot.ref} ${trace}`);
55463
+ await restoreTransferSnapshot(path109, snapshot);
55464
+ },
55465
+ { inPlace: meta.inPlace },
55466
+ context
55467
+ );
55468
+ try {
55469
+ await prepared;
55470
+ } catch (error) {
55471
+ const reason4 = error instanceof Error ? error.message : String(error);
55472
+ daemonLog(
55473
+ `session ${id} transfer restore failed ${trace}: ${reason4}; run not started, ${snapshot.ref} left on origin`
55474
+ );
55475
+ discard(id);
55476
+ throw new Error(`restore failed: ${reason4}`);
55477
+ }
55478
+ daemonLog(`session ${id} transfer restored ${trace}; starting run`);
55479
+ return id;
55480
+ }
55481
+
55482
+ // src/commands/sessions/daemon/transfer/TransferSnapshot.ts
55483
+ function transferRefFor(itemId2) {
55484
+ return `assist/transfer/${itemId2}`;
55485
+ }
55486
+ function parseTransferSnapshot(value) {
55487
+ if (!value || typeof value !== "object") return void 0;
55488
+ const { ref, branch: branch2, upstream } = value;
55489
+ if (typeof ref !== "string" || !ref.startsWith("assist/transfer/"))
55490
+ return void 0;
55491
+ return {
55492
+ ref,
55493
+ branch: typeof branch2 === "string" ? branch2 : void 0,
55494
+ upstream: typeof upstream === "string" ? upstream : void 0
55495
+ };
55496
+ }
55497
+
55369
55498
  // src/commands/sessions/daemon/worktree/prCheckoutNumber.ts
55370
55499
  var CHECKOUT_COMMANDS = /* @__PURE__ */ new Set([
55371
55500
  "review",
@@ -55416,14 +55545,28 @@ function spawnAssistCreate(m, d) {
55416
55545
  const assistArgs = d.assistArgs ?? [];
55417
55546
  const cwd = d.cwd;
55418
55547
  const inPlace = d.inPlace === true;
55548
+ const meta = {
55549
+ title: d.title,
55550
+ subtitle: d.subtitle
55551
+ };
55552
+ const snapshot = parseTransferSnapshot(d.transfer);
55553
+ if (snapshot)
55554
+ return spawnTransferredRun(
55555
+ m.treeCtx(),
55556
+ m.discardSession,
55557
+ {
55558
+ assistArgs,
55559
+ cwd,
55560
+ meta: { ...meta, inPlace },
55561
+ snapshot,
55562
+ traceId: d.traceId
55563
+ },
55564
+ spawnContextFrom(d)
55565
+ );
55419
55566
  const spawn13 = (holder) => m.spawnAssist(
55420
55567
  assistArgs,
55421
55568
  holder ?? cwd,
55422
- {
55423
- title: d.title,
55424
- subtitle: d.subtitle,
55425
- inPlace: inPlace || holder !== void 0
55426
- },
55569
+ { ...meta, inPlace: inPlace || holder !== void 0 },
55427
55570
  spawnContextFrom(d)
55428
55571
  );
55429
55572
  if (inPlace || !isPrCheckoutArgs(assistArgs)) return spawn13();
@@ -55459,6 +55602,207 @@ function spawnCreate(m, d) {
55459
55602
  );
55460
55603
  }
55461
55604
 
55605
+ // src/commands/sessions/shared/backlogRunArgs.ts
55606
+ function backlogRunArgs(session) {
55607
+ const args = session.assistArgs;
55608
+ if (session.commandType !== "assist" || !args) return void 0;
55609
+ if (args[0] !== "backlog" || args[1] !== "run" || !args[2]) return void 0;
55610
+ return args;
55611
+ }
55612
+
55613
+ // src/commands/sessions/daemon/transfer/launchOnTarget.ts
55614
+ function launchOnTarget(route, request) {
55615
+ return new Promise((resolve32) => {
55616
+ const capture = {
55617
+ send: (raw) => {
55618
+ const msg = JSON.parse(raw);
55619
+ if (msg.type === "created")
55620
+ resolve32({ ok: true, sessionId: msg.sessionId });
55621
+ else if (msg.type === "error")
55622
+ resolve32({ ok: false, error: String(msg.message) });
55623
+ }
55624
+ };
55625
+ if (!route(capture, request))
55626
+ resolve32({ ok: false, error: `${request.node} is not a linked node` });
55627
+ });
55628
+ }
55629
+
55630
+ // src/commands/sessions/daemon/transfer/settleTransfer.ts
55631
+ function settleTransfer(client, deps2, s, outcome, traceId) {
55632
+ const target = s.transfer?.target ?? "target";
55633
+ if (!outcome.ok) {
55634
+ daemonLog(
55635
+ `session ${s.id} transfer to ${target} failed trace=${traceId}: ${outcome.error}; source kept`
55636
+ );
55637
+ s.transfer = { target, error: outcome.error };
55638
+ deps2.notify();
55639
+ sendTo(client, {
55640
+ type: "error",
55641
+ message: `Transfer to ${target} failed: ${outcome.error}`
55642
+ });
55643
+ return;
55644
+ }
55645
+ daemonLog(
55646
+ `session ${s.id} transferred to ${target} as ${outcome.sessionId} trace=${traceId}; closing source`
55647
+ );
55648
+ s.transfer = void 0;
55649
+ deps2.discard(s.id);
55650
+ sendTo(client, { type: "notice", message: `Transferred to ${target}` });
55651
+ }
55652
+
55653
+ // src/commands/sessions/daemon/transfer/handOffToTarget.ts
55654
+ async function handOffToTarget(client, deps2, s, { args, target, cwd, traceId, snapshot }) {
55655
+ deps2.stop(s.id);
55656
+ deps2.notify();
55657
+ const outcome = await launchOnTarget(deps2.links.route, {
55658
+ type: "create-assist",
55659
+ assistArgs: args,
55660
+ cwd,
55661
+ node: target,
55662
+ title: s.title,
55663
+ subtitle: s.subtitle,
55664
+ traceId,
55665
+ transfer: snapshot
55666
+ });
55667
+ settleTransfer(client, deps2, s, outcome, traceId);
55668
+ }
55669
+
55670
+ // src/commands/sessions/daemon/transfer/transferRefusal.ts
55671
+ function transferRefusal(s, target, links2) {
55672
+ if (!s) return "unknown session";
55673
+ if (!backlogRunArgs(s)) return "only backlog run sessions can be transferred";
55674
+ if (s.transfer && !s.transfer.error)
55675
+ return `already transferring to ${s.transfer.target}`;
55676
+ if (!target || target === links2.localNode())
55677
+ return `the session already runs on ${links2.localNode()}`;
55678
+ if (!links2.isConnected(target)) return `${target} is not connected`;
55679
+ return void 0;
55680
+ }
55681
+
55682
+ // src/commands/sessions/daemon/transfer/pushTransferSnapshot.ts
55683
+ import { basename as basename35 } from "path";
55684
+ var PUSH_TIMEOUT_MS = 12e4;
55685
+ var WIP_COMMIT = [
55686
+ "-c",
55687
+ "user.name=assist",
55688
+ "-c",
55689
+ "user.email=assist@localhost",
55690
+ "-c",
55691
+ "commit.gpgsign=false",
55692
+ "commit",
55693
+ "--no-verify",
55694
+ "--allow-empty",
55695
+ "--quiet",
55696
+ "-m",
55697
+ "assist transfer WIP"
55698
+ ];
55699
+ async function currentBranch6(path109, treeRoot) {
55700
+ const branch2 = await gitOrNull(path109, ["symbolic-ref", "--short", "HEAD"]);
55701
+ if (!branch2 || treeRoot && branch2 === basename35(treeRoot)) return void 0;
55702
+ return branch2;
55703
+ }
55704
+ async function upstreamBranch2(path109) {
55705
+ const upstream = await gitOrNull(path109, [
55706
+ "rev-parse",
55707
+ "--abbrev-ref",
55708
+ "--symbolic-full-name",
55709
+ "@{u}"
55710
+ ]);
55711
+ if (!upstream?.startsWith("origin/")) return void 0;
55712
+ return upstream.slice("origin/".length);
55713
+ }
55714
+ async function pushTransferSnapshot(path109, itemId2, treeRoot) {
55715
+ const ref = transferRefFor(itemId2);
55716
+ const branch2 = await currentBranch6(path109, treeRoot);
55717
+ const upstream = await upstreamBranch2(path109);
55718
+ const head = (await git2(path109, ["rev-parse", "HEAD"])).trim();
55719
+ await git2(path109, ["add", "-A"]);
55720
+ try {
55721
+ await git2(path109, WIP_COMMIT);
55722
+ await git2(
55723
+ path109,
55724
+ ["push", "--no-verify", "--force", "origin", `HEAD:refs/heads/${ref}`],
55725
+ PUSH_TIMEOUT_MS
55726
+ );
55727
+ } finally {
55728
+ await git2(path109, ["reset", "--mixed", "--quiet", head]);
55729
+ }
55730
+ return { ref, branch: branch2, upstream };
55731
+ }
55732
+
55733
+ // src/commands/sessions/daemon/transfer/snapshotTree.ts
55734
+ async function snapshotTree(s, args, traceId) {
55735
+ const path109 = s.worktree?.path ?? s.cwd;
55736
+ if (!path109) return void 0;
55737
+ const snapshot = await pushTransferSnapshot(path109, args[2], s.worktree?.path);
55738
+ daemonLog(
55739
+ `session ${s.id} pushed ${path109} to ${snapshot.ref} (branch=${snapshot.branch ?? "tree"} upstream=${snapshot.upstream ?? "none"}) trace=${traceId}`
55740
+ );
55741
+ return snapshot;
55742
+ }
55743
+
55744
+ // src/commands/sessions/daemon/transfer/snapshotOrFail.ts
55745
+ async function snapshotOrFail(s, args, traceId) {
55746
+ try {
55747
+ return { ok: true, snapshot: await snapshotTree(s, args, traceId) };
55748
+ } catch (error) {
55749
+ const reason4 = error instanceof Error ? error.message : String(error);
55750
+ return { ok: false, error: `push failed: ${reason4}` };
55751
+ }
55752
+ }
55753
+
55754
+ // src/commands/sessions/daemon/transfer/transferSession.ts
55755
+ function refuse(client, id, message3) {
55756
+ daemonLog(`session ${id} transfer refused: ${message3}`);
55757
+ sendTo(client, { type: "error", message: `Transfer refused: ${message3}` });
55758
+ }
55759
+ async function transferSession(client, deps2, { sessionId: id, target, cwd }) {
55760
+ const s = deps2.session(id);
55761
+ const refused = transferRefusal(s, target, deps2.links);
55762
+ const args = s && backlogRunArgs(s);
55763
+ if (refused || !s || !args)
55764
+ return refuse(client, id, refused ?? "unknown session");
55765
+ const traceId = newTraceId();
55766
+ daemonLog(
55767
+ `session ${id} transfer to ${target} (cwd=${cwd ?? "default"}) trace=${traceId}: snapshotting tree`
55768
+ );
55769
+ s.transfer = { target };
55770
+ deps2.notify();
55771
+ const pushed = await snapshotOrFail(s, args, traceId);
55772
+ if (!pushed.ok) {
55773
+ s.transfer = { target, error: pushed.error };
55774
+ deps2.notify();
55775
+ return refuse(client, id, pushed.error);
55776
+ }
55777
+ await handOffToTarget(client, deps2, s, {
55778
+ args,
55779
+ target,
55780
+ cwd,
55781
+ traceId,
55782
+ snapshot: pushed.snapshot
55783
+ });
55784
+ }
55785
+
55786
+ // src/commands/sessions/daemon/transfer/handleTransfer.ts
55787
+ var handleTransfer = routed((client, m, d) => {
55788
+ daemonLog(`transfer received: id=${d.sessionId} target=${d.target}`);
55789
+ void transferSession(
55790
+ client,
55791
+ {
55792
+ session: m.session,
55793
+ links: m.links,
55794
+ stop: (id) => m.stopSession(id),
55795
+ discard: m.discardSession,
55796
+ notify: () => m.update(() => true)
55797
+ },
55798
+ {
55799
+ sessionId: d.sessionId,
55800
+ target: d.target,
55801
+ cwd: d.cwd
55802
+ }
55803
+ );
55804
+ });
55805
+
55462
55806
  // src/commands/sessions/daemon/messageHandlers.ts
55463
55807
  var messageHandlers = {
55464
55808
  ping: (client) => sendTo(client, { type: "pong", pid: process.pid }),
@@ -55517,6 +55861,7 @@ var messageHandlers = {
55517
55861
  dismiss: routed((_client, m, d) => m.dismissSession(d.sessionId)),
55518
55862
  discard: routed((_client, m, d) => m.discardSession(d.sessionId)),
55519
55863
  stop: routed((_client, m, d) => m.stopSession(d.sessionId)),
55864
+ transfer: handleTransfer,
55520
55865
  ...sessionSettingHandlers,
55521
55866
  "set-active": (_client, m, d) => m.active.set(d.cwd, d.sessionId),
55522
55867
  "set-status": handleSetStatus,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@staff0rd/assist",
3
- "version": "0.1016.2",
3
+ "version": "0.1018.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "bin": {