fix(#2618): thread --ws through query dispatch and sync root STATE.md on workstream.set (#2627)

* fix(#2618): thread --ws through query dispatch for state and init handlers

Gap 1 of #2618: the query dispatcher already accepts a workstream via
registry.dispatch(cmd, args, projectDir, ws), but several handlers drop it
before reaching planningPaths() / getMilestoneInfo() / findPhase() — so
stateJson and the init.* handlers return root-scoped results even when --ws
is provided.

Changes:

- sdk/src/query/state.ts: forward workstream into getMilestoneInfo() and
  extractCurrentMilestone() so buildStateFrontmatter resolves milestone data
  from the workstream ROADMAP/STATE instead of the root mirror.
- sdk/src/query/init.ts: thread workstream through initExecutePhase,
  initPlanPhase, initPhaseOp, and getPhaseInfoWithFallback (which fans out
  to findPhase() and roadmapGetPhase()). Also switch hardcoded
  join(projectDir, '.planning') to relPlanningPath(workstream) so returned
  state_path/roadmap_path/config_path reflect the workstream layout.

Regression test: stateJson with --ws workstream reads STATE.md from
.planning/workstreams/<name>/ when workstream is provided.

Closes #2618 (gap 1)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(#2618): sync root .planning/STATE.md mirror on workstream.set

Gap 2 of #2618: setActiveWorkstream only flips the active-workstream
pointer file; the root .planning/STATE.md mirror stays stale. Downstream
consumers (statusline, gsd-sdk query progress, any tool that reads the
root STATE.md) continue to see the previous workstream's state.

After setActiveWorkstream(), copy .planning/workstreams/<name>/STATE.md
verbatim to .planning/STATE.md via writeFileSync. The workstream STATE.md
is authoritative; the root file is a pass-through mirror. Missing source
STATE.md is a no-op rather than an error — a freshly created workstream
with no STATE.md yet should still activate cleanly.

The response now includes `mirror_synced: boolean` so callers can
observe whether the root mirror was updated.

Regression test: workstreamSet root STATE.md mirror sync — switches
from a stale root mirror to a workstream STATE.md with different
frontmatter and asserts the root file now matches.

Closes #2618 (gap 2)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-04-23 11:54:34 -04:00
committed by GitHub
parent df0ab0c0c9
commit bdba40cc3d
5 changed files with 147 additions and 17 deletions

View File

@@ -28,6 +28,7 @@ import { resolveModel, MODEL_PROFILES } from './config-query.js';
import { findPhase } from './phase.js';
import { roadmapGetPhase, getMilestoneInfo } from './roadmap.js';
import { planningPaths, normalizePhaseName, toPosixPath, resolveAgentsDir, detectRuntime } from './helpers.js';
import { relPlanningPath } from '../workstream-utils.js';
import type { QueryHandler } from './utils.js';
// ─── Internal helpers ──────────────────────────────────────────────────────
@@ -116,15 +117,16 @@ function checkAgentsInstalled(config?: { runtime?: unknown }): { agents_installe
async function getPhaseInfoWithFallback(
phase: string,
projectDir: string,
workstream?: string,
): Promise<{ phaseInfo: Record<string, unknown> | null; roadmapPhase: Record<string, unknown> | null }> {
const phaseResult = await findPhase([phase], projectDir);
const phaseResult = await findPhase([phase], projectDir, workstream);
let phaseInfo = phaseResult.data as Record<string, unknown> | null;
// findPhase returns { found: false } when missing; findPhaseInternal returns null — align for init parity.
if (phaseInfo && phaseInfo.found === false) {
phaseInfo = null;
}
const roadmapResult = await roadmapGetPhase([phase], projectDir);
const roadmapResult = await roadmapGetPhase([phase], projectDir, workstream);
const roadmapPhase = roadmapResult.data as Record<string, unknown> | null;
// Match init.cjs: drop archived disk match when the phase is listed in the current ROADMAP
@@ -264,16 +266,16 @@ export function withProjectRoot(
* Init handler for execute-phase workflow.
* Port of cmdInitExecutePhase from init.cjs lines 50-171.
*/
export const initExecutePhase: QueryHandler = async (args, projectDir) => {
export const initExecutePhase: QueryHandler = async (args, projectDir, workstream) => {
const phase = args[0];
if (!phase) {
return { data: { error: 'phase required for init execute-phase' } };
}
const config = await loadConfig(projectDir);
const planningDir = join(projectDir, '.planning');
const planningDir = join(projectDir, relPlanningPath(workstream));
const { phaseInfo, roadmapPhase } = await getPhaseInfoWithFallback(phase, projectDir);
const { phaseInfo, roadmapPhase } = await getPhaseInfoWithFallback(phase, projectDir, workstream);
const phase_req_ids = extractReqIds(roadmapPhase);
const [executorModel, verifierModel] = await Promise.all([
@@ -281,7 +283,7 @@ export const initExecutePhase: QueryHandler = async (args, projectDir) => {
getModelAlias('gsd-verifier', projectDir),
]);
const milestone = await getMilestoneInfo(projectDir);
const milestone = await getMilestoneInfo(projectDir, workstream);
const phaseNumber = (phaseInfo?.phase_number as string) || null;
const phaseSlug = (phaseInfo?.phase_slug as string) || null;
@@ -343,16 +345,16 @@ export const initExecutePhase: QueryHandler = async (args, projectDir) => {
* Init handler for plan-phase workflow.
* Port of cmdInitPlanPhase from init.cjs lines 173-293.
*/
export const initPlanPhase: QueryHandler = async (args, projectDir) => {
export const initPlanPhase: QueryHandler = async (args, projectDir, workstream) => {
const phase = args[0];
if (!phase) {
return { data: { error: 'phase required for init plan-phase' } };
}
const config = await loadConfig(projectDir);
const planningDir = join(projectDir, '.planning');
const planningDir = join(projectDir, relPlanningPath(workstream));
const { phaseInfo, roadmapPhase } = await getPhaseInfoWithFallback(phase, projectDir);
const { phaseInfo, roadmapPhase } = await getPhaseInfoWithFallback(phase, projectDir, workstream);
const phase_req_ids = extractReqIds(roadmapPhase);
const [researcherModel, plannerModel, checkerModel] = await Promise.all([
@@ -603,20 +605,20 @@ export const initVerifyWork: QueryHandler = async (args, projectDir) => {
* Init handler for discuss-phase and similar phase operations.
* Port of cmdInitPhaseOp from init.cjs lines 588-697.
*/
export const initPhaseOp: QueryHandler = async (args, projectDir) => {
export const initPhaseOp: QueryHandler = async (args, projectDir, workstream) => {
const phase = args[0];
if (!phase) {
return { data: { error: 'phase required for init phase-op' } };
}
const config = await loadConfig(projectDir);
const planningDir = join(projectDir, '.planning');
const planningDir = join(projectDir, relPlanningPath(workstream));
// findPhase with archived override: if only match is archived, prefer ROADMAP
const phaseResult = await findPhase([phase], projectDir);
const phaseResult = await findPhase([phase], projectDir, workstream);
let phaseInfo = phaseResult.data as Record<string, unknown> | null;
const roadmapResult = await roadmapGetPhase([phase], projectDir);
const roadmapResult = await roadmapGetPhase([phase], projectDir, workstream);
const roadmapPhase = roadmapResult.data as Record<string, unknown> | null;
// If the only match comes from an archived milestone, prefer current ROADMAP