pi-better-server 0.6.0 → 0.7.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.
Files changed (142) hide show
  1. package/package.json +4 -3
  2. package/scripts/measure-full-runtime/frontend.ts +10 -2
  3. package/scripts/measure-full-runtime.mjs +2 -0
  4. package/src/codex/return.ts +3 -2
  5. package/src/detach/extension.ts +20 -3
  6. package/src/event-log.ts +40 -12
  7. package/src/hosted-session.ts +11 -5
  8. package/src/remote-client/packages.ts +1 -4
  9. package/src/runtime/background-tools.ts +54 -8
  10. package/src/runtime/background.ts +16 -4
  11. package/src/runtime/domain/background/process.mjs +49 -11
  12. package/src/runtime/domain/background/supervisor/store.mjs +3 -3
  13. package/src/runtime/domain/background/supervisor/supervisor.mjs +9 -3
  14. package/src/runtime/domain/background-service/index.domain.mjs +19 -0
  15. package/src/runtime/domain/background-service/index.ts +19 -0
  16. package/src/runtime/domain/background-service/managed-frontend.domain.mjs +87 -5
  17. package/src/runtime/domain/background-service/managed-frontend.ts +76 -5
  18. package/src/runtime/domain/background-service/navigator-provider.domain.mjs +5 -0
  19. package/src/runtime/domain/background-service/navigator-provider.ts +4 -0
  20. package/src/runtime/domain/background-service/observed-waits.domain.mjs +203 -31
  21. package/src/runtime/domain/background-service/observed-waits.ts +168 -24
  22. package/src/runtime/domain/background-service/process.domain.mjs +49 -11
  23. package/src/runtime/domain/background-service/process.ts +45 -11
  24. package/src/runtime/domain/background-service/review-settings.domain.mjs +16 -0
  25. package/src/runtime/domain/background-service/review-settings.ts +18 -0
  26. package/src/runtime/domain/background-service/runtime.domain.mjs +8 -70
  27. package/src/runtime/domain/background-service/runtime.ts +8 -73
  28. package/src/runtime/domain/background-service/shared-harness-settings.domain.mjs +2 -3
  29. package/src/runtime/domain/background-service/shared-harness-settings.ts +2 -5
  30. package/src/runtime/domain/background-service/shared-navigator.domain.mjs +55 -28
  31. package/src/runtime/domain/background-service/shared-navigator.ts +47 -26
  32. package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
  33. package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.ts +9 -0
  34. package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +5 -1
  35. package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +6 -0
  36. package/src/runtime/domain/background-service/shared-ssh-core/index.domain.mjs +1 -1
  37. package/src/runtime/domain/background-service/shared-ssh-core/index.ts +1 -1
  38. package/src/runtime/domain/background-service/stall.domain.mjs +5 -3
  39. package/src/runtime/domain/background-service/stall.ts +7 -4
  40. package/src/runtime/domain/background-service/supervised-progress.domain.mjs +42 -0
  41. package/src/runtime/domain/background-service/supervised-progress.ts +43 -0
  42. package/src/runtime/domain/background-service/supervisor/README.md +1 -4
  43. package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +1 -0
  44. package/src/runtime/domain/background-service/supervisor/generated/process.mjs +49 -11
  45. package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +47 -4
  46. package/src/runtime/domain/background-service/supervisor/generated/store.mjs +3 -3
  47. package/src/runtime/domain/background-service/supervisor/generated/watch-fingerprint.mjs +69 -0
  48. package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +47 -4
  49. package/src/runtime/domain/background-service/supervisor/runner.ts +38 -3
  50. package/src/runtime/domain/background-service/supervisor/store.domain.mjs +3 -3
  51. package/src/runtime/domain/background-service/supervisor/store.ts +3 -3
  52. package/src/runtime/domain/background-service/supervisor/supervisor.domain.mjs +9 -3
  53. package/src/runtime/domain/background-service/supervisor/supervisor.ts +6 -3
  54. package/src/runtime/domain/background-service/tools.domain.mjs +22 -16
  55. package/src/runtime/domain/background-service/tools.ts +26 -16
  56. package/src/runtime/domain/background-service/types.ts +4 -2
  57. package/src/runtime/domain/background-service/watch-fingerprint.domain.mjs +67 -0
  58. package/src/runtime/domain/background-service/watch-fingerprint.ts +70 -0
  59. package/src/runtime/domain/catalog/catalog-preferences.mjs +1 -3
  60. package/src/runtime/domain/coordinator/src/outbox.mjs +7 -0
  61. package/src/runtime/domain/coordinator/src/store.mjs +5 -1
  62. package/src/runtime/domain/log-paths.mjs +8 -1
  63. package/src/runtime/domain/parse.mjs +9 -5
  64. package/src/runtime/domain/sandbox-policy/README.md +3 -3
  65. package/src/runtime/domain/sandbox-policy/package.json +1 -1
  66. package/src/runtime/domain/sandbox-policy/shared-harness-settings.domain.mjs +2 -3
  67. package/src/runtime/domain/sandbox-policy/shared-harness-settings.ts +2 -5
  68. package/src/runtime/domain/session-store.mjs +7 -0
  69. package/src/runtime/domain/shared-harness-settings.mjs +2 -3
  70. package/src/runtime/domain/subagents/README.md +12 -10
  71. package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +2 -5
  72. package/src/runtime/domain/subagents/assignment-outcomes.ts +2 -5
  73. package/src/runtime/domain/subagents/assignment-report.domain.mjs +22 -0
  74. package/src/runtime/domain/subagents/assignment-report.ts +20 -0
  75. package/src/runtime/domain/subagents/batch.mjs +13 -5
  76. package/src/runtime/domain/subagents/catalog-preferences.domain.mjs +1 -3
  77. package/src/runtime/domain/subagents/catalog-preferences.ts +1 -5
  78. package/src/runtime/domain/subagents/cleanup.domain.mjs +11 -2
  79. package/src/runtime/domain/subagents/cleanup.ts +10 -2
  80. package/src/runtime/domain/subagents/config.domain.mjs +5 -0
  81. package/src/runtime/domain/subagents/config.ts +8 -2
  82. package/src/runtime/domain/subagents/continuation.domain.mjs +4 -2
  83. package/src/runtime/domain/subagents/continuation.ts +4 -2
  84. package/src/runtime/domain/subagents/extensions.mjs +1 -0
  85. package/src/runtime/domain/subagents/failures.domain.mjs +11 -2
  86. package/src/runtime/domain/subagents/failures.ts +10 -2
  87. package/src/runtime/domain/subagents/finalization.domain.mjs +39 -32
  88. package/src/runtime/domain/subagents/finalization.ts +37 -30
  89. package/src/runtime/domain/subagents/git-workspace.domain.mjs +51 -6
  90. package/src/runtime/domain/subagents/git-workspace.ts +48 -6
  91. package/src/runtime/domain/subagents/index.domain.mjs +208 -75
  92. package/src/runtime/domain/subagents/index.ts +213 -75
  93. package/src/runtime/domain/subagents/isolated-workspace.domain.mjs +508 -0
  94. package/src/runtime/domain/subagents/isolated-workspace.ts +551 -0
  95. package/src/runtime/domain/subagents/list.mjs +2 -2
  96. package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +22 -5
  97. package/src/runtime/domain/subagents/managed-launch-client.ts +23 -6
  98. package/src/runtime/domain/subagents/output-payload.domain.mjs +24 -9
  99. package/src/runtime/domain/subagents/output-payload.ts +26 -12
  100. package/src/runtime/domain/subagents/package.json +4 -2
  101. package/src/runtime/domain/subagents/parse.domain.mjs +9 -5
  102. package/src/runtime/domain/subagents/parse.ts +10 -5
  103. package/src/runtime/domain/subagents/registry.domain.mjs +31 -25
  104. package/src/runtime/domain/subagents/registry.ts +31 -13
  105. package/src/runtime/domain/subagents/retained-result.domain.mjs +56 -0
  106. package/src/runtime/domain/subagents/retained-result.ts +65 -0
  107. package/src/runtime/domain/subagents/session-selection.domain.mjs +3 -1
  108. package/src/runtime/domain/subagents/session-selection.ts +2 -1
  109. package/src/runtime/domain/subagents/session-store.domain.mjs +7 -0
  110. package/src/runtime/domain/subagents/session-store.ts +9 -0
  111. package/src/runtime/domain/subagents/settings-page.domain.mjs +151 -107
  112. package/src/runtime/domain/subagents/settings-page.ts +147 -108
  113. package/src/runtime/domain/subagents/shared-catalog.domain.mjs +23 -7
  114. package/src/runtime/domain/subagents/shared-catalog.ts +23 -7
  115. package/src/runtime/domain/subagents/shared-harness-settings.domain.mjs +2 -3
  116. package/src/runtime/domain/subagents/shared-harness-settings.ts +2 -5
  117. package/src/runtime/domain/subagents/shared-navigator.domain.mjs +55 -28
  118. package/src/runtime/domain/subagents/shared-navigator.ts +47 -26
  119. package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
  120. package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.ts +9 -0
  121. package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +5 -1
  122. package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +6 -0
  123. package/src/runtime/domain/subagents/task-guard.domain.mjs +14 -1
  124. package/src/runtime/domain/subagents/task-guard.ts +13 -1
  125. package/src/runtime/domain/subagents/task-policy.domain.mjs +2 -1
  126. package/src/runtime/domain/subagents/task-policy.ts +5 -1
  127. package/src/runtime/domain/subagents/tools.domain.mjs +4 -8
  128. package/src/runtime/domain/subagents/tools.ts +4 -7
  129. package/src/runtime/domain/subagents/work-observation.domain.mjs +25 -1
  130. package/src/runtime/domain/subagents/work-observation.ts +20 -1
  131. package/src/runtime/domain/subagents/workspace-files.domain.mjs +103 -0
  132. package/src/runtime/domain/subagents/workspace-files.ts +114 -0
  133. package/src/runtime/domain/subagents/workspace-prepare-worker.mjs +12 -0
  134. package/src/runtime/domain/subagents/workspace-scope.domain.d.mts +38 -0
  135. package/src/runtime/domain/subagents/workspace-scope.domain.mjs +5 -0
  136. package/src/runtime/domain/subagents/workspace-scope.ts +6 -0
  137. package/src/runtime/domain/subagents/worktree-feedback.domain.mjs +55 -0
  138. package/src/runtime/domain/subagents/worktree-feedback.ts +77 -0
  139. package/src/runtime/read-model.ts +4 -2
  140. package/src/runtime/root.ts +5 -2
  141. package/src/shared-ssh-core/index.ts +1 -1
  142. package/src/version.ts +1 -1
@@ -3,13 +3,36 @@ import * as Tui from "@earendil-works/pi-tui";
3
3
  import { isConcurrencyCap } from "./config.domain.mjs";
4
4
  import { DELEGATION_MODES } from "./delegation.domain.mjs";
5
5
  import { createSkillPicker } from "./skill-picker.domain.mjs";
6
- import { validatedTimingMinutes } from "./timing.domain.mjs";
6
+ import { isTimingMinutes, MAX_TIMING_MINUTES, validatedTimingMinutes } from "./timing.domain.mjs";
7
+ const rows = [
8
+ { kind: "mode", label: "Delegation mode" },
9
+ { kind: "cap", label: "Concurrent subagents" },
10
+ { kind: "workspace", label: "Git workspace" },
11
+ { kind: "timeout", label: "Run timeout" },
12
+ { kind: "toolWait", label: "Tool-wait alert" },
13
+ { kind: "stuck", label: "No-progress alert" },
14
+ { kind: "skills", label: "Skill exposure" },
15
+ { kind: "choose", label: "Selected skills" },
16
+ { kind: "agents", label: "Agents" },
17
+ { kind: "reset", label: "Reset to defaults" },
18
+ ];
19
+ const labelWidth = Math.max(...rows.map(({ label }) => Tui.visibleWidth(label)));
7
20
  const SKILL_MODES = [
8
21
  { mode: "inherit", label: "Discover automatically" },
9
22
  { mode: "none", label: "No skill catalog" },
10
23
  { mode: "explicit", label: "Selected skills" },
11
24
  ];
12
- const LABEL_WIDTH = 26;
25
+ const NO_LIMIT = "No limit";
26
+ function parseRunTimeout(input) {
27
+ const text = input.trim();
28
+ if (text.toLowerCase() === NO_LIMIT.toLowerCase())
29
+ return 0;
30
+ const minutes = Number(text);
31
+ if (!text || !isTimingMinutes(minutes)) {
32
+ throw new Error(`Enter No limit or a duration up to ${MAX_TIMING_MINUTES} minutes.`);
33
+ }
34
+ return minutes;
35
+ }
13
36
  function skillLabel(mode) {
14
37
  return SKILL_MODES.find((entry) => entry.mode === mode).label;
15
38
  }
@@ -17,17 +40,67 @@ function cycleSkillMode(mode) {
17
40
  return mode === "inherit" ? "none" : "inherit";
18
41
  }
19
42
  function skillSource(settings) {
20
- if (settings.skillsError)
21
- return `Config error: ${settings.skillsError}`;
22
43
  if (settings.skillsSource === "session")
23
44
  return "Source: this session (unsaved)";
24
45
  if (settings.skillsSource === "config")
25
46
  return "Source: config defaultSkills";
26
47
  return "Source: discovery default";
27
48
  }
28
- function cell(text, width) {
29
- const clipped = Tui.truncateToWidth(text, width, "");
30
- return clipped + " ".repeat(Math.max(0, width - Tui.visibleWidth(clipped)));
49
+ function settingValue(kind, s) {
50
+ switch (kind) {
51
+ case "mode": return s.mode;
52
+ case "cap": return String(s.maxConcurrent);
53
+ case "workspace": return s.gitWorkspace === "worktree" ? "Worktree" : "Clone";
54
+ case "timeout": return s.maxMinutes === 0 ? NO_LIMIT : `${s.maxMinutes} min`;
55
+ case "toolWait": return s.toolWaitMinutes === 0 ? "Off" : `${s.toolWaitMinutes} min`;
56
+ case "stuck": return s.stuckMinutes === 0 ? "Off" : `${s.stuckMinutes} min`;
57
+ case "skills": return skillLabel(s.skillsMode);
58
+ case "choose": return s.skillsMode === "explicit" ? `${s.skillsPaths.length} checked` : "Choose…";
59
+ case "agents": return "Open";
60
+ case "reset": return "Restore";
61
+ }
62
+ }
63
+ function description(kind, s) {
64
+ switch (kind) {
65
+ case "mode": return {
66
+ manual: "Delegate when you ask or when the active workflow requires it.",
67
+ adaptive: "Delegate independent work when a suitable agent is available.",
68
+ coordinator: "Delegate work covered by an agent role. Keep coordination and verification in the foreground.",
69
+ }[s.mode];
70
+ case "cap": return "Set how many subagents can run at the same time.";
71
+ case "workspace": return s.gitWorkspace === "worktree"
72
+ ? "Create a linked worktree that shares the repository’s Git metadata."
73
+ : "Create a separate local clone for each isolated Git workspace.";
74
+ case "timeout": return "Stop a subagent when its run reaches this limit. Choose No limit to disable the timeout.";
75
+ case "toolWait": return "Notify the parent when a tool call stays open this long. Enter 0 to turn this alert off.";
76
+ case "stuck": return "Notify the parent after this long without progress. Time spent waiting for a tool is excluded. Enter 0 to turn this alert off.";
77
+ case "skills": return "Choose how new subagents discover skills.\n" + skillSource(s);
78
+ case "choose": return "Choose the skills to load for new subagents.";
79
+ case "agents": return "Browse agent roles and edit their profiles in /agents.";
80
+ case "reset": return "Restore saved defaults for this session, including its workspace, limits, and skills.";
81
+ }
82
+ }
83
+ function primaryKey(action, excludeSpace = false) {
84
+ const key = Tui.getKeybindings().getKeys(action).find(key => !excludeSpace || key !== "space");
85
+ if (!key)
86
+ return undefined;
87
+ if (key === "escape")
88
+ return "Esc";
89
+ return key.split("+").map(part => part.length === 1 ? part : part[0].toUpperCase() + part.slice(1)).join("+");
90
+ }
91
+ function shortcuts(kind, editing) {
92
+ const cancel = primaryKey("tui.select.cancel");
93
+ const confirm = primaryKey(editing ? "tui.input.submit" : "tui.select.confirm", !editing);
94
+ const change = confirm ? `${confirm}/Space` : "Space";
95
+ const verb = kind === "agents" || kind === "choose" ? "open" : kind === "reset" ? "restore" : "change";
96
+ const actions = editing ? [confirm && `${confirm} to apply`]
97
+ : kind === "timeout" ? ["Space for No limit", confirm && `${confirm} to edit minutes`]
98
+ : [`${change} to ${verb}`];
99
+ if (!editing)
100
+ actions.push("Ctrl+S to save defaults");
101
+ if (cancel)
102
+ actions.push(`${cancel} to ${editing ? "cancel" : "go back"}`);
103
+ return actions.filter(Boolean).join(" · ");
31
104
  }
32
105
  export function createSubagentSettingsPage(theme, handlers, requestRender, close, getHeight = () => process.stdout.rows ?? 30) {
33
106
  const input = new Tui.Input();
@@ -44,14 +117,6 @@ export function createSubagentSettingsPage(theme, handlers, requestRender, close
44
117
  error = failed;
45
118
  requestRender();
46
119
  }
47
- function rowsOf(settings) {
48
- const rows = [
49
- { kind: "mode" }, { kind: "cap" }, { kind: "toolWait" }, { kind: "stuck" }, { kind: "timeout" },
50
- { kind: "skills" }, { kind: "choose" },
51
- ];
52
- rows.push({ kind: "agents" }, { kind: "reset" });
53
- return rows;
54
- }
55
120
  function startEditing(target, value) {
56
121
  editing = target;
57
122
  input.setValue(value);
@@ -99,9 +164,13 @@ export function createSubagentSettingsPage(theme, handlers, requestRender, close
99
164
  picker.focused = focused;
100
165
  requestRender();
101
166
  }
102
- function action(settings, selected) {
167
+ function action(settings, selected, key) {
103
168
  try {
104
- if (selected.kind === "mode") {
169
+ if (selected.kind === "workspace") {
170
+ handlers.changeWorkspace(settings.gitWorkspace === "worktree" ? "clone" : "worktree");
171
+ report("Session workspace updated.");
172
+ }
173
+ else if (selected.kind === "mode") {
105
174
  const next = DELEGATION_MODES[(DELEGATION_MODES.indexOf(settings.mode) + 1) % DELEGATION_MODES.length];
106
175
  handlers.changeMode(next);
107
176
  report("Session mode updated.");
@@ -116,7 +185,13 @@ export function createSubagentSettingsPage(theme, handlers, requestRender, close
116
185
  startEditing({ kind: "stuck" }, String(settings.stuckMinutes));
117
186
  }
118
187
  else if (selected.kind === "timeout") {
119
- startEditing({ kind: "timeout" }, String(settings.maxMinutes));
188
+ if (key === " ") {
189
+ handlers.changeTiming("maxMinutes", 0);
190
+ report("Run timeout set to No limit.");
191
+ }
192
+ else {
193
+ startEditing(selected, settings.maxMinutes === 0 ? NO_LIMIT : String(settings.maxMinutes));
194
+ }
120
195
  }
121
196
  else if (selected.kind === "skills") {
122
197
  if (settings.skillsMode === "explicit")
@@ -146,7 +221,6 @@ export function createSubagentSettingsPage(theme, handlers, requestRender, close
146
221
  const target = editing;
147
222
  if (!target)
148
223
  return;
149
- const settings = handlers.get();
150
224
  if (target.kind === "cap") {
151
225
  const cap = /^\d+$/.test(value.trim()) ? Number(value.trim()) : NaN;
152
226
  if (!isConcurrencyCap(cap)) {
@@ -165,7 +239,7 @@ export function createSubagentSettingsPage(theme, handlers, requestRender, close
165
239
  const key = target.kind === "toolWait" ? "toolWaitMinutes"
166
240
  : target.kind === "stuck" ? "stuckMinutes" : "maxMinutes";
167
241
  try {
168
- const minutes = validatedTimingMinutes(value.trim(), key);
242
+ const minutes = target.kind === "timeout" ? parseRunTimeout(value) : validatedTimingMinutes(value.trim(), key);
169
243
  handlers.changeTiming(key, minutes);
170
244
  finishEditing(target.kind === "toolWait" ? "Tool-wait alert updated."
171
245
  : target.kind === "stuck" ? "No-progress alert updated." : "Timeout updated.");
@@ -201,17 +275,16 @@ export function createSubagentSettingsPage(theme, handlers, requestRender, close
201
275
  requestRender();
202
276
  return;
203
277
  }
204
- const rows = rowsOf(settings);
205
- row = Math.min(row, rows.length - 1);
206
278
  const selected = rows[row];
207
- if (Tui.matchesKey(data, Tui.Key.escape))
279
+ const keys = Tui.getKeybindings();
280
+ if (keys.matches(data, "tui.select.cancel"))
208
281
  close();
209
- else if (Tui.matchesKey(data, Tui.Key.up))
210
- row = Math.max(0, row - 1);
211
- else if (Tui.matchesKey(data, Tui.Key.down))
212
- row = Math.min(rows.length - 1, row + 1);
213
- else if (Tui.matchesKey(data, Tui.Key.enter) || data === " ")
214
- action(settings, selected);
282
+ else if (keys.matches(data, "tui.select.up") || keys.matches(data, "tui.select.down")) {
283
+ row = Math.max(0, Math.min(rows.length - 1, row + (keys.matches(data, "tui.select.up") ? -1 : 1)));
284
+ message = "";
285
+ }
286
+ else if (keys.matches(data, "tui.select.confirm") || data === " ")
287
+ action(settings, selected, data);
215
288
  else if (Tui.matchesKey(data, "ctrl+s")) {
216
289
  const result = handlers.save();
217
290
  if (result.ok) {
@@ -225,89 +298,60 @@ export function createSubagentSettingsPage(theme, handlers, requestRender, close
225
298
  if (picker)
226
299
  return picker.render(width);
227
300
  const settings = handlers.get();
228
- const rows = rowsOf(settings);
229
- row = Math.min(row, rows.length - 1);
230
301
  const w = Math.max(1, Math.floor(width));
231
- const compact = w < 40;
232
- const valueWidth = Math.max(1, w - LABEL_WIDTH - 1);
233
- const highlight = (text, selected) => selected
234
- ? theme.bg("selectedBg", theme.bold(cell(text, w))) : text;
235
- const marker = (selected) => selected ? "> " : " ";
236
- const source = skillSource(settings);
237
- const lines = [theme.fg("accent", theme.bold(" Subagent settings")), ""];
238
- const pushSetting = (label, value, selected, active) => {
239
- const title = theme.fg(selected ? "accent" : "text", marker(selected) + label);
240
- if (compact) {
241
- lines.push(highlight(title, selected));
242
- lines.push(" " + (selected ? theme.inverse(active) : theme.fg("text", active)));
243
- }
244
- else {
245
- lines.push(highlight(cell(title, LABEL_WIDTH) + " " +
246
- (selected ? theme.inverse(active) : theme.fg("text", active)), selected));
247
- }
248
- };
249
- let selectedStart = 2;
250
- let selectedEnd = 3;
251
- rows.forEach((entry, index) => {
252
- const selected = index === row;
253
- if (selected)
254
- selectedStart = lines.length;
255
- if (entry.kind === "mode" || entry.kind === "cap" || entry.kind === "toolWait"
256
- || entry.kind === "stuck" || entry.kind === "timeout") {
257
- const label = entry.kind === "mode" ? "Delegation mode"
258
- : entry.kind === "cap" ? "Concurrent subagents"
259
- : entry.kind === "toolWait" ? "Tool-wait alert"
260
- : entry.kind === "stuck" ? "No-progress alert" : "Timeout (minutes)";
261
- const value = entry.kind === "mode" ? settings.mode
262
- : entry.kind === "cap" ? String(settings.maxConcurrent)
263
- : entry.kind === "timeout" ? String(settings.maxMinutes)
264
- : `${entry.kind === "toolWait" ? settings.toolWaitMinutes : settings.stuckMinutes} min`;
265
- const active = editing?.kind === entry.kind
266
- ? input.render(compact ? Math.max(1, w - 2) : valueWidth)[0]
267
- : value;
268
- pushSetting(label, value, selected, active);
269
- if (entry.kind === "stuck" && settings.timingError) {
270
- const sourceLine = compact ? " " + settings.timingError : " ".repeat(LABEL_WIDTH) + " " + settings.timingError;
271
- lines.push(theme.fg("error", Tui.truncateToWidth(sourceLine, w, "")));
272
- }
273
- }
274
- else if (entry.kind === "skills") {
275
- pushSetting("Skill exposure", skillLabel(settings.skillsMode), selected, skillLabel(settings.skillsMode));
276
- const sourceLine = compact ? " " + source : " ".repeat(LABEL_WIDTH) + " " + source;
277
- lines.push(theme.fg(settings.skillsError ? "error" : "muted", Tui.truncateToWidth(sourceLine, w, "")));
278
- }
279
- else if (entry.kind === "choose") {
280
- const value = settings.skillsMode === "explicit" ? `${settings.skillsPaths.length} checked` : "Choose…";
281
- pushSetting("Selected skills", value, selected, value);
282
- }
283
- else if (entry.kind === "agents") {
284
- lines.push(highlight(theme.fg(selected ? "accent" : "text", marker(selected) + "Agents"), selected));
302
+ const height = Math.max(1, Math.floor(getHeight()));
303
+ const valueColumn = 2 + labelWidth + 2;
304
+ const body = [];
305
+ let selectedStart = 0;
306
+ let selectedEnd = 0;
307
+ for (const [index, entry] of rows.entries()) {
308
+ const active = index === row;
309
+ const value = settingValue(entry.kind, settings);
310
+ const stacked = valueColumn + Math.max(Tui.visibleWidth(value), editing?.kind === entry.kind ? 8 : 0) > w;
311
+ const column = stacked ? Math.min(4, Math.max(0, w - 1)) : valueColumn;
312
+ const valueText = editing?.kind === entry.kind
313
+ ? input.render(Math.max(1, w - column))[0] ?? ""
314
+ : active ? theme.inverse(value) : theme.fg("muted", value);
315
+ const label = (active ? "> " : " ") + theme.fg(active ? "accent" : "text", entry.label);
316
+ if (active)
317
+ selectedStart = body.length;
318
+ if (stacked) {
319
+ const valueLines = Tui.wrapTextWithAnsi(valueText, Math.max(1, w - column));
320
+ body.push(label, ...valueLines.map(line => " ".repeat(column) + line));
285
321
  }
286
322
  else {
287
- lines.push("");
288
- lines.push(highlight(theme.fg(selected ? "accent" : "text", marker(selected) + "Reset to defaults"), selected));
323
+ body.push(label + " ".repeat(valueColumn - Tui.visibleWidth(label)) + valueText);
289
324
  }
290
- if (selected)
291
- selectedEnd = lines.length;
292
- });
293
- const body = lines.slice(2);
294
- const footer = [];
295
- if (message)
296
- footer.push("", theme.fg(error ? "error" : "muted", message));
297
- footer.push("", theme.fg("dim", editing
298
- ? "Enter Apply · Esc Cancel"
299
- : "Up/Down Select · Space/Enter Change · ctrl+s Save default · Esc Back"));
300
- const height = Math.max(1, Math.floor(getHeight()));
301
- const room = Math.max(1, height - 2 - footer.length);
302
- if (selectedEnd - 2 > viewportStart + room)
303
- viewportStart = selectedEnd - 2 - room;
304
- if (selectedStart - 2 < viewportStart)
305
- viewportStart = selectedStart - 2;
325
+ if (active)
326
+ selectedEnd = body.length;
327
+ }
328
+ const selected = rows[row].kind;
329
+ const hint = shortcuts(selected, editing !== undefined);
330
+ const wrap = (text, color) => Tui.wrapTextWithAnsi(text, Math.max(1, w - 2)).map(line => " " + theme.fg(color, line));
331
+ const help = wrap(description(selected, settings), "text");
332
+ const feedback = [
333
+ ...(settings.timingError ? wrap(settings.timingError, "error") : []),
334
+ ...(settings.skillsError ? wrap(`Config error: ${settings.skillsError}`, "error") : []),
335
+ ...(message ? wrap(message, error ? "error" : "muted") : []),
336
+ ];
337
+ const title = theme.bold(theme.fg("text", "Subagent settings"));
338
+ const header = height >= 6 ? [title, ""] : [];
339
+ // Reserve space for the focused control and validation before its help or shortcuts.
340
+ const footerRoom = Math.max(0, height - header.length - (selectedEnd - selectedStart));
341
+ const status = feedback.slice(0, footerRoom);
342
+ const hints = wrap(hint, "dim");
343
+ const footer = [...help, "", ...hints];
344
+ const budget = footerRoom - status.length;
345
+ const visibleHelp = footer.length + 1 <= budget ? ["", ...footer]
346
+ : editing ? hints.slice(0, budget) : help.slice(0, Math.min(budget, 2));
347
+ const room = Math.max(1, height - header.length - visibleHelp.length - status.length);
306
348
  viewportStart = Math.max(0, Math.min(viewportStart, body.length - room));
307
- const visible = [...lines.slice(0, 2), ...body.slice(viewportStart, viewportStart + room), ...footer];
308
- // A very short terminal prioritizes the active control over chrome.
309
- const result = height < 2 + footer.length + 1 ? body.slice(selectedStart - 2, selectedStart - 2 + height) : visible;
310
- return result.map((line) => Tui.truncateToWidth(line, w, ""));
349
+ if (selectedEnd > viewportStart + room)
350
+ viewportStart = selectedEnd - room;
351
+ if (selectedStart < viewportStart)
352
+ viewportStart = selectedStart;
353
+ return [...header, ...body.slice(viewportStart, viewportStart + room), ...visibleHelp, ...status]
354
+ .map(line => Tui.truncateToWidth(line, w, ""));
311
355
  },
312
356
  };
313
357
  }