Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Copilot CLI namespaces plugin commands by plugin name. For example:
pi install git:github.com/DietrichGebert/ponytail
```

Subagents started by the pi-code `subagent` tool (child `pi` processes) start in the parent's current mode, not the saved default, so `/ponytail off` also covers them. `PONYTAIL_SUBAGENT_MATCHER` (regex on the agent name, same rules as above) turns ponytail off for the agents that do not match.

## Kimi Code

In Kimi Code, run `/plugins install https://github.com/DietrichGebert/ponytail`, then `/reload` or start a new session. The plugin puts [`AGENTS.md`](AGENTS.md) into the system prompt every turn and adds the six skills, which Kimi also offers as `/ponytail`, `/ponytail-review`, `/ponytail-audit`, `/ponytail-debt`, `/ponytail-gain` and `/ponytail-help`. Level switching (`/ponytail lite`) loads the skill for that turn; there is no persistent mode flag in Kimi.
Expand Down
37 changes: 37 additions & 0 deletions pi-extension/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createRequire } from "node:module";
import vm from "node:vm";

const require = createRequire(import.meta.url);
const {
Expand Down Expand Up @@ -44,6 +45,29 @@ export function resolveSessionMode(entries, fallbackMode = DEFAULT_MODE) {
return fallback;
}

// Subagent name from the `--system-prompt <dir>/prompt-<safeName>.md` argument that pi-code passes to child pi
// processes; null when absent or unparseable.
export function subagentName(argv) {
const i = Array.isArray(argv) ? argv.indexOf("--system-prompt") : -1;
const m = i >= 0 ? /^prompt-(.+)\.md$/.exec(String(argv[i + 1] || "").split(/[\\/]/).pop()) : null;
return m ? m[1] : null;
}

// Mode of a child (subagent) pi process: the parent's mode, then PONYTAIL_SUBAGENT_MATCHER (unanchored,
// case-insensitive, like the SubagentStart hook) turns it off for agents that do not match.
// An unparseable name, a bad regex or a matcher that times out (#658) fail open: the mode is kept.
export function childMode({ parentMode, matcher, argv, fallback }) {
const mode = normalizePersistedMode(parentMode) || fallback;
if (mode === "off" || !matcher) return mode;
const name = subagentName(argv);
if (name === null) return mode;
try {
return vm.runInNewContext("re.test(s)", { re: new RegExp(matcher, "i"), s: name }, { timeout: 100 }) ? mode : "off";
} catch {
return mode;
}
}

export function parsePonytailCommand(text, defaultMode = DEFAULT_MODE, currentMode = null) {
const fallback = normalizePersistedMode(defaultMode) || DEFAULT_MODE;
const normalizedText = String(text || "").trim().toLowerCase();
Expand Down Expand Up @@ -103,6 +127,7 @@ export default function ponytailExtension(pi) {
if (!normalized) return;

currentMode = normalized;
process.env.PONYTAIL_PARENT_MODE = currentMode;
pi.appendEntry("ponytail-mode", { mode: normalized });
syncStatus(ctx);
ctx?.ui?.notify?.(`Ponytail mode set to ${normalized}.`, "info");
Expand Down Expand Up @@ -204,6 +229,18 @@ export default function ponytailExtension(pi) {
configuredDefaultMode = getDefaultMode();
hideStatus = getHideStatus();
restoreSessionMode(ctx);
if (process.env.PI_CODE_SUBAGENT) {
// Child: inherit the parent's mode (not the persisted default) and honor the matcher; never persisted.
currentMode = childMode({
parentMode: process.env.PONYTAIL_PARENT_MODE,
matcher: process.env.PONYTAIL_SUBAGENT_MATCHER,
argv: process.argv,
fallback: currentMode,
});
syncStatus(ctx);
}
// Children spawned later inherit the mode through process.env.
process.env.PONYTAIL_PARENT_MODE = currentMode;
if (!getQuietStartup()) {
ctx?.ui?.notify?.(`Ponytail loaded: ${currentMode}`, "info");
}
Expand Down
3 changes: 3 additions & 0 deletions pi-extension/test/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import test from "node:test";

import ponytailExtension, { parsePonytailCommand } from "../index.js";

// These tests drive a parent session: a run inside a pi subagent must not switch them to the child-mode path.
for (const key of ["PI_CODE_SUBAGENT", "PONYTAIL_PARENT_MODE", "PONYTAIL_SUBAGENT_MATCHER"]) delete process.env[key];

function createPiHarness() {
const events = new Map();
const commands = new Map();
Expand Down
57 changes: 57 additions & 0 deletions pi-extension/test/subagent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import assert from "node:assert/strict";
import test from "node:test";

import ponytailExtension, { childMode, subagentName } from "../index.js";

const argv = (name) => ["node", "pi", "--system-prompt", `/tmp/x/prompt-${name}.md`];

test("subagentName parses pi-code's prompt file name", () => {
assert.equal(subagentName(argv("swarm-worker")), "swarm-worker");
assert.equal(subagentName(["node", "pi"]), null);
assert.equal(subagentName(["--system-prompt", "/tmp/other.md"]), null);
});

test("childMode inherits the parent mode and applies the matcher", () => {
const base = { argv: argv("swarm-worker"), fallback: "full" };
assert.equal(childMode({ ...base }), "full");
assert.equal(childMode({ ...base, parentMode: "ultra" }), "ultra");
assert.equal(childMode({ ...base, parentMode: "off", matcher: "swarm" }), "off");
assert.equal(childMode({ ...base, parentMode: "lite", matcher: "SWARM" }), "lite");
assert.equal(childMode({ ...base, matcher: "^explore$" }), "off");
});

test("childMode fails open on bad regex, unknown agent or a matcher that times out", () => {
assert.equal(childMode({ argv: argv("a"), fallback: "full", matcher: "(" }), "full");
assert.equal(childMode({ argv: ["node"], fallback: "full", matcher: "zzz" }), "full");
// Catastrophic backtracking (#658): the 100 ms vm timeout aborts the match and the mode is kept.
assert.equal(childMode({ argv: argv(`${"a".repeat(40)}!`), fallback: "full", matcher: "^(a+)+$" }), "full");
});

test("a child session_start follows the parent mode and does not persist it", async () => {
const saved = { ...process.env };
const savedArgv = process.argv;
const events = new Map();
const appended = [];
try {
process.env.PI_CODE_SUBAGENT = "1";
process.env.PONYTAIL_PARENT_MODE = "lite";
process.env.PONYTAIL_SUBAGENT_MATCHER = "^other$";
process.argv = argv("swarm-worker");
ponytailExtension({
on: (n, h) => events.set(n, h),
registerCommand() {},
appendEntry: (...a) => appended.push(a),
sendUserMessage() {},
});
await events.get("session_start")({}, { sessionManager: { getEntries: () => [] }, ui: { notify() {} } });
const ev = { systemPrompt: "base", systemPromptOptions: { sections: {} } };
await events.get("before_agent_start")(ev);
assert.equal(ev.systemPromptOptions.sections.ponytail, undefined);
assert.equal(process.env.PONYTAIL_PARENT_MODE, "off");
assert.deepEqual(appended, []);
} finally {
process.argv = savedArgv;
for (const k of Object.keys(process.env)) if (!(k in saved)) delete process.env[k];
Object.assign(process.env, saved);
}
});