Files
msd-core/hooks/lib/cursor-workspace.js
Jakub Zych a9a7a328e6 refactor: hard-fork GSD -> MSD (Make Software Done)
Mechanical rename produced by scripts/msd-rename.cjs: gsd/Gsd/GSD -> msd/Msd/MSD
across contents and paths, upstream package/repo coordinates -> @golem15/msd-core
and golem15com/msd-core. Deep links into upstream history, sibling upstream
packages, the GSD-2 import feature, CHANGELOG.md and .changeset/ are kept as-is.

Hand edits on top: MSD block-letter banner and logos, LICENSE copyright line,
package/plugin identity, regenerated lockfile, install-tree fixtures, derived
registries and benchmark baseline; migration checksum baseline re-locked
(MSD keeps its own install state, so no install had applied the old sums);
sort-order and regex-escaped expectations in tests adjusted.
2026-10-06 01:47:40 +02:00

75 lines
3.4 KiB
JavaScript

// cursor-workspace.js — shared workspace resolution for Cursor lifecycle hooks (#2587).
//
// No `msd-hook-version:` marker, deliberately — matching hooks/lib/git-cmd.js.
// Neither copy path that stages hooks/lib/*.js substitutes the version
// placeholder (copyLibDir stamps .sh files only), and the managed-hooks
// staleness scan covers top-level hooks/ names, not hooks/lib/. A marker here
// would ship to users as an unsubstituted literal.
//
// Cursor invokes hooks with cwd set to the Cursor config dir (~/.cursor) under
// the cursor-agent CLI, NOT the workspace. Every hook that resolves a project
// path from process.cwd() therefore missed: sessionStart could only emit its
// "no .planning/ workflow found" nudge, stop's verify-work reminder never fired,
// and subagentStart left every subagent without phase context.
//
// The hook payload carries the real path in `workspace_roots`. This module is
// the single place that turns that payload into a project root, so the three
// hooks cannot drift apart (they previously carried three copies of it).
//
// Consumers: msd-cursor-session-start.js, msd-cursor-stop.js,
// msd-cursor-subagent-start.js. Staged into <config>/hooks/lib/ by
// writeCursorHooksJson (src/runtime-hooks-surface.cts) alongside the scripts
// that require it, and registered in the installer's MSD_HOOK_LIB_FILES so
// uninstall and the manifest manage it.
'use strict';
const fs = require('fs');
const path = require('path');
/**
* Resolve the workspace root a Cursor hook should report on.
*
* Search order:
* 1. Each entry of `workspace_roots` that actually carries .planning/STATE.md
* — so a multi-root workspace whose MSD project is not the first root
* still resolves.
* 2. process.cwd(), if IT carries .planning/STATE.md. cwd is a CANDIDATE, not
* merely the empty-roots fallback: an IDE invocation can supply
* workspace_roots AND run from the project, and searching roots alone
* would report "absent" for a project sitting right at cwd — narrower than
* the pre-fix always-cwd behavior this replaces.
* 3. The first declared root, else cwd — so the "absent" message still names
* a sensible directory when there is genuinely no project.
*
* Never throws: a malformed payload degrades to cwd rather than wedging the
* session. (fs.existsSync itself does not throw — it returns false for NUL
* bytes, over-long components, and non-directory ancestors.)
*
* @param {string} rawInput Raw stdin payload as received by the hook.
* @returns {string} Absolute-or-relative directory to resolve .planning/ against.
*/
function resolveWorkspaceRoot(rawInput) {
let input = {};
try { input = JSON.parse(rawInput || '{}'); } catch { /* fall back to cwd */ }
const roots = Array.isArray(input.workspace_roots)
? input.workspace_roots.filter((r) => typeof r === 'string' && r.length > 0)
: [];
for (const root of [...roots, process.cwd()]) {
if (fs.existsSync(path.join(root, '.planning', 'STATE.md'))) return root;
}
return roots[0] || process.cwd();
}
/**
* Convenience: the .planning/STATE.md path for the resolved workspace.
*
* @param {string} rawInput Raw stdin payload as received by the hook.
* @returns {string}
*/
function resolveStatePath(rawInput) {
return path.join(resolveWorkspaceRoot(rawInput), '.planning', 'STATE.md');
}
module.exports = { resolveWorkspaceRoot, resolveStatePath };