Files
msd-core/sdk/src/query/profile-sample.ts
Rezolv c5b1445529 feat(sdk): golden parity harness and query handler CJS alignment (#2302 Track A) (#2341)
* feat(sdk): golden parity harness and query handler CJS alignment (#2302 Track A)

Golden/read-only parity tests and registry alignment, query handler fixes
(check-completion, state-mutation, commit, validate, summary, etc.), and
WAITING.json dual-write for .gsd/.planning readers.

Refs gsd-build/get-shit-done#2341

* fix(sdk): getMilestoneInfo matches GSD ROADMAP (🟡, last bold, STATE fallback)

- Recognize in-flight 🟡 milestone bullets like 🚧.
- Derive from last **vX.Y Title** before ## Phases when emoji absent.
- Fall back to STATE.md milestone when ROADMAP is missing; use last bare vX.Y
  in cleaned text instead of first (avoids v1.0 from shipped list).
- Fixes init.execute-phase milestone_version and buildStateFrontmatter after
  state.begin-phase (syncStateFrontmatter).

* feat(sdk): phase list, plan task structure, requirements extract handlers

- Register phase.list-plans, phase.list-artifacts, plan.task-structure,
  requirements.extract-from-plans (SDK-only; golden-policy exceptions).
- Add unit tests; document in QUERY-HANDLERS.md.
- writeProfile: honor --output, render dimensions, return profile_path and dimensions_scored.

* feat(sdk): centralize getGsdAgentsDir in query helpers

Extract agent directory resolution to helpers (GSD_AGENTS_DIR, primary
~/.claude/agents, legacy path). Use from init and docs-init init bundles.

docs(15): add 15-CONTEXT for autonomous phase-15 run.

* feat(sdk): query CLI CJS fallback and session correlation

- createRegistry(eventStream, sessionId) threads correlation into mutation events
- gsd-sdk query falls back to gsd-tools.cjs when no native handler matches
  (disable with GSD_QUERY_FALLBACK=off); stderr bridge warnings
- Export createRegistry from @gsd-build/sdk; add sdk/README.md
- Update QUERY-HANDLERS.md and registry module docs for fallback + sessionId
- Agents: prefer node dist/cli.js query over cat/grep for STATE and plans

* fix(sdk): init phase_found parity, docs-init agents path, state field extract

- Normalize findPhase not-found to null before roadmap fallback (matches findPhaseInternal)

- docs-init: use detectRuntime + resolveAgentsDir for checkAgentsInstalled

- state.cjs stateExtractField: horizontal whitespace only after colon (YAML progress guard)

- Tests: commit_docs default true; config-get golden uses temp config; golden integration green

Refs: #2302

* refactor(sdk): share SessionJsonlRecord in profile-extract-messages

CodeRabbit nit: dedupe JSONL record shape for isGenuineUserMessage and streamExtractMessages.

* fix(sdk): address CodeRabbit major threads (paths, gates, audit, verify)

- Resolve @file: and CLI JSON indirection relative to projectDir; guard empty normalized query command

- plan.task-structure + intel extract/patch-meta: resolvePathUnderProject containment

- check.config-gates: safe string booleans; plan_checker alias precedence over plan_check default

- state.validate/sync: phaseTokenMatches + comparePhaseNum ordering

- verify.schema-drift: token match phase dirs; files_modified from parsed frontmatter

- audit-open: has_scan_errors, unreadable rows, human report when scans fail

- requirements PLANNED key PLAN for root PLAN.md; gsd-tools timeout note

- ingest-docs: repo-root path containment; classifier output slug-hash

Golden parity test strips has_scan_errors until CJS adds field.

* fix: Resolve CodeRabbit security and quality findings
- Secure intel.ts and cli.ts against path traversal
- Catch and validate git add status in commit.ts
- Expand roadmap milestone marker extraction
- Fix parsing array-of-objects in frontmatter YAML
- Fix unhandled config evaluations
- Improve coverage test parity mapping

* test: raise planner character extraction limit to 48K

* fix(sdk): resolve TS build error in docs-init passing config
2026-04-20 18:09:02 -04:00

185 lines
6.0 KiB
TypeScript

/**
* `profile-sample` — parity with `get-shit-done/bin/lib/profile-pipeline.cjs` `cmdProfileSample`.
*/
import { appendFileSync, mkdtempSync, readdirSync, statSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { GSDError, ErrorClassification } from '../errors.js';
import { getScanSessionsRoot, scanProjectDir, readSessionIndex, getProjectName } from './profile-scan-sessions.js';
import { isGenuineUserMessage, streamExtractMessages, truncateContent } from './profile-extract-messages.js';
export type ProfileSampleResult = {
output_file: string;
projects_sampled: number;
messages_sampled: number;
per_project_cap: number;
message_char_limit: number;
skipped_context_dumps: number;
project_breakdown: Array<{ project: string; messages: number; sessions: number }>;
};
/**
* Port of `cmdProfileSample` — same JSON + JSONL file shape as `gsd-tools profile-sample`.
*/
export async function runProfileSample(
overridePath: string | null,
options: { limit: number; maxPerProject: number | null; maxChars: number },
): Promise<ProfileSampleResult> {
const sessionsDir = getScanSessionsRoot(overridePath);
if (!sessionsDir) {
const searchedPath = overridePath || '~/.claude/projects';
throw new GSDError(
`No Claude Code sessions found at ${searchedPath}.${overridePath ? '' : ' Is Claude Code installed?'}`,
ErrorClassification.Validation,
);
}
const limit = options.limit || 150;
const maxChars = options.maxChars || 500;
const maxPerProject = options.maxPerProject;
let projectDirs: string[];
try {
projectDirs = readdirSync(sessionsDir).filter((entry) => {
const fullPath = join(sessionsDir, entry);
try {
return statSync(fullPath).isDirectory();
} catch {
return false;
}
});
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
throw new GSDError(`Cannot read sessions directory: ${msg}`, ErrorClassification.Validation);
}
if (projectDirs.length === 0) {
throw new GSDError('No project directories found in sessions directory.', ErrorClassification.Validation);
}
const projectMeta: Array<{
dirName: string;
projectPath: string;
sessions: ReturnType<typeof scanProjectDir>;
projectName: string;
lastActive: Date;
}> = [];
for (const dirName of projectDirs) {
const projectPath = join(sessionsDir, dirName);
const sessions = scanProjectDir(projectPath);
if (sessions.length === 0) continue;
const indexData = readSessionIndex(projectPath);
const projectName = getProjectName(dirName, indexData);
const lastActive = sessions[0]!.modified;
projectMeta.push({ dirName, projectPath, sessions, projectName, lastActive });
}
projectMeta.sort((a, b) => b.lastActive.getTime() - a.lastActive.getTime());
const projectCount = projectMeta.length;
if (projectCount === 0) {
throw new GSDError('No projects with sessions found.', ErrorClassification.Validation);
}
const perProjectCap = maxPerProject || Math.max(5, Math.floor(limit / projectCount));
const recencyThreshold = Date.now() - 30 * 24 * 60 * 60 * 1000;
const allMessages: Array<{
sessionId: string;
projectName: string;
projectPath: string | null;
timestamp: string | number | null;
content: string;
}> = [];
let skippedContextDumps = 0;
const projectBreakdown: Array<{ project: string; messages: number; sessions: number }> = [];
for (const proj of projectMeta) {
if (allMessages.length >= limit) break;
const cappedSessions = proj.sessions.slice(0, perProjectCap);
let projectMessages = 0;
let projectSessionsUsed = 0;
for (const session of cappedSessions) {
if (allMessages.length >= limit) break;
const isRecent = session.modified.getTime() >= recencyThreshold;
const perSessionMax = isRecent ? 10 : 3;
const remaining = Math.min(perSessionMax, limit - allMessages.length);
try {
const msgs = await streamExtractMessages(session.filePath, isGenuineUserMessage, remaining);
let sessionUsed = false;
for (const msg of msgs) {
if (allMessages.length >= limit) break;
const content = msg.content || '';
if (content.startsWith('This session is being continued')) {
skippedContextDumps++;
continue;
}
const lines = content.split('\n').filter((l) => l.trim().length > 0);
if (lines.length > 3) {
const logPattern = /^\[?(DEBUG|INFO|WARN|ERROR|LOG)\]?/i;
const timestampPattern = /^\d{4}-\d{2}-\d{2}/;
const logLines = lines.filter(
(l) => logPattern.test(l.trim()) || timestampPattern.test(l.trim()),
);
if (logLines.length / lines.length > 0.8) {
skippedContextDumps++;
continue;
}
}
const truncated = truncateContent(content, maxChars);
allMessages.push({
sessionId: msg.sessionId,
projectName: proj.projectName,
projectPath: msg.projectPath,
timestamp: msg.timestamp,
content: truncated,
});
projectMessages++;
sessionUsed = true;
}
if (sessionUsed) projectSessionsUsed++;
} catch {
continue;
}
}
if (projectMessages > 0) {
projectBreakdown.push({
project: proj.projectName,
messages: projectMessages,
sessions: projectSessionsUsed,
});
}
}
const tmpDir = mkdtempSync(join(tmpdir(), 'gsd-profile-'));
const outputPath = join(tmpDir, 'profile-sample.jsonl');
for (const msg of allMessages) {
appendFileSync(outputPath, JSON.stringify(msg) + '\n');
}
return {
output_file: outputPath,
projects_sampled: projectBreakdown.length,
messages_sampled: allMessages.length,
per_project_cap: perProjectCap,
message_char_limit: maxChars,
skipped_context_dumps: skippedContextDumps,
project_breakdown: projectBreakdown,
};
}