@yandy0725/pi-subagents 0.7.0 → 0.9.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.
@@ -40,6 +40,7 @@ export interface AgentToolRuntime {
40
40
  /** Narrow settings accessor — only the fields the Agent tool reads. */
41
41
  export type AgentToolSettings = {
42
42
  readonly defaultMaxTurns: number | undefined;
43
+ readonly defaultTimeoutSeconds: number | undefined;
43
44
  readonly maxConcurrent: number;
44
45
  readonly modelTiers?: ModelTierMap;
45
46
  };
@@ -148,6 +149,7 @@ Guidelines:
148
149
  - Use steer_subagent to send mid-run messages to a running background agent.
149
150
  - Use model to specify a different model (as "provider/modelId", or fuzzy e.g. "haiku", "sonnet").
150
151
  - Use thinking to control extended thinking level.
152
+ - Use timeout (seconds) as a hard safety net for tasks that might run away — the agent is killed at the deadline and output may be truncated.
151
153
  - Use inherit_context if the agent needs the parent conversation history.
152
154
  `,
153
155
  parameters: Type.Object({
@@ -177,6 +179,13 @@ Guidelines:
177
179
  minimum: 1,
178
180
  }),
179
181
  ),
182
+ timeout: Type.Optional(
183
+ Type.Number({
184
+ description:
185
+ "Hard wall-clock limit in seconds. When exceeded, the agent is terminated immediately and only partial output is returned. Use as a safety net for tasks that could run away (long builds, hanging commands); omit for unlimited.",
186
+ minimum: 1,
187
+ }),
188
+ ),
180
189
  run_in_background: Type.Optional(
181
190
  Type.Boolean({
182
191
  description:
@@ -32,6 +32,7 @@ export function spawnBackground(manager: BackgroundManagerDeps, params: Backgrou
32
32
  description: execution.description,
33
33
  model: execution.model,
34
34
  maxTurns: execution.effectiveMaxTurns,
35
+ timeoutSeconds: execution.effectiveTimeoutSeconds,
35
36
  inheritContext: execution.inheritContext,
36
37
  thinkingLevel: execution.thinking,
37
38
  isBackground: true,
@@ -74,6 +74,7 @@ export async function runForeground(
74
74
  description: execution.description,
75
75
  model: execution.model,
76
76
  maxTurns: execution.effectiveMaxTurns,
77
+ timeoutSeconds: execution.effectiveTimeoutSeconds,
77
78
  inheritContext: execution.inheritContext,
78
79
  thinkingLevel: execution.thinking,
79
80
  invocation: execution.agentInvocation,
@@ -6,7 +6,7 @@ import { type AgentDetails, formatTokens } from "../ui/display";
6
6
  export function getStatusNote(status: string): string {
7
7
  switch (status) {
8
8
  case "aborted":
9
- return " (aborted \u2014 max turns exceeded, output may be incomplete)";
9
+ return " (aborted \u2014 hard limit exceeded, output may be incomplete)";
10
10
  case "steered":
11
11
  return " (wrapped up \u2014 reached turn limit)";
12
12
  case "stopped":
@@ -85,7 +85,7 @@ export function renderFailed(details: AgentDetails, theme: Theme): string {
85
85
  if (details.status === "error") {
86
86
  line += `\n${theme.fg("error", ` \u23BF Error: ${details.error ?? "unknown"}`)}`;
87
87
  } else {
88
- line += `\n${theme.fg("warning", " \u23BF Aborted (max turns exceeded)")}`;
88
+ line += `\n${theme.fg("warning", " \u23BF Aborted (hard limit exceeded)")}`;
89
89
  }
90
90
  return line;
91
91
  }
@@ -10,7 +10,7 @@
10
10
  import type { Model } from "@earendil-works/pi-ai";
11
11
  import type { AgentTypeRegistry } from "../config/agent-types";
12
12
  import { resolveAgentInvocationConfig } from "../config/invocation-config";
13
- import { normalizeMaxTurns } from "../lifecycle/turn-limits";
13
+ import { normalizeMaxTurns, normalizeTimeoutSeconds } from "../lifecycle/turn-limits";
14
14
  import { resolveInvocationModel } from "../session/model-resolver";
15
15
  import type { AgentInvocation, SubagentType, ThinkingLevel } from "../types";
16
16
  import { type AgentDetails, buildInvocationTags, getDisplayName, getPromptModeLabel } from "../ui/display";
@@ -46,6 +46,7 @@ export interface SpawnExecution {
46
46
  description: string;
47
47
  model: Model<any> | undefined;
48
48
  effectiveMaxTurns: number | undefined;
49
+ effectiveTimeoutSeconds: number | undefined;
49
50
  thinking: ThinkingLevel | undefined;
50
51
  inheritContext: boolean;
51
52
  runInBackground: boolean;
@@ -81,7 +82,7 @@ export function resolveSpawnConfig(
81
82
  params: Record<string, unknown>,
82
83
  registry: AgentTypeRegistry,
83
84
  modelInfo: ModelInfo,
84
- settings: { readonly defaultMaxTurns: number | undefined },
85
+ settings: { readonly defaultMaxTurns: number | undefined; readonly defaultTimeoutSeconds: number | undefined },
85
86
  ): ResolvedSpawnConfig | SpawnConfigError {
86
87
  const rawType = params.subagent_type as SubagentType;
87
88
  const resolved = registry.resolveType(rawType);
@@ -118,11 +119,13 @@ export function resolveSpawnConfig(
118
119
  const modelName = resolveModelName(model ?? modelInfo.parentModel);
119
120
 
120
121
  const effectiveMaxTurns = normalizeMaxTurns(resolvedConfig.maxTurns ?? settings.defaultMaxTurns);
122
+ const effectiveTimeoutSeconds = normalizeTimeoutSeconds(resolvedConfig.timeout ?? settings.defaultTimeoutSeconds);
121
123
 
122
124
  const agentInvocation: AgentInvocation = {
123
125
  modelName,
124
126
  thinking,
125
127
  maxTurns: normalizeMaxTurns(resolvedConfig.maxTurns),
128
+ timeout: normalizeTimeoutSeconds(resolvedConfig.timeout),
126
129
  inheritContext,
127
130
  runInBackground,
128
131
  };
@@ -146,6 +149,7 @@ export function resolveSpawnConfig(
146
149
  description: params.description as string,
147
150
  model,
148
151
  effectiveMaxTurns,
152
+ effectiveTimeoutSeconds,
149
153
  thinking,
150
154
  inheritContext,
151
155
  runInBackground,
package/src/types.ts CHANGED
@@ -50,6 +50,8 @@ export interface AgentConfig extends AgentIdentity, AgentPromptConfig {
50
50
  model?: string;
51
51
  thinking?: ThinkingLevel;
52
52
  maxTurns?: number;
53
+ /** Wall-clock timeout in seconds; 0 = unlimited. */
54
+ timeout?: number;
53
55
  /** Default for spawn: fork parent conversation. undefined = caller decides. */
54
56
  inheritContext?: boolean;
55
57
  /** Default for spawn: run in background. undefined = caller decides. */
@@ -67,6 +69,8 @@ export interface AgentInvocation {
67
69
  modelName?: string;
68
70
  thinking?: ThinkingLevel;
69
71
  maxTurns?: number;
72
+ /** Normalized wall-clock timeout in seconds (display value; undefined = unlimited). */
73
+ timeout?: number;
70
74
  inheritContext?: boolean;
71
75
  runInBackground?: boolean;
72
76
  }
@@ -83,7 +87,6 @@ export interface SessionContext {
83
87
  readonly cwd: string;
84
88
  readonly model: unknown;
85
89
  readonly modelRegistry: ModelRegistry | undefined;
86
- getSystemPrompt(): string;
87
90
  readonly sessionManager: {
88
91
  getSessionFile(): string | undefined;
89
92
  getSessionId(): string;
package/src/ui/display.ts CHANGED
@@ -117,10 +117,10 @@ export function getDisplayName(type: SubagentType, registry: AgentConfigLookup):
117
117
  return config.displayName ?? config.name;
118
118
  }
119
119
 
120
- /** Short label for prompt mode: "twin" for append, nothing for replace (the default). */
120
+ /** Short label for prompt mode: "append" for append, nothing for replace (the default). */
121
121
  export function getPromptModeLabel(type: SubagentType, registry: AgentConfigLookup): string | undefined {
122
122
  const config = registry.resolveAgentConfig(type);
123
- return config.promptMode === "append" ? "twin" : undefined;
123
+ return config.promptMode === "append" ? "append" : undefined;
124
124
  }
125
125
 
126
126
  /** Mode label is not included — callers add it where they want it. */
@@ -131,6 +131,7 @@ export function buildInvocationTags(invocation: AgentInvocation | undefined): {
131
131
  if (invocation.inheritContext) tags.push("inherit context");
132
132
  if (invocation.runInBackground) tags.push("background");
133
133
  if (invocation.maxTurns != null) tags.push(`max turns: ${invocation.maxTurns}`);
134
+ if (invocation.timeout != null) tags.push(`timeout: ${invocation.timeout}s`);
134
135
  return { modelName: invocation.modelName, tags };
135
136
  }
136
137
 
@@ -4,9 +4,11 @@
4
4
  export interface SubagentsSettingsManager {
5
5
  readonly maxConcurrent: number;
6
6
  readonly defaultMaxTurns: number | undefined;
7
+ readonly defaultTimeoutSeconds: number | undefined;
7
8
  readonly graceTurns: number;
8
9
  applyMaxConcurrent(n: number): { message: string; level: "info" | "warning" };
9
10
  applyDefaultMaxTurns(n: number): { message: string; level: "info" | "warning" };
11
+ applyDefaultTimeoutSeconds(n: number): { message: string; level: "info" | "warning" };
10
12
  applyGraceTurns(n: number): { message: string; level: "info" | "warning" };
11
13
  }
12
14
 
@@ -32,6 +34,7 @@ export class SubagentsSettingsHandler {
32
34
  const choice = await ui.select("Settings", [
33
35
  `Max concurrency (current: ${this.settings.maxConcurrent})`,
34
36
  `Default max turns (current: ${this.settings.defaultMaxTurns ?? "unlimited"})`,
37
+ `Default timeout (current: ${this.settings.defaultTimeoutSeconds != null ? `${this.settings.defaultTimeoutSeconds}s` : "unlimited"})`,
35
38
  `Grace turns (current: ${this.settings.graceTurns})`,
36
39
  ]);
37
40
  if (!choice) return;
@@ -61,6 +64,20 @@ export class SubagentsSettingsHandler {
61
64
  ui.notify("Must be 0 (unlimited) or a positive integer.", "warning");
62
65
  }
63
66
  }
67
+ } else if (choice.startsWith("Default timeout")) {
68
+ const val = await ui.input(
69
+ "Default timeout in seconds (0 = unlimited)",
70
+ String(this.settings.defaultTimeoutSeconds ?? 0),
71
+ );
72
+ if (val) {
73
+ const n = parseInt(val, 10);
74
+ if (n >= 0) {
75
+ const toast = this.settings.applyDefaultTimeoutSeconds(n);
76
+ ui.notify(toast.message, toast.level);
77
+ } else {
78
+ ui.notify("Must be 0 (unlimited) or a positive integer.", "warning");
79
+ }
80
+ }
64
81
  } else if (choice.startsWith("Grace turns")) {
65
82
  const val = await ui.input("Grace turns after wrap-up steer", String(this.settings.graceTurns));
66
83
  if (val) {
@@ -1,59 +0,0 @@
1
- /**
2
- * workspace-bracket.ts — Owned prepare/dispose lifecycle for a child workspace.
3
- *
4
- * Captures the provider resolver (not the provider itself) so provider
5
- * resolution stays lazy at run-start. The prepared Workspace is held
6
- * privately; dispose() centralises the guard and addendum-unwrap so callers
7
- * never reach through to workspace.dispose().resultAddendum directly.
8
- *
9
- * dispose() deliberately does NOT catch errors — the best-effort try/catch
10
- * for failRun() belongs at the call site, preserving the per-caller semantics.
11
- */
12
-
13
- import type {
14
- Workspace,
15
- WorkspaceDisposeOutcome,
16
- WorkspacePrepareContext,
17
- WorkspaceProvider,
18
- } from "../lifecycle/workspace";
19
-
20
- /** Owns the child workspace lifecycle: prepare at run-start, dispose at run-end. */
21
- export class WorkspaceBracket {
22
- private prepared?: Workspace;
23
-
24
- constructor(private readonly resolveProvider: () => WorkspaceProvider | undefined) {}
25
-
26
- /**
27
- * Returns true when a workspace provider is currently registered.
28
- * Use to guard the `await prepare(...)` call and avoid an unnecessary
29
- * microtask boundary in the no-provider path.
30
- */
31
- hasProvider(): boolean {
32
- return this.resolveProvider() !== undefined;
33
- }
34
-
35
- /**
36
- * Resolve the registered provider and prepare the child workspace.
37
- * Returns the workspace's cwd, or undefined when no provider is registered
38
- * or the provider resolves to undefined.
39
- */
40
- async prepare(ctx: WorkspacePrepareContext): Promise<string | undefined> {
41
- const provider = this.resolveProvider();
42
- if (!provider) return undefined;
43
- this.prepared = await provider.prepare(ctx);
44
- return this.prepared?.cwd;
45
- }
46
-
47
- /**
48
- * Dispose the prepared workspace (if any) and return the result addendum
49
- * verbatim. Returns an empty string when no workspace was prepared or when
50
- * the workspace returns no addendum.
51
- *
52
- * Throws propagate — wrap in try/catch at the call site when best-effort
53
- * disposal is desired (e.g. failRun).
54
- */
55
- dispose(outcome: WorkspaceDisposeOutcome): string {
56
- if (!this.prepared) return "";
57
- return this.prepared.dispose(outcome)?.resultAddendum ?? "";
58
- }
59
- }
@@ -1,47 +0,0 @@
1
- /**
2
- * workspace.ts — The single generative extension seam (ADR 0002, Phase 16 Step 2).
3
- *
4
- * "Where does a child run, and what brackets the run?" is a strategy (git
5
- * worktree, container, tmpdir, remote sandbox), not core behavior. The core
6
- * needs only a working directory plus a disposal hook; the default — the
7
- * parent's cwd, with no setup/teardown — is always correct.
8
- *
9
- * Unlike the observational lifecycle events in child-lifecycle.ts, this is a
10
- * *generative* seam: a registered provider returns a value the core consumes
11
- * synchronously at run-start. The core has no knowledge of git or worktrees.
12
- */
13
-
14
- import type { SubagentStatus } from "../lifecycle/subagent";
15
- import type { AgentInvocation, SubagentType } from "../types";
16
-
17
- /** Context the core hands a provider when a child run starts. */
18
- export interface WorkspacePrepareContext {
19
- agentId: string;
20
- agentType: SubagentType;
21
- baseCwd: string;
22
- invocation?: AgentInvocation;
23
- }
24
-
25
- /** Outcome the core reports to a workspace when the run ends. */
26
- export interface WorkspaceDisposeOutcome {
27
- status: SubagentStatus;
28
- description: string;
29
- }
30
-
31
- /** What dispose may hand back for the core to fold into the child result. */
32
- export interface WorkspaceDisposeResult {
33
- /** Appended verbatim to the child's result text — the provider owns the wording. */
34
- resultAddendum?: string;
35
- }
36
-
37
- /** A prepared working directory plus its bracketed teardown. Born complete. */
38
- export interface Workspace {
39
- /** The working directory — already exists when the workspace is handed back. */
40
- readonly cwd: string;
41
- dispose(outcome: WorkspaceDisposeOutcome): WorkspaceDisposeResult | undefined;
42
- }
43
-
44
- /** The single generative seam: supplies a child's workspace. */
45
- export interface WorkspaceProvider {
46
- prepare(ctx: WorkspacePrepareContext): Promise<Workspace | undefined>;
47
- }