diff --git a/packages/pi-coding-agent/src/modes/interactive/controllers/chat-controller.ts b/packages/pi-coding-agent/src/modes/interactive/controllers/chat-controller.ts new file mode 100644 index 000000000..f1ec8dd6e --- /dev/null +++ b/packages/pi-coding-agent/src/modes/interactive/controllers/chat-controller.ts @@ -0,0 +1,302 @@ +import { Loader, Spacer, Text } from "@gsd/pi-tui"; + +import type { InteractiveModeEvent, InteractiveModeStateHost } from "../interactive-mode-state.js"; +import { theme } from "../theme/theme.js"; +import { AssistantMessageComponent } from "../components/assistant-message.js"; +import { ToolExecutionComponent } from "../components/tool-execution.js"; +import { appKey } from "../components/keybinding-hints.js"; + +export async function handleAgentEvent(host: InteractiveModeStateHost & { + init: () => Promise; + getMarkdownThemeWithSettings: () => any; + addMessageToChat: (message: any, options?: any) => void; + formatWebSearchResult: (content: unknown) => string; + getRegisteredToolDefinition: (toolName: string) => any; + checkShutdownRequested: () => Promise; + rebuildChatFromMessages: () => void; + flushCompactionQueue: (options?: { willRetry?: boolean }) => Promise; + showStatus: (message: string) => void; + showError: (message: string) => void; + updatePendingMessagesDisplay: () => void; +}, event: InteractiveModeEvent): Promise { + if (!host.isInitialized) { + await host.init(); + } + + host.footer.invalidate(); + + switch (event.type) { + case "agent_start": + if (host.retryEscapeHandler) { + host.defaultEditor.onEscape = host.retryEscapeHandler; + host.retryEscapeHandler = undefined; + } + if (host.retryLoader) { + host.retryLoader.stop(); + host.retryLoader = undefined; + } + if (host.loadingAnimation) { + host.loadingAnimation.stop(); + } + host.statusContainer.clear(); + host.loadingAnimation = new Loader( + host.ui, + (spinner) => theme.fg("accent", spinner), + (text) => theme.fg("muted", text), + host.defaultWorkingMessage, + ); + host.statusContainer.addChild(host.loadingAnimation); + if (host.pendingWorkingMessage !== undefined) { + if (host.pendingWorkingMessage) { + host.loadingAnimation.setMessage(host.pendingWorkingMessage); + } + host.pendingWorkingMessage = undefined; + } + host.ui.requestRender(); + break; + + case "message_start": + if (event.message.role === "custom") { + host.addMessageToChat(event.message); + host.ui.requestRender(); + } else if (event.message.role === "user") { + host.addMessageToChat(event.message); + host.updatePendingMessagesDisplay(); + host.ui.requestRender(); + } else if (event.message.role === "assistant") { + host.streamingComponent = new AssistantMessageComponent( + undefined, + host.hideThinkingBlock, + host.getMarkdownThemeWithSettings(), + ); + host.streamingMessage = event.message; + host.chatContainer.addChild(host.streamingComponent); + host.streamingComponent.updateContent(host.streamingMessage); + host.ui.requestRender(); + } + break; + + case "message_update": + if (host.streamingComponent && event.message.role === "assistant") { + host.streamingMessage = event.message; + host.streamingComponent.updateContent(host.streamingMessage); + for (const content of host.streamingMessage.content) { + if (content.type === "toolCall") { + if (!host.pendingTools.has(content.id)) { + const component = new ToolExecutionComponent( + content.name, + content.arguments, + { showImages: host.settingsManager.getShowImages() }, + host.getRegisteredToolDefinition(content.name), + host.ui, + ); + component.setExpanded(host.toolOutputExpanded); + host.chatContainer.addChild(component); + host.pendingTools.set(content.id, component); + } else { + host.pendingTools.get(content.id)?.updateArgs(content.arguments); + } + } else if (content.type === "serverToolUse") { + if (!host.pendingTools.has(content.id)) { + const component = new ToolExecutionComponent( + content.name, + content.input ?? {}, + { showImages: host.settingsManager.getShowImages() }, + undefined, + host.ui, + ); + component.setExpanded(host.toolOutputExpanded); + host.chatContainer.addChild(component); + host.pendingTools.set(content.id, component); + } + } else if (content.type === "webSearchResult") { + const component = host.pendingTools.get(content.toolUseId); + if (component) { + const searchContent = content.content; + const isError = searchContent && typeof searchContent === "object" && "type" in (searchContent as any) && (searchContent as any).type === "web_search_tool_result_error"; + component.updateResult({ + content: [{ type: "text", text: host.formatWebSearchResult(searchContent) }], + isError: !!isError, + }); + host.pendingTools.delete(content.toolUseId); + } + } + } + host.ui.requestRender(); + } + break; + + case "message_end": + if (event.message.role === "user") break; + if (host.streamingComponent && event.message.role === "assistant") { + host.streamingMessage = event.message; + let errorMessage: string | undefined; + if (host.streamingMessage.stopReason === "aborted") { + const retryAttempt = host.session.retryAttempt; + errorMessage = retryAttempt > 0 + ? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}` + : "Operation aborted"; + host.streamingMessage.errorMessage = errorMessage; + } + host.streamingComponent.updateContent(host.streamingMessage); + if (host.streamingMessage.stopReason === "aborted" || host.streamingMessage.stopReason === "error") { + if (!errorMessage) { + errorMessage = host.streamingMessage.errorMessage || "Error"; + } + for (const [, component] of host.pendingTools.entries()) { + component.updateResult({ content: [{ type: "text", text: errorMessage }], isError: true }); + } + host.pendingTools.clear(); + } else { + for (const [, component] of host.pendingTools.entries()) { + component.setArgsComplete(); + } + } + host.streamingComponent = undefined; + host.streamingMessage = undefined; + host.footer.invalidate(); + } + host.ui.requestRender(); + break; + + case "tool_execution_start": + if (!host.pendingTools.has(event.toolCallId)) { + const component = new ToolExecutionComponent( + event.toolName, + event.args, + { showImages: host.settingsManager.getShowImages() }, + host.getRegisteredToolDefinition(event.toolName), + host.ui, + ); + component.setExpanded(host.toolOutputExpanded); + host.chatContainer.addChild(component); + host.pendingTools.set(event.toolCallId, component); + host.ui.requestRender(); + } + break; + + case "tool_execution_update": { + const component = host.pendingTools.get(event.toolCallId); + if (component) { + component.updateResult({ ...event.partialResult, isError: false }, true); + host.ui.requestRender(); + } + break; + } + + case "tool_execution_end": { + const component = host.pendingTools.get(event.toolCallId); + if (component) { + component.updateResult({ ...event.result, isError: event.isError }); + host.pendingTools.delete(event.toolCallId); + host.ui.requestRender(); + } + break; + } + + case "agent_end": + if (host.loadingAnimation) { + host.loadingAnimation.stop(); + host.loadingAnimation = undefined; + host.statusContainer.clear(); + } + if (host.streamingComponent) { + host.chatContainer.removeChild(host.streamingComponent); + host.streamingComponent = undefined; + host.streamingMessage = undefined; + } + host.pendingTools.clear(); + await host.checkShutdownRequested(); + host.ui.requestRender(); + break; + + case "auto_compaction_start": + host.autoCompactionEscapeHandler = host.defaultEditor.onEscape; + host.defaultEditor.onEscape = () => host.session.abortCompaction(); + host.statusContainer.clear(); + host.autoCompactionLoader = new Loader( + host.ui, + (spinner) => theme.fg("accent", spinner), + (text) => theme.fg("muted", text), + `${event.reason === "overflow" ? "Context overflow detected, " : ""}Auto-compacting... (${appKey(host.keybindings, "interrupt")} to cancel)`, + ); + host.statusContainer.addChild(host.autoCompactionLoader); + host.ui.requestRender(); + break; + + case "auto_compaction_end": + if (host.autoCompactionEscapeHandler) { + host.defaultEditor.onEscape = host.autoCompactionEscapeHandler; + host.autoCompactionEscapeHandler = undefined; + } + if (host.autoCompactionLoader) { + host.autoCompactionLoader.stop(); + host.autoCompactionLoader = undefined; + host.statusContainer.clear(); + } + if (event.aborted) { + host.showStatus("Auto-compaction cancelled"); + } else if (event.result) { + host.chatContainer.clear(); + host.rebuildChatFromMessages(); + host.addMessageToChat({ + role: "compactionSummary", + tokensBefore: event.result.tokensBefore, + summary: event.result.summary, + timestamp: Date.now(), + }); + host.footer.invalidate(); + } else if (event.errorMessage) { + host.chatContainer.addChild(new Spacer(1)); + host.chatContainer.addChild(new Text(theme.fg("error", event.errorMessage), 1, 0)); + } + void host.flushCompactionQueue({ willRetry: event.willRetry }); + host.ui.requestRender(); + break; + + case "auto_retry_start": + host.retryEscapeHandler = host.defaultEditor.onEscape; + host.defaultEditor.onEscape = () => host.session.abortRetry(); + host.statusContainer.clear(); + host.retryLoader = new Loader( + host.ui, + (spinner) => theme.fg("warning", spinner), + (text) => theme.fg("muted", text), + `Retrying (${event.attempt}/${event.maxAttempts}) in ${Math.round(event.delayMs / 1000)}s... (${appKey(host.keybindings, "interrupt")} to cancel)`, + ); + host.statusContainer.addChild(host.retryLoader); + host.ui.requestRender(); + break; + + case "auto_retry_end": + if (host.retryEscapeHandler) { + host.defaultEditor.onEscape = host.retryEscapeHandler; + host.retryEscapeHandler = undefined; + } + if (host.retryLoader) { + host.retryLoader.stop(); + host.retryLoader = undefined; + host.statusContainer.clear(); + } + if (!event.success) { + host.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || "Unknown error"}`); + } + host.ui.requestRender(); + break; + + case "fallback_provider_switch": + host.showStatus(`Switched from ${event.from} → ${event.to} (${event.reason})`); + host.ui.requestRender(); + break; + + case "fallback_provider_restored": + host.showStatus(`Restored to ${event.provider}`); + host.ui.requestRender(); + break; + + case "fallback_chain_exhausted": + host.showError(event.reason); + host.ui.requestRender(); + break; + } +} diff --git a/packages/pi-coding-agent/src/modes/interactive/controllers/extension-ui-controller.ts b/packages/pi-coding-agent/src/modes/interactive/controllers/extension-ui-controller.ts new file mode 100644 index 000000000..c4ce6c527 --- /dev/null +++ b/packages/pi-coding-agent/src/modes/interactive/controllers/extension-ui-controller.ts @@ -0,0 +1,59 @@ +import type { ExtensionUIContext } from "../../../core/extensions/index.js"; + +import { Theme, getAvailableThemesWithPaths, getThemeByName, setTheme, setThemeInstance, theme } from "../theme/theme.js"; +import { appKey } from "../components/keybinding-hints.js"; + +export function createExtensionUIContext(host: any): ExtensionUIContext { + return { + select: (title, options, opts) => host.showExtensionSelector(title, options, opts), + confirm: (title, message, opts) => host.showExtensionConfirm(title, message, opts), + input: (title, placeholder, opts) => host.showExtensionInput(title, placeholder, opts), + notify: (message, type) => host.showExtensionNotify(message, type), + onTerminalInput: (handler) => host.addExtensionTerminalInputListener(handler), + setStatus: (key, text) => host.setExtensionStatus(key, text), + setWorkingMessage: (message) => { + if (host.loadingAnimation) { + if (message) { + host.loadingAnimation.setMessage(message); + } else { + host.loadingAnimation.setMessage(`${host.defaultWorkingMessage} (${appKey(host.keybindings, "interrupt")} to interrupt)`); + } + } else { + host.pendingWorkingMessage = message; + } + }, + setWidget: (key, content, options) => host.setExtensionWidget(key, content, options), + setFooter: (factory) => host.setExtensionFooter(factory), + setHeader: (factory) => host.setExtensionHeader(factory), + setTitle: (title) => host.ui.terminal.setTitle(title), + custom: (factory, options) => host.showExtensionCustom(factory, options), + pasteToEditor: (text) => host.editor.handleInput(`\x1b[200~${text}\x1b[201~`), + setEditorText: (text) => host.editor.setText(text), + getEditorText: () => host.editor.getText(), + editor: (title, prefill) => host.showExtensionEditor(title, prefill), + setEditorComponent: (factory) => host.setCustomEditorComponent(factory), + get theme() { + return theme; + }, + getAllThemes: () => getAvailableThemesWithPaths(), + getTheme: (name) => getThemeByName(name), + setTheme: (themeOrName) => { + if (themeOrName instanceof Theme) { + setThemeInstance(themeOrName); + host.ui.requestRender(); + return { success: true }; + } + const result = setTheme(themeOrName, true); + if (result.success) { + if (host.settingsManager.getTheme() !== themeOrName) { + host.settingsManager.setTheme(themeOrName); + } + host.ui.requestRender(); + } + return result; + }, + getToolsExpanded: () => host.toolOutputExpanded, + setToolsExpanded: (expanded) => host.setToolsExpanded(expanded), + }; +} + diff --git a/packages/pi-coding-agent/src/modes/interactive/controllers/input-controller.ts b/packages/pi-coding-agent/src/modes/interactive/controllers/input-controller.ts new file mode 100644 index 000000000..9473da995 --- /dev/null +++ b/packages/pi-coding-agent/src/modes/interactive/controllers/input-controller.ts @@ -0,0 +1,68 @@ +import { dispatchSlashCommand } from "../slash-command-handlers.js"; +import type { InteractiveModeStateHost } from "../interactive-mode-state.js"; + +export function setupEditorSubmitHandler(host: InteractiveModeStateHost & { + getSlashCommandContext: () => any; + handleBashCommand: (command: string, excludeFromContext?: boolean) => Promise; + showWarning: (message: string) => void; + updateEditorBorderColor: () => void; + isExtensionCommand: (text: string) => boolean; + queueCompactionMessage: (text: string, mode: "steer" | "followUp") => void; + updatePendingMessagesDisplay: () => void; + flushPendingBashComponents: () => void; +}): void { + host.defaultEditor.onSubmit = async (text: string) => { + text = text.trim(); + if (!text) return; + + if (text.startsWith("/")) { + const handled = await dispatchSlashCommand(text, host.getSlashCommandContext()); + if (handled) { + host.editor.setText(""); + return; + } + } + + if (text.startsWith("!")) { + const isExcluded = text.startsWith("!!"); + const command = isExcluded ? text.slice(2).trim() : text.slice(1).trim(); + if (command) { + if (host.session.isBashRunning) { + host.showWarning("A bash command is already running. Press Esc to cancel it first."); + host.editor.setText(text); + return; + } + host.editor.addToHistory?.(text); + await host.handleBashCommand(command, isExcluded); + host.isBashMode = false; + host.updateEditorBorderColor(); + return; + } + } + + if (host.session.isCompacting) { + if (host.isExtensionCommand(text)) { + host.editor.addToHistory?.(text); + host.editor.setText(""); + await host.session.prompt(text); + } else { + host.queueCompactionMessage(text, "steer"); + } + return; + } + + if (host.session.isStreaming) { + host.editor.addToHistory?.(text); + host.editor.setText(""); + await host.session.prompt(text, { streamingBehavior: "steer" }); + host.updatePendingMessagesDisplay(); + host.ui.requestRender(); + return; + } + + host.flushPendingBashComponents(); + host.onInputCallback?.(text); + host.editor.addToHistory?.(text); + }; +} + diff --git a/packages/pi-coding-agent/src/modes/interactive/controllers/model-controller.ts b/packages/pi-coding-agent/src/modes/interactive/controllers/model-controller.ts new file mode 100644 index 000000000..ab6ccf6a9 --- /dev/null +++ b/packages/pi-coding-agent/src/modes/interactive/controllers/model-controller.ts @@ -0,0 +1,71 @@ +import type { Model } from "@gsd/pi-ai"; + +export async function handleModelCommand(host: any, searchTerm?: string): Promise { + if (!searchTerm) { + host.showModelSelector(); + return; + } + + const model = await findExactModelMatch(host, searchTerm); + if (model) { + try { + await host.session.setModel(model); + host.footer.invalidate(); + host.updateEditorBorderColor(); + host.showStatus(`Model: ${model.id}`); + host.checkDaxnutsEasterEgg(model); + } catch (error) { + host.showError(error instanceof Error ? error.message : String(error)); + } + return; + } + + host.showModelSelector(searchTerm); +} + +export async function findExactModelMatch(host: any, searchTerm: string): Promise | undefined> { + const term = searchTerm.trim(); + if (!term) return undefined; + + let targetProvider: string | undefined; + let targetModelId = ""; + + if (term.includes("/")) { + const parts = term.split("/", 2); + targetProvider = parts[0]?.trim().toLowerCase(); + targetModelId = parts[1]?.trim().toLowerCase() ?? ""; + } else { + targetModelId = term.toLowerCase(); + } + + if (!targetModelId) return undefined; + + const models = await getModelCandidates(host); + const exactMatches = models.filter((item) => { + const idMatch = item.id.toLowerCase() === targetModelId; + const providerMatch = !targetProvider || item.provider.toLowerCase() === targetProvider; + return idMatch && providerMatch; + }); + + return exactMatches.length === 1 ? exactMatches[0] : undefined; +} + +export async function getModelCandidates(host: any): Promise[]> { + if (host.session.scopedModels.length > 0) { + return host.session.scopedModels.map((scoped: any) => scoped.model); + } + + host.session.modelRegistry.refresh(); + try { + return await host.session.modelRegistry.getAvailable(); + } catch { + return []; + } +} + +export async function updateAvailableProviderCount(host: any): Promise { + const models = await getModelCandidates(host); + const uniqueProviders = new Set(models.map((m) => m.provider)); + host.footerDataProvider.setAvailableProviderCount(uniqueProviders.size); +} + diff --git a/packages/pi-coding-agent/src/modes/interactive/interactive-mode-state.ts b/packages/pi-coding-agent/src/modes/interactive/interactive-mode-state.ts new file mode 100644 index 000000000..cf91b00b1 --- /dev/null +++ b/packages/pi-coding-agent/src/modes/interactive/interactive-mode-state.ts @@ -0,0 +1,37 @@ +import type { AgentSessionEvent } from "../../core/agent-session.js"; + +export interface InteractiveModeStateHost { + defaultEditor: any; + editor: any; + session: any; + ui: any; + footer: any; + keybindings: any; + statusContainer: any; + chatContainer: any; + settingsManager: any; + pendingTools: Map; + toolOutputExpanded: boolean; + hideThinkingBlock: boolean; + isBashMode: boolean; + onInputCallback?: (text: string) => void; + isInitialized: boolean; + loadingAnimation?: any; + pendingWorkingMessage?: string; + defaultWorkingMessage: string; + streamingComponent?: any; + streamingMessage?: any; + retryEscapeHandler?: () => void; + retryLoader?: any; + autoCompactionLoader?: any; + autoCompactionEscapeHandler?: () => void; + compactionQueuedMessages: Array<{ text: string; mode: "steer" | "followUp" }>; + extensionSelector?: any; + extensionInput?: any; + extensionEditor?: any; + editorContainer: any; + keybindingsManager?: any; +} + +export type InteractiveModeEvent = AgentSessionEvent; + diff --git a/packages/pi-coding-agent/src/modes/interactive/interactive-mode.ts b/packages/pi-coding-agent/src/modes/interactive/interactive-mode.ts index 162b25913..beacaebe1 100644 --- a/packages/pi-coding-agent/src/modes/interactive/interactive-mode.ts +++ b/packages/pi-coding-agent/src/modes/interactive/interactive-mode.ts @@ -89,6 +89,15 @@ import { TreeSelectorComponent } from "./components/tree-selector.js"; import { UserMessageComponent } from "./components/user-message.js"; import { UserMessageSelectorComponent } from "./components/user-message-selector.js"; import { type SlashCommandContext, dispatchSlashCommand, getAppKeyDisplay } from "./slash-command-handlers.js"; +import { handleAgentEvent } from "./controllers/chat-controller.js"; +import { createExtensionUIContext as buildExtensionUIContext } from "./controllers/extension-ui-controller.js"; +import { setupEditorSubmitHandler as setupEditorSubmitHandlerController } from "./controllers/input-controller.js"; +import { + findExactModelMatch as findExactModelMatchController, + getModelCandidates as getModelCandidatesController, + handleModelCommand as handleModelCommandController, + updateAvailableProviderCount as updateAvailableProviderCountController, +} from "./controllers/model-controller.js"; import { getAvailableThemes, getAvailableThemesWithPaths, @@ -1486,60 +1495,7 @@ export class InteractiveMode { * Create the ExtensionUIContext for extensions. */ private createExtensionUIContext(): ExtensionUIContext { - return { - select: (title, options, opts) => this.showExtensionSelector(title, options, opts), - confirm: (title, message, opts) => this.showExtensionConfirm(title, message, opts), - input: (title, placeholder, opts) => this.showExtensionInput(title, placeholder, opts), - notify: (message, type) => this.showExtensionNotify(message, type), - onTerminalInput: (handler) => this.addExtensionTerminalInputListener(handler), - setStatus: (key, text) => this.setExtensionStatus(key, text), - setWorkingMessage: (message) => { - if (this.loadingAnimation) { - if (message) { - this.loadingAnimation.setMessage(message); - } else { - this.loadingAnimation.setMessage( - `${this.defaultWorkingMessage} (${appKey(this.keybindings, "interrupt")} to interrupt)`, - ); - } - } else { - // Queue message for when loadingAnimation is created (handles agent_start race) - this.pendingWorkingMessage = message; - } - }, - setWidget: (key, content, options) => this.setExtensionWidget(key, content, options), - setFooter: (factory) => this.setExtensionFooter(factory), - setHeader: (factory) => this.setExtensionHeader(factory), - setTitle: (title) => this.ui.terminal.setTitle(title), - custom: (factory, options) => this.showExtensionCustom(factory, options), - pasteToEditor: (text) => this.editor.handleInput(`\x1b[200~${text}\x1b[201~`), - setEditorText: (text) => this.editor.setText(text), - getEditorText: () => this.editor.getText(), - editor: (title, prefill) => this.showExtensionEditor(title, prefill), - setEditorComponent: (factory) => this.setCustomEditorComponent(factory), - get theme() { - return theme; - }, - getAllThemes: () => getAvailableThemesWithPaths(), - getTheme: (name) => getThemeByName(name), - setTheme: (themeOrName) => { - if (themeOrName instanceof Theme) { - setThemeInstance(themeOrName); - this.ui.requestRender(); - return { success: true }; - } - const result = setTheme(themeOrName, true); - if (result.success) { - if (this.settingsManager.getTheme() !== themeOrName) { - this.settingsManager.setTheme(themeOrName); - } - this.ui.requestRender(); - } - return result; - }, - getToolsExpanded: () => this.toolOutputExpanded, - setToolsExpanded: (expanded) => this.setToolsExpanded(expanded), - }; + return buildExtensionUIContext(this); } /** @@ -2017,69 +1973,7 @@ export class InteractiveMode { } private setupEditorSubmitHandler(): void { - this.defaultEditor.onSubmit = async (text: string) => { - text = text.trim(); - if (!text) return; - - // Handle slash commands - if (text.startsWith("/")) { - const handled = await dispatchSlashCommand(text, this.getSlashCommandContext()); - if (handled) { - this.editor.setText(""); - return; - } - } - - // Handle bash command (! for normal, !! for excluded from context) - if (text.startsWith("!")) { - const isExcluded = text.startsWith("!!"); - const command = isExcluded ? text.slice(2).trim() : text.slice(1).trim(); - if (command) { - if (this.session.isBashRunning) { - this.showWarning("A bash command is already running. Press Esc to cancel it first."); - this.editor.setText(text); - return; - } - this.editor.addToHistory?.(text); - await this.handleBashCommand(command, isExcluded); - this.isBashMode = false; - this.updateEditorBorderColor(); - return; - } - } - - // Queue input during compaction (extension commands execute immediately) - if (this.session.isCompacting) { - if (this.isExtensionCommand(text)) { - this.editor.addToHistory?.(text); - this.editor.setText(""); - await this.session.prompt(text); - } else { - this.queueCompactionMessage(text, "steer"); - } - return; - } - - // If streaming, use prompt() with steer behavior - // This handles extension commands (execute immediately), prompt template expansion, and queueing - if (this.session.isStreaming) { - this.editor.addToHistory?.(text); - this.editor.setText(""); - await this.session.prompt(text, { streamingBehavior: "steer" }); - this.updatePendingMessagesDisplay(); - this.ui.requestRender(); - return; - } - - // Normal message submission - // First, move any pending bash components to chat - this.flushPendingBashComponents(); - - if (this.onInputCallback) { - this.onInputCallback(text); - } - this.editor.addToHistory?.(text); - }; + setupEditorSubmitHandlerController(this as any); } private subscribeToAgent(): void { @@ -2089,338 +1983,7 @@ export class InteractiveMode { } private async handleEvent(event: AgentSessionEvent): Promise { - if (!this.isInitialized) { - await this.init(); - } - - this.footer.invalidate(); - - switch (event.type) { - case "agent_start": - // Restore main escape handler if retry handler is still active - // (retry success event fires later, but we need main handler now) - if (this.retryEscapeHandler) { - this.defaultEditor.onEscape = this.retryEscapeHandler; - this.retryEscapeHandler = undefined; - } - if (this.retryLoader) { - this.retryLoader.stop(); - this.retryLoader = undefined; - } - if (this.loadingAnimation) { - this.loadingAnimation.stop(); - } - this.statusContainer.clear(); - this.loadingAnimation = new Loader( - this.ui, - (spinner) => theme.fg("accent", spinner), - (text) => theme.fg("muted", text), - this.defaultWorkingMessage, - ); - this.statusContainer.addChild(this.loadingAnimation); - // Apply any pending working message queued before loader existed - if (this.pendingWorkingMessage !== undefined) { - if (this.pendingWorkingMessage) { - this.loadingAnimation.setMessage(this.pendingWorkingMessage); - } - this.pendingWorkingMessage = undefined; - } - this.ui.requestRender(); - break; - - case "message_start": - if (event.message.role === "custom") { - this.addMessageToChat(event.message); - this.ui.requestRender(); - } else if (event.message.role === "user") { - this.addMessageToChat(event.message); - this.updatePendingMessagesDisplay(); - this.ui.requestRender(); - } else if (event.message.role === "assistant") { - this.streamingComponent = new AssistantMessageComponent( - undefined, - this.hideThinkingBlock, - this.getMarkdownThemeWithSettings(), - ); - this.streamingMessage = event.message; - this.chatContainer.addChild(this.streamingComponent); - this.streamingComponent.updateContent(this.streamingMessage); - this.ui.requestRender(); - } - break; - - case "message_update": - if (this.streamingComponent && event.message.role === "assistant") { - this.streamingMessage = event.message; - this.streamingComponent.updateContent(this.streamingMessage); - - for (const content of this.streamingMessage.content) { - if (content.type === "toolCall") { - if (!this.pendingTools.has(content.id)) { - const component = new ToolExecutionComponent( - content.name, - content.arguments, - { - showImages: this.settingsManager.getShowImages(), - }, - this.getRegisteredToolDefinition(content.name), - this.ui, - ); - component.setExpanded(this.toolOutputExpanded); - this.chatContainer.addChild(component); - this.pendingTools.set(content.id, component); - } else { - const component = this.pendingTools.get(content.id); - if (component) { - component.updateArgs(content.arguments); - } - } - } else if (content.type === "serverToolUse") { - // Server-side tool (e.g., native web search) — show as pending tool execution - if (!this.pendingTools.has(content.id)) { - const component = new ToolExecutionComponent( - content.name, - content.input ?? {}, - { - showImages: this.settingsManager.getShowImages(), - }, - undefined, - this.ui, - ); - component.setExpanded(this.toolOutputExpanded); - this.chatContainer.addChild(component); - this.pendingTools.set(content.id, component); - } - } else if (content.type === "webSearchResult") { - // Server-side tool result — resolve the pending server tool execution - const component = this.pendingTools.get(content.toolUseId); - if (component) { - const searchContent = content.content; - const isError = searchContent && typeof searchContent === "object" && "type" in (searchContent as any) && (searchContent as any).type === "web_search_tool_result_error"; - const resultText = this.formatWebSearchResult(searchContent); - component.updateResult({ - content: [{ type: "text", text: resultText }], - isError: !!isError, - }); - this.pendingTools.delete(content.toolUseId); - } - } - } - this.ui.requestRender(); - } - break; - - case "message_end": - if (event.message.role === "user") break; - if (this.streamingComponent && event.message.role === "assistant") { - this.streamingMessage = event.message; - let errorMessage: string | undefined; - if (this.streamingMessage.stopReason === "aborted") { - const retryAttempt = this.session.retryAttempt; - errorMessage = - retryAttempt > 0 - ? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? "s" : ""}` - : "Operation aborted"; - this.streamingMessage.errorMessage = errorMessage; - } - this.streamingComponent.updateContent(this.streamingMessage); - - if (this.streamingMessage.stopReason === "aborted" || this.streamingMessage.stopReason === "error") { - if (!errorMessage) { - errorMessage = this.streamingMessage.errorMessage || "Error"; - } - for (const [, component] of this.pendingTools.entries()) { - component.updateResult({ - content: [{ type: "text", text: errorMessage }], - isError: true, - }); - } - this.pendingTools.clear(); - } else { - // Args are now complete - trigger diff computation for edit tools - for (const [, component] of this.pendingTools.entries()) { - component.setArgsComplete(); - } - } - this.streamingComponent = undefined; - this.streamingMessage = undefined; - this.footer.invalidate(); - } - this.ui.requestRender(); - break; - - case "tool_execution_start": { - if (!this.pendingTools.has(event.toolCallId)) { - const component = new ToolExecutionComponent( - event.toolName, - event.args, - { - showImages: this.settingsManager.getShowImages(), - }, - this.getRegisteredToolDefinition(event.toolName), - this.ui, - ); - component.setExpanded(this.toolOutputExpanded); - this.chatContainer.addChild(component); - this.pendingTools.set(event.toolCallId, component); - this.ui.requestRender(); - } - break; - } - - case "tool_execution_update": { - const component = this.pendingTools.get(event.toolCallId); - if (component) { - component.updateResult({ ...event.partialResult, isError: false }, true); - this.ui.requestRender(); - } - break; - } - - case "tool_execution_end": { - const component = this.pendingTools.get(event.toolCallId); - if (component) { - component.updateResult({ ...event.result, isError: event.isError }); - this.pendingTools.delete(event.toolCallId); - this.ui.requestRender(); - } - break; - } - - case "agent_end": - if (this.loadingAnimation) { - this.loadingAnimation.stop(); - this.loadingAnimation = undefined; - this.statusContainer.clear(); - } - if (this.streamingComponent) { - this.chatContainer.removeChild(this.streamingComponent); - this.streamingComponent = undefined; - this.streamingMessage = undefined; - } - this.pendingTools.clear(); - - await this.checkShutdownRequested(); - - this.ui.requestRender(); - break; - - case "auto_compaction_start": { - // Keep editor active; submissions are queued during compaction. - // Set up escape to abort auto-compaction - this.autoCompactionEscapeHandler = this.defaultEditor.onEscape; - this.defaultEditor.onEscape = () => { - this.session.abortCompaction(); - }; - // Show compacting indicator with reason - this.statusContainer.clear(); - const reasonText = event.reason === "overflow" ? "Context overflow detected, " : ""; - this.autoCompactionLoader = new Loader( - this.ui, - (spinner) => theme.fg("accent", spinner), - (text) => theme.fg("muted", text), - `${reasonText}Auto-compacting... (${appKey(this.keybindings, "interrupt")} to cancel)`, - ); - this.statusContainer.addChild(this.autoCompactionLoader); - this.ui.requestRender(); - break; - } - - case "auto_compaction_end": { - // Restore escape handler - if (this.autoCompactionEscapeHandler) { - this.defaultEditor.onEscape = this.autoCompactionEscapeHandler; - this.autoCompactionEscapeHandler = undefined; - } - // Stop loader - if (this.autoCompactionLoader) { - this.autoCompactionLoader.stop(); - this.autoCompactionLoader = undefined; - this.statusContainer.clear(); - } - // Handle result - if (event.aborted) { - this.showStatus("Auto-compaction cancelled"); - } else if (event.result) { - // Rebuild chat to show compacted state - this.chatContainer.clear(); - this.rebuildChatFromMessages(); - // Add compaction component at bottom so user sees it without scrolling - this.addMessageToChat({ - role: "compactionSummary", - tokensBefore: event.result.tokensBefore, - summary: event.result.summary, - timestamp: Date.now(), - }); - this.footer.invalidate(); - } else if (event.errorMessage) { - // Compaction failed (e.g., quota exceeded, API error) - this.chatContainer.addChild(new Spacer(1)); - this.chatContainer.addChild(new Text(theme.fg("error", event.errorMessage), 1, 0)); - } - void this.flushCompactionQueue({ willRetry: event.willRetry }); - this.ui.requestRender(); - break; - } - - case "auto_retry_start": { - // Set up escape to abort retry - this.retryEscapeHandler = this.defaultEditor.onEscape; - this.defaultEditor.onEscape = () => { - this.session.abortRetry(); - }; - // Show retry indicator - this.statusContainer.clear(); - const delaySeconds = Math.round(event.delayMs / 1000); - this.retryLoader = new Loader( - this.ui, - (spinner) => theme.fg("warning", spinner), - (text) => theme.fg("muted", text), - `Retrying (${event.attempt}/${event.maxAttempts}) in ${delaySeconds}s... (${appKey(this.keybindings, "interrupt")} to cancel)`, - ); - this.statusContainer.addChild(this.retryLoader); - this.ui.requestRender(); - break; - } - - case "auto_retry_end": { - // Restore escape handler - if (this.retryEscapeHandler) { - this.defaultEditor.onEscape = this.retryEscapeHandler; - this.retryEscapeHandler = undefined; - } - // Stop loader - if (this.retryLoader) { - this.retryLoader.stop(); - this.retryLoader = undefined; - this.statusContainer.clear(); - } - // Show error only on final failure (success shows normal response) - if (!event.success) { - this.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || "Unknown error"}`); - } - this.ui.requestRender(); - break; - } - - case "fallback_provider_switch": { - this.showStatus(`Switched from ${event.from} → ${event.to} (${event.reason})`); - this.ui.requestRender(); - break; - } - - case "fallback_provider_restored": { - this.showStatus(`Restored to ${event.provider}`); - this.ui.requestRender(); - break; - } - - case "fallback_chain_exhausted": { - this.showError(event.reason); - this.ui.requestRender(); - break; - } - } + await handleAgentEvent(this as any, event); } /** Extract text content from a user message */ @@ -3299,73 +2862,20 @@ export class InteractiveMode { } private async handleModelCommand(searchTerm?: string): Promise { - if (!searchTerm) { - this.showModelSelector(); - return; - } - - const model = await this.findExactModelMatch(searchTerm); - if (model) { - try { - await this.session.setModel(model); - this.footer.invalidate(); - this.updateEditorBorderColor(); - this.showStatus(`Model: ${model.id}`); - this.checkDaxnutsEasterEgg(model); - } catch (error) { - this.showError(error instanceof Error ? error.message : String(error)); - } - return; - } - - this.showModelSelector(searchTerm); + await handleModelCommandController(this, searchTerm); } private async findExactModelMatch(searchTerm: string): Promise | undefined> { - const term = searchTerm.trim(); - if (!term) return undefined; - - let targetProvider: string | undefined; - let targetModelId = ""; - - if (term.includes("/")) { - const parts = term.split("/", 2); - targetProvider = parts[0]?.trim().toLowerCase(); - targetModelId = parts[1]?.trim().toLowerCase() ?? ""; - } else { - targetModelId = term.toLowerCase(); - } - - if (!targetModelId) return undefined; - - const models = await this.getModelCandidates(); - const exactMatches = models.filter((item) => { - const idMatch = item.id.toLowerCase() === targetModelId; - const providerMatch = !targetProvider || item.provider.toLowerCase() === targetProvider; - return idMatch && providerMatch; - }); - - return exactMatches.length === 1 ? exactMatches[0] : undefined; + return findExactModelMatchController(this, searchTerm); } private async getModelCandidates(): Promise[]> { - if (this.session.scopedModels.length > 0) { - return this.session.scopedModels.map((scoped) => scoped.model); - } - - this.session.modelRegistry.refresh(); - try { - return await this.session.modelRegistry.getAvailable(); - } catch { - return []; - } + return getModelCandidatesController(this); } /** Update the footer's available provider count from current model candidates */ private async updateAvailableProviderCount(): Promise { - const models = await this.getModelCandidates(); - const uniqueProviders = new Set(models.map((m) => m.provider)); - this.footerDataProvider.setAvailableProviderCount(uniqueProviders.size); + await updateAvailableProviderCountController(this); } private showModelSelector(initialSearchInput?: string): void { diff --git a/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts b/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts new file mode 100644 index 000000000..e9c018101 --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts @@ -0,0 +1,142 @@ +import type { ExtensionAPI, ExtensionContext } from "@gsd/pi-coding-agent"; + +import { checkAutoStartAfterDiscuss } from "../guided-flow.js"; +import { getAutoDashboardData, getAutoModeStartModel, isAutoActive, pauseAuto } from "../auto.js"; +import { getNextFallbackModel, isTransientNetworkError, resolveModelWithFallbacksForUnit } from "../preferences.js"; +import { classifyProviderError, pauseAutoForProviderError } from "../provider-error-pause.js"; +import { isSessionSwitchInFlight, resolveAgentEnd } from "../auto-loop.js"; +import { clearDiscussionFlowState } from "./write-gate.js"; + +const networkRetryCounters = new Map(); +const MAX_TRANSIENT_AUTO_RESUMES = 3; +let consecutiveTransientErrors = 0; + +export async function handleAgentEnd( + pi: ExtensionAPI, + event: { messages: any[] }, + ctx: ExtensionContext, +): Promise { + if (checkAutoStartAfterDiscuss()) { + clearDiscussionFlowState(); + return; + } + if (!isAutoActive()) return; + if (isSessionSwitchInFlight()) return; + + const lastMsg = event.messages[event.messages.length - 1]; + if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "aborted") { + await pauseAuto(ctx, pi); + return; + } + if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "error") { + const errorDetail = "errorMessage" in lastMsg && lastMsg.errorMessage ? `: ${lastMsg.errorMessage}` : ""; + const errorMsg = ("errorMessage" in lastMsg && lastMsg.errorMessage) ? String(lastMsg.errorMessage) : ""; + + if (isTransientNetworkError(errorMsg)) { + const currentModelId = ctx.model?.id ?? "unknown"; + const retryKey = `network-retry:${currentModelId}`; + const currentRetries = networkRetryCounters.get(retryKey) ?? 0; + const maxRetries = 2; + if (currentRetries < maxRetries) { + networkRetryCounters.set(retryKey, currentRetries + 1); + const attempt = currentRetries + 1; + const delayMs = attempt * 3000; + ctx.ui.notify(`Network error on ${currentModelId}${errorDetail}. Retry ${attempt}/${maxRetries} in ${delayMs / 1000}s...`, "warning"); + setTimeout(() => { + pi.sendMessage( + { customType: "gsd-auto-timeout-recovery", content: "Continue execution — retrying after transient network error.", display: false }, + { triggerTurn: true }, + ); + }, delayMs); + return; + } + networkRetryCounters.delete(retryKey); + ctx.ui.notify(`Network retries exhausted for ${currentModelId}. Attempting model fallback.`, "warning"); + } + + const dash = getAutoDashboardData(); + if (dash.currentUnit) { + const modelConfig = resolveModelWithFallbacksForUnit(dash.currentUnit.type); + if (modelConfig && modelConfig.fallbacks.length > 0) { + const availableModels = ctx.modelRegistry.getAvailable(); + const nextModelId = getNextFallbackModel(ctx.model?.id, modelConfig); + if (nextModelId) { + networkRetryCounters.clear(); + const slashIdx = nextModelId.indexOf("/"); + const modelToSet = slashIdx !== -1 + ? availableModels.find((m) => m.provider.toLowerCase() === nextModelId.substring(0, slashIdx).toLowerCase() && m.id.toLowerCase() === nextModelId.substring(slashIdx + 1).toLowerCase()) + : (availableModels.find((m) => m.id === nextModelId && m.provider === ctx.model?.provider) ?? availableModels.find((m) => m.id === nextModelId)); + if (modelToSet) { + const ok = await pi.setModel(modelToSet, { persist: false }); + if (ok) { + ctx.ui.notify(`Model error${errorDetail}. Switched to fallback: ${nextModelId} and resuming.`, "warning"); + pi.sendMessage({ customType: "gsd-auto-timeout-recovery", content: "Continue execution.", display: false }, { triggerTurn: true }); + return; + } + } + } + } + } + + const sessionModel = getAutoModeStartModel(); + if (sessionModel) { + if (ctx.model?.id !== sessionModel.id || ctx.model?.provider !== sessionModel.provider) { + const startModel = ctx.modelRegistry.getAvailable().find((m) => m.provider === sessionModel.provider && m.id === sessionModel.id); + if (startModel) { + const ok = await pi.setModel(startModel, { persist: false }); + if (ok) { + networkRetryCounters.clear(); + ctx.ui.notify(`Model error${errorDetail}. Restored session model: ${sessionModel.provider}/${sessionModel.id} and resuming.`, "warning"); + pi.sendMessage({ customType: "gsd-auto-timeout-recovery", content: "Continue execution.", display: false }, { triggerTurn: true }); + return; + } + } + } + } + + const classification = classifyProviderError(errorMsg); + const explicitRetryAfterMs = ("retryAfterMs" in lastMsg && typeof lastMsg.retryAfterMs === "number") ? lastMsg.retryAfterMs : undefined; + if (classification.isTransient) { + consecutiveTransientErrors += 1; + } else { + consecutiveTransientErrors = 0; + } + const baseRetryAfterMs = explicitRetryAfterMs ?? classification.suggestedDelayMs; + const retryAfterMs = classification.isTransient + ? baseRetryAfterMs * 2 ** Math.max(0, consecutiveTransientErrors - 1) + : baseRetryAfterMs; + const allowAutoResume = classification.isTransient && consecutiveTransientErrors <= MAX_TRANSIENT_AUTO_RESUMES; + if (classification.isTransient && !allowAutoResume) { + ctx.ui.notify(`Transient provider errors persisted after ${MAX_TRANSIENT_AUTO_RESUMES} auto-resume attempts. Pausing for manual review.`, "warning"); + } + await pauseAutoForProviderError(ctx.ui, errorDetail, () => pauseAuto(ctx, pi), { + isRateLimit: classification.isRateLimit, + isTransient: allowAutoResume, + retryAfterMs, + resume: allowAutoResume + ? () => { + pi.sendMessage( + { customType: "gsd-auto-timeout-recovery", content: "Continue execution — provider error recovery delay elapsed.", display: false }, + { triggerTurn: true }, + ); + } + : undefined, + }); + return; + } + + try { + consecutiveTransientErrors = 0; + networkRetryCounters.clear(); + resolveAgentEnd(event); + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + ctx.ui.notify(`Auto-mode error in agent_end handler: ${message}. Stopping auto-mode.`, "error"); + try { + await pauseAuto(ctx, pi); + } catch { + // best-effort + } + } +} + diff --git a/src/resources/extensions/gsd/bootstrap/db-tools.ts b/src/resources/extensions/gsd/bootstrap/db-tools.ts new file mode 100644 index 000000000..2e55ff6cc --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/db-tools.ts @@ -0,0 +1,238 @@ +import { Type } from "@sinclair/typebox"; +import type { ExtensionAPI } from "@gsd/pi-coding-agent"; + +import { findMilestoneIds, nextMilestoneId } from "../guided-flow.js"; +import { loadEffectiveGSDPreferences } from "../preferences.js"; +import { ensureDbOpen } from "./dynamic-tools.js"; + +export function registerDbTools(pi: ExtensionAPI): void { + pi.registerTool({ + name: "gsd_save_decision", + label: "Save Decision", + description: + "Record a project decision to the GSD database and regenerate DECISIONS.md. " + + "Decision IDs are auto-assigned — never provide an ID manually.", + promptSnippet: "Record a project decision to the GSD database (auto-assigns ID, regenerates DECISIONS.md)", + promptGuidelines: [ + "Use gsd_save_decision when recording an architectural, pattern, library, or observability decision.", + "Decision IDs are auto-assigned (D001, D002, ...) — never guess or provide an ID.", + "All fields except revisable and when_context are required.", + "The tool writes to the DB and regenerates .gsd/DECISIONS.md automatically.", + ], + parameters: Type.Object({ + scope: Type.String({ description: "Scope of the decision (e.g. 'architecture', 'library', 'observability')" }), + decision: Type.String({ description: "What is being decided" }), + choice: Type.String({ description: "The choice made" }), + rationale: Type.String({ description: "Why this choice was made" }), + revisable: Type.Optional(Type.String({ description: "Whether this can be revisited (default: 'Yes')" })), + when_context: Type.Optional(Type.String({ description: "When/context for the decision (e.g. milestone ID)" })), + }), + async execute(_toolCallId, params, _signal, _onUpdate, _ctx) { + const dbAvailable = await ensureDbOpen(); + if (!dbAvailable) { + return { + content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot save decision." }], + details: { operation: "save_decision", error: "db_unavailable" } as any, + }; + } + try { + const { saveDecisionToDb } = await import("../db-writer.js"); + const { id } = await saveDecisionToDb( + { + scope: params.scope, + decision: params.decision, + choice: params.choice, + rationale: params.rationale, + revisable: params.revisable, + when_context: params.when_context, + }, + process.cwd(), + ); + return { + content: [{ type: "text" as const, text: `Saved decision ${id}` }], + details: { operation: "save_decision", id } as any, + }; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + process.stderr.write(`gsd-db: gsd_save_decision tool failed: ${msg}\n`); + return { + content: [{ type: "text" as const, text: `Error saving decision: ${msg}` }], + details: { operation: "save_decision", error: msg } as any, + }; + } + }, + }); + + pi.registerTool({ + name: "gsd_update_requirement", + label: "Update Requirement", + description: + "Update an existing requirement in the GSD database and regenerate REQUIREMENTS.md. " + + "Provide the requirement ID (e.g. R001) and any fields to update.", + promptSnippet: "Update an existing GSD requirement by ID (regenerates REQUIREMENTS.md)", + promptGuidelines: [ + "Use gsd_update_requirement to change status, validation, notes, or other fields on an existing requirement.", + "The id parameter is required — it must be an existing RXXX identifier.", + "All other fields are optional — only provided fields are updated.", + "The tool verifies the requirement exists before updating.", + ], + parameters: Type.Object({ + id: Type.String({ description: "The requirement ID (e.g. R001, R014)" }), + status: Type.Optional(Type.String({ description: "New status (e.g. 'active', 'validated', 'deferred')" })), + validation: Type.Optional(Type.String({ description: "Validation criteria or proof" })), + notes: Type.Optional(Type.String({ description: "Additional notes" })), + description: Type.Optional(Type.String({ description: "Updated description" })), + primary_owner: Type.Optional(Type.String({ description: "Primary owning slice" })), + supporting_slices: Type.Optional(Type.String({ description: "Supporting slices" })), + }), + async execute(_toolCallId, params, _signal, _onUpdate, _ctx) { + const dbAvailable = await ensureDbOpen(); + if (!dbAvailable) { + return { + content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot update requirement." }], + details: { operation: "update_requirement", id: params.id, error: "db_unavailable" } as any, + }; + } + try { + const db = await import("../gsd-db.js"); + const existing = db.getRequirementById(params.id); + if (!existing) { + return { + content: [{ type: "text" as const, text: `Error: Requirement ${params.id} not found.` }], + details: { operation: "update_requirement", id: params.id, error: "not_found" } as any, + }; + } + const { updateRequirementInDb } = await import("../db-writer.js"); + const updates: Record = {}; + if (params.status !== undefined) updates.status = params.status; + if (params.validation !== undefined) updates.validation = params.validation; + if (params.notes !== undefined) updates.notes = params.notes; + if (params.description !== undefined) updates.description = params.description; + if (params.primary_owner !== undefined) updates.primary_owner = params.primary_owner; + if (params.supporting_slices !== undefined) updates.supporting_slices = params.supporting_slices; + await updateRequirementInDb(params.id, updates, process.cwd()); + return { + content: [{ type: "text" as const, text: `Updated requirement ${params.id}` }], + details: { operation: "update_requirement", id: params.id } as any, + }; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + process.stderr.write(`gsd-db: gsd_update_requirement tool failed: ${msg}\n`); + return { + content: [{ type: "text" as const, text: `Error updating requirement: ${msg}` }], + details: { operation: "update_requirement", id: params.id, error: msg } as any, + }; + } + }, + }); + + pi.registerTool({ + name: "gsd_save_summary", + label: "Save Summary", + description: + "Save a summary, research, context, or assessment artifact to the GSD database and write it to disk. " + + "Computes the file path from milestone/slice/task IDs automatically.", + promptSnippet: "Save a GSD artifact (summary/research/context/assessment) to DB and disk", + promptGuidelines: [ + "Use gsd_save_summary to persist structured artifacts (SUMMARY, RESEARCH, CONTEXT, ASSESSMENT).", + "milestone_id is required. slice_id and task_id are optional — they determine the file path.", + "The tool computes the relative path automatically: milestones/M001/M001-SUMMARY.md, milestones/M001/slices/S01/S01-SUMMARY.md, etc.", + "artifact_type must be one of: SUMMARY, RESEARCH, CONTEXT, ASSESSMENT.", + ], + parameters: Type.Object({ + milestone_id: Type.String({ description: "Milestone ID (e.g. M001)" }), + slice_id: Type.Optional(Type.String({ description: "Slice ID (e.g. S01)" })), + task_id: Type.Optional(Type.String({ description: "Task ID (e.g. T01)" })), + artifact_type: Type.String({ description: "One of: SUMMARY, RESEARCH, CONTEXT, ASSESSMENT" }), + content: Type.String({ description: "The full markdown content of the artifact" }), + }), + async execute(_toolCallId, params, _signal, _onUpdate, _ctx) { + const dbAvailable = await ensureDbOpen(); + if (!dbAvailable) { + return { + content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot save artifact." }], + details: { operation: "save_summary", error: "db_unavailable" } as any, + }; + } + const validTypes = ["SUMMARY", "RESEARCH", "CONTEXT", "ASSESSMENT"]; + if (!validTypes.includes(params.artifact_type)) { + return { + content: [{ type: "text" as const, text: `Error: Invalid artifact_type "${params.artifact_type}". Must be one of: ${validTypes.join(", ")}` }], + details: { operation: "save_summary", error: "invalid_artifact_type" } as any, + }; + } + try { + let relativePath: string; + if (params.task_id && params.slice_id) { + relativePath = `milestones/${params.milestone_id}/slices/${params.slice_id}/tasks/${params.task_id}-${params.artifact_type}.md`; + } else if (params.slice_id) { + relativePath = `milestones/${params.milestone_id}/slices/${params.slice_id}/${params.slice_id}-${params.artifact_type}.md`; + } else { + relativePath = `milestones/${params.milestone_id}/${params.milestone_id}-${params.artifact_type}.md`; + } + const { saveArtifactToDb } = await import("../db-writer.js"); + await saveArtifactToDb( + { + path: relativePath, + artifact_type: params.artifact_type, + content: params.content, + milestone_id: params.milestone_id, + slice_id: params.slice_id, + task_id: params.task_id, + }, + process.cwd(), + ); + return { + content: [{ type: "text" as const, text: `Saved ${params.artifact_type} artifact to ${relativePath}` }], + details: { operation: "save_summary", path: relativePath, artifact_type: params.artifact_type } as any, + }; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + process.stderr.write(`gsd-db: gsd_save_summary tool failed: ${msg}\n`); + return { + content: [{ type: "text" as const, text: `Error saving artifact: ${msg}` }], + details: { operation: "save_summary", error: msg } as any, + }; + } + }, + }); + + const reservedMilestoneIds = new Set(); + pi.registerTool({ + name: "gsd_generate_milestone_id", + label: "Generate Milestone ID", + description: + "Generate the next milestone ID for a new GSD milestone. " + + "Scans existing milestones on disk and respects the unique_milestone_ids preference. " + + "Always use this tool when creating a new milestone — never invent milestone IDs manually.", + promptSnippet: "Generate a valid milestone ID (respects unique_milestone_ids preference)", + promptGuidelines: [ + "ALWAYS call gsd_generate_milestone_id before creating a new milestone directory or writing milestone files.", + "Never invent or hardcode milestone IDs like M001, M002 — always use this tool.", + "Call it once per milestone you need to create. For multi-milestone projects, call it once for each milestone in sequence.", + "The tool returns the correct format based on project preferences (e.g. M001 or M001-r5jzab).", + ], + parameters: Type.Object({}), + async execute(_toolCallId, _params, _signal, _onUpdate, _ctx) { + try { + const basePath = process.cwd(); + const existingIds = findMilestoneIds(basePath); + const uniqueEnabled = !!loadEffectiveGSDPreferences()?.preferences?.unique_milestone_ids; + const allIds = [...new Set([...existingIds, ...reservedMilestoneIds])]; + const newId = nextMilestoneId(allIds, uniqueEnabled); + reservedMilestoneIds.add(newId); + return { + content: [{ type: "text" as const, text: newId }], + details: { operation: "generate_milestone_id", id: newId, existingCount: existingIds.length, reservedCount: reservedMilestoneIds.size, uniqueEnabled } as any, + }; + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + return { + content: [{ type: "text" as const, text: `Error generating milestone ID: ${msg}` }], + details: { operation: "generate_milestone_id", error: msg } as any, + }; + } + }, + }); +} + diff --git a/src/resources/extensions/gsd/bootstrap/dynamic-tools.ts b/src/resources/extensions/gsd/bootstrap/dynamic-tools.ts new file mode 100644 index 000000000..3dab4b47a --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/dynamic-tools.ts @@ -0,0 +1,90 @@ +import { existsSync } from "node:fs"; +import { join } from "node:path"; + +import type { ExtensionAPI } from "@gsd/pi-coding-agent"; +import { createBashTool, createEditTool, createReadTool, createWriteTool } from "@gsd/pi-coding-agent"; + +import { DEFAULT_BASH_TIMEOUT_SECS } from "../constants.js"; + +export async function ensureDbOpen(): Promise { + try { + const db = await import("../gsd-db.js"); + if (db.isDbAvailable()) return true; + const dbPath = join(process.cwd(), ".gsd", "gsd.db"); + if (existsSync(dbPath)) { + return db.openDatabase(dbPath); + } + return false; + } catch { + return false; + } +} + +export function registerDynamicTools(pi: ExtensionAPI): void { + const baseBash = createBashTool(process.cwd(), { + spawnHook: (ctx) => ({ ...ctx, cwd: process.cwd() }), + }); + const dynamicBash = { + ...baseBash, + execute: async ( + toolCallId: string, + params: { command: string; timeout?: number }, + signal?: AbortSignal, + onUpdate?: unknown, + ctx?: unknown, + ) => { + const paramsWithTimeout = { + ...params, + timeout: params.timeout ?? DEFAULT_BASH_TIMEOUT_SECS, + }; + return (baseBash as any).execute(toolCallId, paramsWithTimeout, signal, onUpdate, ctx); + }, + }; + pi.registerTool(dynamicBash as any); + + const baseWrite = createWriteTool(process.cwd()); + pi.registerTool({ + ...baseWrite, + execute: async ( + toolCallId: string, + params: { path: string; content: string }, + signal?: AbortSignal, + onUpdate?: unknown, + ctx?: unknown, + ) => { + const fresh = createWriteTool(process.cwd()); + return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx); + }, + } as any); + + const baseRead = createReadTool(process.cwd()); + pi.registerTool({ + ...baseRead, + execute: async ( + toolCallId: string, + params: { path: string; offset?: number; limit?: number }, + signal?: AbortSignal, + onUpdate?: unknown, + ctx?: unknown, + ) => { + const fresh = createReadTool(process.cwd()); + return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx); + }, + } as any); + + const baseEdit = createEditTool(process.cwd()); + pi.registerTool({ + ...baseEdit, + execute: async ( + toolCallId: string, + params: { path: string; oldText: string; newText: string }, + signal?: AbortSignal, + onUpdate?: unknown, + ctx?: unknown, + ) => { + const fresh = createEditTool(process.cwd()); + return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx); + }, + } as any); +} + diff --git a/src/resources/extensions/gsd/bootstrap/register-extension.ts b/src/resources/extensions/gsd/bootstrap/register-extension.ts new file mode 100644 index 000000000..0f5b5ea42 --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/register-extension.ts @@ -0,0 +1,46 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { registerGSDCommand } from "../commands.js"; +import { registerExitCommand } from "../exit-command.js"; +import { registerWorktreeCommand } from "../worktree-command.js"; +import { registerDbTools } from "./db-tools.js"; +import { registerDynamicTools } from "./dynamic-tools.js"; +import { registerHooks } from "./register-hooks.js"; +import { registerShortcuts } from "./register-shortcuts.js"; + +function installEpipeGuard(): void { + if (!process.listeners("uncaughtException").some((listener) => listener.name === "_gsdEpipeGuard")) { + const _gsdEpipeGuard = (err: Error): void => { + if ((err as NodeJS.ErrnoException).code === "EPIPE") { + process.exit(0); + } + if ((err as NodeJS.ErrnoException).code === "ENOENT" && (err as any).syscall?.startsWith("spawn")) { + process.stderr.write(`[gsd] spawn ENOENT: ${(err as any).path ?? "unknown"} — command not found\n`); + return; + } + throw err; + }; + process.on("uncaughtException", _gsdEpipeGuard); + } +} + +export function registerGsdExtension(pi: ExtensionAPI): void { + registerGSDCommand(pi); + registerWorktreeCommand(pi); + registerExitCommand(pi); + + installEpipeGuard(); + + pi.registerCommand("kill", { + description: "Exit GSD immediately (no cleanup)", + handler: async (_args: string, _ctx: ExtensionCommandContext) => { + process.exit(0); + }, + }); + + registerDynamicTools(pi); + registerDbTools(pi); + registerShortcuts(pi); + registerHooks(pi); +} + diff --git a/src/resources/extensions/gsd/bootstrap/register-hooks.ts b/src/resources/extensions/gsd/bootstrap/register-hooks.ts new file mode 100644 index 000000000..dc2632fbd --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/register-hooks.ts @@ -0,0 +1,167 @@ +import { join } from "node:path"; + +import type { ExtensionAPI, ExtensionContext } from "@gsd/pi-coding-agent"; +import { isToolCallEventType } from "@gsd/pi-coding-agent"; + +import { buildMilestoneFileName, resolveMilestonePath, resolveSliceFile, resolveSlicePath } from "../paths.js"; +import { buildBeforeAgentStartResult } from "./system-context.js"; +import { handleAgentEnd } from "./agent-end-recovery.js"; +import { clearDiscussionFlowState, isDepthVerified, isQueuePhaseActive, markDepthVerified, resetWriteGateState, shouldBlockContextWrite } from "./write-gate.js"; +import { getDiscussionMilestoneId } from "../guided-flow.js"; +import { loadToolApiKeys } from "../commands-config.js"; +import { loadFile, saveFile, formatContinue } from "../files.js"; +import { deriveState } from "../state.js"; +import { getAutoDashboardData, isAutoActive, isAutoPaused, markToolEnd, markToolStart } from "../auto.js"; +import { isParallelActive, shutdownParallel } from "../parallel-orchestrator.js"; +import { saveActivityLog } from "../activity-log.js"; +import { maybeRenderGsdHeader } from "./register-shortcuts.js"; + +export function registerHooks(pi: ExtensionAPI): void { + pi.on("session_start", async (_event, ctx) => { + resetWriteGateState(); + maybeRenderGsdHeader(ctx); + loadToolApiKeys(); + try { + const [{ getRemoteConfigStatus }, { getLatestPromptSummary }] = await Promise.all([ + import("../../remote-questions/config.js"), + import("../../remote-questions/status.js"), + ]); + const status = getRemoteConfigStatus(); + const latest = getLatestPromptSummary(); + if (!status.includes("not configured")) { + const suffix = latest ? `\nLast remote prompt: ${latest.id} (${latest.status})` : ""; + ctx.ui.notify(`${status}${suffix}`, status.includes("disabled") ? "warning" : "info"); + } + } catch { + // ignore + } + }); + + pi.on("before_agent_start", async (event, ctx: ExtensionContext) => { + return buildBeforeAgentStartResult(event, ctx); + }); + + pi.on("agent_end", async (event, ctx: ExtensionContext) => { + await handleAgentEnd(pi, event, ctx); + }); + + pi.on("session_before_compact", async () => { + if (isAutoActive() || isAutoPaused()) { + return { cancel: true }; + } + const basePath = process.cwd(); + const state = await deriveState(basePath); + if (!state.activeMilestone || !state.activeSlice || !state.activeTask) return; + if (state.phase !== "executing") return; + + const sliceDir = resolveSlicePath(basePath, state.activeMilestone.id, state.activeSlice.id); + if (!sliceDir) return; + + const existingFile = resolveSliceFile(basePath, state.activeMilestone.id, state.activeSlice.id, "CONTINUE"); + if (existingFile && await loadFile(existingFile)) return; + const legacyContinue = join(sliceDir, "continue.md"); + if (await loadFile(legacyContinue)) return; + + const continuePath = join(sliceDir, `${state.activeSlice.id}-CONTINUE.md`); + await saveFile(continuePath, formatContinue({ + frontmatter: { + milestone: state.activeMilestone.id, + slice: state.activeSlice.id, + task: state.activeTask.id, + step: 0, + totalSteps: 0, + status: "compacted" as const, + savedAt: new Date().toISOString(), + }, + completedWork: `Task ${state.activeTask.id} (${state.activeTask.title}) was in progress when compaction occurred.`, + remainingWork: "Check the task plan for remaining steps.", + decisions: "Check task summary files for prior decisions.", + context: "Session was auto-compacted by Pi. Resume with /gsd.", + nextAction: `Resume task ${state.activeTask.id}: ${state.activeTask.title}.`, + })); + }); + + pi.on("session_shutdown", async (_event, ctx: ExtensionContext) => { + if (isParallelActive()) { + try { + await shutdownParallel(process.cwd()); + } catch { + // best-effort + } + } + if (!isAutoActive() && !isAutoPaused()) return; + const dash = getAutoDashboardData(); + if (dash.currentUnit) { + saveActivityLog(ctx, dash.basePath, dash.currentUnit.type, dash.currentUnit.id); + } + }); + + pi.on("tool_call", async (event) => { + if (!isToolCallEventType("write", event)) return; + const result = shouldBlockContextWrite( + event.toolName, + event.input.path, + getDiscussionMilestoneId(), + isDepthVerified(), + isQueuePhaseActive(), + ); + if (result.block) return result; + }); + + pi.on("tool_result", async (event) => { + if (event.toolName !== "ask_user_questions") return; + const milestoneId = getDiscussionMilestoneId(); + if (!milestoneId) return; + + const details = event.details as any; + if (details?.cancelled || !details?.response) return; + + const questions: any[] = (event.input as any)?.questions ?? []; + for (const question of questions) { + if (typeof question.id === "string" && question.id.includes("depth_verification")) { + markDepthVerified(); + break; + } + } + + const basePath = process.cwd(); + const milestoneDir = resolveMilestonePath(basePath, milestoneId); + if (!milestoneDir) return; + + const discussionPath = join(milestoneDir, buildMilestoneFileName(milestoneId, "DISCUSSION")); + const timestamp = new Date().toISOString(); + const lines: string[] = [`## Exchange — ${timestamp}`, ""]; + for (const question of questions) { + lines.push(`### ${question.header ?? "Question"}`, "", question.question ?? ""); + if (Array.isArray(question.options)) { + lines.push(""); + for (const opt of question.options) { + lines.push(`- **${opt.label}** — ${opt.description ?? ""}`); + } + } + const answer = details.response?.answers?.[question.id]; + if (answer) { + lines.push(""); + const selected = Array.isArray(answer.selected) ? answer.selected.join(", ") : answer.selected; + lines.push(`**Selected:** ${selected}`); + if (answer.notes) { + lines.push(`**Notes:** ${answer.notes}`); + } + } + lines.push(""); + } + lines.push("---", ""); + const existing = await loadFile(discussionPath) ?? `# ${milestoneId} Discussion Log\n\n`; + await saveFile(discussionPath, existing + lines.join("\n")); + }); + + pi.on("tool_execution_start", async (event) => { + if (!isAutoActive()) return; + markToolStart(event.toolCallId); + }); + + pi.on("tool_execution_end", async (event) => { + markToolEnd(event.toolCallId); + }); +} + diff --git a/src/resources/extensions/gsd/bootstrap/register-shortcuts.ts b/src/resources/extensions/gsd/bootstrap/register-shortcuts.ts new file mode 100644 index 000000000..c04f58cb7 --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/register-shortcuts.ts @@ -0,0 +1,55 @@ +import { existsSync } from "node:fs"; +import { join } from "node:path"; + +import type { ExtensionAPI } from "@gsd/pi-coding-agent"; +import { Key, Text } from "@gsd/pi-tui"; + +import { GSDDashboardOverlay } from "../dashboard-overlay.js"; +import { shortcutDesc } from "../../shared/mod.js"; + +export const GSD_LOGO_LINES = [ + " ██████╗ ███████╗██████╗ ", + " ██╔════╝ ██╔════╝██╔══██╗", + " ██║ ███╗███████╗██║ ██║", + " ██║ ██║╚════██║██║ ██║", + " ╚██████╔╝███████║██████╔╝", + " ╚═════╝ ╚══════╝╚═════╝ ", +]; + +export function registerShortcuts(pi: ExtensionAPI): void { + pi.registerShortcut(Key.ctrlAlt("g"), { + description: shortcutDesc("Open GSD dashboard", "/gsd status"), + handler: async (ctx) => { + if (!existsSync(join(process.cwd(), ".gsd"))) { + ctx.ui.notify("No .gsd/ directory found. Run /gsd to start.", "info"); + return; + } + await ctx.ui.custom( + (tui, theme, _kb, done) => new GSDDashboardOverlay(tui, theme, () => done()), + { + overlay: true, + overlayOptions: { + width: "90%", + minWidth: 80, + maxHeight: "92%", + anchor: "center", + }, + }, + ); + }, + }); +} + +export function maybeRenderGsdHeader(ctx: { ui: any }): void { + try { + const theme = ctx.ui.theme; + const version = process.env.GSD_VERSION || "0.0.0"; + const logoText = GSD_LOGO_LINES.map((line) => theme.fg("accent", line)).join("\n"); + const titleLine = ` ${theme.bold("Get Shit Done")} ${theme.fg("dim", `v${version}`)}`; + const headerContent = `${logoText}\n${titleLine}`; + ctx.ui.setHeader((_ui: unknown, _theme: unknown) => new Text(headerContent, 1, 0)); + } catch { + // no TUI + } +} + diff --git a/src/resources/extensions/gsd/bootstrap/system-context.ts b/src/resources/extensions/gsd/bootstrap/system-context.ts new file mode 100644 index 000000000..6d4070d7f --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/system-context.ts @@ -0,0 +1,340 @@ +import { existsSync, readFileSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; + +import type { ExtensionContext } from "@gsd/pi-coding-agent"; + +import { debugTime } from "../debug-logger.js"; +import { loadPrompt } from "../prompt-loader.js"; +import { resolveAllSkillReferences, renderPreferencesForSystemPrompt, loadEffectiveGSDPreferences } from "../preferences.js"; +import { resolveGsdRootFile, resolveSliceFile, resolveSlicePath, resolveTaskFile, resolveTaskFiles, resolveTasksDir, relSliceFile, relSlicePath, relTaskFile } from "../paths.js"; +import { hasSkillSnapshot, detectNewSkills, formatSkillsXml } from "../skill-discovery.js"; +import { getActiveAutoWorktreeContext } from "../auto-worktree.js"; +import { getActiveWorktreeName, getWorktreeOriginalCwd } from "../worktree-command.js"; +import { deriveState } from "../state.js"; +import { formatOverridesSection, loadActiveOverrides, loadFile, parseContinue, parseSummary } from "../files.js"; +import { toPosixPath } from "../../shared/mod.js"; +import { markCmuxPromptShown, shouldPromptToEnableCmux } from "../../cmux/index.js"; + +const gsdHome = process.env.GSD_HOME || join(homedir(), ".gsd"); + +function warnDeprecatedAgentInstructions(): void { + const paths = [ + join(gsdHome, "agent-instructions.md"), + join(process.cwd(), ".gsd", "agent-instructions.md"), + ]; + for (const path of paths) { + if (existsSync(path)) { + console.warn( + `[GSD] DEPRECATED: ${path} is no longer loaded. ` + + `Migrate your instructions to AGENTS.md (or CLAUDE.md) in the same directory. ` + + `See https://github.com/gsd-build/GSD-2/issues/1492`, + ); + } + } +} + +export async function buildBeforeAgentStartResult( + event: { prompt: string; systemPrompt: string }, + ctx: ExtensionContext, +): Promise<{ systemPrompt: string; message?: { customType: string; content: string; display: false } } | undefined> { + if (!existsSync(join(process.cwd(), ".gsd"))) return undefined; + + const stopContextTimer = debugTime("context-inject"); + const systemContent = loadPrompt("system"); + const loadedPreferences = loadEffectiveGSDPreferences(); + if (shouldPromptToEnableCmux(loadedPreferences?.preferences)) { + markCmuxPromptShown(); + ctx.ui.notify( + "cmux detected. Run /gsd cmux on to enable sidebar metadata, notifications, and visual subagent splits for this project.", + "info", + ); + } + + let preferenceBlock = ""; + if (loadedPreferences) { + const cwd = process.cwd(); + const report = resolveAllSkillReferences(loadedPreferences.preferences, cwd); + preferenceBlock = `\n\n${renderPreferencesForSystemPrompt(loadedPreferences.preferences, report.resolutions)}`; + if (report.warnings.length > 0) { + ctx.ui.notify( + `GSD skill preferences: ${report.warnings.length} unresolved skill${report.warnings.length === 1 ? "" : "s"}: ${report.warnings.join(", ")}`, + "warning", + ); + } + } + + let knowledgeBlock = ""; + const knowledgePath = resolveGsdRootFile(process.cwd(), "KNOWLEDGE"); + if (existsSync(knowledgePath)) { + try { + const content = readFileSync(knowledgePath, "utf-8").trim(); + if (content) { + knowledgeBlock = `\n\n[PROJECT KNOWLEDGE — Rules, patterns, and lessons learned]\n\n${content}`; + } + } catch { + // skip + } + } + + let memoryBlock = ""; + try { + const { formatMemoriesForPrompt, getActiveMemoriesRanked } = await import("../memory-store.js"); + const memories = getActiveMemoriesRanked(30); + if (memories.length > 0) { + const formatted = formatMemoriesForPrompt(memories, 2000); + if (formatted) { + memoryBlock = `\n\n${formatted}`; + } + } + } catch { + // non-fatal + } + + let newSkillsBlock = ""; + if (hasSkillSnapshot()) { + const newSkills = detectNewSkills(); + if (newSkills.length > 0) { + newSkillsBlock = formatSkillsXml(newSkills); + } + } + + warnDeprecatedAgentInstructions(); + + const injection = await buildGuidedExecuteContextInjection(event.prompt, process.cwd()); + const worktreeBlock = buildWorktreeContextBlock(); + const fullSystem = `${event.systemPrompt}\n\n[SYSTEM CONTEXT — GSD]\n\n${systemContent}${preferenceBlock}${knowledgeBlock}${memoryBlock}${newSkillsBlock}${worktreeBlock}`; + + stopContextTimer({ + systemPromptSize: fullSystem.length, + injectionSize: injection?.length ?? 0, + hasPreferences: preferenceBlock.length > 0, + hasNewSkills: newSkillsBlock.length > 0, + }); + + return { + systemPrompt: fullSystem, + ...(injection + ? { + message: { + customType: "gsd-guided-context", + content: injection, + display: false as const, + }, + } + : {}), + }; +} + +function buildWorktreeContextBlock(): string { + const worktreeName = getActiveWorktreeName(); + const worktreeMainCwd = getWorktreeOriginalCwd(); + const autoWorktree = getActiveAutoWorktreeContext(); + + if (worktreeName && worktreeMainCwd) { + return [ + "", + "", + "[WORKTREE CONTEXT — OVERRIDES CURRENT WORKING DIRECTORY ABOVE]", + `IMPORTANT: Ignore the "Current working directory" shown earlier in this prompt.`, + `The actual current working directory is: ${toPosixPath(process.cwd())}`, + "", + `You are working inside a GSD worktree.`, + `- Worktree name: ${worktreeName}`, + `- Worktree path (this is the real cwd): ${toPosixPath(process.cwd())}`, + `- Main project: ${toPosixPath(worktreeMainCwd)}`, + `- Branch: worktree/${worktreeName}`, + "", + "All file operations, bash commands, and GSD state resolve against the worktree path above.", + "Use /worktree merge to merge changes back. Use /worktree return to switch back to the main tree.", + ].join("\n"); + } + + if (autoWorktree) { + return [ + "", + "", + "[WORKTREE CONTEXT — OVERRIDES CURRENT WORKING DIRECTORY ABOVE]", + `IMPORTANT: Ignore the "Current working directory" shown earlier in this prompt.`, + `The actual current working directory is: ${toPosixPath(process.cwd())}`, + "", + "You are working inside a GSD auto-worktree.", + `- Milestone worktree: ${autoWorktree.worktreeName}`, + `- Worktree path (this is the real cwd): ${toPosixPath(process.cwd())}`, + `- Main project: ${toPosixPath(autoWorktree.originalBase)}`, + `- Branch: ${autoWorktree.branch}`, + "", + "All file operations, bash commands, and GSD state resolve against the worktree path above.", + "Write every .gsd artifact in the worktree path above, never in the main project tree.", + ].join("\n"); + } + + return ""; +} + +async function buildGuidedExecuteContextInjection(prompt: string, basePath: string): Promise { + const executeMatch = prompt.match(/Execute the next task:\s+(T\d+)\s+\("([^"]+)"\)\s+in slice\s+(S\d+)\s+of milestone\s+(M\d+(?:-[a-z0-9]{6})?)/i); + if (executeMatch) { + const [, taskId, taskTitle, sliceId, milestoneId] = executeMatch; + return buildTaskExecutionContextInjection(basePath, milestoneId, sliceId, taskId, taskTitle); + } + + const resumeMatch = prompt.match(/Resume interrupted work\.[\s\S]*?slice\s+(S\d+)\s+of milestone\s+(M\d+(?:-[a-z0-9]{6})?)/i); + if (resumeMatch) { + const [, sliceId, milestoneId] = resumeMatch; + const state = await deriveState(basePath); + if (state.activeMilestone?.id === milestoneId && state.activeSlice?.id === sliceId && state.activeTask) { + return buildTaskExecutionContextInjection(basePath, milestoneId, sliceId, state.activeTask.id, state.activeTask.title); + } + } + + return null; +} + +async function buildTaskExecutionContextInjection( + basePath: string, + milestoneId: string, + sliceId: string, + taskId: string, + taskTitle: string, +): Promise { + const taskPlanPath = resolveTaskFile(basePath, milestoneId, sliceId, taskId, "PLAN"); + const taskPlanRelPath = relTaskFile(basePath, milestoneId, sliceId, taskId, "PLAN"); + const taskPlanContent = taskPlanPath ? await loadFile(taskPlanPath) : null; + const taskPlanInline = taskPlanContent + ? ["## Inlined Task Plan (authoritative local execution contract)", `Source: \`${taskPlanRelPath}\``, "", taskPlanContent.trim()].join("\n") + : ["## Inlined Task Plan (authoritative local execution contract)", `Task plan not found at dispatch time. Read \`${taskPlanRelPath}\` before executing.`].join("\n"); + + const slicePlanPath = resolveSliceFile(basePath, milestoneId, sliceId, "PLAN"); + const slicePlanRelPath = relSliceFile(basePath, milestoneId, sliceId, "PLAN"); + const slicePlanContent = slicePlanPath ? await loadFile(slicePlanPath) : null; + const slicePlanExcerpt = extractSliceExecutionExcerpt(slicePlanContent, slicePlanRelPath); + const priorTaskLines = await buildCarryForwardLines(basePath, milestoneId, sliceId, taskId); + const resumeSection = await buildResumeSection(basePath, milestoneId, sliceId); + const activeOverrides = await loadActiveOverrides(basePath); + const overridesSection = formatOverridesSection(activeOverrides); + + return [ + "[GSD Guided Execute Context]", + "Use this injected context as startup context for guided task execution. Treat the inlined task plan as the authoritative local execution contract. Use source artifacts to verify details and run checks.", + overridesSection, "", + "", + resumeSection, + "", + "## Carry-Forward Context", + ...priorTaskLines, + "", + taskPlanInline, + "", + slicePlanExcerpt, + "", + "## Backing Source Artifacts", + `- Slice plan: \`${slicePlanRelPath}\``, + `- Task plan source: \`${taskPlanRelPath}\``, + ].join("\n"); +} + +async function buildCarryForwardLines( + basePath: string, + milestoneId: string, + sliceId: string, + taskId: string, +): Promise { + const tasksDir = resolveTasksDir(basePath, milestoneId, sliceId); + if (!tasksDir) return ["- No prior task summaries in this slice."]; + + const currentNum = parseInt(taskId.replace(/^T/, ""), 10); + const sliceRel = relSlicePath(basePath, milestoneId, sliceId); + const summaryFiles = resolveTaskFiles(tasksDir, "SUMMARY") + .filter((file) => parseInt(file.replace(/^T/, ""), 10) < currentNum) + .sort(); + + if (summaryFiles.length === 0) return ["- No prior task summaries in this slice."]; + + return Promise.all(summaryFiles.map(async (file) => { + const absPath = join(tasksDir, file); + const content = await loadFile(absPath); + const relPath = `${sliceRel}/tasks/${file}`; + if (!content) return `- \`${relPath}\``; + + const summary = parseSummary(content); + const provided = summary.frontmatter.provides.slice(0, 2).join("; "); + const decisions = summary.frontmatter.key_decisions.slice(0, 2).join("; "); + const patterns = summary.frontmatter.patterns_established.slice(0, 2).join("; "); + const diagnostics = extractMarkdownSection(content, "Diagnostics"); + const parts = [summary.title || relPath]; + if (summary.oneLiner) parts.push(summary.oneLiner); + if (provided) parts.push(`provides: ${provided}`); + if (decisions) parts.push(`decisions: ${decisions}`); + if (patterns) parts.push(`patterns: ${patterns}`); + if (diagnostics) parts.push(`diagnostics: ${oneLine(diagnostics)}`); + return `- \`${relPath}\` — ${parts.join(" | ")}`; + })); +} + +async function buildResumeSection(basePath: string, milestoneId: string, sliceId: string): Promise { + const continueFile = resolveSliceFile(basePath, milestoneId, sliceId, "CONTINUE"); + const legacyDir = resolveSlicePath(basePath, milestoneId, sliceId); + const legacyPath = legacyDir ? join(legacyDir, "continue.md") : null; + const continueContent = continueFile ? await loadFile(continueFile) : null; + const legacyContent = !continueContent && legacyPath ? await loadFile(legacyPath) : null; + const resolvedContent = continueContent ?? legacyContent; + const resolvedRelPath = continueContent + ? relSliceFile(basePath, milestoneId, sliceId, "CONTINUE") + : (legacyPath ? `${relSlicePath(basePath, milestoneId, sliceId)}/continue.md` : null); + + if (!resolvedContent || !resolvedRelPath) { + return ["## Resume State", "- No continue file present. Start from the top of the task plan."].join("\n"); + } + + const cont = parseContinue(resolvedContent); + const lines = [ + "## Resume State", + `Source: \`${resolvedRelPath}\``, + `- Status: ${cont.frontmatter.status || "in_progress"}`, + ]; + if (cont.frontmatter.step && cont.frontmatter.totalSteps) { + lines.push(`- Progress: step ${cont.frontmatter.step} of ${cont.frontmatter.totalSteps}`); + } + if (cont.completedWork) lines.push(`- Completed: ${oneLine(cont.completedWork)}`); + if (cont.remainingWork) lines.push(`- Remaining: ${oneLine(cont.remainingWork)}`); + if (cont.decisions) lines.push(`- Decisions: ${oneLine(cont.decisions)}`); + if (cont.nextAction) lines.push(`- Next action: ${oneLine(cont.nextAction)}`); + return lines.join("\n"); +} + +function extractSliceExecutionExcerpt(content: string | null, relPath: string): string { + if (!content) { + return ["## Slice Plan Excerpt", `Slice plan not found at dispatch time. Read \`${relPath}\` before running slice-level verification.`].join("\n"); + } + const lines = content.split("\n"); + const goalLine = lines.find((line) => line.startsWith("**Goal:**"))?.trim(); + const demoLine = lines.find((line) => line.startsWith("**Demo:**"))?.trim(); + const verification = extractMarkdownSection(content, "Verification"); + const observability = extractMarkdownSection(content, "Observability / Diagnostics"); + const parts = ["## Slice Plan Excerpt", `Source: \`${relPath}\``]; + if (goalLine) parts.push(goalLine); + if (demoLine) parts.push(demoLine); + if (verification) parts.push("", "### Slice Verification", verification.trim()); + if (observability) parts.push("", "### Slice Observability / Diagnostics", observability.trim()); + return parts.join("\n"); +} + +function extractMarkdownSection(content: string, heading: string): string | null { + const match = new RegExp(`^## ${escapeRegExp(heading)}\\s*$`, "m").exec(content); + if (!match) return null; + const start = match.index + match[0].length; + const rest = content.slice(start); + const nextHeading = rest.match(/^##\s+/m); + const end = nextHeading?.index ?? rest.length; + return rest.slice(0, end).trim(); +} + +function escapeRegExp(value: string): string { + return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} + +function oneLine(text: string): string { + return text.replace(/\s+/g, " ").trim(); +} + diff --git a/src/resources/extensions/gsd/bootstrap/write-gate.ts b/src/resources/extensions/gsd/bootstrap/write-gate.ts new file mode 100644 index 000000000..75a964021 --- /dev/null +++ b/src/resources/extensions/gsd/bootstrap/write-gate.ts @@ -0,0 +1,51 @@ +const MILESTONE_CONTEXT_RE = /M\d+(?:-[a-z0-9]{6})?-CONTEXT\.md$/; + +let depthVerificationDone = false; +let activeQueuePhase = false; + +export function isDepthVerified(): boolean { + return depthVerificationDone; +} + +export function isQueuePhaseActive(): boolean { + return activeQueuePhase; +} + +export function setQueuePhaseActive(active: boolean): void { + activeQueuePhase = active; +} + +export function resetWriteGateState(): void { + depthVerificationDone = false; +} + +export function clearDiscussionFlowState(): void { + depthVerificationDone = false; + activeQueuePhase = false; +} + +export function markDepthVerified(): void { + depthVerificationDone = true; +} + +export function shouldBlockContextWrite( + toolName: string, + inputPath: string, + milestoneId: string | null, + depthVerified: boolean, + queuePhaseActive?: boolean, +): { block: boolean; reason?: string } { + if (toolName !== "write") return { block: false }; + + const inDiscussion = milestoneId !== null; + const inQueue = queuePhaseActive ?? false; + if (!inDiscussion && !inQueue) return { block: false }; + if (!MILESTONE_CONTEXT_RE.test(inputPath)) return { block: false }; + if (depthVerified) return { block: false }; + + return { + block: true, + reason: `Blocked: Cannot write to milestone CONTEXT.md during discussion phase without depth verification. Call ask_user_questions with question id "depth_verification" first to confirm discussion depth before writing context.`, + }; +} + diff --git a/src/resources/extensions/gsd/commands-handlers.ts b/src/resources/extensions/gsd/commands-handlers.ts index 13089df2c..e43ecb0fa 100644 --- a/src/resources/extensions/gsd/commands-handlers.ts +++ b/src/resources/extensions/gsd/commands-handlers.ts @@ -21,7 +21,7 @@ import { filterDoctorIssues, } from "./doctor.js"; import { isAutoActive } from "./auto.js"; -import { projectRoot } from "./commands.js"; +import { projectRoot } from "./commands/context.js"; import { loadPrompt } from "./prompt-loader.js"; export function dispatchDoctorHeal(pi: ExtensionAPI, scope: string | undefined, reportText: string, structuredIssues: string): void { diff --git a/src/resources/extensions/gsd/commands.ts b/src/resources/extensions/gsd/commands.ts index df72e2d4e..9d98fc068 100644 --- a/src/resources/extensions/gsd/commands.ts +++ b/src/resources/extensions/gsd/commands.ts @@ -1,1336 +1,17 @@ -/** - * GSD Command — /gsd - * - * One command, one wizard. Routes to smart entry or status. - */ - -import { importExtensionModule, type ExtensionAPI, type ExtensionCommandContext } from "@gsd/pi-coding-agent"; -import type { GSDState } from "./types.js"; -import { existsSync, readFileSync, readdirSync, unlinkSync } from "node:fs"; -import { homedir } from "node:os"; -import { join } from "node:path"; -import { gsdRoot } from "./paths.js"; - -const gsdHome = process.env.GSD_HOME || join(homedir(), ".gsd"); -import { enableDebug } from "./debug-logger.js"; -import { deriveState } from "./state.js"; -import { GSDDashboardOverlay } from "./dashboard-overlay.js"; -import { GSDVisualizerOverlay } from "./visualizer-overlay.js"; -import { showQueue, showDiscuss, showHeadlessMilestoneCreation } from "./guided-flow.js"; -import { startAuto, stopAuto, pauseAuto, isAutoActive, isAutoPaused, isStepMode, stopAutoRemote, checkRemoteAutoSession } from "./auto.js"; -import { dispatchDirectPhase } from "./auto-direct-dispatch.js"; -import { resolveProjectRoot } from "./worktree.js"; -import { assertSafeDirectory } from "./validate-directory.js"; -import { - getGlobalGSDPreferencesPath, - getProjectGSDPreferencesPath, - loadEffectiveGSDPreferences, -} from "./preferences.js"; -import { handleRemote } from "../remote-questions/mod.js"; -import { handleQuick } from "./quick.js"; -import { handleHistory } from "./history.js"; -import { handleUndo } from "./undo.js"; -import { handleExport } from "./export.js"; -import { - isParallelActive, getOrchestratorState, getWorkerStatuses, - prepareParallelStart, startParallel, stopParallel, - pauseWorker, resumeWorker, -} from "./parallel-orchestrator.js"; -import { formatEligibilityReport } from "./parallel-eligibility.js"; -import { mergeAllCompleted, mergeCompletedMilestone, formatMergeResults } from "./parallel-merge.js"; -import { resolveParallelConfig } from "./preferences.js"; - -// ─── Imports from extracted modules ────────────────────────────────────────── -import { handlePrefs, handlePrefsMode, handlePrefsWizard, ensurePreferencesFile } from "./commands-prefs-wizard.js"; -import { handleConfig } from "./commands-config.js"; -import { handleInspect } from "./commands-inspect.js"; -import { handleCleanupBranches, handleCleanupSnapshots, handleSkip, handleDryRun } from "./commands-maintenance.js"; -import { handleDoctor, handleSteer, handleCapture, handleTriage, handleKnowledge, handleRunHook, handleUpdate, handleSkillHealth } from "./commands-handlers.js"; -import { computeProgressScore, formatProgressLine } from "./progress-score.js"; -import { runEnvironmentChecks } from "./doctor-environment.js"; -import { handleLogs } from "./commands-logs.js"; -import { handleStart, handleTemplates, getTemplateCompletions } from "./commands-workflow-templates.js"; -import { readSessionLockData, isSessionLockProcessAlive } from "./session-lock.js"; -import { handleCmux } from "./commands-cmux.js"; -import { showNextAction } from "../shared/mod.js"; - - -/** Resolve the effective project root, accounting for worktree paths. */ -export function projectRoot(): string { - const cwd = process.cwd(); - const root = resolveProjectRoot(cwd); - - // When running inside a GSD worktree, the resolved root may be a "dangerous" - // directory (e.g., $HOME used as a git repo root — #1317). The safety check - // should validate the actual working directory, not the upstream root, - // because the worktree itself is a safe project subdirectory. - // Only skip the root check when we can confirm we're in a valid worktree. - if (root !== cwd) { - // We're in a worktree — validate the worktree path instead of the root - assertSafeDirectory(cwd); - } else { - assertSafeDirectory(root); - } - return root; -} - -/** - * Guard against starting auto-mode when a remote session is already running. - * Returns true if the caller should proceed with startAuto, false if handled. - */ -async function guardRemoteSession( - ctx: ExtensionCommandContext, - pi: ExtensionAPI, -): Promise { - // Local session already active — proceed (startAuto handles re-entrant calls) - if (isAutoActive() || isAutoPaused()) return true; - - const remote = checkRemoteAutoSession(projectRoot()); - if (!remote.running || !remote.pid) return true; - - const unitLabel = remote.unitType && remote.unitId - ? `${remote.unitType} (${remote.unitId})` - : "unknown unit"; - const unitsMsg = remote.completedUnits != null - ? `${remote.completedUnits} units completed` - : ""; - - const choice = await showNextAction(ctx, { - title: `Auto-mode is running in another terminal (PID ${remote.pid})`, - summary: [ - `Currently executing: ${unitLabel}`, - ...(unitsMsg ? [unitsMsg] : []), - ...(remote.startedAt ? [`Started: ${remote.startedAt}`] : []), - ], - actions: [ - { - id: "status", - label: "View status", - description: "Show the current GSD progress dashboard.", - recommended: true, - }, - { - id: "steer", - label: "Steer the session", - description: "Use /gsd steer to redirect the running session.", - }, - { - id: "stop", - label: "Stop remote session", - description: `Send SIGTERM to PID ${remote.pid} to stop it gracefully.`, - }, - { - id: "force", - label: "Force start (steal lock)", - description: "Start a new session, terminating the existing one.", - }, - ], - notYetMessage: "Run /gsd when ready.", - }); - - if (choice === "status") { - await handleStatus(ctx); - return false; - } - if (choice === "steer") { - ctx.ui.notify( - "Use /gsd steer to redirect the running auto-mode session.\n" + - "Example: /gsd steer Use Postgres instead of SQLite", - "info", - ); - return false; - } - if (choice === "stop") { - const result = stopAutoRemote(projectRoot()); - if (result.found) { - ctx.ui.notify(`Sent stop signal to auto-mode session (PID ${result.pid}). It will shut down gracefully.`, "info"); - } else if (result.error) { - ctx.ui.notify(`Failed to stop remote auto-mode: ${result.error}`, "error"); - } else { - ctx.ui.notify("Remote session is no longer running.", "info"); - } - return false; - } - if (choice === "force") { - return true; // Proceed — startAuto will steal the lock - } - - // "not_yet" or escape - return false; -} - -export function registerGSDCommand(pi: ExtensionAPI): void { - pi.registerCommand("gsd", { - description: "GSD — Get Shit Done: /gsd help|start|templates|next|auto|stop|pause|status|widget|visualize|queue|quick|capture|triage|dispatch|history|undo|rate|skip|export|cleanup|mode|prefs|config|keys|hooks|run-hook|skill-health|doctor|logs|forensics|changelog|migrate|remote|steer|knowledge|new-milestone|parallel|cmux|park|unpark|init|setup|inspect|extensions|update", - getArgumentCompletions: (prefix: string) => { - const subcommands = [ - { cmd: "help", desc: "Categorized command reference with descriptions" }, - { cmd: "next", desc: "Explicit step mode (same as /gsd)" }, - { cmd: "auto", desc: "Autonomous mode — research, plan, execute, commit, repeat" }, - { cmd: "stop", desc: "Stop auto mode gracefully" }, - { cmd: "pause", desc: "Pause auto-mode (preserves state, /gsd auto to resume)" }, - { cmd: "status", desc: "Progress dashboard" }, - { cmd: "widget", desc: "Cycle widget: full → small → min → off" }, - { cmd: "visualize", desc: "Open 10-tab workflow visualizer (progress, timeline, deps, metrics, health, agent, changes, knowledge, captures, export)" }, - { cmd: "queue", desc: "Queue and reorder future milestones" }, - { cmd: "quick", desc: "Execute a quick task without full planning overhead" }, - { cmd: "discuss", desc: "Discuss architecture and decisions" }, - { cmd: "capture", desc: "Fire-and-forget thought capture" }, - { cmd: "changelog", desc: "Show categorized release notes" }, - { cmd: "triage", desc: "Manually trigger triage of pending captures" }, - { cmd: "dispatch", desc: "Dispatch a specific phase directly" }, - { cmd: "history", desc: "View execution history" }, - { cmd: "rate", desc: "Rate last unit's model tier (over/ok/under) — improves adaptive routing" }, - { cmd: "undo", desc: "Revert last completed unit" }, - { cmd: "skip", desc: "Prevent a unit from auto-mode dispatch" }, - { cmd: "export", desc: "Export milestone/slice results" }, - { cmd: "cleanup", desc: "Remove merged branches or snapshots" }, - { cmd: "mode", desc: "Switch workflow mode (solo/team)" }, - { cmd: "prefs", desc: "Manage preferences (model selection, timeouts, etc.)" }, - { cmd: "config", desc: "Set API keys for external tools" }, - { cmd: "keys", desc: "API key manager — list, add, remove, test, rotate, doctor" }, - { cmd: "hooks", desc: "Show configured post-unit and pre-dispatch hooks" }, - { cmd: "run-hook", desc: "Manually trigger a specific hook" }, - { cmd: "skill-health", desc: "Skill lifecycle dashboard" }, - { cmd: "doctor", desc: "Runtime health checks with auto-fix" }, - { cmd: "logs", desc: "Browse activity logs, debug logs, and metrics" }, - { cmd: "forensics", desc: "Examine execution logs" }, - { cmd: "init", desc: "Project init wizard — detect, configure, bootstrap .gsd/" }, - { cmd: "setup", desc: "Global setup status and configuration" }, - { cmd: "migrate", desc: "Migrate a v1 .planning directory to .gsd format" }, - { cmd: "remote", desc: "Control remote auto-mode" }, - { cmd: "steer", desc: "Hard-steer plan documents during execution" }, - { cmd: "inspect", desc: "Show SQLite DB diagnostics" }, - { cmd: "knowledge", desc: "Add persistent project knowledge (rule, pattern, or lesson)" }, - { cmd: "new-milestone", desc: "Create a milestone from a specification document (headless)" }, - { cmd: "parallel", desc: "Parallel milestone orchestration (start, status, stop, merge)" }, - { cmd: "cmux", desc: "Manage cmux integration (status, sidebar, notifications, splits)" }, - { cmd: "park", desc: "Park a milestone — skip without deleting" }, - { cmd: "unpark", desc: "Reactivate a parked milestone" }, - { cmd: "update", desc: "Update GSD to the latest version" }, - { cmd: "start", desc: "Start a workflow template (bugfix, spike, feature, etc.)" }, - { cmd: "templates", desc: "List available workflow templates" }, - { cmd: "extensions", desc: "Manage extensions (list, enable, disable, info)" }, - ]; - const hasTrailingSpace = prefix.endsWith(" "); - const parts = prefix.trim().split(/\s+/); - if (hasTrailingSpace && parts.length >= 1) { - parts.push(""); - } - - if (parts.length <= 1) { - return subcommands - .filter((item) => item.cmd.startsWith(parts[0] ?? "")) - .map((item) => ({ - value: item.cmd, - label: item.cmd, - description: item.desc - })); - } - - if (parts[0] === "auto" && parts.length <= 2) { - const flagPrefix = parts[1] ?? ""; - const flags = [ - { flag: "--verbose", desc: "Show detailed execution output" }, - { flag: "--debug", desc: "Enable debug logging" }, - ]; - return flags - .filter((f) => f.flag.startsWith(flagPrefix)) - .map((f) => ({ value: `auto ${f.flag}`, label: f.flag, description: f.desc })); - } - - if (parts[0] === "mode" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const modes = [ - { cmd: "global", desc: "Edit global workflow mode" }, - { cmd: "project", desc: "Edit project-specific workflow mode" }, - ]; - return modes - .filter((m) => m.cmd.startsWith(subPrefix)) - .map((m) => ({ value: `mode ${m.cmd}`, label: m.cmd, description: m.desc })); - } - - if (parts[0] === "parallel" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "start", desc: "Start parallel milestone orchestration" }, - { cmd: "status", desc: "Show parallel worker statuses" }, - { cmd: "stop", desc: "Stop all parallel workers" }, - { cmd: "pause", desc: "Pause a specific worker" }, - { cmd: "resume", desc: "Resume a paused worker" }, - { cmd: "merge", desc: "Merge completed milestone branches" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `parallel ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "cmux") { - if (parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "status", desc: "Show cmux detection, prefs, and capabilities" }, - { cmd: "on", desc: "Enable cmux integration" }, - { cmd: "off", desc: "Disable cmux integration" }, - { cmd: "notifications", desc: "Toggle cmux desktop notifications" }, - { cmd: "sidebar", desc: "Toggle cmux sidebar metadata" }, - { cmd: "splits", desc: "Toggle cmux visual subagent splits" }, - { cmd: "browser", desc: "Toggle future browser integration flag" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `cmux ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts.length <= 3 && ["notifications", "sidebar", "splits", "browser"].includes(parts[1])) { - const togglePrefix = parts[2] ?? ""; - return [ - { cmd: "on", desc: "Enable this cmux area" }, - { cmd: "off", desc: "Disable this cmux area" }, - ] - .filter((item) => item.cmd.startsWith(togglePrefix)) - .map((item) => ({ - value: `cmux ${parts[1]} ${item.cmd}`, - label: item.cmd, - description: item.desc, - })); - } - } - - if (parts[0] === "setup" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "llm", desc: "Configure LLM provider settings" }, - { cmd: "search", desc: "Configure web search provider" }, - { cmd: "remote", desc: "Configure remote integrations" }, - { cmd: "keys", desc: "Manage API keys" }, - { cmd: "prefs", desc: "Configure global preferences" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `setup ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "logs" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "debug", desc: "List or view debug log files" }, - { cmd: "tail", desc: "Show last N activity log summaries" }, - { cmd: "clear", desc: "Remove old activity and debug logs" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `logs ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "keys" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "list", desc: "Show key status dashboard" }, - { cmd: "add", desc: "Add a key for a provider" }, - { cmd: "remove", desc: "Remove a key" }, - { cmd: "test", desc: "Validate key(s) with API call" }, - { cmd: "rotate", desc: "Replace an existing key" }, - { cmd: "doctor", desc: "Health check all keys" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `keys ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "prefs" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "global", desc: "Edit global preferences file" }, - { cmd: "project", desc: "Edit project preferences file" }, - { cmd: "status", desc: "Show effective preferences" }, - { cmd: "wizard", desc: "Interactive preferences wizard" }, - { cmd: "setup", desc: "First-time preferences setup" }, - { cmd: "import-claude", desc: "Import settings from Claude Code" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `prefs ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "remote" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "slack", desc: "Configure Slack integration" }, - { cmd: "discord", desc: "Configure Discord integration" }, - { cmd: "status", desc: "Show remote connection status" }, - { cmd: "disconnect", desc: "Disconnect remote integrations" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `remote ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "next" && parts.length <= 2) { - const flagPrefix = parts[1] ?? ""; - const flags = [ - { flag: "--verbose", desc: "Show detailed step output" }, - { flag: "--dry-run", desc: "Preview next step without executing" }, - ]; - return flags - .filter((f) => f.flag.startsWith(flagPrefix)) - .map((f) => ({ value: `next ${f.flag}`, label: f.flag, description: f.desc })); - } - - if (parts[0] === "history" && parts.length <= 2) { - const flagPrefix = parts[1] ?? ""; - const flags = [ - { flag: "--cost", desc: "Show cost breakdown per entry" }, - { flag: "--phase", desc: "Filter by phase type" }, - { flag: "--model", desc: "Filter by model used" }, - { flag: "10", desc: "Show last 10 entries" }, - { flag: "20", desc: "Show last 20 entries" }, - { flag: "50", desc: "Show last 50 entries" }, - ]; - return flags - .filter((f) => f.flag.startsWith(flagPrefix)) - .map((f) => ({ value: `history ${f.flag}`, label: f.flag, description: f.desc })); - } - - if (parts[0] === "undo" && parts.length <= 2) { - return [{ value: "undo --force", label: "--force", description: "Skip confirmation prompt" }]; - } - - if (parts[0] === "export" && parts.length <= 2) { - const flagPrefix = parts[1] ?? ""; - const flags = [ - { flag: "--json", desc: "Export as JSON" }, - { flag: "--markdown", desc: "Export as Markdown" }, - { flag: "--html", desc: "Export as HTML" }, - { flag: "--html --all", desc: "Export all milestones as HTML" }, - ]; - return flags - .filter((f) => f.flag.startsWith(flagPrefix)) - .map((f) => ({ value: `export ${f.flag}`, label: f.flag, description: f.desc })); - } - - if (parts[0] === "cleanup" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "branches", desc: "Remove merged milestone branches" }, - { cmd: "snapshots", desc: "Remove old execution snapshots" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `cleanup ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "knowledge" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "rule", desc: "Add a project rule (always/never do X)" }, - { cmd: "pattern", desc: "Add a code pattern to follow" }, - { cmd: "lesson", desc: "Record a lesson learned" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `knowledge ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "start" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "bugfix", desc: "Triage, fix, test, and ship a bug fix" }, - { cmd: "small-feature", desc: "Lightweight feature with optional discussion" }, - { cmd: "spike", desc: "Research, prototype, and document findings" }, - { cmd: "hotfix", desc: "Minimal: fix it, test it, ship it" }, - { cmd: "refactor", desc: "Inventory, plan waves, migrate, verify" }, - { cmd: "security-audit", desc: "Scan, triage, remediate, re-scan" }, - { cmd: "dep-upgrade", desc: "Assess, upgrade, fix breaks, verify" }, - { cmd: "full-project", desc: "Complete GSD workflow with full ceremony" }, - { cmd: "resume", desc: "Resume an in-progress workflow" }, - { cmd: "--list", desc: "List all available templates" }, - { cmd: "--dry-run", desc: "Preview workflow without executing" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `start ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "templates" && parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "info", desc: "Show detailed template info" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `templates ${s.cmd}`, label: s.cmd, description: s.desc })); - } - - if (parts[0] === "templates" && parts[1] === "info" && parts.length <= 3) { - const namePrefix = parts[2] ?? ""; - return getTemplateCompletions(namePrefix) - .map((c) => ({ value: `templates ${c.value}`, label: c.label, description: c.description })); - } - - if (parts[0] === "extensions") { - if (parts.length <= 2) { - const subPrefix = parts[1] ?? ""; - const subs = [ - { cmd: "list", desc: "List all extensions and their status" }, - { cmd: "enable", desc: "Enable a disabled extension" }, - { cmd: "disable", desc: "Disable an extension" }, - { cmd: "info", desc: "Show extension details" }, - ]; - return subs - .filter((s) => s.cmd.startsWith(subPrefix)) - .map((s) => ({ value: `extensions ${s.cmd}`, label: s.cmd, description: s.desc })); - } - if (parts.length === 3 && ["enable", "disable", "info"].includes(parts[1])) { - const idPrefix = parts[2] ?? ""; - try { - const extDir = join(gsdHome, "agent", "extensions"); - const ids: { id: string; name: string }[] = []; - for (const entry of readdirSync(extDir, { withFileTypes: true })) { - if (!entry.isDirectory()) continue; - const mPath = join(extDir, entry.name, "extension-manifest.json"); - if (!existsSync(mPath)) continue; - try { - const m = JSON.parse(readFileSync(mPath, "utf-8")); - if (typeof m?.id === "string") ids.push({ id: m.id, name: m.name ?? m.id }); - } catch { /* skip malformed */ } - } - return ids - .filter((e) => e.id.startsWith(idPrefix)) - .map((e) => ({ - value: `extensions ${parts[1]} ${e.id}`, - label: e.id, - description: e.name, - })); - } catch { - return []; - } - } - return []; - } - - if (parts[0] === "doctor") { - const modePrefix = parts[1] ?? ""; - const modes = [ - { cmd: "fix", desc: "Auto-fix detected issues" }, - { cmd: "heal", desc: "AI-driven deep healing" }, - { cmd: "audit", desc: "Run health audit without fixing" }, - { cmd: "--dry-run", desc: "Show what --fix would change without applying" }, - { cmd: "--json", desc: "Output report as JSON (CI/tooling friendly)" }, - { cmd: "--build", desc: "Include slow build health check (npm run build)" }, - { cmd: "--test", desc: "Include slow test health check (npm test)" }, - ]; - - if (parts.length <= 2) { - return modes - .filter((m) => m.cmd.startsWith(modePrefix)) - .map((m) => ({ value: `doctor ${m.cmd}`, label: m.cmd, description: m.desc })); - } - - return []; - } - - if (parts[0] === "dispatch" && parts.length <= 2) { - const phasePrefix = parts[1] ?? ""; - const phases = [ - { cmd: "research", desc: "Run research phase" }, - { cmd: "plan", desc: "Run planning phase" }, - { cmd: "execute", desc: "Run execution phase" }, - { cmd: "complete", desc: "Run completion phase" }, - { cmd: "reassess", desc: "Reassess current progress" }, - { cmd: "uat", desc: "Run user acceptance testing" }, - { cmd: "replan", desc: "Replan the current slice" }, - ]; - return phases - .filter((p) => p.cmd.startsWith(phasePrefix)) - .map((p) => ({ value: `dispatch ${p.cmd}`, label: p.cmd, description: p.desc })); - } - - if (parts[0] === "rate" && parts.length <= 2) { - const tierPrefix = parts[1] ?? ""; - const tiers = [ - { cmd: "over", desc: "Model was overqualified for this task" }, - { cmd: "ok", desc: "Model was appropriate for this task" }, - { cmd: "under", desc: "Model was underqualified for this task" }, - ]; - return tiers - .filter((t) => t.cmd.startsWith(tierPrefix)) - .map((t) => ({ value: `rate ${t.cmd}`, label: t.cmd, description: t.desc })); - } - - return []; - }, - - async handler(args: string, ctx: ExtensionCommandContext) { - await handleGSDCommand(args, ctx, pi); - }, - }); -} +export { registerGSDCommand } from "./commands/index.js"; export async function handleGSDCommand( - args: string, - ctx: ExtensionCommandContext, - pi: ExtensionAPI, -): Promise { - const trimmed = (typeof args === "string" ? args : "").trim(); - - if (trimmed === "help" || trimmed === "h" || trimmed === "?") { - showHelp(ctx); - return; - } - - if (trimmed === "status") { - await handleStatus(ctx); - return; - } - - if (trimmed === "widget" || trimmed.startsWith("widget ")) { - const { cycleWidgetMode, setWidgetMode, getWidgetMode } = await importExtensionModule(import.meta.url, "./auto-dashboard.js"); - const arg = trimmed.replace(/^widget\s*/, "").trim(); - if (arg === "full" || arg === "small" || arg === "min" || arg === "off") { - setWidgetMode(arg); - } else { - cycleWidgetMode(); - } - ctx.ui.notify(`Widget: ${getWidgetMode()}`, "info"); - return; - } - - if (trimmed === "visualize") { - await handleVisualize(ctx); - return; - } - - if (trimmed === "mode" || trimmed.startsWith("mode ")) { - const modeArgs = trimmed.replace(/^mode\s*/, "").trim(); - const scope = modeArgs === "project" ? "project" : "global"; - const path = scope === "project" ? getProjectGSDPreferencesPath() : getGlobalGSDPreferencesPath(); - await ensurePreferencesFile(path, ctx, scope); - await handlePrefsMode(ctx, scope); - return; - } - - if (trimmed === "prefs" || trimmed.startsWith("prefs ")) { - await handlePrefs(trimmed.replace(/^prefs\s*/, "").trim(), ctx); - return; - } - - if (trimmed === "cmux" || trimmed.startsWith("cmux ")) { - await handleCmux(trimmed.replace(/^cmux\s*/, "").trim(), ctx); - return; - } - - if (trimmed === "init") { - const { detectProjectState } = await import("./detection.js"); - const { showProjectInit, handleReinit } = await import("./init-wizard.js"); - const basePath = projectRoot(); - const detection = detectProjectState(basePath); - if (detection.state === "v2-gsd" || detection.state === "v2-gsd-empty") { - await handleReinit(ctx, detection); - } else { - await showProjectInit(ctx, pi, basePath, detection); - } - return; - } - - if (trimmed === "keys" || trimmed.startsWith("keys ")) { - const { handleKeys } = await import("./key-manager.js"); - const keysArgs = trimmed.replace(/^keys\s*/, "").trim(); - await handleKeys(keysArgs, ctx); - return; - } - - if (trimmed === "setup" || trimmed.startsWith("setup ")) { - const setupArgs = trimmed.replace(/^setup\s*/, "").trim(); - await handleSetup(setupArgs, ctx); - return; - } - - if (trimmed === "doctor" || trimmed.startsWith("doctor ")) { - await handleDoctor(trimmed.replace(/^doctor\s*/, "").trim(), ctx, pi); - return; - } - - if (trimmed === "logs" || trimmed.startsWith("logs ")) { - await handleLogs(trimmed.replace(/^logs\s*/, "").trim(), ctx); - return; - } - - if (trimmed === "forensics" || trimmed.startsWith("forensics ")) { - const { handleForensics } = await import("./forensics.js"); - await handleForensics(trimmed.replace(/^forensics\s*/, "").trim(), ctx, pi); - return; - } - - if (trimmed === "changelog" || trimmed.startsWith("changelog ")) { - const { handleChangelog } = await import("./changelog.js"); - await handleChangelog(trimmed.replace(/^changelog\s*/, "").trim(), ctx, pi); - return; - } - - if (trimmed === "next" || trimmed.startsWith("next ")) { - if (trimmed.includes("--dry-run")) { - await handleDryRun(ctx, projectRoot()); - return; - } - const verboseMode = trimmed.includes("--verbose"); - const debugMode = trimmed.includes("--debug"); - if (debugMode) enableDebug(projectRoot()); - if (!(await guardRemoteSession(ctx, pi))) return; - await startAuto(ctx, pi, projectRoot(), verboseMode, { step: true }); - return; - } - - if (trimmed === "auto" || trimmed.startsWith("auto ")) { - const verboseMode = trimmed.includes("--verbose"); - const debugMode = trimmed.includes("--debug"); - if (debugMode) enableDebug(projectRoot()); - if (!(await guardRemoteSession(ctx, pi))) return; - await startAuto(ctx, pi, projectRoot(), verboseMode); - return; - } - - if (trimmed === "stop") { - if (!isAutoActive() && !isAutoPaused()) { - // Not running in this process — check for a remote auto-mode session - const result = stopAutoRemote(projectRoot()); - if (result.found) { - ctx.ui.notify(`Sent stop signal to auto-mode session (PID ${result.pid}). It will shut down gracefully.`, "info"); - } else if (result.error) { - ctx.ui.notify(`Failed to stop remote auto-mode: ${result.error}`, "error"); - } else { - ctx.ui.notify("Auto-mode is not running.", "info"); - } - return; - } - await stopAuto(ctx, pi, "User requested stop"); - return; - } - - if (trimmed === "pause") { - if (!isAutoActive()) { - if (isAutoPaused()) { - ctx.ui.notify("Auto-mode is already paused. /gsd auto to resume.", "info"); - } else { - ctx.ui.notify("Auto-mode is not running.", "info"); - } - return; - } - await pauseAuto(ctx, pi); - return; - } - - if (trimmed === "history" || trimmed.startsWith("history ")) { - await handleHistory(trimmed.replace(/^history\s*/, "").trim(), ctx, projectRoot()); - return; - } - - if (trimmed === "undo" || trimmed.startsWith("undo ")) { - await handleUndo(trimmed.replace(/^undo\s*/, "").trim(), ctx, pi, projectRoot()); - return; - } - - if (trimmed === "rate" || trimmed.startsWith("rate ")) { - const { handleRate } = await import("./commands-rate.js"); - await handleRate(trimmed.replace(/^rate\s*/, "").trim(), ctx, projectRoot()); - return; - } - - if (trimmed.startsWith("skip ")) { - await handleSkip(trimmed.replace(/^skip\s*/, "").trim(), ctx, projectRoot()); - return; - } - - if (trimmed === "export" || trimmed.startsWith("export ")) { - await handleExport(trimmed.replace(/^export\s*/, "").trim(), ctx, projectRoot()); - return; - } - - // ─── Parallel Orchestration ──────────────────────────────────────── - if (trimmed.startsWith("parallel")) { - const parallelArgs = trimmed.slice("parallel".length).trim(); - const [subCmd = "", ...restParts] = parallelArgs.split(/\s+/); - const rest = restParts.join(" "); - - if (subCmd === "start" || subCmd === "") { - const loaded = loadEffectiveGSDPreferences(); - const config = resolveParallelConfig(loaded?.preferences); - if (!config.enabled) { - pi.sendMessage({ - customType: "gsd-parallel", - content: "Parallel mode is not enabled. Set `parallel.enabled: true` in your preferences.", - display: false, - }); - return; - } - const candidates = await prepareParallelStart(projectRoot(), loaded?.preferences); - const report = formatEligibilityReport(candidates); - if (candidates.eligible.length === 0) { - pi.sendMessage({ customType: "gsd-parallel", content: report + "\n\nNo milestones are eligible for parallel execution.", display: false }); - return; - } - const result = await startParallel( - projectRoot(), - candidates.eligible.map(e => e.milestoneId), - loaded?.preferences, - ); - const lines = [`Parallel orchestration started.`, `Workers: ${result.started.join(", ")}`]; - if (result.errors.length > 0) { - lines.push(`Errors: ${result.errors.map(e => `${e.mid}: ${e.error}`).join("; ")}`); - } - pi.sendMessage({ customType: "gsd-parallel", content: report + "\n\n" + lines.join("\n"), display: false }); - return; - } - - if (subCmd === "status") { - if (!isParallelActive()) { - pi.sendMessage({ customType: "gsd-parallel", content: "No parallel orchestration is currently active.", display: false }); - return; - } - const workers = getWorkerStatuses(); - const lines = ["# Parallel Workers\n"]; - for (const w of workers) { - lines.push(`- **${w.milestoneId}** (${w.title}) — ${w.state} — ${w.completedUnits} units — $${w.cost.toFixed(2)}`); - } - const orchState = getOrchestratorState(); - if (orchState) { - lines.push(`\nTotal cost: $${orchState.totalCost.toFixed(2)}`); - } - pi.sendMessage({ customType: "gsd-parallel", content: lines.join("\n"), display: false }); - return; - } - - if (subCmd === "stop") { - const mid = rest.trim() || undefined; - await stopParallel(projectRoot(), mid); - pi.sendMessage({ customType: "gsd-parallel", content: mid ? `Stopped worker for ${mid}.` : "All parallel workers stopped.", display: false }); - return; - } - - if (subCmd === "pause") { - const mid = rest.trim() || undefined; - pauseWorker(projectRoot(), mid); - pi.sendMessage({ customType: "gsd-parallel", content: mid ? `Paused worker for ${mid}.` : "All parallel workers paused.", display: false }); - return; - } - - if (subCmd === "resume") { - const mid = rest.trim() || undefined; - resumeWorker(projectRoot(), mid); - pi.sendMessage({ customType: "gsd-parallel", content: mid ? `Resumed worker for ${mid}.` : "All parallel workers resumed.", display: false }); - return; - } - - if (subCmd === "merge") { - const mid = rest.trim() || undefined; - if (mid) { - // Merge a specific milestone - const result = await mergeCompletedMilestone(projectRoot(), mid); - pi.sendMessage({ customType: "gsd-parallel", content: formatMergeResults([result]), display: false }); - return; - } - // Merge all completed milestones - const workers = getWorkerStatuses(); - if (workers.length === 0) { - pi.sendMessage({ customType: "gsd-parallel", content: "No parallel workers to merge.", display: false }); - return; - } - const results = await mergeAllCompleted(projectRoot(), workers); - pi.sendMessage({ customType: "gsd-parallel", content: formatMergeResults(results), display: false }); - return; - } - - pi.sendMessage({ - customType: "gsd-parallel", - content: `Unknown parallel subcommand "${subCmd}". Usage: /gsd parallel [start|status|stop|pause|resume|merge]`, - display: false, - }); - return; - } - - if (trimmed === "cleanup") { - await handleCleanupBranches(ctx, projectRoot()); - await handleCleanupSnapshots(ctx, projectRoot()); - return; - } - - if (trimmed === "cleanup branches") { - await handleCleanupBranches(ctx, projectRoot()); - return; - } - - if (trimmed === "cleanup snapshots") { - await handleCleanupSnapshots(ctx, projectRoot()); - return; - } - - if (trimmed === "queue") { - await showQueue(ctx, pi, projectRoot()); - return; - } - - if (trimmed === "discuss") { - await showDiscuss(ctx, pi, projectRoot()); - return; - } - - if (trimmed === "park" || trimmed.startsWith("park ")) { - const basePath = projectRoot(); - const arg = trimmed.replace(/^park\s*/, "").trim(); - const { parkMilestone, isParked } = await import("./milestone-actions.js"); - const { deriveState } = await import("./state.js"); - - let targetId = arg; - if (!targetId) { - // Park the current active milestone - const state = await deriveState(basePath); - if (!state.activeMilestone) { - ctx.ui.notify("No active milestone to park.", "warning"); - return; - } - targetId = state.activeMilestone.id; - } - - if (isParked(basePath, targetId)) { - ctx.ui.notify(`${targetId} is already parked. Use /gsd unpark ${targetId} to reactivate.`, "info"); - return; - } - - // Extract reason from remaining args (e.g., /gsd park M002 "reason here") - const reasonParts = arg.replace(targetId, "").trim().replace(/^["']|["']$/g, ""); - const reason = reasonParts || "Parked via /gsd park"; - - const success = parkMilestone(basePath, targetId, reason); - if (success) { - ctx.ui.notify(`Parked ${targetId}. Run /gsd unpark ${targetId} to reactivate.`, "info"); - } else { - ctx.ui.notify(`Could not park ${targetId} — milestone not found.`, "warning"); - } - return; - } - - if (trimmed === "unpark" || trimmed.startsWith("unpark ")) { - const basePath = projectRoot(); - const arg = trimmed.replace(/^unpark\s*/, "").trim(); - const { unparkMilestone } = await import("./milestone-actions.js"); - const { deriveState } = await import("./state.js"); - - let targetId = arg; - if (!targetId) { - // List parked milestones and let user pick - const state = await deriveState(basePath); - const parkedEntries = state.registry.filter(e => e.status === "parked"); - if (parkedEntries.length === 0) { - ctx.ui.notify("No parked milestones.", "info"); - return; - } - if (parkedEntries.length === 1) { - targetId = parkedEntries[0].id; - } else { - ctx.ui.notify(`Parked milestones: ${parkedEntries.map(e => e.id).join(", ")}. Specify which to unpark: /gsd unpark `, "info"); - return; - } - } - - const success = unparkMilestone(basePath, targetId); - if (success) { - ctx.ui.notify(`Unparked ${targetId}. It will resume its normal position in the queue.`, "info"); - } else { - ctx.ui.notify(`Could not unpark ${targetId} — milestone not found or not parked.`, "warning"); - } - return; - } - - if (trimmed === "new-milestone") { - const basePath = projectRoot(); - const headlessContextPath = join(gsdRoot(basePath), "runtime", "headless-context.md"); - if (existsSync(headlessContextPath)) { - const seedContext = readFileSync(headlessContextPath, "utf-8"); - try { unlinkSync(headlessContextPath); } catch { /* non-fatal */ } - await showHeadlessMilestoneCreation(ctx, pi, basePath, seedContext); - } else { - // No headless context — fall back to interactive smart entry - const { showSmartEntry } = await import("./guided-flow.js"); - await showSmartEntry(ctx, pi, basePath); - } - return; - } - - if (trimmed.startsWith("capture ") || trimmed === "capture") { - await handleCapture(trimmed.replace(/^capture\s*/, "").trim(), ctx); - return; - } - - if (trimmed === "triage") { - await handleTriage(ctx, pi, process.cwd()); - return; - } - - if (trimmed === "quick" || trimmed.startsWith("quick ")) { - await handleQuick(trimmed.replace(/^quick\s*/, "").trim(), ctx, pi); - return; - } - - if (trimmed === "config") { - await handleConfig(ctx); - return; - } - - if (trimmed === "hooks") { - const { formatHookStatus } = await import("./post-unit-hooks.js"); - ctx.ui.notify(formatHookStatus(), "info"); - return; - } - - // ─── Skill Health ──────────────────────────────────────────── - if (trimmed === "skill-health" || trimmed.startsWith("skill-health ")) { - await handleSkillHealth(trimmed.replace(/^skill-health\s*/, "").trim(), ctx); - return; - } - - if (trimmed.startsWith("run-hook ")) { - await handleRunHook(trimmed.replace(/^run-hook\s*/, "").trim(), ctx, pi); - return; - } - if (trimmed === "run-hook") { - ctx.ui.notify(`Usage: /gsd run-hook - -Unit types: - execute-task - Task execution (unit-id: M001/S01/T01) - plan-slice - Slice planning (unit-id: M001/S01) - research-milestone - Milestone research (unit-id: M001) - complete-slice - Slice completion (unit-id: M001/S01) - complete-milestone - Milestone completion (unit-id: M001) - -Examples: - /gsd run-hook code-review execute-task M001/S01/T01 - /gsd run-hook lint-check plan-slice M001/S01`, "warning"); - return; - } - - if (trimmed.startsWith("steer ")) { - await handleSteer(trimmed.replace(/^steer\s+/, "").trim(), ctx, pi); - return; - } - if (trimmed === "steer") { - ctx.ui.notify("Usage: /gsd steer . Example: /gsd steer Use Postgres instead of SQLite", "warning"); - return; - } - - if (trimmed.startsWith("knowledge ")) { - await handleKnowledge(trimmed.replace(/^knowledge\s+/, "").trim(), ctx); - return; - } - if (trimmed === "knowledge") { - ctx.ui.notify("Usage: /gsd knowledge . Example: /gsd knowledge rule Use real DB for integration tests", "warning"); - return; - } - - if (trimmed === "migrate" || trimmed.startsWith("migrate ")) { - const { handleMigrate } = await import("./migrate/command.js"); - await handleMigrate(trimmed.replace(/^migrate\s*/, "").trim(), ctx, pi); - return; - } - - if (trimmed === "remote" || trimmed.startsWith("remote ")) { - await handleRemote(trimmed.replace(/^remote\s*/, "").trim(), ctx, pi); - return; - } - - if (trimmed === "dispatch" || trimmed.startsWith("dispatch ")) { - const phase = trimmed.replace(/^dispatch\s*/, "").trim(); - if (!phase) { - ctx.ui.notify("Usage: /gsd dispatch (research|plan|execute|complete|reassess|uat|replan)", "warning"); - return; - } - await dispatchDirectPhase(ctx, pi, phase, projectRoot()); - return; - } - - if (trimmed === "inspect") { - await handleInspect(ctx); - return; - } - - if (trimmed === "update") { - await handleUpdate(ctx); - return; - } - - // ─── Workflow Templates ──────────────────────────────────────── - if (trimmed === "start" || trimmed.startsWith("start ")) { - await handleStart(trimmed.replace(/^start\s*/, "").trim(), ctx, pi); - return; - } - - if (trimmed === "templates" || trimmed.startsWith("templates ")) { - await handleTemplates(trimmed.replace(/^templates\s*/, "").trim(), ctx); - return; - } - - if (trimmed === "") { - if (!(await guardRemoteSession(ctx, pi))) return; - await startAuto(ctx, pi, projectRoot(), false, { step: true }); - return; - } - - if (trimmed === "extensions" || trimmed.startsWith("extensions ")) { - const { handleExtensions } = await import("./commands-extensions.js"); - await handleExtensions(trimmed.replace(/^extensions\s*/, "").trim(), ctx); - return; - } - - ctx.ui.notify( - `Unknown: /gsd ${trimmed}. Run /gsd help for available commands.`, - "warning", - ); -} - -function showHelp(ctx: ExtensionCommandContext): void { - const lines = [ - "GSD — Get Shit Done\n", - "WORKFLOW", - " /gsd start Start a workflow template (bugfix, spike, feature, hotfix, etc.)", - " /gsd templates List available workflow templates [info ]", - " /gsd Run next unit in step mode (same as /gsd next)", - " /gsd next Execute next task, then pause [--dry-run] [--verbose]", - " /gsd auto Run all queued units continuously [--verbose]", - " /gsd stop Stop auto-mode gracefully", - " /gsd pause Pause auto-mode (preserves state, /gsd auto to resume)", - " /gsd discuss Start guided milestone/slice discussion", - " /gsd new-milestone Create milestone from headless context (used by gsd headless)", - "", - "VISIBILITY", - " /gsd status Show progress dashboard (Ctrl+Alt+G)", - " /gsd visualize Interactive 10-tab TUI (progress, timeline, deps, metrics, health, agent, changes, knowledge, captures, export)", - " /gsd queue Show queued/dispatched units and execution order", - " /gsd history View execution history [--cost] [--phase] [--model] [N]", - " /gsd changelog Show categorized release notes [version]", - "", - "COURSE CORRECTION", - " /gsd steer Apply user override to active work", - " /gsd capture Quick-capture a thought to CAPTURES.md", - " /gsd triage Classify and route pending captures", - " /gsd skip Prevent a unit from auto-mode dispatch", - " /gsd undo Revert last completed unit [--force]", - " /gsd park [id] Park a milestone — skip without deleting [reason]", - " /gsd unpark [id] Reactivate a parked milestone", - "", - "PROJECT KNOWLEDGE", - " /gsd knowledge Add rule, pattern, or lesson to KNOWLEDGE.md", - "", - "SETUP & CONFIGURATION", - " /gsd init Project init wizard — detect, configure, bootstrap .gsd/", - " /gsd setup Global setup status [llm|search|remote|keys|prefs]", - " /gsd mode Set workflow mode (solo/team) [global|project]", - " /gsd prefs Manage preferences [global|project|status|wizard|setup|import-claude]", - " /gsd cmux Manage cmux integration [status|on|off|notifications|sidebar|splits|browser]", - " /gsd config Set API keys for external tools", - " /gsd keys API key manager [list|add|remove|test|rotate|doctor]", - " /gsd hooks Show post-unit hook configuration", - " /gsd extensions Manage extensions [list|enable|disable|info]", - "", - "MAINTENANCE", - " /gsd doctor Diagnose and repair .gsd/ state [audit|fix|heal] [scope]", - " /gsd export Export milestone/slice results [--json|--markdown|--html] [--all]", - " /gsd cleanup Remove merged branches or snapshots [branches|snapshots]", - " /gsd migrate Migrate .planning/ (v1) to .gsd/ (v2) format", - " /gsd remote Control remote auto-mode [slack|discord|status|disconnect]", - " /gsd inspect Show SQLite DB diagnostics (schema, row counts, recent entries)", - " /gsd update Update GSD to the latest version via npm", - ]; - ctx.ui.notify(lines.join("\n"), "info"); -} - -async function handleStatus(ctx: ExtensionCommandContext): Promise { - const basePath = projectRoot(); - const state = await deriveState(basePath); - - if (state.registry.length === 0) { - ctx.ui.notify("No GSD milestones found. Run /gsd to start.", "info"); - return; - } - - const result = await ctx.ui.custom( - (tui, theme, _kb, done) => { - return new GSDDashboardOverlay(tui, theme, () => done()); - }, - { - overlay: true, - overlayOptions: { - width: "70%", - minWidth: 60, - maxHeight: "90%", - anchor: "center", - }, - }, - ); - - // Fallback for RPC mode where ctx.ui.custom() returns undefined. - // Produce a text-based status summary so the turn is not empty. - if (result === undefined) { - ctx.ui.notify(formatTextStatus(state), "info"); - } + ...args: Parameters +) { + const { handleGSDCommand: dispatch } = await import("./commands/dispatcher.js"); + return dispatch(...args); } export async function fireStatusViaCommand( - ctx: import("@gsd/pi-coding-agent").ExtensionContext, -): Promise { - await handleStatus(ctx as ExtensionCommandContext); -} - -async function handleVisualize(ctx: ExtensionCommandContext): Promise { - if (!ctx.hasUI) { - ctx.ui.notify("Visualizer requires an interactive terminal.", "warning"); - return; - } - - const result = await ctx.ui.custom( - (tui, theme, _kb, done) => { - return new GSDVisualizerOverlay(tui, theme, () => done()); - }, - { - overlay: true, - overlayOptions: { - width: "80%", - minWidth: 80, - maxHeight: "90%", - anchor: "center", - }, - }, + ...args: Parameters +) { + const { fireStatusViaCommand: fireStatus } = await import( + "./commands/handlers/core.js" ); - - // Fallback for RPC mode where ctx.ui.custom() returns undefined. - if (result === undefined) { - ctx.ui.notify("Visualizer requires an interactive terminal. Use /gsd status for a text-based overview.", "warning"); - } -} - -async function handleSetup(args: string, ctx: ExtensionCommandContext): Promise { - const { detectProjectState, hasGlobalSetup } = await import("./detection.js"); - - // Show current global setup status - const globalConfigured = hasGlobalSetup(); - const detection = detectProjectState(projectRoot()); - - const statusLines = ["GSD Setup Status\n"]; - statusLines.push(` Global preferences: ${globalConfigured ? "configured" : "not set"}`); - statusLines.push(` Project state: ${detection.state}`); - if (detection.projectSignals.primaryLanguage) { - statusLines.push(` Detected: ${detection.projectSignals.primaryLanguage}`); - } - - if (args === "llm" || args === "auth") { - ctx.ui.notify("Use /login to configure LLM authentication.", "info"); - return; - } - - if (args === "search") { - ctx.ui.notify("Use /search-provider to configure web search.", "info"); - return; - } - - if (args === "remote") { - ctx.ui.notify("Use /gsd remote to configure remote questions.", "info"); - return; - } - - if (args === "keys") { - const { handleKeys } = await import("./key-manager.js"); - await handleKeys("", ctx); - return; - } - - if (args === "prefs") { - await ensurePreferencesFile(getGlobalGSDPreferencesPath(), ctx, "global"); - await handlePrefsWizard(ctx, "global"); - return; - } - - // Full setup summary - ctx.ui.notify(statusLines.join("\n"), "info"); - ctx.ui.notify( - "Available setup commands:\n" + - " /gsd setup llm — LLM authentication\n" + - " /gsd setup search — Web search provider\n" + - " /gsd setup remote — Remote questions (Discord/Slack/Telegram)\n" + - " /gsd setup keys — Tool API keys\n" + - " /gsd setup prefs — Global preferences wizard", - "info", - ); -} - -// ─── Text-based status for RPC mode ──────────────────────────────────────── - -/** - * Generate a text-based status summary for non-TUI environments (RPC mode). - * Used as a fallback when the interactive dashboard overlay is unavailable. - */ -function formatTextStatus(state: GSDState): string { - const lines: string[] = ["GSD Status\n"]; - - // Progress score — traffic light (#1221) - const progressScore = computeProgressScore(); - lines.push(formatProgressLine(progressScore)); - lines.push(""); - - // Phase - lines.push(`Phase: ${state.phase}`); - - // Active milestone - if (state.activeMilestone) { - lines.push(`Active milestone: ${state.activeMilestone.id} — ${state.activeMilestone.title}`); - } - - // Active slice / task - if (state.activeSlice) { - lines.push(`Active slice: ${state.activeSlice.id} — ${state.activeSlice.title}`); - } - if (state.activeTask) { - lines.push(`Active task: ${state.activeTask.id} — ${state.activeTask.title}`); - } - - // Progress - if (state.progress) { - const { milestones, slices, tasks } = state.progress; - const parts: string[] = []; - parts.push(`milestones ${milestones.done}/${milestones.total}`); - if (slices) parts.push(`slices ${slices.done}/${slices.total}`); - if (tasks) parts.push(`tasks ${tasks.done}/${tasks.total}`); - lines.push(`Progress: ${parts.join(", ")}`); - } - - // Next action - if (state.nextAction) { - lines.push(`Next: ${state.nextAction}`); - } - - // Blockers - if (state.blockers.length > 0) { - lines.push(`Blockers: ${state.blockers.join("; ")}`); - } - - // Milestone registry summary - if (state.registry.length > 0) { - lines.push(""); - lines.push("Milestones:"); - for (const m of state.registry) { - const statusIcon = m.status === "complete" ? "✓" : m.status === "active" ? "▶" : m.status === "parked" ? "⏸" : "○"; - lines.push(` ${statusIcon} ${m.id}: ${m.title} (${m.status})`); - } - } - - // Environment health (#1221) - const envResults = runEnvironmentChecks(projectRoot()); - const envIssues = envResults.filter(r => r.status !== "ok"); - if (envIssues.length > 0) { - lines.push(""); - lines.push("Environment:"); - for (const r of envIssues) { - const icon = r.status === "error" ? "✗" : "⚠"; - lines.push(` ${icon} ${r.message}`); - } - } - - return lines.join("\n"); + return fireStatus(...args); } diff --git a/src/resources/extensions/gsd/commands/catalog.ts b/src/resources/extensions/gsd/commands/catalog.ts new file mode 100644 index 000000000..e085730e1 --- /dev/null +++ b/src/resources/extensions/gsd/commands/catalog.ts @@ -0,0 +1,301 @@ +import { existsSync, readFileSync, readdirSync } from "node:fs"; +import { homedir } from "node:os"; +import { join } from "node:path"; + +import { loadRegistry } from "../workflow-templates.js"; + +const gsdHome = process.env.GSD_HOME || join(homedir(), ".gsd"); + +export interface GsdCommandDefinition { + cmd: string; + desc: string; +} + +type CompletionMap = Record; + +export const GSD_COMMAND_DESCRIPTION = + "GSD — Get Shit Done: /gsd help|start|templates|next|auto|stop|pause|status|widget|visualize|queue|quick|capture|triage|dispatch|history|undo|rate|skip|export|cleanup|mode|prefs|config|keys|hooks|run-hook|skill-health|doctor|logs|forensics|changelog|migrate|remote|steer|knowledge|new-milestone|parallel|cmux|park|unpark|init|setup|inspect|extensions|update"; + +export const TOP_LEVEL_SUBCOMMANDS: readonly GsdCommandDefinition[] = [ + { cmd: "help", desc: "Categorized command reference with descriptions" }, + { cmd: "next", desc: "Explicit step mode (same as /gsd)" }, + { cmd: "auto", desc: "Autonomous mode — research, plan, execute, commit, repeat" }, + { cmd: "stop", desc: "Stop auto mode gracefully" }, + { cmd: "pause", desc: "Pause auto-mode (preserves state, /gsd auto to resume)" }, + { cmd: "status", desc: "Progress dashboard" }, + { cmd: "widget", desc: "Cycle widget: full → small → min → off" }, + { cmd: "visualize", desc: "Open 10-tab workflow visualizer (progress, timeline, deps, metrics, health, agent, changes, knowledge, captures, export)" }, + { cmd: "queue", desc: "Queue and reorder future milestones" }, + { cmd: "quick", desc: "Execute a quick task without full planning overhead" }, + { cmd: "discuss", desc: "Discuss architecture and decisions" }, + { cmd: "capture", desc: "Fire-and-forget thought capture" }, + { cmd: "changelog", desc: "Show categorized release notes" }, + { cmd: "triage", desc: "Manually trigger triage of pending captures" }, + { cmd: "dispatch", desc: "Dispatch a specific phase directly" }, + { cmd: "history", desc: "View execution history" }, + { cmd: "undo", desc: "Revert last completed unit" }, + { cmd: "rate", desc: "Rate last unit's model tier (over/ok/under) — improves adaptive routing" }, + { cmd: "skip", desc: "Prevent a unit from auto-mode dispatch" }, + { cmd: "export", desc: "Export milestone/slice results" }, + { cmd: "cleanup", desc: "Remove merged branches or snapshots" }, + { cmd: "mode", desc: "Switch workflow mode (solo/team)" }, + { cmd: "prefs", desc: "Manage preferences (model selection, timeouts, etc.)" }, + { cmd: "config", desc: "Set API keys for external tools" }, + { cmd: "keys", desc: "API key manager — list, add, remove, test, rotate, doctor" }, + { cmd: "hooks", desc: "Show configured post-unit and pre-dispatch hooks" }, + { cmd: "run-hook", desc: "Manually trigger a specific hook" }, + { cmd: "skill-health", desc: "Skill lifecycle dashboard" }, + { cmd: "doctor", desc: "Runtime health checks with auto-fix" }, + { cmd: "logs", desc: "Browse activity logs, debug logs, and metrics" }, + { cmd: "forensics", desc: "Examine execution logs" }, + { cmd: "init", desc: "Project init wizard — detect, configure, bootstrap .gsd/" }, + { cmd: "setup", desc: "Global setup status and configuration" }, + { cmd: "migrate", desc: "Migrate a v1 .planning directory to .gsd format" }, + { cmd: "remote", desc: "Control remote auto-mode" }, + { cmd: "steer", desc: "Hard-steer plan documents during execution" }, + { cmd: "inspect", desc: "Show SQLite DB diagnostics" }, + { cmd: "knowledge", desc: "Add persistent project knowledge (rule, pattern, or lesson)" }, + { cmd: "new-milestone", desc: "Create a milestone from a specification document (headless)" }, + { cmd: "parallel", desc: "Parallel milestone orchestration (start, status, stop, merge)" }, + { cmd: "cmux", desc: "Manage cmux integration (status, sidebar, notifications, splits)" }, + { cmd: "park", desc: "Park a milestone — skip without deleting" }, + { cmd: "unpark", desc: "Reactivate a parked milestone" }, + { cmd: "update", desc: "Update GSD to the latest version" }, + { cmd: "start", desc: "Start a workflow template (bugfix, spike, feature, etc.)" }, + { cmd: "templates", desc: "List available workflow templates" }, + { cmd: "extensions", desc: "Manage extensions (list, enable, disable, info)" }, +]; + +const NESTED_COMPLETIONS: CompletionMap = { + auto: [ + { cmd: "--verbose", desc: "Show detailed execution output" }, + { cmd: "--debug", desc: "Enable debug logging" }, + ], + next: [ + { cmd: "--verbose", desc: "Show detailed step output" }, + { cmd: "--dry-run", desc: "Preview next step without executing" }, + ], + mode: [ + { cmd: "global", desc: "Edit global workflow mode" }, + { cmd: "project", desc: "Edit project-specific workflow mode" }, + ], + parallel: [ + { cmd: "start", desc: "Start parallel milestone orchestration" }, + { cmd: "status", desc: "Show parallel worker statuses" }, + { cmd: "stop", desc: "Stop all parallel workers" }, + { cmd: "pause", desc: "Pause a specific worker" }, + { cmd: "resume", desc: "Resume a paused worker" }, + { cmd: "merge", desc: "Merge completed milestone branches" }, + ], + setup: [ + { cmd: "llm", desc: "Configure LLM provider settings" }, + { cmd: "search", desc: "Configure web search provider" }, + { cmd: "remote", desc: "Configure remote integrations" }, + { cmd: "keys", desc: "Manage API keys" }, + { cmd: "prefs", desc: "Configure global preferences" }, + ], + logs: [ + { cmd: "debug", desc: "List or view debug log files" }, + { cmd: "tail", desc: "Show last N activity log summaries" }, + { cmd: "clear", desc: "Remove old activity and debug logs" }, + ], + keys: [ + { cmd: "list", desc: "Show key status dashboard" }, + { cmd: "add", desc: "Add a key for a provider" }, + { cmd: "remove", desc: "Remove a key" }, + { cmd: "test", desc: "Validate key(s) with API call" }, + { cmd: "rotate", desc: "Replace an existing key" }, + { cmd: "doctor", desc: "Health check all keys" }, + ], + prefs: [ + { cmd: "global", desc: "Edit global preferences file" }, + { cmd: "project", desc: "Edit project preferences file" }, + { cmd: "status", desc: "Show effective preferences" }, + { cmd: "wizard", desc: "Interactive preferences wizard" }, + { cmd: "setup", desc: "First-time preferences setup" }, + { cmd: "import-claude", desc: "Import settings from Claude Code" }, + ], + remote: [ + { cmd: "slack", desc: "Configure Slack integration" }, + { cmd: "discord", desc: "Configure Discord integration" }, + { cmd: "status", desc: "Show remote connection status" }, + { cmd: "disconnect", desc: "Disconnect remote integrations" }, + ], + history: [ + { cmd: "--cost", desc: "Show cost breakdown per entry" }, + { cmd: "--phase", desc: "Filter by phase type" }, + { cmd: "--model", desc: "Filter by model used" }, + { cmd: "10", desc: "Show last 10 entries" }, + { cmd: "20", desc: "Show last 20 entries" }, + { cmd: "50", desc: "Show last 50 entries" }, + ], + export: [ + { cmd: "--json", desc: "Export as JSON" }, + { cmd: "--markdown", desc: "Export as Markdown" }, + { cmd: "--html", desc: "Export as HTML" }, + { cmd: "--html --all", desc: "Export all milestones as HTML" }, + ], + cleanup: [ + { cmd: "branches", desc: "Remove merged milestone branches" }, + { cmd: "snapshots", desc: "Remove old execution snapshots" }, + ], + knowledge: [ + { cmd: "rule", desc: "Add a project rule (always/never do X)" }, + { cmd: "pattern", desc: "Add a code pattern to follow" }, + { cmd: "lesson", desc: "Record a lesson learned" }, + ], + start: [ + { cmd: "bugfix", desc: "Triage, fix, test, and ship a bug fix" }, + { cmd: "small-feature", desc: "Lightweight feature with optional discussion" }, + { cmd: "spike", desc: "Research, prototype, and document findings" }, + { cmd: "hotfix", desc: "Minimal: fix it, test it, ship it" }, + { cmd: "refactor", desc: "Inventory, plan waves, migrate, verify" }, + { cmd: "security-audit", desc: "Scan, triage, remediate, re-scan" }, + { cmd: "dep-upgrade", desc: "Assess, upgrade, fix breaks, verify" }, + { cmd: "full-project", desc: "Complete GSD workflow with full ceremony" }, + { cmd: "resume", desc: "Resume an in-progress workflow" }, + { cmd: "--list", desc: "List all available templates" }, + { cmd: "--dry-run", desc: "Preview workflow without executing" }, + ], + templates: [ + { cmd: "info", desc: "Show detailed template info" }, + ], + extensions: [ + { cmd: "list", desc: "List all extensions and their status" }, + { cmd: "enable", desc: "Enable a disabled extension" }, + { cmd: "disable", desc: "Disable an extension" }, + { cmd: "info", desc: "Show extension details" }, + ], + doctor: [ + { cmd: "fix", desc: "Auto-fix detected issues" }, + { cmd: "heal", desc: "AI-driven deep healing" }, + { cmd: "audit", desc: "Run health audit without fixing" }, + { cmd: "--dry-run", desc: "Show what --fix would change without applying" }, + { cmd: "--json", desc: "Output report as JSON (CI/tooling friendly)" }, + { cmd: "--build", desc: "Include slow build health check (npm run build)" }, + { cmd: "--test", desc: "Include slow test health check (npm test)" }, + ], + dispatch: [ + { cmd: "research", desc: "Run research phase" }, + { cmd: "plan", desc: "Run planning phase" }, + { cmd: "execute", desc: "Run execution phase" }, + { cmd: "complete", desc: "Run completion phase" }, + { cmd: "reassess", desc: "Reassess current progress" }, + { cmd: "uat", desc: "Run user acceptance testing" }, + { cmd: "replan", desc: "Replan the current slice" }, + ], + rate: [ + { cmd: "over", desc: "Model was overqualified for this task" }, + { cmd: "ok", desc: "Model was appropriate for this task" }, + { cmd: "under", desc: "Model was underqualified for this task" }, + ], +}; + +function filterOptions( + partial: string, + options: readonly GsdCommandDefinition[], + prefix = "", +) { + const normalizedPrefix = prefix ? `${prefix} ` : ""; + return options + .filter((option) => option.cmd.startsWith(partial)) + .map((option) => ({ + value: `${normalizedPrefix}${option.cmd}`, + label: option.cmd, + description: option.desc, + })); +} + +function getExtensionCompletions(prefix: string, action: string) { + try { + const extDir = join(gsdHome, "agent", "extensions"); + const ids: Array<{ id: string; name: string }> = []; + for (const entry of readdirSync(extDir, { withFileTypes: true })) { + if (!entry.isDirectory()) continue; + const manifestPath = join(extDir, entry.name, "extension-manifest.json"); + if (!existsSync(manifestPath)) continue; + try { + const manifest = JSON.parse(readFileSync(manifestPath, "utf-8")); + if (typeof manifest?.id === "string") { + ids.push({ id: manifest.id, name: manifest.name ?? manifest.id }); + } + } catch { + // ignore malformed manifests + } + } + return ids + .filter((entry) => entry.id.startsWith(prefix)) + .map((entry) => ({ + value: `extensions ${action} ${entry.id}`, + label: entry.id, + description: entry.name, + })); + } catch { + return []; + } +} + +export function getGsdArgumentCompletions(prefix: string) { + const hasTrailingSpace = prefix.endsWith(" "); + const parts = prefix.trim().split(/\s+/); + if (hasTrailingSpace && parts.length >= 1) { + parts.push(""); + } + + if (parts.length <= 1) { + return filterOptions(parts[0] ?? "", TOP_LEVEL_SUBCOMMANDS); + } + + const [command, subcommand = "", third = ""] = parts; + + if (command === "cmux") { + if (parts.length <= 2) { + return filterOptions(subcommand, [ + { cmd: "status", desc: "Show cmux detection, prefs, and capabilities" }, + { cmd: "on", desc: "Enable cmux integration" }, + { cmd: "off", desc: "Disable cmux integration" }, + { cmd: "notifications", desc: "Toggle cmux desktop notifications" }, + { cmd: "sidebar", desc: "Toggle cmux sidebar metadata" }, + { cmd: "splits", desc: "Toggle cmux visual subagent splits" }, + { cmd: "browser", desc: "Toggle future browser integration flag" }, + ], "cmux"); + } + if (parts.length <= 3 && ["notifications", "sidebar", "splits", "browser"].includes(subcommand)) { + return filterOptions(third, [ + { cmd: "on", desc: "Enable this cmux area" }, + { cmd: "off", desc: "Disable this cmux area" }, + ], `cmux ${subcommand}`); + } + return []; + } + + if (command === "templates" && subcommand === "info" && parts.length <= 3) { + try { + const registry = loadRegistry(); + return Object.entries(registry.templates) + .filter(([id]) => id.startsWith(third)) + .map(([id, entry]) => ({ + value: `templates info ${id}`, + label: id, + description: entry.description, + })); + } catch { + return []; + } + } + + if (command === "extensions" && parts.length === 3 && ["enable", "disable", "info"].includes(subcommand)) { + return getExtensionCompletions(third, subcommand); + } + + if (command === "undo" && parts.length <= 2) { + return [{ value: "undo --force", label: "--force", description: "Skip confirmation prompt" }]; + } + + const nested = NESTED_COMPLETIONS[command]; + if (nested && parts.length <= 2) { + return filterOptions(subcommand, nested, command); + } + + return []; +} diff --git a/src/resources/extensions/gsd/commands/context.ts b/src/resources/extensions/gsd/commands/context.ts new file mode 100644 index 000000000..b8c95e608 --- /dev/null +++ b/src/resources/extensions/gsd/commands/context.ts @@ -0,0 +1,101 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { checkRemoteAutoSession, isAutoActive, isAutoPaused, stopAutoRemote } from "../auto.js"; +import { assertSafeDirectory } from "../validate-directory.js"; +import { resolveProjectRoot } from "../worktree.js"; +import { showNextAction } from "../../shared/mod.js"; +import { handleStatus } from "./handlers/core.js"; + +export interface GsdDispatchContext { + ctx: ExtensionCommandContext; + pi: ExtensionAPI; + trimmed: string; +} + +export function projectRoot(): string { + const cwd = process.cwd(); + const root = resolveProjectRoot(cwd); + if (root !== cwd) { + assertSafeDirectory(cwd); + } else { + assertSafeDirectory(root); + } + return root; +} + +export async function guardRemoteSession( + ctx: ExtensionCommandContext, + pi: ExtensionAPI, +): Promise { + if (isAutoActive() || isAutoPaused()) return true; + + const remote = checkRemoteAutoSession(projectRoot()); + if (!remote.running || !remote.pid) return true; + + const unitLabel = remote.unitType && remote.unitId + ? `${remote.unitType} (${remote.unitId})` + : "unknown unit"; + const unitsMsg = remote.completedUnits != null + ? `${remote.completedUnits} units completed` + : ""; + + const choice = await showNextAction(ctx, { + title: `Auto-mode is running in another terminal (PID ${remote.pid})`, + summary: [ + `Currently executing: ${unitLabel}`, + ...(unitsMsg ? [unitsMsg] : []), + ...(remote.startedAt ? [`Started: ${remote.startedAt}`] : []), + ], + actions: [ + { + id: "status", + label: "View status", + description: "Show the current GSD progress dashboard.", + recommended: true, + }, + { + id: "steer", + label: "Steer the session", + description: "Use /gsd steer to redirect the running session.", + }, + { + id: "stop", + label: "Stop remote session", + description: `Send SIGTERM to PID ${remote.pid} to stop it gracefully.`, + }, + { + id: "force", + label: "Force start (steal lock)", + description: "Start a new session, terminating the existing one.", + }, + ], + notYetMessage: "Run /gsd when ready.", + }); + + if (choice === "status") { + await handleStatus(ctx); + return false; + } + if (choice === "steer") { + ctx.ui.notify( + "Use /gsd steer to redirect the running auto-mode session.\n" + + "Example: /gsd steer Use Postgres instead of SQLite", + "info", + ); + return false; + } + if (choice === "stop") { + const result = stopAutoRemote(projectRoot()); + if (result.found) { + ctx.ui.notify(`Sent stop signal to auto-mode session (PID ${result.pid}). It will shut down gracefully.`, "info"); + } else if (result.error) { + ctx.ui.notify(`Failed to stop remote auto-mode: ${result.error}`, "error"); + } else { + ctx.ui.notify("Remote session is no longer running.", "info"); + } + return false; + } + + return choice === "force"; +} + diff --git a/src/resources/extensions/gsd/commands/dispatcher.ts b/src/resources/extensions/gsd/commands/dispatcher.ts new file mode 100644 index 000000000..9f28cbbaa --- /dev/null +++ b/src/resources/extensions/gsd/commands/dispatcher.ts @@ -0,0 +1,32 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { handleAutoCommand } from "./handlers/auto.js"; +import { handleCoreCommand } from "./handlers/core.js"; +import { handleOpsCommand } from "./handlers/ops.js"; +import { handleParallelCommand } from "./handlers/parallel.js"; +import { handleWorkflowCommand } from "./handlers/workflow.js"; + +export async function handleGSDCommand( + args: string, + ctx: ExtensionCommandContext, + pi: ExtensionAPI, +): Promise { + const trimmed = (typeof args === "string" ? args : "").trim(); + + const handlers = [ + () => handleCoreCommand(trimmed, ctx), + () => handleAutoCommand(trimmed, ctx, pi), + () => handleParallelCommand(trimmed, ctx, pi), + () => handleWorkflowCommand(trimmed, ctx, pi), + () => handleOpsCommand(trimmed, ctx, pi), + ]; + + for (const handler of handlers) { + if (await handler()) { + return; + } + } + + ctx.ui.notify(`Unknown: /gsd ${trimmed}. Run /gsd help for available commands.`, "warning"); +} + diff --git a/src/resources/extensions/gsd/commands/handlers/auto.ts b/src/resources/extensions/gsd/commands/handlers/auto.ts new file mode 100644 index 000000000..b261d8a34 --- /dev/null +++ b/src/resources/extensions/gsd/commands/handlers/auto.ts @@ -0,0 +1,74 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { enableDebug } from "../../debug-logger.js"; +import { getAutoDashboardData, isAutoActive, isAutoPaused, pauseAuto, startAuto, stopAuto, stopAutoRemote } from "../../auto.js"; +import { handleRate } from "../../commands-rate.js"; +import { guardRemoteSession, projectRoot } from "../context.js"; + +export async function handleAutoCommand(trimmed: string, ctx: ExtensionCommandContext, pi: ExtensionAPI): Promise { + if (trimmed === "next" || trimmed.startsWith("next ")) { + if (trimmed.includes("--dry-run")) { + const { handleDryRun } = await import("../../commands-maintenance.js"); + await handleDryRun(ctx, projectRoot()); + return true; + } + const verboseMode = trimmed.includes("--verbose"); + const debugMode = trimmed.includes("--debug"); + if (debugMode) enableDebug(projectRoot()); + if (!(await guardRemoteSession(ctx, pi))) return true; + await startAuto(ctx, pi, projectRoot(), verboseMode, { step: true }); + return true; + } + + if (trimmed === "auto" || trimmed.startsWith("auto ")) { + const verboseMode = trimmed.includes("--verbose"); + const debugMode = trimmed.includes("--debug"); + if (debugMode) enableDebug(projectRoot()); + if (!(await guardRemoteSession(ctx, pi))) return true; + await startAuto(ctx, pi, projectRoot(), verboseMode); + return true; + } + + if (trimmed === "stop") { + if (!isAutoActive() && !isAutoPaused()) { + const result = stopAutoRemote(projectRoot()); + if (result.found) { + ctx.ui.notify(`Sent stop signal to auto-mode session (PID ${result.pid}). It will shut down gracefully.`, "info"); + } else if (result.error) { + ctx.ui.notify(`Failed to stop remote auto-mode: ${result.error}`, "error"); + } else { + ctx.ui.notify("Auto-mode is not running.", "info"); + } + return true; + } + await stopAuto(ctx, pi, "User requested stop"); + return true; + } + + if (trimmed === "pause") { + if (!isAutoActive()) { + if (isAutoPaused()) { + ctx.ui.notify("Auto-mode is already paused. /gsd auto to resume.", "info"); + } else { + ctx.ui.notify("Auto-mode is not running.", "info"); + } + return true; + } + await pauseAuto(ctx, pi); + return true; + } + + if (trimmed === "rate" || trimmed.startsWith("rate ")) { + await handleRate(trimmed.replace(/^rate\s*/, "").trim(), ctx, projectRoot()); + return true; + } + + if (trimmed === "") { + if (!(await guardRemoteSession(ctx, pi))) return true; + await startAuto(ctx, pi, projectRoot(), false, { step: true }); + return true; + } + + return false; +} + diff --git a/src/resources/extensions/gsd/commands/handlers/core.ts b/src/resources/extensions/gsd/commands/handlers/core.ts new file mode 100644 index 000000000..3f759daf9 --- /dev/null +++ b/src/resources/extensions/gsd/commands/handlers/core.ts @@ -0,0 +1,274 @@ +import type { ExtensionCommandContext, ExtensionContext } from "@gsd/pi-coding-agent"; +import type { GSDState } from "../../types.js"; + +import { computeProgressScore, formatProgressLine } from "../../progress-score.js"; +import { loadEffectiveGSDPreferences, getGlobalGSDPreferencesPath, getProjectGSDPreferencesPath } from "../../preferences.js"; +import { ensurePreferencesFile, handlePrefs, handlePrefsMode, handlePrefsWizard } from "../../commands-prefs-wizard.js"; +import { runEnvironmentChecks } from "../../doctor-environment.js"; +import { deriveState } from "../../state.js"; +import { handleCmux } from "../../commands-cmux.js"; +import { projectRoot } from "../context.js"; + +export function showHelp(ctx: ExtensionCommandContext): void { + const lines = [ + "GSD — Get Shit Done\n", + "WORKFLOW", + " /gsd start Start a workflow template (bugfix, spike, feature, hotfix, etc.)", + " /gsd templates List available workflow templates [info ]", + " /gsd Run next unit in step mode (same as /gsd next)", + " /gsd next Execute next task, then pause [--dry-run] [--verbose]", + " /gsd auto Run all queued units continuously [--verbose]", + " /gsd stop Stop auto-mode gracefully", + " /gsd pause Pause auto-mode (preserves state, /gsd auto to resume)", + " /gsd discuss Start guided milestone/slice discussion", + " /gsd new-milestone Create milestone from headless context (used by gsd headless)", + "", + "VISIBILITY", + " /gsd status Show progress dashboard (Ctrl+Alt+G)", + " /gsd visualize Interactive 10-tab TUI (progress, timeline, deps, metrics, health, agent, changes, knowledge, captures, export)", + " /gsd queue Show queued/dispatched units and execution order", + " /gsd history View execution history [--cost] [--phase] [--model] [N]", + " /gsd changelog Show categorized release notes [version]", + "", + "COURSE CORRECTION", + " /gsd steer Apply user override to active work", + " /gsd capture Quick-capture a thought to CAPTURES.md", + " /gsd triage Classify and route pending captures", + " /gsd skip Prevent a unit from auto-mode dispatch", + " /gsd undo Revert last completed unit [--force]", + " /gsd park [id] Park a milestone — skip without deleting [reason]", + " /gsd unpark [id] Reactivate a parked milestone", + "", + "PROJECT KNOWLEDGE", + " /gsd knowledge Add rule, pattern, or lesson to KNOWLEDGE.md", + "", + "SETUP & CONFIGURATION", + " /gsd init Project init wizard — detect, configure, bootstrap .gsd/", + " /gsd setup Global setup status [llm|search|remote|keys|prefs]", + " /gsd mode Set workflow mode (solo/team) [global|project]", + " /gsd prefs Manage preferences [global|project|status|wizard|setup|import-claude]", + " /gsd cmux Manage cmux integration [status|on|off|notifications|sidebar|splits|browser]", + " /gsd config Set API keys for external tools", + " /gsd keys API key manager [list|add|remove|test|rotate|doctor]", + " /gsd hooks Show post-unit hook configuration", + " /gsd extensions Manage extensions [list|enable|disable|info]", + "", + "MAINTENANCE", + " /gsd doctor Diagnose and repair .gsd/ state [audit|fix|heal] [scope]", + " /gsd export Export milestone/slice results [--json|--markdown|--html] [--all]", + " /gsd cleanup Remove merged branches or snapshots [branches|snapshots]", + " /gsd migrate Migrate .planning/ (v1) to .gsd/ (v2) format", + " /gsd remote Control remote auto-mode [slack|discord|status|disconnect]", + " /gsd inspect Show SQLite DB diagnostics (schema, row counts, recent entries)", + " /gsd update Update GSD to the latest version via npm", + ]; + ctx.ui.notify(lines.join("\n"), "info"); +} + +export async function handleStatus(ctx: ExtensionCommandContext): Promise { + const basePath = projectRoot(); + const state = await deriveState(basePath); + + if (state.registry.length === 0) { + ctx.ui.notify("No GSD milestones found. Run /gsd to start.", "info"); + return; + } + + const { GSDDashboardOverlay } = await import("../../dashboard-overlay.js"); + const result = await ctx.ui.custom( + (tui, theme, _kb, done) => new GSDDashboardOverlay(tui, theme, () => done()), + { + overlay: true, + overlayOptions: { + width: "70%", + minWidth: 60, + maxHeight: "90%", + anchor: "center", + }, + }, + ); + + if (result === undefined) { + ctx.ui.notify(formatTextStatus(state), "info"); + } +} + +export async function fireStatusViaCommand(ctx: ExtensionContext): Promise { + await handleStatus(ctx as ExtensionCommandContext); +} + +export async function handleVisualize(ctx: ExtensionCommandContext): Promise { + if (!ctx.hasUI) { + ctx.ui.notify("Visualizer requires an interactive terminal.", "warning"); + return; + } + + const { GSDVisualizerOverlay } = await import("../../visualizer-overlay.js"); + const result = await ctx.ui.custom( + (tui, theme, _kb, done) => new GSDVisualizerOverlay(tui, theme, () => done()), + { + overlay: true, + overlayOptions: { + width: "80%", + minWidth: 80, + maxHeight: "90%", + anchor: "center", + }, + }, + ); + + if (result === undefined) { + ctx.ui.notify("Visualizer requires an interactive terminal. Use /gsd status for a text-based overview.", "warning"); + } +} + +export async function handleSetup(args: string, ctx: ExtensionCommandContext): Promise { + const { detectProjectState, hasGlobalSetup } = await import("../../detection.js"); + + const globalConfigured = hasGlobalSetup(); + const detection = detectProjectState(projectRoot()); + + const statusLines = ["GSD Setup Status\n"]; + statusLines.push(` Global preferences: ${globalConfigured ? "configured" : "not set"}`); + statusLines.push(` Project state: ${detection.state}`); + if (detection.projectSignals.primaryLanguage) { + statusLines.push(` Detected: ${detection.projectSignals.primaryLanguage}`); + } + + if (args === "llm" || args === "auth") { + ctx.ui.notify("Use /login to configure LLM authentication.", "info"); + return; + } + if (args === "search") { + ctx.ui.notify("Use /search-provider to configure web search.", "info"); + return; + } + if (args === "remote") { + ctx.ui.notify("Use /gsd remote to configure remote questions.", "info"); + return; + } + if (args === "keys") { + const { handleKeys } = await import("../../key-manager.js"); + await handleKeys("", ctx); + return; + } + if (args === "prefs") { + await ensurePreferencesFile(getGlobalGSDPreferencesPath(), ctx, "global"); + await handlePrefsWizard(ctx, "global"); + return; + } + + ctx.ui.notify(statusLines.join("\n"), "info"); + ctx.ui.notify( + "Available setup commands:\n" + + " /gsd setup llm — LLM authentication\n" + + " /gsd setup search — Web search provider\n" + + " /gsd setup remote — Remote questions (Discord/Slack/Telegram)\n" + + " /gsd setup keys — Tool API keys\n" + + " /gsd setup prefs — Global preferences wizard", + "info", + ); +} + +export async function handleCoreCommand(trimmed: string, ctx: ExtensionCommandContext): Promise { + if (trimmed === "help" || trimmed === "h" || trimmed === "?") { + showHelp(ctx); + return true; + } + if (trimmed === "status") { + await handleStatus(ctx); + return true; + } + if (trimmed === "visualize") { + await handleVisualize(ctx); + return true; + } + if (trimmed === "widget" || trimmed.startsWith("widget ")) { + const { cycleWidgetMode, setWidgetMode, getWidgetMode } = await import("../../auto-dashboard.js"); + const arg = trimmed.replace(/^widget\s*/, "").trim(); + if (arg === "full" || arg === "small" || arg === "min" || arg === "off") { + setWidgetMode(arg); + } else { + cycleWidgetMode(); + } + ctx.ui.notify(`Widget: ${getWidgetMode()}`, "info"); + return true; + } + if (trimmed === "mode" || trimmed.startsWith("mode ")) { + const modeArgs = trimmed.replace(/^mode\s*/, "").trim(); + const scope = modeArgs === "project" ? "project" : "global"; + const path = scope === "project" ? getProjectGSDPreferencesPath() : getGlobalGSDPreferencesPath(); + await ensurePreferencesFile(path, ctx, scope); + await handlePrefsMode(ctx, scope); + return true; + } + if (trimmed === "prefs" || trimmed.startsWith("prefs ")) { + await handlePrefs(trimmed.replace(/^prefs\s*/, "").trim(), ctx); + return true; + } + if (trimmed === "cmux" || trimmed.startsWith("cmux ")) { + await handleCmux(trimmed.replace(/^cmux\s*/, "").trim(), ctx); + return true; + } + if (trimmed === "setup" || trimmed.startsWith("setup ")) { + await handleSetup(trimmed.replace(/^setup\s*/, "").trim(), ctx); + return true; + } + return false; +} + +export function formatTextStatus(state: GSDState): string { + const lines: string[] = ["GSD Status\n"]; + lines.push(formatProgressLine(computeProgressScore())); + lines.push(""); + lines.push(`Phase: ${state.phase}`); + + if (state.activeMilestone) { + lines.push(`Active milestone: ${state.activeMilestone.id} — ${state.activeMilestone.title}`); + } + if (state.activeSlice) { + lines.push(`Active slice: ${state.activeSlice.id} — ${state.activeSlice.title}`); + } + if (state.activeTask) { + lines.push(`Active task: ${state.activeTask.id} — ${state.activeTask.title}`); + } + if (state.progress) { + const { milestones, slices, tasks } = state.progress; + const parts: string[] = [`milestones ${milestones.done}/${milestones.total}`]; + if (slices) parts.push(`slices ${slices.done}/${slices.total}`); + if (tasks) parts.push(`tasks ${tasks.done}/${tasks.total}`); + lines.push(`Progress: ${parts.join(", ")}`); + } + if (state.nextAction) { + lines.push(`Next: ${state.nextAction}`); + } + if (state.blockers.length > 0) { + lines.push(`Blockers: ${state.blockers.join("; ")}`); + } + if (state.registry.length > 0) { + lines.push(""); + lines.push("Milestones:"); + for (const milestone of state.registry) { + const icon = milestone.status === "complete" + ? "✓" + : milestone.status === "active" + ? "▶" + : milestone.status === "parked" + ? "⏸" + : "○"; + lines.push(` ${icon} ${milestone.id}: ${milestone.title} (${milestone.status})`); + } + } + + const envResults = runEnvironmentChecks(projectRoot()); + const envIssues = envResults.filter((result) => result.status !== "ok"); + if (envIssues.length > 0) { + lines.push(""); + lines.push("Environment:"); + for (const issue of envIssues) { + lines.push(` ${issue.status === "error" ? "✗" : "⚠"} ${issue.message}`); + } + } + + return lines.join("\n"); +} diff --git a/src/resources/extensions/gsd/commands/handlers/ops.ts b/src/resources/extensions/gsd/commands/handlers/ops.ts new file mode 100644 index 000000000..c28574196 --- /dev/null +++ b/src/resources/extensions/gsd/commands/handlers/ops.ts @@ -0,0 +1,169 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { enableDebug } from "../../debug-logger.js"; +import { dispatchDirectPhase } from "../../auto-direct-dispatch.js"; +import { handleConfig } from "../../commands-config.js"; +import { handleDoctor, handleCapture, handleKnowledge, handleRunHook, handleSkillHealth, handleSteer, handleTriage, handleUpdate } from "../../commands-handlers.js"; +import { handleInspect } from "../../commands-inspect.js"; +import { handleLogs } from "../../commands-logs.js"; +import { handleCleanupBranches, handleCleanupSnapshots, handleSkip } from "../../commands-maintenance.js"; +import { handleExport } from "../../export.js"; +import { handleHistory } from "../../history.js"; +import { handleUndo } from "../../undo.js"; +import { handleRemote } from "../../../remote-questions/mod.js"; +import { projectRoot } from "../context.js"; + +export async function handleOpsCommand(trimmed: string, ctx: ExtensionCommandContext, pi: ExtensionAPI): Promise { + if (trimmed === "init") { + const { detectProjectState } = await import("../../detection.js"); + const { handleReinit, showProjectInit } = await import("../../init-wizard.js"); + const basePath = projectRoot(); + const detection = detectProjectState(basePath); + if (detection.state === "v2-gsd" || detection.state === "v2-gsd-empty") { + await handleReinit(ctx, detection); + } else { + await showProjectInit(ctx, pi, basePath, detection); + } + return true; + } + if (trimmed === "keys" || trimmed.startsWith("keys ")) { + const { handleKeys } = await import("../../key-manager.js"); + await handleKeys(trimmed.replace(/^keys\s*/, "").trim(), ctx); + return true; + } + if (trimmed === "doctor" || trimmed.startsWith("doctor ")) { + await handleDoctor(trimmed.replace(/^doctor\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "logs" || trimmed.startsWith("logs ")) { + await handleLogs(trimmed.replace(/^logs\s*/, "").trim(), ctx); + return true; + } + if (trimmed === "forensics" || trimmed.startsWith("forensics ")) { + const { handleForensics } = await import("../../forensics.js"); + await handleForensics(trimmed.replace(/^forensics\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "changelog" || trimmed.startsWith("changelog ")) { + const { handleChangelog } = await import("../../changelog.js"); + await handleChangelog(trimmed.replace(/^changelog\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "history" || trimmed.startsWith("history ")) { + await handleHistory(trimmed.replace(/^history\s*/, "").trim(), ctx, projectRoot()); + return true; + } + if (trimmed === "undo" || trimmed.startsWith("undo ")) { + await handleUndo(trimmed.replace(/^undo\s*/, "").trim(), ctx, pi, projectRoot()); + return true; + } + if (trimmed.startsWith("skip ")) { + await handleSkip(trimmed.replace(/^skip\s*/, "").trim(), ctx, projectRoot()); + return true; + } + if (trimmed === "export" || trimmed.startsWith("export ")) { + await handleExport(trimmed.replace(/^export\s*/, "").trim(), ctx, projectRoot()); + return true; + } + if (trimmed === "cleanup") { + await handleCleanupBranches(ctx, projectRoot()); + await handleCleanupSnapshots(ctx, projectRoot()); + return true; + } + if (trimmed === "cleanup branches") { + await handleCleanupBranches(ctx, projectRoot()); + return true; + } + if (trimmed === "cleanup snapshots") { + await handleCleanupSnapshots(ctx, projectRoot()); + return true; + } + if (trimmed.startsWith("capture ") || trimmed === "capture") { + await handleCapture(trimmed.replace(/^capture\s*/, "").trim(), ctx); + return true; + } + if (trimmed === "triage") { + await handleTriage(ctx, pi, process.cwd()); + return true; + } + if (trimmed === "config") { + await handleConfig(ctx); + return true; + } + if (trimmed === "hooks") { + const { formatHookStatus } = await import("../../post-unit-hooks.js"); + ctx.ui.notify(formatHookStatus(), "info"); + return true; + } + if (trimmed === "skill-health" || trimmed.startsWith("skill-health ")) { + await handleSkillHealth(trimmed.replace(/^skill-health\s*/, "").trim(), ctx); + return true; + } + if (trimmed.startsWith("run-hook ")) { + await handleRunHook(trimmed.replace(/^run-hook\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "run-hook") { + ctx.ui.notify(`Usage: /gsd run-hook + +Unit types: + execute-task - Task execution (unit-id: M001/S01/T01) + plan-slice - Slice planning (unit-id: M001/S01) + research-milestone - Milestone research (unit-id: M001) + complete-slice - Slice completion (unit-id: M001/S01) + complete-milestone - Milestone completion (unit-id: M001) + +Examples: + /gsd run-hook code-review execute-task M001/S01/T01 + /gsd run-hook lint-check plan-slice M001/S01`, "warning"); + return true; + } + if (trimmed.startsWith("steer ")) { + await handleSteer(trimmed.replace(/^steer\s+/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "steer") { + ctx.ui.notify("Usage: /gsd steer . Example: /gsd steer Use Postgres instead of SQLite", "warning"); + return true; + } + if (trimmed.startsWith("knowledge ")) { + await handleKnowledge(trimmed.replace(/^knowledge\s+/, "").trim(), ctx); + return true; + } + if (trimmed === "knowledge") { + ctx.ui.notify("Usage: /gsd knowledge . Example: /gsd knowledge rule Use real DB for integration tests", "warning"); + return true; + } + if (trimmed === "migrate" || trimmed.startsWith("migrate ")) { + const { handleMigrate } = await import("../../migrate/command.js"); + await handleMigrate(trimmed.replace(/^migrate\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "remote" || trimmed.startsWith("remote ")) { + await handleRemote(trimmed.replace(/^remote\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "dispatch" || trimmed.startsWith("dispatch ")) { + const phase = trimmed.replace(/^dispatch\s*/, "").trim(); + if (!phase) { + ctx.ui.notify("Usage: /gsd dispatch (research|plan|execute|complete|reassess|uat|replan)", "warning"); + return true; + } + await dispatchDirectPhase(ctx, pi, phase, projectRoot()); + return true; + } + if (trimmed === "inspect") { + await handleInspect(ctx); + return true; + } + if (trimmed === "update") { + await handleUpdate(ctx); + return true; + } + if (trimmed === "extensions" || trimmed.startsWith("extensions ")) { + const { handleExtensions } = await import("../../commands-extensions.js"); + await handleExtensions(trimmed.replace(/^extensions\s*/, "").trim(), ctx); + return true; + } + return false; +} diff --git a/src/resources/extensions/gsd/commands/handlers/parallel.ts b/src/resources/extensions/gsd/commands/handlers/parallel.ts new file mode 100644 index 000000000..0aa27c385 --- /dev/null +++ b/src/resources/extensions/gsd/commands/handlers/parallel.ts @@ -0,0 +1,118 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { + getOrchestratorState, + getWorkerStatuses, + isParallelActive, + pauseWorker, + prepareParallelStart, + resumeWorker, + startParallel, + stopParallel, +} from "../../parallel-orchestrator.js"; +import { formatEligibilityReport } from "../../parallel-eligibility.js"; +import { formatMergeResults, mergeAllCompleted, mergeCompletedMilestone } from "../../parallel-merge.js"; +import { loadEffectiveGSDPreferences, resolveParallelConfig } from "../../preferences.js"; +import { projectRoot } from "../context.js"; + +export async function handleParallelCommand(trimmed: string, _ctx: ExtensionCommandContext, pi: ExtensionAPI): Promise { + if (!trimmed.startsWith("parallel")) return false; + + const parallelArgs = trimmed.slice("parallel".length).trim(); + const [subcommand = "", ...restParts] = parallelArgs.split(/\s+/); + const rest = restParts.join(" "); + + if (subcommand === "start" || subcommand === "") { + const loaded = loadEffectiveGSDPreferences(); + const config = resolveParallelConfig(loaded?.preferences); + if (!config.enabled) { + pi.sendMessage({ + customType: "gsd-parallel", + content: "Parallel mode is not enabled. Set `parallel.enabled: true` in your preferences.", + display: false, + }); + return true; + } + const candidates = await prepareParallelStart(projectRoot(), loaded?.preferences); + const report = formatEligibilityReport(candidates); + if (candidates.eligible.length === 0) { + pi.sendMessage({ customType: "gsd-parallel", content: `${report}\n\nNo milestones are eligible for parallel execution.`, display: false }); + return true; + } + const result = await startParallel( + projectRoot(), + candidates.eligible.map((candidate) => candidate.milestoneId), + loaded?.preferences, + ); + const lines = ["Parallel orchestration started.", `Workers: ${result.started.join(", ")}`]; + if (result.errors.length > 0) { + lines.push(`Errors: ${result.errors.map((entry) => `${entry.mid}: ${entry.error}`).join("; ")}`); + } + pi.sendMessage({ customType: "gsd-parallel", content: `${report}\n\n${lines.join("\n")}`, display: false }); + return true; + } + + if (subcommand === "status") { + if (!isParallelActive()) { + pi.sendMessage({ customType: "gsd-parallel", content: "No parallel orchestration is currently active.", display: false }); + return true; + } + const workers = getWorkerStatuses(); + const lines = ["# Parallel Workers\n"]; + for (const worker of workers) { + lines.push(`- **${worker.milestoneId}** (${worker.title}) — ${worker.state} — ${worker.completedUnits} units — $${worker.cost.toFixed(2)}`); + } + const state = getOrchestratorState(); + if (state) { + lines.push(`\nTotal cost: $${state.totalCost.toFixed(2)}`); + } + pi.sendMessage({ customType: "gsd-parallel", content: lines.join("\n"), display: false }); + return true; + } + + if (subcommand === "stop") { + const milestoneId = rest.trim() || undefined; + await stopParallel(projectRoot(), milestoneId); + pi.sendMessage({ customType: "gsd-parallel", content: milestoneId ? `Stopped worker for ${milestoneId}.` : "All parallel workers stopped.", display: false }); + return true; + } + + if (subcommand === "pause") { + const milestoneId = rest.trim() || undefined; + pauseWorker(projectRoot(), milestoneId); + pi.sendMessage({ customType: "gsd-parallel", content: milestoneId ? `Paused worker for ${milestoneId}.` : "All parallel workers paused.", display: false }); + return true; + } + + if (subcommand === "resume") { + const milestoneId = rest.trim() || undefined; + resumeWorker(projectRoot(), milestoneId); + pi.sendMessage({ customType: "gsd-parallel", content: milestoneId ? `Resumed worker for ${milestoneId}.` : "All parallel workers resumed.", display: false }); + return true; + } + + if (subcommand === "merge") { + const milestoneId = rest.trim() || undefined; + if (milestoneId) { + const result = await mergeCompletedMilestone(projectRoot(), milestoneId); + pi.sendMessage({ customType: "gsd-parallel", content: formatMergeResults([result]), display: false }); + return true; + } + const workers = getWorkerStatuses(); + if (workers.length === 0) { + pi.sendMessage({ customType: "gsd-parallel", content: "No parallel workers to merge.", display: false }); + return true; + } + const results = await mergeAllCompleted(projectRoot(), workers); + pi.sendMessage({ customType: "gsd-parallel", content: formatMergeResults(results), display: false }); + return true; + } + + pi.sendMessage({ + customType: "gsd-parallel", + content: `Unknown parallel subcommand "${subcommand}". Usage: /gsd parallel [start|status|stop|pause|resume|merge]`, + display: false, + }); + return true; +} + diff --git a/src/resources/extensions/gsd/commands/handlers/workflow.ts b/src/resources/extensions/gsd/commands/handlers/workflow.ts new file mode 100644 index 000000000..a74bc3f07 --- /dev/null +++ b/src/resources/extensions/gsd/commands/handlers/workflow.ts @@ -0,0 +1,109 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { existsSync, readFileSync, unlinkSync } from "node:fs"; +import { join } from "node:path"; + +import { handleQuick } from "../../quick.js"; +import { showDiscuss, showHeadlessMilestoneCreation, showQueue } from "../../guided-flow.js"; +import { handleStart, handleTemplates } from "../../commands-workflow-templates.js"; +import { gsdRoot } from "../../paths.js"; +import { deriveState } from "../../state.js"; +import { isParked, parkMilestone, unparkMilestone } from "../../milestone-actions.js"; +import { loadEffectiveGSDPreferences } from "../../preferences.js"; +import { nextMilestoneId } from "../../milestone-ids.js"; +import { findMilestoneIds } from "../../guided-flow.js"; +import { projectRoot } from "../context.js"; + +export async function handleWorkflowCommand(trimmed: string, ctx: ExtensionCommandContext, pi: ExtensionAPI): Promise { + if (trimmed === "queue") { + await showQueue(ctx, pi, projectRoot()); + return true; + } + if (trimmed === "discuss") { + await showDiscuss(ctx, pi, projectRoot()); + return true; + } + if (trimmed === "quick" || trimmed.startsWith("quick ")) { + await handleQuick(trimmed.replace(/^quick\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "new-milestone") { + const basePath = projectRoot(); + const headlessContextPath = join(gsdRoot(basePath), "runtime", "headless-context.md"); + if (existsSync(headlessContextPath)) { + const seedContext = readFileSync(headlessContextPath, "utf-8"); + try { unlinkSync(headlessContextPath); } catch { /* non-fatal */ } + await showHeadlessMilestoneCreation(ctx, pi, basePath, seedContext); + } else { + const { showSmartEntry } = await import("../../guided-flow.js"); + await showSmartEntry(ctx, pi, basePath); + } + return true; + } + if (trimmed === "start" || trimmed.startsWith("start ")) { + await handleStart(trimmed.replace(/^start\s*/, "").trim(), ctx, pi); + return true; + } + if (trimmed === "templates" || trimmed.startsWith("templates ")) { + await handleTemplates(trimmed.replace(/^templates\s*/, "").trim(), ctx); + return true; + } + if (trimmed === "park" || trimmed.startsWith("park ")) { + const basePath = projectRoot(); + const arg = trimmed.replace(/^park\s*/, "").trim(); + let targetId = arg; + if (!targetId) { + const state = await deriveState(basePath); + if (!state.activeMilestone) { + ctx.ui.notify("No active milestone to park.", "warning"); + return true; + } + targetId = state.activeMilestone.id; + } + if (isParked(basePath, targetId)) { + ctx.ui.notify(`${targetId} is already parked. Use /gsd unpark ${targetId} to reactivate.`, "info"); + return true; + } + const reasonParts = arg.replace(targetId, "").trim().replace(/^["']|["']$/g, ""); + const reason = reasonParts || "Parked via /gsd park"; + const success = parkMilestone(basePath, targetId, reason); + ctx.ui.notify( + success ? `Parked ${targetId}. Run /gsd unpark ${targetId} to reactivate.` : `Could not park ${targetId} — milestone not found.`, + success ? "info" : "warning", + ); + return true; + } + if (trimmed === "unpark" || trimmed.startsWith("unpark ")) { + const basePath = projectRoot(); + const arg = trimmed.replace(/^unpark\s*/, "").trim(); + let targetId = arg; + if (!targetId) { + const state = await deriveState(basePath); + const parkedEntries = state.registry.filter((entry) => entry.status === "parked"); + if (parkedEntries.length === 0) { + ctx.ui.notify("No parked milestones.", "info"); + return true; + } + if (parkedEntries.length === 1) { + targetId = parkedEntries[0].id; + } else { + ctx.ui.notify(`Parked milestones: ${parkedEntries.map((entry) => entry.id).join(", ")}. Specify which to unpark: /gsd unpark `, "info"); + return true; + } + } + const success = unparkMilestone(basePath, targetId); + ctx.ui.notify( + success ? `Unparked ${targetId}. It will resume its normal position in the queue.` : `Could not unpark ${targetId} — milestone not found or not parked.`, + success ? "info" : "warning", + ); + return true; + } + return false; +} + +export function getNextMilestoneId(basePath: string): string { + const milestoneIds = findMilestoneIds(basePath); + const uniqueIds = !!loadEffectiveGSDPreferences()?.preferences?.unique_milestone_ids; + return nextMilestoneId(milestoneIds, uniqueIds); +} + diff --git a/src/resources/extensions/gsd/commands/index.ts b/src/resources/extensions/gsd/commands/index.ts new file mode 100644 index 000000000..38f55e0bb --- /dev/null +++ b/src/resources/extensions/gsd/commands/index.ts @@ -0,0 +1,14 @@ +import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent"; + +import { GSD_COMMAND_DESCRIPTION, getGsdArgumentCompletions } from "./catalog.js"; + +export function registerGSDCommand(pi: ExtensionAPI): void { + pi.registerCommand("gsd", { + description: GSD_COMMAND_DESCRIPTION, + getArgumentCompletions: getGsdArgumentCompletions, + handler: async (args: string, ctx: ExtensionCommandContext) => { + const { handleGSDCommand } = await import("./dispatcher.js"); + await handleGSDCommand(args, ctx, pi); + }, + }); +} diff --git a/src/resources/extensions/gsd/health-widget.ts b/src/resources/extensions/gsd/health-widget.ts index 03afa7d3f..fa63e6677 100644 --- a/src/resources/extensions/gsd/health-widget.ts +++ b/src/resources/extensions/gsd/health-widget.ts @@ -15,7 +15,8 @@ import { runEnvironmentChecks } from "./doctor-environment.js"; import { loadEffectiveGSDPreferences } from "./preferences.js"; import { loadLedgerFromDisk, getProjectTotals } from "./metrics.js"; import { describeNextUnit, estimateTimeRemaining, updateSliceProgressCache } from "./auto-dashboard.js"; -import { projectRoot } from "./commands.js"; +import { projectRoot } from "./commands/context.js"; +import { deriveState, invalidateStateCache } from "./state.js"; import { buildHealthLines, detectHealthWidgetProjectState, diff --git a/src/resources/extensions/gsd/index.ts b/src/resources/extensions/gsd/index.ts index 4114639cc..13e6dc97c 100644 --- a/src/resources/extensions/gsd/index.ts +++ b/src/resources/extensions/gsd/index.ts @@ -1,1315 +1,13 @@ -/** - * GSD Extension — /gsd - * - * One command, one wizard. Reads state from disk, shows contextual options, - * dispatches through GSD-WORKFLOW.md. The LLM does the rest. - * - * Auto-mode: /gsd auto loops fresh sessions until milestone complete. - * - * Commands: - * /gsd — contextual wizard (smart entry point) - * /gsd auto — start auto-mode (fresh session per unit) - * /gsd stop — stop auto-mode gracefully - * /gsd status — progress dashboard - * - * Hooks: - * before_agent_start — inject GSD system context for GSD projects - * agent_end — auto-mode advancement - * session_before_compact — save continue.md OR block during auto - */ +import type { ExtensionAPI } from "@gsd/pi-coding-agent"; -import type { - ExtensionAPI, - ExtensionCommandContext, - ExtensionContext, -} from "@gsd/pi-coding-agent"; -import { createBashTool, createWriteTool, createReadTool, createEditTool, isToolCallEventType } from "@gsd/pi-coding-agent"; -import { Type } from "@sinclair/typebox"; +export { + isDepthVerified, + isQueuePhaseActive, + setQueuePhaseActive, + shouldBlockContextWrite, +} from "./bootstrap/write-gate.js"; -import { debugLog, debugTime } from "./debug-logger.js"; -import { registerGSDCommand } from "./commands.js"; -import { loadToolApiKeys } from "./commands-config.js"; -import { registerExitCommand } from "./exit-command.js"; -import { registerWorktreeCommand, getWorktreeOriginalCwd, getActiveWorktreeName } from "./worktree-command.js"; -import { getActiveAutoWorktreeContext } from "./auto-worktree.js"; -import { saveFile, formatContinue, loadFile, parseContinue, parseSummary, loadActiveOverrides, formatOverridesSection } from "./files.js"; -import { loadPrompt } from "./prompt-loader.js"; -import { deriveState } from "./state.js"; -import { isAutoActive, isAutoPaused, pauseAuto, getAutoDashboardData, getAutoModeStartModel, markToolStart, markToolEnd } from "./auto.js"; -import { isSessionSwitchInFlight, resolveAgentEnd } from "./auto-loop.js"; -import { saveActivityLog } from "./activity-log.js"; -import { checkAutoStartAfterDiscuss, getDiscussionMilestoneId, findMilestoneIds, nextMilestoneId } from "./guided-flow.js"; -import { GSDDashboardOverlay } from "./dashboard-overlay.js"; -import { - loadEffectiveGSDPreferences, - renderPreferencesForSystemPrompt, - resolveAllSkillReferences, - resolveModelWithFallbacksForUnit, - getNextFallbackModel, - isTransientNetworkError, -} from "./preferences.js"; -import { hasSkillSnapshot, detectNewSkills, formatSkillsXml } from "./skill-discovery.js"; -import { - resolveSlicePath, resolveSliceFile, resolveTaskFile, resolveTaskFiles, resolveTasksDir, - relSliceFile, relSlicePath, relTaskFile, - buildSliceFileName, buildMilestoneFileName, gsdRoot, resolveMilestonePath, - resolveGsdRootFile, -} from "./paths.js"; -import { Key } from "@gsd/pi-tui"; -import { join } from "node:path"; -import { existsSync, readFileSync } from "node:fs"; -import { homedir } from "node:os"; -import { shortcutDesc } from "../shared/mod.js"; - -const gsdHome = process.env.GSD_HOME || join(homedir(), ".gsd"); -import { Text } from "@gsd/pi-tui"; -import { pauseAutoForProviderError, classifyProviderError } from "./provider-error-pause.js"; -import { toPosixPath } from "../shared/mod.js"; -import { isParallelActive, shutdownParallel } from "./parallel-orchestrator.js"; -import { DEFAULT_BASH_TIMEOUT_SECS } from "./constants.js"; -import { markCmuxPromptShown, shouldPromptToEnableCmux } from "../cmux/index.js"; - -// ── Agent Instructions (DEPRECATED) ────────────────────────────────────── -// agent-instructions.md is deprecated. Use AGENTS.md or CLAUDE.md instead. -// Pi core natively supports AGENTS.md (with CLAUDE.md fallback) per directory. - -function warnDeprecatedAgentInstructions(): void { - const paths = [ - join(gsdHome, "agent-instructions.md"), - join(process.cwd(), ".gsd", "agent-instructions.md"), - ]; - for (const p of paths) { - if (existsSync(p)) { - console.warn( - `[GSD] DEPRECATED: ${p} is no longer loaded. ` + - `Migrate your instructions to AGENTS.md (or CLAUDE.md) in the same directory. ` + - `See https://github.com/gsd-build/GSD-2/issues/1492`, - ); - } - } -} - -// ── Depth verification state ────────────────────────────────────────────── -let depthVerificationDone = false; - -// ── DB lazy-open helper ─────────────────────────────────────────────────── -// In manual sessions (no auto-mode), the DB is never opened by bootstrapAutoSession. -// This helper ensures the DB is lazily opened on first tool call that needs it. -async function ensureDbOpen(): Promise { - try { - const db = await import("./gsd-db.js"); - if (db.isDbAvailable()) return true; - const dbPath = join(process.cwd(), ".gsd", "gsd.db"); - if (existsSync(dbPath)) { - return db.openDatabase(dbPath); - } - return false; - } catch { - return false; - } -} - -// ── Queue phase tracking ────────────────────────────────────────────────── -// When true, the LLM is in a queue flow writing CONTEXT.md files. -// The write-gate applies during queue flows just like discussion flows. -let activeQueuePhase = false; - -// ── Network error retry counters ────────────────────────────────────────── -// Tracks per-model retry attempts for transient network errors. -// Cleared when a model switch occurs or retries are exhausted. -const networkRetryCounters = new Map(); -const MAX_TRANSIENT_AUTO_RESUMES = 3; -let consecutiveTransientErrors = 0; - -export function isDepthVerified(): boolean { - return depthVerificationDone; -} - -/** Check whether a queue phase is active. */ -export function isQueuePhaseActive(): boolean { - return activeQueuePhase; -} - -/** Set the queue phase state — called from guided-flow-queue.ts on dispatch. */ -export function setQueuePhaseActive(active: boolean): void { - activeQueuePhase = active; -} - -// ── Write-gate: block CONTEXT.md writes during discussion without depth verification ── -const MILESTONE_CONTEXT_RE = /M\d+(?:-[a-z0-9]{6})?-CONTEXT\.md$/; - -export function shouldBlockContextWrite( - toolName: string, - inputPath: string, - milestoneId: string | null, - depthVerified: boolean, - queuePhaseActive?: boolean, -): { block: boolean; reason?: string } { - if (toolName !== "write") return { block: false }; - - // Gate applies during both discussion (milestoneId set) and queue (queuePhaseActive) flows - const inDiscussion = milestoneId !== null; - const inQueue = queuePhaseActive ?? false; - if (!inDiscussion && !inQueue) return { block: false }; - - if (!MILESTONE_CONTEXT_RE.test(inputPath)) return { block: false }; - if (depthVerified) return { block: false }; - - return { - block: true, - reason: `Blocked: Cannot write to milestone CONTEXT.md during discussion phase without depth verification. Call ask_user_questions with question id "depth_verification" first to confirm discussion depth before writing context.`, - }; -} - -// ── ASCII logo ──────────────────────────────────────────────────────────── -const GSD_LOGO_LINES = [ - " ██████╗ ███████╗██████╗ ", - " ██╔════╝ ██╔════╝██╔══██╗", - " ██║ ███╗███████╗██║ ██║", - " ██║ ██║╚════██║██║ ██║", - " ╚██████╔╝███████║██████╔╝", - " ╚═════╝ ╚══════╝╚═════╝ ", -]; - -export default function (pi: ExtensionAPI) { - registerGSDCommand(pi); - registerWorktreeCommand(pi); - registerExitCommand(pi); - - // ── EPIPE guard — prevent crash when stdout/stderr pipe closes unexpectedly ── - // Node.js throws a fatal `Error: write EPIPE` when the parent process closes - // its end of the stdio pipe (e.g. during shell/IPC teardown) while auto-mode - // is still writing diagnostics. Catching this here gives auto-mode a clean - // chance to persist state and pause instead of crashing (see issue #739). - if (!process.listeners("uncaughtException").some(l => l.name === "_gsdEpipeGuard")) { - const _gsdEpipeGuard = (err: Error): void => { - if ((err as NodeJS.ErrnoException).code === "EPIPE") { - // Pipe closed — nothing we can write; just exit cleanly - process.exit(0); - } - if ((err as NodeJS.ErrnoException).code === "ENOENT" && - (err as any).syscall?.startsWith("spawn")) { - // spawn ENOENT — command not found (e.g., npx on Windows). - // This surfaces as an uncaught exception from child_process but - // is not a fatal process error. Log and continue instead of - // crashing auto-mode (#1384). - process.stderr.write(`[gsd] spawn ENOENT: ${(err as any).path ?? "unknown"} — command not found\n`); - return; - } - // Re-throw anything that isn't EPIPE/ENOENT so real crashes still surface - throw err; - }; - process.on("uncaughtException", _gsdEpipeGuard); - } - - // ── /kill — immediate exit (bypass cleanup) ───────────────────────────── - pi.registerCommand("kill", { - description: "Exit GSD immediately (no cleanup)", - handler: async (_args: string, _ctx: ExtensionCommandContext) => { - process.exit(0); - }, - }); - - // ── Dynamic-cwd bash tool with default timeout ──────────────────────── - // The built-in bash tool captures cwd at startup. This replacement uses - // a spawnHook to read process.cwd() dynamically so that process.chdir() - // (used by /worktree switch) propagates to shell commands. - // - // The upstream SDK's bash tool has no default timeout — if the LLM omits - // the timeout parameter, commands run indefinitely, causing hangs on - // Windows where process killing is unreliable (see #40). We wrap execute - // to inject a 120-second default when no timeout is provided. - const baseBash = createBashTool(process.cwd(), { - spawnHook: (ctx) => ({ ...ctx, cwd: process.cwd() }), - }); - const dynamicBash = { - ...baseBash, - execute: async ( - toolCallId: string, - params: { command: string; timeout?: number }, - signal?: AbortSignal, - onUpdate?: any, - ctx?: any, - ) => { - const paramsWithTimeout = { - ...params, - timeout: params.timeout ?? DEFAULT_BASH_TIMEOUT_SECS, - }; - return (baseBash as any).execute(toolCallId, paramsWithTimeout, signal, onUpdate, ctx); - }, - }; - pi.registerTool(dynamicBash as any); - - // ── Dynamic-cwd file tools (write, read, edit) ──────────────────────── - // The built-in file tools capture cwd at startup. When process.chdir() - // moves us into a worktree, relative paths still resolve against the - // original launch directory. These replacements delegate to freshly- - // created tools on each call so that process.cwd() is read dynamically. - const baseWrite = createWriteTool(process.cwd()); - const dynamicWrite = { - ...baseWrite, - execute: async ( - toolCallId: string, - params: { path: string; content: string }, - signal?: AbortSignal, - onUpdate?: any, - ctx?: any, - ) => { - const fresh = createWriteTool(process.cwd()); - return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx); - }, - }; - pi.registerTool(dynamicWrite as any); - - const baseRead = createReadTool(process.cwd()); - const dynamicRead = { - ...baseRead, - execute: async ( - toolCallId: string, - params: { path: string; offset?: number; limit?: number }, - signal?: AbortSignal, - onUpdate?: any, - ctx?: any, - ) => { - const fresh = createReadTool(process.cwd()); - return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx); - }, - }; - pi.registerTool(dynamicRead as any); - - const baseEdit = createEditTool(process.cwd()); - const dynamicEdit = { - ...baseEdit, - execute: async ( - toolCallId: string, - params: { path: string; oldText: string; newText: string }, - signal?: AbortSignal, - onUpdate?: any, - ctx?: any, - ) => { - const fresh = createEditTool(process.cwd()); - return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx); - }, - }; - pi.registerTool(dynamicEdit as any); - - // ── Structured LLM tools — DB-first write path (R014) ────────────────── - - pi.registerTool({ - name: "gsd_save_decision", - label: "Save Decision", - description: - "Record a project decision to the GSD database and regenerate DECISIONS.md. " + - "Decision IDs are auto-assigned — never provide an ID manually.", - promptSnippet: "Record a project decision to the GSD database (auto-assigns ID, regenerates DECISIONS.md)", - promptGuidelines: [ - "Use gsd_save_decision when recording an architectural, pattern, library, or observability decision.", - "Decision IDs are auto-assigned (D001, D002, ...) — never guess or provide an ID.", - "All fields except revisable and when_context are required.", - "The tool writes to the DB and regenerates .gsd/DECISIONS.md automatically.", - ], - parameters: Type.Object({ - scope: Type.String({ description: "Scope of the decision (e.g. 'architecture', 'library', 'observability')" }), - decision: Type.String({ description: "What is being decided" }), - choice: Type.String({ description: "The choice made" }), - rationale: Type.String({ description: "Why this choice was made" }), - revisable: Type.Optional(Type.String({ description: "Whether this can be revisited (default: 'Yes')" })), - when_context: Type.Optional(Type.String({ description: "When/context for the decision (e.g. milestone ID)" })), - }), - async execute(_toolCallId, params, _signal, _onUpdate, _ctx) { - // Ensure DB is open (lazy-open on first tool call in manual sessions) - const dbAvailable = await ensureDbOpen(); - - if (!dbAvailable) { - return { - content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot save decision." }], - isError: true, - details: { operation: "save_decision", error: "db_unavailable" }, - }; - } - - try { - const { saveDecisionToDb } = await import("./db-writer.js"); - const { id } = await saveDecisionToDb( - { - scope: params.scope, - decision: params.decision, - choice: params.choice, - rationale: params.rationale, - revisable: params.revisable, - when_context: params.when_context, - }, - process.cwd(), - ); - return { - content: [{ type: "text" as const, text: `Saved decision ${id}` }], - details: { operation: "save_decision", id }, - }; - } catch (err) { - const msg = err instanceof Error ? err.message : String(err); - process.stderr.write(`gsd-db: gsd_save_decision tool failed: ${msg}\n`); - return { - content: [{ type: "text" as const, text: `Error saving decision: ${msg}` }], - isError: true, - details: { operation: "save_decision", error: msg }, - }; - } - }, - }); - - pi.registerTool({ - name: "gsd_update_requirement", - label: "Update Requirement", - description: - "Update an existing requirement in the GSD database and regenerate REQUIREMENTS.md. " + - "Provide the requirement ID (e.g. R001) and any fields to update.", - promptSnippet: "Update an existing GSD requirement by ID (regenerates REQUIREMENTS.md)", - promptGuidelines: [ - "Use gsd_update_requirement to change status, validation, notes, or other fields on an existing requirement.", - "The id parameter is required — it must be an existing RXXX identifier.", - "All other fields are optional — only provided fields are updated.", - "The tool verifies the requirement exists before updating.", - ], - parameters: Type.Object({ - id: Type.String({ description: "The requirement ID (e.g. R001, R014)" }), - status: Type.Optional(Type.String({ description: "New status (e.g. 'active', 'validated', 'deferred')" })), - validation: Type.Optional(Type.String({ description: "Validation criteria or proof" })), - notes: Type.Optional(Type.String({ description: "Additional notes" })), - description: Type.Optional(Type.String({ description: "Updated description" })), - primary_owner: Type.Optional(Type.String({ description: "Primary owning slice" })), - supporting_slices: Type.Optional(Type.String({ description: "Supporting slices" })), - }), - async execute(_toolCallId, params, _signal, _onUpdate, _ctx) { - const dbAvailable = await ensureDbOpen(); - - if (!dbAvailable) { - return { - content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot update requirement." }], - isError: true, - details: { operation: "update_requirement", id: params.id, error: "db_unavailable" }, - }; - } - - try { - // Verify requirement exists - const db = await import("./gsd-db.js"); - const existing = db.getRequirementById(params.id); - if (!existing) { - return { - content: [{ type: "text" as const, text: `Error: Requirement ${params.id} not found.` }], - isError: true, - details: { operation: "update_requirement", id: params.id, error: "not_found" }, - }; - } - - const { updateRequirementInDb } = await import("./db-writer.js"); - const updates: Record = {}; - if (params.status !== undefined) updates.status = params.status; - if (params.validation !== undefined) updates.validation = params.validation; - if (params.notes !== undefined) updates.notes = params.notes; - if (params.description !== undefined) updates.description = params.description; - if (params.primary_owner !== undefined) updates.primary_owner = params.primary_owner; - if (params.supporting_slices !== undefined) updates.supporting_slices = params.supporting_slices; - - await updateRequirementInDb(params.id, updates, process.cwd()); - - return { - content: [{ type: "text" as const, text: `Updated requirement ${params.id}` }], - details: { operation: "update_requirement", id: params.id }, - }; - } catch (err) { - const msg = err instanceof Error ? err.message : String(err); - process.stderr.write(`gsd-db: gsd_update_requirement tool failed: ${msg}\n`); - return { - content: [{ type: "text" as const, text: `Error updating requirement: ${msg}` }], - isError: true, - details: { operation: "update_requirement", id: params.id, error: msg }, - }; - } - }, - }); - - pi.registerTool({ - name: "gsd_save_summary", - label: "Save Summary", - description: - "Save a summary, research, context, or assessment artifact to the GSD database and write it to disk. " + - "Computes the file path from milestone/slice/task IDs automatically.", - promptSnippet: "Save a GSD artifact (summary/research/context/assessment) to DB and disk", - promptGuidelines: [ - "Use gsd_save_summary to persist structured artifacts (SUMMARY, RESEARCH, CONTEXT, ASSESSMENT).", - "milestone_id is required. slice_id and task_id are optional — they determine the file path.", - "The tool computes the relative path automatically: milestones/M001/M001-SUMMARY.md, milestones/M001/slices/S01/S01-SUMMARY.md, etc.", - "artifact_type must be one of: SUMMARY, RESEARCH, CONTEXT, ASSESSMENT.", - ], - parameters: Type.Object({ - milestone_id: Type.String({ description: "Milestone ID (e.g. M001)" }), - slice_id: Type.Optional(Type.String({ description: "Slice ID (e.g. S01)" })), - task_id: Type.Optional(Type.String({ description: "Task ID (e.g. T01)" })), - artifact_type: Type.String({ description: "One of: SUMMARY, RESEARCH, CONTEXT, ASSESSMENT" }), - content: Type.String({ description: "The full markdown content of the artifact" }), - }), - async execute(_toolCallId, params, _signal, _onUpdate, _ctx) { - const dbAvailable = await ensureDbOpen(); - - if (!dbAvailable) { - return { - content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot save artifact." }], - isError: true, - details: { operation: "save_summary", error: "db_unavailable" }, - }; - } - - // Validate artifact_type - const validTypes = ["SUMMARY", "RESEARCH", "CONTEXT", "ASSESSMENT"]; - if (!validTypes.includes(params.artifact_type)) { - return { - content: [{ type: "text" as const, text: `Error: Invalid artifact_type "${params.artifact_type}". Must be one of: ${validTypes.join(", ")}` }], - isError: true, - details: { operation: "save_summary", error: "invalid_artifact_type" }, - }; - } - - try { - // Compute relative path from IDs - let relativePath: string; - if (params.task_id && params.slice_id) { - relativePath = `milestones/${params.milestone_id}/slices/${params.slice_id}/tasks/${params.task_id}-${params.artifact_type}.md`; - } else if (params.slice_id) { - relativePath = `milestones/${params.milestone_id}/slices/${params.slice_id}/${params.slice_id}-${params.artifact_type}.md`; - } else { - relativePath = `milestones/${params.milestone_id}/${params.milestone_id}-${params.artifact_type}.md`; - } - - const { saveArtifactToDb } = await import("./db-writer.js"); - await saveArtifactToDb( - { - path: relativePath, - artifact_type: params.artifact_type, - content: params.content, - milestone_id: params.milestone_id, - slice_id: params.slice_id, - task_id: params.task_id, - }, - process.cwd(), - ); - - return { - content: [{ type: "text" as const, text: `Saved ${params.artifact_type} artifact to ${relativePath}` }], - details: { operation: "save_summary", path: relativePath, artifact_type: params.artifact_type }, - }; - } catch (err) { - const msg = err instanceof Error ? err.message : String(err); - process.stderr.write(`gsd-db: gsd_save_summary tool failed: ${msg}\n`); - return { - content: [{ type: "text" as const, text: `Error saving artifact: ${msg}` }], - isError: true, - details: { operation: "save_summary", error: msg }, - }; - } - }, - }); - - // ── gsd_generate_milestone_id — canonical milestone ID generation ────── - // The LLM cannot generate random suffixes for unique_milestone_ids on its - // own. This tool calls back into the TS code that owns ID generation, - // ensuring the preference is always respected and IDs are always valid. - // - // Reservation set: tracks IDs returned by this tool but not yet persisted - // to disk, preventing duplicate M001 when called multiple times (#961). - const reservedMilestoneIds = new Set(); - - pi.registerTool({ - name: "gsd_generate_milestone_id", - label: "Generate Milestone ID", - description: - "Generate the next milestone ID for a new GSD milestone. " + - "Scans existing milestones on disk and respects the unique_milestone_ids preference. " + - "Always use this tool when creating a new milestone — never invent milestone IDs manually.", - promptSnippet: "Generate a valid milestone ID (respects unique_milestone_ids preference)", - promptGuidelines: [ - "ALWAYS call gsd_generate_milestone_id before creating a new milestone directory or writing milestone files.", - "Never invent or hardcode milestone IDs like M001, M002 — always use this tool.", - "Call it once per milestone you need to create. For multi-milestone projects, call it once for each milestone in sequence.", - "The tool returns the correct format based on project preferences (e.g. M001 or M001-r5jzab).", - ], - parameters: Type.Object({}), - async execute(_toolCallId, _params, _signal, _onUpdate, _ctx) { - try { - const basePath = process.cwd(); - const existingIds = findMilestoneIds(basePath); - const uniqueEnabled = !!loadEffectiveGSDPreferences()?.preferences?.unique_milestone_ids; - // Combine on-disk IDs with previously reserved (but not yet persisted) IDs - const allIds = [...new Set([...existingIds, ...reservedMilestoneIds])]; - const newId = nextMilestoneId(allIds, uniqueEnabled); - reservedMilestoneIds.add(newId); - return { - content: [{ type: "text" as const, text: newId }], - details: { operation: "generate_milestone_id", id: newId, existingCount: existingIds.length, reservedCount: reservedMilestoneIds.size, uniqueEnabled }, - }; - } catch (err) { - const msg = err instanceof Error ? err.message : String(err); - return { - content: [{ type: "text" as const, text: `Error generating milestone ID: ${msg}` }], - isError: true, - details: { operation: "generate_milestone_id", error: msg }, - }; - } - }, - }); - - // ── session_start: render branded GSD header + load tool keys + remote status ── - pi.on("session_start", async (_event, ctx) => { - // Clear per-session state that must not leak across sessions (e.g. RPC mode) - depthVerificationDone = false; - - // Theme access throws in RPC mode (no TUI) — header is decorative, skip it - try { - const theme = ctx.ui.theme; - const version = process.env.GSD_VERSION || "0.0.0"; - - const logoText = GSD_LOGO_LINES.map((line) => theme.fg("accent", line)).join("\n"); - const titleLine = ` ${theme.bold("Get Shit Done")} ${theme.fg("dim", `v${version}`)}`; - - const headerContent = `${logoText}\n${titleLine}`; - ctx.ui.setHeader((_ui, _theme) => new Text(headerContent, 1, 0)); - } catch { - // RPC mode — no TUI, skip header rendering - } - - // Load tool API keys from auth.json into environment - loadToolApiKeys(); - - // Notify remote questions status if configured - try { - const [{ getRemoteConfigStatus }, { getLatestPromptSummary }] = await Promise.all([ - import("../remote-questions/config.js"), - import("../remote-questions/status.js"), - ]); - const status = getRemoteConfigStatus(); - const latest = getLatestPromptSummary(); - if (!status.includes("not configured")) { - const suffix = latest ? `\nLast remote prompt: ${latest.id} (${latest.status})` : ""; - ctx.ui.notify(`${status}${suffix}`, status.includes("disabled") ? "warning" : "info"); - } - } catch { - // Remote questions module not available — ignore - } - }); - - // ── Ctrl+Alt+G shortcut — GSD dashboard overlay ──────────────────────── - pi.registerShortcut(Key.ctrlAlt("g"), { - description: shortcutDesc("Open GSD dashboard", "/gsd status"), - handler: async (ctx) => { - // Only show if .gsd/ exists - if (!existsSync(join(process.cwd(), ".gsd"))) { - ctx.ui.notify("No .gsd/ directory found. Run /gsd to start.", "info"); - return; - } - - await ctx.ui.custom( - (tui, theme, _kb, done) => { - return new GSDDashboardOverlay(tui, theme, () => done()); - }, - { - overlay: true, - overlayOptions: { - width: "90%", - minWidth: 80, - maxHeight: "92%", - anchor: "center", - }, - }, - ); - }, - }); - - // ── before_agent_start: inject GSD contract into true system prompt ───── - pi.on("before_agent_start", async (event, ctx: ExtensionContext) => { - if (!existsSync(join(process.cwd(), ".gsd"))) return; - - const stopContextTimer = debugTime("context-inject"); - const systemContent = loadPrompt("system"); - const loadedPreferences = loadEffectiveGSDPreferences(); - if (shouldPromptToEnableCmux(loadedPreferences?.preferences)) { - markCmuxPromptShown(); - ctx.ui.notify( - "cmux detected. Run /gsd cmux on to enable sidebar metadata, notifications, and visual subagent splits for this project.", - "info", - ); - } - let preferenceBlock = ""; - if (loadedPreferences) { - const cwd = process.cwd(); - const report = resolveAllSkillReferences(loadedPreferences.preferences, cwd); - preferenceBlock = `\n\n${renderPreferencesForSystemPrompt(loadedPreferences.preferences, report.resolutions)}`; - - // Emit warnings for unresolved skill references - if (report.warnings.length > 0) { - ctx.ui.notify( - `GSD skill preferences: ${report.warnings.length} unresolved skill${report.warnings.length === 1 ? "" : "s"}: ${report.warnings.join(", ")}`, - "warning", - ); - } - } - - // Load project knowledge if available - let knowledgeBlock = ""; - const knowledgePath = resolveGsdRootFile(process.cwd(), "KNOWLEDGE"); - if (existsSync(knowledgePath)) { - try { - const content = readFileSync(knowledgePath, "utf-8").trim(); - if (content) { - knowledgeBlock = `\n\n[PROJECT KNOWLEDGE — Rules, patterns, and lessons learned]\n\n${content}`; - } - } catch { - // File read error — skip knowledge injection - } - } - - // Inject auto-learned project memories - let memoryBlock = ""; - try { - const { getActiveMemoriesRanked, formatMemoriesForPrompt } = await import("./memory-store.js"); - const memories = getActiveMemoriesRanked(30); - if (memories.length > 0) { - const formatted = formatMemoriesForPrompt(memories, 2000); - if (formatted) { - memoryBlock = `\n\n${formatted}`; - } - } - } catch { /* non-fatal */ } - - // Detect skills installed during this auto-mode session - let newSkillsBlock = ""; - if (hasSkillSnapshot()) { - const newSkills = detectNewSkills(); - if (newSkills.length > 0) { - newSkillsBlock = formatSkillsXml(newSkills); - } - } - - // Warn if deprecated agent-instructions.md files are still present - warnDeprecatedAgentInstructions(); - - const injection = await buildGuidedExecuteContextInjection(event.prompt, process.cwd()); - - // Worktree context — override the static CWD in the system prompt - let worktreeBlock = ""; - const worktreeName = getActiveWorktreeName(); - const worktreeMainCwd = getWorktreeOriginalCwd(); - const autoWorktree = getActiveAutoWorktreeContext(); - if (worktreeName && worktreeMainCwd) { - worktreeBlock = [ - "", - "", - "[WORKTREE CONTEXT — OVERRIDES CURRENT WORKING DIRECTORY ABOVE]", - `IMPORTANT: Ignore the "Current working directory" shown earlier in this prompt.`, - `The actual current working directory is: ${toPosixPath(process.cwd())}`, - "", - `You are working inside a GSD worktree.`, - `- Worktree name: ${worktreeName}`, - `- Worktree path (this is the real cwd): ${toPosixPath(process.cwd())}`, - `- Main project: ${toPosixPath(worktreeMainCwd)}`, - `- Branch: worktree/${worktreeName}`, - "", - "All file operations, bash commands, and GSD state resolve against the worktree path above.", - "Use /worktree merge to merge changes back. Use /worktree return to switch back to the main tree.", - ].join("\n"); - } else if (autoWorktree) { - worktreeBlock = [ - "", - "", - "[WORKTREE CONTEXT — OVERRIDES CURRENT WORKING DIRECTORY ABOVE]", - `IMPORTANT: Ignore the "Current working directory" shown earlier in this prompt.`, - `The actual current working directory is: ${toPosixPath(process.cwd())}`, - "", - "You are working inside a GSD auto-worktree.", - `- Milestone worktree: ${autoWorktree.worktreeName}`, - `- Worktree path (this is the real cwd): ${toPosixPath(process.cwd())}`, - `- Main project: ${toPosixPath(autoWorktree.originalBase)}`, - `- Branch: ${autoWorktree.branch}`, - "", - "All file operations, bash commands, and GSD state resolve against the worktree path above.", - "Write every .gsd artifact in the worktree path above, never in the main project tree.", - ].join("\n"); - } - - const fullSystem = `${event.systemPrompt}\n\n[SYSTEM CONTEXT — GSD]\n\n${systemContent}${preferenceBlock}${knowledgeBlock}${memoryBlock}${newSkillsBlock}${worktreeBlock}`; - stopContextTimer({ - systemPromptSize: fullSystem.length, - injectionSize: injection?.length ?? 0, - hasPreferences: preferenceBlock.length > 0, - hasNewSkills: newSkillsBlock.length > 0, - }); - - return { - systemPrompt: fullSystem, - ...(injection - ? { - message: { - customType: "gsd-guided-context", - content: injection, - display: false, - }, - } - : {}), - }; - }); - - // ── agent_end: auto-mode advancement or auto-start after discuss ─────────── - pi.on("agent_end", async (event, ctx: ExtensionContext) => { - // If discuss phase just finished, start auto-mode - if (checkAutoStartAfterDiscuss()) { - depthVerificationDone = false; - activeQueuePhase = false; - return; - } - - // If auto-mode is already running, advance to next unit - if (!isAutoActive()) return; - - // Fresh-session auto-mode intentionally aborts the previous session during - // cmdCtx.newSession(). Ignore that agent_end so we neither pause nor - // resolve the new unit with an event from the old session. - if (isSessionSwitchInFlight()) { - return; - } - - // If the agent was aborted (user pressed Escape) or hit a provider - // error (fetch failure, rate limit, etc.), pause auto-mode instead of - // advancing. This preserves the conversation so the user can inspect - // what happened, interact with the agent, or resume. - const lastMsg = event.messages[event.messages.length - 1]; - if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "aborted") { - await pauseAuto(ctx, pi); - return; - } - if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "error") { - const errorDetail = - "errorMessage" in lastMsg && lastMsg.errorMessage - ? `: ${lastMsg.errorMessage}` - : ""; - - const errorMsg = ("errorMessage" in lastMsg && lastMsg.errorMessage) ? String(lastMsg.errorMessage) : ""; - - // ── Transient network error retry ────────────────────────────────── - // Before falling back to a different model, retry the current model - // for transient network errors (connection reset, timeout, DNS, etc.). - // This prevents providers with occasional network flakiness from being - // immediately abandoned in favor of fallback models (#941). - if (isTransientNetworkError(errorMsg)) { - const currentModelId = ctx.model?.id ?? "unknown"; - const retryKey = `network-retry:${currentModelId}`; - const maxRetries = 2; - const currentRetries = networkRetryCounters.get(retryKey) ?? 0; - - if (currentRetries < maxRetries) { - networkRetryCounters.set(retryKey, currentRetries + 1); - const attempt = currentRetries + 1; - const delayMs = attempt * 3000; // 3s, 6s backoff - ctx.ui.notify( - `Network error on ${currentModelId}${errorDetail}. Retry ${attempt}/${maxRetries} in ${delayMs / 1000}s...`, - "warning", - ); - setTimeout(() => { - pi.sendMessage( - { customType: "gsd-auto-timeout-recovery", content: "Continue execution — retrying after transient network error.", display: false }, - { triggerTurn: true }, - ); - }, delayMs); - return; - } - // Retries exhausted — clear counter and fall through to fallback logic - networkRetryCounters.delete(retryKey); - ctx.ui.notify( - `Network retries exhausted for ${currentModelId}. Attempting model fallback.`, - "warning", - ); - } - - const dash = getAutoDashboardData(); - if (dash.currentUnit) { - const modelConfig = resolveModelWithFallbacksForUnit(dash.currentUnit.type); - if (modelConfig && modelConfig.fallbacks.length > 0) { - const availableModels = ctx.modelRegistry.getAvailable(); - const currentModelId = ctx.model?.id; - - const nextModelId = getNextFallbackModel(currentModelId, modelConfig); - - if (nextModelId) { - // Clear any network retry counters when switching models - networkRetryCounters.clear(); - - let modelToSet; - const slashIdx = nextModelId.indexOf("/"); - if (slashIdx !== -1) { - const provider = nextModelId.substring(0, slashIdx); - const id = nextModelId.substring(slashIdx + 1); - modelToSet = availableModels.find( - m => m.provider.toLowerCase() === provider.toLowerCase() - && m.id.toLowerCase() === id.toLowerCase() - ); - } else { - const currentProvider = ctx.model?.provider; - const exactProviderMatch = availableModels.find( - m => m.id === nextModelId && m.provider === currentProvider - ); - modelToSet = exactProviderMatch ?? availableModels.find(m => m.id === nextModelId); - } - - if (modelToSet) { - const ok = await pi.setModel(modelToSet, { persist: false }); - if (ok) { - ctx.ui.notify(`Model error${errorDetail}. Switched to fallback: ${nextModelId} and resuming.`, "warning"); - // Trigger a generic "Continue execution" to resume the task since the previous attempt failed - pi.sendMessage( - { customType: "gsd-auto-timeout-recovery", content: "Continue execution.", display: false }, - { triggerTurn: true } - ); - return; - } - } - } - } - } - - // ── Session model recovery (#1065) ────────────────────────────────── - // Before pausing, attempt to restore the model captured at auto-mode - // start. This prevents cross-session model leakage: when fallback - // chains are exhausted (or absent), the session retries with the model - // the user originally chose instead of reading (possibly stale) global - // preferences that another concurrent session may have modified. - const sessionModel = getAutoModeStartModel(); - if (sessionModel) { - const currentModelId = ctx.model?.id; - const currentProvider = ctx.model?.provider; - // Only attempt recovery if the current model diverged from the session model - if (currentModelId !== sessionModel.id || currentProvider !== sessionModel.provider) { - const availableModels = ctx.modelRegistry.getAvailable(); - const startModel = availableModels.find( - m => m.provider === sessionModel.provider && m.id === sessionModel.id, - ); - if (startModel) { - const ok = await pi.setModel(startModel, { persist: false }); - if (ok) { - networkRetryCounters.clear(); - ctx.ui.notify( - `Model error${errorDetail}. Restored session model: ${sessionModel.provider}/${sessionModel.id} and resuming.`, - "warning", - ); - pi.sendMessage( - { customType: "gsd-auto-timeout-recovery", content: "Continue execution.", display: false }, - { triggerTurn: true }, - ); - return; - } - } - } - } - - // Classify the error: transient (auto-resume) vs permanent (manual resume) - const classification = classifyProviderError(errorMsg); - - // Extract explicit retry-after from the message or response metadata - const explicitRetryAfterMs = ("retryAfterMs" in lastMsg && typeof lastMsg.retryAfterMs === "number") - ? lastMsg.retryAfterMs - : undefined; - if (classification.isTransient) { - consecutiveTransientErrors += 1; - } else { - consecutiveTransientErrors = 0; - } - const baseRetryAfterMs = explicitRetryAfterMs ?? classification.suggestedDelayMs; - const retryAfterMs = classification.isTransient ? baseRetryAfterMs * 2 ** Math.max(0, consecutiveTransientErrors - 1) : baseRetryAfterMs; - const allowAutoResume = classification.isTransient - && consecutiveTransientErrors <= MAX_TRANSIENT_AUTO_RESUMES; - - if (classification.isTransient && !allowAutoResume) { - ctx.ui.notify( - `Transient provider errors persisted after ${MAX_TRANSIENT_AUTO_RESUMES} auto-resume attempts. Pausing for manual review.`, - "warning", - ); - } - - await pauseAutoForProviderError(ctx.ui, errorDetail, () => pauseAuto(ctx, pi), { - isRateLimit: classification.isRateLimit, - isTransient: allowAutoResume, - retryAfterMs, - resume: allowAutoResume - ? () => { - pi.sendMessage( - { customType: "gsd-auto-timeout-recovery", content: "Continue execution \u2014 provider error recovery delay elapsed.", display: false }, - { triggerTurn: true }, - ); - } - : undefined, - }); - return; - } - - try { - consecutiveTransientErrors = 0; - networkRetryCounters.clear(); // Clear network retry state on successful unit completion - resolveAgentEnd(event); - } catch (err) { - // Safety net: if resolveAgentEnd throws, ensure auto-mode stops gracefully (#381). - const message = err instanceof Error ? err.message : String(err); - ctx.ui.notify( - `Auto-mode error in agent_end handler: ${message}. Stopping auto-mode.`, - "error", - ); - try { - await pauseAuto(ctx, pi); - } catch { - // Last resort — at least log - } - } - }); - - // ── session_before_compact ──────────────────────────────────────────────── - pi.on("session_before_compact", async (_event, _ctx: ExtensionContext) => { - // Block compaction during auto-mode — each unit is a fresh session - // Also block during paused state — context is valuable for the user - if (isAutoActive() || isAutoPaused()) { - return { cancel: true }; - } - - const basePath = process.cwd(); - const state = await deriveState(basePath); - - // Only save continue.md if we're actively executing a task - if (!state.activeMilestone || !state.activeSlice || !state.activeTask) return; - if (state.phase !== "executing") return; - - const sDir = resolveSlicePath(basePath, state.activeMilestone.id, state.activeSlice.id); - if (!sDir) return; - - // Check for existing continue file (new naming or legacy) - const existingFile = resolveSliceFile(basePath, state.activeMilestone.id, state.activeSlice.id, "CONTINUE"); - if (existingFile && await loadFile(existingFile)) return; - const legacyContinue = join(sDir, "continue.md"); - if (await loadFile(legacyContinue)) return; - - const continuePath = join(sDir, buildSliceFileName(state.activeSlice.id, "CONTINUE")); - - const continueData = { - frontmatter: { - milestone: state.activeMilestone.id, - slice: state.activeSlice.id, - task: state.activeTask.id, - step: 0, - totalSteps: 0, - status: "compacted" as const, - savedAt: new Date().toISOString(), - }, - completedWork: `Task ${state.activeTask.id} (${state.activeTask.title}) was in progress when compaction occurred.`, - remainingWork: "Check the task plan for remaining steps.", - decisions: "Check task summary files for prior decisions.", - context: "Session was auto-compacted by Pi. Resume with /gsd.", - nextAction: `Resume task ${state.activeTask.id}: ${state.activeTask.title}.`, - }; - - await saveFile(continuePath, formatContinue(continueData)); - }); - - // ── session_shutdown: save activity log on Ctrl+C / SIGTERM ───────────── - pi.on("session_shutdown", async (_event, ctx: ExtensionContext) => { - if (isParallelActive()) { - try { - await shutdownParallel(process.cwd()); - } catch { /* best-effort */ } - } - - if (!isAutoActive() && !isAutoPaused()) return; - - // Save the current session — the lock file stays on disk - // so the next /gsd auto knows it was interrupted - const dash = getAutoDashboardData(); - if (dash.currentUnit) { - saveActivityLog(ctx, dash.basePath, dash.currentUnit.type, dash.currentUnit.id); - } - }); - - // ── tool_call: block CONTEXT.md writes during discussion without depth verification ── - pi.on("tool_call", async (event) => { - if (!isToolCallEventType("write", event)) return; - const result = shouldBlockContextWrite( - event.toolName, - event.input.path, - getDiscussionMilestoneId(), - isDepthVerified(), - activeQueuePhase, - ); - if (result.block) return result; - }); - - // ── tool_result: persist discussion exchanges & detect depth gate ────── - pi.on("tool_result", async (event) => { - if (event.toolName !== "ask_user_questions") return; - - const milestoneId = getDiscussionMilestoneId(); - if (!milestoneId) return; - - const details = event.details as any; - if (details?.cancelled || !details?.response) return; - - // ── Depth gate detection ────────────────────────────────────────── - const questions: any[] = (event.input as any)?.questions ?? []; - for (const q of questions) { - if (typeof q.id === "string" && q.id.includes("depth_verification")) { - depthVerificationDone = true; - break; - } - } - - // ── Persist exchange to DISCUSSION.md ────────────────────────────── - const basePath = process.cwd(); - const milestoneDir = resolveMilestonePath(basePath, milestoneId); - if (!milestoneDir) return; - - const fileName = buildMilestoneFileName(milestoneId, "DISCUSSION"); - const discussionPath = join(milestoneDir, fileName); - const timestamp = new Date().toISOString(); - - // Format exchange as markdown - const lines: string[] = [`## Exchange — ${timestamp}`, ""]; - - for (const q of questions) { - lines.push(`### ${q.header ?? "Question"}`); - lines.push(""); - lines.push(q.question ?? ""); - if (Array.isArray(q.options)) { - lines.push(""); - for (const opt of q.options) { - lines.push(`- **${opt.label}** — ${opt.description ?? ""}`); - } - } - - // Append user response for this question - const answer = details.response?.answers?.[q.id]; - if (answer) { - lines.push(""); - const selected = Array.isArray(answer.selected) ? answer.selected.join(", ") : answer.selected; - lines.push(`**Selected:** ${selected}`); - if (answer.notes) { - lines.push(`**Notes:** ${answer.notes}`); - } - } - lines.push(""); - } - - lines.push("---", ""); - - const newBlock = lines.join("\n"); - const existing = await loadFile(discussionPath) ?? `# ${milestoneId} Discussion Log\n\n`; - await saveFile(discussionPath, existing + newBlock); - }); - - // ── tool_execution_start/end: track in-flight tools for idle detection ── - pi.on("tool_execution_start", async (event) => { - if (!isAutoActive()) return; - markToolStart(event.toolCallId); - }); - - pi.on("tool_execution_end", async (event) => { - markToolEnd(event.toolCallId); - }); -} - -async function buildGuidedExecuteContextInjection(prompt: string, basePath: string): Promise { - const executeMatch = prompt.match(/Execute the next task:\s+(T\d+)\s+\("([^"]+)"\)\s+in slice\s+(S\d+)\s+of milestone\s+(M\d+(?:-[a-z0-9]{6})?)/i); - if (executeMatch) { - const [, taskId, taskTitle, sliceId, milestoneId] = executeMatch; - return buildTaskExecutionContextInjection(basePath, milestoneId, sliceId, taskId, taskTitle); - } - - const resumeMatch = prompt.match(/Resume interrupted work\.[\s\S]*?slice\s+(S\d+)\s+of milestone\s+(M\d+(?:-[a-z0-9]{6})?)/i); - if (resumeMatch) { - const [, sliceId, milestoneId] = resumeMatch; - const state = await deriveState(basePath); - if ( - state.activeMilestone?.id === milestoneId && - state.activeSlice?.id === sliceId && - state.activeTask - ) { - return buildTaskExecutionContextInjection( - basePath, - milestoneId, - sliceId, - state.activeTask.id, - state.activeTask.title, - ); - } - } - - return null; -} - -async function buildTaskExecutionContextInjection( - basePath: string, - milestoneId: string, - sliceId: string, - taskId: string, - taskTitle: string, -): Promise { - const taskPlanPath = resolveTaskFile(basePath, milestoneId, sliceId, taskId, "PLAN"); - const taskPlanRelPath = relTaskFile(basePath, milestoneId, sliceId, taskId, "PLAN"); - const taskPlanContent = taskPlanPath ? await loadFile(taskPlanPath) : null; - const taskPlanInline = taskPlanContent - ? [ - "## Inlined Task Plan (authoritative local execution contract)", - `Source: \`${taskPlanRelPath}\``, - "", - taskPlanContent.trim(), - ].join("\n") - : [ - "## Inlined Task Plan (authoritative local execution contract)", - `Task plan not found at dispatch time. Read \`${taskPlanRelPath}\` before executing.`, - ].join("\n"); - - const slicePlanPath = resolveSliceFile(basePath, milestoneId, sliceId, "PLAN"); - const slicePlanRelPath = relSliceFile(basePath, milestoneId, sliceId, "PLAN"); - const slicePlanContent = slicePlanPath ? await loadFile(slicePlanPath) : null; - const slicePlanExcerpt = extractSliceExecutionExcerpt(slicePlanContent, slicePlanRelPath); - - const priorTaskLines = await buildCarryForwardLines(basePath, milestoneId, sliceId, taskId); - const resumeSection = await buildResumeSection(basePath, milestoneId, sliceId); - - const activeOverrides = await loadActiveOverrides(basePath); - const overridesSection = formatOverridesSection(activeOverrides); - - return [ - "[GSD Guided Execute Context]", - "Use this injected context as startup context for guided task execution. Treat the inlined task plan as the authoritative local execution contract. Use source artifacts to verify details and run checks.", - overridesSection, "", - "", - resumeSection, - "", - "## Carry-Forward Context", - ...priorTaskLines, - "", - taskPlanInline, - "", - slicePlanExcerpt, - "", - "## Backing Source Artifacts", - `- Slice plan: \`${slicePlanRelPath}\``, - `- Task plan source: \`${taskPlanRelPath}\``, - ].join("\n"); -} - -async function buildCarryForwardLines( - basePath: string, - milestoneId: string, - sliceId: string, - taskId: string, -): Promise { - const tDir = resolveTasksDir(basePath, milestoneId, sliceId); - if (!tDir) return ["- No prior task summaries in this slice."]; - - const currentNum = parseInt(taskId.replace(/^T/, ""), 10); - const sRel = relSlicePath(basePath, milestoneId, sliceId); - const summaryFiles = resolveTaskFiles(tDir, "SUMMARY") - .filter((file) => parseInt(file.replace(/^T/, ""), 10) < currentNum) - .sort(); - - if (summaryFiles.length === 0) return ["- No prior task summaries in this slice."]; - - const lines = await Promise.all(summaryFiles.map(async (file) => { - const absPath = join(tDir, file); - const content = await loadFile(absPath); - const relPath = `${sRel}/tasks/${file}`; - if (!content) return `- \`${relPath}\``; - - const summary = parseSummary(content); - const provided = summary.frontmatter.provides.slice(0, 2).join("; "); - const decisions = summary.frontmatter.key_decisions.slice(0, 2).join("; "); - const patterns = summary.frontmatter.patterns_established.slice(0, 2).join("; "); - const diagnostics = extractMarkdownSection(content, "Diagnostics"); - - const parts = [summary.title || relPath]; - if (summary.oneLiner) parts.push(summary.oneLiner); - if (provided) parts.push(`provides: ${provided}`); - if (decisions) parts.push(`decisions: ${decisions}`); - if (patterns) parts.push(`patterns: ${patterns}`); - if (diagnostics) parts.push(`diagnostics: ${oneLine(diagnostics)}`); - - return `- \`${relPath}\` — ${parts.join(" | ")}`; - })); - - return lines; -} - -async function buildResumeSection(basePath: string, milestoneId: string, sliceId: string): Promise { - const continueFile = resolveSliceFile(basePath, milestoneId, sliceId, "CONTINUE"); - const legacyDir = resolveSlicePath(basePath, milestoneId, sliceId); - const legacyPath = legacyDir ? join(legacyDir, "continue.md") : null; - const continueContent = continueFile ? await loadFile(continueFile) : null; - const legacyContent = !continueContent && legacyPath ? await loadFile(legacyPath) : null; - const resolvedContent = continueContent ?? legacyContent; - const resolvedRelPath = continueContent - ? relSliceFile(basePath, milestoneId, sliceId, "CONTINUE") - : (legacyPath ? `${relSlicePath(basePath, milestoneId, sliceId)}/continue.md` : null); - - if (!resolvedContent || !resolvedRelPath) { - return ["## Resume State", "- No continue file present. Start from the top of the task plan."].join("\n"); - } - - const cont = parseContinue(resolvedContent); - const lines = [ - "## Resume State", - `Source: \`${resolvedRelPath}\``, - `- Status: ${cont.frontmatter.status || "in_progress"}`, - ]; - - if (cont.frontmatter.step && cont.frontmatter.totalSteps) { - lines.push(`- Progress: step ${cont.frontmatter.step} of ${cont.frontmatter.totalSteps}`); - } - if (cont.completedWork) lines.push(`- Completed: ${oneLine(cont.completedWork)}`); - if (cont.remainingWork) lines.push(`- Remaining: ${oneLine(cont.remainingWork)}`); - if (cont.decisions) lines.push(`- Decisions: ${oneLine(cont.decisions)}`); - if (cont.nextAction) lines.push(`- Next action: ${oneLine(cont.nextAction)}`); - - return lines.join("\n"); -} - -function extractSliceExecutionExcerpt(content: string | null, relPath: string): string { - if (!content) { - return [ - "## Slice Plan Excerpt", - `Slice plan not found at dispatch time. Read \`${relPath}\` before running slice-level verification.`, - ].join("\n"); - } - - const lines = content.split("\n"); - const goalLine = lines.find((line) => line.startsWith("**Goal:**"))?.trim(); - const demoLine = lines.find((line) => line.startsWith("**Demo:**"))?.trim(); - const verification = extractMarkdownSection(content, "Verification"); - const observability = extractMarkdownSection(content, "Observability / Diagnostics"); - - const parts = ["## Slice Plan Excerpt", `Source: \`${relPath}\``]; - if (goalLine) parts.push(goalLine); - if (demoLine) parts.push(demoLine); - if (verification) parts.push("", "### Slice Verification", verification.trim()); - if (observability) parts.push("", "### Slice Observability / Diagnostics", observability.trim()); - return parts.join("\n"); -} - -function extractMarkdownSection(content: string, heading: string): string | null { - const match = new RegExp(`^## ${escapeRegExp(heading)}\\s*$`, "m").exec(content); - if (!match) return null; - const start = match.index + match[0].length; - const rest = content.slice(start); - const nextHeading = rest.match(/^##\s+/m); - const end = nextHeading?.index ?? rest.length; - return rest.slice(0, end).trim(); -} - -function escapeRegExp(value: string): string { - return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); -} - -function oneLine(text: string): string { - return text.replace(/\s+/g, " ").trim(); +export default async function registerExtension(pi: ExtensionAPI) { + const { registerGsdExtension } = await import("./bootstrap/register-extension.js"); + registerGsdExtension(pi); } diff --git a/src/resources/extensions/gsd/tests/auto-loop.test.ts b/src/resources/extensions/gsd/tests/auto-loop.test.ts index e51fb40fa..d1070021d 100644 --- a/src/resources/extensions/gsd/tests/auto-loop.test.ts +++ b/src/resources/extensions/gsd/tests/auto-loop.test.ts @@ -962,21 +962,25 @@ test("auto.ts startAuto calls autoLoop (not dispatchNextUnit as first dispatch)" ); }); -test("index.ts agent_end handler calls resolveAgentEnd (not handleAgentEnd)", () => { - const src = readFileSync( - resolve(import.meta.dirname, "..", "index.ts"), +test("agent_end handler calls resolveAgentEnd (not handleAgentEnd)", () => { + const hooksSrc = readFileSync( + resolve(import.meta.dirname, "..", "bootstrap", "register-hooks.ts"), + "utf-8", + ); + // Verify the agent_end hook is registered + const handlerIdx = hooksSrc.indexOf('pi.on("agent_end"'); + assert.ok(handlerIdx > -1, "register-hooks.ts must have an agent_end handler"); + + const recoverySrc = readFileSync( + resolve(import.meta.dirname, "..", "bootstrap", "agent-end-recovery.ts"), "utf-8", ); - // Find the agent_end handler success path - const handlerIdx = src.indexOf('pi.on("agent_end"'); - assert.ok(handlerIdx > -1, "index.ts must have an agent_end handler"); - const handlerBlock = src.slice(handlerIdx, handlerIdx + 10000); assert.ok( - handlerBlock.includes("resolveAgentEnd(event)"), + recoverySrc.includes("resolveAgentEnd(event)"), "agent_end success path must call resolveAgentEnd(event) instead of handleAgentEnd(ctx, pi)", ); assert.ok( - handlerBlock.includes("isSessionSwitchInFlight()"), + recoverySrc.includes("isSessionSwitchInFlight()"), "agent_end handler must ignore session-switch agent_end events from cmdCtx.newSession()", ); }); diff --git a/src/resources/extensions/gsd/tests/provider-errors.test.ts b/src/resources/extensions/gsd/tests/provider-errors.test.ts index 35a7dd9ff..0512b4d90 100644 --- a/src/resources/extensions/gsd/tests/provider-errors.test.ts +++ b/src/resources/extensions/gsd/tests/provider-errors.test.ts @@ -261,38 +261,38 @@ test("pauseAutoForProviderError falls back to indefinite pause when not rate lim // ── Escalating backoff for transient errors (#1166) ───────────────────────── -test("index.ts tracks consecutive transient errors for escalating backoff", () => { - const indexSource = readFileSync(join(__dirname, "..", "index.ts"), "utf-8"); +test("agent-end-recovery.ts tracks consecutive transient errors for escalating backoff", () => { + const src = readFileSync(join(__dirname, "..", "bootstrap", "agent-end-recovery.ts"), "utf-8"); assert.ok( - indexSource.includes("consecutiveTransientErrors"), - "index.ts must track consecutiveTransientErrors for escalating backoff (#1166)", + src.includes("consecutiveTransientErrors"), + "agent-end-recovery.ts must track consecutiveTransientErrors for escalating backoff (#1166)", ); assert.ok( - indexSource.includes("MAX_TRANSIENT_AUTO_RESUMES"), - "index.ts must define MAX_TRANSIENT_AUTO_RESUMES to cap infinite retries (#1166)", + src.includes("MAX_TRANSIENT_AUTO_RESUMES"), + "agent-end-recovery.ts must define MAX_TRANSIENT_AUTO_RESUMES to cap infinite retries (#1166)", ); }); -test("index.ts resets consecutive transient error counter on success", () => { - const indexSource = readFileSync(join(__dirname, "..", "index.ts"), "utf-8"); +test("agent-end-recovery.ts resets consecutive transient error counter on success", () => { + const src = readFileSync(join(__dirname, "..", "bootstrap", "agent-end-recovery.ts"), "utf-8"); - // After successful unit completion, the counter must be reset. + // After successful agent_end (before resolveAgentEnd), the counter must be reset. // Use a regex across the success block so CRLF checkouts on Windows do not // push the reset line outside a fixed substring window. assert.ok( - /consecutiveTransientErrors\s*=\s*0\s*;[\s\S]{0,250}successful unit completion/.test(indexSource), - "consecutive transient error counter must be reset on successful unit completion (#1166)", + /consecutiveTransientErrors\s*=\s*0\s*;[\s\S]{0,250}resolveAgentEnd/.test(src), + "consecutive transient error counter must be reset before resolveAgentEnd on the success path (#1166)", ); }); -test("index.ts applies escalating delay for repeated transient errors", () => { - const indexSource = readFileSync(join(__dirname, "..", "index.ts"), "utf-8"); +test("agent-end-recovery.ts applies escalating delay for repeated transient errors", () => { + const src = readFileSync(join(__dirname, "..", "bootstrap", "agent-end-recovery.ts"), "utf-8"); - // Must contain the exponential backoff formula + // Must contain the exponential backoff formula (may span multiple lines) assert.ok( - /retryAfterMs\s*[=*].*2\s*\*\*/.test(indexSource), - "index.ts must escalate retryAfterMs exponentially for consecutive transient errors (#1166)", + src.includes("2 ** Math.max(0, consecutiveTransientErrors"), + "agent-end-recovery.ts must escalate retryAfterMs exponentially for consecutive transient errors (#1166)", ); }); diff --git a/src/resources/extensions/gsd/tests/visualizer-data.test.ts b/src/resources/extensions/gsd/tests/visualizer-data.test.ts index 06ce5a89b..9f9548169 100644 --- a/src/resources/extensions/gsd/tests/visualizer-data.test.ts +++ b/src/resources/extensions/gsd/tests/visualizer-data.test.ts @@ -422,25 +422,25 @@ assertTrue( "overlay has 10 tab labels", ); -// Verify commands.ts integration -const commandsPath = join(__dirname, "..", "commands.ts"); -const commandsSrc = readFileSync(commandsPath, "utf-8"); +// Verify commands/handlers/core.ts integration +const coreHandlerPath = join(__dirname, "..", "commands", "handlers", "core.ts"); +const coreHandlerSrc = readFileSync(coreHandlerPath, "utf-8"); -console.log("\n=== commands.ts integration ==="); +console.log("\n=== commands/handlers/core.ts integration ==="); assertTrue( - commandsSrc.includes('"visualize"'), - "commands.ts has visualize in subcommands array", + coreHandlerSrc.includes('"visualize"'), + "core.ts has visualize in subcommands array", ); assertTrue( - commandsSrc.includes("GSDVisualizerOverlay"), - "commands.ts imports GSDVisualizerOverlay", + coreHandlerSrc.includes("GSDVisualizerOverlay"), + "core.ts imports GSDVisualizerOverlay", ); assertTrue( - commandsSrc.includes("handleVisualize"), - "commands.ts has handleVisualize handler", + coreHandlerSrc.includes("handleVisualize"), + "core.ts has handleVisualize handler", ); report();