pi-cowboy 0.0.0 → 0.3.2

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 (137) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +281 -2
  3. package/agents/explore.toml +40 -0
  4. package/dist/agents/agent-assets.js +261 -0
  5. package/dist/agents/agent-cleanup.js +284 -0
  6. package/dist/agents/agent-discovery.js +108 -0
  7. package/dist/agents/agent-host.js +10 -0
  8. package/dist/agents/agent-manager.js +367 -0
  9. package/dist/agents/agent-reviver.js +64 -0
  10. package/dist/agents/agent-runner.js +288 -0
  11. package/dist/agents/agent-spawn-store.js +57 -0
  12. package/dist/agents/agent-template.js +46 -0
  13. package/dist/agents/agent-types.js +144 -0
  14. package/dist/agents/batch.js +92 -0
  15. package/dist/agents/cleanup-policy.js +194 -0
  16. package/dist/agents/cleanup-report.js +128 -0
  17. package/dist/agents/default-agents.js +19 -0
  18. package/dist/agents/harness/pi-bolt.js +15 -0
  19. package/dist/agents/harness/pi-compatible.js +68 -0
  20. package/dist/agents/harness/pig.js +16 -0
  21. package/dist/agents/harness/registry.js +27 -0
  22. package/dist/agents/harness.js +32 -0
  23. package/dist/agents/result-file-permissions.js +29 -0
  24. package/dist/agents/run-state.js +270 -0
  25. package/dist/agents/schemas/cleanup-batch.schema.js +19 -0
  26. package/dist/agents/schemas/cowboy-agents.schema.js +39 -0
  27. package/dist/agents/schemas/merge-batch.schema.js +25 -0
  28. package/dist/agents/schemas/steer-batch.schema.js +22 -0
  29. package/dist/agents/schemas/stop-batch.schema.js +18 -0
  30. package/dist/agents/spawn-defaults.js +70 -0
  31. package/dist/agents/subagent-session.js +601 -0
  32. package/dist/agents/tool-cleanup.js +57 -0
  33. package/dist/agents/tool-execution.js +125 -0
  34. package/dist/agents/tool-merge.js +190 -0
  35. package/dist/agents/tool-result.js +17 -0
  36. package/dist/agents/tool-steer.js +89 -0
  37. package/dist/agents/types.js +1 -0
  38. package/dist/availability.js +38 -0
  39. package/dist/config/config-io.js +261 -0
  40. package/dist/config/config-store.js +321 -0
  41. package/dist/config/config-validation.js +173 -0
  42. package/dist/config/layered-config.js +117 -0
  43. package/dist/config/types.js +23 -0
  44. package/dist/cow-support-launch.js +43 -0
  45. package/dist/events.js +146 -0
  46. package/dist/extension-toggle.js +28 -0
  47. package/dist/harness-availability-launch.js +23 -0
  48. package/dist/index.js +52 -0
  49. package/dist/infrastructure/exec-path.js +35 -0
  50. package/dist/infrastructure/git/cow-clone.js +398 -0
  51. package/dist/infrastructure/git/git-materializer.js +342 -0
  52. package/dist/infrastructure/git/git-merger.js +234 -0
  53. package/dist/infrastructure/git/git-retention.js +43 -0
  54. package/dist/infrastructure/git/git-runner.js +145 -0
  55. package/dist/infrastructure/git/git-worktree.js +143 -0
  56. package/dist/infrastructure/git/index.js +11 -0
  57. package/dist/infrastructure/git-client.js +11 -0
  58. package/dist/infrastructure/herdr/agent-stopper.js +60 -0
  59. package/dist/infrastructure/herdr/agents.js +329 -0
  60. package/dist/infrastructure/herdr/herdr-client.js +36 -0
  61. package/dist/infrastructure/herdr/herdr-transport.js +158 -0
  62. package/dist/infrastructure/herdr/index.js +15 -0
  63. package/dist/infrastructure/herdr/panes.js +174 -0
  64. package/dist/infrastructure/herdr/polling.js +44 -0
  65. package/dist/infrastructure/herdr/worktrees.js +76 -0
  66. package/dist/infrastructure/herdr-client.js +6 -0
  67. package/dist/infrastructure/herdr-host.js +163 -0
  68. package/dist/logger.js +79 -0
  69. package/dist/models/model-groups.js +72 -0
  70. package/dist/models/model-precedence.js +50 -0
  71. package/dist/models/model-request.js +108 -0
  72. package/dist/orchestrators/briefing-section.js +14 -0
  73. package/dist/orchestrators/context.js +116 -0
  74. package/dist/orchestrators/cues.js +90 -0
  75. package/dist/orchestrators/default-orchestrators.js +36 -0
  76. package/dist/orchestrators/orchestrator-discovery.js +68 -0
  77. package/dist/orchestrators/orchestrator-template.js +43 -0
  78. package/dist/orchestrators/orchestrator-types.js +78 -0
  79. package/dist/orchestrators/protocol.js +292 -0
  80. package/dist/orchestrators/types.js +7 -0
  81. package/dist/paths.js +66 -0
  82. package/dist/pi-settings.js +7 -0
  83. package/dist/predicates.js +25 -0
  84. package/dist/prompt/prompts.js +110 -0
  85. package/dist/prompt/skill-loader.js +153 -0
  86. package/dist/prompt/subagent-system-prompt.js +173 -0
  87. package/dist/registration.js +220 -0
  88. package/dist/shell.js +225 -0
  89. package/dist/spawn/cow-support.js +53 -0
  90. package/dist/spawn/herdr-launcher.js +80 -0
  91. package/dist/spawn/repo-lock.js +34 -0
  92. package/dist/spawn/sandbox.js +286 -0
  93. package/dist/spawn/spawn-coordinator.js +255 -0
  94. package/dist/spawn/spawn-id.js +39 -0
  95. package/dist/spawn/worktree-policy.js +137 -0
  96. package/dist/status-note.js +30 -0
  97. package/dist/subagent/ipc-protocol.js +68 -0
  98. package/dist/subagent/ipc.js +110 -0
  99. package/dist/subagent/pane-supervisor.js +24 -0
  100. package/dist/subagent/socket-ipc.js +240 -0
  101. package/dist/subagent/supervisor.js +315 -0
  102. package/dist/task-registry.js +258 -0
  103. package/dist/templates/template-files.js +84 -0
  104. package/dist/types.js +84 -0
  105. package/dist/ui/action-report.js +21 -0
  106. package/dist/ui/format.js +47 -0
  107. package/dist/ui/indicator.js +480 -0
  108. package/dist/ui/menu/helpers.js +227 -0
  109. package/dist/ui/menu/menu-agent-settings.js +182 -0
  110. package/dist/ui/menu/menu-agent-status.js +444 -0
  111. package/dist/ui/menu/menu-concurrency.js +226 -0
  112. package/dist/ui/menu/menu-model-settings.js +244 -0
  113. package/dist/ui/menu/menu-spawn-defaults.js +71 -0
  114. package/dist/ui/menu/menu-spawn-wizard.js +367 -0
  115. package/dist/ui/menu/menu-spawn-worktree.js +107 -0
  116. package/dist/ui/menu/menu-system-prompt.js +104 -0
  117. package/dist/ui/menu/menu-worktree-command.js +150 -0
  118. package/dist/ui/menu/menus.js +187 -0
  119. package/dist/ui/menu/model-picker.js +160 -0
  120. package/dist/ui/menu/screen-host.js +87 -0
  121. package/dist/ui/menu/submenus/confirm.js +23 -0
  122. package/dist/ui/menu/submenus/model-select.js +42 -0
  123. package/dist/ui/menu/submenus/name-field.js +43 -0
  124. package/dist/ui/menu/submenus/numeric-input.js +61 -0
  125. package/dist/ui/menu/submenus/target-select.js +129 -0
  126. package/dist/ui/menu/submenus/text-editor.js +49 -0
  127. package/dist/ui/menu/submenus/worktree-command-name.js +73 -0
  128. package/dist/ui/menu/submenus/worktree-name.js +53 -0
  129. package/dist/ui/menu/wrappers/settings-list.js +101 -0
  130. package/dist/ui/searchable-select.js +147 -0
  131. package/dist/ui/types.js +1 -0
  132. package/dist/utils.js +70 -0
  133. package/docs/assets/grazing.png +0 -0
  134. package/docs/assets/herdr-containers.png +0 -0
  135. package/docs/assets/logo-transparent.png +0 -0
  136. package/orchestrators/with-code-review.toml +20 -0
  137. package/package.json +109 -3
@@ -0,0 +1,107 @@
1
+ /**
2
+ * menu-spawn-worktree.ts — the spawn wizard's worktree step: the repo's
3
+ * worktrees, the row that picks one, and the name field a new worktree is shown
4
+ * after Spawn.
5
+ *
6
+ * The git questions belong to `src/infrastructure/git/git-worktree.ts`, reached
7
+ * through the `git-client` facade; this module only decides what the wizard
8
+ * offers and how a choice reads. The picker is a submenu, so it renders in place
9
+ * of the options rows rather than opening a screen of its own.
10
+ */
11
+ import * as path from "node:path";
12
+ import { isGitRepo, listGitWorktrees, } from "../../infrastructure/git-client.js";
13
+ import { truncatePath } from "../format.js";
14
+ import { createSearchableSelect } from "./helpers.js";
15
+ import { createWorktreeNameInput } from "./submenus/worktree-name.js";
16
+ import { SettingsListWrapper } from "./wrappers/settings-list.js";
17
+ /** Picker sentinels: not produced by `git worktree list`, so they cannot collide with a path. */
18
+ const INHERIT_WORKTREE_VALUE = "__inherit_worktree__";
19
+ const CREATE_NEW_WORKTREE_VALUE = "__create_new_worktree__";
20
+ /** Real paths are shown by this much of their tail; the row still carries the whole path. */
21
+ const WORKTREE_PATH_TRUNCATE_LEN = 60;
22
+ /** The branch a run in this worktree lands on; a detached checkout is named by its directory. */
23
+ function worktreeBranch(entry) {
24
+ return entry.branch ?? path.basename(entry.path);
25
+ }
26
+ /** The Worktree row's summary for a pick. */
27
+ function pickLabel(pick) {
28
+ switch (pick.kind) {
29
+ case "inherit":
30
+ return "Inherits parent cwd";
31
+ case "picked":
32
+ return pick.branch;
33
+ case "new":
34
+ return "New worktree";
35
+ }
36
+ }
37
+ /** The picker's rows: a new worktree, the parent cwd, then one per existing worktree. */
38
+ function pickerItems(worktrees) {
39
+ return [
40
+ { value: CREATE_NEW_WORKTREE_VALUE, label: "New worktree" },
41
+ { value: INHERIT_WORKTREE_VALUE, label: "Inherits parent cwd" },
42
+ ...worktrees.map((entry) => ({
43
+ value: entry.path,
44
+ label: truncatePath(entry.path, WORKTREE_PATH_TRUNCATE_LEN),
45
+ provider: entry.isDetached ? "detached" : (entry.branch ?? "detached"),
46
+ })),
47
+ ];
48
+ }
49
+ /**
50
+ * The worktrees a spawn can pick from, or null when this session has no repo to
51
+ * offer one (no cwd, or a cwd outside git). A repo whose listing fails still
52
+ * offers the picker, with only the new and inherit choices.
53
+ */
54
+ export async function loadWorktreeChoices(pi, cwd) {
55
+ if (cwd === null)
56
+ return null;
57
+ if (!(await isGitRepo(pi, cwd)))
58
+ return null;
59
+ return (await listGitWorktrees(pi, cwd)) ?? [];
60
+ }
61
+ /** The Worktree row, whose picker lists the repo's checkouts. */
62
+ export function buildWorktreeRow(worktrees, options) {
63
+ return {
64
+ id: "worktree",
65
+ label: "Worktree",
66
+ currentValue: pickLabel(options.current),
67
+ description: "Run in a linked git worktree instead of parent cwd; a new one is named after Spawn",
68
+ submenu: (_value, done) => {
69
+ const record = (pick) => {
70
+ options.onPick(pick);
71
+ done(pickLabel(pick));
72
+ };
73
+ return createSearchableSelect(pickerItems(worktrees), {
74
+ onSelect: (value) => {
75
+ if (value === CREATE_NEW_WORKTREE_VALUE) {
76
+ record({ kind: "new" });
77
+ return;
78
+ }
79
+ if (value === INHERIT_WORKTREE_VALUE) {
80
+ record({ kind: "inherit" });
81
+ return;
82
+ }
83
+ const entry = worktrees.find((wt) => wt.path === value);
84
+ if (entry) {
85
+ record({
86
+ kind: "picked",
87
+ path: entry.path,
88
+ branch: worktreeBranch(entry),
89
+ });
90
+ }
91
+ },
92
+ onCancel: () => done(),
93
+ }, options.theme);
94
+ },
95
+ };
96
+ }
97
+ /**
98
+ * The Worktree Name screen: one screen, resolved with the accepted name or
99
+ * undefined when the user left without one.
100
+ */
101
+ export async function showWorktreeNameStep(host, notify) {
102
+ return await host.open(({ theme, close }) => new SettingsListWrapper(createWorktreeNameInput({
103
+ notify,
104
+ onDone: (name) => close(name),
105
+ onCancel: () => close(),
106
+ }), { title: "Worktree Name", theme, passthroughKeys: true }));
107
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * menu-system-prompt.ts — system prompt mode, AGENTS.md, skills, extensions.
3
+ * Exports: showSystemPromptMenu.
4
+ */
5
+ import fs from "node:fs";
6
+ import path from "node:path";
7
+ import { SettingsList } from "@earendil-works/pi-tui";
8
+ import { getStore } from "../../shell.js";
9
+ import { errorMessage } from "../../utils.js";
10
+ import { customPromptPath } from "../../config/config-io.js";
11
+ import { SettingsListWrapper } from "./wrappers/settings-list.js";
12
+ import { ScreenHost } from "./screen-host.js";
13
+ import { buildSettingsListTheme } from "./helpers.js";
14
+ export async function showSystemPromptMenu(ctx) {
15
+ const store = getStore();
16
+ const buildItems = () => {
17
+ const promptPath = customPromptPath();
18
+ const items = [
19
+ {
20
+ id: "systemPromptMode",
21
+ label: "System prompt mode",
22
+ currentValue: store.agent.systemPromptMode,
23
+ values: ["replace", "inherit", "custom"],
24
+ description: "How the subagent system prompt is built: replace, inherit, or custom.",
25
+ },
26
+ ];
27
+ if (store.agent.systemPromptMode === "custom" &&
28
+ !fs.existsSync(promptPath)) {
29
+ items.push({
30
+ id: "createPromptFile",
31
+ label: "Create prompt file",
32
+ currentValue: promptPath,
33
+ values: ["Create"],
34
+ description: `Create ${promptPath} with a starter template for custom mode.`,
35
+ });
36
+ }
37
+ items.push({
38
+ id: "includeContextFiles",
39
+ label: "Include context files",
40
+ currentValue: store.agent.includeContextFiles ? "ON" : "OFF",
41
+ values: ["ON", "OFF"],
42
+ description: "Load project and ~/.pi/agent AGENTS.md as shared <project_context>.",
43
+ }, {
44
+ id: "loadSkillsImplicitly",
45
+ label: "Load skills implicitly",
46
+ currentValue: store.agent.loadSkillsImplicitly ? "ON" : "OFF",
47
+ values: ["ON", "OFF"],
48
+ description: "Give new agents all skills when an agent template omits the field.",
49
+ }, {
50
+ id: "loadExtensionsImplicitly",
51
+ label: "Load extensions implicitly",
52
+ currentValue: store.agent.loadExtensionsImplicitly ? "ON" : "OFF",
53
+ values: ["ON", "OFF"],
54
+ description: "Give new agents all extensions when an agent template omits the field.",
55
+ });
56
+ return items;
57
+ };
58
+ let items = buildItems();
59
+ let rebuild = null;
60
+ const onChange = (id, newValue) => {
61
+ switch (id) {
62
+ case "systemPromptMode":
63
+ store.mutate.agent.setSystemPromptMode(newValue);
64
+ ctx.ui.notify(`System prompt mode set to ${newValue}`, "info");
65
+ items = buildItems();
66
+ rebuild?.(items);
67
+ break;
68
+ case "createPromptFile":
69
+ try {
70
+ const promptPath = customPromptPath();
71
+ fs.mkdirSync(path.dirname(promptPath), { recursive: true });
72
+ fs.writeFileSync(promptPath, "You are a Pi, an expert coding sub-agent.\nYou have been invoked to handle a specific task autonomously", "utf-8");
73
+ ctx.ui.notify(`Created prompt file: ${promptPath}`, "info");
74
+ }
75
+ catch (err) {
76
+ ctx.ui.notify(`Failed to create prompt file: ${errorMessage(err)}`, "error");
77
+ }
78
+ return;
79
+ case "includeContextFiles":
80
+ store.mutate.agent.setIncludeContextFiles(newValue === "ON");
81
+ ctx.ui.notify(`Include context files set to ${newValue}`, "info");
82
+ break;
83
+ case "loadSkillsImplicitly":
84
+ store.mutate.agent.setLoadSkillsImplicitly(newValue === "ON");
85
+ ctx.ui.notify(`Load skills implicitly set to ${newValue}`, "info");
86
+ break;
87
+ case "loadExtensionsImplicitly":
88
+ store.mutate.agent.setLoadExtensionsImplicitly(newValue === "ON");
89
+ ctx.ui.notify(`Load extensions implicitly set to ${newValue}`, "info");
90
+ break;
91
+ }
92
+ };
93
+ await new ScreenHost(ctx).open(({ theme, close }) => {
94
+ const settingsList = new SettingsList(items, 10, buildSettingsListTheme(theme), onChange, close);
95
+ return new SettingsListWrapper(settingsList, {
96
+ title: "System Prompt",
97
+ theme,
98
+ onCancel: close,
99
+ onRebuild: (r) => {
100
+ rebuild = r;
101
+ },
102
+ });
103
+ });
104
+ }
@@ -0,0 +1,150 @@
1
+ /**
2
+ * menu-worktree-command.ts — the `/cowboy worktree` command: name → checkout → create → report.
3
+ *
4
+ * Creates one git worktree under the configured worktree root and adopts it in
5
+ * herdr with nothing running in it. The name is the branch and the directory at
6
+ * once, the parent is the repository the session sits in, and the base is HEAD;
7
+ * nothing else is asked. Only git knows about the result — there is no spawn
8
+ * record, no entry in `/cowboy status`, and nothing cleans it up. Any failure
9
+ * after the git create, a failed herdr adoption included, removes the worktree
10
+ * and its branch.
11
+ */
12
+ import * as fs from "node:fs";
13
+ import * as path from "node:path";
14
+ import { SelectList } from "@earendil-works/pi-tui";
15
+ import { gitProbe, isGitRepo, resolveMainCheckout, } from "../../infrastructure/git-client.js";
16
+ import { getPiInstance, getRuntime, getStore, getWorktreeMaterialization, } from "../../shell.js";
17
+ import { createAdoptedWorktree, } from "../../spawn/sandbox.js";
18
+ import { VALID_WORKTREE_CHECKOUT_TYPES, parseWorktreeCheckoutType, resolveWorktreeRoot, } from "../../spawn/worktree-policy.js";
19
+ import { errorMessage } from "../../utils.js";
20
+ import { actionReport, buildSelectListTheme } from "./helpers.js";
21
+ import { ScreenHost } from "./screen-host.js";
22
+ import { createWorktreeCommandNameInput } from "./submenus/worktree-command-name.js";
23
+ import { SettingsListWrapper } from "./wrappers/settings-list.js";
24
+ /** What each checkout policy means for the worktree this command is about to create. */
25
+ const CHECKOUT_DESCRIPTIONS = {
26
+ dirty: "Start from the parent's uncommitted work: copy-on-write clones its edits and untracked files, checkout applies only its tracked changes.",
27
+ clean: "Start at HEAD: the parent's uncommitted work stays out of the worktree.",
28
+ };
29
+ /** How the created worktree's tree was actually populated. */
30
+ function materializationLabel(outcome) {
31
+ switch (outcome.kind) {
32
+ case "cow":
33
+ return "copy-on-write";
34
+ case "cow-fallback":
35
+ return "checkout (the volume cannot clone, so copy-on-write was unavailable)";
36
+ case "checkout":
37
+ return "checkout";
38
+ }
39
+ }
40
+ /** What the created worktree took from the parent working tree. */
41
+ function inheritedLabel(outcome, dirtyCheckout) {
42
+ switch (outcome.kind) {
43
+ case "cow":
44
+ if (outcome.clone.mode === "seeded") {
45
+ return "nothing the parent had uncommitted: tracked files came from HEAD and only ignored state was seeded";
46
+ }
47
+ return outcome.clone.reason !== undefined
48
+ ? "the parent's uncommitted work: its tracked edits and its untracked files"
49
+ : "nothing uncommitted — the parent was clean, so the worktree is a clone at HEAD";
50
+ case "cow-fallback":
51
+ return "nothing: the fallback checkout starts at HEAD, whatever the checkout policy says";
52
+ case "checkout":
53
+ return dirtyCheckout === "dirty"
54
+ ? "the parent's tracked changes, applied on top of git's checkout of HEAD (a clean parent carries none)"
55
+ : "nothing: git checked out HEAD, and the parent's uncommitted work stays out";
56
+ }
57
+ }
58
+ /** The one message a created worktree is reported with. */
59
+ function describeCreatedWorktree(created, dirtyCheckout) {
60
+ return [
61
+ `Worktree ready: ${created.path}`,
62
+ `branch: ${created.branch}`,
63
+ `materialization: ${materializationLabel(created.materialization)}`,
64
+ `inherited: ${inheritedLabel(created.materialization, dirtyCheckout)}`,
65
+ ].join("\n");
66
+ }
67
+ /** Why the name is already taken, or undefined when it is free. */
68
+ async function describeCollision(pi, repoCwd, branch, targetPath) {
69
+ if (fs.existsSync(targetPath)) {
70
+ return `Cannot create the worktree: ${targetPath} already exists. Pick another name.`;
71
+ }
72
+ const existingBranch = await gitProbe(pi, ["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`], repoCwd);
73
+ if (existingBranch !== undefined) {
74
+ return `Cannot create the worktree: the branch "${branch}" already exists. Pick another name.`;
75
+ }
76
+ return undefined;
77
+ }
78
+ /**
79
+ * The `/cowboy worktree` flow. `inlineName` is the name given on the command
80
+ * line, prefilled into the field; a bare command opens it empty.
81
+ */
82
+ export async function showWorktreeCommandMenu(ctx, inlineName) {
83
+ const host = new ScreenHost(ctx);
84
+ const notify = (message, kind) => ctx.ui.notify(message, kind);
85
+ const name = await host.open(({ theme, close }) => new SettingsListWrapper(createWorktreeCommandNameInput({
86
+ prefill: inlineName,
87
+ notify,
88
+ onDone: (accepted) => close(accepted),
89
+ onCancel: () => close(),
90
+ }), { title: "Worktree Name", theme, passthroughKeys: true }));
91
+ if (name === undefined)
92
+ return;
93
+ const store = getStore();
94
+ const dirtyCheckout = await host.open(({ theme, close }) => {
95
+ const list = new SelectList(VALID_WORKTREE_CHECKOUT_TYPES.map((value) => ({
96
+ value,
97
+ label: value,
98
+ description: CHECKOUT_DESCRIPTIONS[value],
99
+ })), VALID_WORKTREE_CHECKOUT_TYPES.length, buildSelectListTheme(theme));
100
+ // The configured policy leads; it is what a spawn would use right now.
101
+ list.setSelectedIndex(VALID_WORKTREE_CHECKOUT_TYPES.indexOf(store.agent.worktreeCheckoutType));
102
+ list.onSelect = (item) => close(parseWorktreeCheckoutType(item.value));
103
+ list.onCancel = () => close();
104
+ return new SettingsListWrapper(list, {
105
+ title: "Worktree Checkout",
106
+ theme,
107
+ onCancel: () => close(),
108
+ });
109
+ });
110
+ if (dirtyCheckout === undefined)
111
+ return;
112
+ const pi = getPiInstance();
113
+ if (!(await isGitRepo(pi, ctx.cwd))) {
114
+ notify("Cannot create a worktree: this session is not inside a git repository.", "error");
115
+ return;
116
+ }
117
+ let repoRoot;
118
+ try {
119
+ repoRoot = await resolveMainCheckout(pi, ctx.cwd);
120
+ }
121
+ catch (err) {
122
+ notify(`Cannot create a worktree: ${errorMessage(err)}`, "error");
123
+ return;
124
+ }
125
+ const targetPath = path.join(resolveWorktreeRoot(store.agent.worktreeRoot, repoRoot), name);
126
+ const collision = await describeCollision(pi, repoRoot, name, targetPath);
127
+ if (collision !== undefined) {
128
+ notify(collision, "error");
129
+ return;
130
+ }
131
+ const report = actionReport(ctx.ui);
132
+ report.pending(`Creating worktree ${name}`);
133
+ let created;
134
+ try {
135
+ created = await createAdoptedWorktree(pi, {
136
+ repoCwd: repoRoot,
137
+ path: targetPath,
138
+ branch: name,
139
+ materialization: getWorktreeMaterialization(),
140
+ dirtyCheckout,
141
+ notify,
142
+ host: getRuntime().host,
143
+ });
144
+ }
145
+ catch (err) {
146
+ report.failed("Worktree creation failed", err);
147
+ return;
148
+ }
149
+ report.succeeded(describeCreatedWorktree(created, dirtyCheckout));
150
+ }
@@ -0,0 +1,187 @@
1
+ /**
2
+ * menus.ts — /cowboy command dispatcher: main menu and settings menu.
3
+ * Picker rows host their submenu in place; the status/spawn/settings rows
4
+ * close this list and hand off to a full-screen flow (cf. buildAgentActionsList),
5
+ * which showAgentsActionMenu also opens for a `/cowboy status|spawn` subcommand.
6
+ * The Enabled switch closes the main menu, and while the extension is off it is
7
+ * the whole of it.
8
+ * Exports: showAgentsMainMenu, showAgentsActionMenu, showSettingsMenu,
9
+ * showSpawnAgentMenu (re-export).
10
+ */
11
+ import { SettingsList } from "@earendil-works/pi-tui";
12
+ import { getStore } from "../../shell.js";
13
+ import { setExtensionEnabled } from "../../registration.js";
14
+ import { setShowActiveIndicator } from "../../extension-toggle.js";
15
+ import { getPiDefaultThinkingLevel } from "../../pi-settings.js";
16
+ import { buildSettingsListTheme } from "./helpers.js";
17
+ import { CLOSED_SUBMENU, ScreenHost } from "./screen-host.js";
18
+ import { SettingsListWrapper } from "./wrappers/settings-list.js";
19
+ import { buildModelOverridesRow } from "./menu-model-settings.js";
20
+ import { showConcurrencySettingsMenu } from "./menu-concurrency.js";
21
+ import { showAgentStatusMenu } from "./menu-agent-status.js";
22
+ import { showSpawnOptionsMenu } from "./menu-agent-settings.js";
23
+ import { buildDefaultAgentTypeRow, buildDefaultOrchestratorRow, } from "./menu-spawn-defaults.js";
24
+ import { showSystemPromptMenu } from "./menu-system-prompt.js";
25
+ import { showSpawnAgentMenu } from "./menu-spawn-wizard.js";
26
+ export { showSpawnAgentMenu };
27
+ /**
28
+ * Settings list: the screens, then the indicator switches. Resolves with the
29
+ * flow to open, or undefined on cancel.
30
+ */
31
+ async function showSettingsList(ctx) {
32
+ return await new ScreenHost(ctx).open(({ theme, close }) => {
33
+ const handOff = (action) => close(action);
34
+ const store = getStore();
35
+ const shown = store.agent.showActiveIndicator;
36
+ const grazing = store.agent.grazingEnabled;
37
+ const items = [
38
+ handOffRow("concurrency", "Concurrency Settings", "Set per-model slot limits", handOff),
39
+ handOffRow("spawnoptions", "Agent behavior", "spawn defaults, results delivery, output, tools, worktrees", handOff),
40
+ handOffRow("systemprompt", "System prompt", "Prompt mode, AGENTS.md, skills, extensions", handOff),
41
+ {
42
+ id: "showActiveIndicator",
43
+ label: "Show cowboy 🤠",
44
+ currentValue: shown ? "ON" : "OFF",
45
+ values: ["ON", "OFF"],
46
+ },
47
+ {
48
+ id: "grazingEnabled",
49
+ label: "Grazing 🐄",
50
+ currentValue: grazing ? "ON" : "OFF",
51
+ values: ["ON", "OFF"],
52
+ },
53
+ ];
54
+ const settingsList = new SettingsList(items, 10, buildSettingsListTheme(theme), (id, value) => {
55
+ if (id === "showActiveIndicator") {
56
+ setShowActiveIndicator(ctx.ui, value === "ON");
57
+ }
58
+ else if (id === "grazingEnabled") {
59
+ store.mutate.agent.setGrazingEnabled(value === "ON");
60
+ }
61
+ }, close);
62
+ return new SettingsListWrapper(settingsList, {
63
+ title: "Settings",
64
+ theme,
65
+ onCancel: close,
66
+ });
67
+ });
68
+ }
69
+ export async function showSettingsMenu(ctx, modelOptions) {
70
+ while (true) {
71
+ const action = await showSettingsList(ctx);
72
+ if (action === undefined)
73
+ return;
74
+ switch (action) {
75
+ case "concurrency":
76
+ await showConcurrencySettingsMenu(ctx, modelOptions);
77
+ break;
78
+ case "spawnoptions":
79
+ await showSpawnOptionsMenu(ctx);
80
+ break;
81
+ case "systemprompt":
82
+ await showSystemPromptMenu(ctx);
83
+ break;
84
+ }
85
+ }
86
+ }
87
+ /**
88
+ * Opens the flow a topmost row hands off to, and the one a `/cowboy status` or
89
+ * `/cowboy spawn` subcommand reaches directly.
90
+ */
91
+ export async function showAgentsActionMenu(ctx, action, modelOptions) {
92
+ switch (action) {
93
+ case "status":
94
+ await showAgentStatusMenu(ctx);
95
+ break;
96
+ case "spawn":
97
+ await showSpawnAgentMenu(ctx, modelOptions);
98
+ break;
99
+ case "settings":
100
+ await showSettingsMenu(ctx, modelOptions);
101
+ break;
102
+ }
103
+ }
104
+ /** Row that resolves the list with `handOff` and renders nothing (CLOSED_SUBMENU). */
105
+ function handOffRow(action, label, description, handOff) {
106
+ return {
107
+ id: action,
108
+ label,
109
+ description,
110
+ currentValue: "",
111
+ submenu: () => {
112
+ handOff(action);
113
+ return CLOSED_SUBMENU;
114
+ },
115
+ };
116
+ }
117
+ /** Topmost menu. Resolves with the action to run, or undefined on cancel. */
118
+ async function showAgentsMainList(ctx, modelOptions) {
119
+ // Resolved once here because the model screen's submenu factory is synchronous.
120
+ const piDefaultThinking = await getPiDefaultThinkingLevel(ctx.cwd);
121
+ let rebuild;
122
+ return await new ScreenHost(ctx).open(({ theme, close }) => {
123
+ const handOff = (action) => close(action);
124
+ const buildItems = () => {
125
+ const store = getStore();
126
+ const notify = (message, type) => ctx.ui.notify(message, type);
127
+ const enabled = store.agent.extensionEnabled;
128
+ const switchRow = {
129
+ id: "extensionEnabled",
130
+ label: "Enabled",
131
+ currentValue: enabled ? "ON" : "OFF",
132
+ values: ["ON", "OFF"],
133
+ description: enabled
134
+ ? "Unload the cowboy tools and stop subagent-result notifications. Persisted; active agents keep working."
135
+ : "cowboy tools are unloaded and the rest of this menu is hidden. Active agents keep working.",
136
+ };
137
+ // Off, the switch is the whole menu: the other rows configure an
138
+ // extension that is not running.
139
+ if (!enabled)
140
+ return [switchRow];
141
+ return [
142
+ buildModelOverridesRow({
143
+ ctx,
144
+ store,
145
+ theme,
146
+ modelOptions,
147
+ piDefaultThinking,
148
+ }),
149
+ buildDefaultOrchestratorRow({ store, theme, notify }),
150
+ buildDefaultAgentTypeRow({ store, theme, notify }),
151
+ handOffRow("status", "Status", "List spawned, queued and settled agents", handOff),
152
+ handOffRow("spawn", "Spawn agent", "Manually spawn a new agent", handOff),
153
+ handOffRow("settings", "Settings", "Concurrency, agent behavior, and system prompt settings", handOff),
154
+ switchRow,
155
+ ];
156
+ };
157
+ const settingsList = new SettingsList(buildItems(), 10, buildSettingsListTheme(theme), (id, value) => {
158
+ const flipped = id === "extensionEnabled";
159
+ if (flipped) {
160
+ const enabled = value === "ON";
161
+ setExtensionEnabled(ctx.ui, enabled);
162
+ ctx.ui.notify(enabled ? "pi-cowboy enabled" : "pi-cowboy disabled", "info");
163
+ }
164
+ // The row's handler already persisted, so the rebuild repaints the
165
+ // values from the store — and after a flip, the rows it governs. The
166
+ // switch sits at row 6 enabled and row 0 collapsed, so the flip asks
167
+ // the rebuild to hold the cursor on it.
168
+ rebuild?.(buildItems(), flipped ? "extensionEnabled" : undefined);
169
+ }, close);
170
+ return new SettingsListWrapper(settingsList, {
171
+ title: "Agents",
172
+ theme,
173
+ onCancel: close,
174
+ onRebuild: (r) => {
175
+ rebuild = r;
176
+ },
177
+ });
178
+ });
179
+ }
180
+ export async function showAgentsMainMenu(ctx, modelOptions) {
181
+ while (true) {
182
+ const action = await showAgentsMainList(ctx, modelOptions);
183
+ if (action === undefined)
184
+ return;
185
+ await showAgentsActionMenu(ctx, action, modelOptions);
186
+ }
187
+ }