repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(devil): comprehensive SDK integration improvements

7b175ca7d903351c4bc3769a1ffae78e5f9b7cdb

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

- Add promptSnippet + promptGuidelines for LLM awareness
- Use StringEnum for Google API compatibility
- Add Ctrl+Alt+D shortcut (grabs editor text, guards debate output)
- Add --devil-max-rounds and --devil-stop CLI flags
- Replace setEditorText + widget with ctx.ui.editor() modal (proper Esc/Enter)
- Add before_agent_start + session_shutdown cleanup for stale UI
- gatherContext scans assistant toolCall blocks for file paths
  (not toolResult — those lack input), reads current disk contents
  (3KB/file, max 10 files) so devil can verify claims against code
- Split blockingIssues into blockingRaised + blockingRemaining
  (synthesis evaluation vs raw devil pattern count)
- Richer renderCall (stakeholder, stopping condition)
- Richer renderResult expanded (synthesis sections, not raw truncation)
- Richer onUpdate streaming (challenge/rebuttal snippets)
- Trim types.ts (removed 60+ lines of vestigial types)

Changed files (2)

pi/agent/extensions/devil/index.ts

Modified
5 5 * Designed to reduce sycophancy by forcing critical examination.
7 7 * Usage:
8 - * /devil <your idea> - Start a debate
9 - * Call devils_advocate tool
8 + * /devil <your idea> - Start an interactive debate with live progress
9 + * Call devils_advocate tool - Non-interactive debate (for LLM use)
11 11 * Configuration (settings.json):
13 13 * "devilsAdvocate": {
14 14 * "model": "anthropic/claude-haiku-4-5", // Optional: specific model
15 15 * "maxRounds": 5, // Maximum debate rounds
16 - * "stoppingConditions": "consensus", // consensus | blocking_found | max_rounds
17 - * "concessionThreshold": 0.7, // 0.0-1.0 for concession detection
18 - * "onBlocking": "surface" // surface | escalate | ignore
16 + * "stoppingConditions": "consensus", // consensus | blocking_found | max_rounds
17 + * "concessionThreshold": 0.7, // 0.0-1.0 for concession detection
18 + * "onBlocking": "surface" // surface | escalate | ignore
21 - *
22 - * Pi Session Implications:
23 - * - Debates are stateless (no persistence between calls)
24 - * - Uses current session's model and API credentials
25 - * - Results are returned as tool output, not added to conversation
26 - * - Context from current conversation is NOT automatically included
29 23 import { Type } from "@sinclair/typebox";
30 -import { complete, type Message } from "@mariozechner/pi-ai";
31 -import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
24 +import * as fs from "node:fs";
25 +import * as path from "node:path";
26 +import { complete, StringEnum, type Message } from "@mariozechner/pi-ai";
27 +import {
28 + BorderedLoader,
29 + convertToLlm,
30 + serializeConversation,
31 +} from "@mariozechner/pi-coding-agent";
32 +import type {
33 + ExtensionAPI,
34 + ExtensionContext,
35 + Model,
36 + SessionEntry,
37 +} from "@mariozechner/pi-coding-agent";
38 +import { Text } from "@mariozechner/pi-tui";
39 +import { Key } from "@mariozechner/pi-tui";
40 +import type { DebateOutput } from "./types";
33 42 const DEVIL_SYSTEM_PROMPT = `Devil advocate. STRESS TEST ideas. Find blocking issues.
38 47 - **assumed**: Premise may not hold.
39 48 - **minor**: Cosmetic. No functional impact.
41 -EXAMPLES:
42 -- "Req auth missing" → blocking
43 -- "Users confused" → addressable (add onboarding)
44 -- "Assumes fast net" → assumed (premise fragile)
45 -- "Blue vs green button" → minor
47 50 RULES:
48 51 1. Lead with blocking. Skip minor issues.
49 52 2. Name exact failure modes. No vague risks.
50 53 3. Acknowledge successful defenses.
51 54 4. Focus on what BREAKS the idea.
55 +5. Do NOT repeat concerns already raised in previous rounds.
56 +6. ESCALATE: if a previous concern was partially addressed, find the deeper issue.
57 +7. If all previous concerns were resolved, concede and say "No new blocking concerns."
53 59 FORMAT:
54 60 ### [CLASSIFICATION] Title
63 69 2. Distinguish: "I don't know" vs "doesn't apply" vs "addressed that".
64 70 3. If concern valid → acknowledge + explain mitigation.
65 71 4. Never concede blocking issues without explaining fix.
72 +5. Maintain consistency with your previous defenses.
73 +6. If you previously conceded something, don't walk it back without new evidence.
67 75 FOR EACH CHALLENGE:
68 76 1. **Defense**: What defeats/challenges it
72 80 Be specific. "Addressed that" requires HOW.`;
74 -const SYNTHESIS_SYSTEM_PROMPT = `Analyze debate. Produce honest summary. Help user decide.
82 +const SYNTHESIS_SYSTEM_PROMPT = `Analyze the full debate and produce a structured verdict.
76 -CLASSIFICATION:
77 -- **blocking**: Must fix
78 -- **addressable**: Has mitigations
79 -- **assumed**: Premise unverified
80 -- **minor**: Not worth fixing
84 +You MUST respond with ONLY a valid JSON object. No markdown, no explanation, no code fences.
85 +Just the raw JSON object, nothing else.
82 -OUTPUT:
83 -## Summary
87 +JSON schema:
89 + "recommendation": "proceed" | "proceed_with_caution" | "revise" | "abandon",
90 + "summary": "2-3 sentence overview of the debate outcome",
91 + "blocking": ["concern description → proposed fix"],
92 + "addressable": ["concern description → mitigation"],
93 + "survived": ["claims that withstood challenges"],
94 + "conceded": ["acknowledged weaknesses"],
95 + "next_steps": ["action item"]
85 -**Recommendation:** proceed / proceed_with_caution / revise / abandon
87 -### Blocking Issues
88 -- [concern] → [fix]
90 -### Addressable Concerns
91 -- [concern] → [mitigation]
93 -### Survived
94 -- [claims that held]
96 -### Conceded
97 -- [acknowledged weaknesses]
99 -### Next Steps
100 -1. [action]
101 -2. [action]`;
98 +Classification rules:
99 +- blocking: Must fix before proceeding
100 +- addressable: Has known mitigations
101 +- If no blocking issues remain and concerns were resolved: "proceed"
102 +- If blocking issues remain but have clear fixes: "revise"
103 +- If fundamental premise is destroyed: "abandon"
104 +- Otherwise: "proceed_with_caution"`;
103 106 // Default settings
104 107 const DEFAULT_MAX_ROUNDS = 5;
114 117 onBlocking?: "surface" | "escalate" | "ignore";
120 +interface SynthesisResult {
121 + recommendation: "proceed" | "proceed_with_caution" | "revise" | "abandon";
122 + summary: string;
123 + blocking: string[];
124 + addressable: string[];
125 + survived: string[];
126 + conceded: string[];
127 + next_steps: string[];
130 +interface RunDebateOptions {
131 + idea: string;
132 + context?: Record<string, string>;
133 + model: Model;
134 + apiKey: string;
135 + headers?: Record<string, string>;
136 + maxRounds: number;
137 + stoppingConditions: "consensus" | "blocking_found" | "max_rounds";
138 + concessionThreshold: number;
139 + signal?: AbortSignal;
140 + /** Fires after devil responds, before proposer responds (mid-round update) */
141 + onChallenge?: (
142 + round: number,
143 + maxRounds: number,
144 + blocking: number,
145 + devilOutput: string,
146 + ) => void;
147 + /** Fires after both devil and proposer respond (round complete) */
148 + onRound?: (
149 + round: number,
150 + maxRounds: number,
151 + blocking: number,
152 + status: string,
153 + devilOutput: string,
154 + proposerOutput: string,
155 + ) => void;
158 +// ── Helpers ───────────────────────────────────────────────────────────
161 +const MAX_FILE_CHARS = 3000; // per-file cap for context injection
162 +const MAX_FILES = 10; // max files to include
163 +const FILE_TOOL_NAMES = new Set(["read", "edit", "write"]);
165 +interface ToolCallBlock {
166 + type: "toolCall";
167 + name: string;
168 + arguments: Record<string, unknown>;
171 +function gatherContext(ctx: ExtensionContext): Record<string, string> {
172 + const context: Record<string, string> = {};
173 + context["working_directory"] = ctx.cwd;
175 + const branch = ctx.sessionManager.getBranch();
177 + // ── Part 1: Extract file paths from assistant toolCall blocks, read current contents ──
178 + // Tool arguments (file paths) live in assistant messages' content[type=toolCall].arguments
179 + // NOT in toolResult messages (those only have output text, no input).
180 + // Pattern from summarize.ts: scan assistant content for toolCall blocks.
181 + const filePaths = new Set<string>();
183 + for (const entry of branch) {
184 + if (entry.type !== "message") continue;
185 + const msg = entry.message;
186 + if (!msg || msg.role !== "assistant" || !Array.isArray(msg.content)) continue;
188 + for (const block of msg.content) {
189 + const tc = block as ToolCallBlock;
190 + if (tc.type !== "toolCall" || !FILE_TOOL_NAMES.has(tc.name)) continue;
192 + // read/edit/write all use "path" (edit may also have "file_path" in older sessions)
193 + const filePath = (tc.arguments.path ?? tc.arguments.file_path) as string | undefined;
194 + if (typeof filePath !== "string" || !filePath.trim()) continue;
196 + const resolved = path.resolve(ctx.cwd, filePath.trim());
197 + try {
198 + if (fs.existsSync(resolved) && fs.statSync(resolved).isFile()) {
199 + filePaths.add(resolved);
201 + } catch {
202 + // skip inaccessible files
207 + if (filePaths.size > 0) {
208 + const files = [...filePaths].slice(0, MAX_FILES);
209 + const fileContents = files
210 + .map((fp) => {
211 + try {
212 + let content = fs.readFileSync(fp, "utf-8");
213 + if (content.length > MAX_FILE_CHARS) {
214 + content = content.slice(0, MAX_FILE_CHARS) + "\n... (truncated)";
216 + const rel = path.relative(ctx.cwd, fp);
217 + return `### ${rel}\n\`${content}\``;
218 + } catch {
219 + return null;
221 + })
222 + .filter((c): c is string => c !== null);
223 + if (fileContents.length > 0) {
224 + context["relevant_files"] = fileContents.join("\n\n");
228 + // ── Part 2: Recent conversation (last 20 messages) ──
229 + const messages = branch
230 + .filter(
231 + (entry): entry is SessionEntry & { type: "message" } =>
232 + entry.type === "message",
234 + .map((entry) => entry.message);
236 + if (messages.length > 0) {
237 + const recent = messages.slice(-20);
238 + const llmMessages = convertToLlm(recent);
239 + context["conversation"] = serializeConversation(llmMessages);
242 + return context;
245 +/** Extract first meaningful line from devil's challenge for widget display */
246 +function extractSnippet(text: string, maxLen = 60): string {
247 + const lines = text.split("\n").filter((l) => l.trim().length > 0);
248 + for (const line of lines) {
249 + const clean = line
250 + .replace(/^#{1,6}\s*/, "")
251 + .replace(/\*\*/g, "")
252 + .trim();
253 + if (
254 + clean.length > 5 &&
255 + !clean.startsWith("Concern:") &&
256 + !clean.startsWith("Fix:") &&
257 + !clean.startsWith("Status:")
258 + ) {
259 + return clean.length > maxLen ? clean.slice(0, maxLen) + "…" : clean;
262 + return lines[0]?.slice(0, maxLen) ?? "";
265 +function formatSynthesis(s: SynthesisResult): string {
266 + const recEmoji: Record<string, string> = {
267 + proceed: "✅",
268 + proceed_with_caution: "⚡",
269 + revise: "⚠️",
270 + abandon: "🚫",
271 + };
272 + const emoji = recEmoji[s.recommendation] ?? "⚡";
274 + let text = `## Verdict: ${s.recommendation.replace(/_/g, " ")} ${emoji}\n\n`;
275 + text += `${s.summary}\n`;
277 + if (s.blocking.length > 0) {
278 + text += `\n### Blocking Issues\n`;
279 + for (const b of s.blocking) text += `- ${b}\n`;
281 + if (s.addressable.length > 0) {
282 + text += `\n### Addressable Concerns\n`;
283 + for (const a of s.addressable) text += `- ${a}\n`;
285 + if (s.survived.length > 0) {
286 + text += `\n### Survived\n`;
287 + for (const c of s.survived) text += `- ${c}\n`;
289 + if (s.conceded.length > 0) {
290 + text += `\n### Conceded\n`;
291 + for (const c of s.conceded) text += `- ${c}\n`;
293 + if (s.next_steps.length > 0) {
294 + text += `\n### Next Steps\n`;
295 + for (const n of s.next_steps) text += `${n}\n`;
298 + return text;
301 +function parseSynthesis(raw: string): SynthesisResult {
302 + // Try JSON parse first
303 + try {
304 + const jsonMatch = raw.match(/\{[\s\S]*\}/);
305 + if (jsonMatch) {
306 + const parsed = JSON.parse(jsonMatch[0]);
307 + return {
308 + recommendation:
309 + ["proceed", "proceed_with_caution", "revise", "abandon"].includes(
310 + parsed.recommendation,
312 + ? parsed.recommendation
313 + : "proceed_with_caution",
314 + summary: String(parsed.summary ?? ""),
315 + blocking: Array.isArray(parsed.blocking) ? parsed.blocking.map(String) : [],
316 + addressable: Array.isArray(parsed.addressable)
317 + ? parsed.addressable.map(String)
318 + : [],
319 + survived: Array.isArray(parsed.survived) ? parsed.survived.map(String) : [],
320 + conceded: Array.isArray(parsed.conceded) ? parsed.conceded.map(String) : [],
321 + next_steps: Array.isArray(parsed.next_steps)
322 + ? parsed.next_steps.map(String)
323 + : [],
324 + };
326 + } catch {}
328 + // Fallback: regex parse from markdown
329 + const recMatch = raw.match(/\*\*recommendation\*\*:\s*(\w+)/i);
330 + const rec = recMatch?.[1]?.toLowerCase();
331 + return {
332 + recommendation:
333 + rec && ["proceed", "proceed_with_caution", "revise", "abandon"].includes(rec)
334 + ? (rec as SynthesisResult["recommendation"])
335 + : "proceed_with_caution",
336 + summary: raw.slice(0, 200),
337 + blocking: [],
338 + addressable: [],
339 + survived: [],
340 + conceded: [],
341 + next_steps: [],
342 + };
345 +// ── Core debate logic ─────────────────────────────────────────────────
347 +async function runDebate(opts: RunDebateOptions): Promise<DebateOutput> {
348 + const {
349 + idea,
350 + context,
351 + model,
352 + apiKey,
353 + headers,
354 + maxRounds,
355 + stoppingConditions,
356 + concessionThreshold,
357 + signal,
358 + onChallenge,
359 + onRound,
360 + } = opts;
362 + const contextText = context
363 + ? Object.entries(context)
364 + .map(([k, v]) => `- **${k}**: ${v}`)
365 + .join("\n")
366 + : "No additional context provided.";
368 + const roundsData: string[] = [];
369 + let blockingIssuesFound = 0;
370 + let consecutiveResolved = 0;
371 + let shouldContinue = true;
372 + let roundHistory = ""; // Accumulates for cross-round memory
374 + for (let i = 1; i <= maxRounds && shouldContinue; i++) {
375 + if (signal?.aborted) {
376 + return {
377 + synthesis: "Debate cancelled by user.",
378 + roundsData,
379 + recommendation: "proceed_with_caution",
380 + blockingRaised: blockingIssuesFound,
381 + blockingRemaining: blockingIssuesFound,
382 + aborted: true,
383 + };
386 + // ── Devil's challenge (sees full round history) ──
387 + const devilUserText = [
388 + `Context:\n${contextText}`,
389 + `Idea: ${idea}`,
390 + roundHistory
391 + ? `Previous rounds:\n${roundHistory}`
392 + : "This is the first round.",
393 + `Round ${i}/${maxRounds}. Identify the most blocking concern not yet resolved.`,
394 + ].join("\n\n");
396 + const devilResponse = await complete(
397 + model,
399 + systemPrompt: DEVIL_SYSTEM_PROMPT,
400 + messages: [
402 + role: "user",
403 + content: [{ type: "text", text: devilUserText }],
404 + timestamp: Date.now(),
405 + },
406 + ],
407 + },
408 + { apiKey, headers, signal },
409 + );
411 + if (signal?.aborted) {
412 + return {
413 + synthesis: "Debate cancelled by user.",
414 + roundsData,
415 + recommendation: "proceed_with_caution",
416 + blockingRaised: blockingIssuesFound,
417 + blockingRemaining: blockingIssuesFound,
418 + aborted: true,
419 + };
422 + const devilOutput = devilResponse.content
423 + .filter((c): c is { type: "text"; text: string } => c.type === "text")
424 + .map((c) => c.text)
425 + .join("\n");
427 + const roundBlockingCount =
428 + (devilOutput.match(/\[blocking\]/gi) || []).length +
429 + (devilOutput.match(/\*\*blocking\*\*/gi) || []).length;
430 + blockingIssuesFound = Math.max(blockingIssuesFound, roundBlockingCount);
432 + // Mid-round update: devil has spoken, widget shows challenge
433 + onChallenge?.(i, maxRounds, blockingIssuesFound, devilOutput);
435 + // ── Proposer's rebuttal (sees full round history + current challenge) ──
436 + const proposerUserText = [
437 + `Your idea: ${idea}`,
438 + roundHistory
439 + ? `Previous rounds:\n${roundHistory}`
440 + : "This is the first round.",
441 + `Current challenge (Round ${i}):`,
442 + devilOutput,
443 + "Respond to this challenge.",
444 + ].join("\n\n");
446 + const proposerResponse = await complete(
447 + model,
449 + systemPrompt: PROPOSER_SYSTEM_PROMPT,
450 + messages: [
452 + role: "user",
453 + content: [{ type: "text", text: proposerUserText }],
454 + timestamp: Date.now(),
455 + },
456 + ],
457 + },
458 + { apiKey, headers, signal },
459 + );
461 + if (signal?.aborted) {
462 + return {
463 + synthesis: "Debate cancelled by user.",
464 + roundsData,
465 + recommendation: "proceed_with_caution",
466 + blockingRaised: blockingIssuesFound,
467 + blockingRemaining: blockingIssuesFound,
468 + aborted: true,
469 + };
472 + const proposerOutput = proposerResponse.content
473 + .filter((c): c is { type: "text"; text: string } => c.type === "text")
474 + .map((c) => c.text)
475 + .join("\n");
477 + roundsData.push(
478 + `### Round ${i}\n\n**Challenge:**\n${devilOutput}\n\n**Response:**\n${proposerOutput}`,
479 + );
481 + // Append to round history for next iteration
482 + roundHistory += `### Round ${i}\n**Challenge:** ${devilOutput.slice(0, 500)}\n**Response:** ${proposerOutput.slice(0, 500)}\n\n`;
484 + // Round complete update
485 + const status = `Round ${i}/${maxRounds} · ${blockingIssuesFound} blocking`;
486 + onRound?.(i, maxRounds, blockingIssuesFound, status, devilOutput, proposerOutput);
488 + // Check stopping conditions
489 + const resolvedCount =
491 + proposerOutput.match(
492 + /\*\*status\*\*:\s*resolved|status:\s*resolved/gi,
493 + ) || []
494 + ).length;
495 + const concessionIndicators = (
496 + proposerOutput.match(
497 + /fair point|good point|you're right|concede|acknowledge|valid point|no new blocking/i,
498 + ) || []
499 + ).length;
500 + const concessionScore = resolvedCount + concessionIndicators * 0.5;
502 + if (stoppingConditions === "consensus") {
503 + if (concessionScore >= concessionThreshold) {
504 + consecutiveResolved++;
505 + if (consecutiveResolved >= 2) shouldContinue = false;
506 + } else {
507 + consecutiveResolved = 0;
509 + } else if (
510 + stoppingConditions === "blocking_found" &&
511 + roundBlockingCount === 0
512 + ) {
513 + shouldContinue = false;
514 + } else if (stoppingConditions === "max_rounds") {
515 + shouldContinue = i < maxRounds;
519 + if (signal?.aborted) {
520 + return {
521 + synthesis: "Debate cancelled by user.",
522 + roundsData,
523 + recommendation: "proceed_with_caution",
524 + blockingRaised: blockingIssuesFound,
525 + blockingRemaining: blockingIssuesFound,
526 + aborted: true,
527 + };
530 + // ── Synthesis (JSON structured output) ──
531 + const synthesisUserText = [
532 + `Idea: ${idea}`,
533 + `Debate (${roundsData.length} rounds):`,
534 + roundsData.join("\n\n---\n\n"),
535 + ].join("\n\n");
537 + const synthesisResponse = await complete(
538 + model,
540 + systemPrompt: SYNTHESIS_SYSTEM_PROMPT,
541 + messages: [
543 + role: "user",
544 + content: [{ type: "text", text: synthesisUserText }],
545 + timestamp: Date.now(),
546 + },
547 + ],
548 + },
549 + { apiKey, headers, signal },
550 + );
552 + const synthesisRaw = synthesisResponse.content
553 + .filter((c): c is { type: "text"; text: string } => c.type === "text")
554 + .map((c) => c.text)
555 + .join("\n");
557 + const synthesis = parseSynthesis(synthesisRaw);
558 + const synthesisFormatted = formatSynthesis(synthesis);
560 + return {
561 + synthesis: synthesisFormatted,
562 + roundsData,
563 + recommendation: synthesis.recommendation,
564 + blockingRaised: blockingIssuesFound,
565 + blockingRemaining: synthesis.blocking.length,
566 + };
569 +function formatDebateOutput(
570 + output: DebateOutput,
571 + onBlocking: "surface" | "escalate" | "ignore",
572 +): string {
573 + let blockingNote = "";
574 + if (output.blockingRemaining > 0 && onBlocking === "escalate") {
575 + blockingNote = `\n\n⚠️ **${output.blockingRemaining} unresolved blocking issue(s).** Review before proceeding.`;
578 + const meta = output.aborted
579 + ? `*Debate cancelled | ${output.roundsData.length} rounds completed*`
580 + : `*Debate: ${output.roundsData.length}r | ${output.blockingRaised} raised, ${output.blockingRemaining} remaining*`;
582 + return `${output.synthesis}${blockingNote}\n\n${meta}`;
585 +// ── Extension entry ───────────────────────────────────────────────────
117 587 export default async function (pi: ExtensionAPI) {
118 - // Read settings
119 588 const settings = pi.settingsManager?.getSettings();
120 589 const devilSettings: DevilSettings = settings?.devilsAdvocate ?? {};
122 - // Register the devil's advocate tool
591 + // ── Flags ─────────────────────────────────────────────────────────
593 + pi.registerFlag("devil-max-rounds", {
594 + description: "Override max debate rounds (1-10)",
595 + type: "number",
596 + default: devilSettings.maxRounds ?? DEFAULT_MAX_ROUNDS,
597 + });
599 + pi.registerFlag("devil-stop", {
600 + description: "Override stopping condition (consensus | blocking_found | max_rounds)",
601 + type: "string",
602 + default: devilSettings.stoppingConditions ?? DEFAULT_STOPPING_CONDITIONS,
603 + });
605 + // ── Cleanup: clear devil status/widget on next prompt or abort ──
606 + pi.on("before_agent_start", async (_event, ctx) => {
607 + ctx.ui.setStatus("devil", undefined);
608 + ctx.ui.setWidget("devil", undefined);
609 + });
610 + pi.on("session_shutdown", async (_event, ctx) => {
611 + ctx.ui.setStatus("devil", undefined);
612 + ctx.ui.setWidget("devil", undefined);
613 + });
615 + // ── Tool: devils_advocate ──────────────────────────────────────────
123 617 pi.registerTool({
124 618 name: "devils_advocate",
125 619 label: "Devil's Advocate",
126 - description: `Debate tool. STRESS TEST ideas. Surface blocking issues before committing.
128 -Parameters:
129 -- idea: Idea to debate (req)
130 -- context: Background kvpairs (opt)
131 -- model: Model override (opt)
132 -- maxRounds: 1-10 rounds (default: ${devilSettings.maxRounds ?? DEFAULT_MAX_ROUNDS})
133 -- stakeholder: security | cost | ux | impl | ethics | skeptical
134 -- stoppingConditions: When stop (default: ${devilSettings.stoppingConditions ?? DEFAULT_STOPPING_CONDITIONS})
135 -- output: full | summary_only`,
620 + description:
621 + "Debate tool. STRESS TEST ideas. Surface blocking issues before committing. Use when: the user asks to stress-test, validate, critique, or play devil's advocate with an idea, design decision, architecture choice, or plan. Good for reducing sycophancy by forcing critical examination.",
622 + promptSnippet: "Stress-test ideas via structured devil's advocate debate",
623 + promptGuidelines: [
624 + "Use devils_advocate when the user wants to validate an idea, check for flaws, or get a critical perspective before committing to a decision.",
625 + "Include relevant context (problem, constraints, scale) as kvpairs for better-targeted challenges.",
626 + "Set stakeholder to match the perspective most relevant to the idea (security, cost, ux, impl, ethics, skeptical).",
627 + "After devils_advocate returns, summarize the verdict and actionable next steps for the user.",
628 + ],
136 629 parameters: Type.Object({
137 - idea: Type.String({
138 - description: "Idea/claim/decision. Be specific.",
139 - }),
630 + idea: Type.String({ description: "Idea/claim/decision. Be specific." }),
140 631 context: Type.Optional(
141 632 Type.Record(Type.String(), Type.String(), {
142 - description: "Background: { problem: \"...\", constraint: \"...\", scale: \"...\" }",
143 - })
633 + description: 'Background: { problem: "...", constraint: "...", scale: "..." }',
634 + }),
145 - model: Type.Optional(Type.String({
146 - description: "Model override (e.g., 'anthropic/claude-haiku-4-5')",
147 - })),
148 - maxRounds: Type.Optional(
149 - Type.Number({ minimum: 1, maximum: 10 })
636 + model: Type.Optional(
637 + Type.String({
638 + description: "Model override (e.g., 'anthropic/claude-haiku-4-5')",
639 + }),
641 + maxRounds: Type.Optional(Type.Number({ minimum: 1, maximum: 10 })),
151 642 stakeholder: Type.Optional(
152 - Type.Union([
153 - Type.Literal("security"),
154 - Type.Literal("cost"),
155 - Type.Literal("ux"),
156 - Type.Literal("implementation"),
157 - Type.Literal("ethics"),
158 - Type.Literal("skeptical"),
159 - ])
643 + StringEnum(["security", "cost", "ux", "implementation", "ethics", "skeptical"] as const),
161 645 stoppingConditions: Type.Optional(
162 - Type.Union([
163 - Type.Literal("consensus"),
164 - Type.Literal("blocking_found"),
165 - Type.Literal("max_rounds"),
166 - ])
646 + StringEnum(["consensus", "blocking_found", "max_rounds"] as const),
168 648 concessionThreshold: Type.Optional(
169 - Type.Number({ minimum: 0, maximum: 1 })
649 + Type.Number({ minimum: 0, maximum: 1 }),
171 651 onBlocking: Type.Optional(
172 - Type.Union([
173 - Type.Literal("surface"),
174 - Type.Literal("escalate"),
175 - Type.Literal("ignore"),
176 - ])
652 + StringEnum(["surface", "escalate", "ignore"] as const),
178 654 output: Type.Optional(
179 - Type.Union([Type.Literal("full"), Type.Literal("summary_only")])
655 + StringEnum(["full", "summary_only"] as const),
659 + renderCall(args, theme) {
660 + const idea = args.idea as string;
661 + const maxRounds =
662 + (args.maxRounds as number | undefined) ??
663 + devilSettings.maxRounds ??
664 + DEFAULT_MAX_ROUNDS;
665 + const stakeholder = args.stakeholder as string | undefined;
666 + const stopping = args.stoppingConditions as string | undefined;
667 + const snippet = idea.length > 30 ? idea.slice(0, 30) + "..." : idea;
668 + const focus = stakeholder ? ` · ${stakeholder}` : "";
669 + const stop = stopping && stopping !== DEFAULT_STOPPING_CONDITIONS ? ` · ${stopping}` : "";
671 + return new Text(
672 + theme.fg("toolTitle", theme.bold("devils_advocate ")) +
673 + theme.fg("dim", `"${snippet}"`) +
674 + theme.fg("muted", ` ${maxRounds}r${focus}${stop}`),
675 + 0,
676 + 0,
677 + );
678 + },
680 + renderResult(result, { expanded }, theme) {
681 + const details = result.details as
682 + | {
683 + recommendation?: string;
684 + rounds?: number;
685 + blockingRaised?: number;
686 + blockingRemaining?: number;
687 + output?: string;
688 + aborted?: boolean;
690 + | undefined;
692 + if (!details) {
693 + const text = result.content[0];
694 + return new Text(text?.type === "text" ? text.text : "", 0, 0);
697 + if (details.aborted) {
698 + return new Text(
699 + theme.fg("warning", "⚠ ") + theme.fg("muted", "Debate cancelled"),
700 + 0,
701 + 0,
702 + );
705 + const rec = details.recommendation ?? "proceed_with_caution";
706 + const rounds = details.rounds ?? 0;
707 + const raised = details.blockingRaised ?? 0;
708 + const remaining = details.blockingRemaining ?? raised;
710 + let recColor: string;
711 + let recLabel: string;
712 + switch (rec) {
713 + case "proceed":
714 + recColor = "success";
715 + recLabel = "✓ proceed";
716 + break;
717 + case "revise":
718 + recColor = "warning";
719 + recLabel = "⚠ revise";
720 + break;
721 + case "abandon":
722 + recColor = "error";
723 + recLabel = "✗ abandon";
724 + break;
725 + default:
726 + recColor = "muted";
727 + recLabel = "~ caution";
730 + let text = theme.fg(recColor, recLabel);
731 + text += theme.fg("muted", ` · ${rounds}r · ${raised}→${remaining} blocking`);
733 + if (expanded && details.output) {
734 + const lines = details.output.split("\n").filter((l: string) => l.trim());
735 + let inSection = false;
736 + for (const line of lines.slice(0, 30)) {
737 + if (line.startsWith("### ") || line.startsWith("## ")) {
738 + inSection = true;
739 + text += "\n" + theme.fg("toolTitle", line);
740 + } else if (inSection) {
741 + text += "\n" + theme.fg("dim", line);
744 + if (lines.length > 30) text += "\n" + theme.fg("dim", "...");
747 + return new Text(text, 0, 0);
748 + },
182 750 execute: async (_toolCallId, params, signal, onUpdate, ctx) => {
183 751 const {
185 - context,
753 + context: explicitContext,
186 754 model: modelOverride,
187 755 maxRounds: maxRoundsParam,
188 756 stakeholder,
189 757 stoppingConditions: stoppingConditionsParam,
190 758 concessionThreshold: concessionThresholdParam,
191 759 onBlocking: onBlockingParam,
192 - output = "summary_only"
760 + output = "summary_only",
193 761 } = params;
195 - const maxRounds = maxRoundsParam ?? devilSettings.maxRounds ?? DEFAULT_MAX_ROUNDS;
196 - const stoppingConditions = stoppingConditionsParam ?? devilSettings.stoppingConditions ?? DEFAULT_STOPPING_CONDITIONS;
197 - const concessionThreshold = concessionThresholdParam ?? devilSettings.concessionThreshold ?? DEFAULT_CONCESSION_THRESHOLD;
198 - const onBlocking = onBlockingParam ?? devilSettings.onBlocking ?? DEFAULT_ON_BLOCKING;
763 + const maxRounds =
764 + maxRoundsParam ?? devilSettings.maxRounds ?? DEFAULT_MAX_ROUNDS;
765 + const stoppingConditions =
766 + stoppingConditionsParam ??
767 + devilSettings.stoppingConditions ??
768 + DEFAULT_STOPPING_CONDITIONS;
769 + const concessionThreshold =
770 + concessionThresholdParam ??
771 + devilSettings.concessionThreshold ??
772 + DEFAULT_CONCESSION_THRESHOLD;
773 + const onBlocking =
774 + onBlockingParam ?? devilSettings.onBlocking ?? DEFAULT_ON_BLOCKING;
200 - // Determine which model to use
776 + // Resolve model
201 777 let model = ctx.model;
203 778 if (modelOverride) {
204 779 const [provider, modelId] = modelOverride.includes("/")
205 780 ? modelOverride.split("/")
206 781 : [modelOverride, modelOverride];
207 - const foundModel = ctx.modelRegistry.find(provider, modelId);
208 - if (foundModel) model = foundModel;
782 + const found = ctx.modelRegistry.find(provider, modelId);
783 + if (found) model = found;
209 784 } else if (devilSettings.model && ctx.model) {
210 785 const [provider, modelId] = devilSettings.model.includes("/")
211 786 ? devilSettings.model.split("/")
212 787 : [ctx.model.provider, devilSettings.model];
213 - const foundModel = ctx.modelRegistry.find(provider, modelId);
214 - if (foundModel) model = foundModel;
788 + const found = ctx.modelRegistry.find(provider, modelId);
789 + if (found) model = found;
217 792 if (!model) {
218 793 return {
219 - content: [{ type: "text", text: "Error: No model available. Select a model first with /model." }],
794 + content: [
795 + { type: "text", text: "Error: No model available. Use /model." },
796 + ],
220 797 details: { error: "No model" },
224 801 const auth = await ctx.modelRegistry.getApiKeyAndHeaders(model);
225 802 if (!auth.ok || !auth.apiKey) {
226 803 return {
227 - content: [{ type: "text", text: `Error: No API key for ${model.provider}` }],
804 + content: [
805 + { type: "text", text: `Error: No API key for ${model.provider}` },
806 + ],
228 807 details: { error: "No API key" },
232 - const contextText = context
233 - ? Object.entries(context).map(([k, v]) => `- **${k}**: ${v}`).join("\n")
234 - : "No additional context provided.";
811 + // Merge session context with LLM-provided context
812 + const sessionContext = gatherContext(ctx);
813 + const mergedContext = {
814 + ...sessionContext,
815 + ...(explicitContext ?? {}),
816 + ...(stakeholder
817 + ? { _stakeholder: `Challenge from ${stakeholder} perspective.` }
818 + : {}),
819 + };
236 - const stakeholderContext = stakeholder
237 - ? `\n\n**Focus:** Challenge specifically from ${stakeholder} perspective.`
238 - : "";
240 - // Show progress via status (only if UI available)
241 821 if (ctx.hasUI) {
242 - ctx.ui.setStatus("devil", `🗡️ Devil: ${idea.slice(0, 40)}... (${maxRounds}r)`);
822 + ctx.ui.setStatus(
823 + "devil",
824 + `🗡️ Devil: ${idea.slice(0, 40)}... (${maxRounds}r)`,
825 + );
246 - const rounds_data: string[] = [];
247 - let blockingIssuesFound = 0;
248 - let consecutiveResolved = 0;
249 - let shouldContinue = true;
251 - for (let i = 1; i <= maxRounds && shouldContinue; i++) {
252 - // Devil's challenge
253 - const devilMessages: Message[] = [
254 - { role: "system", content: [{ type: "text", text: DEVIL_SYSTEM_PROMPT }], timestamp: Date.now() },
255 - { role: "user", content: [{ type: "text", text: `Context:\n${contextText}${stakeholderContext}\n\nIdea: ${idea}\n\nRound ${i}/${maxRounds}. Identify the most blocking concern.` }], timestamp: Date.now() },
256 - ];
258 - const devilResponse = await complete(model, { systemPrompt: "", messages: devilMessages }, { apiKey: auth.apiKey, headers: auth.headers, signal });
259 - const devilOutput = devilResponse.content.filter((c): c is { type: "text"; text: string } => c.type === "text").map(c => c.text).join("\n");
261 - // Count blocking issues
262 - const roundBlockingCount = (devilOutput.match(/\[blocking\]/gi) || []).length;
263 - blockingIssuesFound = Math.max(blockingIssuesFound, roundBlockingCount);
265 - // Proposer's rebuttal
266 - const proposerMessages: Message[] = [
267 - { role: "system", content: [{ type: "text", text: PROPOSER_SYSTEM_PROMPT }], timestamp: Date.now() },
268 - { role: "user", content: [{ type: "text", text: `Your idea: ${idea}\n\nChallenges:\n${devilOutput}\n\nRespond to these challenges.` }], timestamp: Date.now() },
269 - ];
271 - const proposerResponse = await complete(model, { systemPrompt: "", messages: proposerMessages }, { apiKey: auth.apiKey, headers: auth.headers, signal });
272 - const proposerOutput = proposerResponse.content.filter((c): c is { type: "text"; text: string } => c.type === "text").map(c => c.text).join("\n");
274 - rounds_data.push(`### Round ${i}\n\n**Challenge:**\n${devilOutput}\n\n**Your Response:**\n${proposerOutput}`);
276 - // Update progress
277 - if (ctx.hasUI) {
278 - ctx.ui.setStatus("devil", `🗡️ Devil: Round ${i}/${maxRounds} - ${roundBlockingCount} blocking`);
279 - }
281 - // Check stopping conditions
282 - const resolvedCount = (proposerOutput.match(/\*\*status\*\*:\s*resolved|status:\s*resolved/gi) || []).length;
283 - const concessionIndicators = (proposerOutput.match(/fair point|good point|you're right|concede|acknowledge/i) || []).length;
284 - const concessionScore = resolvedCount + (concessionIndicators * 0.5);
286 - if (stoppingConditions === "consensus" && concessionScore >= concessionThreshold) {
287 - consecutiveResolved++;
288 - if (consecutiveResolved >= 2) {
289 - shouldContinue = false;
829 + const result = await runDebate({
830 + idea,
831 + context: mergedContext,
832 + model,
833 + apiKey: auth.apiKey,
834 + headers: auth.headers,
835 + maxRounds,
836 + stoppingConditions: stoppingConditions as
837 + | "consensus"
838 + | "blocking_found"
839 + | "max_rounds",
840 + concessionThreshold,
841 + signal,
842 + onChallenge: (round, max, blocking, devilOutput) => {
843 + if (ctx.hasUI) {
844 + ctx.ui.setStatus("devil", `🗡️ Devil: R${round}/${max} challenging...`);
845 + const snippet = extractSnippet(devilOutput, 60);
846 + ctx.ui.setWidget("devil", [
847 + `🗡️ Round ${round}/${max} · ${blocking} blocking`,
848 + snippet ? ` ⚑ ${snippet}` : " ⚑ Analyzing...",
849 + ]);
291 - } else if (stoppingConditions === "blocking_found" && roundBlockingCount === 0) {
292 - shouldContinue = false;
293 - } else if (stoppingConditions === "max_rounds") {
294 - // Always continue until max_rounds
295 - shouldContinue = i < maxRounds;
296 - }
851 + },
852 + onRound: (round, max, blocking, status, devilOutput, proposerOutput) => {
853 + if (ctx.hasUI) {
854 + ctx.ui.setStatus("devil", `🗡️ Devil: ${status}`);
855 + const snippet = extractSnippet(proposerOutput, 60);
856 + ctx.ui.setWidget("devil", [
857 + `🗡️ Round ${round}/${max} · ${blocking} blocking`,
858 + snippet ? ` ✓ ${snippet}` : " ✓ Defended",
859 + ]);
861 + onUpdate?.({
862 + content: [
864 + type: "text",
865 + text: `Round ${round}/${max} · ${blocking} blocking\n⚑ ${extractSnippet(devilOutput, 80)}\n✓ ${extractSnippet(proposerOutput, 80)}`,
866 + },
867 + ],
868 + details: { round, maxRounds: max, blockingRaised: blocking },
869 + });
870 + },
871 + });
298 - if (resolvedCount > 0) consecutiveResolved++;
299 - }
873 + const outputText =
874 + output === "full"
875 + ? `## Full Debate\n\n${result.roundsData.join("\n\n---\n\n")}\n\n---\n\n${formatDebateOutput(result, onBlocking)}`
876 + : formatDebateOutput(result, onBlocking);
301 - // Synthesis
302 - const synthesisMessages: Message[] = [
303 - { role: "system", content: [{ type: "text", text: SYNTHESIS_SYSTEM_PROMPT }], timestamp: Date.now() },
304 - { role: "user", content: [{ type: "text", text: `Idea: ${idea}\n\nDebate (${rounds_data.length} rounds):\n${rounds_data.join("\n\n---\n\n")}` }], timestamp: Date.now() },
305 - ];
307 - const synthesisResponse = await complete(model, { systemPrompt: "", messages: synthesisMessages }, { apiKey: auth.apiKey, headers: auth.headers, signal });
308 - const synthesisOutput = synthesisResponse.content.filter((c): c is { type: "text"; text: string } => c.type === "text").map(c => c.text).join("\n");
310 - // Parse recommendation
311 - const recMatch = synthesisOutput.match(/\*\*recommendation\*\*:\s*(\w+)/i);
312 - let recommendation = "proceed_with_caution";
313 - if (recMatch) {
314 - recommendation = recMatch[1].toLowerCase();
315 - if (!["proceed", "proceed_with_caution", "revise", "abandon"].includes(recommendation)) {
316 - recommendation = "proceed_with_caution";
317 - }
318 - }
320 - // Handle blocking issues based on onBlocking setting
321 - let blockingNote = "";
322 - if (blockingIssuesFound > 0 && onBlocking === "escalate") {
323 - blockingNote = `\n\n⚠️ **${blockingIssuesFound} blocking issue(s) found.** Review before proceeding.`;
324 - } else if (blockingIssuesFound > 0 && onBlocking === "ignore") {
325 - blockingNote = "";
326 - }
328 - const outputText = output === "full"
329 - ? `## Full Debate\n\n${rounds_data.join("\n\n---\n\n")}\n\n---\n\n## Summary\n\n${synthesisOutput}${blockingNote}\n\n**Debate:** ${rounds_data.length} rounds | **Blocking issues:** ${blockingIssuesFound}`
330 - : `${synthesisOutput}${blockingNote}\n\n*Debate: ${rounds_data.length} rounds | Blocking: ${blockingIssuesFound}*`;
332 - // Clear status on complete
333 878 if (ctx.hasUI) {
334 879 ctx.ui.setStatus("devil", undefined);
880 + ctx.ui.setWidget("devil", undefined);
337 883 return {
338 884 content: [{ type: "text", text: outputText }],
339 - details: { recommendation, rounds: rounds_data.length, blockingIssues: blockingIssuesFound },
885 + details: {
886 + recommendation: result.recommendation,
887 + rounds: result.roundsData.length,
888 + blockingRaised: result.blockingRaised,
889 + blockingRemaining: result.blockingRemaining,
890 + output: result.synthesis,
891 + aborted: result.aborted,
892 + },
341 894 } catch (error) {
342 895 const message = error instanceof Error ? error.message : String(error);
343 - // Clear status on error
344 896 if (ctx.hasUI) {
345 897 ctx.ui.setStatus("devil", undefined);
898 + ctx.ui.setWidget("devil", undefined);
347 900 return {
348 901 content: [{ type: "text", text: `## Debate Failed\n\nError: ${message}` }],
355 - // Register the /devil command
908 + // ── Command: /devil ────────────────────────────────────────────────
910 + pi.registerShortcut(Key.ctrlAlt("d"), {
911 + description: "Start devil's advocate debate with current editor text",
912 + handler: async (ctx) => {
913 + const text = ctx.ui.getEditorText?.().trim();
914 + if (!text) {
915 + ctx.ui?.notify("Type an idea in the editor first, or use /devil <idea>", "info");
916 + return;
918 + // Don't re-debate existing debate output
919 + if (text.startsWith("## Verdict:") || text.startsWith("*Debate:")) {
920 + ctx.ui?.notify("Editor contains debate output, not an idea. Clear it first.", "info");
921 + return;
923 + pi.sendUserMessage(`/devil ${text}`);
924 + },
925 + });
356 927 pi.registerCommand("devil", {
357 - description: "Start a devil's advocate debate",
928 + description: "Start an interactive devil's advocate debate",
358 929 handler: async (args, ctx) => {
359 930 const idea = args.trim();
360 931 if (!idea) {
361 932 ctx.ui?.notify("Usage: /devil <your idea>", "error");
362 933 return;
364 - await ctx.tools?.invoke("devils_advocate", {
365 - idea,
366 - maxRounds: devilSettings.maxRounds ?? DEFAULT_MAX_ROUNDS,
367 - stoppingConditions: (devilSettings.stoppingConditions as any) ?? DEFAULT_STOPPING_CONDITIONS,
368 - });
935 + if (!ctx.hasUI) {
936 + ctx.ui?.notify("Devil requires interactive mode", "error");
937 + return;
939 + if (!ctx.model) {
940 + ctx.ui?.notify("No model selected. Use /model first.", "error");
941 + return;
944 + const auth = await ctx.modelRegistry.getApiKeyAndHeaders(ctx.model);
945 + if (!auth.ok || !auth.apiKey) {
946 + ctx.ui?.notify(`No API key for ${ctx.model.provider}`, "error");
947 + return;
950 + const maxRounds = pi.getFlag("devil-max-rounds") as number ?? devilSettings.maxRounds ?? DEFAULT_MAX_ROUNDS;
951 + const stoppingConditions = ((pi.getFlag("devil-stop") as string) ??
952 + devilSettings.stoppingConditions ??
953 + DEFAULT_STOPPING_CONDITIONS) as "consensus" | "blocking_found" | "max_rounds";
954 + const concessionThreshold =
955 + devilSettings.concessionThreshold ?? DEFAULT_CONCESSION_THRESHOLD;
956 + const onBlocking = devilSettings.onBlocking ?? DEFAULT_ON_BLOCKING;
957 + const debateContext = gatherContext(ctx);
959 + const result = await ctx.ui.custom<DebateOutput | null>(
960 + (tui, theme, _kb, done) => {
961 + const loader = new BorderedLoader(
962 + tui,
963 + theme,
964 + `🗡️ Devil: "${idea.length > 40 ? idea.slice(0, 40) + "..." : idea}" (${maxRounds}r)`,
965 + );
966 + loader.onAbort = () => done(null);
968 + runDebate({
969 + idea,
970 + context: debateContext,
971 + model: ctx.model!,
972 + apiKey: auth.apiKey!,
973 + headers: auth.headers,
974 + maxRounds,
975 + stoppingConditions,
976 + concessionThreshold,
977 + signal: loader.signal,
978 + onChallenge: (round, max, blocking, devilOutput) => {
979 + ctx.ui.setStatus("devil", `🗡️ Devil: R${round}/${max} challenging...`);
980 + const snippet = extractSnippet(devilOutput, 60);
981 + ctx.ui.setWidget("devil", [
982 + `🗡️ Round ${round}/${max} · ${blocking} blocking`,
983 + snippet ? ` ⚑ ${snippet}` : " ⚑ Analyzing...",
984 + ]);
985 + },
986 + onRound: (round, max, blocking, _status, _devilOutput, proposerOutput) => {
987 + ctx.ui.setStatus("devil", `🗡️ Devil: R${round}/${max} defending...`);
988 + const snippet = extractSnippet(proposerOutput, 60);
989 + ctx.ui.setWidget("devil", [
990 + `🗡️ Round ${round}/${max} · ${blocking} blocking`,
991 + snippet ? ` ✓ ${snippet}` : " ✓ Defended",
992 + ]);
993 + },
994 + })
995 + .then((output) => {
996 + ctx.ui.setWidget("devil", undefined);
997 + if (output.aborted) done(null);
998 + else done(output);
999 + })
1000 + .catch((err) => {
1001 + ctx.ui.setWidget("devil", undefined);
1002 + console.error("[devil] runDebate error:", err);
1003 + done(null);
1004 + });
1006 + return loader;
1007 + },
1008 + );
1010 + if (result === null) {
1011 + ctx.ui.setStatus("devil", undefined);
1012 + ctx.ui.setWidget("devil", undefined);
1013 + ctx.ui.notify("Debate cancelled", "info");
1014 + return;
1017 + const outputText = formatDebateOutput(result, onBlocking);
1019 + // Modal editor: Enter confirms (→ main editor), Esc discards
1020 + const edited = await ctx.ui.editor(
1021 + `🗡️ ${result.recommendation.replace(/_/g, " ")} · ${result.blockingRaised} raised, ${result.blockingRemaining} remaining`,
1022 + outputText,
1023 + );
1025 + ctx.ui.setStatus("devil", undefined);
1026 + ctx.ui.setWidget("devil", undefined);
1028 + if (edited === undefined) {
1029 + ctx.ui.notify("Debate result discarded", "info");
1030 + return;
1033 + ctx.ui.setEditorText(edited);
1034 + ctx.ui.notify("Edit and submit when ready", "success");

pi/agent/extensions/devil/types.ts

Modified
3 3 * Types for the Devil's Advocate debate tool
6 -export interface DebateChallenge {
7 - id: string;
8 - claim: string;
9 - severity: {
10 - premise_fatality: number; // 0-1: destroys entire idea?
11 - scope_impact: number; // 0-1: how much of idea affected?
12 - reversibility: number; // 0-1: can idea be amended?
13 - evidence_weight: number; // 0-1: quality of challenge
14 - };
15 - weighted_impact: number; // computed: weighted sum
16 - confidence: {
17 - initial: { point: number; range: [number, number] };
18 - post_rebuttal: {
19 - point: number;
20 - range: [number, number];
21 - shift_reason: string;
22 - };
23 - };
24 - resolution: "resolved" | "contested" | "unresolved";
25 -}
27 -export interface DebateRound {
28 - round_id: number;
29 - challenges: string[];
30 - rebuttals: string[];
31 - new_information: Array<{
32 - claim: string;
33 - novelty_score: number; // 0-1
34 - significance: "major" | "minor" | "none";
35 - }>;
36 - round_shift_detected: boolean;
37 -}
39 -export interface RemainingTension {
40 - description: string;
41 - affected_claims: string[];
42 - requires_human_judgment: boolean;
43 -}
45 -export interface StrongestObjection {
46 - objection: string;
47 - effectiveness_score: number; // 0-1
48 - why_effective: string;
49 -}
51 -export interface DebateResult {
52 - idea: string;
53 - rounds: DebateRound[];
54 - challenges: DebateChallenge[];
55 - strongest_objection: StrongestObjection;
56 - remaining_tensions: RemainingTension[];
57 - confidence_delta: number; // -1 to +1
58 - recommendation: "proceed" | "revise" | "abandon" | "proceed_with_caution";
59 -}
61 -export interface DevilsAdvocateOptions {
62 - /** The idea/claim/decision to debate */
63 - idea: string;
64 - /** Background context */
65 - context?: Record<string, string>;
66 - /** Number of challenge-response rounds (default: 3) */
67 - rounds?: number;
68 - /** Challenge style */
69 - perspective?: "strongest" | "naive" | "expert" | "contrarian";
70 - /** Include confidence calibration (default: true) */
71 - confidence_mode?: boolean;
72 - /** Track information novelty per round (default: true) */
73 - track_novelty?: boolean;
74 - /** Output mode */
75 - output?: "full" | "critique_only";
76 -}
78 -export const SEVERITY_WEIGHTS = {
79 - premise_fatality: 0.4,
80 - scope_impact: 0.3,
81 - reversibility: 0.2,
82 - evidence_weight: 0.1,
83 -};
85 -export function computeWeightedImpact(
86 - severity: DebateChallenge["severity"]
87 -): number {
88 - return (
89 - severity.premise_fatality * SEVERITY_WEIGHTS.premise_fatality +
90 - severity.scope_impact * SEVERITY_WEIGHTS.scope_impact +
91 - severity.reversibility * SEVERITY_WEIGHTS.reversibility +
92 - severity.evidence_weight * SEVERITY_WEIGHTS.evidence_weight
93 - );
6 +export interface DebateOutput {
7 + synthesis: string;
8 + roundsData: string[];
9 + recommendation: "proceed" | "proceed_with_caution" | "revise" | "abandon";
10 + /** Blocking concerns raised by devil across all rounds */
11 + blockingRaised: number;
12 + /** Blocking concerns remaining after synthesis evaluation */
13 + blockingRemaining: number;
14 + aborted?: boolean;