sdtk-spec-kit 0.4.5 → 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdtk-spec-kit",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "description": "SDTK-SPEC CLI toolkit for deterministic software documentation workflows via the sdtk-spec command",
5
5
  "bin": {
6
6
  "sdtk-spec": "bin/sdtk.js"
@@ -252,7 +252,7 @@ function cmdHelp() {
252
252
  " sdtk-spec auth --verify",
253
253
  ' sdtk-spec generate --feature-key USER_PROFILE --feature-name "User Profile"',
254
254
  " sdtk-spec generate --feature-key ORDER_MGMT --feature-name \"Order Management\" --validate-only",
255
- " sdtk-spec analyze --source ./requirements.md --feature-key ESTEAM_EC --feature-name \"Esteam E-Commerce\"",
255
+ " sdtk-spec analyze --source ./requirements.md --feature-key ORDER_PORTAL --feature-name \"Order Portal\"",
256
256
  " sdtk-spec init --runtime claude --runtime-scope user",
257
257
  " sdtk-spec runtime install --runtime claude --scope project",
258
258
  " sdtk-spec runtime install --runtime codex --scope user",
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+
6
+ const PROVENANCE_VIOLATION = "PROVENANCE_VIOLATION";
7
+ const MIN_SOURCE_QUOTE_CODE_POINTS = 30;
8
+
9
+ function stripUtf8Bom(text) {
10
+ return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
11
+ }
12
+
13
+ function normalizeNewlines(text) {
14
+ return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
15
+ }
16
+
17
+ function readSourceText(sourcePath) {
18
+ return normalizeNewlines(stripUtf8Bom(fs.readFileSync(sourcePath, "utf8")));
19
+ }
20
+
21
+ function codePointLength(value) {
22
+ return Array.from(value).length;
23
+ }
24
+
25
+ function normalizedSampleName(value) {
26
+ return String(value || "")
27
+ .replace(/\.[^.\\/]+$/u, "")
28
+ .normalize("NFD")
29
+ .replace(/[\u0300-\u036f]/g, "")
30
+ .toLowerCase()
31
+ .replace(/[^a-z0-9]+/g, "")
32
+ .trim();
33
+ }
34
+
35
+ function extractScreens(handoff) {
36
+ if (!handoff || typeof handoff !== "object") {
37
+ return [];
38
+ }
39
+ if (Array.isArray(handoff.screens)) {
40
+ return handoff.screens;
41
+ }
42
+ if (Array.isArray(handoff.screen_inventory)) {
43
+ return handoff.screen_inventory;
44
+ }
45
+ if (handoff.handoff && Array.isArray(handoff.handoff.screens)) {
46
+ return handoff.handoff.screens;
47
+ }
48
+ if (handoff.design && Array.isArray(handoff.design.screens)) {
49
+ return handoff.design.screens;
50
+ }
51
+ return [];
52
+ }
53
+
54
+ function addViolation(violations, screenRef, field, message) {
55
+ violations.push({
56
+ code: PROVENANCE_VIOLATION,
57
+ screen: screenRef,
58
+ field,
59
+ message,
60
+ });
61
+ }
62
+
63
+ function validateLineRange(sourceLines, quote, lineRange) {
64
+ if (!Array.isArray(lineRange) || lineRange.length !== 2) {
65
+ return "source_line_range must be [start_line, end_line].";
66
+ }
67
+
68
+ const [startLine, endLine] = lineRange;
69
+ if (!Number.isInteger(startLine) || !Number.isInteger(endLine)) {
70
+ return "source_line_range values must be integers.";
71
+ }
72
+ if (startLine < 1 || endLine < 1 || startLine > endLine || endLine > sourceLines.length) {
73
+ return `source_line_range must be within source bounds 1..${sourceLines.length}.`;
74
+ }
75
+
76
+ const selectedText = sourceLines.slice(startLine - 1, endLine).join("\n");
77
+ if (!selectedText.includes(quote)) {
78
+ return "source_quote must appear within the declared source_line_range.";
79
+ }
80
+ return null;
81
+ }
82
+
83
+ function validateDesignHandoffProvenance(options) {
84
+ const handoffPath = options && options.handoffPath;
85
+ const sourcePath = options && options.sourcePath;
86
+ const sampleNames = options && Array.isArray(options.sampleNames) ? options.sampleNames : [];
87
+
88
+ const violations = [];
89
+ if (!handoffPath || !fs.existsSync(handoffPath)) {
90
+ addViolation(violations, "(handoff)", "handoffPath", `Handoff JSON does not exist: ${handoffPath || ""}`);
91
+ return { ok: false, code: PROVENANCE_VIOLATION, violations };
92
+ }
93
+ if (!sourcePath || !fs.existsSync(sourcePath)) {
94
+ addViolation(violations, "(source)", "sourcePath", `Requirement source does not exist: ${sourcePath || ""}`);
95
+ return { ok: false, code: PROVENANCE_VIOLATION, violations };
96
+ }
97
+
98
+ let handoff;
99
+ try {
100
+ handoff = JSON.parse(fs.readFileSync(handoffPath, "utf8"));
101
+ } catch (error) {
102
+ addViolation(violations, "(handoff)", "json", `Handoff JSON is invalid: ${error.message}`);
103
+ return { ok: false, code: PROVENANCE_VIOLATION, violations };
104
+ }
105
+
106
+ const sourceText = readSourceText(sourcePath);
107
+ const sourceLines = sourceText.split("\n");
108
+ const screens = extractScreens(handoff);
109
+ const normalizedSamples = new Set(sampleNames.map(normalizedSampleName).filter(Boolean));
110
+
111
+ if (screens.length === 0) {
112
+ addViolation(violations, "(handoff)", "screens", "Handoff must include at least one screen.");
113
+ }
114
+
115
+ screens.forEach((screen, index) => {
116
+ const screenRef = screen && (screen.id || screen.title) ? screen.id || screen.title : `screens[${index}]`;
117
+ const id = screen && typeof screen.id === "string" ? screen.id.trim() : "";
118
+ const title = screen && typeof screen.title === "string" ? screen.title.trim() : "";
119
+ const quote = screen && typeof screen.source_quote === "string" ? normalizeNewlines(screen.source_quote) : "";
120
+ const lineRange = screen && screen.source_line_range;
121
+
122
+ if (!id) {
123
+ addViolation(violations, screenRef, "id", "Screen id is required.");
124
+ }
125
+ if (!title) {
126
+ addViolation(violations, screenRef, "title", "Screen title is required.");
127
+ }
128
+ if (!quote) {
129
+ addViolation(violations, screenRef, "source_quote", "source_quote is required.");
130
+ } else {
131
+ if (codePointLength(quote) < MIN_SOURCE_QUOTE_CODE_POINTS) {
132
+ addViolation(
133
+ violations,
134
+ screenRef,
135
+ "source_quote",
136
+ `source_quote must be at least ${MIN_SOURCE_QUOTE_CODE_POINTS} Unicode code points.`
137
+ );
138
+ }
139
+ if (!sourceText.includes(quote)) {
140
+ addViolation(violations, screenRef, "source_quote", "source_quote must exist literally in the requirement source.");
141
+ }
142
+ const lineRangeError = validateLineRange(sourceLines, quote, lineRange);
143
+ if (lineRangeError) {
144
+ addViolation(violations, screenRef, "source_line_range", lineRangeError);
145
+ }
146
+ }
147
+
148
+ for (const candidate of [id, title]) {
149
+ const normalized = normalizedSampleName(candidate);
150
+ if (normalized && normalizedSamples.has(normalized)) {
151
+ addViolation(
152
+ violations,
153
+ screenRef,
154
+ "title",
155
+ "Screen id/title matches an explicit sample filename/name supplied for post-generation evaluation."
156
+ );
157
+ }
158
+ }
159
+ });
160
+
161
+ return {
162
+ ok: violations.length === 0,
163
+ code: violations.length === 0 ? "OK" : PROVENANCE_VIOLATION,
164
+ handoffPath: path.resolve(handoffPath),
165
+ sourcePath: path.resolve(sourcePath),
166
+ screenCount: screens.length,
167
+ violations,
168
+ };
169
+ }
170
+
171
+ function assertValidDesignHandoffProvenance(options) {
172
+ const result = validateDesignHandoffProvenance(options);
173
+ if (!result.ok) {
174
+ const details = result.violations.map((item) => `${item.screen} ${item.field}: ${item.message}`).join("; ");
175
+ const error = new Error(`${PROVENANCE_VIOLATION}: ${details}`);
176
+ error.code = PROVENANCE_VIOLATION;
177
+ error.result = result;
178
+ throw error;
179
+ }
180
+ return result;
181
+ }
182
+
183
+ module.exports = {
184
+ PROVENANCE_VIOLATION,
185
+ MIN_SOURCE_QUOTE_CODE_POINTS,
186
+ validateDesignHandoffProvenance,
187
+ assertValidDesignHandoffProvenance,
188
+ normalizedSampleName,
189
+ };
@@ -20,6 +20,15 @@ const FLAG_DEFS = {
20
20
  };
21
21
 
22
22
  const ANALYSIS_STATUS = "SOURCE_INTAKE_READY_FOR_AGENT_ANALYSIS";
23
+ const DESIGN_HANDOFF_ARTIFACTS = [
24
+ "docs/design/SCREEN_INVENTORY_<KEY>.md",
25
+ "docs/design/SCREEN_INVENTORY_<KEY>.json",
26
+ "docs/design/USER_FLOWS_<KEY>.md",
27
+ "docs/design/USER_FLOWS_<KEY>.json",
28
+ "docs/specs/DOMAIN_MODEL_<KEY>.md",
29
+ "docs/specs/DOMAIN_MODEL_<KEY>.json",
30
+ "docs/design/SPEC_TO_DESIGN_HANDOFF_<KEY>.json",
31
+ ];
23
32
 
24
33
  const MOJIBAKE_MARKERS = [
25
34
  "テ",
@@ -155,9 +164,59 @@ function taskHeader(featureKey, featureName, sourceInfo) {
155
164
  `Feature name: ${featureName}`,
156
165
  `Preserved source: ${sourceInfo.preservedCopyRelativePath}`,
157
166
  `Source SHA-256: ${sourceInfo.sha256}`,
167
+ "Inputs allowed: requirements/**. Forbidden: docs/ui/**",
158
168
  ].join("\n");
159
169
  }
160
170
 
171
+ function artifactListForFeature(featureKey) {
172
+ return DESIGN_HANDOFF_ARTIFACTS.map((item) => item.replace("<KEY>", featureKey));
173
+ }
174
+
175
+ function buildInputBoundarySection() {
176
+ return [
177
+ "## Input Boundary",
178
+ "",
179
+ "Inputs allowed: requirements/**. Forbidden: docs/ui/**",
180
+ "",
181
+ "- Allowed before handoff completion: requirement source files, the preserved `.sdtk/spec/requirements/sources/**` copy, and generated SPEC scaffold artifacts under `docs/**`.",
182
+ "- Forbidden before handoff completion: `docs/ui/**`, reference sample HTML/PNG exports, copied sample layouts, and visual benchmark outputs.",
183
+ "- `docs/ui/**` is evaluation-only after SDTK output generation/handoff; it must not be used as a generation input.",
184
+ "- If worker history shows reads under `docs/ui/**` before `SPEC_TO_DESIGN_HANDOFF` is complete, the lane validator must report `PROVENANCE_VIOLATION`.",
185
+ "",
186
+ ];
187
+ }
188
+
189
+ function buildRequiredDesignHandoffSection(featureKey) {
190
+ return [
191
+ "## Required Design Handoff Artifacts",
192
+ "",
193
+ ...artifactListForFeature(featureKey).map((item) => `- \`${item}\``),
194
+ "",
195
+ ];
196
+ }
197
+
198
+ function buildProvenanceContractSection(featureKey) {
199
+ return [
200
+ "## SPEC To DESIGN Provenance Contract",
201
+ "",
202
+ `Every screen in \`docs/design/SPEC_TO_DESIGN_HANDOFF_${featureKey}.json\` must include:`,
203
+ "",
204
+ "- `id`",
205
+ "- `title`",
206
+ "- `source_quote`: at least 30 Unicode code points copied literally from the requirement source",
207
+ "- `source_line_range`: `[start_line, end_line]` using 1-based source line numbers",
208
+ "",
209
+ "Deterministic verifier requirements:",
210
+ "",
211
+ "- `source_quote` must exist literally in the requirement source after UTF-8 BOM stripping and CRLF-to-LF newline normalization.",
212
+ "- `source_line_range` must be inside the source file bounds and must contain the quoted text.",
213
+ "- Screen `id` and `title` must not match an explicit post-generation sample filename/name list supplied to the verifier.",
214
+ "- Missing screens, malformed JSON, missing fields, invalid ranges, or copied sample-derived names must fail as `PROVENANCE_VIOLATION`.",
215
+ "- Runtime must not infer screens, fields, routes, palette, or ecommerce semantics from the raw requirement text.",
216
+ "",
217
+ ];
218
+ }
219
+
161
220
  function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
162
221
  const header = taskHeader(featureKey, featureName, sourceInfo);
163
222
  return {
@@ -170,6 +229,9 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
170
229
  "",
171
230
  "This packet routes source analysis to the SDTK-SPEC agent pipeline. Runtime `analyze` preserved the source and bootstrapped the standard 17-file scaffold; agents now perform the actual PM/BA/ARCH/DEV/QA analysis.",
172
231
  "",
232
+ ...buildInputBoundarySection(),
233
+ ...buildRequiredDesignHandoffSection(featureKey),
234
+ ...buildProvenanceContractSection(featureKey),
173
235
  "## Required Agent Sequence",
174
236
  "",
175
237
  "1. PM/BA: read the preserved source and `docs/discovery/REQUIREMENT_<KEY>.md`.",
@@ -181,6 +243,7 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
181
243
  "## Guardrails",
182
244
  "",
183
245
  "- Do not invent screens, fields, APIs, data models, visual constraints, or acceptance criteria without source evidence or user confirmation.",
246
+ "- Do not read `docs/ui/**` or sample layout exports before `SPEC_TO_DESIGN_HANDOFF` is complete.",
184
247
  "- Cite source lines or sections where possible.",
185
248
  "- Mark low-confidence or missing details as open questions.",
186
249
  "",
@@ -190,6 +253,7 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
190
253
  "",
191
254
  header,
192
255
  "",
256
+ ...buildInputBoundarySection(),
193
257
  "## PM Tasks",
194
258
  "",
195
259
  "- Read the preserved source and intake requirement artifact.",
@@ -209,6 +273,9 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
209
273
  "",
210
274
  header,
211
275
  "",
276
+ ...buildInputBoundarySection(),
277
+ ...buildRequiredDesignHandoffSection(featureKey),
278
+ ...buildProvenanceContractSection(featureKey),
212
279
  "## ARCH Tasks",
213
280
  "",
214
281
  "- Extract screens, user flows, domain entities, data model, APIs, and design constraints from the preserved source and BA artifact.",
@@ -217,6 +284,7 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
217
284
  "- Fill `docs/database/DATABASE_SPEC_<KEY>.md`.",
218
285
  "- Fill `docs/api/<Pascal>_API.yaml`, `docs/api/<KEY>_ENDPOINTS.md`, `docs/api/<KEY>_API_DESIGN_DETAIL.md`, and `docs/api/<snake>_api_flow_list.txt` when API scope is source-backed.",
219
286
  "- Fill `docs/design/DESIGN_LAYOUT_<KEY>.md` only with source-backed layout constraints or explicit decisions.",
287
+ "- Fill `docs/design/SPEC_TO_DESIGN_HANDOFF_<KEY>.json` only from explicit source evidence and include `source_quote` plus `source_line_range` for every screen.",
220
288
  "",
221
289
  "## Evidence Rule",
222
290
  "",
@@ -228,6 +296,7 @@ function buildTaskPacketFiles(featureKey, featureName, sourceInfo) {
228
296
  "",
229
297
  header,
230
298
  "",
299
+ ...buildInputBoundarySection(),
231
300
  "## DEV Planning Tasks",
232
301
  "",
233
302
  "- Fill `docs/dev/FEATURE_IMPL_PLAN_<KEY>.md` after PM/BA/ARCH artifacts are reviewed.",
@@ -281,6 +350,13 @@ function buildState(featureKey, featureName, sourceInfo, diagnostics, scaffold,
281
350
  analysisStatus: ANALYSIS_STATUS,
282
351
  artifacts,
283
352
  taskPacket,
353
+ designHandoffContract: {
354
+ requiredArtifacts: artifactListForFeature(featureKey).map((item) => item.replace(/\\/g, "/")),
355
+ requiredScreenFields: ["id", "title", "source_quote", "source_line_range"],
356
+ sourceQuoteMinimumCodePoints: 30,
357
+ forbiddenInputPattern: "docs/ui/**",
358
+ violationCode: "PROVENANCE_VIOLATION",
359
+ },
284
360
  nextRecommendedAction:
285
361
  `Open ${taskPacket.files["README.md"]} and run the PM/BA/ARCH/DEV/QA agent workflow against the preserved source.`,
286
362
  };