@yagni-app/code-staging 1.1.0-staging.1329.1 → 1.1.0-staging.1334.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/cli.d.ts CHANGED
@@ -12,6 +12,7 @@
12
12
  * plus a configured pi spawn. Everything that makes this "YAGNI Code" lives in
13
13
  * pi-extension-yagni and the YAGNI backend.
14
14
  */
15
+ import { promptKeepOrRemoveWorktree } from "./worktreeExitPrompt.js";
15
16
  /**
16
17
  * Seed `editorPaddingX` into the per-profile pi `settings.json` so the prompt
17
18
  * input aligns with the chat/output area (`outputPad`) and the status bar.
@@ -42,6 +43,56 @@ export declare function seedCollapseChangelog(piAgentDir: string): void;
42
43
  * persists the user's choice, so this default is always reversible.
43
44
  */
44
45
  export declare function seedHideThinkingBlock(piAgentDir: string): boolean;
46
+ /** The structural slice of the extension's session-worktree entry (by file path). */
47
+ interface SessionWorktreeModule {
48
+ createOrResume: (name: string | undefined, deps: {
49
+ repoCwd: string;
50
+ }) => Promise<{
51
+ worktreePath: string;
52
+ branch: string;
53
+ existed: boolean;
54
+ }>;
55
+ /** Optional so a stale bundled extension (without the exit prompt support) degrades to the durable summary. */
56
+ removeSessionWorktree?: (worktreePath: string, branch: string, deps: {
57
+ repoCwd: string;
58
+ }) => Promise<{
59
+ removed: boolean;
60
+ branchRemoved: boolean;
61
+ note?: string;
62
+ branchUnmerged?: boolean;
63
+ }>;
64
+ }
65
+ /** Seams `handleWorktreeExit` needs; the real one lives in the extension. */
66
+ type WorktreeRemover = NonNullable<SessionWorktreeModule["removeSessionWorktree"]>;
67
+ /** Minimal stream shapes for testability (process.stdin/stderr satisfy these). */
68
+ interface WriteOnlyStream {
69
+ write(chunk: string): unknown;
70
+ isTTY?: boolean;
71
+ }
72
+ interface ReadablePromptStream {
73
+ isTTY?: boolean;
74
+ }
75
+ /** Pure: the one-line worktree summary for the kept path. */
76
+ export declare function worktreeKeptSummary(worktreePath: string, branch: string, existed: boolean): string;
77
+ /**
78
+ * The post-session worktree exit: prompt Keep/Remove on interactive text
79
+ * sessions, perform the removal when chosen, and print exactly ONE summary
80
+ * (kept / not removed / removed) to stderr. Fail-safe in every direction:
81
+ * an aborted prompt, a refused removal, or an erroring remover all leave the
82
+ * worktree in place with the durable resume summary printed.
83
+ */
84
+ export declare function handleWorktreeExit(deps: {
85
+ worktreePath: string;
86
+ branch: string;
87
+ existed: boolean;
88
+ outputFormat: string;
89
+ remainingArgs: string[];
90
+ stdin: ReadablePromptStream;
91
+ stderr: WriteOnlyStream;
92
+ cwd: string;
93
+ removeWorktree?: WorktreeRemover;
94
+ prompt?: typeof promptKeepOrRemoveWorktree;
95
+ }): Promise<void>;
45
96
  export declare const HELP_TEXT: string;
46
97
  /** Parse `use <name> [--base-url <url>]` argv into its parts. */
47
98
  export declare function parseUseArgs(args: string[]): {
@@ -82,4 +133,5 @@ export declare function main(argv: string[]): Promise<number>;
82
133
  * test import (argv[1] points at the test runner) does not.
83
134
  */
84
135
  export declare function isEntrypoint(argv1: string | undefined, moduleUrl: string): boolean;
136
+ export {};
85
137
  //# sourceMappingURL=cli.d.ts.map
package/dist/cli.js CHANGED
@@ -14,7 +14,7 @@
14
14
  */
15
15
  import { spawn } from "node:child_process";
16
16
  import { chmodSync, existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync, } from "node:fs";
17
- import { join } from "node:path";
17
+ import { dirname, join } from "node:path";
18
18
  import { createInterface } from "node:readline/promises";
19
19
  import { fileURLToPath, pathToFileURL } from "node:url";
20
20
  import { PI_CONFIG_NAME } from "./branding.js";
@@ -38,7 +38,8 @@ import { currentCliVersion, maybeNudgeAndRefresh, upgradeCommand } from "./upgra
38
38
  import { maybeRefreshAtLaunch } from "./refresh.js";
39
39
  import { exitCodeFor, installSignalForwarding } from "./signalForward.js";
40
40
  import { PAD_X } from "./padding.js";
41
- import { parseWorktreeFlag, validateWorktreeLaunchArgs } from "./worktreeArgs.js";
41
+ import { canPromptWorktreeCleanup, parseWorktreeFlag, validateWorktreeLaunchArgs, } from "./worktreeArgs.js";
42
+ import { promptKeepOrRemoveWorktree } from "./worktreeExitPrompt.js";
42
43
  import { ensureShadowPiPackage } from "./piPackage.js";
43
44
  import { resolveExtensionPath, resolvePiCliPath, resolvePiPackageDir, resolveSessionWorktreePath } from "./paths.js";
44
45
  import { credentialsFromProfile, getActiveProfileName, listProfiles, migrateLegacyCredentials, persistProfileTokenRotation, profilePath, readActiveProfile, useProfile, } from "./profiles.js";
@@ -371,7 +372,8 @@ async function defaultLoadSessionWorktree() {
371
372
  *
372
373
  * Delegates all git behavior to the extension's `sessionWorktree` entry; this
373
374
  * launcher path only resolves credentials, builds the plan, and spawns pi with
374
- * `cwd` = the worktree. The worktree is DURABLE — nothing is ever removed.
375
+ * `cwd` = the worktree. Durable by default: nothing is removed unless the
376
+ * user explicitly picks Remove at the interactive exit prompt.
375
377
  */
376
378
  async function runWorktreeLaunch(passthroughArgs, worktreeName, loadSessionWorktree = defaultLoadSessionWorktree) {
377
379
  // Validate the name + argv before any side effect (slug guard + `-c`
@@ -486,13 +488,104 @@ async function runWorktreeLaunch(passthroughArgs, worktreeName, loadSessionWorkt
486
488
  outputFormat,
487
489
  cwd: result.worktreePath,
488
490
  });
489
- // Durable by default: nothing is removed. Tell the user where their work
490
- // lives (stderr only — never stdout, to keep json clean).
491
- process.stderr.write(`[yagni] worktree ${result.existed ? "resumed" : "created"}: ${result.worktreePath}\n` +
492
- `[yagni] branch: ${result.branch}\n` +
493
- `[yagni] resume: cd ${result.worktreePath} && yagni\n`);
491
+ // Interactive text sessions get a Keep/Remove list (Claude Code parity).
492
+ // Machine-readable runs (`--output-format json|stream-json`), one-shot `-p`
493
+ // runs, and piped stdin keep the durable-by-default behavior: no prompt,
494
+ // nothing removed. A stale bundled extension without
495
+ // `removeSessionWorktree` also degrades to the durable summary. All output
496
+ // is stderr — stdout stays clean for every output format.
497
+ await handleWorktreeExit({
498
+ worktreePath: result.worktreePath,
499
+ branch: result.branch,
500
+ existed: result.existed,
501
+ outputFormat,
502
+ remainingArgs,
503
+ stdin: process.stdin,
504
+ stderr: process.stderr,
505
+ cwd: process.cwd(),
506
+ removeWorktree: sessionWorktree.removeSessionWorktree,
507
+ });
494
508
  return exitCode;
495
509
  }
510
+ /** Pure: the one-line worktree summary for the kept path. */
511
+ export function worktreeKeptSummary(worktreePath, branch, existed) {
512
+ return (`[yagni] worktree ${existed ? "resumed" : "created"}: ${worktreePath}\n` +
513
+ `[yagni] branch: ${branch}\n` +
514
+ `[yagni] resume: cd ${worktreePath} && yagni\n`);
515
+ }
516
+ /**
517
+ * The post-session worktree exit: prompt Keep/Remove on interactive text
518
+ * sessions, perform the removal when chosen, and print exactly ONE summary
519
+ * (kept / not removed / removed) to stderr. Fail-safe in every direction:
520
+ * an aborted prompt, a refused removal, or an erroring remover all leave the
521
+ * worktree in place with the durable resume summary printed.
522
+ */
523
+ export async function handleWorktreeExit(deps) {
524
+ const { worktreePath, branch, existed, outputFormat, remainingArgs, stdin, stderr, cwd, removeWorktree, } = deps;
525
+ const prompt = deps.prompt ?? promptKeepOrRemoveWorktree;
526
+ const canPrompt = canPromptWorktreeCleanup(outputFormat, remainingArgs, stdin.isTTY === true) &&
527
+ removeWorktree !== undefined;
528
+ let choice;
529
+ if (canPrompt) {
530
+ try {
531
+ choice = await prompt({
532
+ input: stdin,
533
+ output: stderr,
534
+ label: `${worktreePath} — keep or remove?`,
535
+ });
536
+ }
537
+ catch (err) {
538
+ // A genuine prompt failure (stdin/raw-mode error) must not read as a
539
+ // silent no-op: say it failed, then keep the worktree as with any abort.
540
+ stderr.write(`[yagni] exit prompt failed: ${err instanceof Error ? err.message : String(err)}; worktree kept\n`);
541
+ choice = undefined;
542
+ }
543
+ }
544
+ if (choice !== "remove") {
545
+ // Keep (default), aborted prompt, or prompt suppressed: durable summary.
546
+ stderr.write(worktreeKeptSummary(worktreePath, branch, existed));
547
+ return;
548
+ }
549
+ let outcome;
550
+ try {
551
+ outcome = await removeWorktree(worktreePath, branch, { repoCwd: cwd });
552
+ }
553
+ catch (err) {
554
+ // The remover throwing (vs returning removed:false) still keeps the work;
555
+ // never let cleanup turn a successful session into a nonzero exit.
556
+ stderr.write(`[yagni] worktree NOT removed: ${err instanceof Error ? err.message : String(err)}\n` +
557
+ worktreeKeptSummary(worktreePath, branch, existed));
558
+ return;
559
+ }
560
+ if (!outcome.removed) {
561
+ // Git refused (dirty worktree) or failed — the work and the resume path
562
+ // are both intact, so say so and print the kept summary (not a duplicate).
563
+ stderr.write(`[yagni] worktree NOT removed${outcome.note ? `: ${outcome.note}` : ""}\n` +
564
+ worktreeKeptSummary(worktreePath, branch, existed));
565
+ return;
566
+ }
567
+ stderr.write(`[yagni] worktree removed\n` +
568
+ branchKeptMessage(outcome, branch, worktreePath));
569
+ }
570
+ /**
571
+ * The branch-status tail of the removed summary. The extension's safe `-d`
572
+ * (never `-D`) keeps an unmerged branch on disk when its worktree is removed,
573
+ * so the recovery hint points at an ABSOLUTE path (works from any cwd; a
574
+ * relative .worktrees/<slug> only resolves from the main repo root). A
575
+ * branch kept for another reason (checked out elsewhere, git error) states
576
+ * that reason itself instead of the generic unmerged framing.
577
+ */
578
+ function branchKeptMessage(outcome, branch, worktreePath) {
579
+ if (outcome.branchRemoved)
580
+ return "";
581
+ const slug = branch.replace(/^agent\//, "");
582
+ const recovery = `[yagni] branch ${branch} kept — recover with: ` +
583
+ `git worktree add "${join(dirname(worktreePath), slug)}" ${branch}\n`;
584
+ if (outcome.branchUnmerged)
585
+ return recovery;
586
+ const reason = (outcome.note ?? "unknown reason").replace(/\s+/g, " ").trim();
587
+ return `[yagni] branch ${branch} kept: ${reason}\n` + recovery;
588
+ }
496
589
  export const HELP_TEXT = [
497
590
  "YAGNI Code — a business-context-grounded terminal coding agent.",
498
591
  "",
@@ -10,6 +10,8 @@
10
10
  * - **Add-only creation.** Every git op is `worktree add`, `fetch`, `show-ref`,
11
11
  * `symbolic-ref`, or `rev-parse`. Nothing deletes, force-resets, or
12
12
  * `branch -D`s — the worktree is DURABLE by default and never auto-removed.
13
+ * The ONE removal path is `removeSessionWorktree`, which runs only after an
14
+ * explicit "Remove worktree" choice at session exit (never automatic).
13
15
  * - **Get-or-resume.** An existing worktree dir is resumed, never recreated.
14
16
  * - **Lazy fetch.** Base `origin/<default>` is read from the local ref when
15
17
  * present; `git fetch` only runs when that ref is absent, and always with
@@ -23,6 +25,18 @@
23
25
  * (both gitignored in-repo). PR refs (`#N`, GitHub PR URLs) map to `pr-<N>` and
24
26
  * base on `FETCH_HEAD`.
25
27
  */
28
+ export interface RemoveSessionWorktreeResult {
29
+ /** True when the worktree directory is gone from disk. */
30
+ removed: boolean;
31
+ /** True when the `agent/<slug>` branch was also deleted (`-d`, safe delete). */
32
+ branchRemoved: boolean;
33
+ /** Non-fatal note when cleanup was attempted but could not finish. */
34
+ note?: string;
35
+ /** True when the branch was kept because it is not fully merged (the benign
36
+ * case the recovery hint describes); false when it was kept for another
37
+ * reason (checked out elsewhere, git error) — the note carries that. */
38
+ branchUnmerged?: boolean;
39
+ }
26
40
  export interface SessionWorktreeResult {
27
41
  /** Absolute destination (under `<mainRepo>/.worktrees/<slug>`). */
28
42
  worktreePath: string;
@@ -61,4 +75,29 @@ export declare function parsePRReference(input: string): number | null;
61
75
  * branch/worktree: validation happens first, and `git worktree add` is atomic.
62
76
  */
63
77
  export declare function createOrResume(name: string | undefined, deps: CreateOrResumeDeps): Promise<SessionWorktreeResult>;
78
+ /**
79
+ * Remove a session worktree the user explicitly chose to discard at exit.
80
+ *
81
+ * The user's only entry into this is the launcher's "Remove worktree" prompt;
82
+ * it is NEVER called automatically. Cleanup checks the worktree again at exit:
83
+ *
84
+ * - Require the original branch: removing a clean detached HEAD would discard
85
+ * its only reference and reflog, even without `--force`.
86
+ * - Refuse ignored content, which Git's dirty check does not protect. This
87
+ * includes dependency directories and local env files; review those manually.
88
+ * - `git worktree remove` without `--force` refuses tracked changes and
89
+ * non-ignored untracked files. `git branch -d` retains branches Git considers
90
+ * unmerged (against their upstream, or the main checkout's HEAD).
91
+ *
92
+ * These checks do not lock out concurrent edits by other sessions or Git clients.
93
+ *
94
+ * `repoCwd` is only used to locate the repo (resolved to the main repo root
95
+ * before any git op, same as `createOrResume`): the user may have launched
96
+ * `yagni -w` from inside the worktree being removed, and after `git worktree
97
+ * remove` that cwd no longer exists on disk.
98
+ */
99
+ export declare function removeSessionWorktree(worktreePath: string, branch: string, deps: {
100
+ repoCwd: string;
101
+ gitImpl?: SessionGit;
102
+ }): Promise<RemoveSessionWorktreeResult>;
64
103
  //# sourceMappingURL=sessionWorktree.d.ts.map
@@ -10,6 +10,8 @@
10
10
  * - **Add-only creation.** Every git op is `worktree add`, `fetch`, `show-ref`,
11
11
  * `symbolic-ref`, or `rev-parse`. Nothing deletes, force-resets, or
12
12
  * `branch -D`s — the worktree is DURABLE by default and never auto-removed.
13
+ * The ONE removal path is `removeSessionWorktree`, which runs only after an
14
+ * explicit "Remove worktree" choice at session exit (never automatic).
13
15
  * - **Get-or-resume.** An existing worktree dir is resumed, never recreated.
14
16
  * - **Lazy fetch.** Base `origin/<default>` is read from the local ref when
15
17
  * present; `git fetch` only runs when that ref is absent, and always with
@@ -103,7 +105,7 @@ async function resolveMainRepo(gitImpl, repoCwd) {
103
105
  topLevel = await gitImpl(["rev-parse", "--show-toplevel"], repoCwd);
104
106
  }
105
107
  catch (err) {
106
- throw new Error(`Cannot create a worktree: not inside a git repository. ` +
108
+ throw new Error(`Cannot resolve the repository root: not inside a git repository. ` +
107
109
  `${err instanceof Error ? err.message : String(err)}`);
108
110
  }
109
111
  const common = await gitImpl(["rev-parse", "--git-common-dir"], repoCwd);
@@ -222,4 +224,80 @@ export async function createOrResume(name, deps) {
222
224
  }
223
225
  return { worktreePath, branch, existed: false };
224
226
  }
227
+ /**
228
+ * Remove a session worktree the user explicitly chose to discard at exit.
229
+ *
230
+ * The user's only entry into this is the launcher's "Remove worktree" prompt;
231
+ * it is NEVER called automatically. Cleanup checks the worktree again at exit:
232
+ *
233
+ * - Require the original branch: removing a clean detached HEAD would discard
234
+ * its only reference and reflog, even without `--force`.
235
+ * - Refuse ignored content, which Git's dirty check does not protect. This
236
+ * includes dependency directories and local env files; review those manually.
237
+ * - `git worktree remove` without `--force` refuses tracked changes and
238
+ * non-ignored untracked files. `git branch -d` retains branches Git considers
239
+ * unmerged (against their upstream, or the main checkout's HEAD).
240
+ *
241
+ * These checks do not lock out concurrent edits by other sessions or Git clients.
242
+ *
243
+ * `repoCwd` is only used to locate the repo (resolved to the main repo root
244
+ * before any git op, same as `createOrResume`): the user may have launched
245
+ * `yagni -w` from inside the worktree being removed, and after `git worktree
246
+ * remove` that cwd no longer exists on disk.
247
+ */
248
+ export async function removeSessionWorktree(worktreePath, branch, deps) {
249
+ const gitImpl = deps.gitImpl ?? defaultGit;
250
+ // Resolve the MAIN repo root first: every later git op must run from a cwd
251
+ // that survives the removal (repoCwd may be the worktree being removed).
252
+ const repoRoot = await resolveMainRepo(gitImpl, deps.repoCwd);
253
+ try {
254
+ const ignored = await gitImpl(["ls-files", "--others", "--ignored", "--exclude-standard", "--directory", "-z"], worktreePath);
255
+ if (ignored) {
256
+ const paths = ignored.split("\0").filter(Boolean);
257
+ const preview = paths.slice(0, 5).map((path) => JSON.stringify(path)).join(", ");
258
+ const more = paths.length > 5 ? ` (+${paths.length - 5} more)` : "";
259
+ return {
260
+ removed: false,
261
+ branchRemoved: false,
262
+ note: `worktree contains ignored content: ${preview}${more}; review and remove it manually before retrying cleanup`,
263
+ };
264
+ }
265
+ const headRef = await gitImpl(["rev-parse", "--symbolic-full-name", "HEAD"], worktreePath);
266
+ if (headRef !== `refs/heads/${branch}`) {
267
+ return {
268
+ removed: false,
269
+ branchRemoved: false,
270
+ note: headRef === "HEAD"
271
+ ? "worktree HEAD is detached; preserve that work on a branch before retrying cleanup"
272
+ : `worktree HEAD is ${JSON.stringify(headRef)}, no longer on branch ${branch}; preserve that work before retrying cleanup`,
273
+ };
274
+ }
275
+ await gitImpl(["worktree", "remove", worktreePath], repoRoot);
276
+ }
277
+ catch (err) {
278
+ return {
279
+ removed: false,
280
+ branchRemoved: false,
281
+ note: err instanceof Error ? err.message : String(err),
282
+ };
283
+ }
284
+ // Safe delete: only removes a fully-merged branch. Unmerged work stays
285
+ // recoverable on the branch — the directory is gone but the ref is not.
286
+ try {
287
+ await gitImpl(["branch", "-d", branch], repoRoot);
288
+ return { removed: true, branchRemoved: true };
289
+ }
290
+ catch (err) {
291
+ // Distinguish the benign case (unmerged branch, deliberately kept) from
292
+ // a git-level failure so the exit summary can say which it was.
293
+ const message = err instanceof Error ? err.message : String(err);
294
+ const unmerged = /not fully merged|unmerged/i.test(message);
295
+ return {
296
+ removed: true,
297
+ branchRemoved: false,
298
+ branchUnmerged: unmerged,
299
+ ...(unmerged ? {} : { note: message }),
300
+ };
301
+ }
302
+ }
225
303
  //# sourceMappingURL=sessionWorktree.js.map
@@ -33,6 +33,17 @@ export declare function parsePRReference(input: string): number | null;
33
33
  * with a clear message (surfaced by the caller).
34
34
  */
35
35
  export declare function validateWorktreeSlug(slug: string): void;
36
+ /**
37
+ * Whether a `-w` session exit can show the interactive Keep/Remove prompt.
38
+ *
39
+ * The prompt is an interactive-TUI-only affordance. It is suppressed for:
40
+ * - `--output-format json|stream-json` (machine-readable stdout must stay a
41
+ * single clean result object; a trailing readline prompt would corrupt it);
42
+ * - `-p`/`--print` one-shot runs (they exit, they never linger for a prompt);
43
+ * - non-TTY stdin (piped/CI runs must not hang on a question nobody answers).
44
+ * The default `text` format with a TTY is the only path that prompts.
45
+ */
46
+ export declare function canPromptWorktreeCleanup(outputFormat: string, remainingArgs: string[], isTTY: boolean): boolean;
36
47
  /**
37
48
  * Validate the `-w` name + argv before any side effect. Returns a user-facing
38
49
  * error message when the launch should be refused, or `undefined` to proceed.
@@ -73,6 +73,23 @@ export function validateWorktreeSlug(slug) {
73
73
  }
74
74
  }
75
75
  }
76
+ /**
77
+ * Whether a `-w` session exit can show the interactive Keep/Remove prompt.
78
+ *
79
+ * The prompt is an interactive-TUI-only affordance. It is suppressed for:
80
+ * - `--output-format json|stream-json` (machine-readable stdout must stay a
81
+ * single clean result object; a trailing readline prompt would corrupt it);
82
+ * - `-p`/`--print` one-shot runs (they exit, they never linger for a prompt);
83
+ * - non-TTY stdin (piped/CI runs must not hang on a question nobody answers).
84
+ * The default `text` format with a TTY is the only path that prompts.
85
+ */
86
+ export function canPromptWorktreeCleanup(outputFormat, remainingArgs, isTTY) {
87
+ if (outputFormat !== "text")
88
+ return false;
89
+ if (!isTTY)
90
+ return false;
91
+ return !remainingArgs.some((a) => a === "-p" || a === "--print" || a.startsWith("--print="));
92
+ }
76
93
  /**
77
94
  * Validate the `-w` name + argv before any side effect. Returns a user-facing
78
95
  * error message when the launch should be refused, or `undefined` to proceed.
@@ -0,0 +1,54 @@
1
+ /**
2
+ * The arrow-key Keep/Remove list shown after an interactive `-w` session exits.
3
+ *
4
+ * Claude Code parity: a two-option list navigated with the up/down arrows and
5
+ * confirmed with Enter — not a y/N question. Keep is the default (highlighted
6
+ * first), and every escape hatch — Esc, q, Ctrl+C, EOF, a non-TTY stream —
7
+ * fails safe to Keep (resolves `undefined`, which callers treat as keep).
8
+ *
9
+ * This module owns the raw-TTY interaction only. The gating (which launches
10
+ * may prompt at all) lives in `canPromptWorktreeCleanup` (worktreeArgs.ts),
11
+ * and the git behavior lives in the extension's `removeSessionWorktree`.
12
+ */
13
+ export type WorktreeExitChoice = "keep" | "remove";
14
+ /** Minimal stream shape the prompt needs (satisfied by process.stdin/stderr). */
15
+ export interface ExitPromptStream {
16
+ write(chunk: string): unknown;
17
+ isTTY?: boolean;
18
+ }
19
+ /** The structural slice of a readline keypress event. */
20
+ export interface ExitPromptKey {
21
+ name?: string;
22
+ ctrl?: boolean;
23
+ }
24
+ export type ExitPromptAction = {
25
+ type: "toggle";
26
+ } | {
27
+ type: "confirm";
28
+ } | {
29
+ type: "abort";
30
+ } | {
31
+ type: "noop";
32
+ };
33
+ /**
34
+ * Map a keypress to its prompt action (pure, unit-tested). Arrows (plus
35
+ * k/j/tab) toggle between the two options; Enter or Space confirms; Esc, q,
36
+ * Ctrl+C, and Ctrl+D abort (fail-safe to Keep); anything else is ignored so
37
+ * stray keys can neither confirm nor abort by accident.
38
+ */
39
+ export declare function exitPromptAction(key: ExitPromptKey): ExitPromptAction;
40
+ /**
41
+ * Show the Keep/Remove list and resolve with the user's choice.
42
+ *
43
+ * Resolves `"keep"`/`"remove"` on Enter, or `undefined` when aborted
44
+ * (Esc/q/Ctrl+C/Ctrl+D/EOF) or when either stream is not a TTY — callers treat
45
+ * `undefined` exactly like `"keep"`: nothing is removed.
46
+ */
47
+ export declare function promptKeepOrRemoveWorktree(opts: {
48
+ input: NodeJS.ReadStream & {
49
+ isTTY?: boolean;
50
+ };
51
+ output: ExitPromptStream;
52
+ label: string;
53
+ }): Promise<WorktreeExitChoice | undefined>;
54
+ //# sourceMappingURL=worktreeExitPrompt.d.ts.map
@@ -0,0 +1,108 @@
1
+ /**
2
+ * The arrow-key Keep/Remove list shown after an interactive `-w` session exits.
3
+ *
4
+ * Claude Code parity: a two-option list navigated with the up/down arrows and
5
+ * confirmed with Enter — not a y/N question. Keep is the default (highlighted
6
+ * first), and every escape hatch — Esc, q, Ctrl+C, EOF, a non-TTY stream —
7
+ * fails safe to Keep (resolves `undefined`, which callers treat as keep).
8
+ *
9
+ * This module owns the raw-TTY interaction only. The gating (which launches
10
+ * may prompt at all) lives in `canPromptWorktreeCleanup` (worktreeArgs.ts),
11
+ * and the git behavior lives in the extension's `removeSessionWorktree`.
12
+ */
13
+ import { emitKeypressEvents } from "node:readline";
14
+ const OPTION_KEEP = "Keep worktree";
15
+ const OPTION_REMOVE = "Remove worktree";
16
+ /**
17
+ * Map a keypress to its prompt action (pure, unit-tested). Arrows (plus
18
+ * k/j/tab) toggle between the two options; Enter or Space confirms; Esc, q,
19
+ * Ctrl+C, and Ctrl+D abort (fail-safe to Keep); anything else is ignored so
20
+ * stray keys can neither confirm nor abort by accident.
21
+ */
22
+ export function exitPromptAction(key) {
23
+ if (key.ctrl && (key.name === "c" || key.name === "d"))
24
+ return { type: "abort" };
25
+ switch (key.name) {
26
+ case "up":
27
+ case "down":
28
+ case "k":
29
+ case "j":
30
+ case "tab":
31
+ return { type: "toggle" };
32
+ case "return":
33
+ case "enter":
34
+ case "space":
35
+ return { type: "confirm" };
36
+ case "escape":
37
+ case "q":
38
+ return { type: "abort" };
39
+ default:
40
+ return { type: "noop" };
41
+ }
42
+ }
43
+ function renderOptions(output, index) {
44
+ const marker = (i) => (i === index ? "❯ " : " ");
45
+ output.write(`\r\x1b[2K${marker(0)}${OPTION_KEEP}\n`);
46
+ output.write(`\r\x1b[2K${marker(1)}${OPTION_REMOVE}\n`);
47
+ }
48
+ /** Erase the two option lines, leaving the cursor just below where they were. */
49
+ function eraseOptions(output) {
50
+ output.write("\x1b[2A\r\x1b[2K\x1b[1B\r\x1b[2K\x1b[1B");
51
+ }
52
+ /**
53
+ * Show the Keep/Remove list and resolve with the user's choice.
54
+ *
55
+ * Resolves `"keep"`/`"remove"` on Enter, or `undefined` when aborted
56
+ * (Esc/q/Ctrl+C/Ctrl+D/EOF) or when either stream is not a TTY — callers treat
57
+ * `undefined` exactly like `"keep"`: nothing is removed.
58
+ */
59
+ export async function promptKeepOrRemoveWorktree(opts) {
60
+ const { input, output, label } = opts;
61
+ // Fail safe when the terminal cannot render an interactive list.
62
+ if (input.isTTY !== true || output.isTTY !== true)
63
+ return undefined;
64
+ let index = 0; // Keep is the default
65
+ output.write(`\n[yagni] ${label}\n`);
66
+ renderOptions(output, index);
67
+ const canRawMode = typeof input.setRawMode === "function";
68
+ const wasRaw = canRawMode ? input.isRaw === true : false;
69
+ if (canRawMode)
70
+ input.setRawMode(true);
71
+ emitKeypressEvents(input);
72
+ input.resume();
73
+ try {
74
+ return await new Promise((resolve) => {
75
+ const cleanup = () => {
76
+ input.removeListener("keypress", onKeypress);
77
+ input.removeListener("end", onEnd);
78
+ };
79
+ const onKeypress = (_str, key) => {
80
+ const action = exitPromptAction(key ?? {});
81
+ if (action.type === "noop")
82
+ return;
83
+ if (action.type === "toggle") {
84
+ index = index === 0 ? 1 : 0;
85
+ eraseOptions(output);
86
+ renderOptions(output, index);
87
+ return;
88
+ }
89
+ cleanup();
90
+ eraseOptions(output);
91
+ resolve(action.type === "confirm" ? (index === 0 ? "keep" : "remove") : undefined);
92
+ };
93
+ // EOF (Ctrl+D is caught above as abort; a closed stdin arrives here).
94
+ const onEnd = () => {
95
+ cleanup();
96
+ resolve(undefined);
97
+ };
98
+ input.on("keypress", onKeypress);
99
+ input.on("end", onEnd);
100
+ });
101
+ }
102
+ finally {
103
+ if (canRawMode)
104
+ input.setRawMode(wasRaw);
105
+ input.pause();
106
+ }
107
+ }
108
+ //# sourceMappingURL=worktreeExitPrompt.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yagni-app/code-staging",
3
- "version": "1.1.0-staging.1329.1",
3
+ "version": "1.1.0-staging.1334.1",
4
4
  "description": "YAGNI Code: a terminal coding agent that already knows your company. One YAGNI login routes the model and grounds the agent in your team's context.",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "author": "YAGNI, Inc. <jack@yagni.app> (https://yagni.app)",
@@ -58,5 +58,5 @@
58
58
  "turndown": "^7.2.4",
59
59
  "typebox": "^1.3.15"
60
60
  },
61
- "yagniSourceSha": "36a0344047224a979e0714451f6e710658d1454b"
61
+ "yagniSourceSha": "b7b91c0bf94f197fc16643cb9a5c5f580e896001"
62
62
  }