Merge ticket/ultra-t3: ultra mode with injected guidance and size guideline (#17)
This commit is contained in:
commit
7cf6a3cc2c
7 files changed
+423
-10
No files matched your search
@@ -62,3 +62,37 @@ export function readUltraNotify(value: string | undefined): { value: boolean; wa
|
||||
if (value === "off") return { value: false };
|
||||
return { value: true, warning: `Invalid PI_SUBAGENT_ULTRA_NOTIFY="${value}"; using on.` };
|
||||
}
|
||||
|
||||
/**
|
||||
* The session's starting ultra-mode state.
|
||||
*
|
||||
* Mode is opt-in: only an explicit on enables it, and an unrecognized value
|
||||
* stays off with a warning. `/ultra on|off` overrides this per session.
|
||||
*/
|
||||
export function readUltraMode(value: string | undefined): { value: boolean; warning?: string } {
|
||||
if (value === undefined || value === "") return { value: false };
|
||||
if (value === "on") return { value: true };
|
||||
if (value === "off") return { value: false };
|
||||
return { value: false, warning: `Invalid PI_SUBAGENT_ULTRACODE="${value}"; using off.` };
|
||||
}
|
||||
|
||||
export type UltraGuideline = "small" | "medium" | "large" | "unrestricted";
|
||||
|
||||
export const DEFAULT_ULTRA_GUIDELINE: UltraGuideline = "medium";
|
||||
|
||||
/**
|
||||
* The advisory plan-size guideline.
|
||||
*
|
||||
* Advice only: it shapes the injected planning guidance and the start result,
|
||||
* never a cap. Invalid values fall back to the default with a warning.
|
||||
*/
|
||||
export function readUltraGuideline(value: string | undefined): { value: UltraGuideline; warning?: string } {
|
||||
if (value === undefined || value === "") return { value: DEFAULT_ULTRA_GUIDELINE };
|
||||
if (value === "small" || value === "medium" || value === "large" || value === "unrestricted") {
|
||||
return { value };
|
||||
}
|
||||
return {
|
||||
value: DEFAULT_ULTRA_GUIDELINE,
|
||||
warning: `Invalid PI_SUBAGENT_ULTRA_GUIDELINE="${value}"; using ${DEFAULT_ULTRA_GUIDELINE}.`,
|
||||
};
|
||||
}
|
||||
+48
-7
@@ -14,7 +14,14 @@ import {
|
||||
getAgentDir,
|
||||
} from "@earendil-works/pi-coding-agent";
|
||||
import { Text } from "@earendil-works/pi-tui";
|
||||
import { readMaxWorkers, readMaxWorkflowItems, readUltraEnabled, readUltraNotify } from "./config.ts";
|
||||
import {
|
||||
readMaxWorkers,
|
||||
readMaxWorkflowItems,
|
||||
readUltraEnabled,
|
||||
readUltraGuideline,
|
||||
readUltraMode,
|
||||
readUltraNotify,
|
||||
} from "./config.ts";
|
||||
import { SubagentDashboard } from "./dashboard.ts";
|
||||
import { formatJob, formatJobList } from "./job-format.ts";
|
||||
import { SubagentManager } from "./manager.ts";
|
||||
@@ -39,6 +46,7 @@ import {
|
||||
type WorkflowParamsType,
|
||||
} from "./schemas.ts";
|
||||
import { type JobSnapshot, type PreparedInvocation, type SubagentDetails, type SyncOutcome, type TaskRunner } from "./types.ts";
|
||||
import { buildUltraGuidance, formatUltraModeStatus, parseUltraCommand, UltraMode } from "./ultra-mode.ts";
|
||||
import { formatUltraReviewCard, formatUltraStart, UltraDeclinedError } from "./ultra.ts";
|
||||
import { buildUltraNotice, ULTRA_DONE_MESSAGE_TYPE, type UltraNoticeDetails } from "./ultra-notify.ts";
|
||||
import { formatWorkflowStart, parseWorkflowCommand, type WorkflowStartResult } from "./workflow.ts";
|
||||
@@ -116,11 +124,19 @@ export default function (pi: ExtensionAPI, taskRunner?: TaskRunner) {
|
||||
const workflowItemsConfig = readMaxWorkflowItems(process.env.PI_SUBAGENT_WORKFLOW_MAX_ITEMS);
|
||||
const ultraConfig = readUltraEnabled(process.env.PI_SUBAGENT_ULTRA);
|
||||
const ultraNotifyConfig = readUltraNotify(process.env.PI_SUBAGENT_ULTRA_NOTIFY);
|
||||
const ultraModeConfig = readUltraMode(process.env.PI_SUBAGENT_ULTRACODE);
|
||||
const ultraGuidelineConfig = readUltraGuideline(process.env.PI_SUBAGENT_ULTRA_GUIDELINE);
|
||||
const ultraMode = new UltraMode(ultraModeConfig.value);
|
||||
let currentContext: ExtensionContext | undefined;
|
||||
let activeDashboard: SubagentDashboard | undefined;
|
||||
let configurationWarnings = [workerConfig.warning, workflowItemsConfig.warning, ultraConfig.warning, ultraNotifyConfig.warning].filter(
|
||||
(warning): warning is string => warning !== undefined,
|
||||
);
|
||||
let configurationWarnings = [
|
||||
workerConfig.warning,
|
||||
workflowItemsConfig.warning,
|
||||
ultraConfig.warning,
|
||||
ultraNotifyConfig.warning,
|
||||
ultraModeConfig.warning,
|
||||
ultraGuidelineConfig.warning,
|
||||
].filter((warning): warning is string => warning !== undefined);
|
||||
|
||||
const updateStatus = (ctx: ExtensionContext | undefined = currentContext): void => {
|
||||
if (!ctx) return;
|
||||
@@ -204,7 +220,7 @@ export default function (pi: ExtensionAPI, taskRunner?: TaskRunner) {
|
||||
const prepared = await prepareWorkflowInvocation(plan, request.args ?? {}, prepareContext, {
|
||||
approvedDirectories: approvedProjectDirs,
|
||||
});
|
||||
if (prepareContext.hasUI) {
|
||||
if (prepareContext.hasUI && !ultraMode.on) {
|
||||
const card = formatUltraReviewCard(prepared);
|
||||
if (!(await ctx.ui.confirm(card.title, card.message))) throw new UltraDeclinedError(plan.name);
|
||||
}
|
||||
@@ -213,6 +229,7 @@ export default function (pi: ExtensionAPI, taskRunner?: TaskRunner) {
|
||||
|
||||
pi.on("session_start", (_event, ctx) => {
|
||||
currentContext = ctx;
|
||||
ultraMode.reset();
|
||||
manager.restore(extractJobSnapshots(ctx.sessionManager.getBranch()));
|
||||
updateStatus(ctx);
|
||||
for (const warning of configurationWarnings) ctx.ui.notify(warning, "warning");
|
||||
@@ -403,8 +420,8 @@ export default function (pi: ExtensionAPI, taskRunner?: TaskRunner) {
|
||||
"Plan shape: steps carry id, agent, task, optional needs, each ({steps.<id>.output}, fan-out over that output's nonblank lines), model, and cwd. The placeholders {args.<name>}, {steps.<id>.output}, and {item} interpolate when a step is released. Declared args are all required and unknown args are rejected. Loops and conditionals are not expressible: chain separate runs instead. Authoring guidance lives in the workflow-authoring skill; read it rather than guessing field names.",
|
||||
"Shapes the engine expresses well: fan-out then fan-in (each-step, then a step that needs it), adversarial verification (a verifier step that needs the producer), generate-and-filter (parallel producers, then a filter step), and a pre-expanded tournament bracket. Classify-and-act and loop-until-done are not expressible in one plan; run per-class or per-iteration plans from the parent. For a large run, start with a small slice first.",
|
||||
"Validation is identical to file plans and happens before anything starts: structure, step-output references, declared arguments, the 64-step cap, the 256-children-per-run cap, and every step agent and cwd. Project-local agents referenced by an inline plan still hit the existing project-agent confirmation.",
|
||||
"With a UI, one review card per start follows validation and precedes the run: it shows the plan, every step with its needs and fan-out source, the child-count estimate against the caps, and every named agent with its user or project source. Declining spawns nothing. No-UI (print/json) sessions never prompt and start directly. Review policy is the extension's, never a tool parameter.",
|
||||
"Returns the run id and expanded plan; the run is an ordinary async job. Retrieve it with subagent_jobs { action: \"get\" | \"wait\", jobId }. When this run finishes, its status and consolidated answer are delivered back as a message that may start a turn; subagent_async and workflow runs stay pull-based, and PI_SUBAGENT_ULTRA_NOTIFY=off restores pure pull for ultra too. Children of an ultra run cannot dispatch further agents or runs. The run's journal records the plan with source \"inline\" and is its only persisted copy.",
|
||||
"With a UI and ultra mode off, one review card per start follows validation and precedes the run: it shows the plan, every step with its needs and fan-out source, the child-count estimate against the caps, and every named agent with its user or project source. Declining spawns nothing. With ultra mode on, the mode is the consent: no card, and the start result still shows the plan. No-UI (print/json) sessions never prompt and start directly. Review policy is the extension's, never a tool parameter.",
|
||||
"Returns the run id and expanded plan and the plan-size guideline; the run is an ordinary async job. Retrieve it with subagent_jobs { action: \"get\" | \"wait\", jobId }. When this run finishes, its status and consolidated answer are delivered back as a message that may start a turn; subagent_async and workflow runs stay pull-based, and PI_SUBAGENT_ULTRA_NOTIFY=off restores pure pull for ultra too. Children of an ultra run cannot dispatch further agents or runs. The run's journal records the plan with source \"inline\" and is its only persisted copy.",
|
||||
].join(" "),
|
||||
parameters: UltraParams,
|
||||
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
||||
@@ -420,6 +437,30 @@ export default function (pi: ExtensionAPI, taskRunner?: TaskRunner) {
|
||||
return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
|
||||
},
|
||||
});
|
||||
|
||||
pi.on("before_agent_start", (event) => {
|
||||
if (ultraMode.on) {
|
||||
event.systemPromptOptions.sections.ultracode = buildUltraGuidance(
|
||||
ultraGuidelineConfig.value,
|
||||
workflowItemsConfig.value,
|
||||
);
|
||||
} else {
|
||||
delete event.systemPromptOptions.sections.ultracode;
|
||||
}
|
||||
});
|
||||
|
||||
pi.registerCommand("ultra", {
|
||||
description: "Ultra mode: /ultra [on|off] — inject ultra planning guidance; bare /ultra shows status",
|
||||
handler: async (args, ctx) => {
|
||||
const command = parseUltraCommand(args);
|
||||
if (command.kind === "usage") {
|
||||
ctx.ui.notify("Usage: /ultra [on|off]", "warning");
|
||||
return;
|
||||
}
|
||||
if (command.kind === "set") ultraMode.set(command.enabled);
|
||||
ctx.ui.notify(formatUltraModeStatus(ultraMode), "info");
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function showSubagents(ctx: ExtensionContext, jobId?: string): Promise<void> {
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* Ultra mode: the per-session mode state and the standing guidance it injects.
|
||||
*
|
||||
* Mode is policy, not machinery. It changes what the model is told and whether
|
||||
* the review card runs; parsing, validation, caps, and execution stay exactly
|
||||
* the workflow engine's. The guidance is a prompt section, so Pi can diff it
|
||||
* across runs instead of accumulating per-run messages.
|
||||
*/
|
||||
|
||||
import { readMaxWorkflowItems, readUltraGuideline, type UltraGuideline } from "./config.ts";
|
||||
import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflows.ts";
|
||||
|
||||
const GUIDELINE_CHILD_LIMIT: Record<UltraGuideline, number | null> = {
|
||||
small: 5,
|
||||
medium: 10,
|
||||
large: 50,
|
||||
unrestricted: null,
|
||||
};
|
||||
|
||||
/** One-line rendering of the guideline, shared by the guidance and the start result. */
|
||||
export function formatUltraGuideline(guideline: UltraGuideline): string {
|
||||
const limit = GUIDELINE_CHILD_LIMIT[guideline];
|
||||
return limit === null ? `${guideline} (no child-count advice)` : `${guideline} (aim for fewer than ${limit} children)`;
|
||||
}
|
||||
|
||||
/** The hard caps, rendered once for both the guidance and the start result. */
|
||||
export function formatUltraCaps(
|
||||
maxItemsPerEach: number = readMaxWorkflowItems(process.env.PI_SUBAGENT_WORKFLOW_MAX_ITEMS).value,
|
||||
): string {
|
||||
return `≤ ${MAX_WORKFLOW_STEPS} steps, ≤ ${maxItemsPerEach} items per each-step, ≤ ${MAX_EXPANDED_CHILDREN} children per run`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `ultracode` prompt section: when to plan, which shapes fit, how big, and
|
||||
* how to consume a run. Advisory throughout — nothing here is enforced.
|
||||
*/
|
||||
export function buildUltraGuidance(guideline: UltraGuideline, maxItemsPerEach?: number): string {
|
||||
return [
|
||||
"Ultra mode is on for this session. Plan with the `ultra` tool when a task is larger than one agent can hold in context, when the same step must run across many items, or when findings should be cross-checked by another agent before the user sees them. A task one agent handles in one pass does not need a plan.",
|
||||
"",
|
||||
"Patterns and their declarative shapes:",
|
||||
"- Fan-out and synthesize → an each-step over the items, then a fan-in step that needs it.",
|
||||
"- Adversarial verification → a verify step that needs the producer step.",
|
||||
"- Generate and filter → parallel producer steps, then a filter fan-in step.",
|
||||
"- Tournament → pre-expanded competitor steps and judge fan-ins; a bracket cannot loop.",
|
||||
"Classify-and-act and loop-until-done are not expressible in one plan: chain runs instead — one run to understand, one to change, one to verify.",
|
||||
"",
|
||||
`Size guideline: ${formatUltraGuideline(guideline)}. Hard caps: ${formatUltraCaps(maxItemsPerEach)}.`,
|
||||
"",
|
||||
"Start a run, then wait for it with `subagent_jobs { action: \"wait\", jobId }` before continuing. The run's final step output is the consolidated answer; children of an ultra run cannot dispatch further agents.",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
/** The guideline and hard caps, read at format time rather than baked at registration. */
|
||||
export function formatUltraGuidelineLine(
|
||||
guideline: UltraGuideline = readUltraGuideline(process.env.PI_SUBAGENT_ULTRA_GUIDELINE).value,
|
||||
maxItemsPerEach?: number,
|
||||
): string {
|
||||
return `Size guideline: ${formatUltraGuideline(guideline)}; hard caps: ${formatUltraCaps(maxItemsPerEach)}.`;
|
||||
}
|
||||
|
||||
/** In-memory, per-session mode state; the env value is only the starting point. */
|
||||
export class UltraMode {
|
||||
private readonly defaultValue: boolean;
|
||||
private enabled: boolean;
|
||||
|
||||
constructor(defaultValue: boolean) {
|
||||
this.defaultValue = defaultValue;
|
||||
this.enabled = defaultValue;
|
||||
}
|
||||
|
||||
get on(): boolean {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
set(enabled: boolean): void {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this.enabled = this.defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
export type UltraCommand = { kind: "status" } | { kind: "set"; enabled: boolean } | { kind: "usage" };
|
||||
|
||||
/** `/ultra`, `/ultra status`, `/ultra on`, `/ultra off`; anything else is a usage error. */
|
||||
export function parseUltraCommand(args: string): UltraCommand {
|
||||
const parts = args.trim().split(/\s+/).filter(Boolean);
|
||||
if (parts.length === 0 || (parts.length === 1 && parts[0] === "status")) return { kind: "status" };
|
||||
if (parts.length === 1 && (parts[0] === "on" || parts[0] === "off")) {
|
||||
return { kind: "set", enabled: parts[0] === "on" };
|
||||
}
|
||||
return { kind: "usage" };
|
||||
}
|
||||
|
||||
export function formatUltraModeStatus(mode: UltraMode): string {
|
||||
return `Ultra mode is ${mode.on ? "on" : "off"}.`;
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import { readMaxWorkflowItems } from "./config.ts";
|
||||
import { formatUltraGuidelineLine } from "./ultra-mode.ts";
|
||||
import type { PreparedWorkflowInvocation, WorkflowStartResult } from "./workflow.ts";
|
||||
import { MAX_EXPANDED_CHILDREN, MAX_WORKFLOW_STEPS } from "./workflows.ts";
|
||||
|
||||
@@ -106,6 +107,7 @@ export function formatUltraStart(result: WorkflowStartResult): string {
|
||||
lines.push(`- ${step.id} (${step.agent})${needs}${each}`);
|
||||
}
|
||||
lines.push(
|
||||
formatUltraGuidelineLine(),
|
||||
`Use subagent_jobs { action: "get" | "wait", jobId: "${result.id}" } to retrieve it.`,
|
||||
`The run's journal (plan source "inline") is the only persisted copy of this plan.`,
|
||||
);
|
||||
|
||||
+35
-1
@@ -1,6 +1,13 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { readMaxWorkflowItems, readMaxWorkers, readUltraEnabled, readUltraNotify } from "../src/config.ts";
|
||||
import {
|
||||
readMaxWorkflowItems,
|
||||
readMaxWorkers,
|
||||
readUltraEnabled,
|
||||
readUltraGuideline,
|
||||
readUltraMode,
|
||||
readUltraNotify,
|
||||
} from "../src/config.ts";
|
||||
|
||||
test("worker configuration defaults to 16 and accepts only integers from 1 through 32", () => {
|
||||
assert.deepEqual(readMaxWorkers(undefined), { value: 16 });
|
||||
@@ -49,3 +56,30 @@ test("ultra notices default on, disable only on an explicit off, and warn on inv
|
||||
assert.match(result.warning ?? "", /PI_SUBAGENT_ULTRA_NOTIFY/);
|
||||
}
|
||||
});
|
||||
|
||||
test("ultra mode defaults off, accepts on/off, and warns on invalid values", () => {
|
||||
assert.deepEqual(readUltraMode(undefined), { value: false });
|
||||
assert.deepEqual(readUltraMode(""), { value: false });
|
||||
assert.deepEqual(readUltraMode("off"), { value: false });
|
||||
assert.deepEqual(readUltraMode("on"), { value: true });
|
||||
for (const invalid of ["true", "ON", "0", " maybe", "enabled"]) {
|
||||
const result = readUltraMode(invalid);
|
||||
assert.equal(result.value, false);
|
||||
assert.match(result.warning ?? "", /PI_SUBAGENT_ULTRACODE/);
|
||||
assert.match(result.warning ?? "", /using off/);
|
||||
}
|
||||
});
|
||||
|
||||
test("ultra size guideline defaults to medium, accepts the four values, and warns on invalid values", () => {
|
||||
assert.deepEqual(readUltraGuideline(undefined), { value: "medium" });
|
||||
assert.deepEqual(readUltraGuideline(""), { value: "medium" });
|
||||
for (const value of ["small", "medium", "large", "unrestricted"] as const) {
|
||||
assert.deepEqual(readUltraGuideline(value), { value });
|
||||
}
|
||||
for (const invalid of ["tiny", "Medium", "0", " maybe", "huge"]) {
|
||||
const result = readUltraGuideline(invalid);
|
||||
assert.equal(result.value, "medium");
|
||||
assert.match(result.warning ?? "", /PI_SUBAGENT_ULTRA_GUIDELINE/);
|
||||
assert.match(result.warning ?? "", /using medium/);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import {
|
||||
UltraMode,
|
||||
buildUltraGuidance,
|
||||
formatUltraGuideline,
|
||||
formatUltraGuidelineLine,
|
||||
parseUltraCommand,
|
||||
} from "../src/ultra-mode.ts";
|
||||
|
||||
test("the size guideline maps each value to its child limit and unrestricted gives no advice", () => {
|
||||
assert.equal(formatUltraGuideline("small"), "small (aim for fewer than 5 children)");
|
||||
assert.equal(formatUltraGuideline("medium"), "medium (aim for fewer than 10 children)");
|
||||
assert.equal(formatUltraGuideline("large"), "large (aim for fewer than 50 children)");
|
||||
assert.equal(formatUltraGuideline("unrestricted"), "unrestricted (no child-count advice)");
|
||||
assert.doesNotMatch(formatUltraGuideline("unrestricted"), /fewer than/);
|
||||
});
|
||||
|
||||
test("the guideline line is read when formatted, not baked at import time", () => {
|
||||
const previous = process.env.PI_SUBAGENT_ULTRA_GUIDELINE;
|
||||
try {
|
||||
process.env.PI_SUBAGENT_ULTRA_GUIDELINE = "small";
|
||||
assert.equal(
|
||||
formatUltraGuidelineLine(),
|
||||
"Size guideline: small (aim for fewer than 5 children); hard caps: ≤ 64 steps, ≤ 32 items per each-step, ≤ 256 children per run.",
|
||||
);
|
||||
process.env.PI_SUBAGENT_ULTRA_GUIDELINE = "unrestricted";
|
||||
assert.match(formatUltraGuidelineLine(), /Size guideline: unrestricted \(no child-count advice\)/);
|
||||
process.env.PI_SUBAGENT_ULTRA_GUIDELINE = "bogus";
|
||||
assert.match(formatUltraGuidelineLine(), /Size guideline: medium \(aim for fewer than 10 children\)/);
|
||||
} finally {
|
||||
if (previous === undefined) delete process.env.PI_SUBAGENT_ULTRA_GUIDELINE;
|
||||
else process.env.PI_SUBAGENT_ULTRA_GUIDELINE = previous;
|
||||
}
|
||||
});
|
||||
|
||||
test("the guidance names the heuristics, every pattern mapping, the guideline, caps, and how to consume a run", () => {
|
||||
const guidance = buildUltraGuidance("medium", 32);
|
||||
for (const expected of [
|
||||
/`ultra` tool/,
|
||||
/larger than one agent can hold in context/,
|
||||
/same step must run across many items/,
|
||||
/cross-checked by another agent/,
|
||||
/Fan-out and synthesize → an each-step/,
|
||||
/Adversarial verification → a verify step that needs the producer/,
|
||||
/Generate and filter → parallel producer steps/,
|
||||
/Tournament → pre-expanded competitor steps/,
|
||||
/Classify-and-act and loop-until-done are not expressible in one plan/,
|
||||
/chain runs instead — one run to understand, one to change, one to verify/,
|
||||
/Size guideline: medium \(aim for fewer than 10 children\)/,
|
||||
/Hard caps: ≤ 64 steps, ≤ 32 items per each-step, ≤ 256 children per run/,
|
||||
/wait for it with `subagent_jobs/,
|
||||
/final step output is the consolidated answer/,
|
||||
]) {
|
||||
assert.match(guidance, expected);
|
||||
}
|
||||
assert.equal(buildUltraGuidance("medium", 32), guidance);
|
||||
});
|
||||
|
||||
test("mode state starts from its default, toggles, and resets", () => {
|
||||
const mode = new UltraMode(false);
|
||||
assert.equal(mode.on, false);
|
||||
mode.set(true);
|
||||
assert.equal(mode.on, true);
|
||||
mode.reset();
|
||||
assert.equal(mode.on, false);
|
||||
assert.equal(new UltraMode(true).on, true);
|
||||
});
|
||||
|
||||
test("/ultra parses bare status, on, off, and usage errors", () => {
|
||||
assert.deepEqual(parseUltraCommand(""), { kind: "status" });
|
||||
assert.deepEqual(parseUltraCommand(" "), { kind: "status" });
|
||||
assert.deepEqual(parseUltraCommand("status"), { kind: "status" });
|
||||
assert.deepEqual(parseUltraCommand("on"), { kind: "set", enabled: true });
|
||||
assert.deepEqual(parseUltraCommand(" off "), { kind: "set", enabled: false });
|
||||
for (const args of ["on extra", "off extra", "ON", "sometimes"]) {
|
||||
assert.deepEqual(parseUltraCommand(args), { kind: "usage" });
|
||||
}
|
||||
});
|
||||
+126
-2
@@ -55,6 +55,7 @@ async function harness(options: HarnessOptions = {}) {
|
||||
|
||||
const runner = new FakeRunner();
|
||||
const tools = new Map<string, { name: string; description?: string; execute: Execute }>();
|
||||
const commands = new Map<string, (args: string, ctx: ExtensionToolContext) => Promise<void>>();
|
||||
const events = new Map<string, (event: any, ctx: ExtensionToolContext) => unknown>();
|
||||
const entries: unknown[] = [];
|
||||
const notifications: string[] = [];
|
||||
@@ -66,7 +67,9 @@ async function harness(options: HarnessOptions = {}) {
|
||||
registerTool(definition: { name: string; description?: string; execute: Execute }) {
|
||||
tools.set(definition.name, definition);
|
||||
},
|
||||
registerCommand() {},
|
||||
registerCommand(name: string, definition: { handler: (args: string, ctx: ExtensionToolContext) => Promise<void> }) {
|
||||
commands.set(name, definition.handler);
|
||||
},
|
||||
registerEntryRenderer() {},
|
||||
registerMessageRenderer() {},
|
||||
on(name: string, handler: (event: any, ctx: ExtensionToolContext) => unknown) {
|
||||
@@ -101,7 +104,7 @@ async function harness(options: HarnessOptions = {}) {
|
||||
},
|
||||
} as unknown as ExtensionToolContext;
|
||||
|
||||
return { cwd, runner, tools, events, entries, notifications, confirmations, ctx };
|
||||
return { cwd, runner, tools, commands, events, entries, notifications, confirmations, ctx };
|
||||
}
|
||||
|
||||
function textOf(result: AgentToolResult<any>): string {
|
||||
@@ -452,3 +455,124 @@ test("the ultra tool exposes plan/args/cwd and a token-budgeted description", as
|
||||
await fs.promises.rm(h.cwd, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
function promptEvent(sections: Record<string, string> = {}) {
|
||||
return {
|
||||
messageType: "user",
|
||||
prompt: "a prompt",
|
||||
images: [],
|
||||
systemPrompt: "",
|
||||
systemPromptOptions: { sections },
|
||||
};
|
||||
}
|
||||
|
||||
test("/ultra toggles the mode, reports bare status, defaults off, and rejects other words", async () => {
|
||||
const h = await harness();
|
||||
try {
|
||||
await h.commands.get("ultra")!("", h.ctx);
|
||||
assert.equal(h.notifications.at(-1), "Ultra mode is off.");
|
||||
await h.commands.get("ultra")!("on", h.ctx);
|
||||
assert.equal(h.notifications.at(-1), "Ultra mode is on.");
|
||||
await h.commands.get("ultra")!("", h.ctx);
|
||||
assert.equal(h.notifications.at(-1), "Ultra mode is on.");
|
||||
await h.commands.get("ultra")!("off", h.ctx);
|
||||
assert.equal(h.notifications.at(-1), "Ultra mode is off.");
|
||||
await h.commands.get("ultra")!("sometimes", h.ctx);
|
||||
assert.equal(h.notifications.at(-1), "Usage: /ultra [on|off]");
|
||||
assert.equal(h.confirmations.length, 0);
|
||||
} finally {
|
||||
await h.events.get("session_shutdown")?.({ type: "session_shutdown" }, h.ctx);
|
||||
await fs.promises.rm(h.cwd, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("the PI_SUBAGENT_ULTRACODE default is respected for on and off", async () => {
|
||||
const previous = process.env.PI_SUBAGENT_ULTRACODE;
|
||||
try {
|
||||
for (const setting of ["on", "off"] as const) {
|
||||
process.env.PI_SUBAGENT_ULTRACODE = setting;
|
||||
const h = await harness();
|
||||
try {
|
||||
await h.commands.get("ultra")!("", h.ctx);
|
||||
assert.equal(h.notifications.at(-1), `Ultra mode is ${setting}.`);
|
||||
} finally {
|
||||
await h.events.get("session_shutdown")?.({ type: "session_shutdown" }, h.ctx);
|
||||
await fs.promises.rm(h.cwd, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (previous === undefined) delete process.env.PI_SUBAGENT_ULTRACODE;
|
||||
else process.env.PI_SUBAGENT_ULTRACODE = previous;
|
||||
}
|
||||
});
|
||||
|
||||
test("the guidance section is present exactly while the mode is on and stable across consecutive runs", async () => {
|
||||
const h = await harness();
|
||||
try {
|
||||
const off = promptEvent();
|
||||
await h.events.get("before_agent_start")!(off, h.ctx);
|
||||
assert.equal("ultracode" in off.systemPromptOptions.sections, false);
|
||||
|
||||
await h.commands.get("ultra")!("on", h.ctx);
|
||||
const first = promptEvent();
|
||||
await h.events.get("before_agent_start")!(first, h.ctx);
|
||||
const guidance = first.systemPromptOptions.sections.ultracode;
|
||||
assert.equal(typeof guidance, "string");
|
||||
assert.match(guidance, /Ultra mode is on/);
|
||||
assert.match(guidance, /`ultra` tool/);
|
||||
assert.match(guidance, /Size guideline: medium \(aim for fewer than 10 children\)/);
|
||||
assert.match(guidance, /Hard caps: ≤ 64 steps, ≤ 32 items per each-step, ≤ 256 children per run/);
|
||||
assert.match(guidance, /final step output is the consolidated answer/);
|
||||
|
||||
const second = promptEvent();
|
||||
await h.events.get("before_agent_start")!(second, h.ctx);
|
||||
assert.equal(second.systemPromptOptions.sections.ultracode, guidance);
|
||||
|
||||
await h.commands.get("ultra")!("off", h.ctx);
|
||||
const reused = promptEvent({ ultracode: guidance });
|
||||
await h.events.get("before_agent_start")!(reused, h.ctx);
|
||||
assert.equal("ultracode" in reused.systemPromptOptions.sections, false);
|
||||
} finally {
|
||||
await h.events.get("session_shutdown")?.({ type: "session_shutdown" }, h.ctx);
|
||||
await fs.promises.rm(h.cwd, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("mode on is the consent: the review card is skipped while off still shows it, and the start result names the guideline", async () => {
|
||||
const h = await harness();
|
||||
try {
|
||||
await h.commands.get("ultra")!("on", h.ctx);
|
||||
const onResult = await h.tools.get("ultra")!.execute(
|
||||
"ultra",
|
||||
{ plan: inlinePlan([{ id: "scan", agent: "scout", task: "scan while on" }], { name: "consent-plan" }) },
|
||||
undefined,
|
||||
undefined,
|
||||
h.ctx,
|
||||
);
|
||||
assert.notEqual(onResult.isError, true);
|
||||
assert.equal(h.confirmations.length, 0);
|
||||
assert.equal(h.runner.calls.length, 1);
|
||||
const onText = textOf(onResult);
|
||||
assert.match(onText, /Inline plan: consent-plan/);
|
||||
assert.match(
|
||||
onText,
|
||||
/Size guideline: medium \(aim for fewer than 10 children\); hard caps: ≤ 64 steps, ≤ 32 items per each-step, ≤ 256 children per run\./,
|
||||
);
|
||||
|
||||
await h.commands.get("ultra")!("off", h.ctx);
|
||||
const offResult = await h.tools.get("ultra")!.execute(
|
||||
"ultra",
|
||||
{ plan: inlinePlan([{ id: "scan", agent: "scout", task: "scan while off" }], { name: "card-plan" }) },
|
||||
undefined,
|
||||
undefined,
|
||||
h.ctx,
|
||||
);
|
||||
assert.notEqual(offResult.isError, true);
|
||||
assert.equal(h.confirmations.length, 1);
|
||||
assert.match(h.confirmations[0].title, /Run inline plan "card-plan"\?/);
|
||||
assert.equal(h.runner.calls.length, 2);
|
||||
} finally {
|
||||
await h.events.get("session_shutdown")?.({ type: "session_shutdown" }, h.ctx);
|
||||
await fs.promises.rm(h.cwd, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user