@tintinweb/pi-subagents 0.10.0 → 0.10.2

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.
@@ -9,6 +9,7 @@ import { type Component, type TUI } from "@earendil-works/pi-tui";
9
9
  import type { AgentRecord } from "../types.js";
10
10
  import type { Theme } from "./agent-widget.js";
11
11
  import { type AgentActivity } from "./agent-widget.js";
12
+ import { type ViewerKeybindings } from "./viewer-keys.js";
12
13
  /** Height ceiling shared by the overlay's `maxHeight` and the viewer's internal viewport cap. */
13
14
  export declare const VIEWPORT_HEIGHT_PCT = 70;
14
15
  export declare class ConversationViewer implements Component {
@@ -27,9 +28,12 @@ export declare class ConversationViewer implements Component {
27
28
  private closed;
28
29
  /** Two-press confirm guard for the stop key, so a stray key can't kill the agent. */
29
30
  private stopArmed;
31
+ private keys;
30
32
  constructor(tui: TUI, session: AgentSession, record: AgentRecord, activity: AgentActivity | undefined, theme: Theme, done: (result: undefined) => void,
31
33
  /** Abort the agent shown here. Omitted → no stop affordance (e.g. read-only history). */
32
- onStop?: (() => void) | undefined);
34
+ onStop?: (() => void) | undefined,
35
+ /** User keybindings from `ctx.ui.custom()`. Omitted → hardcoded defaults. */
36
+ keybindings?: ViewerKeybindings);
33
37
  handleInput(data: string): void;
34
38
  render(width: number): string[];
35
39
  /** Stoppable only when a stop handler exists and the agent is still active. */
@@ -8,6 +8,7 @@ import { matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi } from "@ea
8
8
  import { extractText } from "../context.js";
9
9
  import { getLifetimeTotal, getSessionContextPercent } from "../usage.js";
10
10
  import { buildInvocationTags, describeActivity, formatDuration, formatSessionTokens, getDisplayName, getPromptModeLabel } from "./agent-widget.js";
11
+ import { createViewerKeys } from "./viewer-keys.js";
11
12
  /** Base lines consumed by chrome: top border + header + header sep + footer sep + footer + bottom border. */
12
13
  const CHROME_LINES_BASE = 6;
13
14
  const MIN_VIEWPORT = 3;
@@ -28,9 +29,12 @@ export class ConversationViewer {
28
29
  closed = false;
29
30
  /** Two-press confirm guard for the stop key, so a stray key can't kill the agent. */
30
31
  stopArmed = false;
32
+ keys;
31
33
  constructor(tui, session, record, activity, theme, done,
32
34
  /** Abort the agent shown here. Omitted → no stop affordance (e.g. read-only history). */
33
- onStop) {
35
+ onStop,
36
+ /** User keybindings from `ctx.ui.custom()`. Omitted → hardcoded defaults. */
37
+ keybindings) {
34
38
  this.tui = tui;
35
39
  this.session = session;
36
40
  this.record = record;
@@ -38,6 +42,7 @@ export class ConversationViewer {
38
42
  this.theme = theme;
39
43
  this.done = done;
40
44
  this.onStop = onStop;
45
+ this.keys = createViewerKeys(keybindings);
41
46
  this.unsubscribe = session.subscribe(() => {
42
47
  if (this.closed)
43
48
  return;
@@ -70,19 +75,19 @@ export class ConversationViewer {
70
75
  const totalLines = this.buildContentLines(this.lastInnerW).length;
71
76
  const viewportHeight = this.viewportHeight();
72
77
  const maxScroll = Math.max(0, totalLines - viewportHeight);
73
- if (matchesKey(data, "up") || matchesKey(data, "k")) {
78
+ if (this.keys.scrollUp(data)) {
74
79
  this.scrollOffset = Math.max(0, this.scrollOffset - 1);
75
80
  this.autoScroll = this.scrollOffset >= maxScroll;
76
81
  }
77
- else if (matchesKey(data, "down") || matchesKey(data, "j")) {
82
+ else if (this.keys.scrollDown(data)) {
78
83
  this.scrollOffset = Math.min(maxScroll, this.scrollOffset + 1);
79
84
  this.autoScroll = this.scrollOffset >= maxScroll;
80
85
  }
81
- else if (matchesKey(data, "pageUp") || matchesKey(data, "shift+up")) {
86
+ else if (this.keys.pageUp(data)) {
82
87
  this.scrollOffset = Math.max(0, this.scrollOffset - viewportHeight);
83
88
  this.autoScroll = false;
84
89
  }
85
- else if (matchesKey(data, "pageDown") || matchesKey(data, "shift+down")) {
90
+ else if (this.keys.pageDown(data)) {
86
91
  this.scrollOffset = Math.min(maxScroll, this.scrollOffset + viewportHeight);
87
92
  this.autoScroll = this.scrollOffset >= maxScroll;
88
93
  }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * viewer-keys.ts — Scroll key matchers for the conversation viewer.
3
+ *
4
+ * Resolves `tui.select.*` through the user's keybindings when pi provides a
5
+ * manager, falling back to the previous hardcoded keys otherwise. The viewer's
6
+ * k/j and shift+arrow aliases always work alongside whatever is bound.
7
+ */
8
+ /** The `tui.select.*` keybinding ids the viewer resolves. */
9
+ export type ViewerScrollKeybinding = "tui.select.up" | "tui.select.down" | "tui.select.pageUp" | "tui.select.pageDown";
10
+ /** Structural subset of pi-tui's `KeybindingsManager` (which satisfies it). */
11
+ export interface ViewerKeybindings {
12
+ matches(data: string, keybinding: ViewerScrollKeybinding): boolean;
13
+ }
14
+ export interface ViewerKeys {
15
+ scrollUp(data: string): boolean;
16
+ scrollDown(data: string): boolean;
17
+ pageUp(data: string): boolean;
18
+ pageDown(data: string): boolean;
19
+ }
20
+ export declare function createViewerKeys(keybindings?: ViewerKeybindings): ViewerKeys;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * viewer-keys.ts — Scroll key matchers for the conversation viewer.
3
+ *
4
+ * Resolves `tui.select.*` through the user's keybindings when pi provides a
5
+ * manager, falling back to the previous hardcoded keys otherwise. The viewer's
6
+ * k/j and shift+arrow aliases always work alongside whatever is bound.
7
+ */
8
+ import { matchesKey } from "@earendil-works/pi-tui";
9
+ export function createViewerKeys(keybindings) {
10
+ const matches = (data, id, fallback) => keybindings ? keybindings.matches(data, id) : matchesKey(data, fallback);
11
+ return {
12
+ scrollUp: (data) => matches(data, "tui.select.up", "up") || matchesKey(data, "k"),
13
+ scrollDown: (data) => matches(data, "tui.select.down", "down") || matchesKey(data, "j"),
14
+ pageUp: (data) => matches(data, "tui.select.pageUp", "pageUp") || matchesKey(data, "shift+up"),
15
+ pageDown: (data) => matches(data, "tui.select.pageDown", "pageDown") || matchesKey(data, "shift+down"),
16
+ };
17
+ }
@@ -10,6 +10,8 @@ export interface WorktreeInfo {
10
10
  path: string;
11
11
  /** Branch name created for this worktree (if changes exist). */
12
12
  branch: string;
13
+ /** Commit SHA that the worktree was created from. */
14
+ baseSha: string;
13
15
  }
14
16
  export interface WorktreeCleanupResult {
15
17
  /** Whether changes were found in the worktree. */
package/dist/worktree.js CHANGED
@@ -16,9 +16,12 @@ import { join } from "node:path";
16
16
  */
17
17
  export function createWorktree(cwd, agentId) {
18
18
  // Verify we're in a git repo with at least one commit (HEAD must exist)
19
+ let baseSha;
19
20
  try {
20
21
  execFileSync("git", ["rev-parse", "--is-inside-work-tree"], { cwd, stdio: "pipe", timeout: 5000 });
21
- execFileSync("git", ["rev-parse", "HEAD"], { cwd, stdio: "pipe", timeout: 5000 });
22
+ baseSha = execFileSync("git", ["rev-parse", "HEAD"], { cwd, stdio: "pipe", timeout: 5000 })
23
+ .toString()
24
+ .trim();
22
25
  }
23
26
  catch {
24
27
  return undefined;
@@ -33,7 +36,7 @@ export function createWorktree(cwd, agentId) {
33
36
  stdio: "pipe",
34
37
  timeout: 30000,
35
38
  });
36
- return { path: worktreePath, branch };
39
+ return { path: worktreePath, branch, baseSha };
37
40
  }
38
41
  catch {
39
42
  // If worktree creation fails, return undefined (agent runs in normal cwd)
@@ -56,21 +59,30 @@ export function cleanupWorktree(cwd, worktree, agentDescription) {
56
59
  stdio: "pipe",
57
60
  timeout: 10000,
58
61
  }).toString().trim();
59
- if (!status) {
60
- // No changesremove worktree
61
- removeWorktree(cwd, worktree.path);
62
- return { hasChanges: false };
62
+ if (status) {
63
+ // Changes existstage, commit, and create a branch
64
+ execFileSync("git", ["add", "-A"], { cwd: worktree.path, stdio: "pipe", timeout: 10000 });
65
+ // Truncate description for commit message (no shell sanitization needed — execFileSync uses argv)
66
+ const safeDesc = agentDescription.slice(0, 200);
67
+ const commitMsg = `pi-agent: ${safeDesc}`;
68
+ execFileSync("git", ["commit", "--no-verify", "-m", commitMsg], {
69
+ cwd: worktree.path,
70
+ stdio: "pipe",
71
+ timeout: 10000,
72
+ });
73
+ }
74
+ else {
75
+ const currentSha = execFileSync("git", ["rev-parse", "HEAD"], {
76
+ cwd: worktree.path,
77
+ stdio: "pipe",
78
+ timeout: 5000,
79
+ }).toString().trim();
80
+ if (currentSha === worktree.baseSha) {
81
+ // No changes — remove worktree
82
+ removeWorktree(cwd, worktree.path);
83
+ return { hasChanges: false };
84
+ }
63
85
  }
64
- // Changes exist — stage, commit, and create a branch
65
- execFileSync("git", ["add", "-A"], { cwd: worktree.path, stdio: "pipe", timeout: 10000 });
66
- // Truncate description for commit message (no shell sanitization needed — execFileSync uses argv)
67
- const safeDesc = agentDescription.slice(0, 200);
68
- const commitMsg = `pi-agent: ${safeDesc}`;
69
- execFileSync("git", ["commit", "-m", commitMsg], {
70
- cwd: worktree.path,
71
- stdio: "pipe",
72
- timeout: 10000,
73
- });
74
86
  // Create a branch pointing to the worktree's HEAD.
75
87
  // If the branch already exists, append a suffix to avoid overwriting previous work.
76
88
  let branchName = worktree.branch;
@@ -0,0 +1,42 @@
1
+ Launch a new agent to handle complex, multi-step tasks autonomously. Each agent type has specific capabilities and tools available to it.
2
+
3
+ Available agent types and the tools they have access to:
4
+ {{typeList}}
5
+
6
+ Custom agents can be defined in .pi/agents/<name>.md (project) or {{agentDir}}/agents/<name>.md (global) — they are picked up automatically. Project-level agents override global ones. Creating a .md file with the same name as a default agent overrides it.
7
+
8
+ When using the Agent tool, specify a subagent_type parameter to select which agent type to use.
9
+
10
+ ## When not to use
11
+
12
+ If the target is already known, use a direct tool — `read` for a known path, `grep`/`find` for a specific symbol or string. Reserve this tool for open-ended questions that span the codebase, or tasks that match an available agent type.
13
+
14
+ ## Usage notes
15
+
16
+ - Always include a short (3-5 word) description summarizing what the agent will do (shown in UI).
17
+ - When you launch multiple agents for independent work, send them in a single message with multiple tool uses, with run_in_background: true on each, so they run concurrently. If the user specifies that they want agents run "in parallel", you MUST send a single message with multiple tool calls. Foreground calls run sequentially — only one executes at a time.
18
+ - When the agent is done, it returns a single message back to you. The result is not visible to the user — to show the user, send a text message with a concise summary.
19
+ - Trust but verify: an agent's summary describes what it intended to do, not necessarily what it did. When an agent writes or edits code, check the actual changes before reporting work as done.
20
+ - Use run_in_background for work you don't need immediately. You will be notified when it completes — do NOT poll or sleep waiting for it. Continue with other work or respond to the user instead.
21
+ - Foreground vs background: use foreground (default) when you need the agent's results before you can proceed. Use background when you have genuinely independent work to do in parallel.
22
+ - Use resume with an agent ID to continue a previous agent's work. A new (non-resume) Agent call starts a fresh agent with no memory of prior runs, so the prompt must be self-contained.
23
+ - Use steer_subagent to send mid-run messages to a running background agent.
24
+ - Clearly tell the agent whether you expect it to write code or just to do research (search, file reads, etc.), since it is not aware of the user's intent.
25
+ - If an agent's description says it should be used proactively, try to use it without the user having to ask for it first.
26
+ - Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
27
+ - Use thinking to control extended thinking level.
28
+ - Use inherit_context if the agent needs the parent conversation history.
29
+ - Use isolation: "worktree" to run the agent in an isolated git worktree (safe parallel file modifications). The worktree is automatically cleaned up if the agent makes no changes; otherwise the path and branch are returned in the result.{{scheduleGuideline}}
30
+
31
+ ## Writing the prompt
32
+
33
+ Provide clear, detailed prompts so the agent can work autonomously. Brief it like a smart colleague who just walked into the room — it hasn't seen this conversation, doesn't know what you've tried, doesn't understand why this task matters.
34
+ - Explain what you're trying to accomplish and why.
35
+ - Describe what you've already learned or ruled out.
36
+ - Give enough context about the surrounding problem that the agent can make judgment calls rather than just following a narrow instruction.
37
+ - If you need a short response, say so ("report in under 200 words").
38
+ - Lookups: hand over the exact command. Investigations: hand over the question — prescribed steps become dead weight when the premise is wrong.
39
+
40
+ Terse command-style prompts produce shallow, generic work.
41
+
42
+ **Never delegate understanding.** Don't write "based on your findings, fix the bug" or "based on the research, implement it." Those phrases push synthesis onto the agent instead of doing it yourself. Write prompts that prove you understood: include file paths, line numbers, what specifically to change.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tintinweb/pi-subagents",
3
- "version": "0.10.0",
3
+ "version": "0.10.2",
4
4
  "description": "A pi extension extension that brings smart Claude Code-style autonomous sub-agents to pi.",
5
5
  "author": "tintinweb",
6
6
  "license": "MIT",
@@ -296,6 +296,9 @@ export async function runAgent(
296
296
 
297
297
  // Resolve extensions/skills: isolated overrides to false
298
298
  const extensions = options.isolated ? false : config.extensions;
299
+ // Nulling excludes under isolated also suppresses the orphaned-exclude warning —
300
+ // isolation is an intentional override, not a misconfiguration.
301
+ const excludeExtensions = options.isolated ? undefined : config.excludeExtensions;
299
302
  const skills = options.isolated ? false : config.skills;
300
303
 
301
304
  // Skill preloading: when skills is string[], preload their content into prompt
@@ -373,18 +376,35 @@ export async function runAgent(
373
376
  ? parseExtensionsSpec(extensions, effectiveCwd)
374
377
  : undefined;
375
378
  const keepNames = extensionsSpec?.names ?? new Set<string>();
376
- // The override filters loaded extensions down to `keepNames`. It's only needed
377
- // when we're neither loading everything (`extensions: true` or a `"*"` wildcard)
378
- // nor nothing (`noExtensions`).
379
+ // `exclude_extensions:` is a denylist applied AFTER the include set exclude wins.
380
+ // Plain canonical names only (case-insensitive). Note: excluded extensions'
381
+ // factories still run once during reload() (see comment above) — exclusion
382
+ // suppresses handler binding and tool registration; it is not a sandbox.
383
+ const excludeNames = new Set((excludeExtensions ?? []).map((n) => n.toLowerCase()));
384
+ const hasExcludes = excludeNames.size > 0;
385
+ // The override filters loaded extensions down to `keepNames` minus `excludeNames`.
386
+ // It's only needed when we're neither loading everything without excludes
387
+ // (`extensions: true` or a `"*"` wildcard) nor nothing (`noExtensions`).
379
388
  const loadAll = extensions === true || extensionsSpec?.wildcard === true;
380
389
  const additionalExtensionPaths = extensionsSpec?.paths.length ? extensionsSpec.paths : undefined;
390
+ // Pre-filter discovered set, captured by the override — the exclude-typo warning
391
+ // must compare against this, not the surviving set (absence from survivors is
392
+ // an exclude *succeeding*).
393
+ let discoveredNames: Set<string> | undefined;
381
394
  const extensionsOverride: ((base: LoadExtensionsResult) => LoadExtensionsResult) | undefined =
382
- loadAll || noExtensions
395
+ noExtensions || (loadAll && !hasExcludes)
383
396
  ? undefined
384
- : (base) => ({
385
- ...base,
386
- extensions: base.extensions.filter((e) => keepNames.has(extensionCanonicalName(e.path))),
387
- });
397
+ : (base) => {
398
+ discoveredNames = new Set(base.extensions.map((e) => extensionCanonicalName(e.path)));
399
+ return {
400
+ ...base,
401
+ extensions: base.extensions.filter((e) => {
402
+ const name = extensionCanonicalName(e.path);
403
+ if (excludeNames.has(name)) return false; // exclude wins
404
+ return loadAll || keepNames.has(name);
405
+ }),
406
+ };
407
+ };
388
408
 
389
409
  const loader = new DefaultResourceLoader({
390
410
  cwd: effectiveCwd,
@@ -425,6 +445,27 @@ export async function runAgent(
425
445
  // - `tools: ext:foo` but foo isn't in the loaded set (because `extensions:`
426
446
  // didn't include it). Since v0.9, `ext:` no longer pulls extensions in;
427
447
  // loading is `extensions:`-authoritative.
448
+ // An exclude_extensions: alongside extensions: false is contradictory — nothing
449
+ // loads, so there is nothing to exclude.
450
+ if (hasExcludes && noExtensions) {
451
+ options.onToolActivity?.({
452
+ type: "end",
453
+ toolName: `extension-error:exclude_extensions has no effect for agent "${type}" — extensions: false loads nothing`,
454
+ });
455
+ }
456
+ // Exclude typo check: compares against the PRE-filter discovered set (an excluded
457
+ // name absent from the surviving set is the exclude working as intended). Also
458
+ // flags path-like and "*" entries — excludes are plain names only.
459
+ if (hasExcludes && discoveredNames) {
460
+ for (const name of excludeNames) {
461
+ if (!discoveredNames.has(name)) {
462
+ options.onToolActivity?.({
463
+ type: "end",
464
+ toolName: `extension-error:exclude_extensions: "${name}" for agent "${type}" did not match any discovered extension`,
465
+ });
466
+ }
467
+ }
468
+ }
428
469
  if (keepNames.size > 0 || extNames.size > 0) {
429
470
  const survivingNames = new Set(
430
471
  loader.getExtensions().extensions.map((e) => extensionCanonicalName(e.path)),
@@ -433,7 +474,9 @@ export async function runAgent(
433
474
  if (!survivingNames.has(name)) {
434
475
  options.onToolActivity?.({
435
476
  type: "end",
436
- toolName: `extension-error:extension "${name}" requested by agent "${type}" was not loaded`,
477
+ toolName: excludeNames.has(name)
478
+ ? `extension-error:extension "${name}" is in both extensions: and exclude_extensions: for agent "${type}" — exclude wins`
479
+ : `extension-error:extension "${name}" requested by agent "${type}" was not loaded`,
437
480
  });
438
481
  }
439
482
  }
@@ -441,7 +484,7 @@ export async function runAgent(
441
484
  if (!survivingNames.has(name)) {
442
485
  options.onToolActivity?.({
443
486
  type: "end",
444
- toolName: `extension-error:ext:${name} referenced by agent "${type}" but extension "${name}" is not loaded (add it to extensions:)`,
487
+ toolName: `extension-error:ext:${name} referenced by agent "${type}" but extension "${name}" is not loaded (check extensions:/exclude_extensions:)`,
445
488
  });
446
489
  }
447
490
  }
@@ -24,6 +24,15 @@ export const BUILTIN_TOOL_NAMES: string[] = [
24
24
  /** Unified runtime registry of all agents (defaults + user-defined). */
25
25
  const agents = new Map<string, AgentConfig>();
26
26
 
27
+ /** When true, DEFAULT_AGENTS are skipped during registration. */
28
+ let disableDefaults = false;
29
+
30
+ /** Check whether default agents are disabled. */
31
+ export function isDefaultsDisabled(): boolean { return disableDefaults; }
32
+
33
+ /** Set whether default agents are disabled. */
34
+ export function setDefaultsDisabled(b: boolean): void { disableDefaults = b; }
35
+
27
36
  /**
28
37
  * Register agents into the unified registry.
29
38
  * Starts with DEFAULT_AGENTS, then overlays user agents (overrides defaults with same name).
@@ -32,9 +41,11 @@ const agents = new Map<string, AgentConfig>();
32
41
  export function registerAgents(userAgents: Map<string, AgentConfig>): void {
33
42
  agents.clear();
34
43
 
35
- // Start with defaults
36
- for (const [name, config] of DEFAULT_AGENTS) {
37
- agents.set(name, config);
44
+ // Start with defaults (unless disabled via settings)
45
+ if (!disableDefaults) {
46
+ for (const [name, config] of DEFAULT_AGENTS) {
47
+ agents.set(name, config);
48
+ }
38
49
  }
39
50
 
40
51
  // Overlay user agents (overrides defaults with same name)
@@ -133,6 +144,7 @@ export function getConfig(type: string): {
133
144
  description: string;
134
145
  builtinToolNames: string[];
135
146
  extensions: true | string[] | false;
147
+ excludeExtensions?: string[];
136
148
  skills: true | string[] | false;
137
149
  promptMode: "replace" | "append";
138
150
  } {
@@ -144,6 +156,7 @@ export function getConfig(type: string): {
144
156
  description: config.description,
145
157
  builtinToolNames: config.builtinToolNames ?? BUILTIN_TOOL_NAMES,
146
158
  extensions: config.extensions,
159
+ excludeExtensions: config.excludeExtensions,
147
160
  skills: config.skills,
148
161
  promptMode: config.promptMode,
149
162
  };
@@ -157,6 +170,7 @@ export function getConfig(type: string): {
157
170
  description: gp.description,
158
171
  builtinToolNames: gp.builtinToolNames ?? BUILTIN_TOOL_NAMES,
159
172
  extensions: gp.extensions,
173
+ excludeExtensions: gp.excludeExtensions,
160
174
  skills: gp.skills,
161
175
  promptMode: gp.promptMode,
162
176
  };
@@ -60,6 +60,7 @@ function loadFromDir(dir: string, agents: Map<string, AgentConfig>, source: "pro
60
60
  extSelectors,
61
61
  disallowedTools: csvListOptional(fm.disallowed_tools),
62
62
  extensions: inheritField(fm.extensions ?? fm.inherit_extensions),
63
+ excludeExtensions: csvListOptional(fm.exclude_extensions),
63
64
  skills: inheritField(fm.skills ?? fm.inherit_skills),
64
65
  model: str(fm.model),
65
66
  thinking: str(fm.thinking) as ThinkingLevel | undefined,