{
try {
const historyPath = join(gsdRoot(basePath), "doctor-history.jsonl");
+ const errorCount = report.issues.filter(i => i.severity === "error").length;
+ const warningCount = report.issues.filter(i => i.severity === "warning").length;
+ const issueDetails = report.issues
+ .filter(i => i.severity === "error" || i.severity === "warning")
+ .slice(0, 10) // cap to keep JSONL lines bounded
+ .map(i => ({ severity: i.severity, code: i.code, message: i.message, unitId: i.unitId }));
+
+ // Human-readable one-line summary
+ const summaryParts: string[] = [];
+ if (report.ok) {
+ summaryParts.push("Clean");
+ } else {
+ const counts: string[] = [];
+ if (errorCount > 0) counts.push(`${errorCount} error${errorCount > 1 ? "s" : ""}`);
+ if (warningCount > 0) counts.push(`${warningCount} warning${warningCount > 1 ? "s" : ""}`);
+ summaryParts.push(counts.join(", "));
+ }
+ if (report.fixesApplied.length > 0) {
+ summaryParts.push(`${report.fixesApplied.length} fixed`);
+ }
+ if (issueDetails.length > 0) {
+ const topIssue = issueDetails.find(i => i.severity === "error") ?? issueDetails[0]!;
+ summaryParts.push(topIssue.message);
+ }
+
const entry = JSON.stringify({
ts: new Date().toISOString(),
ok: report.ok,
- errors: report.issues.filter(i => i.severity === "error").length,
- warnings: report.issues.filter(i => i.severity === "warning").length,
+ errors: errorCount,
+ warnings: warningCount,
fixes: report.fixesApplied.length,
codes: [...new Set(report.issues.map(i => i.code))],
+ issues: issueDetails.length > 0 ? issueDetails : undefined,
+ fixDescriptions: report.fixesApplied.length > 0 ? report.fixesApplied : undefined,
+ scope: (report as any).scope as string | undefined,
+ summary: summaryParts.join(" · "),
} satisfies DoctorHistoryEntry);
const existing = existsSync(historyPath) ? readFileSync(historyPath, "utf-8") : "";
await saveFile(historyPath, existing + entry + "\n");
diff --git a/src/resources/extensions/env-utils.ts b/src/resources/extensions/gsd/env-utils.ts
similarity index 100%
rename from src/resources/extensions/env-utils.ts
rename to src/resources/extensions/gsd/env-utils.ts
diff --git a/src/resources/extensions/gsd/export-html.ts b/src/resources/extensions/gsd/export-html.ts
index 18c367aaf..09c40a022 100644
--- a/src/resources/extensions/gsd/export-html.ts
+++ b/src/resources/extensions/gsd/export-html.ts
@@ -296,9 +296,60 @@ function buildHealthSection(data: VisualizerData): string {
` : '';
+ // Progress score section
+ let progressHtml = '';
+ if (h.progressScore) {
+ const ps = h.progressScore;
+ const scoreColor = ps.level === 'green' ? '#22c55e' : ps.level === 'yellow' ? '#eab308' : '#ef4444';
+ const signalRows = ps.signals.map(s => {
+ const icon = s.kind === 'positive' ? '✓' : s.kind === 'negative' ? '✗' : '·';
+ const color = s.kind === 'positive' ? '#22c55e' : s.kind === 'negative' ? '#ef4444' : '#888';
+ return `${icon} ${esc(s.label)}
`;
+ }).join('');
+ progressHtml = `
+ Progress Score
+ ● ${esc(ps.summary)}
+ ${signalRows}`;
+ }
+
+ // Doctor history section
+ let historyHtml = '';
+ const doctorHistory = h.doctorHistory ?? [];
+ if (doctorHistory.length > 0) {
+ const historyRows = doctorHistory.slice(0, 20).map(entry => {
+ const statusIcon = entry.ok ? '✓' : '✗';
+ const statusColor = entry.ok ? '#22c55e' : '#ef4444';
+ const ts = entry.ts.replace('T', ' ').slice(0, 19);
+ const scopeTag = entry.scope ? ` [${esc(entry.scope)}]` : '';
+ const summaryText = entry.summary ? esc(entry.summary) : `${entry.errors} errors, ${entry.warnings} warnings, ${entry.fixes} fixes`;
+ const issueDetails = (entry.issues ?? []).slice(0, 3).map(i => {
+ const iColor = i.severity === 'error' ? '#ef4444' : '#eab308';
+ return `${i.severity === 'error' ? '✗' : '⚠'} ${esc(i.message)} ${esc(i.unitId)}
`;
+ }).join('');
+ const fixDetails = (entry.fixDescriptions ?? []).slice(0, 2).map(f =>
+ `↳ ${esc(f)}
`
+ ).join('');
+ return `
+ | ${statusIcon} |
+ ${esc(ts)}${scopeTag} |
+ ${summaryText} |
+
+ ${issueDetails || fixDetails ? `| ${issueDetails}${fixDetails} |
` : ''}`;
+ }).join('');
+
+ historyHtml = `
+ Doctor Run History
+
+ | Time | Summary |
+ ${historyRows}
+
`;
+ }
+
return section('health', 'Health', `
${tierRows}
+ ${progressHtml}
+ ${historyHtml}
`);
}
diff --git a/src/resources/extensions/gsd/files.ts b/src/resources/extensions/gsd/files.ts
index 6c17362ef..f60c697a5 100644
--- a/src/resources/extensions/gsd/files.ts
+++ b/src/resources/extensions/gsd/files.ts
@@ -20,7 +20,7 @@ import type {
ManifestStatus,
} from './types.js';
-import { checkExistingEnvKeys } from '../env-utils.js';
+import { checkExistingEnvKeys } from './env-utils.js';
import { parseRoadmapSlices } from './roadmap-slices.js';
import { nativeParseRoadmap, nativeExtractSection, nativeParsePlanFile, nativeParseSummaryFile, NATIVE_UNAVAILABLE } from './native-parser-bridge.js';
import { debugTime, debugCount } from './debug-logger.js';
diff --git a/src/resources/extensions/gsd/health-widget.ts b/src/resources/extensions/gsd/health-widget.ts
index 03afa7d3f..fa63e6677 100644
--- a/src/resources/extensions/gsd/health-widget.ts
+++ b/src/resources/extensions/gsd/health-widget.ts
@@ -15,7 +15,8 @@ import { runEnvironmentChecks } from "./doctor-environment.js";
import { loadEffectiveGSDPreferences } from "./preferences.js";
import { loadLedgerFromDisk, getProjectTotals } from "./metrics.js";
import { describeNextUnit, estimateTimeRemaining, updateSliceProgressCache } from "./auto-dashboard.js";
-import { projectRoot } from "./commands.js";
+import { projectRoot } from "./commands/context.js";
+import { deriveState, invalidateStateCache } from "./state.js";
import {
buildHealthLines,
detectHealthWidgetProjectState,
diff --git a/src/resources/extensions/gsd/index.ts b/src/resources/extensions/gsd/index.ts
index 4114639cc..13e6dc97c 100644
--- a/src/resources/extensions/gsd/index.ts
+++ b/src/resources/extensions/gsd/index.ts
@@ -1,1315 +1,13 @@
-/**
- * GSD Extension — /gsd
- *
- * One command, one wizard. Reads state from disk, shows contextual options,
- * dispatches through GSD-WORKFLOW.md. The LLM does the rest.
- *
- * Auto-mode: /gsd auto loops fresh sessions until milestone complete.
- *
- * Commands:
- * /gsd — contextual wizard (smart entry point)
- * /gsd auto — start auto-mode (fresh session per unit)
- * /gsd stop — stop auto-mode gracefully
- * /gsd status — progress dashboard
- *
- * Hooks:
- * before_agent_start — inject GSD system context for GSD projects
- * agent_end — auto-mode advancement
- * session_before_compact — save continue.md OR block during auto
- */
+import type { ExtensionAPI } from "@gsd/pi-coding-agent";
-import type {
- ExtensionAPI,
- ExtensionCommandContext,
- ExtensionContext,
-} from "@gsd/pi-coding-agent";
-import { createBashTool, createWriteTool, createReadTool, createEditTool, isToolCallEventType } from "@gsd/pi-coding-agent";
-import { Type } from "@sinclair/typebox";
+export {
+ isDepthVerified,
+ isQueuePhaseActive,
+ setQueuePhaseActive,
+ shouldBlockContextWrite,
+} from "./bootstrap/write-gate.js";
-import { debugLog, debugTime } from "./debug-logger.js";
-import { registerGSDCommand } from "./commands.js";
-import { loadToolApiKeys } from "./commands-config.js";
-import { registerExitCommand } from "./exit-command.js";
-import { registerWorktreeCommand, getWorktreeOriginalCwd, getActiveWorktreeName } from "./worktree-command.js";
-import { getActiveAutoWorktreeContext } from "./auto-worktree.js";
-import { saveFile, formatContinue, loadFile, parseContinue, parseSummary, loadActiveOverrides, formatOverridesSection } from "./files.js";
-import { loadPrompt } from "./prompt-loader.js";
-import { deriveState } from "./state.js";
-import { isAutoActive, isAutoPaused, pauseAuto, getAutoDashboardData, getAutoModeStartModel, markToolStart, markToolEnd } from "./auto.js";
-import { isSessionSwitchInFlight, resolveAgentEnd } from "./auto-loop.js";
-import { saveActivityLog } from "./activity-log.js";
-import { checkAutoStartAfterDiscuss, getDiscussionMilestoneId, findMilestoneIds, nextMilestoneId } from "./guided-flow.js";
-import { GSDDashboardOverlay } from "./dashboard-overlay.js";
-import {
- loadEffectiveGSDPreferences,
- renderPreferencesForSystemPrompt,
- resolveAllSkillReferences,
- resolveModelWithFallbacksForUnit,
- getNextFallbackModel,
- isTransientNetworkError,
-} from "./preferences.js";
-import { hasSkillSnapshot, detectNewSkills, formatSkillsXml } from "./skill-discovery.js";
-import {
- resolveSlicePath, resolveSliceFile, resolveTaskFile, resolveTaskFiles, resolveTasksDir,
- relSliceFile, relSlicePath, relTaskFile,
- buildSliceFileName, buildMilestoneFileName, gsdRoot, resolveMilestonePath,
- resolveGsdRootFile,
-} from "./paths.js";
-import { Key } from "@gsd/pi-tui";
-import { join } from "node:path";
-import { existsSync, readFileSync } from "node:fs";
-import { homedir } from "node:os";
-import { shortcutDesc } from "../shared/mod.js";
-
-const gsdHome = process.env.GSD_HOME || join(homedir(), ".gsd");
-import { Text } from "@gsd/pi-tui";
-import { pauseAutoForProviderError, classifyProviderError } from "./provider-error-pause.js";
-import { toPosixPath } from "../shared/mod.js";
-import { isParallelActive, shutdownParallel } from "./parallel-orchestrator.js";
-import { DEFAULT_BASH_TIMEOUT_SECS } from "./constants.js";
-import { markCmuxPromptShown, shouldPromptToEnableCmux } from "../cmux/index.js";
-
-// ── Agent Instructions (DEPRECATED) ──────────────────────────────────────
-// agent-instructions.md is deprecated. Use AGENTS.md or CLAUDE.md instead.
-// Pi core natively supports AGENTS.md (with CLAUDE.md fallback) per directory.
-
-function warnDeprecatedAgentInstructions(): void {
- const paths = [
- join(gsdHome, "agent-instructions.md"),
- join(process.cwd(), ".gsd", "agent-instructions.md"),
- ];
- for (const p of paths) {
- if (existsSync(p)) {
- console.warn(
- `[GSD] DEPRECATED: ${p} is no longer loaded. ` +
- `Migrate your instructions to AGENTS.md (or CLAUDE.md) in the same directory. ` +
- `See https://github.com/gsd-build/GSD-2/issues/1492`,
- );
- }
- }
-}
-
-// ── Depth verification state ──────────────────────────────────────────────
-let depthVerificationDone = false;
-
-// ── DB lazy-open helper ───────────────────────────────────────────────────
-// In manual sessions (no auto-mode), the DB is never opened by bootstrapAutoSession.
-// This helper ensures the DB is lazily opened on first tool call that needs it.
-async function ensureDbOpen(): Promise {
- try {
- const db = await import("./gsd-db.js");
- if (db.isDbAvailable()) return true;
- const dbPath = join(process.cwd(), ".gsd", "gsd.db");
- if (existsSync(dbPath)) {
- return db.openDatabase(dbPath);
- }
- return false;
- } catch {
- return false;
- }
-}
-
-// ── Queue phase tracking ──────────────────────────────────────────────────
-// When true, the LLM is in a queue flow writing CONTEXT.md files.
-// The write-gate applies during queue flows just like discussion flows.
-let activeQueuePhase = false;
-
-// ── Network error retry counters ──────────────────────────────────────────
-// Tracks per-model retry attempts for transient network errors.
-// Cleared when a model switch occurs or retries are exhausted.
-const networkRetryCounters = new Map();
-const MAX_TRANSIENT_AUTO_RESUMES = 3;
-let consecutiveTransientErrors = 0;
-
-export function isDepthVerified(): boolean {
- return depthVerificationDone;
-}
-
-/** Check whether a queue phase is active. */
-export function isQueuePhaseActive(): boolean {
- return activeQueuePhase;
-}
-
-/** Set the queue phase state — called from guided-flow-queue.ts on dispatch. */
-export function setQueuePhaseActive(active: boolean): void {
- activeQueuePhase = active;
-}
-
-// ── Write-gate: block CONTEXT.md writes during discussion without depth verification ──
-const MILESTONE_CONTEXT_RE = /M\d+(?:-[a-z0-9]{6})?-CONTEXT\.md$/;
-
-export function shouldBlockContextWrite(
- toolName: string,
- inputPath: string,
- milestoneId: string | null,
- depthVerified: boolean,
- queuePhaseActive?: boolean,
-): { block: boolean; reason?: string } {
- if (toolName !== "write") return { block: false };
-
- // Gate applies during both discussion (milestoneId set) and queue (queuePhaseActive) flows
- const inDiscussion = milestoneId !== null;
- const inQueue = queuePhaseActive ?? false;
- if (!inDiscussion && !inQueue) return { block: false };
-
- if (!MILESTONE_CONTEXT_RE.test(inputPath)) return { block: false };
- if (depthVerified) return { block: false };
-
- return {
- block: true,
- reason: `Blocked: Cannot write to milestone CONTEXT.md during discussion phase without depth verification. Call ask_user_questions with question id "depth_verification" first to confirm discussion depth before writing context.`,
- };
-}
-
-// ── ASCII logo ────────────────────────────────────────────────────────────
-const GSD_LOGO_LINES = [
- " ██████╗ ███████╗██████╗ ",
- " ██╔════╝ ██╔════╝██╔══██╗",
- " ██║ ███╗███████╗██║ ██║",
- " ██║ ██║╚════██║██║ ██║",
- " ╚██████╔╝███████║██████╔╝",
- " ╚═════╝ ╚══════╝╚═════╝ ",
-];
-
-export default function (pi: ExtensionAPI) {
- registerGSDCommand(pi);
- registerWorktreeCommand(pi);
- registerExitCommand(pi);
-
- // ── EPIPE guard — prevent crash when stdout/stderr pipe closes unexpectedly ──
- // Node.js throws a fatal `Error: write EPIPE` when the parent process closes
- // its end of the stdio pipe (e.g. during shell/IPC teardown) while auto-mode
- // is still writing diagnostics. Catching this here gives auto-mode a clean
- // chance to persist state and pause instead of crashing (see issue #739).
- if (!process.listeners("uncaughtException").some(l => l.name === "_gsdEpipeGuard")) {
- const _gsdEpipeGuard = (err: Error): void => {
- if ((err as NodeJS.ErrnoException).code === "EPIPE") {
- // Pipe closed — nothing we can write; just exit cleanly
- process.exit(0);
- }
- if ((err as NodeJS.ErrnoException).code === "ENOENT" &&
- (err as any).syscall?.startsWith("spawn")) {
- // spawn ENOENT — command not found (e.g., npx on Windows).
- // This surfaces as an uncaught exception from child_process but
- // is not a fatal process error. Log and continue instead of
- // crashing auto-mode (#1384).
- process.stderr.write(`[gsd] spawn ENOENT: ${(err as any).path ?? "unknown"} — command not found\n`);
- return;
- }
- // Re-throw anything that isn't EPIPE/ENOENT so real crashes still surface
- throw err;
- };
- process.on("uncaughtException", _gsdEpipeGuard);
- }
-
- // ── /kill — immediate exit (bypass cleanup) ─────────────────────────────
- pi.registerCommand("kill", {
- description: "Exit GSD immediately (no cleanup)",
- handler: async (_args: string, _ctx: ExtensionCommandContext) => {
- process.exit(0);
- },
- });
-
- // ── Dynamic-cwd bash tool with default timeout ────────────────────────
- // The built-in bash tool captures cwd at startup. This replacement uses
- // a spawnHook to read process.cwd() dynamically so that process.chdir()
- // (used by /worktree switch) propagates to shell commands.
- //
- // The upstream SDK's bash tool has no default timeout — if the LLM omits
- // the timeout parameter, commands run indefinitely, causing hangs on
- // Windows where process killing is unreliable (see #40). We wrap execute
- // to inject a 120-second default when no timeout is provided.
- const baseBash = createBashTool(process.cwd(), {
- spawnHook: (ctx) => ({ ...ctx, cwd: process.cwd() }),
- });
- const dynamicBash = {
- ...baseBash,
- execute: async (
- toolCallId: string,
- params: { command: string; timeout?: number },
- signal?: AbortSignal,
- onUpdate?: any,
- ctx?: any,
- ) => {
- const paramsWithTimeout = {
- ...params,
- timeout: params.timeout ?? DEFAULT_BASH_TIMEOUT_SECS,
- };
- return (baseBash as any).execute(toolCallId, paramsWithTimeout, signal, onUpdate, ctx);
- },
- };
- pi.registerTool(dynamicBash as any);
-
- // ── Dynamic-cwd file tools (write, read, edit) ────────────────────────
- // The built-in file tools capture cwd at startup. When process.chdir()
- // moves us into a worktree, relative paths still resolve against the
- // original launch directory. These replacements delegate to freshly-
- // created tools on each call so that process.cwd() is read dynamically.
- const baseWrite = createWriteTool(process.cwd());
- const dynamicWrite = {
- ...baseWrite,
- execute: async (
- toolCallId: string,
- params: { path: string; content: string },
- signal?: AbortSignal,
- onUpdate?: any,
- ctx?: any,
- ) => {
- const fresh = createWriteTool(process.cwd());
- return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx);
- },
- };
- pi.registerTool(dynamicWrite as any);
-
- const baseRead = createReadTool(process.cwd());
- const dynamicRead = {
- ...baseRead,
- execute: async (
- toolCallId: string,
- params: { path: string; offset?: number; limit?: number },
- signal?: AbortSignal,
- onUpdate?: any,
- ctx?: any,
- ) => {
- const fresh = createReadTool(process.cwd());
- return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx);
- },
- };
- pi.registerTool(dynamicRead as any);
-
- const baseEdit = createEditTool(process.cwd());
- const dynamicEdit = {
- ...baseEdit,
- execute: async (
- toolCallId: string,
- params: { path: string; oldText: string; newText: string },
- signal?: AbortSignal,
- onUpdate?: any,
- ctx?: any,
- ) => {
- const fresh = createEditTool(process.cwd());
- return (fresh as any).execute(toolCallId, params, signal, onUpdate, ctx);
- },
- };
- pi.registerTool(dynamicEdit as any);
-
- // ── Structured LLM tools — DB-first write path (R014) ──────────────────
-
- pi.registerTool({
- name: "gsd_save_decision",
- label: "Save Decision",
- description:
- "Record a project decision to the GSD database and regenerate DECISIONS.md. " +
- "Decision IDs are auto-assigned — never provide an ID manually.",
- promptSnippet: "Record a project decision to the GSD database (auto-assigns ID, regenerates DECISIONS.md)",
- promptGuidelines: [
- "Use gsd_save_decision when recording an architectural, pattern, library, or observability decision.",
- "Decision IDs are auto-assigned (D001, D002, ...) — never guess or provide an ID.",
- "All fields except revisable and when_context are required.",
- "The tool writes to the DB and regenerates .gsd/DECISIONS.md automatically.",
- ],
- parameters: Type.Object({
- scope: Type.String({ description: "Scope of the decision (e.g. 'architecture', 'library', 'observability')" }),
- decision: Type.String({ description: "What is being decided" }),
- choice: Type.String({ description: "The choice made" }),
- rationale: Type.String({ description: "Why this choice was made" }),
- revisable: Type.Optional(Type.String({ description: "Whether this can be revisited (default: 'Yes')" })),
- when_context: Type.Optional(Type.String({ description: "When/context for the decision (e.g. milestone ID)" })),
- }),
- async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
- // Ensure DB is open (lazy-open on first tool call in manual sessions)
- const dbAvailable = await ensureDbOpen();
-
- if (!dbAvailable) {
- return {
- content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot save decision." }],
- isError: true,
- details: { operation: "save_decision", error: "db_unavailable" },
- };
- }
-
- try {
- const { saveDecisionToDb } = await import("./db-writer.js");
- const { id } = await saveDecisionToDb(
- {
- scope: params.scope,
- decision: params.decision,
- choice: params.choice,
- rationale: params.rationale,
- revisable: params.revisable,
- when_context: params.when_context,
- },
- process.cwd(),
- );
- return {
- content: [{ type: "text" as const, text: `Saved decision ${id}` }],
- details: { operation: "save_decision", id },
- };
- } catch (err) {
- const msg = err instanceof Error ? err.message : String(err);
- process.stderr.write(`gsd-db: gsd_save_decision tool failed: ${msg}\n`);
- return {
- content: [{ type: "text" as const, text: `Error saving decision: ${msg}` }],
- isError: true,
- details: { operation: "save_decision", error: msg },
- };
- }
- },
- });
-
- pi.registerTool({
- name: "gsd_update_requirement",
- label: "Update Requirement",
- description:
- "Update an existing requirement in the GSD database and regenerate REQUIREMENTS.md. " +
- "Provide the requirement ID (e.g. R001) and any fields to update.",
- promptSnippet: "Update an existing GSD requirement by ID (regenerates REQUIREMENTS.md)",
- promptGuidelines: [
- "Use gsd_update_requirement to change status, validation, notes, or other fields on an existing requirement.",
- "The id parameter is required — it must be an existing RXXX identifier.",
- "All other fields are optional — only provided fields are updated.",
- "The tool verifies the requirement exists before updating.",
- ],
- parameters: Type.Object({
- id: Type.String({ description: "The requirement ID (e.g. R001, R014)" }),
- status: Type.Optional(Type.String({ description: "New status (e.g. 'active', 'validated', 'deferred')" })),
- validation: Type.Optional(Type.String({ description: "Validation criteria or proof" })),
- notes: Type.Optional(Type.String({ description: "Additional notes" })),
- description: Type.Optional(Type.String({ description: "Updated description" })),
- primary_owner: Type.Optional(Type.String({ description: "Primary owning slice" })),
- supporting_slices: Type.Optional(Type.String({ description: "Supporting slices" })),
- }),
- async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
- const dbAvailable = await ensureDbOpen();
-
- if (!dbAvailable) {
- return {
- content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot update requirement." }],
- isError: true,
- details: { operation: "update_requirement", id: params.id, error: "db_unavailable" },
- };
- }
-
- try {
- // Verify requirement exists
- const db = await import("./gsd-db.js");
- const existing = db.getRequirementById(params.id);
- if (!existing) {
- return {
- content: [{ type: "text" as const, text: `Error: Requirement ${params.id} not found.` }],
- isError: true,
- details: { operation: "update_requirement", id: params.id, error: "not_found" },
- };
- }
-
- const { updateRequirementInDb } = await import("./db-writer.js");
- const updates: Record = {};
- if (params.status !== undefined) updates.status = params.status;
- if (params.validation !== undefined) updates.validation = params.validation;
- if (params.notes !== undefined) updates.notes = params.notes;
- if (params.description !== undefined) updates.description = params.description;
- if (params.primary_owner !== undefined) updates.primary_owner = params.primary_owner;
- if (params.supporting_slices !== undefined) updates.supporting_slices = params.supporting_slices;
-
- await updateRequirementInDb(params.id, updates, process.cwd());
-
- return {
- content: [{ type: "text" as const, text: `Updated requirement ${params.id}` }],
- details: { operation: "update_requirement", id: params.id },
- };
- } catch (err) {
- const msg = err instanceof Error ? err.message : String(err);
- process.stderr.write(`gsd-db: gsd_update_requirement tool failed: ${msg}\n`);
- return {
- content: [{ type: "text" as const, text: `Error updating requirement: ${msg}` }],
- isError: true,
- details: { operation: "update_requirement", id: params.id, error: msg },
- };
- }
- },
- });
-
- pi.registerTool({
- name: "gsd_save_summary",
- label: "Save Summary",
- description:
- "Save a summary, research, context, or assessment artifact to the GSD database and write it to disk. " +
- "Computes the file path from milestone/slice/task IDs automatically.",
- promptSnippet: "Save a GSD artifact (summary/research/context/assessment) to DB and disk",
- promptGuidelines: [
- "Use gsd_save_summary to persist structured artifacts (SUMMARY, RESEARCH, CONTEXT, ASSESSMENT).",
- "milestone_id is required. slice_id and task_id are optional — they determine the file path.",
- "The tool computes the relative path automatically: milestones/M001/M001-SUMMARY.md, milestones/M001/slices/S01/S01-SUMMARY.md, etc.",
- "artifact_type must be one of: SUMMARY, RESEARCH, CONTEXT, ASSESSMENT.",
- ],
- parameters: Type.Object({
- milestone_id: Type.String({ description: "Milestone ID (e.g. M001)" }),
- slice_id: Type.Optional(Type.String({ description: "Slice ID (e.g. S01)" })),
- task_id: Type.Optional(Type.String({ description: "Task ID (e.g. T01)" })),
- artifact_type: Type.String({ description: "One of: SUMMARY, RESEARCH, CONTEXT, ASSESSMENT" }),
- content: Type.String({ description: "The full markdown content of the artifact" }),
- }),
- async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
- const dbAvailable = await ensureDbOpen();
-
- if (!dbAvailable) {
- return {
- content: [{ type: "text" as const, text: "Error: GSD database is not available. Cannot save artifact." }],
- isError: true,
- details: { operation: "save_summary", error: "db_unavailable" },
- };
- }
-
- // Validate artifact_type
- const validTypes = ["SUMMARY", "RESEARCH", "CONTEXT", "ASSESSMENT"];
- if (!validTypes.includes(params.artifact_type)) {
- return {
- content: [{ type: "text" as const, text: `Error: Invalid artifact_type "${params.artifact_type}". Must be one of: ${validTypes.join(", ")}` }],
- isError: true,
- details: { operation: "save_summary", error: "invalid_artifact_type" },
- };
- }
-
- try {
- // Compute relative path from IDs
- let relativePath: string;
- if (params.task_id && params.slice_id) {
- relativePath = `milestones/${params.milestone_id}/slices/${params.slice_id}/tasks/${params.task_id}-${params.artifact_type}.md`;
- } else if (params.slice_id) {
- relativePath = `milestones/${params.milestone_id}/slices/${params.slice_id}/${params.slice_id}-${params.artifact_type}.md`;
- } else {
- relativePath = `milestones/${params.milestone_id}/${params.milestone_id}-${params.artifact_type}.md`;
- }
-
- const { saveArtifactToDb } = await import("./db-writer.js");
- await saveArtifactToDb(
- {
- path: relativePath,
- artifact_type: params.artifact_type,
- content: params.content,
- milestone_id: params.milestone_id,
- slice_id: params.slice_id,
- task_id: params.task_id,
- },
- process.cwd(),
- );
-
- return {
- content: [{ type: "text" as const, text: `Saved ${params.artifact_type} artifact to ${relativePath}` }],
- details: { operation: "save_summary", path: relativePath, artifact_type: params.artifact_type },
- };
- } catch (err) {
- const msg = err instanceof Error ? err.message : String(err);
- process.stderr.write(`gsd-db: gsd_save_summary tool failed: ${msg}\n`);
- return {
- content: [{ type: "text" as const, text: `Error saving artifact: ${msg}` }],
- isError: true,
- details: { operation: "save_summary", error: msg },
- };
- }
- },
- });
-
- // ── gsd_generate_milestone_id — canonical milestone ID generation ──────
- // The LLM cannot generate random suffixes for unique_milestone_ids on its
- // own. This tool calls back into the TS code that owns ID generation,
- // ensuring the preference is always respected and IDs are always valid.
- //
- // Reservation set: tracks IDs returned by this tool but not yet persisted
- // to disk, preventing duplicate M001 when called multiple times (#961).
- const reservedMilestoneIds = new Set();
-
- pi.registerTool({
- name: "gsd_generate_milestone_id",
- label: "Generate Milestone ID",
- description:
- "Generate the next milestone ID for a new GSD milestone. " +
- "Scans existing milestones on disk and respects the unique_milestone_ids preference. " +
- "Always use this tool when creating a new milestone — never invent milestone IDs manually.",
- promptSnippet: "Generate a valid milestone ID (respects unique_milestone_ids preference)",
- promptGuidelines: [
- "ALWAYS call gsd_generate_milestone_id before creating a new milestone directory or writing milestone files.",
- "Never invent or hardcode milestone IDs like M001, M002 — always use this tool.",
- "Call it once per milestone you need to create. For multi-milestone projects, call it once for each milestone in sequence.",
- "The tool returns the correct format based on project preferences (e.g. M001 or M001-r5jzab).",
- ],
- parameters: Type.Object({}),
- async execute(_toolCallId, _params, _signal, _onUpdate, _ctx) {
- try {
- const basePath = process.cwd();
- const existingIds = findMilestoneIds(basePath);
- const uniqueEnabled = !!loadEffectiveGSDPreferences()?.preferences?.unique_milestone_ids;
- // Combine on-disk IDs with previously reserved (but not yet persisted) IDs
- const allIds = [...new Set([...existingIds, ...reservedMilestoneIds])];
- const newId = nextMilestoneId(allIds, uniqueEnabled);
- reservedMilestoneIds.add(newId);
- return {
- content: [{ type: "text" as const, text: newId }],
- details: { operation: "generate_milestone_id", id: newId, existingCount: existingIds.length, reservedCount: reservedMilestoneIds.size, uniqueEnabled },
- };
- } catch (err) {
- const msg = err instanceof Error ? err.message : String(err);
- return {
- content: [{ type: "text" as const, text: `Error generating milestone ID: ${msg}` }],
- isError: true,
- details: { operation: "generate_milestone_id", error: msg },
- };
- }
- },
- });
-
- // ── session_start: render branded GSD header + load tool keys + remote status ──
- pi.on("session_start", async (_event, ctx) => {
- // Clear per-session state that must not leak across sessions (e.g. RPC mode)
- depthVerificationDone = false;
-
- // Theme access throws in RPC mode (no TUI) — header is decorative, skip it
- try {
- const theme = ctx.ui.theme;
- const version = process.env.GSD_VERSION || "0.0.0";
-
- const logoText = GSD_LOGO_LINES.map((line) => theme.fg("accent", line)).join("\n");
- const titleLine = ` ${theme.bold("Get Shit Done")} ${theme.fg("dim", `v${version}`)}`;
-
- const headerContent = `${logoText}\n${titleLine}`;
- ctx.ui.setHeader((_ui, _theme) => new Text(headerContent, 1, 0));
- } catch {
- // RPC mode — no TUI, skip header rendering
- }
-
- // Load tool API keys from auth.json into environment
- loadToolApiKeys();
-
- // Notify remote questions status if configured
- try {
- const [{ getRemoteConfigStatus }, { getLatestPromptSummary }] = await Promise.all([
- import("../remote-questions/config.js"),
- import("../remote-questions/status.js"),
- ]);
- const status = getRemoteConfigStatus();
- const latest = getLatestPromptSummary();
- if (!status.includes("not configured")) {
- const suffix = latest ? `\nLast remote prompt: ${latest.id} (${latest.status})` : "";
- ctx.ui.notify(`${status}${suffix}`, status.includes("disabled") ? "warning" : "info");
- }
- } catch {
- // Remote questions module not available — ignore
- }
- });
-
- // ── Ctrl+Alt+G shortcut — GSD dashboard overlay ────────────────────────
- pi.registerShortcut(Key.ctrlAlt("g"), {
- description: shortcutDesc("Open GSD dashboard", "/gsd status"),
- handler: async (ctx) => {
- // Only show if .gsd/ exists
- if (!existsSync(join(process.cwd(), ".gsd"))) {
- ctx.ui.notify("No .gsd/ directory found. Run /gsd to start.", "info");
- return;
- }
-
- await ctx.ui.custom(
- (tui, theme, _kb, done) => {
- return new GSDDashboardOverlay(tui, theme, () => done());
- },
- {
- overlay: true,
- overlayOptions: {
- width: "90%",
- minWidth: 80,
- maxHeight: "92%",
- anchor: "center",
- },
- },
- );
- },
- });
-
- // ── before_agent_start: inject GSD contract into true system prompt ─────
- pi.on("before_agent_start", async (event, ctx: ExtensionContext) => {
- if (!existsSync(join(process.cwd(), ".gsd"))) return;
-
- const stopContextTimer = debugTime("context-inject");
- const systemContent = loadPrompt("system");
- const loadedPreferences = loadEffectiveGSDPreferences();
- if (shouldPromptToEnableCmux(loadedPreferences?.preferences)) {
- markCmuxPromptShown();
- ctx.ui.notify(
- "cmux detected. Run /gsd cmux on to enable sidebar metadata, notifications, and visual subagent splits for this project.",
- "info",
- );
- }
- let preferenceBlock = "";
- if (loadedPreferences) {
- const cwd = process.cwd();
- const report = resolveAllSkillReferences(loadedPreferences.preferences, cwd);
- preferenceBlock = `\n\n${renderPreferencesForSystemPrompt(loadedPreferences.preferences, report.resolutions)}`;
-
- // Emit warnings for unresolved skill references
- if (report.warnings.length > 0) {
- ctx.ui.notify(
- `GSD skill preferences: ${report.warnings.length} unresolved skill${report.warnings.length === 1 ? "" : "s"}: ${report.warnings.join(", ")}`,
- "warning",
- );
- }
- }
-
- // Load project knowledge if available
- let knowledgeBlock = "";
- const knowledgePath = resolveGsdRootFile(process.cwd(), "KNOWLEDGE");
- if (existsSync(knowledgePath)) {
- try {
- const content = readFileSync(knowledgePath, "utf-8").trim();
- if (content) {
- knowledgeBlock = `\n\n[PROJECT KNOWLEDGE — Rules, patterns, and lessons learned]\n\n${content}`;
- }
- } catch {
- // File read error — skip knowledge injection
- }
- }
-
- // Inject auto-learned project memories
- let memoryBlock = "";
- try {
- const { getActiveMemoriesRanked, formatMemoriesForPrompt } = await import("./memory-store.js");
- const memories = getActiveMemoriesRanked(30);
- if (memories.length > 0) {
- const formatted = formatMemoriesForPrompt(memories, 2000);
- if (formatted) {
- memoryBlock = `\n\n${formatted}`;
- }
- }
- } catch { /* non-fatal */ }
-
- // Detect skills installed during this auto-mode session
- let newSkillsBlock = "";
- if (hasSkillSnapshot()) {
- const newSkills = detectNewSkills();
- if (newSkills.length > 0) {
- newSkillsBlock = formatSkillsXml(newSkills);
- }
- }
-
- // Warn if deprecated agent-instructions.md files are still present
- warnDeprecatedAgentInstructions();
-
- const injection = await buildGuidedExecuteContextInjection(event.prompt, process.cwd());
-
- // Worktree context — override the static CWD in the system prompt
- let worktreeBlock = "";
- const worktreeName = getActiveWorktreeName();
- const worktreeMainCwd = getWorktreeOriginalCwd();
- const autoWorktree = getActiveAutoWorktreeContext();
- if (worktreeName && worktreeMainCwd) {
- worktreeBlock = [
- "",
- "",
- "[WORKTREE CONTEXT — OVERRIDES CURRENT WORKING DIRECTORY ABOVE]",
- `IMPORTANT: Ignore the "Current working directory" shown earlier in this prompt.`,
- `The actual current working directory is: ${toPosixPath(process.cwd())}`,
- "",
- `You are working inside a GSD worktree.`,
- `- Worktree name: ${worktreeName}`,
- `- Worktree path (this is the real cwd): ${toPosixPath(process.cwd())}`,
- `- Main project: ${toPosixPath(worktreeMainCwd)}`,
- `- Branch: worktree/${worktreeName}`,
- "",
- "All file operations, bash commands, and GSD state resolve against the worktree path above.",
- "Use /worktree merge to merge changes back. Use /worktree return to switch back to the main tree.",
- ].join("\n");
- } else if (autoWorktree) {
- worktreeBlock = [
- "",
- "",
- "[WORKTREE CONTEXT — OVERRIDES CURRENT WORKING DIRECTORY ABOVE]",
- `IMPORTANT: Ignore the "Current working directory" shown earlier in this prompt.`,
- `The actual current working directory is: ${toPosixPath(process.cwd())}`,
- "",
- "You are working inside a GSD auto-worktree.",
- `- Milestone worktree: ${autoWorktree.worktreeName}`,
- `- Worktree path (this is the real cwd): ${toPosixPath(process.cwd())}`,
- `- Main project: ${toPosixPath(autoWorktree.originalBase)}`,
- `- Branch: ${autoWorktree.branch}`,
- "",
- "All file operations, bash commands, and GSD state resolve against the worktree path above.",
- "Write every .gsd artifact in the worktree path above, never in the main project tree.",
- ].join("\n");
- }
-
- const fullSystem = `${event.systemPrompt}\n\n[SYSTEM CONTEXT — GSD]\n\n${systemContent}${preferenceBlock}${knowledgeBlock}${memoryBlock}${newSkillsBlock}${worktreeBlock}`;
- stopContextTimer({
- systemPromptSize: fullSystem.length,
- injectionSize: injection?.length ?? 0,
- hasPreferences: preferenceBlock.length > 0,
- hasNewSkills: newSkillsBlock.length > 0,
- });
-
- return {
- systemPrompt: fullSystem,
- ...(injection
- ? {
- message: {
- customType: "gsd-guided-context",
- content: injection,
- display: false,
- },
- }
- : {}),
- };
- });
-
- // ── agent_end: auto-mode advancement or auto-start after discuss ───────────
- pi.on("agent_end", async (event, ctx: ExtensionContext) => {
- // If discuss phase just finished, start auto-mode
- if (checkAutoStartAfterDiscuss()) {
- depthVerificationDone = false;
- activeQueuePhase = false;
- return;
- }
-
- // If auto-mode is already running, advance to next unit
- if (!isAutoActive()) return;
-
- // Fresh-session auto-mode intentionally aborts the previous session during
- // cmdCtx.newSession(). Ignore that agent_end so we neither pause nor
- // resolve the new unit with an event from the old session.
- if (isSessionSwitchInFlight()) {
- return;
- }
-
- // If the agent was aborted (user pressed Escape) or hit a provider
- // error (fetch failure, rate limit, etc.), pause auto-mode instead of
- // advancing. This preserves the conversation so the user can inspect
- // what happened, interact with the agent, or resume.
- const lastMsg = event.messages[event.messages.length - 1];
- if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "aborted") {
- await pauseAuto(ctx, pi);
- return;
- }
- if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "error") {
- const errorDetail =
- "errorMessage" in lastMsg && lastMsg.errorMessage
- ? `: ${lastMsg.errorMessage}`
- : "";
-
- const errorMsg = ("errorMessage" in lastMsg && lastMsg.errorMessage) ? String(lastMsg.errorMessage) : "";
-
- // ── Transient network error retry ──────────────────────────────────
- // Before falling back to a different model, retry the current model
- // for transient network errors (connection reset, timeout, DNS, etc.).
- // This prevents providers with occasional network flakiness from being
- // immediately abandoned in favor of fallback models (#941).
- if (isTransientNetworkError(errorMsg)) {
- const currentModelId = ctx.model?.id ?? "unknown";
- const retryKey = `network-retry:${currentModelId}`;
- const maxRetries = 2;
- const currentRetries = networkRetryCounters.get(retryKey) ?? 0;
-
- if (currentRetries < maxRetries) {
- networkRetryCounters.set(retryKey, currentRetries + 1);
- const attempt = currentRetries + 1;
- const delayMs = attempt * 3000; // 3s, 6s backoff
- ctx.ui.notify(
- `Network error on ${currentModelId}${errorDetail}. Retry ${attempt}/${maxRetries} in ${delayMs / 1000}s...`,
- "warning",
- );
- setTimeout(() => {
- pi.sendMessage(
- { customType: "gsd-auto-timeout-recovery", content: "Continue execution — retrying after transient network error.", display: false },
- { triggerTurn: true },
- );
- }, delayMs);
- return;
- }
- // Retries exhausted — clear counter and fall through to fallback logic
- networkRetryCounters.delete(retryKey);
- ctx.ui.notify(
- `Network retries exhausted for ${currentModelId}. Attempting model fallback.`,
- "warning",
- );
- }
-
- const dash = getAutoDashboardData();
- if (dash.currentUnit) {
- const modelConfig = resolveModelWithFallbacksForUnit(dash.currentUnit.type);
- if (modelConfig && modelConfig.fallbacks.length > 0) {
- const availableModels = ctx.modelRegistry.getAvailable();
- const currentModelId = ctx.model?.id;
-
- const nextModelId = getNextFallbackModel(currentModelId, modelConfig);
-
- if (nextModelId) {
- // Clear any network retry counters when switching models
- networkRetryCounters.clear();
-
- let modelToSet;
- const slashIdx = nextModelId.indexOf("/");
- if (slashIdx !== -1) {
- const provider = nextModelId.substring(0, slashIdx);
- const id = nextModelId.substring(slashIdx + 1);
- modelToSet = availableModels.find(
- m => m.provider.toLowerCase() === provider.toLowerCase()
- && m.id.toLowerCase() === id.toLowerCase()
- );
- } else {
- const currentProvider = ctx.model?.provider;
- const exactProviderMatch = availableModels.find(
- m => m.id === nextModelId && m.provider === currentProvider
- );
- modelToSet = exactProviderMatch ?? availableModels.find(m => m.id === nextModelId);
- }
-
- if (modelToSet) {
- const ok = await pi.setModel(modelToSet, { persist: false });
- if (ok) {
- ctx.ui.notify(`Model error${errorDetail}. Switched to fallback: ${nextModelId} and resuming.`, "warning");
- // Trigger a generic "Continue execution" to resume the task since the previous attempt failed
- pi.sendMessage(
- { customType: "gsd-auto-timeout-recovery", content: "Continue execution.", display: false },
- { triggerTurn: true }
- );
- return;
- }
- }
- }
- }
- }
-
- // ── Session model recovery (#1065) ──────────────────────────────────
- // Before pausing, attempt to restore the model captured at auto-mode
- // start. This prevents cross-session model leakage: when fallback
- // chains are exhausted (or absent), the session retries with the model
- // the user originally chose instead of reading (possibly stale) global
- // preferences that another concurrent session may have modified.
- const sessionModel = getAutoModeStartModel();
- if (sessionModel) {
- const currentModelId = ctx.model?.id;
- const currentProvider = ctx.model?.provider;
- // Only attempt recovery if the current model diverged from the session model
- if (currentModelId !== sessionModel.id || currentProvider !== sessionModel.provider) {
- const availableModels = ctx.modelRegistry.getAvailable();
- const startModel = availableModels.find(
- m => m.provider === sessionModel.provider && m.id === sessionModel.id,
- );
- if (startModel) {
- const ok = await pi.setModel(startModel, { persist: false });
- if (ok) {
- networkRetryCounters.clear();
- ctx.ui.notify(
- `Model error${errorDetail}. Restored session model: ${sessionModel.provider}/${sessionModel.id} and resuming.`,
- "warning",
- );
- pi.sendMessage(
- { customType: "gsd-auto-timeout-recovery", content: "Continue execution.", display: false },
- { triggerTurn: true },
- );
- return;
- }
- }
- }
- }
-
- // Classify the error: transient (auto-resume) vs permanent (manual resume)
- const classification = classifyProviderError(errorMsg);
-
- // Extract explicit retry-after from the message or response metadata
- const explicitRetryAfterMs = ("retryAfterMs" in lastMsg && typeof lastMsg.retryAfterMs === "number")
- ? lastMsg.retryAfterMs
- : undefined;
- if (classification.isTransient) {
- consecutiveTransientErrors += 1;
- } else {
- consecutiveTransientErrors = 0;
- }
- const baseRetryAfterMs = explicitRetryAfterMs ?? classification.suggestedDelayMs;
- const retryAfterMs = classification.isTransient ? baseRetryAfterMs * 2 ** Math.max(0, consecutiveTransientErrors - 1) : baseRetryAfterMs;
- const allowAutoResume = classification.isTransient
- && consecutiveTransientErrors <= MAX_TRANSIENT_AUTO_RESUMES;
-
- if (classification.isTransient && !allowAutoResume) {
- ctx.ui.notify(
- `Transient provider errors persisted after ${MAX_TRANSIENT_AUTO_RESUMES} auto-resume attempts. Pausing for manual review.`,
- "warning",
- );
- }
-
- await pauseAutoForProviderError(ctx.ui, errorDetail, () => pauseAuto(ctx, pi), {
- isRateLimit: classification.isRateLimit,
- isTransient: allowAutoResume,
- retryAfterMs,
- resume: allowAutoResume
- ? () => {
- pi.sendMessage(
- { customType: "gsd-auto-timeout-recovery", content: "Continue execution \u2014 provider error recovery delay elapsed.", display: false },
- { triggerTurn: true },
- );
- }
- : undefined,
- });
- return;
- }
-
- try {
- consecutiveTransientErrors = 0;
- networkRetryCounters.clear(); // Clear network retry state on successful unit completion
- resolveAgentEnd(event);
- } catch (err) {
- // Safety net: if resolveAgentEnd throws, ensure auto-mode stops gracefully (#381).
- const message = err instanceof Error ? err.message : String(err);
- ctx.ui.notify(
- `Auto-mode error in agent_end handler: ${message}. Stopping auto-mode.`,
- "error",
- );
- try {
- await pauseAuto(ctx, pi);
- } catch {
- // Last resort — at least log
- }
- }
- });
-
- // ── session_before_compact ────────────────────────────────────────────────
- pi.on("session_before_compact", async (_event, _ctx: ExtensionContext) => {
- // Block compaction during auto-mode — each unit is a fresh session
- // Also block during paused state — context is valuable for the user
- if (isAutoActive() || isAutoPaused()) {
- return { cancel: true };
- }
-
- const basePath = process.cwd();
- const state = await deriveState(basePath);
-
- // Only save continue.md if we're actively executing a task
- if (!state.activeMilestone || !state.activeSlice || !state.activeTask) return;
- if (state.phase !== "executing") return;
-
- const sDir = resolveSlicePath(basePath, state.activeMilestone.id, state.activeSlice.id);
- if (!sDir) return;
-
- // Check for existing continue file (new naming or legacy)
- const existingFile = resolveSliceFile(basePath, state.activeMilestone.id, state.activeSlice.id, "CONTINUE");
- if (existingFile && await loadFile(existingFile)) return;
- const legacyContinue = join(sDir, "continue.md");
- if (await loadFile(legacyContinue)) return;
-
- const continuePath = join(sDir, buildSliceFileName(state.activeSlice.id, "CONTINUE"));
-
- const continueData = {
- frontmatter: {
- milestone: state.activeMilestone.id,
- slice: state.activeSlice.id,
- task: state.activeTask.id,
- step: 0,
- totalSteps: 0,
- status: "compacted" as const,
- savedAt: new Date().toISOString(),
- },
- completedWork: `Task ${state.activeTask.id} (${state.activeTask.title}) was in progress when compaction occurred.`,
- remainingWork: "Check the task plan for remaining steps.",
- decisions: "Check task summary files for prior decisions.",
- context: "Session was auto-compacted by Pi. Resume with /gsd.",
- nextAction: `Resume task ${state.activeTask.id}: ${state.activeTask.title}.`,
- };
-
- await saveFile(continuePath, formatContinue(continueData));
- });
-
- // ── session_shutdown: save activity log on Ctrl+C / SIGTERM ─────────────
- pi.on("session_shutdown", async (_event, ctx: ExtensionContext) => {
- if (isParallelActive()) {
- try {
- await shutdownParallel(process.cwd());
- } catch { /* best-effort */ }
- }
-
- if (!isAutoActive() && !isAutoPaused()) return;
-
- // Save the current session — the lock file stays on disk
- // so the next /gsd auto knows it was interrupted
- const dash = getAutoDashboardData();
- if (dash.currentUnit) {
- saveActivityLog(ctx, dash.basePath, dash.currentUnit.type, dash.currentUnit.id);
- }
- });
-
- // ── tool_call: block CONTEXT.md writes during discussion without depth verification ──
- pi.on("tool_call", async (event) => {
- if (!isToolCallEventType("write", event)) return;
- const result = shouldBlockContextWrite(
- event.toolName,
- event.input.path,
- getDiscussionMilestoneId(),
- isDepthVerified(),
- activeQueuePhase,
- );
- if (result.block) return result;
- });
-
- // ── tool_result: persist discussion exchanges & detect depth gate ──────
- pi.on("tool_result", async (event) => {
- if (event.toolName !== "ask_user_questions") return;
-
- const milestoneId = getDiscussionMilestoneId();
- if (!milestoneId) return;
-
- const details = event.details as any;
- if (details?.cancelled || !details?.response) return;
-
- // ── Depth gate detection ──────────────────────────────────────────
- const questions: any[] = (event.input as any)?.questions ?? [];
- for (const q of questions) {
- if (typeof q.id === "string" && q.id.includes("depth_verification")) {
- depthVerificationDone = true;
- break;
- }
- }
-
- // ── Persist exchange to DISCUSSION.md ──────────────────────────────
- const basePath = process.cwd();
- const milestoneDir = resolveMilestonePath(basePath, milestoneId);
- if (!milestoneDir) return;
-
- const fileName = buildMilestoneFileName(milestoneId, "DISCUSSION");
- const discussionPath = join(milestoneDir, fileName);
- const timestamp = new Date().toISOString();
-
- // Format exchange as markdown
- const lines: string[] = [`## Exchange — ${timestamp}`, ""];
-
- for (const q of questions) {
- lines.push(`### ${q.header ?? "Question"}`);
- lines.push("");
- lines.push(q.question ?? "");
- if (Array.isArray(q.options)) {
- lines.push("");
- for (const opt of q.options) {
- lines.push(`- **${opt.label}** — ${opt.description ?? ""}`);
- }
- }
-
- // Append user response for this question
- const answer = details.response?.answers?.[q.id];
- if (answer) {
- lines.push("");
- const selected = Array.isArray(answer.selected) ? answer.selected.join(", ") : answer.selected;
- lines.push(`**Selected:** ${selected}`);
- if (answer.notes) {
- lines.push(`**Notes:** ${answer.notes}`);
- }
- }
- lines.push("");
- }
-
- lines.push("---", "");
-
- const newBlock = lines.join("\n");
- const existing = await loadFile(discussionPath) ?? `# ${milestoneId} Discussion Log\n\n`;
- await saveFile(discussionPath, existing + newBlock);
- });
-
- // ── tool_execution_start/end: track in-flight tools for idle detection ──
- pi.on("tool_execution_start", async (event) => {
- if (!isAutoActive()) return;
- markToolStart(event.toolCallId);
- });
-
- pi.on("tool_execution_end", async (event) => {
- markToolEnd(event.toolCallId);
- });
-}
-
-async function buildGuidedExecuteContextInjection(prompt: string, basePath: string): Promise {
- const executeMatch = prompt.match(/Execute the next task:\s+(T\d+)\s+\("([^"]+)"\)\s+in slice\s+(S\d+)\s+of milestone\s+(M\d+(?:-[a-z0-9]{6})?)/i);
- if (executeMatch) {
- const [, taskId, taskTitle, sliceId, milestoneId] = executeMatch;
- return buildTaskExecutionContextInjection(basePath, milestoneId, sliceId, taskId, taskTitle);
- }
-
- const resumeMatch = prompt.match(/Resume interrupted work\.[\s\S]*?slice\s+(S\d+)\s+of milestone\s+(M\d+(?:-[a-z0-9]{6})?)/i);
- if (resumeMatch) {
- const [, sliceId, milestoneId] = resumeMatch;
- const state = await deriveState(basePath);
- if (
- state.activeMilestone?.id === milestoneId &&
- state.activeSlice?.id === sliceId &&
- state.activeTask
- ) {
- return buildTaskExecutionContextInjection(
- basePath,
- milestoneId,
- sliceId,
- state.activeTask.id,
- state.activeTask.title,
- );
- }
- }
-
- return null;
-}
-
-async function buildTaskExecutionContextInjection(
- basePath: string,
- milestoneId: string,
- sliceId: string,
- taskId: string,
- taskTitle: string,
-): Promise {
- const taskPlanPath = resolveTaskFile(basePath, milestoneId, sliceId, taskId, "PLAN");
- const taskPlanRelPath = relTaskFile(basePath, milestoneId, sliceId, taskId, "PLAN");
- const taskPlanContent = taskPlanPath ? await loadFile(taskPlanPath) : null;
- const taskPlanInline = taskPlanContent
- ? [
- "## Inlined Task Plan (authoritative local execution contract)",
- `Source: \`${taskPlanRelPath}\``,
- "",
- taskPlanContent.trim(),
- ].join("\n")
- : [
- "## Inlined Task Plan (authoritative local execution contract)",
- `Task plan not found at dispatch time. Read \`${taskPlanRelPath}\` before executing.`,
- ].join("\n");
-
- const slicePlanPath = resolveSliceFile(basePath, milestoneId, sliceId, "PLAN");
- const slicePlanRelPath = relSliceFile(basePath, milestoneId, sliceId, "PLAN");
- const slicePlanContent = slicePlanPath ? await loadFile(slicePlanPath) : null;
- const slicePlanExcerpt = extractSliceExecutionExcerpt(slicePlanContent, slicePlanRelPath);
-
- const priorTaskLines = await buildCarryForwardLines(basePath, milestoneId, sliceId, taskId);
- const resumeSection = await buildResumeSection(basePath, milestoneId, sliceId);
-
- const activeOverrides = await loadActiveOverrides(basePath);
- const overridesSection = formatOverridesSection(activeOverrides);
-
- return [
- "[GSD Guided Execute Context]",
- "Use this injected context as startup context for guided task execution. Treat the inlined task plan as the authoritative local execution contract. Use source artifacts to verify details and run checks.",
- overridesSection, "",
- "",
- resumeSection,
- "",
- "## Carry-Forward Context",
- ...priorTaskLines,
- "",
- taskPlanInline,
- "",
- slicePlanExcerpt,
- "",
- "## Backing Source Artifacts",
- `- Slice plan: \`${slicePlanRelPath}\``,
- `- Task plan source: \`${taskPlanRelPath}\``,
- ].join("\n");
-}
-
-async function buildCarryForwardLines(
- basePath: string,
- milestoneId: string,
- sliceId: string,
- taskId: string,
-): Promise {
- const tDir = resolveTasksDir(basePath, milestoneId, sliceId);
- if (!tDir) return ["- No prior task summaries in this slice."];
-
- const currentNum = parseInt(taskId.replace(/^T/, ""), 10);
- const sRel = relSlicePath(basePath, milestoneId, sliceId);
- const summaryFiles = resolveTaskFiles(tDir, "SUMMARY")
- .filter((file) => parseInt(file.replace(/^T/, ""), 10) < currentNum)
- .sort();
-
- if (summaryFiles.length === 0) return ["- No prior task summaries in this slice."];
-
- const lines = await Promise.all(summaryFiles.map(async (file) => {
- const absPath = join(tDir, file);
- const content = await loadFile(absPath);
- const relPath = `${sRel}/tasks/${file}`;
- if (!content) return `- \`${relPath}\``;
-
- const summary = parseSummary(content);
- const provided = summary.frontmatter.provides.slice(0, 2).join("; ");
- const decisions = summary.frontmatter.key_decisions.slice(0, 2).join("; ");
- const patterns = summary.frontmatter.patterns_established.slice(0, 2).join("; ");
- const diagnostics = extractMarkdownSection(content, "Diagnostics");
-
- const parts = [summary.title || relPath];
- if (summary.oneLiner) parts.push(summary.oneLiner);
- if (provided) parts.push(`provides: ${provided}`);
- if (decisions) parts.push(`decisions: ${decisions}`);
- if (patterns) parts.push(`patterns: ${patterns}`);
- if (diagnostics) parts.push(`diagnostics: ${oneLine(diagnostics)}`);
-
- return `- \`${relPath}\` — ${parts.join(" | ")}`;
- }));
-
- return lines;
-}
-
-async function buildResumeSection(basePath: string, milestoneId: string, sliceId: string): Promise {
- const continueFile = resolveSliceFile(basePath, milestoneId, sliceId, "CONTINUE");
- const legacyDir = resolveSlicePath(basePath, milestoneId, sliceId);
- const legacyPath = legacyDir ? join(legacyDir, "continue.md") : null;
- const continueContent = continueFile ? await loadFile(continueFile) : null;
- const legacyContent = !continueContent && legacyPath ? await loadFile(legacyPath) : null;
- const resolvedContent = continueContent ?? legacyContent;
- const resolvedRelPath = continueContent
- ? relSliceFile(basePath, milestoneId, sliceId, "CONTINUE")
- : (legacyPath ? `${relSlicePath(basePath, milestoneId, sliceId)}/continue.md` : null);
-
- if (!resolvedContent || !resolvedRelPath) {
- return ["## Resume State", "- No continue file present. Start from the top of the task plan."].join("\n");
- }
-
- const cont = parseContinue(resolvedContent);
- const lines = [
- "## Resume State",
- `Source: \`${resolvedRelPath}\``,
- `- Status: ${cont.frontmatter.status || "in_progress"}`,
- ];
-
- if (cont.frontmatter.step && cont.frontmatter.totalSteps) {
- lines.push(`- Progress: step ${cont.frontmatter.step} of ${cont.frontmatter.totalSteps}`);
- }
- if (cont.completedWork) lines.push(`- Completed: ${oneLine(cont.completedWork)}`);
- if (cont.remainingWork) lines.push(`- Remaining: ${oneLine(cont.remainingWork)}`);
- if (cont.decisions) lines.push(`- Decisions: ${oneLine(cont.decisions)}`);
- if (cont.nextAction) lines.push(`- Next action: ${oneLine(cont.nextAction)}`);
-
- return lines.join("\n");
-}
-
-function extractSliceExecutionExcerpt(content: string | null, relPath: string): string {
- if (!content) {
- return [
- "## Slice Plan Excerpt",
- `Slice plan not found at dispatch time. Read \`${relPath}\` before running slice-level verification.`,
- ].join("\n");
- }
-
- const lines = content.split("\n");
- const goalLine = lines.find((line) => line.startsWith("**Goal:**"))?.trim();
- const demoLine = lines.find((line) => line.startsWith("**Demo:**"))?.trim();
- const verification = extractMarkdownSection(content, "Verification");
- const observability = extractMarkdownSection(content, "Observability / Diagnostics");
-
- const parts = ["## Slice Plan Excerpt", `Source: \`${relPath}\``];
- if (goalLine) parts.push(goalLine);
- if (demoLine) parts.push(demoLine);
- if (verification) parts.push("", "### Slice Verification", verification.trim());
- if (observability) parts.push("", "### Slice Observability / Diagnostics", observability.trim());
- return parts.join("\n");
-}
-
-function extractMarkdownSection(content: string, heading: string): string | null {
- const match = new RegExp(`^## ${escapeRegExp(heading)}\\s*$`, "m").exec(content);
- if (!match) return null;
- const start = match.index + match[0].length;
- const rest = content.slice(start);
- const nextHeading = rest.match(/^##\s+/m);
- const end = nextHeading?.index ?? rest.length;
- return rest.slice(0, end).trim();
-}
-
-function escapeRegExp(value: string): string {
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
-}
-
-function oneLine(text: string): string {
- return text.replace(/\s+/g, " ").trim();
+export default async function registerExtension(pi: ExtensionAPI) {
+ const { registerGsdExtension } = await import("./bootstrap/register-extension.js");
+ registerGsdExtension(pi);
}
diff --git a/src/resources/extensions/gsd/progress-score.ts b/src/resources/extensions/gsd/progress-score.ts
index 59b71f602..1d1e0f3f9 100644
--- a/src/resources/extensions/gsd/progress-score.ts
+++ b/src/resources/extensions/gsd/progress-score.ts
@@ -14,6 +14,8 @@ import {
getHealthTrend,
getConsecutiveErrorUnits,
getHealthHistory,
+ getLatestHealthIssues,
+ getLatestHealthFixes,
type HealthSnapshot,
} from "./doctor-proactive.js";
@@ -77,6 +79,27 @@ export function computeProgressScore(): ProgressScore {
signals.push({ kind: "neutral", label: "No health data yet" });
}
+ // Surface actual doctor issue details when degraded
+ if (level !== "green") {
+ const latestIssues = getLatestHealthIssues();
+ // Show up to 5 most relevant issues (errors first, then warnings)
+ const sorted = [...latestIssues].sort((a, b) => {
+ const rank = { error: 0, warning: 1, info: 2 };
+ return rank[a.severity] - rank[b.severity];
+ });
+ for (const issue of sorted.slice(0, 5)) {
+ signals.push({
+ kind: issue.severity === "error" ? "negative" : "neutral",
+ label: issue.message,
+ });
+ }
+
+ const latestFixes = getLatestHealthFixes();
+ for (const fix of latestFixes.slice(0, 3)) {
+ signals.push({ kind: "positive", label: `Fixed: ${fix}` });
+ }
+ }
+
const summary = level === "green"
? "Progressing well"
: level === "yellow"
diff --git a/src/resources/extensions/gsd/prompts/forensics.md b/src/resources/extensions/gsd/prompts/forensics.md
index a3922e8e8..71225fcf8 100644
--- a/src/resources/extensions/gsd/prompts/forensics.md
+++ b/src/resources/extensions/gsd/prompts/forensics.md
@@ -1,4 +1,4 @@
-You are investigating a GSD auto-mode failure. The user has described their problem and a structured forensic report has been gathered automatically.
+You are debugging GSD itself. The user is donating their tokens to help find bugs in GSD's source code. Your job is to trace from symptom to root cause in the actual source and produce a filing-ready GitHub issue with specific file:line references and a concrete fix suggestion.
## User's Problem
@@ -10,62 +10,137 @@ You are investigating a GSD auto-mode failure. The user has described their prob
## GSD Source Location
-GSD extension source code is at: {{gsdSourceDir}}
-Key files for understanding failures:
-- auto.ts — unit dispatch loop, stuck detection, timeout recovery
-- session-forensics.ts — trace extraction from activity logs
-- auto-recovery.ts — artifact verification, skip logic
-- crash-recovery.ts — crash lock lifecycle
-- doctor.ts — state integrity checks
+GSD extension source code is at: `{{gsdSourceDir}}`
-You may read these files to identify the specific code path that caused the failure.
+### Source Map by Domain
-## Your Task
+| Domain | Files |
+|--------|-------|
+| **Auto-mode engine** | `auto.ts` `auto-loop.ts` `auto-dispatch.ts` `auto-start.ts` `auto-supervisor.ts` `auto-timers.ts` `auto-timeout-recovery.ts` `auto-unit-closeout.ts` `auto-post-unit.ts` `auto-verification.ts` `auto-recovery.ts` `auto-worktree.ts` `auto-worktree-sync.ts` `auto-model-selection.ts` `auto-budget.ts` `dispatch-guard.ts` |
+| **State & persistence** | `state.ts` `types.ts` `files.ts` `paths.ts` `json-persistence.ts` `atomic-write.ts` |
+| **Forensics & recovery** | `forensics.ts` `session-forensics.ts` `crash-recovery.ts` `session-lock.ts` |
+| **Metrics & telemetry** | `metrics.ts` `skill-telemetry.ts` `token-counter.ts` |
+| **Health & diagnostics** | `doctor.ts` `doctor-types.ts` `doctor-checks.ts` `doctor-format.ts` `doctor-environment.ts` |
+| **Prompts & context** | `prompt-loader.ts` `prompt-cache-optimizer.ts` `context-budget.ts` |
+| **Git & worktrees** | `git-service.ts` `worktree.ts` `worktree-manager.ts` `git-self-heal.ts` |
+| **Commands** | `commands.ts` `commands-inspect.ts` `commands-maintenance.ts` |
-1. **Analyze** the forensic report. Identify the root cause of the user's problem.
+### Runtime Path Reference
-2. **Clarify** if needed. Use ask_user_questions (max 2 questions) to narrow down ambiguity. Only ask if the report is genuinely insufficient — do not ask questions you can answer from the data.
+```
+.gsd/
+├── PROJECT.md, DECISIONS.md, QUEUE.md, STATE.md, REQUIREMENTS.md, OVERRIDES.md, KNOWLEDGE.md, RUNTIME.md
+├── auto.lock — crash lock (JSON: pid, unitType, unitId, sessionFile)
+├── metrics.json — token/cost ledger (units array with cost, tokens, duration)
+├── completed-units.json — array of "type/id" strings
+├── doctor-history.jsonl — doctor check history
+├── activity/ — session activity logs (JSONL per unit)
+│ └── {seq}-{unitType}-{unitId}.jsonl
+├── runtime/
+│ ├── paused-session.json — serialized session when auto pauses
+│ └── headless-context.md — headless resume context
+├── debug/ — debug logs
+├── forensics/ — saved forensic reports
+├── milestones/{ID}/ — milestone artifacts
+│ ├── {ID}-ROADMAP.md, {ID}-RESEARCH.md, {ID}-CONTEXT.md, {ID}-SUMMARY.md
+│ └── slices/{SID}/ — slice artifacts
+│ ├── {SID}-PLAN.md, {SID}-RESEARCH.md, {SID}-UAT-RESULT.md, {SID}-SUMMARY.md
+│ └── tasks/{TID}-PLAN.md, {TID}-SUMMARY.md
+└── worktrees/{milestoneId}/ — per-milestone worktree with replicated .gsd/
+```
-3. **Explain** your findings clearly:
- - What happened (the failure sequence)
- - Why it happened (root cause in GSD's logic)
- - What the user can do to recover (immediate fix)
+### Activity Log Format
-4. **Offer GitHub issue creation.** Ask the user:
- "Would you like me to create a GitHub issue for this on gsd-build/gsd-2?"
+- **Filename**: `{3-digit-seq}-{unitType}-{unitId}.jsonl`
+- Each line is a JSON object with `type: "message"` and a `message` field
+- `message.role: "assistant"` — contains `content[]` array:
+ - `type: "text"` entries hold the agent's reasoning
+ - `type: "toolCall"` entries hold tool invocations (`name`, `id`, `arguments`)
+- `message.role: "toolResult"` — contains `toolCallId`, `toolName`, `isError`, `content`
+- `usage` field on assistant messages: `input`, `output`, `cacheRead`, `cacheWrite`, `totalTokens`, `cost`
+- **To trace a failure**: find the last activity log, search for `isError: true` tool results, then read the agent's reasoning text preceding that error
- If yes, create the issue using bash with `gh issue create`:
- - Repository: gsd-build/gsd-2
- - Labels: bug, auto-generated
- - Title: concise description of the failure
- - Body format:
- ```
- ## Problem
- [1-2 sentence summary]
+### Crash Lock Format (`auto.lock`)
- ## Environment
- - GSD version: [from report]
- - Model: [from report]
- - Unit: [type/id that failed]
+JSON with fields: `pid`, `startedAt`, `unitType`, `unitId`, `unitStartedAt`, `completedUnits`, `sessionFile`
- ## Reproduction Context
- [What was happening when it failed — phase, milestone, slice]
+A stale lock (PID is dead) means the previous auto-mode session crashed mid-unit.
- ## Forensic Findings
- [Key anomalies detected, error traces, relevant tool call sequences]
+### Metrics Ledger Format (`metrics.json`)
- ## Suggested Fix Area
- [File:line references in GSD source if identified]
+```
+{ version: 1, projectStartedAt: , units: [{ type, id, model, startedAt, finishedAt, tokens: { input, output, cacheRead, cacheWrite, total }, cost, toolCalls, assistantMessages, ... }] }
+```
- ---
- *Auto-generated by `/gsd forensics`*
- ```
+A unit dispatched more than once (`type/id` appears multiple times) indicates a stuck loop — the unit completed but artifact verification failed.
- **CRITICAL REDACTION RULES** before creating the issue:
- - Replace all absolute paths with relative paths
- - Remove any API keys, tokens, or credentials
- - Remove any environment variable values
- - Do not include file content (code written by the user)
- - Only include GSD structural information (tool names, file names, error messages)
+## Investigation Protocol
-5. **Report saved.** Remind the user that the full forensic report was saved locally (the path will be in the notification).
+1. **Start with the pre-parsed forensic report** above. The anomaly section contains automated findings — treat these as leads, not conclusions.
+
+2. **Form hypotheses** about which module and code path is responsible. Use the source map to identify candidate files.
+
+3. **Read the actual GSD source code** at `{{gsdSourceDir}}` to confirm or deny each hypothesis. Do not guess what code does — read it.
+
+4. **Trace the code path** from the entry point (usually `auto-loop.ts` dispatch or `auto-dispatch.ts`) through to the failure point. Follow function calls across files.
+
+5. **Identify the specific file and line** where the bug lives. Determine what kind of defect it is:
+ - Missing edge case / unhandled condition
+ - Wrong boolean logic or comparison
+ - Race condition or ordering issue
+ - State corruption (e.g. completed-units.json out of sync with artifacts)
+ - Timeout / recovery logic not triggering correctly
+
+6. **Clarify if needed.** Use ask_user_questions (max 2 questions) only if the report is genuinely insufficient. Do not ask questions you can answer from the data or source code.
+
+## Output
+
+Explain your findings:
+- **What happened** — the failure sequence reconstructed from activity logs and anomalies
+- **Why it happened** — root cause traced to specific code in GSD source, with `file:line` references
+- **Code snippet** — the problematic code and what it should do instead
+- **Recovery** — what the user can do right now to get unstuck
+
+Then **offer GitHub issue creation**: "Would you like me to create a GitHub issue for this on gsd-build/gsd-2?"
+
+If yes, create using `gh issue create` with this format:
+
+```
+## Problem
+[1-2 sentence summary]
+
+## Root Cause
+[Specific file:line in GSD source, with code snippet showing the bug]
+
+## Expected Behavior
+[What the code should do instead — concrete fix suggestion]
+
+## Environment
+- GSD version: [from report]
+- Model: [from report]
+- Unit: [type/id that failed]
+
+## Reproduction Context
+[Phase, milestone, slice, what was happening when it failed]
+
+## Forensic Evidence
+[Key anomalies, error traces, relevant tool call sequences from the report]
+
+---
+*Auto-generated by `/gsd forensics`*
+```
+
+**Repository:** gsd-build/gsd-2
+**Labels:** bug, auto-generated
+
+### Redaction Rules (CRITICAL)
+
+Before creating the issue, you MUST:
+- Replace all absolute paths with relative paths
+- Remove any API keys, tokens, or credentials
+- Remove any environment variable values
+- Do not include user's project code — only GSD structural information (tool names, file names, error messages)
+
+## Report Saved
+
+Remind the user that the full forensic report was saved locally (the path will be in the notification).
diff --git a/src/resources/extensions/gsd/tests/auto-loop.test.ts b/src/resources/extensions/gsd/tests/auto-loop.test.ts
index e51fb40fa..d1070021d 100644
--- a/src/resources/extensions/gsd/tests/auto-loop.test.ts
+++ b/src/resources/extensions/gsd/tests/auto-loop.test.ts
@@ -962,21 +962,25 @@ test("auto.ts startAuto calls autoLoop (not dispatchNextUnit as first dispatch)"
);
});
-test("index.ts agent_end handler calls resolveAgentEnd (not handleAgentEnd)", () => {
- const src = readFileSync(
- resolve(import.meta.dirname, "..", "index.ts"),
+test("agent_end handler calls resolveAgentEnd (not handleAgentEnd)", () => {
+ const hooksSrc = readFileSync(
+ resolve(import.meta.dirname, "..", "bootstrap", "register-hooks.ts"),
+ "utf-8",
+ );
+ // Verify the agent_end hook is registered
+ const handlerIdx = hooksSrc.indexOf('pi.on("agent_end"');
+ assert.ok(handlerIdx > -1, "register-hooks.ts must have an agent_end handler");
+
+ const recoverySrc = readFileSync(
+ resolve(import.meta.dirname, "..", "bootstrap", "agent-end-recovery.ts"),
"utf-8",
);
- // Find the agent_end handler success path
- const handlerIdx = src.indexOf('pi.on("agent_end"');
- assert.ok(handlerIdx > -1, "index.ts must have an agent_end handler");
- const handlerBlock = src.slice(handlerIdx, handlerIdx + 10000);
assert.ok(
- handlerBlock.includes("resolveAgentEnd(event)"),
+ recoverySrc.includes("resolveAgentEnd(event)"),
"agent_end success path must call resolveAgentEnd(event) instead of handleAgentEnd(ctx, pi)",
);
assert.ok(
- handlerBlock.includes("isSessionSwitchInFlight()"),
+ recoverySrc.includes("isSessionSwitchInFlight()"),
"agent_end handler must ignore session-switch agent_end events from cmdCtx.newSession()",
);
});
diff --git a/src/resources/extensions/gsd/tests/doctor-proactive.test.ts b/src/resources/extensions/gsd/tests/doctor-proactive.test.ts
index 0bbbf2a83..f45f6a75e 100644
--- a/src/resources/extensions/gsd/tests/doctor-proactive.test.ts
+++ b/src/resources/extensions/gsd/tests/doctor-proactive.test.ts
@@ -176,9 +176,9 @@ async function main(): Promise {
recordHealthSnapshot(2, 3, 1);
const summary = formatHealthSummary();
- assertTrue(summary.includes("2E/3W"), "summary includes error/warning counts");
- assertTrue(summary.includes("fixes:1"), "summary includes fix count");
- assertTrue(summary.includes("streak:1/5"), "summary includes error streak");
+ assertTrue(summary.includes("2 errors") && summary.includes("3 warnings"), "summary includes error/warning counts");
+ assertTrue(summary.includes("1 fix applied"), "summary includes fix count");
+ assertTrue(summary.includes("1 of 5 consecutive errors"), "summary includes error streak");
}
// ─── Pre-Dispatch Health Gate ─────────────────────────────────────
diff --git a/src/resources/extensions/gsd/tests/provider-errors.test.ts b/src/resources/extensions/gsd/tests/provider-errors.test.ts
index 35a7dd9ff..0512b4d90 100644
--- a/src/resources/extensions/gsd/tests/provider-errors.test.ts
+++ b/src/resources/extensions/gsd/tests/provider-errors.test.ts
@@ -261,38 +261,38 @@ test("pauseAutoForProviderError falls back to indefinite pause when not rate lim
// ── Escalating backoff for transient errors (#1166) ─────────────────────────
-test("index.ts tracks consecutive transient errors for escalating backoff", () => {
- const indexSource = readFileSync(join(__dirname, "..", "index.ts"), "utf-8");
+test("agent-end-recovery.ts tracks consecutive transient errors for escalating backoff", () => {
+ const src = readFileSync(join(__dirname, "..", "bootstrap", "agent-end-recovery.ts"), "utf-8");
assert.ok(
- indexSource.includes("consecutiveTransientErrors"),
- "index.ts must track consecutiveTransientErrors for escalating backoff (#1166)",
+ src.includes("consecutiveTransientErrors"),
+ "agent-end-recovery.ts must track consecutiveTransientErrors for escalating backoff (#1166)",
);
assert.ok(
- indexSource.includes("MAX_TRANSIENT_AUTO_RESUMES"),
- "index.ts must define MAX_TRANSIENT_AUTO_RESUMES to cap infinite retries (#1166)",
+ src.includes("MAX_TRANSIENT_AUTO_RESUMES"),
+ "agent-end-recovery.ts must define MAX_TRANSIENT_AUTO_RESUMES to cap infinite retries (#1166)",
);
});
-test("index.ts resets consecutive transient error counter on success", () => {
- const indexSource = readFileSync(join(__dirname, "..", "index.ts"), "utf-8");
+test("agent-end-recovery.ts resets consecutive transient error counter on success", () => {
+ const src = readFileSync(join(__dirname, "..", "bootstrap", "agent-end-recovery.ts"), "utf-8");
- // After successful unit completion, the counter must be reset.
+ // After successful agent_end (before resolveAgentEnd), the counter must be reset.
// Use a regex across the success block so CRLF checkouts on Windows do not
// push the reset line outside a fixed substring window.
assert.ok(
- /consecutiveTransientErrors\s*=\s*0\s*;[\s\S]{0,250}successful unit completion/.test(indexSource),
- "consecutive transient error counter must be reset on successful unit completion (#1166)",
+ /consecutiveTransientErrors\s*=\s*0\s*;[\s\S]{0,250}resolveAgentEnd/.test(src),
+ "consecutive transient error counter must be reset before resolveAgentEnd on the success path (#1166)",
);
});
-test("index.ts applies escalating delay for repeated transient errors", () => {
- const indexSource = readFileSync(join(__dirname, "..", "index.ts"), "utf-8");
+test("agent-end-recovery.ts applies escalating delay for repeated transient errors", () => {
+ const src = readFileSync(join(__dirname, "..", "bootstrap", "agent-end-recovery.ts"), "utf-8");
- // Must contain the exponential backoff formula
+ // Must contain the exponential backoff formula (may span multiple lines)
assert.ok(
- /retryAfterMs\s*[=*].*2\s*\*\*/.test(indexSource),
- "index.ts must escalate retryAfterMs exponentially for consecutive transient errors (#1166)",
+ src.includes("2 ** Math.max(0, consecutiveTransientErrors"),
+ "agent-end-recovery.ts must escalate retryAfterMs exponentially for consecutive transient errors (#1166)",
);
});
diff --git a/src/resources/extensions/gsd/tests/skill-activation.test.ts b/src/resources/extensions/gsd/tests/skill-activation.test.ts
index 23df394ca..e2c6c7be0 100644
--- a/src/resources/extensions/gsd/tests/skill-activation.test.ts
+++ b/src/resources/extensions/gsd/tests/skill-activation.test.ts
@@ -60,17 +60,17 @@ test("buildSkillActivationBlock matches installed skills from task context", ()
}
});
-test("buildSkillActivationBlock includes always_use_skills from preferences", () => {
+test("buildSkillActivationBlock includes always_use_skills from preferences using exact Skill tool format", () => {
const base = makeTempBase();
try {
- writeSkill(base, "testing", "Use for test setup, assertions, and verification patterns.");
+ writeSkill(base, "swift-testing", "Use for Swift Testing assertions and verification patterns.");
loadOnlyTestSkills(base);
const result = buildBlock(base, { taskTitle: "Unrelated task title" }, {
- always_use_skills: ["testing"],
+ always_use_skills: ["swift-testing"],
});
- assert.match(result, /Call Skill\('testing'\)/);
+ assert.equal(result, "Call Skill('swift-testing').");
} finally {
cleanup(base);
}
diff --git a/src/resources/extensions/gsd/tests/visualizer-data.test.ts b/src/resources/extensions/gsd/tests/visualizer-data.test.ts
index 06ce5a89b..9f9548169 100644
--- a/src/resources/extensions/gsd/tests/visualizer-data.test.ts
+++ b/src/resources/extensions/gsd/tests/visualizer-data.test.ts
@@ -422,25 +422,25 @@ assertTrue(
"overlay has 10 tab labels",
);
-// Verify commands.ts integration
-const commandsPath = join(__dirname, "..", "commands.ts");
-const commandsSrc = readFileSync(commandsPath, "utf-8");
+// Verify commands/handlers/core.ts integration
+const coreHandlerPath = join(__dirname, "..", "commands", "handlers", "core.ts");
+const coreHandlerSrc = readFileSync(coreHandlerPath, "utf-8");
-console.log("\n=== commands.ts integration ===");
+console.log("\n=== commands/handlers/core.ts integration ===");
assertTrue(
- commandsSrc.includes('"visualize"'),
- "commands.ts has visualize in subcommands array",
+ coreHandlerSrc.includes('"visualize"'),
+ "core.ts has visualize in subcommands array",
);
assertTrue(
- commandsSrc.includes("GSDVisualizerOverlay"),
- "commands.ts imports GSDVisualizerOverlay",
+ coreHandlerSrc.includes("GSDVisualizerOverlay"),
+ "core.ts imports GSDVisualizerOverlay",
);
assertTrue(
- commandsSrc.includes("handleVisualize"),
- "commands.ts has handleVisualize handler",
+ coreHandlerSrc.includes("handleVisualize"),
+ "core.ts has handleVisualize handler",
);
report();
diff --git a/src/resources/extensions/gsd/visualizer-data.ts b/src/resources/extensions/gsd/visualizer-data.ts
index b06fe92d2..b196b7efa 100644
--- a/src/resources/extensions/gsd/visualizer-data.ts
+++ b/src/resources/extensions/gsd/visualizer-data.ts
@@ -1,10 +1,11 @@
// Data loader for workflow visualizer overlay — aggregates state + metrics.
import { existsSync, readFileSync, statSync } from 'node:fs';
+import { join } from 'node:path';
import { deriveState } from './state.js';
import { parseRoadmap, parsePlan, parseSummary, loadFile } from './files.js';
import { findMilestoneIds } from './milestone-ids.js';
-import { resolveMilestoneFile, resolveSliceFile, resolveGsdRootFile } from './paths.js';
+import { resolveMilestoneFile, resolveSliceFile, resolveGsdRootFile, gsdRoot } from './paths.js';
import {
getLedger,
getProjectTotals,
@@ -21,6 +22,8 @@ import { loadEffectiveGSDPreferences } from './preferences.js';
import { runProviderChecks, type ProviderCheckResult } from './doctor-providers.js';
import { generateSkillHealthReport } from './skill-health.js';
import { runEnvironmentChecks, type EnvironmentCheckResult } from './doctor-environment.js';
+import { computeProgressScore } from './progress-score.js';
+import { getHealthHistory } from './doctor-proactive.js';
import type { Phase } from './types.js';
import type { CaptureEntry } from './captures.js';
@@ -161,6 +164,27 @@ export interface SkillSummaryInfo {
topIssue: string | null;
}
+/** A single doctor history entry for visualizer display. */
+export interface VisualizerDoctorEntry {
+ ts: string;
+ ok: boolean;
+ errors: number;
+ warnings: number;
+ fixes: number;
+ codes: string[];
+ issues?: Array<{ severity: string; code: string; message: string; unitId: string }>;
+ fixDescriptions?: string[];
+ scope?: string;
+ summary?: string;
+}
+
+/** Current progress score snapshot for health display. */
+export interface VisualizerProgressScore {
+ level: "green" | "yellow" | "red";
+ summary: string;
+ signals: Array<{ kind: "positive" | "negative" | "neutral"; label: string }>;
+}
+
export interface HealthInfo {
budgetCeiling: number | undefined;
tokenProfile: string;
@@ -174,6 +198,10 @@ export interface HealthInfo {
providers: ProviderStatusSummary[];
skillSummary: SkillSummaryInfo;
environmentIssues: import("./doctor-environment.js").EnvironmentCheckResult[];
+ /** Persisted doctor run history (most recent first, up to 20 entries). */
+ doctorHistory?: VisualizerDoctorEntry[];
+ /** Current in-memory progress score (null if auto-mode not active). */
+ progressScore?: VisualizerProgressScore | null;
}
export interface VisualizerData {
@@ -608,6 +636,26 @@ function loadHealth(units: UnitMetrics[], totals: ProjectTotals | null, basePath
environmentIssues = runEnvironmentChecks(basePath).filter(r => r.status !== "ok");
} catch { /* non-fatal */ }
+ // Doctor run history — persisted across sessions (sync read to keep loadHealth sync)
+ let doctorHistory: VisualizerDoctorEntry[] = [];
+ try {
+ const historyPath = join(gsdRoot(basePath), "doctor-history.jsonl");
+ if (existsSync(historyPath)) {
+ const lines = readFileSync(historyPath, "utf-8").split("\n").filter(l => l.trim());
+ doctorHistory = lines.slice(-20).reverse().map(l => JSON.parse(l) as VisualizerDoctorEntry);
+ }
+ } catch { /* non-fatal */ }
+
+ // Current progress score — only meaningful when auto-mode has health data
+ let progressScore: VisualizerProgressScore | null = null;
+ try {
+ const history = getHealthHistory();
+ if (history.length > 0) {
+ const score = computeProgressScore();
+ progressScore = { level: score.level, summary: score.summary, signals: score.signals };
+ }
+ } catch { /* non-fatal */ }
+
return {
budgetCeiling,
tokenProfile,
@@ -621,6 +669,8 @@ function loadHealth(units: UnitMetrics[], totals: ProjectTotals | null, basePath
providers,
skillSummary,
environmentIssues,
+ doctorHistory,
+ progressScore,
};
}
diff --git a/src/resources/extensions/gsd/visualizer-views.ts b/src/resources/extensions/gsd/visualizer-views.ts
index 30e459390..44de80d41 100644
--- a/src/resources/extensions/gsd/visualizer-views.ts
+++ b/src/resources/extensions/gsd/visualizer-views.ts
@@ -1150,6 +1150,64 @@ export function renderHealthView(
}
}
+ // Progress score section — current traffic light status
+ if (health.progressScore) {
+ lines.push("");
+ lines.push(th.fg("accent", th.bold("Progress Score")));
+ lines.push("");
+ const ps = health.progressScore;
+ const scoreColor = ps.level === "green" ? "success" : ps.level === "yellow" ? "warning" : "error";
+ const scoreIcon = ps.level === "green" ? "●" : ps.level === "yellow" ? "◐" : "○";
+ lines.push(` ${th.fg(scoreColor, scoreIcon)} ${th.fg(scoreColor, ps.summary)}`);
+ for (const signal of ps.signals) {
+ const prefix = signal.kind === "positive" ? th.fg("success", " ✓")
+ : signal.kind === "negative" ? th.fg("error", " ✗")
+ : th.fg("dim", " ·");
+ lines.push(` ${prefix} ${th.fg("dim", signal.label)}`);
+ }
+ }
+
+ // Doctor history section — persisted across sessions
+ const doctorHistory = health.doctorHistory ?? [];
+ if (doctorHistory.length > 0) {
+ lines.push("");
+ lines.push(th.fg("accent", th.bold("Doctor History")));
+ lines.push("");
+
+ for (const entry of doctorHistory.slice(0, 10)) {
+ const icon = entry.ok ? th.fg("success", "✓") : th.fg("error", "✗");
+ const ts = entry.ts.replace("T", " ").slice(0, 19);
+ const scopeTag = entry.scope ? th.fg("accent", ` [${entry.scope}]`) : "";
+ // Prefer human-readable summary, fall back to counts
+ const detail = entry.summary
+ ? th.fg("text", entry.summary)
+ : th.fg("text", `${entry.errors} errors, ${entry.warnings} warnings, ${entry.fixes} fixes`);
+ lines.push(` ${icon} ${th.fg("dim", ts)}${scopeTag} ${detail}`);
+
+ // Show issue details if available
+ if (entry.issues && entry.issues.length > 0) {
+ for (const issue of entry.issues.slice(0, 3)) {
+ const issuePfx = issue.severity === "error" ? th.fg("error", " ✗") : th.fg("warning", " ⚠");
+ lines.push(` ${issuePfx} ${th.fg("dim", truncateToWidth(issue.message, width - 12))}`);
+ }
+ if (entry.issues.length > 3) {
+ lines.push(` ${th.fg("dim", `+${entry.issues.length - 3} more`)}`);
+ }
+ }
+
+ // Show fixes if available
+ if (entry.fixDescriptions && entry.fixDescriptions.length > 0) {
+ for (const fix of entry.fixDescriptions.slice(0, 2)) {
+ lines.push(` ${th.fg("success", "↳")} ${th.fg("dim", truncateToWidth(fix, width - 12))}`);
+ }
+ }
+ }
+
+ if (doctorHistory.length > 10) {
+ lines.push(` ${th.fg("dim", `...${doctorHistory.length - 10} older entries`)}`);
+ }
+ }
+
// Skills section
if (health.skillSummary?.total > 0) {
lines.push("");
diff --git a/src/welcome-screen.ts b/src/welcome-screen.ts
index 4d4b13772..7b8d37773 100644
--- a/src/welcome-screen.ts
+++ b/src/welcome-screen.ts
@@ -1,8 +1,9 @@
/**
* GSD Welcome Screen
*
- * Rendered to stderr before the TUI takes over.
- * No box, no panels — logo with metadata alongside, dim hint below.
+ * Two-panel bar layout: full-width accent bars at top/bottom (matching the
+ * auto-mode progress widget style), logo left (fixed width), info right.
+ * Falls back to simple text on narrow terminals (<70 cols) or non-TTY.
*/
import os from 'node:os'
@@ -21,44 +22,95 @@ function getShortCwd(): string {
return cwd.startsWith(home) ? '~' + cwd.slice(home.length) : cwd
}
+/** Visible length — strips ANSI escape codes before measuring. */
+function visLen(s: string): number {
+ return s.replace(/\x1b\[[0-9;]*m/g, '').length
+}
+
+/** Right-pad a string to the given visible width. */
+function rpad(s: string, w: number): string {
+ return s + ' '.repeat(Math.max(0, w - visLen(s)))
+}
+
export function printWelcomeScreen(opts: WelcomeScreenOptions): void {
if (!process.stderr.isTTY) return
const { version, modelName, provider } = opts
const shortCwd = getShortCwd()
+ const termWidth = Math.min((process.stderr.columns || 80) - 1, 200)
- // Info lines to sit alongside the logo (one per logo row)
- const modelLine = [modelName, provider].filter(Boolean).join(' · ')
- const INFO: (string | undefined)[] = [
- ` ${chalk.bold('Get Shit Done')} ${chalk.dim('v' + version)}`,
- undefined,
- modelLine ? ` ${chalk.dim(modelLine)}` : undefined,
- ` ${chalk.dim(shortCwd)}`,
- undefined,
- undefined,
- ]
-
- const lines: string[] = ['']
- for (let i = 0; i < GSD_LOGO.length; i++) {
- lines.push(chalk.cyan(GSD_LOGO[i]) + (INFO[i] ?? ''))
+ // Narrow terminal fallback
+ if (termWidth < 70) {
+ process.stderr.write(`\n Get Shit Done v${version}\n ${shortCwd}\n\n`)
+ return
}
- // Tool status + hint — dim, aligned under the info text
- const pad = ' '.repeat(28) + ' ' // aligns with the info text column
+ // ── Panel widths ────────────────────────────────────────────────────────────
+ // Layout: 1 leading space + LEFT_INNER logo content + 1 inner divider + RIGHT_INNER info
+ // Total: 1 + LEFT_INNER + 1 + RIGHT_INNER = termWidth
+ const LEFT_INNER = 34
+ const RIGHT_INNER = termWidth - LEFT_INNER - 2 // 2 = leading space + inner divider
+
+ // ── Bar/divider chars (matching GLYPH.separator + widget ui.bar() style) ────
+ const H = '─', DV = '│', DS = '├'
+
+ // ── Left rows: blank + 6 logo lines + blank (8 total) ───────────────────────
+ const leftRows = ['', ...GSD_LOGO, '']
+
+ // ── Right rows (8 total, null = divider) ────────────────────────────────────
+ const titleLeft = ` ${chalk.bold('Get Shit Done')}`
+ const titleRight = chalk.dim(`v${version}`)
+ const titleFill = RIGHT_INNER - visLen(titleLeft) - visLen(titleRight)
+ const titleRow = titleLeft + ' '.repeat(Math.max(1, titleFill)) + titleRight
const toolParts: string[] = []
- if (process.env.BRAVE_API_KEY) toolParts.push('Brave ✓')
- if (process.env.BRAVE_ANSWERS_KEY) toolParts.push('Answers ✓')
- if (process.env.JINA_API_KEY) toolParts.push('Jina ✓')
- if (process.env.TAVILY_API_KEY) toolParts.push('Tavily ✓')
- if (process.env.CONTEXT7_API_KEY) toolParts.push('Context7 ✓')
+ if (process.env.BRAVE_API_KEY) toolParts.push('Brave ✓')
+ if (process.env.BRAVE_ANSWERS_KEY) toolParts.push('Answers ✓')
+ if (process.env.JINA_API_KEY) toolParts.push('Jina ✓')
+ if (process.env.TAVILY_API_KEY) toolParts.push('Tavily ✓')
+ if (process.env.CONTEXT7_API_KEY) toolParts.push('Context7 ✓')
- if (toolParts.length > 0) {
- lines.push(chalk.dim(pad + ['Web search loaded', ...toolParts].join(' · ')))
+ // Tools left, hint right-aligned on the same row
+ const toolsLeft = toolParts.length > 0 ? chalk.dim(' ' + toolParts.join(' · ')) : ''
+ const hintRight = chalk.dim('/gsd to begin · /gsd help')
+ const footerFill = RIGHT_INNER - visLen(toolsLeft) - visLen(hintRight)
+ const footerRow = toolsLeft + ' '.repeat(Math.max(1, footerFill)) + hintRight
+
+ const DIVIDER = null
+ const rightRows: (string | null)[] = [
+ titleRow,
+ DIVIDER,
+ modelName ? ` Model ${chalk.dim(modelName)}` : '',
+ provider ? ` Provider ${chalk.dim(provider)}` : '',
+ ` Directory ${chalk.dim(shortCwd)}`,
+ DIVIDER,
+ footerRow,
+ '',
+ ]
+
+ // ── Render ──────────────────────────────────────────────────────────────────
+ const out: string[] = ['']
+
+ // Top bar — full-width accent separator, matches auto-mode widget ui.bar()
+ out.push(chalk.cyan(H.repeat(termWidth)))
+
+ for (let i = 0; i < 8; i++) {
+ const row = leftRows[i] ?? ''
+ const lContent = rpad(row ? chalk.cyan(row) : '', LEFT_INNER)
+ const rRow = rightRows[i]
+
+ if (rRow === null) {
+ // Section divider: left logo area + dim ├────... extending right
+ out.push(' ' + lContent + chalk.dim(DS + H.repeat(RIGHT_INNER)))
+ } else {
+ // Content row: 1 space + logo │ info (no outer vertical borders)
+ out.push(' ' + lContent + chalk.dim(DV) + rpad(rRow, RIGHT_INNER))
+ }
}
- lines.push(chalk.dim(pad + '/gsd to begin · /gsd help for all commands'))
- lines.push('')
+ // Bottom bar — full-width accent separator
+ out.push(chalk.cyan(H.repeat(termWidth)))
+ out.push('')
- process.stderr.write(lines.join('\n') + '\n')
+ process.stderr.write(out.join('\n') + '\n')
}