5 5 * Designed to reduce sycophancy by forcing critical examination.
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",
16 - *
"stoppingConditions":
"consensus",
17 - *
"concessionThreshold":
0.7,
18 - *
"onBlocking":
"surface" 16 + *
"stoppingConditions":
"consensus",
17 + *
"concessionThreshold":
0.7,
18 + *
"onBlocking":
"surface" 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";
30 + serializeConversation,
31 +}
from "@mariozechner/pi-coding-agent";
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.
42 -- "Req auth missing" → blocking
43 -- "Users confused" →
addressable (add onboarding)
44 -- "Assumes fast net" →
assumed (premise fragile)
45 -- "Blue vs green button" → minor
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." 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.
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.
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
90 -### Addressable Concerns
91 -- [concern] → [mitigation]
97 -- [acknowledged weaknesses]
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";
123 + blocking
: string[];
124 + addressable
: string[];
125 + survived
: string[];
126 + conceded
: string[];
127 + next_steps
: string[];
130 +interface RunDebateOptions {
132 + context
?: Record<
string,
string>;
135 + headers
?: Record<
string,
string>;
137 + stoppingConditions
: "consensus" | "blocking_found" | "max_rounds";
138 + concessionThreshold
: number;
139 + signal
?: AbortSignal;
145 + devilOutput
: string,
153 + devilOutput
: string,
154 + proposerOutput
: string,
161 +const MAX_FILE_CHARS
= 3000;
162 +const MAX_FILES
= 10;
163 +const FILE_TOOL_NAMES
= new Set([
"read",
"edit",
"write"]);
165 +interface ToolCallBlock {
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();
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;
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());
198 + if (fs.
existsSync(resolved)
&& fs.
statSync(resolved).
isFile()) {
199 + filePaths.
add(resolved);
207 + if (filePaths.
size > 0) {
208 + const files
= [
...filePaths].
slice(
0, MAX_FILES);
209 + const fileContents
= files
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}\``;
222 + .
filter((c)
: c
is string => c
!== null);
223 + if (fileContents.
length > 0) {
224 + context[
"relevant_files"]
= fileContents.
join(
"\n\n");
229 + const messages
= branch
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);
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) {
250 + .
replace(
/^#{1,6}\s*/,
"")
251 + .
replace(
/\*\*/g,
"")
254 + clean.
length > 5 && 255 + !clean.
startsWith(
"Concern:")
&& 256 + !clean.
startsWith(
"Fix:")
&& 257 + !clean.
startsWith(
"Status:")
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>
= {
268 + proceed_with_caution:
"⚡",
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`;
301 +function parseSynthesis(raw
: string)
: SynthesisResult {
304 + const jsonMatch
= raw.
match(
/\{[\s\S]*\}/);
306 + const parsed
= JSON.
parse(jsonMatch[
0]);
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)
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)
329 + const recMatch
= raw.
match(
/\*\*recommendation\*\*:\s*(\w+)/i);
330 + const rec
= recMatch?.[
1]?.
toLowerCase();
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),
347 +async function runDebate(opts
: RunDebateOptions)
: Promise<
DebateOutput> {
355 + stoppingConditions,
356 + concessionThreshold,
362 + const contextText
= context
363 + ? Object.
entries(context)
364 + .
map(([k, v])
=> `- **${k}**: ${v}`)
366 + : "No additional context provided.";
368 + const roundsData
: string[]
= [];
369 + let blockingIssuesFound
= 0;
370 + let consecutiveResolved
= 0;
371 + let shouldContinue
= true;
372 + let roundHistory
= "";
374 + for (
let i
= 1; i
<= maxRounds
&& shouldContinue; i
++) {
375 + if (signal?.aborted) {
377 + synthesis:
"Debate cancelled by user.",
379 + recommendation:
"proceed_with_caution",
380 + blockingRaised: blockingIssuesFound,
381 + blockingRemaining: blockingIssuesFound,
387 + const devilUserText
= [
388 + `Context:\n${contextText}`,
391 + ? `Previous rounds:\n${roundHistory}` 392 + : "This is the first round.",
393 + `Round ${i}/${maxRounds}. Identify the most blocking concern not yet resolved.`,
396 + const devilResponse
= await complete(
399 + systemPrompt: DEVIL_SYSTEM_PROMPT,
403 + content: [{ type:
"text", text: devilUserText }],
404 + timestamp:
Date.
now(),
408 + { apiKey, headers, signal },
411 + if (signal?.aborted) {
413 + synthesis:
"Debate cancelled by user.",
415 + recommendation:
"proceed_with_caution",
416 + blockingRaised: blockingIssuesFound,
417 + blockingRemaining: blockingIssuesFound,
422 + const devilOutput
= devilResponse.
content 423 + .
filter((c)
: c
is { type:
"text"; text: string }
=> c.
type === "text")
424 + .
map((c)
=> c.
text)
427 + const roundBlockingCount
= 428 + (devilOutput.
match(
/\[blocking\]/gi)
|| []).
length + 429 + (devilOutput.
match(
/\*\*blocking\*\*/gi)
|| []).
length;
430 + blockingIssuesFound
= Math.
max(blockingIssuesFound, roundBlockingCount);
433 + onChallenge?.(i, maxRounds, blockingIssuesFound, devilOutput);
436 + const proposerUserText
= [
437 + `Your idea: ${idea}`,
439 + ? `Previous rounds:\n${roundHistory}` 440 + : "This is the first round.",
441 + `Current challenge (Round ${i}):`,
443 + "Respond to this challenge.",
446 + const proposerResponse
= await complete(
449 + systemPrompt: PROPOSER_SYSTEM_PROMPT,
453 + content: [{ type:
"text", text: proposerUserText }],
454 + timestamp:
Date.
now(),
458 + { apiKey, headers, signal },
461 + if (signal?.aborted) {
463 + synthesis:
"Debate cancelled by user.",
465 + recommendation:
"proceed_with_caution",
466 + blockingRaised: blockingIssuesFound,
467 + blockingRemaining: blockingIssuesFound,
472 + const proposerOutput
= proposerResponse.
content 473 + .
filter((c)
: c
is { type:
"text"; text: string }
=> c.
type === "text")
474 + .
map((c)
=> c.
text)
478 + `### Round ${i}\n\n**Challenge:**\n${devilOutput}\n\n**Response:**\n${proposerOutput}`,
482 + roundHistory
+= `### Round ${i}\n**Challenge:** ${devilOutput.slice(0, 500)}\n**Response:** ${proposerOutput.slice(0, 500)}\n\n`;
485 + const status
= `Round ${i}/${maxRounds} · ${blockingIssuesFound} blocking`;
486 + onRound?.(i, maxRounds, blockingIssuesFound, status, devilOutput, proposerOutput);
489 + const resolvedCount
= 491 + proposerOutput.
match(
492 + /\*\*status\*\*:\s*resolved|status:\s*resolved/gi,
495 + const concessionIndicators = (
496 + proposerOutput.
match(
497 + /fair point|good point|you're right|concede|acknowledge|valid point|no new blocking/i,
500 + const concessionScore
= resolvedCount
+ concessionIndicators
* 0.5;
502 + if (stoppingConditions
=== "consensus") {
503 + if (concessionScore
>= concessionThreshold) {
504 + consecutiveResolved
++;
505 + if (consecutiveResolved
>= 2) shouldContinue
= false;
507 + consecutiveResolved
= 0;
510 + stoppingConditions
=== "blocking_found" && 511 + roundBlockingCount
=== 0 513 + shouldContinue
= false;
514 + }
else if (stoppingConditions
=== "max_rounds") {
515 + shouldContinue
= i
< maxRounds;
519 + if (signal?.aborted) {
521 + synthesis:
"Debate cancelled by user.",
523 + recommendation:
"proceed_with_caution",
524 + blockingRaised: blockingIssuesFound,
525 + blockingRemaining: blockingIssuesFound,
531 + const synthesisUserText
= [
533 + `Debate (${roundsData.length} rounds):`,
534 + roundsData.
join(
"\n\n---\n\n"),
537 + const synthesisResponse
= await complete(
540 + systemPrompt: SYNTHESIS_SYSTEM_PROMPT,
544 + content: [{ type:
"text", text: synthesisUserText }],
545 + timestamp:
Date.
now(),
549 + { apiKey, headers, signal },
552 + const synthesisRaw
= synthesisResponse.
content 553 + .
filter((c)
: c
is { type:
"text"; text: string }
=> c.
type === "text")
554 + .
map((c)
=> c.
text)
557 + const synthesis
= parseSynthesis(synthesisRaw);
558 + const synthesisFormatted
= formatSynthesis(synthesis);
561 + synthesis: synthesisFormatted,
563 + recommendation: synthesis.recommendation,
564 + blockingRaised: blockingIssuesFound,
565 + blockingRemaining: synthesis.blocking.
length,
569 +function formatDebateOutput(
570 + output
: DebateOutput,
571 + onBlocking
: "surface" | "escalate" | "ignore",
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}`;
117 587 export default async function (pi
: ExtensionAPI) {
119 588 const settings
= pi.settingsManager?.
getSettings();
120 589 const devilSettings
: DevilSettings = settings?.devilsAdvocate
?? {};
593 + pi.
registerFlag(
"devil-max-rounds", {
594 + description:
"Override max debate rounds (1-10)",
596 + default: devilSettings.maxRounds
?? DEFAULT_MAX_ROUNDS,
599 + pi.
registerFlag(
"devil-stop", {
600 + description:
"Override stopping condition (consensus | blocking_found | max_rounds)",
602 + default: devilSettings.stoppingConditions
?? DEFAULT_STOPPING_CONDITIONS,
606 + pi.
on(
"before_agent_start",
async (_event, ctx)
=> {
607 + ctx.ui.
setStatus(
"devil",
undefined);
608 + ctx.ui.
setWidget(
"devil",
undefined);
610 + pi.
on(
"session_shutdown",
async (_event, ctx)
=> {
611 + ctx.ui.
setStatus(
"devil",
undefined);
612 + ctx.ui.
setWidget(
"devil",
undefined);
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. 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`,
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.",
136 629 parameters: Type.
Object({
137 - idea: Type.
String({
138 - description:
"Idea/claim/decision. Be specific.",
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: \"...\" }",
633 + description:
'Background: { problem: "...", constraint: "...", scale: "..." }',
145 - model: Type.
Optional(Type.
String({
146 - description:
"Model override (e.g., 'anthropic/claude-haiku-4-5')",
148 - maxRounds: Type.
Optional(
149 - Type.
Number({ minimum:
1, maximum:
10 })
636 + model: Type.
Optional(
638 + description:
"Model override (e.g., 'anthropic/claude-haiku-4-5')",
641 + maxRounds: Type.
Optional(Type.
Number({ minimum:
1, maximum:
10 })),
151 642 stakeholder: Type.
Optional(
153 - Type.
Literal(
"security"),
154 - Type.
Literal(
"cost"),
155 - Type.
Literal(
"ux"),
156 - Type.
Literal(
"implementation"),
157 - Type.
Literal(
"ethics"),
158 - Type.
Literal(
"skeptical"),
643 + StringEnum([
"security",
"cost",
"ux",
"implementation",
"ethics",
"skeptical"]
as const),
161 645 stoppingConditions: Type.
Optional(
163 - Type.
Literal(
"consensus"),
164 - Type.
Literal(
"blocking_found"),
165 - Type.
Literal(
"max_rounds"),
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(
173 - Type.
Literal(
"surface"),
174 - Type.
Literal(
"escalate"),
175 - Type.
Literal(
"ignore"),
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;
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}` : "";
672 + theme.
fg(
"toolTitle", theme.
bold(
"devils_advocate "))
+ 673 + theme.
fg(
"dim",
`"${snippet}"`)
+ 674 + theme.
fg(
"muted",
` ${maxRounds}r${focus}${stop}`),
680 + renderResult(result, { expanded }, theme) {
681 + const details
= result.details as
683 + recommendation
?: string;
685 + blockingRaised
?: number;
686 + blockingRemaining
?: number;
693 + const text
= result.
content[
0];
694 + return new Text(text?.
type === "text" ? text.
text : "",
0,
0);
697 + if (details.aborted) {
699 + theme.
fg(
"warning",
"⚠ ")
+ theme.
fg(
"muted",
"Debate cancelled"),
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;
714 + recColor
= "success";
715 + recLabel
= "✓ proceed";
718 + recColor
= "warning";
719 + recLabel
= "⚠ revise";
722 + recColor
= "error";
723 + recLabel
= "✗ abandon";
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(
"## ")) {
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);
182 750 execute:
async (_toolCallId, params, signal, onUpdate, ctx)
=> {
753 + context: explicitContext,
186 754 model: modelOverride,
187 755 maxRounds: maxRoundsParam,
189 757 stoppingConditions: stoppingConditionsParam,
190 758 concessionThreshold: concessionThresholdParam,
191 759 onBlocking: onBlockingParam,
192 - output
= "summary_only" 760 + output
= "summary_only",
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;
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;
774 + onBlockingParam
?? devilSettings.onBlocking
?? DEFAULT_ON_BLOCKING;
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;
219 - content: [{ type:
"text", text:
"Error: No model available. Select a model first with /model." }],
795 + { type:
"text", text:
"Error: No model available. Use /model." },
220 797 details: { error:
"No model" },
224 801 const auth
= await ctx.modelRegistry.
getApiKeyAndHeaders(model);
225 802 if (
!auth.ok
|| !auth.apiKey) {
227 - content: [{ type:
"text", text:
`Error: No API key for ${model.provider}` }],
805 + { type:
"text", text:
`Error: No API key for ${model.provider}` },
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.";
812 + const sessionContext
= gatherContext(ctx);
813 + const mergedContext
= {
815 + ...(explicitContext
?? {}),
817 + ? { _stakeholder:
`Challenge from ${stakeholder} perspective.` }
236 - const stakeholderContext
= stakeholder
237 - ? `\n\n**Focus:** Challenge specifically from ${stakeholder} perspective.` 242 - ctx.ui.
setStatus(
"devil",
`🗡️ Devil: ${idea.slice(0, 40)}... (${maxRounds}r)`);
824 + `🗡️ Devil: ${idea.slice(0, 40)}... (${maxRounds}r)`,
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
++) {
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() },
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");
262 - const roundBlockingCount
= (devilOutput.
match(
/\[blocking\]/gi)
|| []).
length;
263 - blockingIssuesFound
= Math.
max(blockingIssuesFound, roundBlockingCount);
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() },
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}`);
278 - ctx.ui.
setStatus(
"devil",
`🗡️ Devil: Round ${i}/${maxRounds} - ${roundBlockingCount} blocking`);
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({
831 + context: mergedContext,
833 + apiKey: auth.apiKey,
834 + headers: auth.
headers,
836 + stoppingConditions: stoppingConditions as
840 + concessionThreshold,
842 + onChallenge: (round, max, blocking, devilOutput)
=> {
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...",
291 - }
else if (stoppingConditions
=== "blocking_found" && roundBlockingCount
=== 0) {
292 - shouldContinue
= false;
293 - }
else if (stoppingConditions
=== "max_rounds") {
295 - shouldContinue
= i
< maxRounds;
852 + onRound: (round, max, blocking, status, devilOutput, proposerOutput)
=> {
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",
865 + text:
`Round ${round}/${max} · ${blocking} blocking\n⚑ ${extractSnippet(devilOutput, 80)}\n✓ ${extractSnippet(proposerOutput, 80)}`,
868 + details: { round, maxRounds: max, blockingRaised: blocking },
298 - if (resolvedCount
> 0) consecutiveResolved
++;
875 + ? `## Full Debate\n\n${result.roundsData.join("\n\n---\n\n")}\n\n---\n\n${formatDebateOutput(result, onBlocking)}` 876 + : formatDebateOutput(result, onBlocking);
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() },
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");
311 - const recMatch
= synthesisOutput.
match(
/\*\*recommendation\*\*:\s*(\w+)/i);
312 - let recommendation
= "proceed_with_caution";
314 - recommendation
= recMatch[
1].
toLowerCase();
315 - if (
![
"proceed",
"proceed_with_caution",
"revise",
"abandon"].
includes(recommendation)) {
316 - recommendation
= "proceed_with_caution";
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") {
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}*`;
334 879 ctx.ui.
setStatus(
"devil",
undefined);
880 + ctx.ui.
setWidget(
"devil",
undefined);
338 884 content: [{ type:
"text", text: outputText }],
339 - details: { recommendation, rounds: rounds_data.
length, blockingIssues: blockingIssuesFound },
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,
341 894 }
catch (error) {
342 895 const message
= error
instanceof Error ? error.message
: String(error);
345 897 ctx.ui.
setStatus(
"devil",
undefined);
898 + ctx.ui.
setWidget(
"devil",
undefined);
348 901 content: [{ type:
"text", text:
`## Debate Failed\n\nError: ${message}` }],
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();
915 + ctx.ui?.
notify(
"Type an idea in the editor first, or use /devil <idea>",
"info");
919 + if (text.
startsWith(
"## Verdict:")
|| text.
startsWith(
"*Debate:")) {
920 + ctx.ui?.
notify(
"Editor contains debate output, not an idea. Clear it first.",
"info");
923 + pi.
sendUserMessage(
`/devil ${text}`);
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();
361 932 ctx.ui?.
notify(
"Usage: /devil <your idea>",
"error");
364 - await ctx.tools?.
invoke(
"devils_advocate", {
366 - maxRounds: devilSettings.maxRounds
?? DEFAULT_MAX_ROUNDS,
367 - stoppingConditions: (devilSettings.stoppingConditions
as any)
?? DEFAULT_STOPPING_CONDITIONS,
936 + ctx.ui?.
notify(
"Devil requires interactive mode",
"error");
940 + ctx.ui?.
notify(
"No model selected. Use /model first.",
"error");
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");
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(
964 + `🗡️ Devil: "${idea.length > 40 ? idea.slice(0, 40) + "..." : idea}" (${maxRounds}r)`,
966 + loader.
onAbort = ()
=> done(
null);
970 + context: debateContext,
972 + apiKey: auth.apiKey
!,
973 + headers: auth.
headers,
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...",
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",
995 + .
then((output)
=> {
996 + ctx.ui.
setWidget(
"devil",
undefined);
997 + if (output.aborted)
done(
null);
1001 + ctx.ui.
setWidget(
"devil",
undefined);
1002 + console.
error(
"[devil] runDebate error:", err);
1010 + if (result
=== null) {
1011 + ctx.ui.
setStatus(
"devil",
undefined);
1012 + ctx.ui.
setWidget(
"devil",
undefined);
1013 + ctx.ui.
notify(
"Debate cancelled",
"info");
1017 + const outputText
= formatDebateOutput(result, onBlocking);
1020 + const edited
= await ctx.ui.
editor(
1021 + `🗡️ ${result.recommendation.replace(/_/g, " ")} · ${result.blockingRaised} raised, ${result.blockingRemaining} remaining`,
1025 + ctx.ui.
setStatus(
"devil",
undefined);
1026 + ctx.ui.
setWidget(
"devil",
undefined);
1028 + if (edited
=== undefined) {
1029 + ctx.ui.
notify(
"Debate result discarded",
"info");
1033 + ctx.ui.
setEditorText(edited);
1034 + ctx.ui.
notify(
"Edit and submit when ready",
"success");