pi-workflow-engine 0.13.0 → 1.0.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.
Files changed (92) hide show
  1. package/.pi/extensions/pi-workflow-engine/index.ts +106 -391
  2. package/.pi/extensions/pi-workflow-engine/src/advisory-challenge.ts +1 -1
  3. package/.pi/extensions/pi-workflow-engine/src/advisory-evidence.ts +8 -8
  4. package/.pi/extensions/pi-workflow-engine/src/advisory-schema.ts +7 -1
  5. package/.pi/extensions/pi-workflow-engine/src/agent-attempt.ts +48 -62
  6. package/.pi/extensions/pi-workflow-engine/src/agent-replay.ts +46 -67
  7. package/.pi/extensions/pi-workflow-engine/src/agent-retry.ts +14 -37
  8. package/.pi/extensions/pi-workflow-engine/src/agent-runner-types.ts +5 -11
  9. package/.pi/extensions/pi-workflow-engine/src/agent-runner.ts +15 -21
  10. package/.pi/extensions/pi-workflow-engine/src/agent-session-identity.ts +13 -43
  11. package/.pi/extensions/pi-workflow-engine/src/agent-session-providers.ts +17 -5
  12. package/.pi/extensions/pi-workflow-engine/src/agent-session.ts +93 -100
  13. package/.pi/extensions/pi-workflow-engine/src/agent-skills.ts +11 -43
  14. package/.pi/extensions/pi-workflow-engine/src/agent-workspace.ts +4 -18
  15. package/.pi/extensions/pi-workflow-engine/src/background-workflows.ts +33 -86
  16. package/.pi/extensions/pi-workflow-engine/src/cancellation.ts +1 -5
  17. package/.pi/extensions/pi-workflow-engine/src/concurrency.ts +25 -80
  18. package/.pi/extensions/pi-workflow-engine/src/discovery.ts +16 -23
  19. package/.pi/extensions/pi-workflow-engine/src/dynamax-shortcuts.ts +1 -4
  20. package/.pi/extensions/pi-workflow-engine/src/dynamax.ts +39 -98
  21. package/.pi/extensions/pi-workflow-engine/src/engine.ts +37 -80
  22. package/.pi/extensions/pi-workflow-engine/src/finalizers.ts +2 -10
  23. package/.pi/extensions/pi-workflow-engine/src/git.ts +55 -0
  24. package/.pi/extensions/pi-workflow-engine/src/guards.ts +8 -0
  25. package/.pi/extensions/pi-workflow-engine/src/identity-canonicalization.ts +7 -19
  26. package/.pi/extensions/pi-workflow-engine/src/inline-workflow.ts +171 -106
  27. package/.pi/extensions/pi-workflow-engine/src/journal.ts +0 -12
  28. package/.pi/extensions/pi-workflow-engine/src/model-profile-command.ts +37 -8
  29. package/.pi/extensions/pi-workflow-engine/src/model-profiles.ts +15 -66
  30. package/.pi/extensions/pi-workflow-engine/src/options.ts +97 -16
  31. package/.pi/extensions/pi-workflow-engine/src/perf.ts +43 -72
  32. package/.pi/extensions/pi-workflow-engine/src/process-runner.ts +14 -4
  33. package/.pi/extensions/pi-workflow-engine/src/progress-types.ts +2 -0
  34. package/.pi/extensions/pi-workflow-engine/src/progress.ts +79 -80
  35. package/.pi/extensions/pi-workflow-engine/src/provider-usage-limit.ts +28 -20
  36. package/.pi/extensions/pi-workflow-engine/src/replay-path-identity.ts +2 -7
  37. package/.pi/extensions/pi-workflow-engine/src/research-contract.ts +0 -1
  38. package/.pi/extensions/pi-workflow-engine/src/research-evidence.ts +27 -31
  39. package/.pi/extensions/pi-workflow-engine/src/resume-context.ts +75 -139
  40. package/.pi/extensions/pi-workflow-engine/src/review/github-pr-comments.ts +14 -31
  41. package/.pi/extensions/pi-workflow-engine/src/review/patch-validation.ts +3 -4
  42. package/.pi/extensions/pi-workflow-engine/src/review/review-actions.ts +1 -1
  43. package/.pi/extensions/pi-workflow-engine/src/review/review-diff-lines.ts +54 -0
  44. package/.pi/extensions/pi-workflow-engine/src/review/review-fix-workflow.ts +7 -9
  45. package/.pi/extensions/pi-workflow-engine/src/review/review-format.ts +51 -117
  46. package/.pi/extensions/pi-workflow-engine/src/review/review-handoff.ts +1 -1
  47. package/.pi/extensions/pi-workflow-engine/src/review/review-issues.ts +8 -13
  48. package/.pi/extensions/pi-workflow-engine/src/review/review-report.ts +3 -7
  49. package/.pi/extensions/pi-workflow-engine/src/review/review-results-viewer.ts +158 -241
  50. package/.pi/extensions/pi-workflow-engine/src/review/review-session-coordinator.ts +28 -38
  51. package/.pi/extensions/pi-workflow-engine/src/review/review-snapshot.ts +35 -44
  52. package/.pi/extensions/pi-workflow-engine/src/review-diff-target.ts +13 -14
  53. package/.pi/extensions/pi-workflow-engine/src/text.ts +29 -0
  54. package/.pi/extensions/pi-workflow-engine/src/tool-capabilities.ts +2 -1
  55. package/.pi/extensions/pi-workflow-engine/src/tool-source-identity.ts +16 -28
  56. package/.pi/extensions/pi-workflow-engine/src/tree-fingerprint.ts +34 -14
  57. package/.pi/extensions/pi-workflow-engine/src/types.ts +6 -0
  58. package/.pi/extensions/pi-workflow-engine/src/ui/kit.ts +193 -0
  59. package/.pi/extensions/pi-workflow-engine/src/ui/research-result-renderer.ts +177 -0
  60. package/.pi/extensions/pi-workflow-engine/src/ui/result-layout.ts +209 -0
  61. package/.pi/extensions/pi-workflow-engine/src/ui/workflow-format.ts +17 -44
  62. package/.pi/extensions/pi-workflow-engine/src/ui/workflow-inspector.ts +703 -235
  63. package/.pi/extensions/pi-workflow-engine/src/ui/workflow-picker.ts +48 -0
  64. package/.pi/extensions/pi-workflow-engine/src/ui/workflow-result-renderer.ts +239 -129
  65. package/.pi/extensions/pi-workflow-engine/src/ui/workflow-runs-browser.ts +461 -0
  66. package/.pi/extensions/pi-workflow-engine/src/ui/workflow-tool-renderer.ts +91 -0
  67. package/.pi/extensions/pi-workflow-engine/src/ui/workflow-widget.ts +316 -98
  68. package/.pi/extensions/pi-workflow-engine/src/usage.ts +21 -32
  69. package/.pi/extensions/pi-workflow-engine/src/workflow-advisory-utils.ts +56 -19
  70. package/.pi/extensions/pi-workflow-engine/src/workflow-execution.ts +19 -24
  71. package/.pi/extensions/pi-workflow-engine/src/workflow-module.ts +17 -37
  72. package/.pi/extensions/pi-workflow-engine/src/workflow-result-context.ts +79 -0
  73. package/.pi/extensions/pi-workflow-engine/src/workflow-run-background.ts +4 -14
  74. package/.pi/extensions/pi-workflow-engine/src/workflow-run-controller.ts +77 -63
  75. package/.pi/extensions/pi-workflow-engine/src/workflow-run-history.ts +42 -29
  76. package/.pi/extensions/pi-workflow-engine/src/workflow-run-record.ts +15 -14
  77. package/.pi/extensions/pi-workflow-engine/src/workflow-run-store.ts +27 -31
  78. package/.pi/extensions/pi-workflow-engine/src/workflow-usage-limit-scheduler.ts +1 -5
  79. package/.pi/extensions/pi-workflow-engine/src/workflows.ts +3 -6
  80. package/.pi/extensions/pi-workflow-engine/src/worktree.ts +48 -110
  81. package/.pi/extensions/pi-workflow-engine/workflows/code-review.ts +64 -93
  82. package/.pi/extensions/pi-workflow-engine/workflows/diagnose.ts +25 -67
  83. package/.pi/extensions/pi-workflow-engine/workflows/perf-review.ts +19 -53
  84. package/.pi/extensions/pi-workflow-engine/workflows/refactor-scout.ts +21 -49
  85. package/.pi/extensions/pi-workflow-engine/workflows/research.ts +74 -66
  86. package/USAGE.md +31 -17
  87. package/package.json +1 -2
  88. package/skills/workflow-code-review-actions/SKILL.md +12 -23
  89. package/.pi/extensions/pi-workflow-engine/src/diff-capture.ts +0 -81
  90. package/.pi/extensions/pi-workflow-engine/src/review/code-review-orchestration.ts +0 -24
  91. package/.pi/extensions/pi-workflow-engine/src/review/review-results-flow.ts +0 -52
  92. package/.pi/settings.json +0 -3
@@ -9,13 +9,9 @@ import {
9
9
  type AgentAttemptResult,
10
10
  type AgentReplayPlan,
11
11
  } from "./agent-replay.ts";
12
- import {
13
- resolveAgentModel,
14
- type ResolvedAgentModel,
15
- } from "./agent-session.ts";
12
+ import { resolveAgentModel } from "./agent-session.ts";
16
13
  import type {
17
14
  AgentExecutionOptions,
18
- AgentRunTags,
19
15
  RunContext,
20
16
  } from "./agent-runner-types.ts";
21
17
  import type { AgentResumeBaseContext } from "./resume-context.ts";
@@ -33,8 +29,6 @@ export type {
33
29
  CreateAgentSession,
34
30
  RunContext,
35
31
  } from "./agent-runner-types.ts";
36
- export { resolveAgentModel } from "./agent-session.ts";
37
- export type { ResolvedAgentModel, ResolvedAgentModelRequest } from "./agent-session.ts";
38
32
 
39
33
  /**
40
34
  * Run one subagent to completion in an isolated in-memory session.
@@ -54,7 +48,6 @@ export async function runAgent(
54
48
 
55
49
  const label = opts.label ?? "agent";
56
50
  const phase = opts.phase ?? "Workflow";
57
- const tags: AgentRunTags = { label, phase };
58
51
 
59
52
  return await rc.perf.time("agent.total_ms", async () => {
60
53
  throwIfAborted(rc.signal);
@@ -73,7 +66,7 @@ export async function runAgent(
73
66
  const agentRc: RunContext = { ...rc, signal: liveScope.signal };
74
67
  throwIfAborted(agentRc.signal);
75
68
  if (!isReplayEnabled(replay)) assertWorkflowBudgetAvailable(agentRc.budget);
76
- rc.progress.agentStart(opts.phase, label, rowId);
69
+ rc.progress.agentStart(rowId);
77
70
  if (replay.kind === "disabled") {
78
71
  rc.progress.log(`${label}: resume disabled for this call (${replay.reason})`);
79
72
  }
@@ -92,7 +85,7 @@ export async function runAgent(
92
85
  replay: attemptPlan,
93
86
  label,
94
87
  rowId,
95
- tags,
88
+ phase,
96
89
  admitLiveAgent: liveScope.admit,
97
90
  });
98
91
  } catch (error) {
@@ -108,23 +101,23 @@ export async function runAgent(
108
101
  providerRetries++;
109
102
  const delayMs = agentRetryDelayMs(providerRetries);
110
103
  rc.progress.log(`${label}: transient provider failure; retry ${providerRetries}/${agentRc.agentRetries} in ${delayMs}ms`);
111
- rc.perf.counter("agent.provider_retry", 1, tags);
112
- rc.perf.observe("agent.provider_retry_delay_ms", delayMs, tags);
113
- attemptPlan = { kind: "off" };
104
+ rc.perf.counter("agent.provider_retry");
105
+ rc.perf.observe("agent.provider_retry_delay_ms", delayMs);
106
+ // Keep the replay plan: the failed attempt recorded nothing, and the restart recaptures identity.
114
107
  await agentRc.retryScheduler.sleep(delayMs, agentRc.signal);
115
108
  continue;
116
109
  }
117
- const settlement = await settleAgentAttempt({ rc: agentRc, label, tags, replay: attemptPlan, outcome });
110
+ const settlement = await settleAgentAttempt({ rc: agentRc, label, outcome });
118
111
  if (settlement.kind === "retry-live") {
119
112
  attemptPlan = { kind: "off" };
120
113
  continue;
121
114
  }
122
- rc.progress.agentDone(label, rowId);
115
+ rc.progress.agentDone(rowId);
123
116
  return settlement.result;
124
117
  }
125
118
  },
126
119
  {
127
- onQueueWaitMs: (durationMs) => rc.perf.observe("agent.queue_wait_ms", durationMs, tags),
120
+ onQueueWaitMs: (durationMs) => rc.perf.observe("agent.queue_wait_ms", durationMs),
128
121
  signal: rc.signal,
129
122
  },
130
123
  );
@@ -132,13 +125,13 @@ export async function runAgent(
132
125
  let failure = error;
133
126
  if (liveScope.signal.aborted) failure = abortReason(liveScope.signal);
134
127
  if (rc.signal?.aborted) failure = abortReason(rc.signal);
135
- rc.progress.agentFailed(label, failure, rowId);
128
+ rc.progress.agentFailed(rowId, failure);
136
129
  rc.progress.log(`${label} failed: ${unknownErrorMessage(failure)}`);
137
130
  throw failure;
138
131
  } finally {
139
132
  liveScope.dispose();
140
133
  }
141
- }, tags);
134
+ });
142
135
  }
143
136
 
144
137
  function createAgentLiveScope(rc: RunContext, label: string) {
@@ -169,18 +162,19 @@ function resolveAgentRouting(
169
162
  rc: RunContext,
170
163
  opts: AgentExecutionOptions,
171
164
  label: string,
172
- ): { readonly model: ResolvedAgentModel["model"]; readonly thinkingLevel: AgentExecutionOptions["thinkingLevel"] } {
165
+ ): ReturnType<typeof resolveAgentModelProfile> {
173
166
  try {
174
167
  return resolveAgentModelProfile(
175
168
  {
176
169
  request: opts,
177
170
  profiles: rc.modelProfiles,
178
- resolveExplicitModel: (modelRef) => resolveAgentModel(modelRef, rc.modelRegistry, rc.hostModel).model,
171
+ resolveExplicitModel: (modelRef) => resolveAgentModel(modelRef, rc.modelRegistry),
179
172
  hostModel: rc.hostModel,
180
173
  },
181
174
  );
182
175
  } catch (error) {
183
- rc.progress.agentFailed(label, error);
176
+ // Routing runs before the call has a row; give the failure its own so it cannot land on a same-label sibling.
177
+ rc.progress.agentFailed(rc.progress.agentQueued(opts.phase, label), error);
184
178
  throw error;
185
179
  }
186
180
  }
@@ -6,11 +6,11 @@ import { normalizeWorkspaceReferences } from "./replay-path-identity.ts";
6
6
  import { unknownErrorMessage } from "./unknown-error.ts";
7
7
  import {
8
8
  captureEffectiveToolSourceIdentity,
9
+ nonEmptyString,
9
10
  type EffectiveToolSourceIdentity,
10
11
  type EffectiveToolSourceInfoLike,
11
- type ToolSourceFingerprintCache,
12
+ type ToolSourceIdentityOptions,
12
13
  } from "./tool-source-identity.ts";
13
- export type { EffectiveToolSourceIdentity, EffectiveToolSourceInfoLike } from "./tool-source-identity.ts";
14
14
 
15
15
  export interface EffectiveAgentModelLike {
16
16
  readonly provider: string;
@@ -78,14 +78,6 @@ export interface EffectiveAgentSessionIdentityOptions {
78
78
  readonly signal?: AbortSignal;
79
79
  }
80
80
 
81
- interface IdentityComponents {
82
- readonly runtimeVersion: string;
83
- readonly systemPromptFingerprint: string;
84
- readonly model: EffectiveAgentModelLike;
85
- readonly thinkingLevel: string;
86
- readonly tools: readonly EffectiveToolIdentity[];
87
- }
88
-
89
81
  /**
90
82
  * Capture the post-creation AgentSession state that determines whether a cached
91
83
  * agent result is safe to replay. Any missing or opaque executable identity
@@ -105,7 +97,13 @@ export async function captureEffectiveAgentSessionIdentity(
105
97
  const model = captureModel(session.model);
106
98
  const activeToolNames = captureActiveToolNames(session.getActiveToolNames());
107
99
  const toolInfoByName = indexToolInfo(session.getAllTools());
108
- const sourceCaptures: ToolSourceFingerprintCache = new Map();
100
+ const toolSourceOptions: ToolSourceIdentityOptions = {
101
+ runtimeVersion,
102
+ signal: options.signal,
103
+ sessionCwd,
104
+ cache: new Map(),
105
+ workspaceRoot,
106
+ };
109
107
  const tools: EffectiveToolIdentity[] = [];
110
108
 
111
109
  for (const name of activeToolNames) {
@@ -114,18 +112,10 @@ export async function captureEffectiveAgentSessionIdentity(
114
112
  if (!info) throw new Error(`active tool "${name}" is absent from the tool registry`);
115
113
  const definition = session.getToolDefinition(name);
116
114
  if (!definition) throw new Error(`active tool "${name}" has no executable definition`);
117
- tools.push(
118
- await captureToolIdentity(name, info, definition, {
119
- runtimeVersion,
120
- signal: options.signal,
121
- sessionCwd,
122
- sourceCaptures,
123
- workspaceRoot,
124
- }),
125
- );
115
+ tools.push(await captureToolIdentity(name, info, definition, toolSourceOptions));
126
116
  }
127
117
 
128
- const components: IdentityComponents = {
118
+ const components = {
129
119
  runtimeVersion,
130
120
  systemPromptFingerprint: hashIdentity(
131
121
  normalizeWorkspaceReferences(systemPrompt, { sessionCwd, workspaceRoot }),
@@ -147,19 +137,11 @@ export async function captureEffectiveAgentSessionIdentity(
147
137
  }
148
138
  }
149
139
 
150
- interface ToolCaptureOptions {
151
- readonly runtimeVersion: string;
152
- readonly sessionCwd: string;
153
- readonly workspaceRoot: string;
154
- readonly sourceCaptures: ToolSourceFingerprintCache;
155
- readonly signal?: AbortSignal;
156
- }
157
-
158
140
  async function captureToolIdentity(
159
141
  name: string,
160
142
  info: EffectiveToolInfoLike,
161
143
  definition: EffectiveToolDefinitionLike,
162
- options: ToolCaptureOptions,
144
+ options: ToolSourceIdentityOptions,
163
145
  ): Promise<EffectiveToolIdentity> {
164
146
  if (nonEmptyString(info.name, `registry name for active tool "${name}"`) !== name) {
165
147
  throw new Error(`tool registry returned a mismatched definition for "${name}"`);
@@ -175,13 +157,7 @@ async function captureToolIdentity(
175
157
  definition.prepareArguments === undefined
176
158
  ? null
177
159
  : inspectableFunctionSource(definition.prepareArguments, `argument-preparation handler for active tool "${name}"`);
178
- const source = await captureEffectiveToolSourceIdentity(info.sourceInfo, {
179
- runtimeVersion: options.runtimeVersion,
180
- sessionCwd: options.sessionCwd,
181
- workspaceRoot: options.workspaceRoot,
182
- cache: options.sourceCaptures,
183
- signal: options.signal,
184
- });
160
+ const source = await captureEffectiveToolSourceIdentity(info.sourceInfo, options);
185
161
 
186
162
  return {
187
163
  name,
@@ -244,12 +220,6 @@ function optionalString(value: string | undefined, label: string): string | null
244
220
  return stringValue(value, label);
245
221
  }
246
222
 
247
- function nonEmptyString(value: string, label: string): string {
248
- const result = stringValue(value, label);
249
- if (result.length === 0) throw new Error(`${label} is empty`);
250
- return result;
251
- }
252
-
253
223
  function stringValue(value: string, label: string): string {
254
224
  if (typeof value !== "string") throw new Error(`${label} is not a string`);
255
225
  return value;
@@ -8,12 +8,17 @@ type HostProviderRegistry = Pick<
8
8
  | "getRegisteredProviderConfig"
9
9
  | "getRegisteredProviderIds"
10
10
  | "isUsingOAuth"
11
- >;
11
+ > & {
12
+ /** pi 1.x keeps providers registered as objects (`pi.registerProvider(provider)`) apart from config providers. */
13
+ getRegisteredNativeProvider?(providerId: string): unknown;
14
+ };
15
+
16
+ type ChildModelRuntime = ModelRuntime & { registerNativeProvider?(provider: unknown): void };
12
17
 
13
18
  /** Mirror the host's live provider and selected-model auth state into a child runtime. */
14
19
  export async function synchronizeWorkflowModelRuntime(input: {
15
20
  readonly host: HostProviderRegistry;
16
- readonly child: ModelRuntime;
21
+ readonly child: ChildModelRuntime;
17
22
  readonly selectedModel: Model<Api> | undefined;
18
23
  readonly removeChildOnlyProviders: boolean;
19
24
  }): Promise<void> {
@@ -28,9 +33,16 @@ export async function synchronizeWorkflowModelRuntime(input: {
28
33
 
29
34
  for (const providerId of hostProviderIds) {
30
35
  const config = host.getRegisteredProviderConfig(providerId);
31
- if (!config) continue;
32
- child.unregisterProvider(providerId);
33
- child.registerProvider(providerId, config);
36
+ if (config) {
37
+ child.unregisterProvider(providerId);
38
+ child.registerProvider(providerId, config);
39
+ continue;
40
+ }
41
+ const native = host.getRegisteredNativeProvider?.(providerId);
42
+ if (native && child.registerNativeProvider) {
43
+ child.unregisterProvider(providerId);
44
+ child.registerNativeProvider(native);
45
+ }
34
46
  }
35
47
 
36
48
  const selectedProvider = selectedModel?.provider;
@@ -1,4 +1,4 @@
1
- import type { Api, Model } from "@earendil-works/pi-ai";
1
+ import type { Api, AssistantMessage, Model } from "@earendil-works/pi-ai";
2
2
  import {
3
3
  createAgentSessionFromServices,
4
4
  createAgentSessionServices,
@@ -12,7 +12,7 @@ import {
12
12
  import { prepareAgentSkillResources } from "./agent-skills.ts";
13
13
  import type {
14
14
  AgentExecutionOptions,
15
- AgentRunTags,
15
+ AgentProgress,
16
16
  AgentRunnerSession,
17
17
  RunContext,
18
18
  } from "./agent-runner-types.ts";
@@ -24,7 +24,8 @@ import {
24
24
  import { providerErrorFromMessages } from "./agent-retry.ts";
25
25
  import { synchronizeWorkflowModelRuntime } from "./agent-session-providers.ts";
26
26
  import { matchesAgentToolHint, WorkflowToolHintUnavailableError } from "./tool-capabilities.ts";
27
- import type { AgentToolHint } from "./types.ts";
27
+ import { truncateText } from "./text.ts";
28
+ import { WORKFLOW_TOOL_NAME, type AgentToolHint } from "./types.ts";
28
29
 
29
30
  export const FINAL_TOOL = "final_answer";
30
31
 
@@ -32,17 +33,12 @@ const SCHEMA_REPROMPT =
32
33
  `You ended your turn without calling the ${FINAL_TOOL} tool, so no result was recorded. ` +
33
34
  `Call ${FINAL_TOOL} now with your final answer as its arguments. Do not reply with plain text.`;
34
35
  const DEFAULT_SEARCH_BASE_TOOLS = ["read", "bash", "grep", "find", "ls"];
35
-
36
- export interface ResolvedAgentModelRequest {
37
- readonly ref: string;
38
- readonly provider: string;
39
- readonly id: string;
40
- }
41
-
42
- export interface ResolvedAgentModel {
43
- readonly model: Model<Api> | undefined;
44
- readonly requested: ResolvedAgentModelRequest | undefined;
45
- }
36
+ const RETRY_REASON_CHARS = 160;
37
+ const RETRY_DISABLED_WARNING =
38
+ "pi auto-retry is disabled (retry.enabled: false in pi settings), so subagents will not retry failed provider turns. " +
39
+ "pi-workflow-engine 0.12.0 and 0.13.0 wrote that setting into ~/.pi/agent/settings.json; remove it unless you disabled retry on purpose.";
40
+ /** Runs (keyed by their shared progress sink) already warned about disabled pi auto-retry. */
41
+ const retryDisabledWarnedRuns = new WeakSet<AgentProgress>();
46
42
 
47
43
  export interface AgentSessionHandle {
48
44
  readonly session: AgentRunnerSession;
@@ -51,19 +47,14 @@ export interface AgentSessionHandle {
51
47
  structuredResult(): unknown;
52
48
  }
53
49
 
54
- export function resolveAgentModel(
55
- modelRef: string | undefined,
56
- modelRegistry: Pick<ModelRegistry, "find">,
57
- hostModel: Model<Api> | undefined,
58
- ): ResolvedAgentModel {
59
- if (modelRef === undefined) return { model: hostModel, requested: undefined };
60
-
50
+ /** Resolve an explicit `agent({ model })` ref; unknown refs fail instead of falling back to another model. */
51
+ export function resolveAgentModel(modelRef: string, modelRegistry: Pick<ModelRegistry, "find">): Model<Api> {
61
52
  const parsed = parseAgentModelRef(modelRef);
62
53
  const found = modelRegistry.find(parsed.provider, parsed.id);
63
54
  if (!found) {
64
55
  throw new Error(`Agent model "${modelRef}" not found (resolved as ${parsed.provider}/${parsed.id}).`);
65
56
  }
66
- return { model: found, requested: parsed };
57
+ return found;
67
58
  }
68
59
 
69
60
  export async function openAgentSession(input: {
@@ -73,9 +64,8 @@ export async function openAgentSession(input: {
73
64
  readonly cwd: string;
74
65
  readonly model: Model<Api> | undefined;
75
66
  readonly label: string;
76
- readonly tags: AgentRunTags;
77
67
  }): Promise<AgentSessionHandle> {
78
- const { rc, prompt, opts, cwd, model, label, tags } = input;
68
+ const { rc, prompt, opts, cwd, model, label } = input;
79
69
  let captured = false;
80
70
  let structuredResult: unknown;
81
71
  const customTools: ToolDefinition[] = opts.schema
@@ -97,26 +87,22 @@ export async function openAgentSession(input: {
97
87
  let session: AgentRunnerSession | undefined;
98
88
  try {
99
89
  throwIfAborted(rc.signal);
100
- const resourceInput = { rc, prompt, opts, cwd, model, customTools, label };
101
- const resources = rc.createSession
102
- ? await prepareAgentSessionResources(resourceInput)
103
- : await rc.perf.time(
104
- "agent.session_resources_ms",
105
- () => prepareAgentSessionResources(resourceInput),
106
- tags,
107
- );
90
+ const resources = await rc.perf.time(
91
+ "agent.session_resources_ms",
92
+ () => prepareAgentSessionResources({ rc, prompt, opts, cwd, model, customTools, label }),
93
+ );
108
94
  const toolSelection = buildToolSelection(opts, resources.selectedSkills.length > 0);
109
95
  session = (
110
96
  await rc.perf.time(
111
97
  "agent.create_session_ms",
112
98
  () => resources.createSession(toolSelection.sessionOptions),
113
- tags,
114
99
  )
115
100
  ).session;
116
- session.setAutoRetryEnabled(false);
117
101
  const matchedToolHints = toolSelection.toolHints.length === 0
118
102
  ? new Set<AgentToolHint>()
119
103
  : applyDynamicToolHints(session, toolSelection);
104
+ // A nested run would get its own concurrency cap and budget, so subagents may start one only when allowlisted.
105
+ if (!opts.tools?.includes(WORKFLOW_TOOL_NAME)) withholdWorkflowTool(session);
120
106
  if (opts.requireToolHints) {
121
107
  const missing = toolSelection.toolHints.filter((hint) => !matchedToolHints.has(hint));
122
108
  if (missing.length > 0) throw new WorkflowToolHintUnavailableError(missing);
@@ -129,11 +115,8 @@ export async function openAgentSession(input: {
129
115
  structuredResult: () => structuredResult,
130
116
  };
131
117
  } catch (error) {
132
- if (session) {
133
- const activeSession = session;
134
- session = undefined;
135
- rc.perf.timeSync("agent.dispose_ms", () => activeSession.dispose(), tags);
136
- }
118
+ const created = session;
119
+ if (created) rc.perf.timeSync("agent.dispose_ms", () => created.dispose());
137
120
  throw error;
138
121
  }
139
122
  }
@@ -145,70 +128,79 @@ export async function promptAgentSession(input: {
145
128
  readonly opts: AgentExecutionOptions;
146
129
  readonly label: string;
147
130
  readonly rowId: number;
148
- readonly tags: AgentRunTags;
131
+ readonly phase: string;
149
132
  }): Promise<unknown> {
150
- const { rc, handle, prompt, opts, label, rowId, tags } = input;
133
+ const { rc, handle, prompt, opts, label, rowId, phase } = input;
151
134
  const { session } = handle;
135
+ throwIfAborted(rc.signal);
136
+ // pi drops a retried turn's failed message from session.messages; keep it so usage covers every attempt.
137
+ const retriedFailures: AssistantMessage[] = [];
138
+ let lastFailure: AssistantMessage | undefined;
152
139
  const unsubscribe = session.subscribe((event) => {
153
- if (event.type === "tool_execution_start" && event.toolName !== undefined && event.toolName !== FINAL_TOOL) {
154
- rc.progress.agentTool(label, event.toolName, rowId);
140
+ if (event.type === "tool_execution_start" && event.toolName !== FINAL_TOOL) {
141
+ rc.progress.agentTool(rowId, event.toolName);
142
+ } else if (event.type === "message_end" && event.message.role === "assistant" && event.message.stopReason === "error") {
143
+ lastFailure = event.message;
144
+ } else if (event.type === "auto_retry_start") {
145
+ if (lastFailure) retriedFailures.push(lastFailure);
146
+ lastFailure = undefined;
147
+ const reason = truncateText(event.errorMessage, RETRY_REASON_CHARS);
148
+ rc.progress.log(
149
+ `${label}: transient provider failure (${reason}); retrying turn ${event.attempt}/${event.maxAttempts} in ${event.delayMs}ms`,
150
+ );
151
+ rc.perf.counter("agent.turn_retry");
155
152
  }
156
153
  });
154
+ const unlinkPromptAbort = linkSessionAbort(rc.signal, session);
155
+ const promptSession = async (text: string) => {
156
+ await rc.perf.time("agent.prompt_ms", () => raceWithAbort(() => session.prompt(text), rc.signal));
157
+ const failure = providerErrorFromMessages(session.messages, {
158
+ pauseOnUsageLimit: rc.pauseOnProviderUsageLimit,
159
+ });
160
+ if (failure) throw failure;
161
+ };
157
162
 
158
163
  try {
159
- const finalPrompt = opts.schema
160
- ? `${prompt}\n\nWhen finished, return your result by calling the ${FINAL_TOOL} tool.`
161
- : prompt;
162
- throwIfAborted(rc.signal);
163
- const unlinkPromptAbort = linkSessionAbort(rc.signal, session);
164
- try {
165
- try {
166
- const promptSession = async (text: string) => {
167
- await rc.perf.time("agent.prompt_ms", () => raceWithAbort(() => session.prompt(text), rc.signal), tags);
168
- const failure = providerErrorFromMessages(session.messages, {
169
- pauseOnUsageLimit: rc.pauseOnProviderUsageLimit,
170
- });
171
- if (failure) throw failure;
172
- };
173
- await promptSession(finalPrompt);
174
- if (opts.schema) {
175
- for (let attempt = 0; !handle.hasStructuredResult() && attempt < MAX_SCHEMA_REPAIR_ATTEMPTS; attempt++) {
176
- throwIfAborted(rc.signal);
177
- session.setActiveToolsByName([FINAL_TOOL]);
178
- rc.progress.log(`${label}: no final answer; re-prompting (${attempt + 1}/${MAX_SCHEMA_REPAIR_ATTEMPTS})`);
179
- rc.perf.counter("agent.structured_reprompt", 1, tags);
180
- await promptSession(SCHEMA_REPROMPT);
181
- }
182
- }
183
-
164
+ await promptSession(
165
+ opts.schema ? `${prompt}\n\nWhen finished, return your result by calling the ${FINAL_TOOL} tool.` : prompt,
166
+ );
167
+ if (opts.schema && !handle.hasStructuredResult()) {
168
+ const activeTools = session.getActiveToolNames();
169
+ for (let attempt = 0; !handle.hasStructuredResult() && attempt < MAX_SCHEMA_REPAIR_ATTEMPTS; attempt++) {
184
170
  throwIfAborted(rc.signal);
185
- return rc.perf.timeSync(
186
- "agent.extract_result_ms",
187
- () => {
188
- if (!opts.schema) {
189
- return session.getLastAssistantText() ?? "";
190
- }
191
- if (!handle.hasStructuredResult()) {
192
- rc.progress.log(`${label}: no structured answer returned`);
193
- rc.perf.counter("agent.structured_missing", 1, tags);
194
- throw new WorkflowStructuredOutputError(label, MAX_SCHEMA_REPAIR_ATTEMPTS);
195
- }
196
- return handle.structuredResult();
197
- },
198
- tags,
199
- );
200
- } finally {
201
- rc.usage.recordAgentSession({ label, phase: tags.phase, messages: session.messages });
171
+ session.setActiveToolsByName([FINAL_TOOL]);
172
+ rc.progress.log(`${label}: no final answer; re-prompting (${attempt + 1}/${MAX_SCHEMA_REPAIR_ATTEMPTS})`);
173
+ rc.perf.counter("agent.structured_reprompt");
174
+ await promptSession(SCHEMA_REPROMPT);
202
175
  }
203
- } finally {
204
- unlinkPromptAbort();
176
+ // Narrowing also rebuilds pi's system prompt; restore both so replay validation sees the captured identity.
177
+ session.setActiveToolsByName(activeTools);
205
178
  }
179
+
180
+ throwIfAborted(rc.signal);
181
+ return rc.perf.timeSync(
182
+ "agent.extract_result_ms",
183
+ () => {
184
+ if (!opts.schema) {
185
+ return session.getLastAssistantText() ?? "";
186
+ }
187
+ if (!handle.hasStructuredResult()) {
188
+ rc.progress.log(`${label}: no structured answer returned`);
189
+ rc.perf.counter("agent.structured_missing");
190
+ throw new WorkflowStructuredOutputError(label, MAX_SCHEMA_REPAIR_ATTEMPTS);
191
+ }
192
+ return handle.structuredResult();
193
+ },
194
+ );
206
195
  } finally {
196
+ // Detach first: recording publishes usage to the UI and must not be able to skip listener cleanup.
197
+ unlinkPromptAbort();
207
198
  unsubscribe();
199
+ rc.usage.recordAgentSession({ label, phase, messages: [...retriedFailures, ...session.messages] });
208
200
  }
209
201
  }
210
202
 
211
- function parseAgentModelRef(modelRef: string): ResolvedAgentModelRequest {
203
+ function parseAgentModelRef(modelRef: string): { readonly provider: string; readonly id: string } {
212
204
  const normalized = modelRef.trim();
213
205
  if (normalized.length === 0) {
214
206
  throw new Error('Invalid agent model ref: expected a bare model id or "provider/id".');
@@ -218,13 +210,13 @@ function parseAgentModelRef(modelRef: string): ResolvedAgentModelRequest {
218
210
  }
219
211
 
220
212
  const slash = modelRef.indexOf("/");
221
- if (slash === -1) return { ref: modelRef, provider: "anthropic", id: modelRef };
213
+ if (slash === -1) return { provider: "anthropic", id: modelRef };
222
214
  const provider = modelRef.slice(0, slash);
223
215
  const id = modelRef.slice(slash + 1);
224
216
  if (provider.length === 0 || id.length === 0 || id.startsWith("/")) {
225
217
  throw new Error(`Invalid agent model ref "${modelRef}": expected "provider/id".`);
226
218
  }
227
- return { ref: modelRef, provider, id };
219
+ return { provider, id };
228
220
  }
229
221
 
230
222
  interface AgentSessionResources {
@@ -282,6 +274,10 @@ async function prepareAgentSessionResources(input: {
282
274
  for (const diagnostic of services.diagnostics) {
283
275
  rc.progress.log(`${label}: session ${diagnostic.type}: ${diagnostic.message}`);
284
276
  }
277
+ if (!services.settingsManager.getRetryEnabled() && !retryDisabledWarnedRuns.has(rc.progress)) {
278
+ retryDisabledWarnedRuns.add(rc.progress);
279
+ rc.progress.log(RETRY_DISABLED_WARNING);
280
+ }
285
281
  const selectedSkills = preparedSkills.resolve(services.resourceLoader);
286
282
  return {
287
283
  selectedSkills,
@@ -322,16 +318,8 @@ function buildToolSelection(opts: AgentExecutionOptions, skillsEnabled: boolean)
322
318
  ? ["read"]
323
319
  : undefined;
324
320
  const activeTools = buildToolList(opts, skillsEnabled, fallback);
325
- if (toolHints.length === 0) {
326
- return {
327
- sessionOptions: { tools: activeTools ? [...activeTools] : undefined },
328
- activeTools,
329
- toolHints,
330
- };
331
- }
332
-
333
321
  return {
334
- sessionOptions: { noTools: "builtin" },
322
+ sessionOptions: toolHints.length === 0 ? { tools: activeTools } : { noTools: "builtin" },
335
323
  activeTools,
336
324
  toolHints,
337
325
  };
@@ -350,11 +338,16 @@ function buildToolList(
350
338
  return allow;
351
339
  }
352
340
 
341
+ function withholdWorkflowTool(session: AgentRunnerSession): void {
342
+ const active = session.getActiveToolNames();
343
+ if (active.includes(WORKFLOW_TOOL_NAME)) session.setActiveToolsByName(active.filter((name) => name !== WORKFLOW_TOOL_NAME));
344
+ }
345
+
353
346
  function applyDynamicToolHints(
354
347
  session: AgentRunnerSession,
355
348
  selection: ToolSelection,
356
349
  ): ReadonlySet<AgentToolHint> {
357
- const active = new Set(selection.activeTools ?? []);
350
+ const active = new Set(selection.activeTools);
358
351
  const matched = new Set<AgentToolHint>();
359
352
  for (const tool of session.getAllTools()) {
360
353
  for (const hint of selection.toolHints) {
@@ -62,12 +62,13 @@ export function resolveAgentSkillRequest(prompt: string, explicitSkills: unknown
62
62
  }
63
63
 
64
64
  export function extractSkillSelectorsFromText(text: string): string[] {
65
- const selectors: string[] = [];
66
- collectPatternMatches(text, SLASH_SKILL_PATTERN, selectors, 1);
67
- collectListPatternMatches(text, SKILLS_FIELD_PATTERN, selectors);
68
- collectListPatternMatches(text, VERB_SKILL_LIST_PATTERN, selectors);
69
- collectPatternMatches(text, VERB_NAMED_SKILL_PATTERN, selectors, 2);
70
- return uniqueSelectors(selectors);
65
+ const selectors = [
66
+ ...Array.from(text.matchAll(SLASH_SKILL_PATTERN), (match) => match[1] ?? ""),
67
+ ...[...text.matchAll(SKILLS_FIELD_PATTERN), ...text.matchAll(VERB_SKILL_LIST_PATTERN)]
68
+ .flatMap((match) => parseSkillList(match[1] ?? "")),
69
+ ...Array.from(text.matchAll(VERB_NAMED_SKILL_PATTERN), (match) => match[2] ?? ""),
70
+ ].map(normalizeSkillSelector);
71
+ return [...new Set(selectors.filter((value) => value && !isGenericSkillWord(value)))];
71
72
  }
72
73
 
73
74
  export function normalizeSkillSelector(value: string): string {
@@ -179,23 +180,6 @@ export async function captureAgentSkillIdentities(
179
180
  return { kind: "verified", skills: identities };
180
181
  }
181
182
 
182
- function collectPatternMatches(text: string, pattern: RegExp, selectors: string[], captureIndex: number): void {
183
- pattern.lastIndex = 0;
184
- for (let match = pattern.exec(text); match !== null; match = pattern.exec(text)) {
185
- const value = match[captureIndex];
186
- if (value !== undefined) selectors.push(value);
187
- }
188
- }
189
-
190
- function collectListPatternMatches(text: string, pattern: RegExp, selectors: string[]): void {
191
- pattern.lastIndex = 0;
192
- for (let match = pattern.exec(text); match !== null; match = pattern.exec(text)) {
193
- const value = match[1];
194
- if (value === undefined) continue;
195
- selectors.push(...parseSkillList(value));
196
- }
197
- }
198
-
199
183
  function parseSkillList(fragment: string): string[] {
200
184
  const scoped = fragment.replace(PURPOSE_BOUNDARY_PATTERN, "");
201
185
  return scoped
@@ -208,9 +192,7 @@ function normalizeExplicitSkillSelectors(value: unknown): string[] {
208
192
  if (!Array.isArray(value)) {
209
193
  throw new Error("Invalid subagent skills option: expected an array of skill names.");
210
194
  }
211
- const seen = new Set<string>();
212
- const result: string[] = [];
213
- for (const item of value) {
195
+ const selectors = value.map((item: unknown) => {
214
196
  if (typeof item !== "string") {
215
197
  throw new Error("Invalid subagent skills option: every skill name must be a string.");
216
198
  }
@@ -218,11 +200,9 @@ function normalizeExplicitSkillSelectors(value: unknown): string[] {
218
200
  if (!normalized || isGenericSkillWord(normalized)) {
219
201
  throw new Error(`Invalid subagent skills option: \"${item}\" is not a valid skill name.`);
220
202
  }
221
- if (seen.has(normalized)) continue;
222
- seen.add(normalized);
223
- result.push(normalized);
224
- }
225
- return result;
203
+ return normalized;
204
+ });
205
+ return [...new Set(selectors)];
226
206
  }
227
207
 
228
208
  function logSkillDiagnostics(log: ((message: string) => void) | undefined, diagnostics: readonly ResourceDiagnostic[], requested: boolean): void {
@@ -233,18 +213,6 @@ function logSkillDiagnostics(log: ((message: string) => void) | undefined, diagn
233
213
  }
234
214
  }
235
215
 
236
- function uniqueSelectors(values: Iterable<string>): string[] {
237
- const seen = new Set<string>();
238
- const result: string[] = [];
239
- for (const value of values) {
240
- const normalized = normalizeSkillSelector(value);
241
- if (!normalized || isGenericSkillWord(normalized) || seen.has(normalized)) continue;
242
- seen.add(normalized);
243
- result.push(normalized);
244
- }
245
- return result;
246
- }
247
-
248
216
  function isGenericSkillWord(value: string): boolean {
249
217
  return value === "skill" || value === "skills" || value === "the" || value === "this" || value === "these" || value === "that";
250
218
  }