{"version":3,"file":"register-main.d.ts","sourceRoot":"","sources":["../../../src/watchdog/register-main.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAA2B,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAgBzG,OAAO,EAAE,mBAAmB,EAAE,KAAK,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAUhF,UAAU,2BAA2B;IACpC,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAC9B,MAAM,CAAC,EAAE,sBAAsB,CAAC;CAChC;AAiGD,wBAAgB,mBAAmB,CAClC,QAAQ,EAAE,UAAU,CAAC,mBAAmB,CAAC,aAAa,CAAC,CAAC,EACxD,GAAG,EAAE,gBAAgB,GACnB,MAAM,CAsDR;AAiQD,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,YAAY,EAAE,OAAO,GAAE,2BAAgC,GAAG,mBAAmB,CA2FrH","sourcesContent":["import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext } from \"@lpb-work/pi-coding-agent\";\nimport { Text } from \"@lpb-work/pi-tui\";\nimport {\n\tresolveEffectiveThinking,\n\tsplitKnownThinkingSuffix,\n\tTHINKING_LEVELS,\n\ttype ThinkingLevel,\n} from \"../shared/model-info.ts\";\nimport { SLASH_TEXT_RESULT_TYPE } from \"../shared/types.ts\";\nimport {\n\tparseWatchdogThinkingInput,\n\trecommendStrongWatchdogModel,\n\tresolveWatchdogModelInput,\n} from \"./model-selection.ts\";\nimport { renderWatchdogWarning } from \"./render.ts\";\nimport { createMainWatchdogReview } from \"./review.ts\";\nimport { MainWatchdogRuntime, type WatchdogReviewFunction } from \"./runtime.ts\";\nimport { getWatchdogUserSettingsPath, writeUserWatchdogEnabled, writeWatchdogModelSettings } from \"./settings.ts\";\nimport {\n\tSUBAGENT_WATCHDOG_WARNING_TYPE,\n\ttype WatchdogRuntimeStatus,\n\ttype WatchdogWarning,\n\ttype WatchdogWarningDetails,\n} from \"./types.ts\";\nimport { createWatchdogWarningMessage } from \"./warning-format.ts\";\n\ninterface RegisterMainWatchdogOptions {\n\truntime?: MainWatchdogRuntime;\n\treview?: WatchdogReviewFunction;\n}\n\nfunction sendSlashText(pi: ExtensionAPI, text: string): void {\n\tpi.sendMessage({ customType: SLASH_TEXT_RESULT_TYPE, content: text, display: true });\n}\n\nfunction messageFromError(error: unknown): string {\n\treturn error instanceof Error ? error.message : String(error);\n}\n\nfunction boolLabel(value: boolean): string {\n\treturn value ? \"on\" : \"off\";\n}\n\nfunction statusLabel(status: WatchdogRuntimeStatus): string {\n\treturn status.replaceAll(\"-\", \" \");\n}\n\nfunction sourceLine(source: { scope: string; path?: string; exists: boolean }): string {\n\tconst location = source.path ? ` ${source.path}` : \"\";\n\treturn `- ${source.scope}${location}: ${source.exists ? \"found\" : \"not found\"}`;\n}\n\nfunction currentSessionModelLine(ctx: ExtensionContext): string {\n\tconst model = ctx.model as { provider?: unknown; id?: unknown } | undefined;\n\tif (model && typeof model.provider === \"string\" && typeof model.id === \"string\")\n\t\treturn `current session (${model.provider}/${model.id})`;\n\treturn \"current session (not configured)\";\n}\n\nfunction mainThinkingLine(snapshot: ReturnType<MainWatchdogRuntime[\"getSnapshot\"]>, ctx: ExtensionContext): string {\n\tconst configuredModel = snapshot.config.main.model;\n\tconst configuredThinking = snapshot.config.main.thinking;\n\tif (configuredModel) {\n\t\tconst effective = resolveEffectiveThinking(configuredModel, configuredThinking);\n\t\tif (effective) return effective;\n\t\treturn \"off (default for explicit watchdog model)\";\n\t}\n\tif (configuredThinking === false) return \"off\";\n\tif (configuredThinking !== undefined) return configuredThinking;\n\tconst currentThinking = (ctx as { thinkingLevel?: unknown }).thinkingLevel;\n\treturn typeof currentThinking === \"string\" ? `current session (${currentThinking})` : \"current session\";\n}\n\nfunction mainModelLine(snapshot: ReturnType<MainWatchdogRuntime[\"getSnapshot\"]>, ctx: ExtensionContext): string {\n\tif (snapshot.config.main.model) {\n\t\tconst source = snapshot.sessionModelOverride?.model ? \"session override\" : \"configured\";\n\t\treturn `Main model: ${splitKnownThinkingSuffix(snapshot.config.main.model).baseModel} (${source})`;\n\t}\n\treturn `Main model: ${currentSessionModelLine(ctx)}`;\n}\n\nfunction childrenLine(snapshot: ReturnType<MainWatchdogRuntime[\"getSnapshot\"]>): string {\n\tconst children = snapshot.config.children;\n\tconst model = children.model ? splitKnownThinkingSuffix(children.model).baseModel : \"current child session\";\n\tconst thinking =\n\t\tchildren.thinking === undefined\n\t\t\t? \"current child session\"\n\t\t\t: children.thinking === false\n\t\t\t\t? \"off\"\n\t\t\t\t: children.thinking;\n\tconst overrides = Object.entries(children.overrides);\n\tconst overrideText = overrides.length\n\t\t? ` · overrides ${overrides\n\t\t\t\t.map(([agent, override]) => {\n\t\t\t\t\tconst bits = [agent];\n\t\t\t\t\tif (override.enabled !== undefined) bits.push(boolLabel(override.enabled));\n\t\t\t\t\tif (override.model) bits.push(splitKnownThinkingSuffix(override.model).baseModel);\n\t\t\t\t\tif (override.thinking !== undefined)\n\t\t\t\t\t\tbits.push(`thinking ${override.thinking === false ? \"off\" : override.thinking}`);\n\t\t\t\t\treturn bits.join(\" \");\n\t\t\t\t})\n\t\t\t\t.join(\"; \")}`\n\t\t: \"\";\n\treturn `Children: ${boolLabel(snapshot.config.enabled && children.enabled)} · model ${model} · thinking ${thinking}${overrideText}`;\n}\n\nfunction recommendationLine(ctx: ExtensionContext): string {\n\ttry {\n\t\tconst recommendation = recommendStrongWatchdogModel(ctx);\n\t\treturn `Recommended strong watchdog: ${recommendation.model}:${recommendation.thinking} (${recommendation.label}, complementary reviewer)`;\n\t} catch (error) {\n\t\treturn `Recommended strong watchdog: unavailable (${messageFromError(error)})`;\n\t}\n}\n\nfunction lspLine(snapshot: ReturnType<MainWatchdogRuntime[\"getSnapshot\"]>): string {\n\tconst lsp = snapshot.lsp;\n\tconst provider = lsp.provider ? ` · ${lsp.provider}` : \"\";\n\tconst counts =\n\t\tlsp.diagnosticCount > 0 || lsp.freshDiagnosticCount > 0\n\t\t\t? ` · ${lsp.freshDiagnosticCount} new/${lsp.diagnosticCount} total`\n\t\t\t: \"\";\n\tconst message = lsp.message ? ` · ${lsp.message}` : \"\";\n\treturn `LSP diagnostics: ${lsp.enabled ? \"on\" : \"off\"} · ${lsp.status}${provider}${counts}${message}`;\n}\n\nexport function buildWatchdogStatus(\n\tsnapshot: ReturnType<MainWatchdogRuntime[\"getSnapshot\"]>,\n\tctx: ExtensionContext,\n): string {\n\tconst lines = [\n\t\t\"Subagent watchdog\",\n\t\t`Main: ${boolLabel(snapshot.enabled)}${!snapshot.config.enabled && snapshot.sessionOverride === undefined ? \" (default off)\" : \"\"}`,\n\t\t`Runtime: ${statusLabel(snapshot.status)}${snapshot.bufferedDeltas > 0 ? ` · buffered deltas ${snapshot.bufferedDeltas}` : \"\"}`,\n\t\t`Review trigger: ${snapshot.reviewTrigger === \"repo-edits\" ? \"repo edits only\" : \"every non-empty turn delta\"}`,\n\t\t`Scope context: ${snapshot.config.scope.enabled ? \"on\" : \"off\"}`,\n\t\t`Cadence: ${snapshot.config.cadence.everyNTools === null ? \"boundary only\" : `every ${snapshot.config.cadence.everyNTools} tools + boundary`}`,\n\t\tlspLine(snapshot),\n\t\t`Session override: ${snapshot.sessionOverride === undefined ? \"none\" : boolLabel(snapshot.sessionOverride)}`,\n\t\tmainModelLine(snapshot, ctx),\n\t\t`Main thinking: ${mainThinkingLine(snapshot, ctx)}`,\n\t\tchildrenLine(snapshot),\n\t\trecommendationLine(ctx),\n\t\t`Agent-end timeout: ${snapshot.config.agentEndTimeoutMs}ms`,\n\t\t`Auto-follow: ${snapshot.enabled && snapshot.config.autoFollow.blockers ? \"on for blockers\" : \"off\"} · attempts ${snapshot.autoFollowAttempts}${snapshot.config.autoFollow.maxAttempts === null ? \"\" : `/${snapshot.config.autoFollow.maxAttempts}`}${snapshot.autoFollowQueued ? \" · queued\" : \"\"}${snapshot.autoFollowStalemate ? \" · stalemate\" : \"\"}`,\n\t\t`Review model call: ${snapshot.reviewDescription}`,\n\t];\n\tif (snapshot.failedReviews > 0) lines.push(`Failed reviews: ${snapshot.failedReviews}`);\n\tif (snapshot.staleReviews > 0) lines.push(`Stale reviews: ${snapshot.staleReviews}`);\n\tif (snapshot.changedPaths?.length) {\n\t\tlines.push(\n\t\t\t`Changed paths: ${snapshot.changedPaths.slice(0, 8).join(\", \")}${snapshot.changedPaths.length > 8 ? `, +${snapshot.changedPaths.length - 8} more` : \"\"}`,\n\t\t);\n\t}\n\tif (snapshot.lastWarning) {\n\t\tlines.push(\n\t\t\t`Last warning: ${snapshot.lastWarning.severity} · ${snapshot.lastWarning.state ?? \"candidate\"} · ${snapshot.lastWarning.summary}`,\n\t\t);\n\t}\n\tif (snapshot.lastError) lines.push(`Last error: ${snapshot.lastError}`);\n\tif (!snapshot.configOk) {\n\t\tlines.push(\n\t\t\t\"\",\n\t\t\t\"Config errors:\",\n\t\t\t...snapshot.errors.map((error) => `- ${error.message}`),\n\t\t\t\"Watchdog is disabled until the config is fixed.\",\n\t\t);\n\t} else {\n\t\tlines.push(\"\", \"Config: ok\");\n\t}\n\tlines.push(\n\t\t\"Sources:\",\n\t\t...snapshot.sources.map(sourceLine),\n\t\t\"\",\n\t\t\"Model commands:\",\n\t\t\"- /subagents-watchdog recommend-model\",\n\t\t\"- /subagents-watchdog model recommended\",\n\t\t\"- /subagents-watchdog model <provider/model[:thinking]>\",\n\t\t\"- /subagents-watchdog model inherit\",\n\t\t\"- /subagents-watchdog session model recommended\",\n\t\t'Agent action: subagent({ action: \"watchdog.configure\", model: \"recommended\", scope: \"session\" })',\n\t);\n\treturn lines.join(\"\\n\");\n}\n\nfunction parseTestCommand(input: string): { severity: \"concern\" | \"blocker\"; text: string } | undefined {\n\tconst match = input.match(/^test\\s+(concern|blocker)\\s+([\\s\\S]+)$/);\n\tif (!match) return undefined;\n\treturn { severity: match[1] as \"concern\" | \"blocker\", text: match[2]!.trim() };\n}\n\nfunction formatThinking(value: ThinkingLevel | false | undefined): string {\n\tif (value === undefined) return \"inherit\";\n\treturn value === false ? \"off\" : value;\n}\n\nfunction parseThinkingCommand(raw: string): ThinkingLevel | false | null {\n\tconst value = raw.trim();\n\tif (value === \"inherit\") return null;\n\treturn parseWatchdogThinkingInput(value, \"/subagents-watchdog thinking\") ?? null;\n}\n\nfunction resolveModelCommandValue(\n\tctx: ExtensionCommandContext,\n\traw: string,\n): { model: string | null; thinking: ThinkingLevel | false | null; description: string } {\n\tconst value = raw.trim();\n\tif (!value) throw new Error(\"Expected a model, 'recommended', or 'inherit'.\");\n\tif (value === \"inherit\") return { model: null, thinking: null, description: \"current session model and thinking\" };\n\tif (value === \"recommended\") {\n\t\tconst recommendation = recommendStrongWatchdogModel(ctx as ExtensionContext);\n\t\treturn {\n\t\t\tmodel: recommendation.model,\n\t\t\tthinking: recommendation.thinking,\n\t\t\tdescription: `${recommendation.model}:${recommendation.thinking} (${recommendation.label})`,\n\t\t};\n\t}\n\tconst resolved = resolveWatchdogModelInput(ctx as ExtensionContext, value);\n\treturn {\n\t\tmodel: resolved.model,\n\t\tthinking: resolved.thinking ?? null,\n\t\tdescription: `${resolved.model}${resolved.thinking ? `:${resolved.thinking}` : \"\"}`,\n\t};\n}\n\nfunction buildRecommendationText(ctx: ExtensionCommandContext): string {\n\tconst recommendation = recommendStrongWatchdogModel(ctx as ExtensionContext);\n\treturn [\n\t\t\"Subagent watchdog recommended model\",\n\t\t`Current session: ${currentSessionModelLine(ctx as ExtensionContext)}`,\n\t\t`Recommended: ${recommendation.model}:${recommendation.thinking}`,\n\t\t`Reason: ${recommendation.reason}`,\n\t\t\"\",\n\t\t\"Apply for this session:\",\n\t\t\"/subagents-watchdog session model recommended\",\n\t\t\"\",\n\t\t\"Save as your user default:\",\n\t\t\"/subagents-watchdog model recommended\",\n\t].join(\"\\n\");\n}\n\nfunction buildCheckText(runtime: MainWatchdogRuntime, ctx: ExtensionCommandContext): string {\n\tconst snapshot = runtime.getSnapshot(ctx.cwd);\n\tif (!snapshot.configOk) {\n\t\treturn [\n\t\t\t\"Subagent watchdog config check\",\n\t\t\t\"\",\n\t\t\t\"Config errors:\",\n\t\t\t...snapshot.errors.map((error) => `- ${error.message}`),\n\t\t].join(\"\\n\");\n\t}\n\tconst lines = [\"Subagent watchdog config check\", \"\", \"Config: ok\"];\n\tif (snapshot.config.main.model) {\n\t\tconst resolved = resolveWatchdogModelInput(ctx as ExtensionContext, snapshot.config.main.model);\n\t\tlines.push(`Main model: ${resolved.model} auth ok`);\n\t} else {\n\t\tlines.push(`Main model: ${currentSessionModelLine(ctx as ExtensionContext)}`);\n\t}\n\tlines.push(`Main thinking: ${mainThinkingLine(snapshot, ctx as ExtensionContext)}`);\n\tlines.push(lspLine(snapshot));\n\ttry {\n\t\tconst recommendation = recommendStrongWatchdogModel(ctx as ExtensionContext);\n\t\tlines.push(`Recommended strong watchdog: ${recommendation.model}:${recommendation.thinking}`);\n\t} catch (error) {\n\t\tlines.push(`Recommended strong watchdog: unavailable (${messageFromError(error)})`);\n\t}\n\treturn lines.join(\"\\n\");\n}\n\nfunction createTestWarning(severity: \"concern\" | \"blocker\", text: string): WatchdogWarning {\n\treturn {\n\t\tseverity,\n\t\tcategory: \"other\",\n\t\tconfidence: \"high\",\n\t\tsource: \"main\",\n\t\tstate: \"displayed\",\n\t\tsummary: text,\n\t\tevidence: `Manual /subagents-watchdog test ${severity} message from the main session.`,\n\t\trecommendedAction:\n\t\t\tseverity === \"blocker\"\n\t\t\t\t? \"Verify the renderer, transcript delivery, and auto-follow policy.\"\n\t\t\t\t: \"Verify the renderer and transcript delivery; decide manually whether any action is needed.\",\n\t};\n}\n\nasync function handleWatchdogCommand(\n\tpi: ExtensionAPI,\n\truntime: MainWatchdogRuntime,\n\targs: string,\n\tctx: ExtensionCommandContext,\n): Promise<void> {\n\tconst input = args.trim();\n\tif (!input || input === \"status\") {\n\t\tsendSlashText(pi, buildWatchdogStatus(runtime.getSnapshot(ctx.cwd), ctx));\n\t\treturn;\n\t}\n\tif (input === \"recommend-model\") {\n\t\ttry {\n\t\t\tsendSlashText(pi, buildRecommendationText(ctx));\n\t\t} catch (error) {\n\t\t\tsendSlashText(pi, `Subagent watchdog recommended model\\n\\n${messageFromError(error)}`);\n\t\t}\n\t\treturn;\n\t}\n\tif (input === \"check\") {\n\t\ttry {\n\t\t\tsendSlashText(pi, buildCheckText(runtime, ctx));\n\t\t} catch (error) {\n\t\t\tsendSlashText(pi, `Subagent watchdog config check\\n\\n${messageFromError(error)}`);\n\t\t}\n\t\treturn;\n\t}\n\tif (input === \"on\" || input === \"off\") {\n\t\tconst enabled = input === \"on\";\n\t\ttry {\n\t\t\tconst settingsPath = writeUserWatchdogEnabled(enabled);\n\t\t\tconst snapshot = runtime.getSnapshot(ctx.cwd);\n\t\t\tsendSlashText(\n\t\t\t\tpi,\n\t\t\t\t[\n\t\t\t\t\t`Subagent watchdog ${boolLabel(enabled)} saved to user settings.`,\n\t\t\t\t\t`Updated: ${settingsPath}`,\n\t\t\t\t\t`Main now: ${boolLabel(snapshot.enabled)}${snapshot.sessionOverride !== undefined ? ` (session override ${boolLabel(snapshot.sessionOverride)})` : \"\"}`,\n\t\t\t\t].join(\"\\n\"),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tsendSlashText(\n\t\t\t\tpi,\n\t\t\t\t`Subagent watchdog\\n\\nCould not update ${getWatchdogUserSettingsPath()}: ${messageFromError(error)}`,\n\t\t\t);\n\t\t}\n\t\treturn;\n\t}\n\tif (input === \"session on\" || input === \"session off\") {\n\t\tconst enabled = input.endsWith(\"on\");\n\t\tconst snapshot = runtime.setSessionEnabled(enabled, ctx.cwd);\n\t\tsendSlashText(\n\t\t\tpi,\n\t\t\t[\n\t\t\t\t`Subagent watchdog session override: ${boolLabel(enabled)}.`,\n\t\t\t\t\"No settings files were changed.\",\n\t\t\t\t\"\",\n\t\t\t\tbuildWatchdogStatus(snapshot, ctx),\n\t\t\t].join(\"\\n\"),\n\t\t);\n\t\treturn;\n\t}\n\tif (input.startsWith(\"session model \")) {\n\t\tconst rawModel = input.slice(\"session model \".length);\n\t\ttry {\n\t\t\tconst value = resolveModelCommandValue(ctx, rawModel);\n\t\t\tconst snapshot =\n\t\t\t\tvalue.model === null\n\t\t\t\t\t? runtime.clearSessionModel(ctx.cwd)\n\t\t\t\t\t: runtime.setSessionModel({ model: value.model, thinking: value.thinking ?? null }, ctx.cwd);\n\t\t\tsendSlashText(\n\t\t\t\tpi,\n\t\t\t\t[\n\t\t\t\t\t`Subagent watchdog session model: ${value.description}.`,\n\t\t\t\t\t\"No settings files were changed.\",\n\t\t\t\t\t\"\",\n\t\t\t\t\tbuildWatchdogStatus(snapshot, ctx),\n\t\t\t\t].join(\"\\n\"),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tsendSlashText(pi, `Subagent watchdog session model\\n\\n${messageFromError(error)}`);\n\t\t}\n\t\treturn;\n\t}\n\tif (input.startsWith(\"model \")) {\n\t\tconst rawModel = input.slice(\"model \".length);\n\t\ttry {\n\t\t\tconst value = resolveModelCommandValue(ctx, rawModel);\n\t\t\tconst settingsPath = writeWatchdogModelSettings({\n\t\t\t\tscope: \"user\",\n\t\t\t\ttarget: { kind: \"main\" },\n\t\t\t\tmodel: value.model,\n\t\t\t\tthinking: value.thinking,\n\t\t\t});\n\t\t\truntime.refreshConfig(ctx.cwd);\n\t\t\tconst snapshot = runtime.getSnapshot(ctx.cwd);\n\t\t\tsendSlashText(\n\t\t\t\tpi,\n\t\t\t\t[\n\t\t\t\t\t`Subagent watchdog model saved: ${value.description}.`,\n\t\t\t\t\t`Updated: ${settingsPath}`,\n\t\t\t\t\t`Main now: ${boolLabel(snapshot.enabled)}`,\n\t\t\t\t\tvalue.model === null\n\t\t\t\t\t\t? \"The watchdog now inherits the current session model and thinking.\"\n\t\t\t\t\t\t: \"Run /subagents-watchdog on if the watchdog is still off.\",\n\t\t\t\t\t\"\",\n\t\t\t\t\tbuildWatchdogStatus(snapshot, ctx),\n\t\t\t\t].join(\"\\n\"),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tsendSlashText(pi, `Subagent watchdog model\\n\\n${messageFromError(error)}\\nNo settings files were changed.`);\n\t\t}\n\t\treturn;\n\t}\n\tif (input.startsWith(\"thinking \")) {\n\t\tconst rawThinking = input.slice(\"thinking \".length);\n\t\ttry {\n\t\t\tconst thinking = parseThinkingCommand(rawThinking);\n\t\t\tconst settingsPath = writeWatchdogModelSettings({\n\t\t\t\tscope: \"user\",\n\t\t\t\ttarget: { kind: \"main\" },\n\t\t\t\tthinking,\n\t\t\t});\n\t\t\truntime.refreshConfig(ctx.cwd);\n\t\t\tsendSlashText(\n\t\t\t\tpi,\n\t\t\t\t[\n\t\t\t\t\t`Subagent watchdog thinking saved: ${formatThinking(thinking ?? undefined)}.`,\n\t\t\t\t\t`Updated: ${settingsPath}`,\n\t\t\t\t\t\"\",\n\t\t\t\t\tbuildWatchdogStatus(runtime.getSnapshot(ctx.cwd), ctx),\n\t\t\t\t].join(\"\\n\"),\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tsendSlashText(pi, `Subagent watchdog thinking\\n\\n${messageFromError(error)}\\nNo settings files were changed.`);\n\t\t}\n\t\treturn;\n\t}\n\tconst test = parseTestCommand(input);\n\tif (test) {\n\t\tif (!test.text) {\n\t\t\tctx.ui.notify(\"Usage: /subagents-watchdog test concern|blocker <text>\", \"error\");\n\t\t\treturn;\n\t\t}\n\t\tconst warning = createTestWarning(test.severity, test.text);\n\t\tconst details = runtime.recordDisplayedWarning(warning);\n\t\tpi.sendMessage(createWatchdogWarningMessage(details, { display: true, details }));\n\t\treturn;\n\t}\n\tctx.ui.notify(\n\t\t`Usage: /subagents-watchdog [status|on|off|session on|session off|recommend-model|model recommended|model <provider/model[:thinking]>|model inherit|thinking ${THINKING_LEVELS.join(\"|\")}|thinking inherit|session model recommended|check|test concern <text>|test blocker <text>]`,\n\t\t\"error\",\n\t);\n}\n\nexport function registerMainWatchdog(pi: ExtensionAPI, options: RegisterMainWatchdogOptions = {}): MainWatchdogRuntime {\n\tlet currentContext: ExtensionContext | undefined;\n\tconst rememberContext = (ctx: ExtensionContext) => {\n\t\tcurrentContext = ctx;\n\t};\n\tconst runtime =\n\t\toptions.runtime ??\n\t\tnew MainWatchdogRuntime({\n\t\t\treview:\n\t\t\t\toptions.review ??\n\t\t\t\tcreateMainWatchdogReview(() => currentContext, { getThinkingLevel: () => pi.getThinkingLevel() }),\n\t\t\treviewDescription: options.review ? \"injected seam\" : \"real model review\",\n\t\t\treviewChangesOnly: true,\n\t\t\tdisplayWarning: (details, delivery) => {\n\t\t\t\tpi.sendMessage(\n\t\t\t\t\tcreateWatchdogWarningMessage(details, { display: true, details }),\n\t\t\t\t\tdelivery?.deliverAs === \"steer\" ? { deliverAs: \"steer\" } : undefined,\n\t\t\t\t);\n\t\t\t},\n\t\t\tsendUserMessage: (message) => pi.sendUserMessage(message),\n\t\t});\n\n\tpi.registerMessageRenderer<WatchdogWarningDetails>(\n\t\tSUBAGENT_WATCHDOG_WARNING_TYPE,\n\t\t(message, renderOptions, theme) => {\n\t\t\tconst details = message.details as WatchdogWarningDetails | undefined;\n\t\t\tif (!details?.summary || !details.evidence || !details.recommendedAction) {\n\t\t\t\tconst content =\n\t\t\t\t\ttypeof message.content === \"string\"\n\t\t\t\t\t\t? message.content\n\t\t\t\t\t\t: message.content\n\t\t\t\t\t\t\t\t.filter((entry) => entry.type === \"text\")\n\t\t\t\t\t\t\t\t.map((entry) => entry.text)\n\t\t\t\t\t\t\t\t.join(\"\\n\");\n\t\t\t\treturn new Text(content, 0, 0);\n\t\t\t}\n\t\t\treturn renderWatchdogWarning(details, renderOptions, theme);\n\t\t},\n\t);\n\n\tpi.registerCommand(\"subagents-watchdog\", {\n\t\tdescription: \"Show or toggle the default-off subagent watchdog\",\n\t\thandler: (args, ctx) => {\n\t\t\trememberContext(ctx);\n\t\t\treturn handleWatchdogCommand(pi, runtime, args, ctx);\n\t\t},\n\t});\n\n\tpi.on(\"session_start\", (_event, ctx) => {\n\t\trememberContext(ctx);\n\t\truntime.bindSession(ctx);\n\t});\n\tpi.on(\"before_agent_start\", (event, ctx) => {\n\t\trememberContext(ctx);\n\t\truntime.handleBeforeAgentStart(event, ctx);\n\t});\n\tpi.on(\"turn_end\", (event, ctx) => {\n\t\trememberContext(ctx);\n\t\truntime.handleTurnEnd(event, ctx);\n\t});\n\tpi.on(\"tool_result\", (_event, ctx) => {\n\t\trememberContext(ctx);\n\t\truntime.handleToolResult(ctx);\n\t});\n\tpi.on(\"agent_end\", (event, ctx) => {\n\t\trememberContext(ctx);\n\t\treturn runtime.handleAgentEnd(event, ctx);\n\t});\n\tpi.on(\"session_before_switch\", () =>\n\t\truntime.reset(\"session switch\", {\n\t\t\tclearReviewInputSignature: true,\n\t\t\tclearLspLedger: true,\n\t\t\tclearScope: true,\n\t\t\tresetAutoFollow: true,\n\t\t}),\n\t);\n\tpi.on(\"session_before_fork\", () =>\n\t\truntime.reset(\"session fork\", {\n\t\t\tclearReviewInputSignature: true,\n\t\t\tclearLspLedger: true,\n\t\t\tclearScope: true,\n\t\t\tresetAutoFollow: true,\n\t\t}),\n\t);\n\tpi.on(\"session_compact\", () => runtime.reset(\"session compact\", { clearScope: true }));\n\tpi.on(\"session_shutdown\", () => {\n\t\tcurrentContext = undefined;\n\t\truntime.dispose();\n\t});\n\n\treturn runtime;\n}\n"]}