pi-better-server 0.6.0 → 0.7.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/package.json +4 -3
- package/scripts/measure-full-runtime/frontend.ts +10 -2
- package/scripts/measure-full-runtime.mjs +2 -0
- package/src/runtime/background-tools.ts +54 -8
- package/src/runtime/background.ts +16 -4
- package/src/runtime/domain/background/process.mjs +49 -11
- package/src/runtime/domain/background/supervisor/store.mjs +3 -3
- package/src/runtime/domain/background/supervisor/supervisor.mjs +9 -3
- package/src/runtime/domain/background-service/index.domain.mjs +19 -0
- package/src/runtime/domain/background-service/index.ts +19 -0
- package/src/runtime/domain/background-service/managed-frontend.domain.mjs +87 -5
- package/src/runtime/domain/background-service/managed-frontend.ts +76 -5
- package/src/runtime/domain/background-service/navigator-provider.domain.mjs +5 -0
- package/src/runtime/domain/background-service/navigator-provider.ts +4 -0
- package/src/runtime/domain/background-service/observed-waits.domain.mjs +203 -31
- package/src/runtime/domain/background-service/observed-waits.ts +168 -24
- package/src/runtime/domain/background-service/process.domain.mjs +49 -11
- package/src/runtime/domain/background-service/process.ts +45 -11
- package/src/runtime/domain/background-service/review-settings.domain.mjs +16 -0
- package/src/runtime/domain/background-service/review-settings.ts +18 -0
- package/src/runtime/domain/background-service/runtime.domain.mjs +8 -70
- package/src/runtime/domain/background-service/runtime.ts +8 -73
- package/src/runtime/domain/background-service/shared-navigator.domain.mjs +55 -28
- package/src/runtime/domain/background-service/shared-navigator.ts +47 -26
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/outbox.ts +9 -0
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.domain.mjs +5 -1
- package/src/runtime/domain/background-service/shared-operation-coordinator/src/store.ts +6 -0
- package/src/runtime/domain/background-service/stall.domain.mjs +5 -3
- package/src/runtime/domain/background-service/stall.ts +7 -4
- package/src/runtime/domain/background-service/supervised-progress.domain.mjs +42 -0
- package/src/runtime/domain/background-service/supervised-progress.ts +43 -0
- package/src/runtime/domain/background-service/supervisor/README.md +1 -4
- package/src/runtime/domain/background-service/supervisor/emit-runner.mjs +1 -0
- package/src/runtime/domain/background-service/supervisor/generated/process.mjs +49 -11
- package/src/runtime/domain/background-service/supervisor/generated/runner.mjs +47 -4
- package/src/runtime/domain/background-service/supervisor/generated/store.mjs +3 -3
- package/src/runtime/domain/background-service/supervisor/generated/watch-fingerprint.mjs +69 -0
- package/src/runtime/domain/background-service/supervisor/runner.domain.mjs +47 -4
- package/src/runtime/domain/background-service/supervisor/runner.ts +38 -3
- package/src/runtime/domain/background-service/supervisor/store.domain.mjs +3 -3
- package/src/runtime/domain/background-service/supervisor/store.ts +3 -3
- package/src/runtime/domain/background-service/supervisor/supervisor.domain.mjs +9 -3
- package/src/runtime/domain/background-service/supervisor/supervisor.ts +6 -3
- package/src/runtime/domain/background-service/tools.domain.mjs +22 -16
- package/src/runtime/domain/background-service/tools.ts +26 -16
- package/src/runtime/domain/background-service/types.ts +4 -2
- package/src/runtime/domain/background-service/watch-fingerprint.domain.mjs +67 -0
- package/src/runtime/domain/background-service/watch-fingerprint.ts +70 -0
- package/src/runtime/domain/coordinator/src/outbox.mjs +7 -0
- package/src/runtime/domain/coordinator/src/store.mjs +5 -1
- package/src/runtime/domain/log-paths.mjs +8 -1
- package/src/runtime/domain/parse.mjs +9 -5
- package/src/runtime/domain/sandbox-policy/README.md +3 -3
- package/src/runtime/domain/sandbox-policy/package.json +1 -1
- package/src/runtime/domain/session-store.mjs +7 -0
- package/src/runtime/domain/subagents/README.md +12 -10
- package/src/runtime/domain/subagents/assignment-outcomes.domain.mjs +2 -5
- package/src/runtime/domain/subagents/assignment-outcomes.ts +2 -5
- package/src/runtime/domain/subagents/assignment-report.domain.mjs +22 -0
- package/src/runtime/domain/subagents/assignment-report.ts +20 -0
- package/src/runtime/domain/subagents/batch.mjs +13 -5
- package/src/runtime/domain/subagents/cleanup.domain.mjs +11 -2
- package/src/runtime/domain/subagents/cleanup.ts +10 -2
- package/src/runtime/domain/subagents/config.domain.mjs +5 -0
- package/src/runtime/domain/subagents/config.ts +7 -1
- package/src/runtime/domain/subagents/continuation.domain.mjs +4 -2
- package/src/runtime/domain/subagents/continuation.ts +4 -2
- package/src/runtime/domain/subagents/failures.domain.mjs +11 -2
- package/src/runtime/domain/subagents/failures.ts +10 -2
- package/src/runtime/domain/subagents/finalization.domain.mjs +5 -2
- package/src/runtime/domain/subagents/finalization.ts +7 -2
- package/src/runtime/domain/subagents/index.domain.mjs +163 -61
- package/src/runtime/domain/subagents/index.ts +163 -60
- package/src/runtime/domain/subagents/isolated-workspace.domain.mjs +515 -0
- package/src/runtime/domain/subagents/isolated-workspace.ts +557 -0
- package/src/runtime/domain/subagents/list.mjs +2 -2
- package/src/runtime/domain/subagents/managed-launch-client.domain.mjs +22 -5
- package/src/runtime/domain/subagents/managed-launch-client.ts +23 -6
- package/src/runtime/domain/subagents/output-payload.domain.mjs +24 -9
- package/src/runtime/domain/subagents/output-payload.ts +26 -12
- package/src/runtime/domain/subagents/package.json +4 -2
- package/src/runtime/domain/subagents/parse.domain.mjs +9 -5
- package/src/runtime/domain/subagents/parse.ts +10 -5
- package/src/runtime/domain/subagents/registry.domain.mjs +24 -18
- package/src/runtime/domain/subagents/registry.ts +23 -5
- package/src/runtime/domain/subagents/retained-result.domain.mjs +56 -0
- package/src/runtime/domain/subagents/retained-result.ts +65 -0
- package/src/runtime/domain/subagents/session-selection.domain.mjs +3 -1
- package/src/runtime/domain/subagents/session-selection.ts +2 -1
- package/src/runtime/domain/subagents/session-store.domain.mjs +7 -0
- package/src/runtime/domain/subagents/session-store.ts +9 -0
- package/src/runtime/domain/subagents/settings-page.domain.mjs +151 -107
- package/src/runtime/domain/subagents/settings-page.ts +147 -108
- package/src/runtime/domain/subagents/shared-catalog.domain.mjs +23 -7
- package/src/runtime/domain/subagents/shared-catalog.ts +23 -7
- package/src/runtime/domain/subagents/shared-navigator.domain.mjs +55 -28
- package/src/runtime/domain/subagents/shared-navigator.ts +47 -26
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.domain.mjs +7 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/outbox.ts +9 -0
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.domain.mjs +5 -1
- package/src/runtime/domain/subagents/shared-operation-coordinator/src/store.ts +6 -0
- package/src/runtime/domain/subagents/task-guard.domain.mjs +14 -1
- package/src/runtime/domain/subagents/task-guard.ts +13 -1
- package/src/runtime/domain/subagents/task-policy.domain.mjs +2 -1
- package/src/runtime/domain/subagents/task-policy.ts +5 -1
- package/src/runtime/domain/subagents/tools.domain.mjs +4 -8
- package/src/runtime/domain/subagents/tools.ts +4 -7
- package/src/runtime/domain/subagents/work-observation.domain.mjs +25 -1
- package/src/runtime/domain/subagents/work-observation.ts +20 -1
- package/src/runtime/domain/subagents/workspace-files.domain.mjs +103 -0
- package/src/runtime/domain/subagents/workspace-files.ts +114 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.d.mts +38 -0
- package/src/runtime/domain/subagents/workspace-scope.domain.mjs +5 -0
- package/src/runtime/domain/subagents/workspace-scope.ts +6 -0
- package/src/runtime/domain/subagents/worktree-feedback.domain.mjs +55 -0
- package/src/runtime/domain/subagents/worktree-feedback.ts +77 -0
- package/src/runtime/read-model.ts +4 -2
- package/src/runtime/root.ts +5 -2
- package/src/version.ts +1 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Generated fixed launch domain from packages/pi-better-subagents/retained-result.ts. Do not edit directly.
|
|
2
|
+
import { copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { snapshotTree, verifyTree } from "./workspace-files.domain.mjs";
|
|
5
|
+
import { resultDir } from "./session-store.domain.mjs";
|
|
6
|
+
// Published only while retiring a quiescent attempt. A result record can answer reads,
|
|
7
|
+
// but cannot authorize admission, revive a writer or make a failed attempt resumable.
|
|
8
|
+
export function retainResult(meta, logPath, scratch) {
|
|
9
|
+
const dir = resultDir(meta.id);
|
|
10
|
+
if (!dir)
|
|
11
|
+
throw new Error("Invalid result identity");
|
|
12
|
+
if (existsSync(dir)) {
|
|
13
|
+
if (readRetainedResult(meta.id)?.sessionId !== meta.sessionId)
|
|
14
|
+
throw new Error("Retained result identity changed");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
mkdirSync(dirname(dir), { recursive: true, mode: 0o700 });
|
|
18
|
+
const temp = mkdtempSync(dir + "-");
|
|
19
|
+
try {
|
|
20
|
+
for (const name of ["output.log", "failures.jsonl"]) {
|
|
21
|
+
const source = join(dirname(logPath), name);
|
|
22
|
+
if (existsSync(source))
|
|
23
|
+
copyFileSync(source, join(temp, name));
|
|
24
|
+
}
|
|
25
|
+
let savedScratch;
|
|
26
|
+
if (scratch && existsSync(scratch)) {
|
|
27
|
+
const files = snapshotTree(scratch);
|
|
28
|
+
if (files.some((entry) => entry.name !== ".sandbox-anchor")) {
|
|
29
|
+
cpSync(scratch, join(temp, "scratch"), { recursive: true, verbatimSymlinks: true });
|
|
30
|
+
verifyTree(join(temp, "scratch"), files);
|
|
31
|
+
verifyTree(scratch, files);
|
|
32
|
+
savedScratch = join(dir, "scratch");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
writeFileSync(join(temp, "result.json"), JSON.stringify({ ...meta, taskScratch: savedScratch, logPath: join(dir, "output.log") }), { mode: 0o600 });
|
|
36
|
+
renameSync(temp, dir);
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
rmSync(temp, { recursive: true, force: true });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function readRetainedResult(id) {
|
|
43
|
+
const dir = resultDir(id);
|
|
44
|
+
if (!dir)
|
|
45
|
+
return;
|
|
46
|
+
try {
|
|
47
|
+
const meta = JSON.parse(readFileSync(join(dir, "result.json"), "utf8"));
|
|
48
|
+
if (meta.id === id && ["completed", "killed", "failed", "lost"].includes(meta.status))
|
|
49
|
+
return meta;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error.code !== "ENOENT")
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
copyFileSync,
|
|
3
|
+
cpSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
mkdtempSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
rmSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { dirname, join } from "node:path";
|
|
13
|
+
import { snapshotTree, verifyTree } from "./workspace-files.ts";
|
|
14
|
+
import type { RunMeta } from "./registry.ts";
|
|
15
|
+
import { resultDir } from "./session-store.ts";
|
|
16
|
+
|
|
17
|
+
// Published only while retiring a quiescent attempt. A result record can answer reads,
|
|
18
|
+
// but cannot authorize admission, revive a writer or make a failed attempt resumable.
|
|
19
|
+
export function retainResult(meta: RunMeta, logPath: string, scratch?: string): void {
|
|
20
|
+
const dir = resultDir(meta.id);
|
|
21
|
+
if (!dir) throw new Error("Invalid result identity");
|
|
22
|
+
if (existsSync(dir)) {
|
|
23
|
+
if (readRetainedResult(meta.id)?.sessionId !== meta.sessionId)
|
|
24
|
+
throw new Error("Retained result identity changed");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
mkdirSync(dirname(dir), { recursive: true, mode: 0o700 });
|
|
28
|
+
const temp = mkdtempSync(dir + "-");
|
|
29
|
+
try {
|
|
30
|
+
for (const name of ["output.log", "failures.jsonl"]) {
|
|
31
|
+
const source = join(dirname(logPath), name);
|
|
32
|
+
if (existsSync(source)) copyFileSync(source, join(temp, name));
|
|
33
|
+
}
|
|
34
|
+
let savedScratch: string | undefined;
|
|
35
|
+
if (scratch && existsSync(scratch)) {
|
|
36
|
+
const files = snapshotTree(scratch);
|
|
37
|
+
if (files.some((entry) => entry.name !== ".sandbox-anchor")) {
|
|
38
|
+
cpSync(scratch, join(temp, "scratch"), { recursive: true, verbatimSymlinks: true });
|
|
39
|
+
verifyTree(join(temp, "scratch"), files);
|
|
40
|
+
verifyTree(scratch, files);
|
|
41
|
+
savedScratch = join(dir, "scratch");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
writeFileSync(
|
|
45
|
+
join(temp, "result.json"),
|
|
46
|
+
JSON.stringify({ ...meta, taskScratch: savedScratch, logPath: join(dir, "output.log") }),
|
|
47
|
+
{ mode: 0o600 },
|
|
48
|
+
);
|
|
49
|
+
renameSync(temp, dir);
|
|
50
|
+
} finally {
|
|
51
|
+
rmSync(temp, { recursive: true, force: true });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function readRetainedResult(id: string): RunMeta | undefined {
|
|
56
|
+
const dir = resultDir(id);
|
|
57
|
+
if (!dir) return;
|
|
58
|
+
try {
|
|
59
|
+
const meta = JSON.parse(readFileSync(join(dir, "result.json"), "utf8")) as RunMeta;
|
|
60
|
+
if (meta.id === id && ["completed", "killed", "failed", "lost"].includes(meta.status)) return meta;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
// Generated fixed launch domain from packages/pi-better-subagents/session-selection.ts. Do not edit directly.
|
|
2
|
+
/** Exact-name selection of saved conversations; attempt IDs never become session IDs. */
|
|
3
|
+
import { discoveryWorkspace } from "./workspace-scope.domain.mjs";
|
|
2
4
|
/**
|
|
3
5
|
* Select a single conversation in the caller's workspace scope. Several attempts
|
|
4
6
|
* of one conversation count once; distinct conversations with the same display
|
|
@@ -18,7 +20,7 @@ export function selectSessionByName(metas, name, inWorkspace) {
|
|
|
18
20
|
latest.set(meta.sessionId, meta);
|
|
19
21
|
}
|
|
20
22
|
const matches = [...latest.values()]
|
|
21
|
-
.filter((meta) => meta.name === requested && inWorkspace(meta
|
|
23
|
+
.filter((meta) => meta.name === requested && inWorkspace(discoveryWorkspace(meta)))
|
|
22
24
|
.sort((a, b) => a.sessionId.localeCompare(b.sessionId));
|
|
23
25
|
if (matches.length === 0) {
|
|
24
26
|
throw new Error(`No saved subagent conversation named ${JSON.stringify(requested)} in the current folder. Use subagent_list({all:true}) to find its session_id.`);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Exact-name selection of saved conversations; attempt IDs never become session IDs. */
|
|
2
|
+
import { discoveryWorkspace } from "./workspace-scope.ts";
|
|
2
3
|
import type { RunMeta } from "./registry.ts";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -21,7 +22,7 @@ export function selectSessionByName(
|
|
|
21
22
|
if (!previous || meta.startedAt > previous.startedAt) latest.set(meta.sessionId, meta);
|
|
22
23
|
}
|
|
23
24
|
const matches = [...latest.values()]
|
|
24
|
-
.filter((meta) => meta.name === requested && inWorkspace(meta
|
|
25
|
+
.filter((meta) => meta.name === requested && inWorkspace(discoveryWorkspace(meta)))
|
|
25
26
|
.sort((a, b) => a.sessionId.localeCompare(b.sessionId));
|
|
26
27
|
if (matches.length === 0) {
|
|
27
28
|
throw new Error(`No saved subagent conversation named ${JSON.stringify(requested)} in the current folder. Use subagent_list({all:true}) to find its session_id.`);
|
|
@@ -72,6 +72,13 @@ export function durableSubagentRoot(env = process.env, home = homedir()) {
|
|
|
72
72
|
const agentDir = env.PI_CODING_AGENT_DIR?.trim();
|
|
73
73
|
return join(agentDir || join(home, ".pi", "agent"), "subagents");
|
|
74
74
|
}
|
|
75
|
+
export function resultDir(id) {
|
|
76
|
+
return /^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id) ? join(durableSubagentRoot(), "results", id) : undefined;
|
|
77
|
+
}
|
|
78
|
+
export function retainedLogPath(id) {
|
|
79
|
+
const dir = resultDir(id);
|
|
80
|
+
return dir && join(dir, "output.log");
|
|
81
|
+
}
|
|
75
82
|
/** Durable `--session-dir` for new child launches. */
|
|
76
83
|
export function durableSessionsDir() {
|
|
77
84
|
return join(durableSubagentRoot(), "sessions");
|
|
@@ -83,6 +83,15 @@ export function durableSubagentRoot(env: NodeJS.ProcessEnv = process.env, home:
|
|
|
83
83
|
return join(agentDir || join(home, ".pi", "agent"), "subagents");
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
export function resultDir(id: string): string | undefined {
|
|
87
|
+
return /^sa_[a-z0-9]+_[a-z0-9]+$/i.test(id) ? join(durableSubagentRoot(), "results", id) : undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function retainedLogPath(id: string): string | undefined {
|
|
91
|
+
const dir = resultDir(id);
|
|
92
|
+
return dir && join(dir, "output.log");
|
|
93
|
+
}
|
|
94
|
+
|
|
86
95
|
/** Durable `--session-dir` for new child launches. */
|
|
87
96
|
export function durableSessionsDir(): string {
|
|
88
97
|
return join(durableSubagentRoot(), "sessions");
|
|
@@ -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
|
|
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
|
|
29
|
-
|
|
30
|
-
|
|
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 === "
|
|
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
|
-
|
|
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
|
-
|
|
279
|
+
const keys = Tui.getKeybindings();
|
|
280
|
+
if (keys.matches(data, "tui.select.cancel"))
|
|
208
281
|
close();
|
|
209
|
-
else if (
|
|
210
|
-
row = Math.max(0, row - 1);
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
else if (
|
|
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
|
|
232
|
-
const
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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
|
-
|
|
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 (
|
|
291
|
-
selectedEnd =
|
|
292
|
-
}
|
|
293
|
-
const
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
? "
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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
|
}
|