@xzzpig/pi-subagents 0.1.0 → 0.3.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/CHANGELOG.md +135 -0
- package/README.md +4 -0
- package/agents/reviewer.md +3 -4
- package/docs/agents.md +343 -0
- package/docs/configuration.md +395 -0
- package/docs/extension-api.md +308 -0
- package/docs/missions.md +121 -0
- package/docs/models.md +192 -0
- package/docs/observability.md +198 -0
- package/docs/tool-reference.md +343 -0
- package/docs/watchdog.md +176 -0
- package/docs/workflows.md +163 -0
- package/package.json +13 -11
- package/skills/pi-subagents/references/constraints-and-recipes.md +1 -1
- package/skills/pi-subagents/references/execution-controls.md +7 -7
- package/skills/pi-subagents/references/management-authoring-rpc.md +1 -1
- package/skills/pi-subagents/references/prompting-and-roles.md +1 -1
- package/src/agents/agents.ts +53 -23
- package/src/agents/frontmatter.ts +7 -3
- package/src/agents/skills.ts +2 -9
- package/src/api/preflight.ts +1 -1
- package/src/api/project-panes.ts +30 -0
- package/src/extension/config.ts +54 -1
- package/src/extension/doctor.ts +40 -0
- package/src/extension/fanout-child.ts +5 -4
- package/src/extension/index.ts +114 -38
- package/src/extension/public-execution.ts +35 -4
- package/src/extension/rpc.ts +10 -15
- package/src/extension/schemas.ts +33 -11
- package/src/extension/subagent-guide.ts +39 -0
- package/src/extension/tool-description.ts +38 -18
- package/src/inspectors/herdr/actions.ts +11 -2
- package/src/inspectors/herdr/inspector-runner.ts +16 -3
- package/src/inspectors/herdr/project-panes.ts +459 -63
- package/src/intercom/intercom-bridge.ts +7 -2
- package/src/intercom/native-supervisor-channel.ts +102 -4
- package/src/missions/actions.ts +25 -2
- package/src/missions/lifecycle.ts +25 -9
- package/src/missions/store.ts +89 -7
- package/src/missions/types.ts +33 -0
- package/src/missions/workflow-state.ts +25 -15
- package/src/runs/background/active-async-capacity.ts +431 -0
- package/src/runs/background/active-run-index.ts +46 -0
- package/src/runs/background/async-execution.ts +158 -71
- package/src/runs/background/async-job-tracker.ts +297 -133
- package/src/runs/background/async-resume.ts +37 -5
- package/src/runs/background/async-status.ts +39 -5
- package/src/runs/background/chain-append.ts +33 -15
- package/src/runs/background/completion-replay.ts +267 -0
- package/src/runs/background/control-channel.ts +95 -54
- package/src/runs/background/fleet-view.ts +18 -5
- package/src/runs/background/owned-process-tree.ts +104 -0
- package/src/runs/background/process-terminal.ts +17 -3
- package/src/runs/background/result-watcher.ts +113 -10
- package/src/runs/background/resume-guidance.ts +53 -0
- package/src/runs/background/retained-children.ts +14 -6
- package/src/runs/background/run-status.ts +119 -5
- package/src/runs/background/scheduled-runs.ts +25 -1
- package/src/runs/background/stale-run-reconciler.ts +12 -24
- package/src/runs/background/subagent-runner.ts +127 -41
- package/src/runs/background/subagent-wait.ts +5 -2
- package/src/runs/background/wait-completions.ts +39 -5
- package/src/runs/background/wait-subscriptions.ts +21 -5
- package/src/runs/foreground/async-dismiss-action.ts +85 -0
- package/src/runs/foreground/async-steering-action.ts +7 -8
- package/src/runs/foreground/chain-execution.ts +40 -2
- package/src/runs/foreground/execution.ts +97 -19
- package/src/runs/foreground/foreground-control.ts +12 -0
- package/src/runs/foreground/foreground-history.ts +137 -0
- package/src/runs/foreground/prompt-audit.ts +172 -0
- package/src/runs/foreground/subagent-executor.ts +1274 -243
- package/src/runs/foreground/workflow-foreground-steering.ts +187 -0
- package/src/runs/shared/acceptance.ts +13 -4
- package/src/runs/shared/completion-guard.ts +17 -1
- package/src/runs/shared/dynamic-fanout.ts +1 -1
- package/src/runs/shared/llm-intent-arbiter.ts +302 -0
- package/src/runs/shared/model-fallback.ts +8 -4
- package/src/runs/shared/model-scope.ts +12 -2
- package/src/runs/shared/parallel-utils.ts +3 -0
- package/src/runs/shared/pi-args.ts +44 -1
- package/src/runs/shared/run-fanout-budget.ts +280 -0
- package/src/runs/shared/single-output.ts +4 -2
- package/src/runs/shared/subagent-prompt-runtime.ts +94 -9
- package/src/runs/shared/task-intent.ts +19 -3
- package/src/runs/shared/worktree.ts +20 -7
- package/src/shared/agent-stream-options.ts +5 -0
- package/src/shared/artifacts.ts +17 -21
- package/src/shared/display-text.ts +150 -0
- package/src/shared/file-coalescer.ts +9 -0
- package/src/shared/fork-context.ts +13 -0
- package/src/shared/formatters.ts +4 -6
- package/src/shared/node-executable.ts +21 -0
- package/src/shared/prompt-resources.ts +51 -0
- package/src/shared/settings.ts +15 -2
- package/src/shared/types.ts +158 -4
- package/src/shared/utf8.ts +11 -0
- package/src/shared/utils.ts +46 -34
- package/src/slash/prompt-workflows.ts +2 -15
- package/src/slash/slash-commands.ts +22 -2
- package/src/tui/fleet-status.ts +29 -17
- package/src/tui/fleet-transcript.ts +1 -48
- package/src/tui/fleet.ts +362 -37
- package/src/tui/render.ts +235 -71
- package/src/watchdog/change-signature.ts +4 -3
- package/src/watchdog/permission-arbiter.ts +2 -1
- package/src/watchdog/review.ts +4 -3
- package/src/workflows/chat-progress.ts +8 -2
- package/src/workflows/scripted-workflow.ts +273 -20
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import type { AgentProgress, ForegroundChildControl, ForegroundRunControl } from "../../shared/types.ts";
|
|
2
|
+
import { registerLivePromptAudit, removeLivePromptAudit, type PromptAuditRerunContract } from "./prompt-audit.ts";
|
|
2
3
|
|
|
3
4
|
interface BeginForegroundChildInput {
|
|
4
5
|
index: number;
|
|
5
6
|
agent: string;
|
|
6
7
|
description?: string;
|
|
8
|
+
authoredTask: string;
|
|
9
|
+
effectivePrompt: string;
|
|
10
|
+
cwd?: string;
|
|
11
|
+
outputPath?: string;
|
|
12
|
+
rerun?: PromptAuditRerunContract;
|
|
7
13
|
model?: string;
|
|
8
14
|
thinking?: string;
|
|
9
15
|
interrupt: () => boolean;
|
|
@@ -109,6 +115,11 @@ export function beginForegroundChild(control: ForegroundRunControl, input: Begin
|
|
|
109
115
|
}
|
|
110
116
|
control.activeChildren ??= new Map();
|
|
111
117
|
control.activeChildren.set(input.index, child);
|
|
118
|
+
registerLivePromptAudit(control, input.index, input.authoredTask, input.effectivePrompt, {
|
|
119
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
120
|
+
...(input.outputPath ? { outputPath: input.outputPath } : {}),
|
|
121
|
+
...(input.rerun ? { rerun: input.rerun } : {}),
|
|
122
|
+
});
|
|
112
123
|
syncCurrentChild(control, child);
|
|
113
124
|
}
|
|
114
125
|
|
|
@@ -121,6 +132,7 @@ export function updateForegroundChild(control: ForegroundRunControl, index: numb
|
|
|
121
132
|
}
|
|
122
133
|
|
|
123
134
|
export function finishForegroundChild(control: ForegroundRunControl, index: number): void {
|
|
135
|
+
removeLivePromptAudit(control, index);
|
|
124
136
|
control.activeChildren?.delete(index);
|
|
125
137
|
if (control.currentIndex === index) {
|
|
126
138
|
const next = [...(control.activeChildren?.values() ?? [])]
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import type { ForegroundResumeChild, ForegroundResumeRun, SubagentState } from "../../shared/types.ts";
|
|
4
|
+
import { DIRS } from "../../shared/types.ts";
|
|
5
|
+
import { writeAtomicJson } from "../../shared/atomic-json.ts";
|
|
6
|
+
import { utf8Tail } from "../../shared/utf8.ts";
|
|
7
|
+
|
|
8
|
+
export const MAX_REMEMBERED_FOREGROUND_RUNS = 50;
|
|
9
|
+
const HISTORY_VERSION = 1;
|
|
10
|
+
const MAX_INLINE_OUTPUT_BYTES = 64 * 1024;
|
|
11
|
+
|
|
12
|
+
interface ForegroundHistoryIndex {
|
|
13
|
+
version: 1;
|
|
14
|
+
runs: ForegroundResumeRun[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function historyPath(resultsDir: string): string {
|
|
18
|
+
return path.join(resultsDir, "foreground-history.json");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function boundedTail(value: string): string {
|
|
22
|
+
return utf8Tail(value, MAX_INLINE_OUTPUT_BYTES).text;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function compactChild(child: ForegroundResumeChild): ForegroundResumeChild {
|
|
26
|
+
const outputPath = child.artifactPaths?.outputPath ?? child.savedOutputPath;
|
|
27
|
+
return {
|
|
28
|
+
agent: child.agent,
|
|
29
|
+
index: child.index,
|
|
30
|
+
...(child.context ? { context: child.context } : {}),
|
|
31
|
+
...(child.sessionFile ? { sessionFile: child.sessionFile } : {}),
|
|
32
|
+
...(child.model ? { model: child.model } : {}),
|
|
33
|
+
...(child.thinking ? { thinking: child.thinking } : {}),
|
|
34
|
+
status: child.status,
|
|
35
|
+
...(child.activityState ? { activityState: child.activityState } : {}),
|
|
36
|
+
...(child.lastActivityAt !== undefined ? { lastActivityAt: child.lastActivityAt } : {}),
|
|
37
|
+
...(child.currentTool ? { currentTool: child.currentTool } : {}),
|
|
38
|
+
...(child.currentToolStartedAt !== undefined ? { currentToolStartedAt: child.currentToolStartedAt } : {}),
|
|
39
|
+
...(child.currentPath ? { currentPath: child.currentPath } : {}),
|
|
40
|
+
...(child.turnCount !== undefined ? { turnCount: child.turnCount } : {}),
|
|
41
|
+
...(child.tokens !== undefined ? { tokens: child.tokens } : {}),
|
|
42
|
+
...(child.toolCount !== undefined ? { toolCount: child.toolCount } : {}),
|
|
43
|
+
...(child.exitCode !== undefined ? { exitCode: child.exitCode } : {}),
|
|
44
|
+
...(child.error ? { error: child.error } : {}),
|
|
45
|
+
...(!outputPath && child.finalOutput ? { finalOutput: boundedTail(child.finalOutput) } : {}),
|
|
46
|
+
...(child.outputState ? { outputState: child.outputState } : {}),
|
|
47
|
+
...(child.outputMode ? { outputMode: child.outputMode } : {}),
|
|
48
|
+
...(child.savedOutputPath ? { savedOutputPath: child.savedOutputPath } : {}),
|
|
49
|
+
...(child.outputSaveError ? { outputSaveError: child.outputSaveError } : {}),
|
|
50
|
+
...(child.artifactPaths ? { artifactPaths: child.artifactPaths } : {}),
|
|
51
|
+
...(child.transcriptPath ? { transcriptPath: child.transcriptPath } : {}),
|
|
52
|
+
...(child.transcriptError ? { transcriptError: child.transcriptError } : {}),
|
|
53
|
+
...(child.acceptance ? { acceptance: child.acceptance } : {}),
|
|
54
|
+
...(child.launchContractDigest ? { launchContractDigest: child.launchContractDigest } : {}),
|
|
55
|
+
...(child.capabilityCeiling ? { capabilityCeiling: child.capabilityCeiling } : {}),
|
|
56
|
+
...(child.updatedAt !== undefined ? { updatedAt: child.updatedAt } : {}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isRestorableForegroundStatus(status: unknown): status is ForegroundResumeChild["status"] {
|
|
61
|
+
return status === "completed" || status === "failed" || status === "paused" || status === "stopped";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function compactRun(run: ForegroundResumeRun): ForegroundResumeRun | undefined {
|
|
65
|
+
if (!run.sessionId) return undefined;
|
|
66
|
+
if (run.children.length === 0 || !run.children.every((child) => isRestorableForegroundStatus(child.status))) return undefined;
|
|
67
|
+
return {
|
|
68
|
+
runId: run.runId,
|
|
69
|
+
mode: run.mode,
|
|
70
|
+
cwd: run.cwd,
|
|
71
|
+
sessionId: run.sessionId,
|
|
72
|
+
updatedAt: run.updatedAt,
|
|
73
|
+
children: run.children.map(compactChild),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function readIndex(resultsDir: string): ForegroundHistoryIndex {
|
|
78
|
+
const filePath = historyPath(resultsDir);
|
|
79
|
+
if (!fs.existsSync(filePath)) return { version: HISTORY_VERSION, runs: [] };
|
|
80
|
+
try {
|
|
81
|
+
const parsed = JSON.parse(fs.readFileSync(filePath, "utf-8")) as unknown;
|
|
82
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return { version: HISTORY_VERSION, runs: [] };
|
|
83
|
+
const record = parsed as Partial<ForegroundHistoryIndex>;
|
|
84
|
+
if (record.version !== HISTORY_VERSION || !Array.isArray(record.runs)) return { version: HISTORY_VERSION, runs: [] };
|
|
85
|
+
return { version: HISTORY_VERSION, runs: record.runs.filter(isRestorableRun) };
|
|
86
|
+
} catch {
|
|
87
|
+
return { version: HISTORY_VERSION, runs: [] };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isRestorableRun(value: unknown): value is ForegroundResumeRun {
|
|
92
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
93
|
+
const run = value as Partial<ForegroundResumeRun>;
|
|
94
|
+
return typeof run.runId === "string" && Boolean(run.runId)
|
|
95
|
+
&& (run.mode === "single" || run.mode === "parallel" || run.mode === "chain")
|
|
96
|
+
&& typeof run.cwd === "string" && Boolean(run.cwd)
|
|
97
|
+
&& typeof run.sessionId === "string" && Boolean(run.sessionId)
|
|
98
|
+
&& typeof run.updatedAt === "number" && Number.isFinite(run.updatedAt)
|
|
99
|
+
&& Array.isArray(run.children)
|
|
100
|
+
&& run.children.length > 0
|
|
101
|
+
&& run.children.every((child) => Boolean(child && typeof child === "object" && !Array.isArray(child)
|
|
102
|
+
&& typeof (child as Partial<ForegroundResumeChild>).agent === "string"
|
|
103
|
+
&& typeof (child as Partial<ForegroundResumeChild>).index === "number"
|
|
104
|
+
&& isRestorableForegroundStatus((child as Partial<ForegroundResumeChild>).status)));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function sortAndBound(runs: ForegroundResumeRun[], limit: number): ForegroundResumeRun[] {
|
|
108
|
+
return [...runs].sort((left, right) => right.updatedAt - left.updatedAt).slice(0, limit);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function persistForegroundRunHistory(state: SubagentState, options: { resultsDir?: string; limit?: number } = {}): void {
|
|
112
|
+
const resultsDir = options.resultsDir ?? DIRS.results;
|
|
113
|
+
const limit = options.limit ?? MAX_REMEMBERED_FOREGROUND_RUNS;
|
|
114
|
+
const existing = readIndex(resultsDir);
|
|
115
|
+
const merged = new Map(existing.runs.map((run) => [run.runId, run]));
|
|
116
|
+
for (const run of state.foregroundRuns?.values() ?? []) {
|
|
117
|
+
const compact = compactRun(run);
|
|
118
|
+
if (compact) merged.set(compact.runId, compact);
|
|
119
|
+
}
|
|
120
|
+
const runs = sortAndBound([...merged.values()], limit);
|
|
121
|
+
writeAtomicJson(historyPath(resultsDir), { version: HISTORY_VERSION, runs });
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function restoreForegroundRunHistory(state: SubagentState, options: { resultsDir?: string; sessionId?: string | null; limit?: number } = {}): number {
|
|
125
|
+
const sessionId = options.sessionId ?? state.currentSessionId;
|
|
126
|
+
if (!sessionId) return 0;
|
|
127
|
+
const index = readIndex(options.resultsDir ?? DIRS.results);
|
|
128
|
+
const runs = sortAndBound(index.runs.filter((run) => run.sessionId === sessionId), options.limit ?? MAX_REMEMBERED_FOREGROUND_RUNS);
|
|
129
|
+
state.foregroundRuns ??= new Map();
|
|
130
|
+
let restored = 0;
|
|
131
|
+
for (const run of runs) {
|
|
132
|
+
if (state.foregroundRuns.has(run.runId)) continue;
|
|
133
|
+
state.foregroundRuns.set(run.runId, run);
|
|
134
|
+
restored += 1;
|
|
135
|
+
}
|
|
136
|
+
return restored;
|
|
137
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Agent, type StreamFn, type ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import { convertToLlm, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { streamSimple } from "@earendil-works/pi-ai/compat";
|
|
4
|
+
import type { Model, ProviderHeaders } from "@earendil-works/pi-ai";
|
|
5
|
+
import { agentStreamOptions } from "../../shared/agent-stream-options.ts";
|
|
6
|
+
import type { ForegroundRunControl } from "../../shared/types.ts";
|
|
7
|
+
export type PromptAuditView = "authored" | "runtime" | "effective";
|
|
8
|
+
|
|
9
|
+
export interface PromptAuditRerunContract {
|
|
10
|
+
params: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface LivePromptAudit {
|
|
14
|
+
authoredTask: string;
|
|
15
|
+
runtimeAdditions: string;
|
|
16
|
+
finalEffectivePrompt: string;
|
|
17
|
+
cwd?: string;
|
|
18
|
+
outputPath?: string;
|
|
19
|
+
rerun?: PromptAuditRerunContract;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const livePrompts = new WeakMap<ForegroundRunControl, Map<number, LivePromptAudit>>();
|
|
23
|
+
|
|
24
|
+
type RegistryModel = Model<any>;
|
|
25
|
+
|
|
26
|
+
function fullModelId(model: Pick<RegistryModel, "provider" | "id">): string {
|
|
27
|
+
return `${model.provider}/${model.id}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function textContent(value: unknown): string {
|
|
31
|
+
if (typeof value === "string") return value;
|
|
32
|
+
if (!Array.isArray(value)) return "";
|
|
33
|
+
return value.map((item) => item && typeof item === "object" && (item as { type?: unknown }).type === "text" ? String((item as { text?: unknown }).text ?? "") : "").join("");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function finalAssistantText(agent: Agent): string {
|
|
37
|
+
for (let index = agent.state.messages.length - 1; index >= 0; index--) {
|
|
38
|
+
const message = agent.state.messages[index];
|
|
39
|
+
if (message && typeof message === "object" && (message as { role?: unknown }).role === "assistant") {
|
|
40
|
+
return textContent((message as { content?: unknown }).content).trim();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return "";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function resolveRewriteAuth(ctx: ExtensionContext, model: RegistryModel): Promise<{ apiKey?: string; headers?: ProviderHeaders; env?: Record<string, string> }> {
|
|
47
|
+
const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
|
|
48
|
+
if (auth.ok === false) throw new Error(`Prompt redo model auth failed for ${fullModelId(model)}: ${auth.error}`);
|
|
49
|
+
return {
|
|
50
|
+
...(auth.apiKey ? { apiKey: auth.apiKey } : {}),
|
|
51
|
+
...(auth.headers ? { headers: auth.headers } : {}),
|
|
52
|
+
...(auth.env ? { env: auth.env } : {}),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function rewritePromptWithGuidance(input: {
|
|
57
|
+
ctx: ExtensionContext;
|
|
58
|
+
authoredTask: string;
|
|
59
|
+
runtimeAdditions: string;
|
|
60
|
+
finalEffectivePrompt: string;
|
|
61
|
+
guidance: string;
|
|
62
|
+
signal?: AbortSignal;
|
|
63
|
+
streamFn?: StreamFn;
|
|
64
|
+
}): Promise<string> {
|
|
65
|
+
const model = input.ctx.model;
|
|
66
|
+
if (!model) throw new Error("Prompt redo needs the current session model to rewrite the authored task.");
|
|
67
|
+
const auth = await resolveRewriteAuth(input.ctx, model);
|
|
68
|
+
const registeredProvider = (input.ctx.modelRegistry as {
|
|
69
|
+
getRegisteredProviderConfig?: (provider: string) => { api?: string; streamSimple?: StreamFn } | undefined;
|
|
70
|
+
}).getRegisteredProviderConfig?.(model.provider);
|
|
71
|
+
const baseStreamFn = input.streamFn ?? (registeredProvider?.streamSimple && registeredProvider.api === model.api
|
|
72
|
+
? registeredProvider.streamSimple
|
|
73
|
+
: streamSimple);
|
|
74
|
+
const streamFn: StreamFn = (nextModel, context, streamOptions) => baseStreamFn(nextModel, context, {
|
|
75
|
+
...streamOptions,
|
|
76
|
+
...(auth.apiKey ? { apiKey: auth.apiKey } : {}),
|
|
77
|
+
env: auth.env || streamOptions?.env ? { ...(auth.env ?? {}), ...(streamOptions?.env ?? {}) } : undefined,
|
|
78
|
+
headers: { ...(streamOptions?.headers ?? {}), ...(auth.headers ?? {}) },
|
|
79
|
+
});
|
|
80
|
+
const ctxThinking = (input.ctx as { getThinkingLevel?: () => ThinkingLevel }).getThinkingLevel?.();
|
|
81
|
+
const agent = new Agent({
|
|
82
|
+
initialState: {
|
|
83
|
+
systemPrompt: [
|
|
84
|
+
"Rewrite one subagent authored task from Prompt Audit context.",
|
|
85
|
+
"Return only the revised authored task text.",
|
|
86
|
+
"Do not add Markdown fences, explanations, labels, or commentary.",
|
|
87
|
+
"Preserve the original intent unless the guidance explicitly changes it.",
|
|
88
|
+
"Do not include runtime additions unless they are needed as ordinary task context.",
|
|
89
|
+
].join("\n"),
|
|
90
|
+
model,
|
|
91
|
+
thinkingLevel: ctxThinking ?? "off",
|
|
92
|
+
tools: [],
|
|
93
|
+
},
|
|
94
|
+
convertToLlm,
|
|
95
|
+
...agentStreamOptions(streamFn),
|
|
96
|
+
getApiKey: (providerName) => providerName === model.provider ? auth.apiKey : undefined,
|
|
97
|
+
toolExecution: "sequential",
|
|
98
|
+
});
|
|
99
|
+
const abort = () => agent.abort();
|
|
100
|
+
input.ctx.signal?.addEventListener("abort", abort, { once: true });
|
|
101
|
+
input.signal?.addEventListener("abort", abort, { once: true });
|
|
102
|
+
try {
|
|
103
|
+
await agent.prompt([
|
|
104
|
+
"Guidance from the human:",
|
|
105
|
+
input.guidance.trim(),
|
|
106
|
+
"",
|
|
107
|
+
"Authored task:",
|
|
108
|
+
input.authoredTask,
|
|
109
|
+
"",
|
|
110
|
+
"Runtime additions for context only:",
|
|
111
|
+
input.runtimeAdditions,
|
|
112
|
+
"",
|
|
113
|
+
"Final effective prompt for context only:",
|
|
114
|
+
input.finalEffectivePrompt,
|
|
115
|
+
].join("\n"));
|
|
116
|
+
} finally {
|
|
117
|
+
input.ctx.signal?.removeEventListener("abort", abort);
|
|
118
|
+
input.signal?.removeEventListener("abort", abort);
|
|
119
|
+
}
|
|
120
|
+
const rewritten = finalAssistantText(agent);
|
|
121
|
+
if (!rewritten) throw new Error("Prompt redo rewrite returned an empty prompt.");
|
|
122
|
+
return rewritten;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function runtimeAdditions(authoredTask: string, effectivePrompt: string): string {
|
|
126
|
+
if (!authoredTask) return effectivePrompt;
|
|
127
|
+
const authoredIndex = effectivePrompt.indexOf(authoredTask);
|
|
128
|
+
if (authoredIndex < 0) return "(runtime additions unavailable)";
|
|
129
|
+
const before = effectivePrompt.slice(0, authoredIndex).trim();
|
|
130
|
+
const after = effectivePrompt.slice(authoredIndex + authoredTask.length).trim();
|
|
131
|
+
return [before, after].filter(Boolean).join("\n\n") || "(none)";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function registerLivePromptAudit(
|
|
135
|
+
control: ForegroundRunControl,
|
|
136
|
+
index: number,
|
|
137
|
+
authoredTask: string,
|
|
138
|
+
effectivePrompt: string,
|
|
139
|
+
metadata: { cwd?: string; outputPath?: string; rerun?: PromptAuditRerunContract } = {},
|
|
140
|
+
): void {
|
|
141
|
+
let prompts = livePrompts.get(control);
|
|
142
|
+
if (!prompts) {
|
|
143
|
+
prompts = new Map();
|
|
144
|
+
livePrompts.set(control, prompts);
|
|
145
|
+
}
|
|
146
|
+
prompts.set(index, {
|
|
147
|
+
authoredTask,
|
|
148
|
+
runtimeAdditions: runtimeAdditions(authoredTask, effectivePrompt),
|
|
149
|
+
finalEffectivePrompt: effectivePrompt,
|
|
150
|
+
...(metadata.cwd ? { cwd: metadata.cwd } : {}),
|
|
151
|
+
...(metadata.outputPath ? { outputPath: metadata.outputPath } : {}),
|
|
152
|
+
...(metadata.rerun ? { rerun: metadata.rerun } : {}),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function updateLiveEffectivePrompt(control: ForegroundRunControl, index: number, effectivePrompt: string): void {
|
|
157
|
+
const prompt = livePrompts.get(control)?.get(index);
|
|
158
|
+
if (!prompt) return;
|
|
159
|
+
prompt.finalEffectivePrompt = effectivePrompt;
|
|
160
|
+
prompt.runtimeAdditions = runtimeAdditions(prompt.authoredTask, effectivePrompt);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function getLivePromptAudit(control: ForegroundRunControl, index: number): LivePromptAudit | undefined {
|
|
164
|
+
return livePrompts.get(control)?.get(index);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function removeLivePromptAudit(control: ForegroundRunControl, index: number): void {
|
|
168
|
+
const prompts = livePrompts.get(control);
|
|
169
|
+
if (!prompts) return;
|
|
170
|
+
prompts.delete(index);
|
|
171
|
+
if (prompts.size === 0) livePrompts.delete(control);
|
|
172
|
+
}
|