Refactor GSD command and bootstrap modules (#1634)
* Refactor GSD command/bootstrap modules * fix: resolve TypeScript build errors in refactored db-tools and catalog - db-tools.ts: add missing execute callback params (signal, onUpdate, ctx), remove isError from return objects (not in AgentToolResult type), cast details as any to avoid union type mismatch across error/success paths - catalog.ts: use Object.entries() on TemplateRegistry.templates Record instead of treating it as an array, use Record key as template id Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: update source-contract tests to reference refactored file locations The god-file refactor moved code from index.ts and commands.ts into bootstrap/agent-end-recovery.ts, bootstrap/register-hooks.ts, and commands/handlers/core.ts. Update three test files to read from the correct paths and adjust pattern assertions to match the new code structure. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
39f9faffa8
commit
cc2c887948
30 changed files with 2936 additions and 3184 deletions
|
|
@ -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<void>;
|
||||
getMarkdownThemeWithSettings: () => any;
|
||||
addMessageToChat: (message: any, options?: any) => void;
|
||||
formatWebSearchResult: (content: unknown) => string;
|
||||
getRegisteredToolDefinition: (toolName: string) => any;
|
||||
checkShutdownRequested: () => Promise<void>;
|
||||
rebuildChatFromMessages: () => void;
|
||||
flushCompactionQueue: (options?: { willRetry?: boolean }) => Promise<void>;
|
||||
showStatus: (message: string) => void;
|
||||
showError: (message: string) => void;
|
||||
updatePendingMessagesDisplay: () => void;
|
||||
}, event: InteractiveModeEvent): Promise<void> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -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<void>;
|
||||
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);
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
import type { Model } from "@gsd/pi-ai";
|
||||
|
||||
export async function handleModelCommand(host: any, searchTerm?: string): Promise<void> {
|
||||
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<Model<any> | 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<Model<any>[]> {
|
||||
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<void> {
|
||||
const models = await getModelCandidates(host);
|
||||
const uniqueProviders = new Set(models.map((m) => m.provider));
|
||||
host.footerDataProvider.setAvailableProviderCount(uniqueProviders.size);
|
||||
}
|
||||
|
||||
|
|
@ -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<string, any>;
|
||||
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;
|
||||
|
||||
|
|
@ -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<void> {
|
||||
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<void> {
|
||||
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<Model<any> | 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<Model<any>[]> {
|
||||
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<void> {
|
||||
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 {
|
||||
|
|
|
|||
142
src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts
Normal file
142
src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts
Normal file
|
|
@ -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<string, number>();
|
||||
const MAX_TRANSIENT_AUTO_RESUMES = 3;
|
||||
let consecutiveTransientErrors = 0;
|
||||
|
||||
export async function handleAgentEnd(
|
||||
pi: ExtensionAPI,
|
||||
event: { messages: any[] },
|
||||
ctx: ExtensionContext,
|
||||
): Promise<void> {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
238
src/resources/extensions/gsd/bootstrap/db-tools.ts
Normal file
238
src/resources/extensions/gsd/bootstrap/db-tools.ts
Normal file
|
|
@ -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<string, string | undefined> = {};
|
||||
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<string>();
|
||||
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,
|
||||
};
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
90
src/resources/extensions/gsd/bootstrap/dynamic-tools.ts
Normal file
90
src/resources/extensions/gsd/bootstrap/dynamic-tools.ts
Normal file
|
|
@ -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<boolean> {
|
||||
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);
|
||||
}
|
||||
|
||||
46
src/resources/extensions/gsd/bootstrap/register-extension.ts
Normal file
46
src/resources/extensions/gsd/bootstrap/register-extension.ts
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
||||
167
src/resources/extensions/gsd/bootstrap/register-hooks.ts
Normal file
167
src/resources/extensions/gsd/bootstrap/register-hooks.ts
Normal file
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
55
src/resources/extensions/gsd/bootstrap/register-shortcuts.ts
Normal file
55
src/resources/extensions/gsd/bootstrap/register-shortcuts.ts
Normal file
|
|
@ -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<void>(
|
||||
(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
|
||||
}
|
||||
}
|
||||
|
||||
340
src/resources/extensions/gsd/bootstrap/system-context.ts
Normal file
340
src/resources/extensions/gsd/bootstrap/system-context.ts
Normal file
|
|
@ -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<string | null> {
|
||||
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<string> {
|
||||
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<string[]> {
|
||||
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<string> {
|
||||
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();
|
||||
}
|
||||
|
||||
51
src/resources/extensions/gsd/bootstrap/write-gate.ts
Normal file
51
src/resources/extensions/gsd/bootstrap/write-gate.ts
Normal file
|
|
@ -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.`,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
301
src/resources/extensions/gsd/commands/catalog.ts
Normal file
301
src/resources/extensions/gsd/commands/catalog.ts
Normal file
|
|
@ -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<string, readonly GsdCommandDefinition[]>;
|
||||
|
||||
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 [];
|
||||
}
|
||||
101
src/resources/extensions/gsd/commands/context.ts
Normal file
101
src/resources/extensions/gsd/commands/context.ts
Normal file
|
|
@ -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<boolean> {
|
||||
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 <instruction> 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 <instruction> 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";
|
||||
}
|
||||
|
||||
32
src/resources/extensions/gsd/commands/dispatcher.ts
Normal file
32
src/resources/extensions/gsd/commands/dispatcher.ts
Normal file
|
|
@ -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<void> {
|
||||
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");
|
||||
}
|
||||
|
||||
74
src/resources/extensions/gsd/commands/handlers/auto.ts
Normal file
74
src/resources/extensions/gsd/commands/handlers/auto.ts
Normal file
|
|
@ -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<boolean> {
|
||||
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;
|
||||
}
|
||||
|
||||
274
src/resources/extensions/gsd/commands/handlers/core.ts
Normal file
274
src/resources/extensions/gsd/commands/handlers/core.ts
Normal file
|
|
@ -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 <tpl> Start a workflow template (bugfix, spike, feature, hotfix, etc.)",
|
||||
" /gsd templates List available workflow templates [info <name>]",
|
||||
" /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 <desc> Apply user override to active work",
|
||||
" /gsd capture <text> Quick-capture a thought to CAPTURES.md",
|
||||
" /gsd triage Classify and route pending captures",
|
||||
" /gsd skip <unit> 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 <type> <text> 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<void> {
|
||||
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<void>(
|
||||
(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<void> {
|
||||
await handleStatus(ctx as ExtensionCommandContext);
|
||||
}
|
||||
|
||||
export async function handleVisualize(ctx: ExtensionCommandContext): Promise<void> {
|
||||
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<void>(
|
||||
(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<void> {
|
||||
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<boolean> {
|
||||
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");
|
||||
}
|
||||
169
src/resources/extensions/gsd/commands/handlers/ops.ts
Normal file
169
src/resources/extensions/gsd/commands/handlers/ops.ts
Normal file
|
|
@ -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<boolean> {
|
||||
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 <hook-name> <unit-type> <unit-id>
|
||||
|
||||
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 <description of change>. 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 <rule|pattern|lesson> <description>. 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 <phase> (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;
|
||||
}
|
||||
118
src/resources/extensions/gsd/commands/handlers/parallel.ts
Normal file
118
src/resources/extensions/gsd/commands/handlers/parallel.ts
Normal file
|
|
@ -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<boolean> {
|
||||
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;
|
||||
}
|
||||
|
||||
109
src/resources/extensions/gsd/commands/handlers/workflow.ts
Normal file
109
src/resources/extensions/gsd/commands/handlers/workflow.ts
Normal file
|
|
@ -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<boolean> {
|
||||
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 <id>`, "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);
|
||||
}
|
||||
|
||||
14
src/resources/extensions/gsd/commands/index.ts
Normal file
14
src/resources/extensions/gsd/commands/index.ts
Normal file
|
|
@ -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);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -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()",
|
||||
);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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)",
|
||||
);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue