Files
msd-core/get-shit-done/bin/lib/runtime-slash.cjs
Tom Boucher 034b47c8d0 fix(3584): runtime-aware slash formatter for user-facing emissions
Introduce `runtime-slash.cjs` as the single source of truth for emitting
GSD slash-command references in user-facing runtime output and persisted
artifacts. `formatGsdSlash(commandName, runtime)` produces `/gsd-<cmd>`
for skills-based runtimes (Claude/Cursor/OpenCode/Kilo/etc.) and
`$gsd-<cmd>` for Codex. The deprecated `/gsd:<cmd>` colon form is never
emitted — pasting a recommended-action command into Claude Code now
routes correctly instead of failing with `Unknown command`.

Wired into the high-impact emitters identified in #3584:

- `init.cjs` `cmdInitManager` recommended_actions[].command (the
  original failure path in the bug report) plus the no-ROADMAP /
  no-STATE error hints.
- `phase.cjs` `cmdPhaseAdd`, `cmdPhaseAddBatch`, `cmdPhaseInsert` —
  ROADMAP.md `Plans:` references now persist the routable form
  instead of the legacy colon form.
- `verify.cjs` `cmdValidateHealth` — every fix-hint addIssue() call
  (E001/E002/E003/E004/E005, W002/W003/W008/W009/W011/W016/W018) and
  the persisted STATE.md regenerate / MILESTONES.md backfill notes.
- `milestone.cjs` `cmdMilestoneComplete` — Operator Next Steps tail
  rewrite.
- `validate-command-router.cjs` — `validate context` recommendation
  strings for WARNING/CRITICAL utilization bands.
- `workstream.cjs` — missing .planning hint.
- `profile-output.cjs` — `generate-claude-md` workflow enforcement
  block, project/skills fallbacks, profile placeholder, and the
  dev-preferences refresh hints.
- `drift.cjs`, `gsd2-import.cjs`, `commands.cjs scaffold context` —
  remaining one-off persisted references.

Runtime detection: `resolveRuntime(projectDir)` reads
`process.env.GSD_RUNTIME` first, then a side-effect-free direct read of
`.planning/config.json` (NOT `loadConfig`, which would normalize legacy
keys and re-write the file just to read the runtime name).

Tests:
- `tests/bug-3584-runtime-slash-formatter.test.cjs` — 22 unit tests
  covering the pure formatter and resolver (hyphen vs codex, prefix
  normalization, defensive returns, env/config/default chain).
- `tests/bug-3584-runtime-slash-emitters.test.cjs` — 6 integration
  tests exercising `init manager`, `phase add` (via the structured
  `roadmap get-phase` payload to avoid raw-text matching on the
  on-disk artifact), `validate health`, `validate context`, and the
  codex variant.
- Existing tests updated to assert the new contract: validate-context
  recommendations, claude-md workflow block, milestone complete
  Operator Next Steps. Copilot-install engine-conversion test now
  asserts against a synthetic input since bin/lib/*.cjs no longer
  contains literal `/gsd:` references for the install-time converter
  to rewrite.

INVENTORY.md and INVENTORY-MANIFEST.json updated for the new module
(64 CLI modules shipped, +1).

Fixes #3584

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 19:32:02 -04:00

97 lines
3.6 KiB
JavaScript

'use strict';
/**
* runtime-slash.cjs — single source of truth for emitting GSD slash-command
* references in user-facing runtime output (recommended-actions JSON, persisted
* ROADMAP.md entries, verify/validate fix hints, error messages, etc.).
*
* Background: #2808 unified all GSD skill installs to register under the hyphen
* form (`name: gsd-<cmd>`). The legacy colon form `/gsd:<cmd>` is no longer
* routable by Claude Code skill installs, but ~50 runtime emissions in
* bin/lib/*.cjs still hardcoded it (#3584). Codex installs need the shell-var
* `$gsd-<cmd>` form. This module is the only place the runtime should decide
* which shape to emit.
*
* - codex: $gsd-<cmd> (shell-var syntax)
* - claude, cursor, opencode, kilo, etc.: /gsd-<cmd>
*
* The colon form is never emitted.
*/
function formatGsdSlash(commandName, runtime) {
if (typeof commandName !== 'string') return commandName;
if (commandName === '') return commandName;
// Strip any existing leading prefix so the helper is idempotent and accepts
// both legacy `/gsd:<name>` and canonical hyphen-form input (plus the bare
// `gsd:<name>` shorthand and codex `$gsd-<name>` shell-var input).
const stripped = commandName.replace(/^[/$]?gsd[-:]/i, '');
// If the regex matched nothing (no prefix), the input is already a bare name.
const bare = stripped === commandName ? commandName : stripped;
if (bare === '') return commandName;
const rt = String(runtime || 'claude').toLowerCase();
if (rt === 'codex') {
// Codex skills are invoked as $gsd-<cmd> (shell-var syntax). Lowercased
// because shell-var identifiers in the surrounding prose are conventionally
// lowercase; matches the convertCodexSlash() projection in bin/install.js.
return `$gsd-${bare.toLowerCase()}`;
}
return `/gsd-${bare}`;
}
/**
* Resolve the effective runtime for a project directory.
*
* process.env.GSD_RUNTIME > config.runtime > 'claude'
*
* Mirrors the precedence already used by profile-output.cjs and the rest of
* the runtime resolution chain. Returns a lowercased string so downstream
* comparisons can be case-blind.
*
* @param {string|null|undefined} projectDir
* @returns {string}
*/
function resolveRuntime(projectDir) {
if (process.env.GSD_RUNTIME) {
return String(process.env.GSD_RUNTIME).toLowerCase();
}
if (projectDir) {
try {
// Read config.json directly (not via loadConfig). loadConfig has a side
// effect of normalizing and re-writing legacy keys back to disk, which
// would mutate the project file just to read the runtime name. We only
// need the literal `runtime:` value, so a plain JSON read is sufficient
// and side-effect-free.
const fs = require('fs');
const path = require('path');
const configPath = path.join(projectDir, '.planning', 'config.json');
if (fs.existsSync(configPath)) {
const raw = fs.readFileSync(configPath, 'utf-8');
const parsed = JSON.parse(raw);
if (parsed && typeof parsed === 'object' && parsed.runtime) {
return String(parsed.runtime).toLowerCase();
}
}
} catch {
// Fall through to default — a missing/broken config must not crash
// runtime output formatting.
}
}
return 'claude';
}
/**
* Convenience: format using the runtime resolved from a project directory.
* Equivalent to `formatGsdSlash(name, resolveRuntime(projectDir))`.
*/
function formatGsdSlashFor(projectDir, commandName) {
return formatGsdSlash(commandName, resolveRuntime(projectDir));
}
module.exports = {
formatGsdSlash,
resolveRuntime,
formatGsdSlashFor,
};