{"version":3,"file":"thinking-escalation.d.ts","sourceRoot":"","sources":["../../../src/extensions/core/thinking-escalation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EAEX,YAAY,EAIZ,MAAM,gCAAgC,CAAC;AAGxC,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI,CAuD/D","sourcesContent":["/**\n * Tool-outcome-driven thinking escalation.\n *\n * Raise the thinking level for the turn(s) following a tool error, then restore\n * it. On by default; configured via `thinking_escalation` in hoo-config.json.\n *\n * Timing model (per agent run):\n *   turn N: assistant calls tools → tool_execution_end(s) → turn_end\n *           a failing tool here escalates the level used by turn N+1\n *   turn N+1: runs escalated; its turn_end decrements the cooldown and, at zero,\n *             restores the captured baseline so turn N+2 is fast again\n *\n * `escalatedThisTurn` ensures the error turn's own `turn_end` does not consume a\n * cooldown step — the cooldown counts the turns *after* the failure.\n */\n\nimport type { ThinkingLevel } from \"@kolisachint/hoocode-agent-core\";\nimport type {\n\tAgentStartEvent,\n\tExtensionAPI,\n\tExtensionContext,\n\tToolExecutionEndEvent,\n\tTurnEndEvent,\n} from \"../../core/extensions/types.js\";\nimport { readMergedConfig } from \"./config.js\";\n\nexport function setupThinkingEscalation(hoo: ExtensionAPI): void {\n\t// Captured user level to restore to; null means \"not currently escalated\".\n\tlet baseline: ThinkingLevel | null = null;\n\tlet remaining = 0;\n\tlet escalatedThisTurn = false;\n\n\tconst restore = (): void => {\n\t\tif (baseline !== null) {\n\t\t\thoo.setThinkingLevel(baseline);\n\t\t}\n\t\tbaseline = null;\n\t\tremaining = 0;\n\t\tescalatedThisTurn = false;\n\t};\n\n\t// A fresh user prompt starts clean — restore any lingering escalation.\n\thoo.on(\"agent_start\", (_event: AgentStartEvent) => {\n\t\trestore();\n\t});\n\n\thoo.on(\"tool_execution_end\", (event: ToolExecutionEndEvent, ctx: ExtensionContext) => {\n\t\tif (!event.isError) return;\n\n\t\tconst cfg = readMergedConfig(ctx.cwd).thinking_escalation;\n\t\t// On by default: only an explicit `enabled: false` disables it. `cfg` may be\n\t\t// undefined when no thinking_escalation block is configured at all.\n\t\tif (cfg?.enabled === false) return;\n\t\tif (cfg?.tools && cfg.tools.length > 0 && !cfg.tools.includes(event.toolName)) return;\n\n\t\tconst target = cfg?.on_error ?? \"high\";\n\t\tconst cooldown = Math.max(1, cfg?.cooldown_turns ?? 1);\n\n\t\t// Capture the user's level only on the first escalation so repeated errors\n\t\t// extend the window without overwriting the restore point with \"high\".\n\t\tif (baseline === null) {\n\t\t\tbaseline = hoo.getThinkingLevel();\n\t\t}\n\t\thoo.setThinkingLevel(target);\n\t\tremaining = cooldown;\n\t\tescalatedThisTurn = true;\n\t});\n\n\thoo.on(\"turn_end\", (_event: TurnEndEvent) => {\n\t\tif (baseline === null) return; // not escalated\n\t\tif (escalatedThisTurn) {\n\t\t\t// This is the turn_end of the turn that failed; the cooldown applies to\n\t\t\t// subsequent turns, so don't consume a step here.\n\t\t\tescalatedThisTurn = false;\n\t\t\treturn;\n\t\t}\n\t\tremaining -= 1;\n\t\tif (remaining <= 0) {\n\t\t\trestore();\n\t\t}\n\t});\n}\n"]}