@xzzpig/pi-subagents 0.14.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +239 -0
- package/README.md +7 -6
- package/agents/evidence-auditor.md +34 -0
- package/agents/researcher.md +23 -13
- package/agents/reviewer.md +3 -2
- package/docs/agents.md +55 -136
- package/docs/configuration.md +92 -14
- package/docs/extension-api.md +132 -74
- package/docs/fork-extensions.md +184 -0
- package/docs/missions.md +10 -2
- package/docs/models.md +11 -22
- package/docs/observability.md +63 -11
- package/docs/standalone-background.md +59 -0
- package/docs/tool-reference.md +42 -23
- package/docs/watchdog.md +39 -10
- package/docs/workflows.md +58 -19
- package/index.ts +5 -2
- package/inspector-runner.mjs +2 -2
- package/node_modules/@xzzpig/pi-components/dist/src/transcript.d.ts +13 -3
- package/node_modules/@xzzpig/pi-components/dist/src/transcript.js +18 -6
- package/node_modules/@xzzpig/pi-components/src/transcript.ts +26 -6
- package/package.json +9 -4
- package/prompts/parallel-review.md +1 -1
- package/runner-peer-loader.mjs +24 -0
- package/runner-peer-preload.mjs +36 -0
- package/skills/pi-subagents/SKILL.md +34 -22
- package/skills/pi-subagents/references/constraints-and-recipes.md +3 -2
- package/skills/pi-subagents/references/execution-controls.md +27 -8
- package/skills/pi-subagents/references/management-authoring-rpc.md +2 -2
- package/skills/pi-subagents/references/multi-lane-orchestration.md +3 -1
- package/skills/pi-subagents/references/prompting-and-roles.md +17 -13
- package/skills/pi-subagents/references/review-and-validation.md +3 -3
- package/src/agents/advertised-agent-prompt.ts +94 -0
- package/src/agents/agent-eject.ts +339 -0
- package/src/agents/agent-management.ts +79 -387
- package/src/agents/agent-serializer.ts +6 -3
- package/src/agents/agents.ts +198 -72
- package/src/agents/builtin-names.ts +1 -0
- package/src/agents/chain-serializer.ts +5 -0
- package/src/agents/runtime-agent-registry.ts +7 -6
- package/src/api/agent-management.ts +2 -2
- package/src/api/delegation.ts +4 -0
- package/src/api/preflight.ts +131 -86
- package/src/api/required-child-extensions.ts +6 -0
- package/src/api/shared-types.ts +3 -1
- package/src/api/workflow-resources.ts +6 -0
- package/src/extension/config.ts +14 -37
- package/src/extension/fanout-child.ts +68 -5
- package/src/extension/herdr-pi-bridge.ts +164 -0
- package/src/extension/index.ts +141 -51
- package/src/extension/public-execution.ts +7 -6
- package/src/extension/rpc.ts +8 -21
- package/src/extension/schemas.ts +86 -84
- package/src/extension/tool-description.ts +32 -79
- package/src/inspectors/actions.ts +148 -0
- package/src/inspectors/ghostty/actions.ts +74 -0
- package/src/inspectors/ghostty/plugin.ts +17 -0
- package/src/inspectors/herdr/actions.ts +99 -179
- package/src/inspectors/herdr/plugin.ts +20 -0
- package/src/inspectors/herdr/project-panes.ts +4 -1
- package/src/inspectors/{herdr/inspector-runner.ts → inspector-runner.ts} +12 -12
- package/src/inspectors/plugins.ts +8 -0
- package/src/inspectors/{herdr/session-roots-codec.ts → session-roots-codec.ts} +3 -14
- package/src/inspectors/types.ts +51 -0
- package/src/integrations/pi-web-session-liveness.ts +73 -0
- package/src/intercom/intercom-bridge.ts +50 -8
- package/src/intercom/native-supervisor-channel.ts +142 -115
- package/src/intercom/supervisor-ui.ts +3 -2
- package/src/missions/workflow-state.ts +37 -16
- package/src/policy/authority.ts +4 -0
- package/src/profiles/profiles.ts +12 -6
- package/src/runs/background/active-async-capacity.ts +22 -18
- package/src/runs/background/active-run-index.ts +17 -1
- package/src/runs/background/async-execution.ts +359 -176
- package/src/runs/background/async-job-tracker.ts +41 -7
- package/src/runs/background/async-resume.ts +19 -12
- package/src/runs/background/async-retention.ts +20 -0
- package/src/runs/background/async-status-snapshot.ts +10 -12
- package/src/runs/background/async-status.ts +38 -35
- package/src/runs/background/auto-drain.ts +59 -35
- package/src/runs/background/binary-bootstrap.ts +38 -0
- package/src/runs/background/chain-append.ts +1 -1
- package/src/runs/background/chain-root-attachment.ts +22 -33
- package/src/runs/background/control-channel.ts +78 -44
- package/src/runs/background/fleet-view.ts +30 -2
- package/src/runs/background/notify.ts +193 -19
- package/src/runs/background/owned-process-tree.ts +35 -8
- package/src/runs/background/process-terminal.ts +23 -23
- package/src/runs/background/result-files.ts +8 -4
- package/src/runs/background/result-watcher.ts +19 -2
- package/src/runs/background/retained-nested-route-tracker.ts +96 -0
- package/src/runs/background/run-child-session.ts +118 -41
- package/src/runs/background/run-status.ts +78 -5
- package/src/runs/background/runner-aliases.ts +22 -5
- package/src/runs/background/runner-child-launch.ts +93 -0
- package/src/runs/background/runner-child-sessions.ts +5 -4
- package/src/runs/background/runner-http-dispatcher.ts +119 -0
- package/src/runs/background/scheduled-runs.ts +51 -18
- package/src/runs/background/stale-run-reconciler.ts +38 -12
- package/src/runs/background/steering.ts +20 -2
- package/src/runs/background/subagent-runner.ts +770 -444
- package/src/runs/background/subagent-wait.ts +179 -28
- package/src/runs/background/wait-completions.ts +79 -27
- package/src/runs/background/wait-subscriptions.ts +9 -3
- package/src/runs/background/wait-tool.ts +5 -3
- package/src/runs/foreground/async-steering-action.ts +37 -7
- package/src/runs/foreground/async-stop-action.ts +93 -3
- package/src/runs/foreground/execution.ts +174 -194
- package/src/runs/foreground/foreground-history.ts +5 -2
- package/src/runs/foreground/prompt-audit.ts +12 -6
- package/src/runs/foreground/subagent-executor.ts +915 -411
- package/src/runs/foreground/workflow-detach-reconcile.ts +10 -5
- package/src/runs/foreground/workflow-foreground-steering.ts +57 -2
- package/src/runs/shared/acceptance.ts +54 -14
- package/src/runs/shared/agent-contract.ts +1 -1
- package/src/runs/shared/async-status-projection.ts +172 -78
- package/src/runs/shared/capability-ceiling.ts +2 -0
- package/src/runs/shared/child-hooks.ts +167 -3
- package/src/runs/shared/child-launch-plan.ts +15 -3
- package/src/runs/shared/child-launch.ts +49 -85
- package/src/runs/shared/child-lifecycle.ts +6 -3
- package/src/runs/shared/child-runtime-config.ts +8 -1
- package/src/runs/shared/child-session.ts +130 -24
- package/src/runs/shared/child-tool-plan.ts +149 -112
- package/src/runs/shared/completion-evidence.ts +2 -2
- package/src/runs/shared/completion-guard.ts +6 -3
- package/src/runs/shared/dynamic-fanout.ts +2 -2
- package/src/runs/shared/effective-system-prompt.ts +33 -0
- package/src/runs/shared/external-cli-contract.ts +11 -1
- package/src/runs/shared/external-cli-preflight.ts +6 -2
- package/src/runs/shared/external-cli-runner.ts +9 -7
- package/src/runs/shared/herdr-connection.ts +134 -0
- package/src/runs/shared/herdr-external-adapters.ts +169 -0
- package/src/runs/shared/herdr-machine.ts +279 -0
- package/src/runs/shared/herdr-pi-protocol.ts +59 -0
- package/src/runs/shared/herdr-placed-run.ts +263 -0
- package/src/runs/shared/host-step-status.ts +11 -11
- package/src/runs/shared/llm-intent-arbiter.ts +40 -21
- package/src/runs/shared/model-resolution-diagnostic.ts +76 -0
- package/src/runs/shared/{model-fallback.ts → model-resolution.ts} +34 -218
- package/src/runs/shared/model-scope.ts +1 -1
- package/src/runs/shared/nested-events.ts +27 -34
- package/src/runs/shared/orca-progress-tabs.ts +7 -1
- package/src/runs/shared/parallel-handoff.ts +57 -12
- package/src/runs/shared/parallel-utils.ts +10 -4
- package/src/runs/shared/pi-spawn.ts +10 -0
- package/src/runs/shared/profile-extensions.ts +134 -0
- package/src/runs/shared/profile-launch-env.ts +113 -0
- package/src/runs/shared/readonly-drain-observation.ts +42 -0
- package/src/runs/shared/run-fanout-budget.ts +8 -8
- package/src/runs/shared/runtime-acknowledged-extensions.ts +3 -3
- package/src/runs/shared/sandbox-profile-guard.ts +34 -0
- package/src/runs/shared/subagent-prompt-runtime.ts +23 -5
- package/src/runs/shared/task-intent.ts +46 -13
- package/src/runs/shared/workflow-async-child-guidance.ts +18 -0
- package/src/runs/shared/worktree-cleanup-plan.ts +6 -3
- package/src/runs/shared/worktree-setup-command.ts +213 -0
- package/src/runs/shared/worktree.ts +437 -214
- package/src/shared/child-cache-retention.ts +43 -0
- package/src/shared/fork-context.ts +15 -72
- package/src/shared/launch-contract.ts +70 -8
- package/src/shared/model-response-aliases.ts +13 -0
- package/src/shared/opencode-session-headers.ts +30 -0
- package/src/shared/permission-profile.ts +14 -0
- package/src/shared/pruned-fork.ts +1 -1
- package/src/shared/required-child-extensions.ts +81 -0
- package/src/shared/sandbox-profile.ts +14 -0
- package/src/shared/settings.ts +5 -2
- package/src/shared/shortcuts.ts +0 -4
- package/src/shared/types.ts +163 -90
- package/src/shared/utils.ts +10 -2
- package/src/shared/watch-strategy.ts +2 -0
- package/src/shared/workflow-child-permit.ts +18 -13
- package/src/slash/delegation-adapters.ts +3 -1
- package/src/slash/delegation-request.ts +14 -0
- package/src/slash/slash-commands.ts +2 -7
- package/src/slash/subagents-admin.ts +31 -14
- package/src/tui/fleet-status.ts +165 -20
- package/src/tui/fleet.ts +30 -19
- package/src/tui/render.ts +210 -50
- package/src/watchdog/child-status.ts +28 -28
- package/src/watchdog/lsp-diagnostics.ts +1 -1
- package/src/watchdog/model-selection.ts +21 -1
- package/src/watchdog/permission-arbiter.ts +5 -2
- package/src/watchdog/register-child.ts +10 -2
- package/src/watchdog/register-main.ts +39 -35
- package/src/watchdog/render.ts +1 -1
- package/src/watchdog/review.ts +122 -74
- package/src/watchdog/rules.ts +1 -1
- package/src/watchdog/runtime.ts +100 -27
- package/src/watchdog/scope.ts +1 -1
- package/src/watchdog/settings.ts +3 -0
- package/src/watchdog/tool-actions.ts +13 -12
- package/src/watchdog/turn-delta.ts +23 -0
- package/src/watchdog/types.ts +5 -3
- package/src/watchdog/warning-format.ts +1 -1
- package/src/workflows/chat-progress.ts +3 -3
- package/src/workflows/scripted-workflow.ts +349 -26
- package/src/workflows/workflow-checklist.ts +17 -20
- package/src/workflows/workflow-child-summary.ts +57 -8
- package/src/workflows/workflow-preflight.ts +19 -19
- package/src/workflows/workflow-receipt.ts +24 -6
- package/src/workflows/workflow-resources.ts +108 -22
- package/src/workflows/workflow-settlement.ts +3 -0
- package/src/runs/shared/model-exclusions.ts +0 -373
- /package/src/inspectors/{herdr/shell-command.ts → shell-command.ts} +0 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [fork] Agent ejection implementation.
|
|
3
|
+
*
|
|
4
|
+
* This module is fork-only: it was split out of agents/agent-management.ts so
|
|
5
|
+
* that file stays byte-close to upstream. The management action handler
|
|
6
|
+
* (handleEject) remains in agent-management.ts and delegates here, and
|
|
7
|
+
* src/api/agent-management.ts re-exports the public API from this module.
|
|
8
|
+
*/
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as path from "node:path";
|
|
11
|
+
import type { AgentConfig } from "./agents.ts";
|
|
12
|
+
import { discoverAgents, discoverAgentsAll } from "./agents.ts";
|
|
13
|
+
import { serializeAgent } from "./agent-serializer.ts";
|
|
14
|
+
import { resolveSkillsWithFallback } from "./skills.ts";
|
|
15
|
+
import { parseFrontmatter } from "./frontmatter.ts";
|
|
16
|
+
import { resolvePiLaunchToolPlan } from "../runs/shared/child-tool-plan.ts";
|
|
17
|
+
import { availableAgentNames, nameExistsInScope, sanitizeName } from "./agent-management.ts";
|
|
18
|
+
|
|
19
|
+
export type AgentEjectionScope = "user" | "project";
|
|
20
|
+
|
|
21
|
+
export type AgentEjectionErrorCode =
|
|
22
|
+
| "invalid_agent"
|
|
23
|
+
| "untrusted_project"
|
|
24
|
+
| "missing_project_root"
|
|
25
|
+
| "missing_source"
|
|
26
|
+
| "existing_custom"
|
|
27
|
+
| "name_conflict"
|
|
28
|
+
| "existing_file"
|
|
29
|
+
| "source_read_failed"
|
|
30
|
+
| "resource_missing"
|
|
31
|
+
| "write_failed"
|
|
32
|
+
| "rediscovery_failed"
|
|
33
|
+
| "preflight_failed";
|
|
34
|
+
|
|
35
|
+
export interface EjectAgentDefinitionInput {
|
|
36
|
+
cwd: string;
|
|
37
|
+
agent: string;
|
|
38
|
+
scope: AgentEjectionScope;
|
|
39
|
+
/** Direct project-scope API callers must affirm host project trust. */
|
|
40
|
+
projectTrusted?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface EjectAgentDefinitionVerification {
|
|
44
|
+
agent: string;
|
|
45
|
+
scope: AgentEjectionScope;
|
|
46
|
+
targetPath: string;
|
|
47
|
+
source: "builtin" | "package";
|
|
48
|
+
resourcePaths: string[];
|
|
49
|
+
launchPreflighted: true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type EjectAgentDefinitionResult =
|
|
53
|
+
| {
|
|
54
|
+
ok: true;
|
|
55
|
+
code: "ejected";
|
|
56
|
+
message: string;
|
|
57
|
+
agent: string;
|
|
58
|
+
source: "builtin" | "package";
|
|
59
|
+
scope: AgentEjectionScope;
|
|
60
|
+
targetPath: string;
|
|
61
|
+
verification: EjectAgentDefinitionVerification;
|
|
62
|
+
}
|
|
63
|
+
| {
|
|
64
|
+
ok: false;
|
|
65
|
+
code: AgentEjectionErrorCode;
|
|
66
|
+
message: string;
|
|
67
|
+
scope?: AgentEjectionScope;
|
|
68
|
+
targetPath?: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function isRelativeAgentResourcePath(value: string): boolean {
|
|
72
|
+
return value === "." || value === ".." || value.startsWith("./") || value.startsWith("../");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Upstream v0.53.0 resolves agent-frontmatter extension paths to absolute
|
|
77
|
+
* paths at discovery time, so a portable resource is any entry that is a
|
|
78
|
+
* path (relative or absolute) rather than a bare tool name.
|
|
79
|
+
*/
|
|
80
|
+
function isAgentResourcePath(value: string): boolean {
|
|
81
|
+
return isRelativeAgentResourcePath(value) || path.isAbsolute(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function resolvePortableAgentResources(
|
|
85
|
+
entries: string[] | undefined,
|
|
86
|
+
sourceFilePath: string,
|
|
87
|
+
): { entries: string[] | undefined; resourcePaths: string[] } {
|
|
88
|
+
const baseDir = path.dirname(sourceFilePath);
|
|
89
|
+
const resourcePaths = (entries ?? []).flatMap((entry) =>
|
|
90
|
+
isAgentResourcePath(entry) ? [path.resolve(baseDir, entry)] : [],
|
|
91
|
+
);
|
|
92
|
+
return {
|
|
93
|
+
entries: entries?.map((entry) => isAgentResourcePath(entry) ? path.resolve(baseDir, entry) : entry),
|
|
94
|
+
resourcePaths,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function portableEjectedAgentContent(source: AgentConfig): {
|
|
99
|
+
content?: string;
|
|
100
|
+
resourcePaths?: string[];
|
|
101
|
+
error?: EjectAgentDefinitionResult;
|
|
102
|
+
} {
|
|
103
|
+
let sourceContent: string;
|
|
104
|
+
try {
|
|
105
|
+
sourceContent = fs.readFileSync(source.filePath, "utf-8");
|
|
106
|
+
} catch (error) {
|
|
107
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
108
|
+
return {
|
|
109
|
+
error: {
|
|
110
|
+
ok: false,
|
|
111
|
+
code: "source_read_failed",
|
|
112
|
+
message: `Failed to read source agent at ${source.filePath}: ${message}`,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const extensions = resolvePortableAgentResources(source.extensions, source.filePath);
|
|
118
|
+
const subagentOnlyExtensions = resolvePortableAgentResources(source.subagentOnlyExtensions, source.filePath);
|
|
119
|
+
const skillPath = resolvePortableAgentResources(source.skillPath, source.filePath);
|
|
120
|
+
const tools = resolvePortableAgentResources(source.tools, source.filePath);
|
|
121
|
+
const resourcePaths = [...new Set([
|
|
122
|
+
...extensions.resourcePaths,
|
|
123
|
+
...subagentOnlyExtensions.resourcePaths,
|
|
124
|
+
...skillPath.resourcePaths,
|
|
125
|
+
...tools.resourcePaths,
|
|
126
|
+
])].sort((a, b) => a.localeCompare(b));
|
|
127
|
+
const missingResources = resourcePaths.filter((resourcePath) => !fs.existsSync(resourcePath));
|
|
128
|
+
if (missingResources.length > 0) {
|
|
129
|
+
return {
|
|
130
|
+
error: {
|
|
131
|
+
ok: false,
|
|
132
|
+
code: "resource_missing",
|
|
133
|
+
message: `Agent '${source.name}' cannot be ejected because its relative resource path(s) do not exist: ${missingResources.join(", ")}.`,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
if (resourcePaths.length === 0) return { content: sourceContent, resourcePaths };
|
|
138
|
+
|
|
139
|
+
const { frontmatter } = parseFrontmatter(sourceContent);
|
|
140
|
+
const portableAgent: AgentConfig = {
|
|
141
|
+
...source,
|
|
142
|
+
...(source.extensions !== undefined ? { extensions: extensions.entries! } : {}),
|
|
143
|
+
...(source.subagentOnlyExtensions !== undefined ? { subagentOnlyExtensions: subagentOnlyExtensions.entries! } : {}),
|
|
144
|
+
...(source.skillPath !== undefined ? { skillPath: skillPath.entries! } : {}),
|
|
145
|
+
...(source.tools !== undefined ? { tools: tools.entries! } : {}),
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
content: serializeAgent(portableAgent, { preserveFrontmatterFields: new Set(Object.keys(frontmatter)) }),
|
|
149
|
+
resourcePaths,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function cleanupEjectedFile(targetPath: string, expectedContent: string): void {
|
|
154
|
+
try {
|
|
155
|
+
if (fs.readFileSync(targetPath, "utf-8") === expectedContent) fs.unlinkSync(targetPath);
|
|
156
|
+
} catch {
|
|
157
|
+
// The service only cleans the file it created during this call.
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function validateEjectedAgentLaunchPreflight(agent: AgentConfig, cwd: string): string | undefined {
|
|
162
|
+
const resolvedSkills = resolveSkillsWithFallback(
|
|
163
|
+
agent.skills ?? [],
|
|
164
|
+
cwd,
|
|
165
|
+
undefined,
|
|
166
|
+
agent.skillPath,
|
|
167
|
+
agent.filePath ? path.dirname(agent.filePath) : cwd,
|
|
168
|
+
);
|
|
169
|
+
if (resolvedSkills.missing.length > 0) {
|
|
170
|
+
return `Missing skills: ${resolvedSkills.missing.join(", ")}`;
|
|
171
|
+
}
|
|
172
|
+
try {
|
|
173
|
+
resolvePiLaunchToolPlan({
|
|
174
|
+
tools: agent.tools,
|
|
175
|
+
extensions: agent.extensions,
|
|
176
|
+
subagentOnlyExtensions: agent.subagentOnlyExtensions,
|
|
177
|
+
mcpDirectTools: agent.mcpDirectTools,
|
|
178
|
+
cwd,
|
|
179
|
+
requireReadTool: resolvedSkills.resolved.length > 0,
|
|
180
|
+
agentName: agent.name,
|
|
181
|
+
sandbox: agent.sandbox,
|
|
182
|
+
permissionProfile: agent.permissionProfile,
|
|
183
|
+
});
|
|
184
|
+
} catch (error) {
|
|
185
|
+
return error instanceof Error ? error.message : String(error);
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Shared extraction of the existing eject handler. It deliberately owns only
|
|
192
|
+
* agent-definition copying, no-overwrite checks, project-trust input, and
|
|
193
|
+
* portable package-relative resource paths.
|
|
194
|
+
*/
|
|
195
|
+
export function ejectAgentDefinition(input: EjectAgentDefinitionInput): EjectAgentDefinitionResult {
|
|
196
|
+
const raw = input.agent.trim();
|
|
197
|
+
const sanitized = sanitizeName(raw);
|
|
198
|
+
if (!raw || !sanitized) {
|
|
199
|
+
return { ok: false, code: "invalid_agent", message: "agent must be a non-empty valid agent name." };
|
|
200
|
+
}
|
|
201
|
+
if (input.scope !== "user" && input.scope !== "project") {
|
|
202
|
+
return { ok: false, code: "invalid_agent", message: "scope must be 'user' or 'project'." };
|
|
203
|
+
}
|
|
204
|
+
if (input.scope === "project" && input.projectTrusted !== true) {
|
|
205
|
+
return {
|
|
206
|
+
ok: false,
|
|
207
|
+
code: "untrusted_project",
|
|
208
|
+
scope: input.scope,
|
|
209
|
+
message: "Project scope ejection requires a trusted project. Trust the project and retry.",
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let discovered: ReturnType<typeof discoverAgentsAll>;
|
|
214
|
+
try {
|
|
215
|
+
discovered = discoverAgentsAll(input.cwd);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
return {
|
|
218
|
+
ok: false,
|
|
219
|
+
code: "rediscovery_failed",
|
|
220
|
+
scope: input.scope,
|
|
221
|
+
message: `Unable to discover agents before ejecting: ${error instanceof Error ? error.message : String(error)}`,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if (input.scope === "project" && discovered.projectDir === null) {
|
|
225
|
+
return {
|
|
226
|
+
ok: false,
|
|
227
|
+
code: "missing_project_root",
|
|
228
|
+
scope: input.scope,
|
|
229
|
+
message: "Project scope ejection is not available here: no project config root (.pi or .agents) was found above the cwd.",
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const existingCustom = (input.scope === "user" ? discovered.user : discovered.project)
|
|
234
|
+
.find((candidate) => candidate.name === raw || candidate.name === sanitized);
|
|
235
|
+
if (existingCustom) {
|
|
236
|
+
return {
|
|
237
|
+
ok: false,
|
|
238
|
+
code: "existing_custom",
|
|
239
|
+
scope: input.scope,
|
|
240
|
+
targetPath: existingCustom.filePath,
|
|
241
|
+
message: `Agent '${existingCustom.name}' is already a custom ${input.scope} agent at ${existingCustom.filePath}. Edit it with { action: "update", agent: "${existingCustom.name}" } or delete it first.`,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const bundledCandidates = input.scope === "user"
|
|
245
|
+
? discoverAgents(input.cwd, "user").agents.filter((candidate) => candidate.source === "builtin" || candidate.source === "package")
|
|
246
|
+
: [...discovered.package, ...discovered.builtin];
|
|
247
|
+
const source = bundledCandidates.find((candidate) => candidate.name === raw || candidate.name === sanitized);
|
|
248
|
+
if (!source) {
|
|
249
|
+
return {
|
|
250
|
+
ok: false,
|
|
251
|
+
code: "missing_source",
|
|
252
|
+
scope: input.scope,
|
|
253
|
+
message: `Agent '${raw}' not found or is not a bundled/package agent. eject copies a builtin or package agent to ${input.scope} scope so it can be customized. Available: ${availableAgentNames(input.cwd).join(", ") || "none"}.`,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
const runtimeName = source.name;
|
|
257
|
+
if (nameExistsInScope(input.cwd, input.scope, runtimeName)) {
|
|
258
|
+
return {
|
|
259
|
+
ok: false,
|
|
260
|
+
code: "name_conflict",
|
|
261
|
+
scope: input.scope,
|
|
262
|
+
message: `An agent or chain named '${runtimeName}' already exists in ${input.scope} scope. Remove or rename it first.`,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const targetDir = input.scope === "user" ? discovered.userDir : discovered.projectDir!;
|
|
267
|
+
const targetPath = path.join(targetDir, `${runtimeName}.md`);
|
|
268
|
+
if (fs.existsSync(targetPath)) {
|
|
269
|
+
return {
|
|
270
|
+
ok: false,
|
|
271
|
+
code: "existing_file",
|
|
272
|
+
scope: input.scope,
|
|
273
|
+
targetPath,
|
|
274
|
+
message: `File already exists at ${targetPath} but is not a valid agent definition. Remove or rename it first.`,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const portable = portableEjectedAgentContent(source);
|
|
279
|
+
if (portable.error) return { ...portable.error, scope: input.scope, targetPath };
|
|
280
|
+
const content = portable.content!;
|
|
281
|
+
try {
|
|
282
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
283
|
+
fs.writeFileSync(targetPath, content, { encoding: "utf-8", flag: "wx" });
|
|
284
|
+
} catch (error) {
|
|
285
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
286
|
+
return {
|
|
287
|
+
ok: false,
|
|
288
|
+
code: "write_failed",
|
|
289
|
+
scope: input.scope,
|
|
290
|
+
targetPath,
|
|
291
|
+
message: `Failed to write ejected agent '${runtimeName}' at ${targetPath}: ${message}`,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const rediscovered = discoverAgentsAll(input.cwd);
|
|
296
|
+
const copied = (input.scope === "user" ? rediscovered.user : rediscovered.project)
|
|
297
|
+
.find((candidate) => candidate.name === runtimeName && path.resolve(candidate.filePath) === path.resolve(targetPath));
|
|
298
|
+
if (!copied) {
|
|
299
|
+
cleanupEjectedFile(targetPath, content);
|
|
300
|
+
return {
|
|
301
|
+
ok: false,
|
|
302
|
+
code: "rediscovery_failed",
|
|
303
|
+
scope: input.scope,
|
|
304
|
+
targetPath,
|
|
305
|
+
message: `Ejected agent '${runtimeName}' could not be rediscovered at ${targetPath}; no file was kept.`,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const preflightError = validateEjectedAgentLaunchPreflight(copied, input.cwd);
|
|
310
|
+
if (preflightError) {
|
|
311
|
+
cleanupEjectedFile(targetPath, content);
|
|
312
|
+
return {
|
|
313
|
+
ok: false,
|
|
314
|
+
code: "preflight_failed",
|
|
315
|
+
scope: input.scope,
|
|
316
|
+
targetPath,
|
|
317
|
+
message: `Ejected agent '${runtimeName}' failed launch preflight at ${targetPath}: ${preflightError}. No file was kept.`,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const sourceKind = source.source as "builtin" | "package";
|
|
322
|
+
return {
|
|
323
|
+
ok: true,
|
|
324
|
+
code: "ejected",
|
|
325
|
+
message: `Ejected agent '${runtimeName}' from ${sourceKind} to ${input.scope} scope at ${targetPath}. Edit it there to customize; it shadows the bundled ${sourceKind} agent of the same name.`,
|
|
326
|
+
agent: runtimeName,
|
|
327
|
+
source: sourceKind,
|
|
328
|
+
scope: input.scope,
|
|
329
|
+
targetPath,
|
|
330
|
+
verification: {
|
|
331
|
+
agent: copied.name,
|
|
332
|
+
scope: input.scope,
|
|
333
|
+
targetPath,
|
|
334
|
+
source: sourceKind,
|
|
335
|
+
resourcePaths: portable.resourcePaths ?? [],
|
|
336
|
+
launchPreflighted: true,
|
|
337
|
+
},
|
|
338
|
+
};
|
|
339
|
+
}
|