@fractaal/pi-goal-x 0.20.1 → 0.28.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 (95) hide show
  1. package/README.md +38 -293
  2. package/dist/goal-auditor.d.ts +7 -61
  3. package/dist/goal-auditor.js +98 -300
  4. package/dist/goal-auditor.js.map +1 -1
  5. package/dist/goal-contract.d.ts +205 -0
  6. package/dist/goal-contract.js +135 -0
  7. package/dist/goal-contract.js.map +1 -0
  8. package/dist/goal-transcript-events.d.ts +54 -0
  9. package/dist/goal-transcript-events.js +177 -0
  10. package/dist/goal-transcript-events.js.map +1 -0
  11. package/dist/goal-widget.d.ts +13 -0
  12. package/dist/goal-widget.js +45 -0
  13. package/dist/goal-widget.js.map +1 -0
  14. package/dist/goal.d.ts +8 -49
  15. package/dist/goal.js +581 -3648
  16. package/dist/goal.js.map +1 -1
  17. package/dist/legacy-migration.d.ts +61 -0
  18. package/dist/legacy-migration.js +306 -0
  19. package/dist/legacy-migration.js.map +1 -0
  20. package/docs/architecture.md +14 -232
  21. package/extensions/goal-auditor.ts +108 -363
  22. package/extensions/goal-contract.ts +149 -0
  23. package/extensions/goal-transcript-events.ts +194 -0
  24. package/extensions/goal-widget.ts +50 -0
  25. package/extensions/goal.ts +602 -3920
  26. package/extensions/legacy-migration.ts +340 -0
  27. package/package.json +46 -21
  28. package/dist/goal-compaction.d.ts +0 -10
  29. package/dist/goal-compaction.js +0 -104
  30. package/dist/goal-compaction.js.map +0 -1
  31. package/dist/goal-core.d.ts +0 -19
  32. package/dist/goal-core.js +0 -66
  33. package/dist/goal-core.js.map +0 -1
  34. package/dist/goal-draft.d.ts +0 -71
  35. package/dist/goal-draft.js +0 -201
  36. package/dist/goal-draft.js.map +0 -1
  37. package/dist/goal-ledger.d.ts +0 -139
  38. package/dist/goal-ledger.js +0 -306
  39. package/dist/goal-ledger.js.map +0 -1
  40. package/dist/goal-policy.d.ts +0 -127
  41. package/dist/goal-policy.js +0 -359
  42. package/dist/goal-policy.js.map +0 -1
  43. package/dist/goal-pool.d.ts +0 -17
  44. package/dist/goal-pool.js +0 -85
  45. package/dist/goal-pool.js.map +0 -1
  46. package/dist/goal-questionnaire.d.ts +0 -56
  47. package/dist/goal-questionnaire.js +0 -733
  48. package/dist/goal-questionnaire.js.map +0 -1
  49. package/dist/goal-record.d.ts +0 -94
  50. package/dist/goal-record.js +0 -155
  51. package/dist/goal-record.js.map +0 -1
  52. package/dist/goal-settings.d.ts +0 -56
  53. package/dist/goal-settings.js +0 -184
  54. package/dist/goal-settings.js.map +0 -1
  55. package/dist/goal-tool-names.d.ts +0 -20
  56. package/dist/goal-tool-names.js +0 -69
  57. package/dist/goal-tool-names.js.map +0 -1
  58. package/dist/prompts/goal-prompts.d.ts +0 -17
  59. package/dist/prompts/goal-prompts.js +0 -304
  60. package/dist/prompts/goal-prompts.js.map +0 -1
  61. package/dist/storage/goal-files.d.ts +0 -40
  62. package/dist/storage/goal-files.js +0 -320
  63. package/dist/storage/goal-files.js.map +0 -1
  64. package/dist/widgets/goal-escape-dialog.d.ts +0 -15
  65. package/dist/widgets/goal-escape-dialog.js +0 -118
  66. package/dist/widgets/goal-escape-dialog.js.map +0 -1
  67. package/dist/widgets/goal-notifications.d.ts +0 -5
  68. package/dist/widgets/goal-notifications.js +0 -9
  69. package/dist/widgets/goal-notifications.js.map +0 -1
  70. package/dist/widgets/goal-widget.d.ts +0 -58
  71. package/dist/widgets/goal-widget.js +0 -282
  72. package/dist/widgets/goal-widget.js.map +0 -1
  73. package/dist/widgets/task-list-overlay.d.ts +0 -8
  74. package/dist/widgets/task-list-overlay.js +0 -321
  75. package/dist/widgets/task-list-overlay.js.map +0 -1
  76. package/docs/agent-flow-design.md +0 -432
  77. package/docs/agentic-runtime-prd.md +0 -764
  78. package/docs/goal-ts-refactor-test-strategy.md +0 -82
  79. package/docs/pi-autoresearch-survey.md +0 -45
  80. package/extensions/goal-compaction.ts +0 -133
  81. package/extensions/goal-core.ts +0 -77
  82. package/extensions/goal-draft.ts +0 -263
  83. package/extensions/goal-ledger.ts +0 -354
  84. package/extensions/goal-policy.ts +0 -389
  85. package/extensions/goal-pool.ts +0 -98
  86. package/extensions/goal-questionnaire.ts +0 -779
  87. package/extensions/goal-record.ts +0 -256
  88. package/extensions/goal-settings.ts +0 -181
  89. package/extensions/goal-tool-names.ts +0 -76
  90. package/extensions/prompts/goal-prompts.ts +0 -308
  91. package/extensions/storage/goal-files.ts +0 -352
  92. package/extensions/widgets/goal-escape-dialog.ts +0 -146
  93. package/extensions/widgets/goal-notifications.ts +0 -9
  94. package/extensions/widgets/goal-widget.ts +0 -378
  95. package/extensions/widgets/task-list-overlay.ts +0 -389
@@ -1,436 +1,181 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { Static } from "@earendil-works/pi-ai";
4
- import { Type } from "@earendil-works/pi-ai";
5
- import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
6
- import type { Model } from "@earendil-works/pi-ai";
1
+ import { existsSync } from "node:fs";
2
+ import type { Api, Model } from "@earendil-works/pi-ai";
7
3
  import {
8
4
  createAgentSession,
5
+ createBashTool,
9
6
  createExtensionRuntime,
10
- defineTool,
11
7
  SessionManager,
12
8
  SettingsManager,
13
9
  type ExtensionContext,
14
10
  type ResourceLoader,
15
11
  } from "@earendil-works/pi-coding-agent";
16
- import type { GoalRecord, GoalTask, GoalTaskList } from "./goal-record.ts";
17
- import { loadGoalSettings, type GoalSettings } from "./goal-settings.ts";
18
-
19
- export interface AuditorProgress {
20
- /** Current tool being executed by the auditor, if any */
21
- currentTool?: string;
22
- /** Arguments passed to the current tool (truncated for display) */
23
- currentToolArgs?: string;
24
- /** When the current tool started (ms since epoch) */
25
- currentToolStartedAt?: number;
26
- /** Recent text output lines from the auditor's assistant messages */
27
- recentOutput: string[];
28
- /** Phase of the audit */
29
- phase: "running" | "tool_executing" | "producing_report" | "thinking" | "done";
30
- /** Elapsed ms since audit started */
31
- elapsedMs: number;
32
- /** Current step label shown to the user (e.g. "Inspecting files...") */
33
- label?: string;
34
- /** Completion percentage from 0 to 100 */
35
- percentage?: number;
36
- }
37
-
38
- export type AuditorProgressCallback = (progress: AuditorProgress) => void;
12
+ import type { Goal } from "./goal-contract.ts";
39
13
 
40
14
  export interface GoalAuditorResult {
41
15
  approved: boolean;
42
- disapproved: boolean;
43
16
  output: string;
44
17
  model?: string;
45
- thinkingLevel?: ThinkingLevel;
46
18
  error?: string;
47
19
  }
48
20
 
49
- const THINKING_LEVELS = new Set(["off", "minimal", "low", "medium", "high", "xhigh"]);
50
-
51
- function asNonEmptyString(value: unknown): string | undefined {
52
- return typeof value === "string" && value.trim() ? value.trim() : undefined;
53
- }
54
-
55
- function asThinkingLevel(value: unknown): ThinkingLevel | undefined {
56
- const text = asNonEmptyString(value);
57
- return text && THINKING_LEVELS.has(text) ? text as ThinkingLevel : undefined;
58
- }
59
-
60
-
61
-
62
- export function parseAuditorDecision(output: string): { approved: boolean; disapproved: boolean } {
63
- const approved = /<approved\s*\/>/.test(output);
64
- const disapproved = /<disapproved\s*\/>/.test(output);
65
- return { approved: approved && !disapproved, disapproved };
21
+ function escapePromptPayload(value: string): string {
22
+ return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
66
23
  }
67
24
 
68
- export interface AuditorVerificationEvidence {
69
- /** The agent's verification summary describing what was checked. */
70
- summary: string;
71
- /** The goal's verification contract (what the agent was required to verify), if any. */
72
- contract?: string;
73
- }
74
-
75
- function renderAuditorTaskTree(tasks: GoalTask[], indent: number): string[] {
76
- const prefix = " ".repeat(indent);
77
- const lines: string[] = [];
78
- for (const task of tasks) {
79
- const marker = task.status === "complete" ? "[x]" : task.status === "skipped" ? "[~]" : "[ ]";
80
- lines.push(`${prefix}${marker} ${task.id}: ${task.title}`);
81
- if (task.subtasks && task.subtasks.length > 0) {
82
- lines.push(...renderAuditorTaskTree(task.subtasks, indent + 1));
83
- }
84
- }
85
- return lines;
25
+ export function parseAuditorDecision(output: string): boolean {
26
+ const lines = output.split("\n").map((line) => line.trim()).filter(Boolean);
27
+ return lines.at(-1) === "<approved/>";
86
28
  }
87
29
 
88
- function countAuditorTasks(tasks: GoalTask[]): { total: number; complete: number; skipped: number; pending: number } {
89
- let total = 0;
90
- let complete = 0;
91
- let skipped = 0;
92
- for (const t of tasks) {
93
- total++;
94
- if (t.status === "complete") complete++;
95
- else if (t.status === "skipped") skipped++;
96
- if (t.subtasks && t.subtasks.length > 0) {
97
- const child = countAuditorTasks(t.subtasks);
98
- total += child.total;
99
- complete += child.complete;
100
- skipped += child.skipped;
101
- }
102
- }
103
- return { total, complete, skipped, pending: total - complete - skipped };
104
- }
105
-
106
- function taskSummaryBlock(taskList?: GoalTaskList | null): string {
107
- if (!taskList || taskList.tasks.length === 0) return "";
108
- const { total, complete, skipped, pending } = countAuditorTasks(taskList.tasks);
109
- const lines: string[] = [`Tasks: ${complete}/${total} complete${skipped > 0 ? `, ${skipped} skipped` : ""}`];
110
- lines.push(...renderAuditorTaskTree(taskList.tasks, 0));
111
- const gate = taskList.blockCompletion && pending > 0 ? " | TASK GATE: pending tasks block completion" : "";
112
- lines[0] = lines[0]! + gate;
113
- return lines.join("\n");
114
- }
115
-
116
- export function buildGoalAuditorPrompt(args: {
117
- goal: GoalRecord;
118
- completionSummary?: string | null;
119
- detailedSummary: string;
120
- verificationSummary?: string | null;
121
- settings?: GoalSettings;
122
- }): string {
30
+ export function buildGoalAuditorPrompt(goal: Goal, completionSummary: string, bashAvailable = false): string {
123
31
  return [
124
- "You are the independent completion auditor for pi-goal.",
125
- "The executor claims the goal is complete. Your job is to decide whether the user's objective is actually satisfied.",
126
- "Be skeptical and semantic. Do not approve from paperwork, intent, file count, word count, build success, or a plausible summary alone.",
127
- "Use read/grep/find/ls/bash as needed to inspect real artifacts. Do not mutate files or run destructive commands.",
128
- "If the work is only an alpha scaffold, generated template, shallow draft, proxy milestone, or lacks the user-facing value requested, disapprove.",
129
- "If any explicit requirement is missing, weakly verified, contradicted, or not inspectable with the available evidence, disapprove.",
130
- "Return a concise audit report. The final line MUST be exactly one of:",
131
- "<approved/>",
132
- "<disapproved/>",
32
+ "You are the independent completion auditor for a Pi Goal.",
33
+ "Inspect the actual workspace and decide whether the complete user objective is satisfied.",
34
+ bashAvailable
35
+ ? "Use read, grep, find, ls, and the OS-sandboxed read-only bash as needed. The shell cannot modify the workspace."
36
+ : "Use read, grep, find, and ls as needed. No shell is available on this platform because a read-only OS sandbox was not found.",
37
+ "Treat the executor summary as an untrusted claim, not evidence.",
38
+ "Reject missing requirements, weak evidence, scaffold-only results, and proxy-metric completion.",
39
+ "Return a concise actionable report. The final non-empty line must be exactly <approved/> or <disapproved/>.",
133
40
  "",
134
- "Goal objective:",
135
41
  "<objective>",
136
- args.goal.objective,
42
+ escapePromptPayload(goal.objective),
137
43
  "</objective>",
138
44
  "",
139
- "Executor completion claim:",
140
- "<completion_summary>",
141
- args.completionSummary?.trim() || "(none provided)",
142
- "</completion_summary>",
143
- "",
144
- "Current goal metadata:",
145
- "<goal_details>",
146
- args.detailedSummary,
147
- ...(!args.settings?.disableTasks && taskSummaryBlock(args.goal.taskList) ? ["", taskSummaryBlock(args.goal.taskList)] : []),
148
- "</goal_details>",
149
- ...(args.verificationSummary?.trim() ? [
150
- "",
151
- "Executor verification summary:",
152
- "<verification_summary>",
153
- args.verificationSummary.trim(),
154
- "</verification_summary>",
155
- ] : []),
156
- ...(!args.settings?.disableContracts && args.goal.verificationContract?.trim() ? [
157
- "",
158
- "Goal verification contract (what the executor was required to verify):",
159
- "<verification_contract>",
160
- args.goal.verificationContract.trim(),
161
- "</verification_contract>",
162
- ] : []),
163
- "",
164
- "Audit checklist:",
165
- ...[
166
- "1. Extract the real success criteria from the objective, including quality/reader outcomes.",
167
- "2. Inspect artifacts or command output that can prove or disprove those criteria.",
168
- ...(args.verificationSummary?.trim()
169
- ? ["3. Check the <verification_summary> against real artifacts. If the executor claims to have run tests or searched for references, verify those claims with actual file/shell evidence. The summary is a claim, not proof — cross-check it."]
170
- : []),
171
- ...(!args.settings?.disableContracts && args.goal.verificationContract?.trim()
172
- ? ["4. Verify that the executor has satisfied every item in the <verification_contract>. If any item is missing or weakly addressed, disapprove."]
173
- : []),
174
- "5. Explain missing or weak evidence, especially scaffold-vs-final quality gaps.",
175
- "6. End with exactly <approved/> only if the objective is truly complete; otherwise end with exactly <disapproved/>.",
176
- ],
177
- "",
178
- "Progress reporting:",
179
- "You have the report_auditor_progress tool available to report your progress to the user.",
180
- "Please use it at natural phase boundaries:",
181
- " - When starting: report_auditor_progress(label='Starting audit...', percentage=0)",
182
- " - When beginning file inspection: report_auditor_progress(label='Inspecting files...', percentage=25)",
183
- " - When verifying success criteria: report_auditor_progress(label='Verifying success criteria...', percentage=50)",
184
- " - When evaluating evidence: report_auditor_progress(label='Evaluating evidence...', percentage=75)",
185
- " - When producing final report: report_auditor_progress(label='Producing report...', percentage=90)",
186
- "This is purely for user visibility and does not affect the audit outcome.",
45
+ "<executor_summary>",
46
+ escapePromptPayload(completionSummary),
47
+ "</executor_summary>",
187
48
  ].join("\n");
188
49
  }
189
50
 
190
- /** Tool name for auditor progress reporting */
191
- export const REPORT_AUDITOR_PROGRESS_TOOL_NAME = "report_auditor_progress";
192
-
193
- /** Parameters for the report_auditor_progress tool */
194
- export const reportAuditorProgressParams = Type.Object({
195
- label: Type.String({ description: "Current step label describing what the auditor is doing (e.g. 'Inspecting files...', 'Verifying success criteria...', 'Producing report...')" }),
196
- percentage: Type.Number({ description: "Completion percentage from 0 to 100", minimum: 0, maximum: 100 }),
197
- });
198
-
199
- function makeAuditorResourceLoader(): ResourceLoader {
51
+ function emptyResourceLoader(): ResourceLoader {
200
52
  return {
201
53
  getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),
202
54
  getSkills: () => ({ skills: [], diagnostics: [] }),
203
55
  getPrompts: () => ({ prompts: [], diagnostics: [] }),
204
56
  getThemes: () => ({ themes: [], diagnostics: [] }),
205
57
  getAgentsFiles: () => ({ agentsFiles: [] }),
206
- getSystemPrompt: () => [
207
- "You are a read-only completion auditor running in an isolated pi agent session.",
208
- "Inspect the repository and decide whether the claimed goal completion is genuinely satisfied.",
209
- "Never modify files. Never approve unless the actual user objective is complete.",
210
- "",
211
- "You have the report_auditor_progress tool available. Use it to report your audit progress",
212
- "to the user at natural phase boundaries (starting, inspecting files, verifying criteria,",
213
- "producing report). This helps the user understand what the auditor is doing and how far",
214
- "along it is.",
215
- ].join("\n"),
58
+ getSystemPrompt: () => "You are a read-only completion auditor. Inspect real evidence and never modify the workspace.",
59
+ getSystemPromptSource: () => undefined,
216
60
  getAppendSystemPrompt: () => [],
217
- extendResources: () => {},
61
+ getAppendSystemPromptSources: () => [],
62
+ extendResources: () => {},
218
63
  reload: async () => {},
219
- };
64
+ } as unknown as ResourceLoader;
220
65
  }
221
66
 
222
- function resolveAuditorModel(ctx: ExtensionContext, config: GoalSettings): { model: Model<any> | undefined; error?: string } {
223
- if (!config.model && !config.provider) return { model: ctx.model };
224
- if (config.provider && config.model) {
225
- const model = ctx.modelRegistry.find(config.provider, config.model);
226
- return model ? { model } : { model: undefined, error: `Configured auditor model not found: ${config.provider}/${config.model}` };
227
- }
228
- if (config.provider) {
229
- const matches = ctx.modelRegistry.getAvailable().filter((model) => model.provider === config.provider);
230
- return matches[0] ? { model: matches[0] } : { model: undefined, error: `No available auditor model for provider: ${config.provider}` };
231
- }
232
- if (!config.model) return { model: ctx.model };
233
- const slash = config.model.indexOf("/");
234
- if (slash > 0) {
235
- const provider = config.model.slice(0, slash);
236
- const modelId = config.model.slice(slash + 1);
237
- const model = ctx.modelRegistry.find(provider, modelId);
238
- return model ? { model } : { model: undefined, error: `Configured auditor model not found: ${config.model}` };
239
- }
240
- const matches = ctx.modelRegistry.getAvailable().filter((model) => model.id === config.model || model.name === config.model);
241
- if (matches.length === 1) return { model: matches[0] };
242
- return { model: undefined, error: `Configured auditor model is ambiguous or unavailable: ${config.model}` };
67
+ function shellArgument(value: string): string {
68
+ return `'${value.replace(/'/g, `'"'"'`)}'`;
69
+ }
70
+
71
+ export function createReadOnlyAuditorBash(cwd: string): ReturnType<typeof createBashTool> | null {
72
+ const bubblewrap = ["/usr/bin/bwrap", "/bin/bwrap"].find(existsSync);
73
+ if (!bubblewrap) return null;
74
+ return createBashTool(cwd, {
75
+ spawnHook: ({ command }) => ({
76
+ command: [
77
+ "exec",
78
+ shellArgument(bubblewrap),
79
+ "--die-with-parent --new-session --unshare-all",
80
+ "--ro-bind / / --dev /dev --proc /proc --tmpfs /run",
81
+ "--tmpfs /tmp --dir /tmp/auditor-home",
82
+ `--ro-bind ${shellArgument(cwd)} /mnt --chdir /mnt`,
83
+ "--setenv HOME /tmp/auditor-home --setenv TMPDIR /tmp --setenv CI 1",
84
+ `-- /bin/bash -lc ${shellArgument(command)}`,
85
+ ].join(" "),
86
+ cwd: "/",
87
+ env: {
88
+ PATH: process.env.PATH,
89
+ LANG: process.env.LANG,
90
+ LC_ALL: process.env.LC_ALL,
91
+ TERM: process.env.TERM,
92
+ },
93
+ }),
94
+ });
243
95
  }
244
96
 
245
- function modelLabel(model: Model<any> | undefined): string | undefined {
97
+ function modelLabel(model: Model<Api> | undefined): string | undefined {
246
98
  return model ? `${model.provider}/${model.id}` : undefined;
247
99
  }
248
100
 
101
+ function modelOptions(ctx: ExtensionContext): Record<string, unknown> {
102
+ const registry = ctx.modelRegistry as unknown as { runtime?: unknown };
103
+ return registry.runtime
104
+ ? { modelRegistry: ctx.modelRegistry, modelRuntime: registry.runtime }
105
+ : { modelRegistry: ctx.modelRegistry };
106
+ }
107
+
249
108
  export async function runGoalCompletionAuditor(args: {
250
109
  ctx: ExtensionContext;
251
- goal: GoalRecord;
252
- completionSummary?: string | null;
253
- detailedSummary: string;
254
- verificationSummary?: string | null;
255
- settings?: GoalSettings;
110
+ goal: Goal;
111
+ completionSummary: string;
256
112
  signal?: AbortSignal;
257
- onProgress?: AuditorProgressCallback;
258
- /**
259
- * Optional factory for creating the auditor agent session.
260
- * Exposed for testing so a mock/controllable session can be injected.
261
- * Defaults to the real createAgentSession from @earendil-works/pi-coding-agent.
262
- */
263
113
  createSession?: typeof createAgentSession;
264
114
  }): Promise<GoalAuditorResult> {
265
- const config = loadGoalSettings(args.ctx.cwd);
266
- const resolved = resolveAuditorModel(args.ctx, config);
267
- const model = resolved.model;
268
- const thinkingLevel = config.thinkingLevel;
269
- const outputParts: string[] = [];
270
- if (resolved.error) {
271
- return { approved: false, disapproved: true, output: "", model: modelLabel(model), thinkingLevel, error: resolved.error };
272
- }
115
+ const model = args.ctx.model;
116
+ if (!model) return { approved: false, output: "", error: "No active model is available for the completion auditor." };
117
+ const output: string[] = [];
118
+ let nestedSession: Awaited<ReturnType<typeof createAgentSession>>["session"] | undefined;
273
119
  try {
274
120
  const createSession = args.createSession ?? createAgentSession;
275
- const startedAt = Date.now();
276
- const progress: AuditorProgress = {
277
- recentOutput: [],
278
- phase: "running",
279
- elapsedMs: 0,
280
- };
281
- function emitProgress(): void {
282
- progress.elapsedMs = Date.now() - startedAt;
283
- args.onProgress?.({ ...progress });
284
- }
285
-
286
- // Build the report_auditor_progress tool, capturing the progress state
287
- const reportProgressTool = defineTool({
288
- name: REPORT_AUDITOR_PROGRESS_TOOL_NAME,
289
- label: "Report Auditor Progress",
290
- description: "Report current progress of the audit to the user. Call this at natural phase boundaries (starting, inspecting files, verifying criteria, producing report) to keep the user informed.",
291
- promptSnippet: "Report current audit progress (step label and completion percentage) to the user.",
292
- promptGuidelines: [
293
- "Use report_auditor_progress at natural phase boundaries during the audit:",
294
- " - When starting the audit: label='Starting audit...' percentage=0",
295
- " - When beginning file inspection: label='Inspecting files...' percentage=25",
296
- " - When verifying success criteria: label='Verifying success criteria...' percentage=50",
297
- " - When evaluating evidence: label='Evaluating evidence...' percentage=75",
298
- " - When producing final report: label='Producing report...' percentage=90",
299
- "This is purely for user visibility — it does not affect the audit outcome.",
300
- "Do not call this tool more than once every few seconds to avoid flooding.",
301
- ],
302
- parameters: reportAuditorProgressParams,
303
- executionMode: "sequential",
304
- async execute(_toolCallId, params) {
305
- const { label, percentage } = params as Static<typeof reportAuditorProgressParams>;
306
- progress.label = label;
307
- progress.percentage = percentage;
308
- progress.phase = "running";
309
- emitProgress();
310
- return {
311
- content: [{ type: "text", text: `Progress reported: ${label} (${percentage}%)` }],
312
- details: {},
313
- };
314
- },
315
- });
316
-
121
+ const auditorBash = createReadOnlyAuditorBash(args.ctx.cwd);
122
+ type AuditorOptions = NonNullable<Parameters<typeof createAgentSession>[0]>;
123
+ type AuditorThinkingLevel = NonNullable<AuditorOptions["thinkingLevel"]>;
124
+ const thinkingLevel = (args.ctx as unknown as { thinkingLevel?: AuditorThinkingLevel }).thinkingLevel;
317
125
  const { session } = await createSession({
318
126
  cwd: args.ctx.cwd,
319
127
  model,
320
128
  thinkingLevel,
321
- modelRegistry: args.ctx.modelRegistry,
322
- resourceLoader: makeAuditorResourceLoader(),
129
+ ...modelOptions(args.ctx),
130
+ resourceLoader: emptyResourceLoader(),
323
131
  sessionManager: SessionManager.inMemory(args.ctx.cwd),
324
132
  settingsManager: SettingsManager.inMemory({ compaction: { enabled: false } }),
325
- tools: ["read", "grep", "find", "ls", "bash", REPORT_AUDITOR_PROGRESS_TOOL_NAME],
326
- customTools: [reportProgressTool],
327
- });
133
+ tools: ["read", "grep", "find", "ls", ...(auditorBash ? ["bash" as const] : [])],
134
+ ...(auditorBash ? { customTools: [auditorBash] } : {}),
135
+ } as Parameters<typeof createAgentSession>[0]);
136
+ nestedSession = session;
137
+ let terminalAssistant: { role?: string; stopReason?: string; content?: Array<{ type?: string; text?: string }> } | undefined;
138
+ let terminalOutput = "";
328
139
  const unsubscribe = session.subscribe((event) => {
329
- if (event.type === "tool_execution_start") {
330
- progress.currentTool = event.toolName;
331
- progress.currentToolArgs = typeof event.args === "object" && event.args !== null
332
- ? JSON.stringify(event.args).slice(0, 120)
333
- : String(event.args ?? "").slice(0, 120);
334
- progress.currentToolStartedAt = Date.now();
335
- progress.phase = "tool_executing";
336
- emitProgress();
337
- return;
338
- }
339
- if (event.type === "tool_execution_end") {
340
- progress.currentTool = undefined;
341
- progress.currentToolArgs = undefined;
342
- progress.currentToolStartedAt = undefined;
343
- progress.phase = "running";
344
- emitProgress();
345
- return;
346
- }
347
- if (event.type === "message_update") {
348
- // Check for thinking events from the assistant stream
349
- const streamEvent = (event as any).assistantMessageEvent;
350
- if (streamEvent?.type === "thinking_start") {
351
- progress.phase = "thinking";
352
- if (!progress.label) progress.label = "Analyzing goal...";
353
- emitProgress();
354
- return;
355
- }
356
- if (streamEvent?.type === "thinking_end") {
357
- progress.phase = "running";
358
- emitProgress();
359
- return;
360
- }
361
- // For text content, show producing_report phase
362
- progress.phase = "producing_report";
363
- const message = event.message as any;
364
- if (message?.role === "assistant") {
365
- for (const part of message.content ?? []) {
366
- if (part.type === "text" && typeof part.text === "string" && part.text.trim()) {
367
- // Keep the last 5 non-empty text lines for live display
368
- const lines = part.text.split("\n").filter((l: string) => l.trim());
369
- progress.recentOutput = [...lines.slice(-5)];
370
- }
371
- }
372
- }
373
- emitProgress();
374
- return;
375
- }
376
140
  if (event.type !== "message_end") return;
377
- const message = event.message as any;
141
+ const message = event.message as { role?: string; stopReason?: string; content?: Array<{ type?: string; text?: string }> };
378
142
  if (message.role !== "assistant") return;
143
+ terminalAssistant = message;
144
+ const messageOutput: string[] = [];
379
145
  for (const part of message.content ?? []) {
380
- if (part.type === "text" && typeof part.text === "string") outputParts.push(part.text);
146
+ if (part.type === "text" && typeof part.text === "string") {
147
+ output.push(part.text);
148
+ messageOutput.push(part.text);
149
+ }
381
150
  }
382
- // Show the accumulated output in progress
383
- const fullText = outputParts.join("\n\n");
384
- const lines = fullText.split("\n").filter((l: string) => l.trim());
385
- progress.recentOutput = lines.slice(-8);
386
- emitProgress();
151
+ terminalOutput = messageOutput.join("\n\n").trim();
387
152
  });
388
- // Wire the external AbortSignal to abort the running session when fired
389
- // This is the mechanism that makes Esc-to-skip actually stop the auditor.
390
- const abortSession = () => { session.abort(); };
391
- args.signal?.addEventListener("abort", abortSession, { once: true });
392
-
393
- // Emit initial progress
394
- progress.label = "Starting audit...";
395
- progress.percentage = 0;
396
- emitProgress();
153
+ const abort = () => session.abort();
154
+ args.signal?.addEventListener("abort", abort, { once: true });
397
155
  try {
398
- if (args.signal?.aborted) return { approved: false, disapproved: true, output: "", model: modelLabel(model), thinkingLevel, error: "Auditor aborted." };
399
- await session.prompt(buildGoalAuditorPrompt(args));
156
+ if (args.signal?.aborted) throw new DOMException("Auditor aborted", "AbortError");
157
+ await session.prompt(buildGoalAuditorPrompt(args.goal, args.completionSummary, auditorBash !== null));
400
158
  } finally {
401
- args.signal?.removeEventListener("abort", abortSession);
402
- progress.phase = "done";
403
- progress.label = "Audit complete.";
404
- progress.percentage = 100;
405
- emitProgress();
159
+ args.signal?.removeEventListener("abort", abort);
406
160
  unsubscribe();
407
161
  }
408
- // session.abort() does NOT throw — the agent loop returns normally with
409
- // whatever output was captured before the abort. Check the signal after
410
- // prompt completes and treat any abort as auditor-aborted regardless of
411
- // whether an exception propagated.
412
- if (args.signal?.aborted) {
413
- return {
414
- approved: false,
415
- disapproved: true,
416
- output: outputParts.join("\n\n").trim(),
417
- model: modelLabel(model),
418
- thinkingLevel,
419
- error: "Auditor aborted.",
420
- };
421
- }
422
- const output = outputParts.join("\n\n").trim();
423
- const decision = parseAuditorDecision(output);
424
- return { ...decision, output, model: modelLabel(model), thinkingLevel };
162
+ if (args.signal?.aborted) throw new DOMException("Auditor aborted", "AbortError");
163
+ const report = output.join("\n\n").trim();
164
+ const completedNormally = terminalAssistant?.stopReason === "stop";
165
+ return {
166
+ approved: completedNormally && parseAuditorDecision(terminalOutput),
167
+ output: report,
168
+ model: modelLabel(model),
169
+ ...(completedNormally ? {} : { error: `Auditor did not complete normally (stopReason: ${terminalAssistant?.stopReason ?? "missing"}).` }),
170
+ };
425
171
  } catch (error) {
426
- const isAborted = args.signal?.aborted || (error instanceof Error && error.name === "AbortError");
427
172
  return {
428
173
  approved: false,
429
- disapproved: true,
430
- output: outputParts.join("\n\n").trim(),
174
+ output: output.join("\n\n").trim(),
431
175
  model: modelLabel(model),
432
- thinkingLevel,
433
- error: isAborted ? "Auditor aborted." : (error instanceof Error ? error.message : String(error)),
176
+ error: error instanceof Error ? error.message : String(error),
434
177
  };
178
+ } finally {
179
+ nestedSession?.dispose();
435
180
  }
436
181
  }