ceulen 0.5.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/CHANGELOG.md +317 -12
- package/README.md +87 -91
- package/extensions/lib/advisor-marker.ts +33 -0
- package/extensions/lib/panel.ts +25 -2
- package/extensions/lib/rate.ts +24 -0
- package/extensions/lib/registry.ts +93 -7
- package/extensions/lib/tools.ts +47 -0
- package/extensions/lib/usage-store.ts +33 -0
- package/extensions/modules/advisor/commands/advisor.ts +244 -0
- package/extensions/modules/advisor/configPanel.ts +194 -0
- package/extensions/modules/advisor/index.ts +217 -0
- package/extensions/modules/advisor/lib/config.ts +248 -0
- package/extensions/modules/advisor/lib/emission-guard.ts +153 -0
- package/extensions/modules/advisor/lib/isolated-model.ts +160 -0
- package/extensions/modules/advisor/lib/model-picker.ts +47 -0
- package/extensions/modules/advisor/lib/watcher.ts +286 -0
- package/extensions/modules/advisor/test/compliance.test.ts +125 -0
- package/extensions/modules/advisor/test/config.test.ts +202 -0
- package/extensions/modules/advisor/test/configPanel.test.ts +223 -0
- package/extensions/modules/advisor/test/emission-guard.test.ts +201 -0
- package/extensions/modules/advisor/test/isolated-model.test.ts +311 -0
- package/extensions/modules/advisor/test/model-picker.test.ts +32 -0
- package/extensions/modules/advisor/test/module.test.ts +586 -0
- package/extensions/modules/advisor/test/watcher.test.ts +478 -0
- package/extensions/modules/classifier/configPanel.ts +104 -0
- package/extensions/modules/classifier/index.ts +297 -0
- package/extensions/modules/classifier/lib/settings.ts +95 -0
- package/extensions/modules/classifier/test/classifier.test.ts +475 -0
- package/extensions/modules/composer/index.ts +152 -3
- package/extensions/modules/composer/lib/footer.ts +119 -0
- package/extensions/modules/composer/lib/shapes.ts +265 -26
- package/extensions/modules/composer/test/editor.test.ts +1 -1
- package/extensions/modules/composer/test/footer.test.ts +96 -0
- package/extensions/modules/composer/test/module.test.ts +113 -11
- package/extensions/modules/composer/test/screen.test.ts +29 -1
- package/extensions/modules/composer/test/shapes.test.ts +224 -8
- package/extensions/modules/config/defaultTools.ts +96 -0
- package/extensions/modules/config/index.ts +147 -7
- package/extensions/modules/config/piSettings.ts +254 -69
- package/extensions/modules/config/test/defaultTools.test.ts +122 -0
- package/extensions/modules/config/test/menu.test.ts +64 -0
- package/extensions/modules/config/test/panel.test.ts +38 -23
- package/extensions/modules/config/test/piSettings.test.ts +130 -3
- package/extensions/modules/config/test/renderTools.test.ts +71 -0
- package/extensions/modules/config/test/tools.test.ts +146 -0
- package/extensions/modules/fff/index.ts +2094 -0
- package/extensions/modules/fff/lib/query.ts +129 -0
- package/extensions/modules/fff/test/index.test.ts +607 -0
- package/extensions/modules/fff/test/query.test.ts +71 -0
- package/extensions/modules/munin/configPanel.ts +105 -0
- package/extensions/modules/munin/index.ts +641 -0
- package/extensions/modules/munin/lib/helpers.ts +471 -0
- package/extensions/modules/munin/lib/retry.ts +20 -0
- package/extensions/modules/munin/lib/sdk.ts +218 -0
- package/extensions/modules/munin/test/config.test.ts +168 -0
- package/extensions/modules/munin/test/configPanel.test.ts +161 -0
- package/extensions/modules/munin/test/extension.test.ts +224 -0
- package/extensions/modules/munin/test/helpers.test.ts +523 -0
- package/extensions/modules/munin/test/retry.test.ts +145 -0
- package/extensions/modules/munin/test/stale-protocol.test.ts +554 -0
- package/extensions/modules/plan/configPanel.ts +152 -0
- package/extensions/modules/plan/index.ts +917 -0
- package/extensions/modules/plan/lib/plan-tools.ts +72 -0
- package/extensions/modules/plan/lib/prompt.ts +65 -0
- package/extensions/modules/plan/lib/settings.ts +166 -0
- package/extensions/modules/plan/lib/shell-gate.ts +200 -0
- package/extensions/modules/plan/test/configPanel.test.ts +96 -0
- package/extensions/modules/plan/test/harness.ts +133 -0
- package/extensions/modules/plan/test/module.test.ts +305 -0
- package/extensions/modules/plan/test/settings.test.ts +114 -0
- package/extensions/modules/plan/test/shell-gate.test.ts +103 -0
- package/extensions/modules/ponytail/index.ts +23 -73
- package/extensions/modules/ponytail/lib/config.ts +1 -6
- package/extensions/modules/ponytail/test/extension.test.ts +1 -85
- package/extensions/modules/repair/configPanel.ts +96 -0
- package/extensions/modules/repair/index.ts +414 -0
- package/extensions/modules/repair/lib/auto-bg.ts +333 -0
- package/extensions/modules/repair/lib/edit-repair.ts +155 -0
- package/extensions/modules/repair/lib/editor.ts +251 -0
- package/extensions/modules/repair/lib/guards.ts +34 -0
- package/extensions/modules/repair/lib/input-repair.ts +324 -0
- package/extensions/modules/repair/lib/patch.ts +481 -0
- package/extensions/modules/repair/lib/settings.ts +114 -0
- package/extensions/modules/repair/test/auto-bg.test.ts +405 -0
- package/extensions/modules/repair/test/edit-repair.test.ts +224 -0
- package/extensions/modules/repair/test/editor.test.ts +141 -0
- package/extensions/modules/repair/test/guards.test.ts +64 -0
- package/extensions/modules/repair/test/input-repair.test.ts +231 -0
- package/extensions/modules/repair/test/patch.test.ts +563 -0
- package/extensions/modules/repair/test/settings.test.ts +167 -0
- package/extensions/modules/repair/test/wrapper.test.ts +210 -0
- package/extensions/modules/router/commands/commands.ts +4 -0
- package/extensions/modules/router/lib/client.ts +61 -0
- package/extensions/modules/router/lib/provider.ts +34 -13
- package/extensions/modules/router/lib/systemone.ts +152 -0
- package/extensions/modules/router/test/unit.test.ts +179 -0
- package/extensions/modules/rtk/findFallback.js +68 -0
- package/extensions/modules/rtk/index.ts +248 -0
- package/extensions/modules/rtk/safe-rewrite.js +74 -0
- package/extensions/modules/rtk/test/findFallback.test.js +66 -0
- package/extensions/modules/rtk/test/index.test.js +131 -0
- package/extensions/modules/rtk/test/safe-rewrite.test.js +96 -0
- package/extensions/modules/rtk/version-gate.js +34 -0
- package/extensions/modules/serena/index.ts +629 -0
- package/extensions/modules/serena/lib/detect.ts +73 -0
- package/extensions/modules/serena/lib/env.ts +76 -0
- package/extensions/modules/serena/lib/guidance.ts +26 -0
- package/extensions/modules/serena/lib/normalize.ts +27 -0
- package/extensions/modules/serena/lib/symbol-key.ts +20 -0
- package/extensions/modules/serena/lib/truncate.ts +20 -0
- package/extensions/modules/serena/test/env.test.ts +122 -0
- package/extensions/modules/serena/test/index.test.ts +347 -0
- package/extensions/modules/serena/test/worker.test.ts +562 -0
- package/extensions/modules/serena/worker.ts +858 -0
- package/extensions/modules/steering/configPanel.ts +122 -0
- package/extensions/modules/steering/index.ts +616 -0
- package/extensions/modules/steering/lib/anchor.ts +175 -0
- package/extensions/modules/steering/lib/errors.ts +253 -0
- package/extensions/modules/steering/lib/family.ts +33 -0
- package/extensions/modules/steering/lib/guidance.ts +172 -0
- package/extensions/modules/steering/lib/history.ts +170 -0
- package/extensions/modules/steering/lib/settings.ts +127 -0
- package/extensions/modules/steering/test/anchor-regressions.test.ts +93 -0
- package/extensions/modules/steering/test/anchor-wiring.test.ts +99 -0
- package/extensions/modules/steering/test/anchor.test.ts +160 -0
- package/extensions/modules/steering/test/cache-stats.test.ts +43 -0
- package/extensions/modules/steering/test/errors.test.ts +309 -0
- package/extensions/modules/steering/test/family.test.ts +43 -0
- package/extensions/modules/steering/test/guidance.test.ts +357 -0
- package/extensions/modules/steering/test/harness.ts +93 -0
- package/extensions/modules/steering/test/history.test.ts +118 -0
- package/extensions/modules/steering/test/settings.test.ts +182 -0
- package/extensions/modules/steering/test/status.test.ts +71 -0
- package/extensions/modules/steering/test/toolCall.test.ts +99 -0
- package/extensions/modules/subagent/agents/planner.md +14 -0
- package/extensions/modules/subagent/agents/reviewer.md +42 -0
- package/extensions/modules/subagent/agents/scout.md +47 -0
- package/extensions/modules/subagent/agents/tester.md +12 -0
- package/extensions/modules/subagent/agents/worker.md +18 -0
- package/extensions/modules/subagent/configPanel.ts +219 -0
- package/extensions/modules/subagent/index.ts +2451 -0
- package/extensions/modules/subagent/lib/agents.ts +408 -0
- package/extensions/modules/subagent/lib/background.ts +383 -0
- package/extensions/modules/subagent/lib/herdr.ts +1019 -0
- package/extensions/modules/subagent/lib/history.ts +126 -0
- package/extensions/modules/subagent/lib/model.ts +156 -0
- package/extensions/modules/subagent/lib/render.ts +335 -0
- package/extensions/modules/subagent/lib/result.ts +110 -0
- package/extensions/modules/subagent/lib/roles.ts +296 -0
- package/extensions/modules/subagent/lib/routing.ts +223 -0
- package/extensions/modules/subagent/lib/runner.ts +559 -0
- package/extensions/modules/subagent/lib/schema.ts +21 -0
- package/extensions/modules/subagent/lib/security.ts +752 -0
- package/extensions/modules/subagent/lib/settings.ts +159 -0
- package/extensions/modules/subagent/lib/thread-viewer.ts +319 -0
- package/extensions/modules/subagent/lib/threads.ts +135 -0
- package/extensions/modules/subagent/lib/types.ts +18 -0
- package/extensions/modules/subagent/lib/widget.ts +475 -0
- package/extensions/modules/subagent/test/configPanel.test.ts +132 -0
- package/extensions/modules/subagent/test/herdr-advisor.test.ts +40 -0
- package/extensions/modules/subagent/test/routing.test.ts +350 -0
- package/extensions/modules/subagent/test/widget.test.ts +208 -0
- package/extensions/modules/usage/index.ts +59 -16
- package/extensions/modules/usage/test/genrate.test.ts +70 -0
- package/extensions/modules/usage/test/parsers.test.ts +13 -0
- package/extensions/modules/ux/UPSTREAM +24 -0
- package/extensions/modules/ux/index.ts +393 -0
- package/extensions/modules/ux/lib/audit.ts +562 -0
- package/extensions/modules/ux/lib/config.ts +136 -0
- package/extensions/modules/ux/lib/instructions.ts +77 -0
- package/extensions/modules/ux/test/audit.test.ts +756 -0
- package/extensions/modules/ux/test/extension.test.ts +394 -0
- package/extensions/modules/ux/test/instructions.test.ts +19 -0
- package/extensions/modules/web/configPanel.ts +193 -0
- package/extensions/modules/web/index.ts +978 -0
- package/extensions/modules/web/lib/agy.ts +209 -0
- package/extensions/modules/web/lib/brave.ts +42 -0
- package/extensions/modules/web/lib/cdp.ts +686 -0
- package/extensions/modules/web/lib/chatapi.ts +101 -0
- package/extensions/modules/web/lib/chatgpt.ts +796 -0
- package/extensions/modules/web/lib/chrome.ts +328 -0
- package/extensions/modules/web/lib/config.ts +136 -0
- package/extensions/modules/web/lib/content.ts +99 -0
- package/extensions/modules/web/lib/crawl4ai.ts +172 -0
- package/extensions/modules/web/lib/extract.ts +235 -0
- package/extensions/modules/web/lib/firecrawl.ts +56 -0
- package/extensions/modules/web/lib/format.ts +197 -0
- package/extensions/modules/web/lib/gemini-auth.ts +286 -0
- package/extensions/modules/web/lib/gemini-dr.ts +338 -0
- package/extensions/modules/web/lib/gemini.ts +551 -0
- package/extensions/modules/web/lib/imageapi.ts +559 -0
- package/extensions/modules/web/lib/retry.ts +71 -0
- package/extensions/modules/web/lib/search.ts +237 -0
- package/extensions/modules/web/lib/searxng.ts +69 -0
- package/extensions/modules/web/lib/settings.ts +148 -0
- package/extensions/modules/web/scripts/cdp-smoke.ts +77 -0
- package/extensions/modules/web/scripts/gemini-smoke.ts +107 -0
- package/extensions/modules/web/test/agy.test.ts +383 -0
- package/extensions/modules/web/test/cdp.test.ts +717 -0
- package/extensions/modules/web/test/chat.test.ts +171 -0
- package/extensions/modules/web/test/chatgpt.test.ts +633 -0
- package/extensions/modules/web/test/chrome.test.ts +282 -0
- package/extensions/modules/web/test/config.test.ts +218 -0
- package/extensions/modules/web/test/configPanel.test.ts +132 -0
- package/extensions/modules/web/test/crawl.test.ts +134 -0
- package/extensions/modules/web/test/crawl4ai.test.ts +105 -0
- package/extensions/modules/web/test/extension.test.ts +118 -0
- package/extensions/modules/web/test/extract.test.ts +436 -0
- package/extensions/modules/web/test/fixtures/dr-confirm.bin +12 -0
- package/extensions/modules/web/test/fixtures/dr-plan.bin +20 -0
- package/extensions/modules/web/test/fixtures/interaction.html +31 -0
- package/extensions/modules/web/test/format.test.ts +230 -0
- package/extensions/modules/web/test/gemini-auth.test.ts +465 -0
- package/extensions/modules/web/test/gemini-dr.test.ts +249 -0
- package/extensions/modules/web/test/gemini.test.ts +472 -0
- package/extensions/modules/web/test/hook.test.ts +101 -0
- package/extensions/modules/web/test/image.test.ts +1089 -0
- package/extensions/modules/web/test/retry.test.ts +119 -0
- package/extensions/modules/web/test/screenshot.test.ts +109 -0
- package/extensions/modules/web/test/search.test.ts +210 -0
- package/extensions/modules/web/test/settings.test.ts +165 -0
- package/extensions/modules/web/test/skill.test.ts +72 -0
- package/extensions/modules/web/types.d.ts +3 -0
- package/extensions/modules/web/vendor/package.json +1 -0
- package/extensions/modules/web/vendor/readability/LICENSE.md +13 -0
- package/extensions/modules/web/vendor/readability/Readability-readerable.js +123 -0
- package/extensions/modules/web/vendor/readability/Readability.js +2787 -0
- package/extensions/modules/web/vendor/readability/index.d.ts +64 -0
- package/extensions/modules/web/vendor/readability/index.js +9 -0
- package/extensions/modules/web/vendor/turndown/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown/turndown.cjs.js +784 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/LICENSE +21 -0
- package/extensions/modules/web/vendor/turndown-plugin-gfm/turndown-plugin-gfm.cjs.js +163 -0
- package/extensions/modules/zai/configPanel.ts +151 -0
- package/extensions/modules/zai/index.ts +186 -0
- package/extensions/modules/zai/lib/anthropic.ts +159 -0
- package/extensions/modules/zai/lib/settings.ts +198 -0
- package/extensions/modules/zai/lib/signing.ts +718 -0
- package/extensions/modules/zai/lib/throttle.ts +198 -0
- package/extensions/modules/zai/test/anthropic.test.ts +132 -0
- package/extensions/modules/zai/test/configPanel.test.ts +207 -0
- package/extensions/modules/zai/test/extension.test.ts +249 -0
- package/extensions/modules/zai/test/settings.test.ts +183 -0
- package/extensions/modules/zai/test/signing-wiring.test.ts +108 -0
- package/extensions/modules/zai/test/signing.test.ts +345 -0
- package/extensions/modules/zai/test/throttle.test.ts +329 -0
- package/package.json +19 -4
- package/scripts/validate-themes.mjs +75 -0
- package/skills/munin/SKILL.md +117 -0
- package/skills/ux-capture/SKILL.md +202 -0
- package/skills/ux-design/SKILL.md +279 -0
- package/skills/ux-presets/SKILL.md +484 -0
- package/skills/ux-routing/SKILL.md +46 -0
- package/skills/web/SKILL.md +153 -0
- package/themes/alabaster.json +93 -0
- package/themes/amethyst.json +96 -0
- package/themes/anthracite.json +93 -0
- package/themes/basalt.json +91 -0
- package/themes/birch.json +95 -0
- package/themes/catppuccin-mocha.json +91 -0
- package/themes/dark-abyss.json +91 -0
- package/themes/dark-arctic.json +104 -0
- package/themes/dark-aurora.json +95 -0
- package/themes/dark-catppuccin.json +107 -0
- package/themes/dark-cavern.json +91 -0
- package/themes/dark-celestial.json +106 -0
- package/themes/dark-copper.json +95 -0
- package/themes/dark-cosmos.json +90 -0
- package/themes/dark-cyberpunk.json +102 -0
- package/themes/dark-dracula.json +98 -0
- package/themes/dark-eclipse.json +91 -0
- package/themes/dark-ember.json +95 -0
- package/themes/dark-equinox.json +90 -0
- package/themes/dark-forest.json +96 -0
- package/themes/dark-github.json +105 -0
- package/themes/dark-gruvbox.json +112 -0
- package/themes/dark-lavender.json +95 -0
- package/themes/dark-lunar.json +89 -0
- package/themes/dark-midnight.json +95 -0
- package/themes/dark-monochrome.json +94 -0
- package/themes/dark-monokai.json +98 -0
- package/themes/dark-nebula.json +90 -0
- package/themes/dark-neon-noir.json +100 -0
- package/themes/dark-nord.json +98 -0
- package/themes/dark-ocean.json +101 -0
- package/themes/dark-one.json +100 -0
- package/themes/dark-poimandres.json +115 -0
- package/themes/dark-rainforest.json +91 -0
- package/themes/dark-reef.json +91 -0
- package/themes/dark-retro.json +92 -0
- package/themes/dark-rose-pine.json +96 -0
- package/themes/dark-sakura.json +95 -0
- package/themes/dark-slate.json +95 -0
- package/themes/dark-solarized.json +97 -0
- package/themes/dark-solstice.json +90 -0
- package/themes/dark-starfall.json +91 -0
- package/themes/dark-sunset.json +99 -0
- package/themes/dark-swamp.json +90 -0
- package/themes/dark-synthwave.json +103 -0
- package/themes/dark-taiga.json +91 -0
- package/themes/dark-terminal.json +95 -0
- package/themes/dark-tokyo-night.json +101 -0
- package/themes/dark-tundra.json +91 -0
- package/themes/dark-twilight.json +91 -0
- package/themes/dark-volcanic.json +91 -0
- package/themes/dark.json +80 -0
- package/themes/graphite.json +92 -0
- package/themes/light-arctic.json +107 -0
- package/themes/light-aurora-day.json +91 -0
- package/themes/light-canyon.json +91 -0
- package/themes/light-catppuccin.json +106 -0
- package/themes/light-cirrus.json +90 -0
- package/themes/light-coral.json +95 -0
- package/themes/light-cyberpunk.json +96 -0
- package/themes/light-dawn.json +90 -0
- package/themes/light-dunes.json +91 -0
- package/themes/light-eucalyptus.json +95 -0
- package/themes/light-forest.json +100 -0
- package/themes/light-frost.json +95 -0
- package/themes/light-github.json +115 -0
- package/themes/light-glacier.json +91 -0
- package/themes/light-gruvbox.json +108 -0
- package/themes/light-haze.json +90 -0
- package/themes/light-honeycomb.json +95 -0
- package/themes/light-lagoon.json +91 -0
- package/themes/light-lavender.json +95 -0
- package/themes/light-meadow.json +91 -0
- package/themes/light-mint.json +95 -0
- package/themes/light-monochrome.json +101 -0
- package/themes/light-ocean.json +99 -0
- package/themes/light-one.json +99 -0
- package/themes/light-opal.json +91 -0
- package/themes/light-orchard.json +91 -0
- package/themes/light-paper.json +95 -0
- package/themes/light-poimandres.json +115 -0
- package/themes/light-prism.json +90 -0
- package/themes/light-retro.json +98 -0
- package/themes/light-sand.json +95 -0
- package/themes/light-savanna.json +91 -0
- package/themes/light-solarized.json +102 -0
- package/themes/light-soleil.json +90 -0
- package/themes/light-sunset.json +99 -0
- package/themes/light-synthwave.json +98 -0
- package/themes/light-tokyo-night.json +111 -0
- package/themes/light-wetland.json +91 -0
- package/themes/light-zenith.json +89 -0
- package/themes/light.json +80 -0
- package/themes/limestone.json +94 -0
- package/themes/mahogany.json +97 -0
- package/themes/marble.json +93 -0
- package/themes/mirage.json +79 -0
- package/themes/obsidian.json +91 -0
- package/themes/onyx.json +91 -0
- package/themes/pearl.json +93 -0
- package/themes/porcelain.json +91 -0
- package/themes/quartz.json +96 -0
- package/themes/sandstone.json +95 -0
- package/themes/titanium.json +90 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Fake-pi harness for the plan module lifecycle tests: module handlers are
|
|
2
|
+
// collected off a stub ExtensionAPI and fired directly with synthetic events,
|
|
3
|
+
// so returned results can be asserted on (steering-harness pattern).
|
|
4
|
+
|
|
5
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import planModule from "../index.js";
|
|
9
|
+
|
|
10
|
+
type Hook = (event: any, ctx: any) => any;
|
|
11
|
+
|
|
12
|
+
/** Point the `plan` settings reads at a throwaway agent dir. */
|
|
13
|
+
export function isolateAgentDir(prefix = "ceulen-plan-test-"): { dir: string; restore: () => void } {
|
|
14
|
+
const real = process.env.PI_CODING_AGENT_DIR;
|
|
15
|
+
const dir = mkdtempSync(join(tmpdir(), prefix));
|
|
16
|
+
process.env.PI_CODING_AGENT_DIR = dir;
|
|
17
|
+
return {
|
|
18
|
+
dir,
|
|
19
|
+
restore: () => {
|
|
20
|
+
if (real === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
21
|
+
else process.env.PI_CODING_AGENT_DIR = real;
|
|
22
|
+
rmSync(dir, { recursive: true, force: true });
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function setPlanSettings(dir: string, plan: Record<string, unknown>): void {
|
|
28
|
+
writeFileSync(join(dir, "settings.json"), JSON.stringify({ plan }, null, 2));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface FakePi {
|
|
32
|
+
fire: (name: string, event: any, ctx: any) => Promise<any>;
|
|
33
|
+
ctx: (extra?: Record<string, unknown>) => any;
|
|
34
|
+
commands: Record<string, any>;
|
|
35
|
+
tools: Record<string, any>;
|
|
36
|
+
shortcuts: Record<string, any>;
|
|
37
|
+
notifications: string[];
|
|
38
|
+
userMessages: Array<{ content: any; options: any }>;
|
|
39
|
+
entryTypes: string[];
|
|
40
|
+
/** Every pi.appendEntry call with its data. */
|
|
41
|
+
entries: Array<{ customType: string; data: any }>;
|
|
42
|
+
/** State appended to a fresh session by /plan-approve new (setup hook). */
|
|
43
|
+
newSessionStates: Array<{ customType: string; data: any }>;
|
|
44
|
+
/** Messages the replacement session received (withSession hook). */
|
|
45
|
+
newSessionMessages: Array<{ content: any; options: any }>;
|
|
46
|
+
activeTools: () => string[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function createFakePi(initialTools: string[] = ["bash", "read", "edit", "write", "grep"]): FakePi {
|
|
50
|
+
const hooks = new Map<string, Hook[]>();
|
|
51
|
+
const commands: Record<string, any> = {};
|
|
52
|
+
const tools: Record<string, any> = {};
|
|
53
|
+
const shortcuts: Record<string, any> = {};
|
|
54
|
+
const notifications: string[] = [];
|
|
55
|
+
const userMessages: Array<{ content: any; options: any }> = [];
|
|
56
|
+
const entryTypes: string[] = [];
|
|
57
|
+
const entries: Array<{ customType: string; data: any }> = [];
|
|
58
|
+
const newSessionStates: Array<{ customType: string; data: any }> = [];
|
|
59
|
+
const newSessionMessages: Array<{ content: any; options: any }> = [];
|
|
60
|
+
let activeTools = [...initialTools];
|
|
61
|
+
let branchEntries: any[] = [];
|
|
62
|
+
|
|
63
|
+
const pi: any = {
|
|
64
|
+
registerCommand: (name: string, def: any) => { commands[name] = def; },
|
|
65
|
+
registerTool: (tool: any) => { tools[tool.name] = tool; },
|
|
66
|
+
registerShortcut: (key: string, def: any) => { shortcuts[key] = def; },
|
|
67
|
+
registerFlag: () => {},
|
|
68
|
+
getFlag: () => undefined,
|
|
69
|
+
on: (name: string, fn: Hook) => { (hooks.get(name) ?? hooks.set(name, []).get(name)!).push(fn); },
|
|
70
|
+
getActiveTools: () => [...activeTools],
|
|
71
|
+
setActiveTools: (names: string[]) => { activeTools = [...names]; },
|
|
72
|
+
appendEntry: (customType: string, data: unknown) => { entryTypes.push(customType); entries.push({ customType, data }); },
|
|
73
|
+
sendUserMessage: (content: any, options: any) => { userMessages.push({ content, options }); },
|
|
74
|
+
sendMessage: () => {},
|
|
75
|
+
getThinkingLevel: () => "medium",
|
|
76
|
+
setThinkingLevel: () => {},
|
|
77
|
+
setModel: async () => true,
|
|
78
|
+
getSettings: () => ({}),
|
|
79
|
+
events: { emit: () => {}, on: () => () => {} },
|
|
80
|
+
};
|
|
81
|
+
planModule(pi);
|
|
82
|
+
|
|
83
|
+
const fire = async (name: string, event: any, ctx: any) => {
|
|
84
|
+
let last: any;
|
|
85
|
+
for (const handler of hooks.get(name) ?? []) {
|
|
86
|
+
const out = await handler(event, ctx);
|
|
87
|
+
if (out !== undefined) last = out;
|
|
88
|
+
}
|
|
89
|
+
return last;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const ctx = (extra: Record<string, unknown> = {}): any => ({
|
|
93
|
+
cwd: "/tmp",
|
|
94
|
+
mode: "tui",
|
|
95
|
+
hasUI: true,
|
|
96
|
+
isProjectTrusted: () => false,
|
|
97
|
+
model: { id: "m1", provider: "test" },
|
|
98
|
+
modelRegistry: { getAvailable: () => [], find: () => undefined, refresh: async () => {} },
|
|
99
|
+
sessionManager: { getBranch: () => branchEntries, getSessionFile: () => undefined, appendCustomEntry: () => {} },
|
|
100
|
+
waitForIdle: async () => {},
|
|
101
|
+
newSession: async (options?: { setup?: (sm: any) => Promise<void>; withSession?: (ctx: any) => Promise<void> }) => {
|
|
102
|
+
await options?.setup?.({ appendCustomEntry: (customType: string, data: unknown) => { newSessionStates.push({ customType, data }); } });
|
|
103
|
+
await options?.withSession?.({ sendUserMessage: (content: any, opts: any) => { newSessionMessages.push({ content, options: opts }); } });
|
|
104
|
+
return { cancelled: false };
|
|
105
|
+
},
|
|
106
|
+
getContextUsage: () => ({ percent: 10 }),
|
|
107
|
+
ui: {
|
|
108
|
+
notify: (message: string) => { notifications.push(message); },
|
|
109
|
+
select: async () => undefined,
|
|
110
|
+
editor: async () => undefined,
|
|
111
|
+
setStatus: () => {},
|
|
112
|
+
setEditorText: () => {},
|
|
113
|
+
theme: { fg: (_c: string, t: string) => t },
|
|
114
|
+
},
|
|
115
|
+
...extra,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
fire,
|
|
120
|
+
ctx,
|
|
121
|
+
commands,
|
|
122
|
+
tools,
|
|
123
|
+
shortcuts,
|
|
124
|
+
notifications,
|
|
125
|
+
userMessages,
|
|
126
|
+
entryTypes,
|
|
127
|
+
entries,
|
|
128
|
+
newSessionStates,
|
|
129
|
+
newSessionMessages,
|
|
130
|
+
activeTools: () => [...activeTools],
|
|
131
|
+
setEntries: (e: any[]) => { branchEntries = e; },
|
|
132
|
+
} as FakePi & { setEntries: (e: any[]) => void };
|
|
133
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// Plan module lifecycle: /plan toggle + tool gating + write_plan + approval.
|
|
2
|
+
// Handlers fire through the fake-pi harness (module seams, no real session).
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { readdirSync } from "node:fs";
|
|
5
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { after, beforeEach, describe, it } from "node:test";
|
|
9
|
+
import { createFakePi, isolateAgentDir, setPlanSettings } from "./harness.js";
|
|
10
|
+
|
|
11
|
+
const realAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
12
|
+
let agent: ReturnType<typeof isolateAgentDir>;
|
|
13
|
+
let REPO: string;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
agent = isolateAgentDir();
|
|
17
|
+
REPO = mkdtempSync(join(tmpdir(), "ceulen-plan-repo-"));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
after(() => {
|
|
21
|
+
agent.restore();
|
|
22
|
+
if (realAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
23
|
+
else process.env.PI_CODING_AGENT_DIR = realAgentDir;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/** Start the module in a fresh session at REPO. */
|
|
27
|
+
async function start(h = createFakePi()) {
|
|
28
|
+
await h.fire("session_start", { reason: "startup" }, h.ctx({ cwd: REPO }));
|
|
29
|
+
return h;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe("plan mode — toggle", () => {
|
|
33
|
+
it("/plan enters plan mode: mutators stripped, plan tools added, status set", async () => {
|
|
34
|
+
const h = await start();
|
|
35
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
36
|
+
const active = h.activeTools();
|
|
37
|
+
assert.deepEqual(active.sort(), ["bash", "grep", "read", "write_plan", "ask_user_question"].sort());
|
|
38
|
+
assert.ok(h.entryTypes.includes("ceulen-plan"), "state persisted");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("/plan toggles back and restores the original tool set (plan tools stay)", async () => {
|
|
42
|
+
const h = await start();
|
|
43
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
44
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
45
|
+
// write_plan/ask_user_question are session-visible tools: they must stay
|
|
46
|
+
// active after exit (the model can refine a plan during execution).
|
|
47
|
+
assert.deepEqual(h.activeTools().sort(), ["bash", "read", "edit", "write", "grep", "write_plan", "ask_user_question"].sort());
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("/plan rejects arguments", async () => {
|
|
51
|
+
const h = await start();
|
|
52
|
+
await h.commands.plan.handler("please", h.ctx({ cwd: REPO }));
|
|
53
|
+
assert.ok(h.notifications.some((n) => n.includes("does not take arguments")));
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("plan mode — tool gating", () => {
|
|
58
|
+
it("hard-blocks mutators while planning and lets them through after exit", async () => {
|
|
59
|
+
const h = await start();
|
|
60
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
61
|
+
const blocked = await h.fire("tool_call", { toolName: "edit", input: {} }, h.ctx({ cwd: REPO }));
|
|
62
|
+
assert.equal(blocked.block, true);
|
|
63
|
+
assert.match(blocked.reason, /not available while planning/);
|
|
64
|
+
|
|
65
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
66
|
+
const allowed = await h.fire("tool_call", { toolName: "edit", input: {} }, h.ctx({ cwd: REPO }));
|
|
67
|
+
assert.equal(allowed, undefined);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("auto-allows read-only tools and read bash, hard-blocks bash writers", async () => {
|
|
71
|
+
const h = await start();
|
|
72
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
73
|
+
assert.equal(await h.fire("tool_call", { toolName: "read", input: {} }, h.ctx({ cwd: REPO })), undefined);
|
|
74
|
+
assert.equal(await h.fire("tool_call", { toolName: "bash", input: { command: "git status" } }, h.ctx({ cwd: REPO })), undefined);
|
|
75
|
+
const write = await h.fire("tool_call", { toolName: "bash", input: { command: "echo x > f.txt" } }, h.ctx({ cwd: REPO }));
|
|
76
|
+
assert.equal(write.block, true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("confirm tier: unknown bash prompts; 'Allow for this session' remembers the first token", async () => {
|
|
80
|
+
const h = await start();
|
|
81
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
82
|
+
const ctx = h.ctx({ cwd: REPO, ui: { ...h.ctx().ui, select: async () => "Allow for this session" } });
|
|
83
|
+
assert.equal(await h.fire("tool_call", { toolName: "bash", input: { command: "npm test" } }, ctx), undefined);
|
|
84
|
+
// Remembered: no prompt, no block on the second call.
|
|
85
|
+
let prompted = false;
|
|
86
|
+
const ctx2 = h.ctx({ cwd: REPO, ui: { ...h.ctx().ui, select: async () => { prompted = true; return "Deny"; } } });
|
|
87
|
+
assert.equal(await h.fire("tool_call", { toolName: "bash", input: { command: "npm test" } }, ctx2), undefined);
|
|
88
|
+
assert.equal(prompted, false, "session allow short-circuits the prompt");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("confirm tier: Deny blocks; no-UI blocks outright", async () => {
|
|
92
|
+
const h = await start();
|
|
93
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
94
|
+
const deny = h.ctx({ cwd: REPO, ui: { ...h.ctx().ui, select: async () => "Deny" } });
|
|
95
|
+
const blocked = await h.fire("tool_call", { toolName: "bash", input: { command: "npm test" } }, deny);
|
|
96
|
+
assert.equal(blocked.block, true);
|
|
97
|
+
|
|
98
|
+
const headless = h.ctx({ cwd: REPO, hasUI: false, ui: { ...h.ctx().ui, select: async () => undefined } });
|
|
99
|
+
const blockedHeadless = await h.fire("tool_call", { toolName: "bash", input: { command: "npm test" } }, headless);
|
|
100
|
+
assert.equal(blockedHeadless.block, true);
|
|
101
|
+
assert.match(blockedHeadless.reason, /UI is not available/);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("non-read custom tools take the confirm tier, not a hard block", async () => {
|
|
105
|
+
const h = await start();
|
|
106
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
107
|
+
const allow = await h.fire("tool_call", { toolName: "some_mcp_tool", input: {} }, h.ctx({ cwd: REPO, ui: { ...h.ctx().ui, select: async () => "Allow once" } }));
|
|
108
|
+
assert.equal(allow, undefined);
|
|
109
|
+
const deny = await h.fire("tool_call", { toolName: "some_mcp_tool", input: {} }, h.ctx({ cwd: REPO, ui: { ...h.ctx().ui, select: async () => undefined } }));
|
|
110
|
+
assert.equal(deny.block, true);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
describe("plan mode — write_plan", () => {
|
|
115
|
+
it("writes to .pi/plans by default and reports the path", async () => {
|
|
116
|
+
const h = await start();
|
|
117
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
118
|
+
const result = await h.tools.write_plan.execute("tc1", { title: "Add widget", content: "## Approach\nDo it." }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
119
|
+
const text = result.content[0].text;
|
|
120
|
+
assert.match(text, /Plan written to \.pi\/plans\/\d{4}-.*-add-widget\.md/);
|
|
121
|
+
const file = join(REPO, ".pi", "plans");
|
|
122
|
+
const name = readSyncRecursive(file)[0];
|
|
123
|
+
assert.ok(name, "plan file exists");
|
|
124
|
+
assert.match(readFileSync(join(file, name), "utf8"), /^# Add widget/);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("savePlans=approved defers the file until approval", async () => {
|
|
128
|
+
setPlanSettings(agent.dir, { savePlans: "approved" });
|
|
129
|
+
const h = await start();
|
|
130
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
131
|
+
const result = await h.tools.write_plan.execute("tc1", { title: "Deferred", content: "body" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
132
|
+
assert.match(result.content[0].text, /held for approval/);
|
|
133
|
+
assert.equal(existsSync(join(REPO, ".pi", "plans")), false, "no file yet");
|
|
134
|
+
|
|
135
|
+
await h.commands["plan-approve"].handler("current", h.ctx({ cwd: REPO }));
|
|
136
|
+
const names = readSyncRecursive(join(REPO, ".pi", "plans"));
|
|
137
|
+
assert.equal(names.length, 1, "file written at approval");
|
|
138
|
+
assert.match(h.userMessages[0].content, /Execute the approved plan/);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("savePlans=none keeps the plan in the conversation and disallows new-session execution", async () => {
|
|
142
|
+
setPlanSettings(agent.dir, { savePlans: "none" });
|
|
143
|
+
const h = await start();
|
|
144
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
145
|
+
const result = await h.tools.write_plan.execute("tc1", { title: "Ephemeral", content: "body" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
146
|
+
assert.match(result.content[0].text, /kept in this conversation only/);
|
|
147
|
+
assert.equal(existsSync(join(REPO, ".pi", "plans")), false);
|
|
148
|
+
|
|
149
|
+
await h.commands["plan-approve"].handler("new", h.ctx({ cwd: REPO }));
|
|
150
|
+
assert.ok(h.notifications.some((n) => n.includes("fresh-session execution needs a plan file")));
|
|
151
|
+
|
|
152
|
+
await h.commands["plan-approve"].handler("current", h.ctx({ cwd: REPO }));
|
|
153
|
+
assert.equal(h.userMessages.length, 1);
|
|
154
|
+
assert.equal(existsSync(join(REPO, ".pi", "plans")), false, "still nothing on disk");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("refinement of the same draft reuses its file", async () => {
|
|
158
|
+
const h = await start();
|
|
159
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
160
|
+
await h.tools.write_plan.execute("tc1", { title: "Same", content: "v1" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
161
|
+
await h.tools.write_plan.execute("tc2", { title: "Same", content: "v2" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
162
|
+
const names = readSyncRecursive(join(REPO, ".pi", "plans"));
|
|
163
|
+
assert.equal(names.length, 1, "one file for the same draft");
|
|
164
|
+
assert.equal(readFileSync(join(REPO, ".pi", "plans", names[0]), "utf8").trim(), "# Same\n\nv2");
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("plan mode — prompt + approval flow", () => {
|
|
169
|
+
it("injects the plan contract into before_agent_start only while planning", async () => {
|
|
170
|
+
const h = await start();
|
|
171
|
+
const off = await h.fire("before_agent_start", { systemPrompt: "BASE" }, h.ctx({ cwd: REPO }));
|
|
172
|
+
assert.equal(off, undefined, "no injection outside plan mode");
|
|
173
|
+
|
|
174
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
175
|
+
const on = await h.fire("before_agent_start", { systemPrompt: "BASE" }, h.ctx({ cwd: REPO }));
|
|
176
|
+
assert.match(on.systemPrompt, /^BASE/);
|
|
177
|
+
assert.match(on.systemPrompt, /## Plan Mode/);
|
|
178
|
+
assert.match(on.systemPrompt, /\.pi\/plans/);
|
|
179
|
+
assert.match(on.systemPrompt, /decision-complete/);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("write_plan marks the plan ready and agent_settled prefills /plan-approve", async () => {
|
|
183
|
+
const h = await start();
|
|
184
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
185
|
+
await h.tools.write_plan.execute("tc1", { title: "T", content: "body" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
186
|
+
let prefilled: string | undefined;
|
|
187
|
+
const ctx = h.ctx({ cwd: REPO, ui: { ...h.ctx().ui, setEditorText: (t: string) => { prefilled = t; } } });
|
|
188
|
+
await h.fire("agent_settled", {}, ctx);
|
|
189
|
+
assert.equal(prefilled, "/plan-approve");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("auto-approve executes on settle without a keypress", async () => {
|
|
193
|
+
setPlanSettings(agent.dir, { autoApprove: true });
|
|
194
|
+
const h = await start();
|
|
195
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
196
|
+
await h.tools.write_plan.execute("tc1", { title: "T", content: "body" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
197
|
+
await h.fire("agent_settled", {}, h.ctx({ cwd: REPO }));
|
|
198
|
+
assert.equal(h.userMessages.length, 1);
|
|
199
|
+
assert.equal(h.userMessages[0].options.deliverAs, "followUp");
|
|
200
|
+
assert.match(h.userMessages[0].content, /Execute the approved plan/);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("/plan-approve without a plan warns", async () => {
|
|
204
|
+
const h = await start();
|
|
205
|
+
await h.commands["plan-approve"].handler("current", h.ctx({ cwd: REPO }));
|
|
206
|
+
assert.ok(h.notifications.some((n) => n.includes("No plan is ready for approval")));
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("approving clears the draft: re-entering plan mode starts fresh", async () => {
|
|
210
|
+
const h = await start();
|
|
211
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
212
|
+
await h.tools.write_plan.execute("tc1", { title: "First", content: "body" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
213
|
+
await h.commands["plan-approve"].handler("current", h.ctx({ cwd: REPO }));
|
|
214
|
+
|
|
215
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
216
|
+
await h.commands["plan-approve"].handler("current", h.ctx({ cwd: REPO }));
|
|
217
|
+
assert.ok(h.notifications.some((n) => n.includes("No plan is ready for approval")), "stale plan is gone");
|
|
218
|
+
|
|
219
|
+
// An unapproved draft survives a toggle so refinement keeps the file.
|
|
220
|
+
await h.tools.write_plan.execute("tc2", { title: "Second", content: "v1" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
221
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO })); // off
|
|
222
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO })); // on
|
|
223
|
+
const result = await h.tools.write_plan.execute("tc3", { title: "Second", content: "v2" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
224
|
+
assert.match(result.content[0].text, /Plan written to/);
|
|
225
|
+
const names = readSyncRecursive(join(REPO, ".pi", "plans"));
|
|
226
|
+
// Two plans exist (First's approved file stays as history; Second was
|
|
227
|
+
// refined in place across the toggle).
|
|
228
|
+
assert.equal(names.length, 2, names.join(","));
|
|
229
|
+
assert.equal(readFileSync(join(REPO, ".pi", "plans", names.find((n) => n.includes("second"))!), "utf8").trim(), "# Second\n\nv2");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("fresh-session handoff marks the plan approved and does not reuse it as a draft", async () => {
|
|
233
|
+
const h = await start();
|
|
234
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
235
|
+
await h.tools.write_plan.execute("tc1", { title: "Handoff", content: "body" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
236
|
+
await h.commands["plan-approve"].handler("new", h.ctx({ cwd: REPO }));
|
|
237
|
+
|
|
238
|
+
// The child session gets the approved plan state, marked so a later
|
|
239
|
+
// plan-mode entry does not mistake it for a draft to refine.
|
|
240
|
+
const handedOff = h.newSessionStates.find((e) => e.customType === "ceulen-plan");
|
|
241
|
+
assert.ok(handedOff, "child session received the plan state entry");
|
|
242
|
+
assert.equal(handedOff.data.planApproved, true, "handed-off plan is marked approved");
|
|
243
|
+
assert.equal(handedOff.data.enabled, false);
|
|
244
|
+
assert.match(handedOff.data.lastPlanPath, /\.pi\/plans\/.*handoff\.md$/);
|
|
245
|
+
assert.equal(h.newSessionMessages.length, 1, "child received the execution prompt");
|
|
246
|
+
assert.match(h.newSessionMessages[0].content, /Execute the approved plan/);
|
|
247
|
+
|
|
248
|
+
// Regression: re-entering plan mode after the handoff must NOT reuse the
|
|
249
|
+
// child's plan path — a new plan writes a NEW file.
|
|
250
|
+
await h.commands.plan.handler("", h.ctx({ cwd: REPO }));
|
|
251
|
+
await h.tools.write_plan.execute("tc2", { title: "Handoff", content: "v2" }, undefined, undefined, h.ctx({ cwd: REPO }));
|
|
252
|
+
assert.equal(readSyncRecursive(join(REPO, ".pi", "plans")).filter((n) => n.includes("handoff")).length, 2, "fresh plan file, not an in-place refinement");
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe("plan mode — ask_user_question", () => {
|
|
257
|
+
it("returns the selected option and marks the recommendation", async () => {
|
|
258
|
+
const h = await start();
|
|
259
|
+
let dialog: string[] = [];
|
|
260
|
+
const ctx = h.ctx({ cwd: REPO, ui: { ...h.ctx().ui, select: async (_t: string, options: string[]) => { dialog = options; return options[1]; } } });
|
|
261
|
+
const result = await h.tools.ask_user_question.execute("tc1", {
|
|
262
|
+
question: "Which?",
|
|
263
|
+
options: [{ label: "A" }, { label: "B", description: "better" }],
|
|
264
|
+
recommended: "A",
|
|
265
|
+
}, undefined, undefined, ctx);
|
|
266
|
+
assert.equal(dialog[0], "★ A");
|
|
267
|
+
assert.match(dialog[1], /^B — better$/);
|
|
268
|
+
assert.match(result.content[0].text, /User selected: B/);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it("rejects duplicate/non-matching parameters", async () => {
|
|
272
|
+
const h = await start();
|
|
273
|
+
await assert.rejects(
|
|
274
|
+
() => h.tools.ask_user_question.execute("tc1", { question: "q", options: [{ label: "A" }, { label: "A" }] }, undefined, undefined, h.ctx()),
|
|
275
|
+
/unique/,
|
|
276
|
+
);
|
|
277
|
+
await assert.rejects(
|
|
278
|
+
() => h.tools.ask_user_question.execute("tc1", { question: "q", options: [{ label: "A" }, { label: "B" }], recommended: "C" }, undefined, undefined, h.ctx()),
|
|
279
|
+
/must match/,
|
|
280
|
+
);
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
describe("plan mode — branch restore", () => {
|
|
285
|
+
it("restores enabled state from the persisted entry", async () => {
|
|
286
|
+
const h = createFakePi();
|
|
287
|
+
(h as unknown as { setEntries: (e: unknown[]) => void }).setEntries([
|
|
288
|
+
{ type: "custom", customType: "ceulen-plan", data: { enabled: true, toolsBeforePlan: ["bash", "read", "edit", "write", "grep"] } },
|
|
289
|
+
]);
|
|
290
|
+
await h.fire("session_start", { reason: "resume" }, h.ctx({ cwd: REPO }));
|
|
291
|
+
assert.ok(h.activeTools().includes("write_plan"), "plan tools re-enabled");
|
|
292
|
+
assert.equal(h.activeTools().includes("edit"), false, "mutators stripped");
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
/** Recursively collect file names under dir ([] when missing). */
|
|
297
|
+
function readSyncRecursive(dir: string, prefix = ""): string[] {
|
|
298
|
+
if (!existsSync(dir)) return [];
|
|
299
|
+
const out: string[] = [];
|
|
300
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
301
|
+
if (entry.isDirectory()) out.push(...readSyncRecursive(join(dir, entry.name), `${prefix}${entry.name}/`));
|
|
302
|
+
else out.push(`${prefix}${entry.name}`);
|
|
303
|
+
}
|
|
304
|
+
return out;
|
|
305
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Plan settings layer: defaults, trusted-project overlay, save policy
|
|
2
|
+
// validation, plans-dir expansion/containment, atomic writes.
|
|
3
|
+
import assert from "node:assert/strict";
|
|
4
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { after, beforeEach, describe, it } from "node:test";
|
|
8
|
+
import {
|
|
9
|
+
DEFAULT_PLAN_SETTINGS,
|
|
10
|
+
DEFAULT_PLANS_DIR,
|
|
11
|
+
expandPlansDir,
|
|
12
|
+
isInsidePlansDir,
|
|
13
|
+
isSavePlans,
|
|
14
|
+
planPath,
|
|
15
|
+
projectOverridesPlan,
|
|
16
|
+
readPlanSettings,
|
|
17
|
+
writePlanSection,
|
|
18
|
+
} from "../lib/settings.js";
|
|
19
|
+
|
|
20
|
+
const realAgentDir = process.env.PI_CODING_AGENT_DIR;
|
|
21
|
+
let AGENT: string;
|
|
22
|
+
let REPO: string;
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
AGENT = mkdtempSync(join(tmpdir(), "ceulen-plan-settings-"));
|
|
26
|
+
REPO = mkdtempSync(join(tmpdir(), "ceulen-plan-repo-"));
|
|
27
|
+
process.env.PI_CODING_AGENT_DIR = AGENT;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
after(() => {
|
|
31
|
+
if (realAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
|
|
32
|
+
else process.env.PI_CODING_AGENT_DIR = realAgentDir;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const writeGlobal = (body: unknown): void => writeFileSync(join(AGENT, "settings.json"), JSON.stringify(body, null, 2));
|
|
36
|
+
const trustedCtx = (trusted = true) => ({ cwd: REPO, isProjectTrusted: () => trusted });
|
|
37
|
+
|
|
38
|
+
describe("plan settings", () => {
|
|
39
|
+
it("defaults: .pi/plans, save all, no model, no auto-approve", () => {
|
|
40
|
+
const s = readPlanSettings(trustedCtx());
|
|
41
|
+
assert.equal(s.plansDir, DEFAULT_PLANS_DIR);
|
|
42
|
+
assert.equal(s.savePlans, "all");
|
|
43
|
+
assert.equal(s.planModel, "");
|
|
44
|
+
assert.equal(s.planThinking, "");
|
|
45
|
+
assert.equal(s.autoApprove, false);
|
|
46
|
+
assert.deepEqual(DEFAULT_PLAN_SETTINGS, s);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("reads the global section and validates savePlans", () => {
|
|
50
|
+
writeGlobal({ plan: { plansDir: ".agents/plans/{yyyymm}", savePlans: "approved", planModel: "router/zai/glm-5.3", planThinking: "high", autoApprove: true } });
|
|
51
|
+
const s = readPlanSettings(trustedCtx());
|
|
52
|
+
assert.equal(s.plansDir, ".agents/plans/{yyyymm}");
|
|
53
|
+
assert.equal(s.savePlans, "approved");
|
|
54
|
+
assert.equal(s.planModel, "router/zai/glm-5.3");
|
|
55
|
+
assert.equal(s.planThinking, "high");
|
|
56
|
+
assert.equal(s.autoApprove, true);
|
|
57
|
+
|
|
58
|
+
writeGlobal({ plan: { savePlans: "sometimes" } }); // invalid → default preserved
|
|
59
|
+
assert.equal(readPlanSettings(trustedCtx()).savePlans, "all");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("trusted project overrides the global section; untrusted is ignored", () => {
|
|
63
|
+
writeGlobal({ plan: { plansDir: "global-dir", savePlans: "all" } });
|
|
64
|
+
const projectFile = join(REPO, ".pi", "settings.json");
|
|
65
|
+
mkdirSync(join(REPO, ".pi"), { recursive: true });
|
|
66
|
+
writeFileSync(projectFile, JSON.stringify({ plan: { plansDir: "project-dir", savePlans: "none" } }));
|
|
67
|
+
|
|
68
|
+
const trusted = readPlanSettings(trustedCtx(true));
|
|
69
|
+
assert.equal(trusted.plansDir, "project-dir");
|
|
70
|
+
assert.equal(trusted.savePlans, "none");
|
|
71
|
+
assert.equal(projectOverridesPlan(REPO, true), true);
|
|
72
|
+
|
|
73
|
+
const untrusted = readPlanSettings(trustedCtx(false));
|
|
74
|
+
assert.equal(untrusted.plansDir, "global-dir");
|
|
75
|
+
assert.equal(untrusted.savePlans, "all");
|
|
76
|
+
assert.equal(projectOverridesPlan(REPO, false), false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("writePlanSection merges without clobbering sibling keys and is atomic", () => {
|
|
80
|
+
writeGlobal({ ceulen: { disabled: ["munin"] }, plan: { plansDir: "old" } });
|
|
81
|
+
const file = writePlanSection({ plansDir: ".pi/plans", savePlans: "approved" });
|
|
82
|
+
assert.equal(file, join(AGENT, "settings.json"));
|
|
83
|
+
const body = JSON.parse(readFileSync(file, "utf8"));
|
|
84
|
+
assert.deepEqual(body.ceulen, { disabled: ["munin"] }); // sibling preserved
|
|
85
|
+
assert.equal(body.plan.plansDir, ".pi/plans");
|
|
86
|
+
assert.equal(body.plan.savePlans, "approved");
|
|
87
|
+
assert.equal(existsSync(file + ".tmp"), false, "tmp file renamed away");
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("writePlanSection refuses a corrupt file instead of clobbering it", () => {
|
|
91
|
+
writeFileSync(join(AGENT, "settings.json"), "{ not json");
|
|
92
|
+
assert.throws(() => writePlanSection({ savePlans: "none" }), /not valid JSON/);
|
|
93
|
+
assert.equal(readFileSync(join(AGENT, "settings.json"), "utf8"), "{ not json", "corrupt file untouched");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("{yyyymm} expands to the UTC month and planPath stays inside", () => {
|
|
97
|
+
const now = new Date(Date.UTC(2026, 8, 30));
|
|
98
|
+
assert.equal(expandPlansDir(".pi/plans/{yyyymm}", now), ".pi/plans/202609");
|
|
99
|
+
const file = planPath(REPO, "My Feature Plan", ".pi/plans/{yyyymm}");
|
|
100
|
+
assert.match(file, /\.pi\/plans\/2026\d\d\/\d{4}-.*-my-feature-plan\.md$/);
|
|
101
|
+
assert.equal(isInsidePlansDir(file, ".pi/plans/{yyyymm}", REPO), true);
|
|
102
|
+
assert.equal(isInsidePlansDir(join(REPO, ".pi/plans/202609/x.md"), ".pi/plans/{yyyymm}", REPO), true);
|
|
103
|
+
assert.equal(isInsidePlansDir(join(REPO, ".pi/plan-escape.md"), ".pi/plans/{yyyymm}", REPO), false);
|
|
104
|
+
assert.equal(isInsidePlansDir(join(REPO, ".pi/plans"), ".pi/plans/{yyyymm}", REPO), false, "the dir itself is not inside");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("isSavePlans accepts only the closed set", () => {
|
|
108
|
+
assert.equal(isSavePlans("all"), true);
|
|
109
|
+
assert.equal(isSavePlans("approved"), true);
|
|
110
|
+
assert.equal(isSavePlans("none"), true);
|
|
111
|
+
assert.equal(isSavePlans("always"), false);
|
|
112
|
+
assert.equal(isSavePlans(1), false);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Bash classification for plan mode: read auto-runs, writers hard-block,
|
|
2
|
+
// everything else takes the confirm tier. The vendored rules are
|
|
3
|
+
// regression-hardened upstream — these tests pin the contract, not the regexes.
|
|
4
|
+
import assert from "node:assert/strict";
|
|
5
|
+
import { describe, it } from "node:test";
|
|
6
|
+
import { classifyCommand, splitShellSegments, xargsPayload } from "../lib/shell-gate.js";
|
|
7
|
+
|
|
8
|
+
describe("plan shell gate — reads auto-run", () => {
|
|
9
|
+
for (const cmd of [
|
|
10
|
+
"ls -la",
|
|
11
|
+
"grep -rn foo src",
|
|
12
|
+
"cat package.json",
|
|
13
|
+
"git status",
|
|
14
|
+
"git log --oneline -5",
|
|
15
|
+
"git diff HEAD",
|
|
16
|
+
"git branch --show-current",
|
|
17
|
+
"git config --get user.email",
|
|
18
|
+
"head -20 README.md | wc -l",
|
|
19
|
+
"grep foo src | head",
|
|
20
|
+
"ls -la; echo done",
|
|
21
|
+
"cd src && ls",
|
|
22
|
+
"FOO=1 LANG=C ls",
|
|
23
|
+
"find . -name '*.ts' 2>/dev/null | head",
|
|
24
|
+
"file README.md",
|
|
25
|
+
"jq '.name' package.json",
|
|
26
|
+
"echo a | xargs grep -l x",
|
|
27
|
+
"tar -tf archive.tar",
|
|
28
|
+
"tar -xOf archive.tar file",
|
|
29
|
+
"command -v node",
|
|
30
|
+
"type git",
|
|
31
|
+
"sort -n numbers.txt",
|
|
32
|
+
"sed -E 's/a/b/' file.txt | head",
|
|
33
|
+
"while IFS= read -r f; do cat \"$f\"; done",
|
|
34
|
+
]) {
|
|
35
|
+
it(`read: ${cmd}`, () => assert.equal(classifyCommand(cmd), "read", cmd));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("plan shell gate — writers hard-block", () => {
|
|
40
|
+
for (const cmd of [
|
|
41
|
+
"echo hi > out.txt",
|
|
42
|
+
"ls >> log.txt",
|
|
43
|
+
"cat <<EOF > x.md\nhello\nEOF",
|
|
44
|
+
"sed -i 's/a/b/' file.ts",
|
|
45
|
+
"echo x | tee out.txt",
|
|
46
|
+
"cp a b",
|
|
47
|
+
"mv a b",
|
|
48
|
+
"rm -rf node_modules",
|
|
49
|
+
"mkdir -p .pi/plans",
|
|
50
|
+
"touch newfile",
|
|
51
|
+
"find . -name '*.tmp' -delete",
|
|
52
|
+
"perl -pi -e 's/a/b/' file.ts",
|
|
53
|
+
"sort -o out.txt in.txt",
|
|
54
|
+
"git commit -m x",
|
|
55
|
+
"git checkout main",
|
|
56
|
+
"git config user.email x@y.z",
|
|
57
|
+
"echo $(date)",
|
|
58
|
+
"echo a | xargs rm",
|
|
59
|
+
"echo a | xargs --null rm",
|
|
60
|
+
]) {
|
|
61
|
+
it(`write: ${cmd}`, () => assert.equal(classifyCommand(cmd), "write", cmd));
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("plan shell gate — unknown executables confirm", () => {
|
|
66
|
+
for (const cmd of [
|
|
67
|
+
"npm test",
|
|
68
|
+
"bun run build",
|
|
69
|
+
"pytest -q",
|
|
70
|
+
"make all",
|
|
71
|
+
"node script.js",
|
|
72
|
+
"python3 -c 'open(\"x\",\"w\")'",
|
|
73
|
+
"git -C repo status", // tolerated global opt → read (regression pin)
|
|
74
|
+
]) {
|
|
75
|
+
it(`confirm-or-read: ${cmd}`, () => {
|
|
76
|
+
const disposition = classifyCommand(cmd);
|
|
77
|
+
// Git global opts are explicitly read; the rest are confirm.
|
|
78
|
+
if (cmd.startsWith("git ")) assert.equal(disposition, "read", cmd);
|
|
79
|
+
else assert.equal(disposition, "confirm", cmd);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("plan shell gate — helpers", () => {
|
|
85
|
+
it("splitShellSegments keeps quoted separators in one segment", () => {
|
|
86
|
+
assert.deepEqual(splitShellSegments("grep 'a|b' f | head"), ["grep 'a|b' f", "head"]);
|
|
87
|
+
assert.deepEqual(splitShellSegments("ls; echo done"), ["ls", "echo done"]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("xargsPayload strips separate-value flags and attached clusters", () => {
|
|
91
|
+
assert.equal(xargsPayload("xargs -I {} grep -l x"), "grep -l x");
|
|
92
|
+
assert.equal(xargsPayload("xargs -0 -r grep -l x"), "grep -l x");
|
|
93
|
+
// Regression (fixed deviation): a VALUELESS long option must not eat the
|
|
94
|
+
// payload — `--null rm` must yield `rm`, not "" (which upstream read as
|
|
95
|
+
// "read" and auto-allowed a writer).
|
|
96
|
+
assert.equal(xargsPayload("xargs --null rm"), "rm");
|
|
97
|
+
assert.equal(xargsPayload("xargs --arg-file list grep x"), "grep x");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("empty command → confirm (never auto-run)", () => {
|
|
101
|
+
assert.equal(classifyCommand(" "), "confirm");
|
|
102
|
+
});
|
|
103
|
+
});
|